Skip to content

Commit 572d2fd

Browse files
authored
[Attributor] Fix an issue that could potentially cause AccessList and OffsetBins out of sync (#106187)
The implementation of `AAPointerInfo::RangeList::set_difference` doesn't consider the case where two ranges have the same offset but different sizes. This could cause `AccessList` and `OffsetBins` out of sync because a range has been already updated in `AccessList` but missing in `ToRemove`. I do have a reproducer but the reproducer itself is 248kb. `llvm-reduce` can't further reduce it. Not sure how I can make a smaller reproducer. Fixes: SWDEV-479757.
1 parent 051054e commit 572d2fd

File tree

2 files changed

+108
-8
lines changed

2 files changed

+108
-8
lines changed

llvm/include/llvm/Transforms/IPO/Attributor.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,16 @@ struct RangeTy {
294294
return *this;
295295
}
296296

297-
/// Comparison for sorting ranges by offset.
297+
/// Comparison for sorting ranges.
298298
///
299-
/// Returns true if the offset \p L is less than that of \p R.
300-
inline static bool OffsetLessThan(const RangeTy &L, const RangeTy &R) {
301-
return L.Offset < R.Offset;
299+
/// Returns true if the offset of \p L is less than that of \p R. If the two
300+
/// offsets are same, compare the sizes instead.
301+
inline static bool LessThan(const RangeTy &L, const RangeTy &R) {
302+
if (L.Offset < R.Offset)
303+
return true;
304+
if (L.Offset == R.Offset)
305+
return L.Size < R.Size;
306+
return false;
302307
}
303308

304309
/// Constants used to represent special offsets or sizes.
@@ -5809,7 +5814,7 @@ struct AAPointerInfo : public AbstractAttribute {
58095814
// Helpers required for std::set_difference
58105815
using value_type = RangeTy;
58115816
void push_back(const RangeTy &R) {
5812-
assert((Ranges.empty() || RangeTy::OffsetLessThan(Ranges.back(), R)) &&
5817+
assert((Ranges.empty() || RangeTy::LessThan(Ranges.back(), R)) &&
58135818
"Ensure the last element is the greatest.");
58145819
Ranges.push_back(R);
58155820
}
@@ -5818,7 +5823,7 @@ struct AAPointerInfo : public AbstractAttribute {
58185823
static void set_difference(const RangeList &L, const RangeList &R,
58195824
RangeList &D) {
58205825
std::set_difference(L.begin(), L.end(), R.begin(), R.end(),
5821-
std::back_inserter(D), RangeTy::OffsetLessThan);
5826+
std::back_inserter(D), RangeTy::LessThan);
58225827
}
58235828

58245829
unsigned size() const { return Ranges.size(); }
@@ -5856,7 +5861,7 @@ struct AAPointerInfo : public AbstractAttribute {
58565861

58575862
/// Insert \p R at the given iterator \p Pos, and merge if necessary.
58585863
///
5859-
/// This assumes that all ranges before \p Pos are OffsetLessThan \p R, and
5864+
/// This assumes that all ranges before \p Pos are LessThan \p R, and
58605865
/// then maintains the sorted order for the suffix list.
58615866
///
58625867
/// \return The place of insertion and true iff anything changed.
@@ -5868,7 +5873,7 @@ struct AAPointerInfo : public AbstractAttribute {
58685873
}
58695874

58705875
// Maintain this as a sorted vector of unique entries.
5871-
auto LB = std::lower_bound(Pos, Ranges.end(), R, RangeTy::OffsetLessThan);
5876+
auto LB = std::lower_bound(Pos, Ranges.end(), R, RangeTy::LessThan);
58725877
if (LB == Ranges.end() || LB->Offset != R.Offset)
58735878
return std::make_pair(Ranges.insert(LB, R), true);
58745879
bool Changed = *LB != R;
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -passes='amdgpu-attributor' %s -o - | FileCheck %s
3+
4+
%struct.ShaderClosure = type { <3 x float>, i32, float, <3 x float>, [10 x float], [8 x i8] }
5+
%struct.ShaderData = type { <3 x float>, <3 x float>, <3 x float>, <3 x float>, i32, i32, i32, i32, i32, float, float, i32, i32, float, float, %struct.differential3, %struct.differential3, %struct.differential, %struct.differential, <3 x float>, <3 x float>, <3 x float>, %struct.differential3, i32, i32, i32, float, <3 x float>, <3 x float>, <3 x float>, [64 x %struct.ShaderClosure] }
6+
%struct.differential = type { float, float }
7+
%struct.differential3 = type { <3 x float>, <3 x float> }
8+
9+
define internal fastcc void @foo(ptr %kg) {
10+
; CHECK-LABEL: define internal fastcc void @foo(
11+
; CHECK-SAME: ptr [[KG:%.*]]) #[[ATTR0:[0-9]+]] {
12+
; CHECK-NEXT: [[ENTRY:.*:]]
13+
; CHECK-NEXT: [[CLOSURE_I25_I:%.*]] = getelementptr i8, ptr [[KG]], i64 336
14+
; CHECK-NEXT: [[NUM_CLOSURE_I26_I:%.*]] = getelementptr i8, ptr [[KG]], i64 276
15+
; CHECK-NEXT: br label %[[WHILE_COND:.*]]
16+
; CHECK: [[WHILE_COND]]:
17+
; CHECK-NEXT: [[TMP0:%.*]] = addrspacecast ptr [[KG]] to ptr addrspace(5)
18+
; CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr addrspace(5) [[TMP0]], align 4
19+
; CHECK-NEXT: [[IDXPROM_I:%.*]] = zext i32 [[TMP1]] to i64
20+
; CHECK-NEXT: switch i32 0, label %[[SW_BB92:.*]] [
21+
; CHECK-NEXT: i32 1, label %[[SW_BB92]]
22+
; CHECK-NEXT: i32 0, label %[[SUBD_TRIANGLE_PATCH_EXIT_I_I35:.*]]
23+
; CHECK-NEXT: ]
24+
; CHECK: [[SUBD_TRIANGLE_PATCH_EXIT_I_I35]]:
25+
; CHECK-NEXT: [[ARRAYIDX_I27_I:%.*]] = getelementptr float, ptr [[KG]], i64 [[IDXPROM_I]]
26+
; CHECK-NEXT: [[TMP2:%.*]] = addrspacecast ptr [[ARRAYIDX_I27_I]] to ptr addrspace(5)
27+
; CHECK-NEXT: store float 0.000000e+00, ptr addrspace(5) [[TMP2]], align 4
28+
; CHECK-NEXT: br label %[[WHILE_COND]]
29+
; CHECK: [[SW_BB92]]:
30+
; CHECK-NEXT: [[INSERT:%.*]] = insertelement <3 x i32> zeroinitializer, i32 [[TMP1]], i64 0
31+
; CHECK-NEXT: [[SPLAT_SPLATINSERT_I:%.*]] = bitcast <3 x i32> [[INSERT]] to <3 x float>
32+
; CHECK-NEXT: [[SHFL:%.*]] = shufflevector <3 x float> [[SPLAT_SPLATINSERT_I]], <3 x float> zeroinitializer, <4 x i32> zeroinitializer
33+
; CHECK-NEXT: [[TMP3:%.*]] = addrspacecast ptr [[NUM_CLOSURE_I26_I]] to ptr addrspace(5)
34+
; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr addrspace(5) [[TMP3]], align 4
35+
; CHECK-NEXT: [[IDXPROM_I27_I:%.*]] = sext i32 [[LOAD]] to i64
36+
; CHECK-NEXT: [[ARRAYIDX_I28_I:%.*]] = getelementptr [64 x %struct.ShaderClosure], ptr [[CLOSURE_I25_I]], i64 0, i64 [[IDXPROM_I27_I]]
37+
; CHECK-NEXT: [[TMP4:%.*]] = addrspacecast ptr [[ARRAYIDX_I28_I]] to ptr addrspace(5)
38+
; CHECK-NEXT: store <4 x float> [[SHFL]], ptr addrspace(5) [[TMP4]], align 16
39+
; CHECK-NEXT: [[INC_I30_I:%.*]] = or i32 [[LOAD]], 1
40+
; CHECK-NEXT: [[TMP5:%.*]] = addrspacecast ptr [[NUM_CLOSURE_I26_I]] to ptr addrspace(5)
41+
; CHECK-NEXT: store i32 [[INC_I30_I]], ptr addrspace(5) [[TMP5]], align 4
42+
; CHECK-NEXT: br label %[[WHILE_COND]]
43+
;
44+
entry:
45+
%closure.i25.i = getelementptr i8, ptr %kg, i64 336
46+
%num_closure.i26.i = getelementptr i8, ptr %kg, i64 276
47+
br label %while.cond
48+
49+
while.cond:
50+
%load = load i32, ptr %kg, align 4
51+
%idxprom.i = zext i32 %load to i64
52+
switch i32 0, label %sw.bb92 [
53+
i32 1, label %sw.bb92
54+
i32 0, label %subd_triangle_patch.exit.i.i35
55+
]
56+
57+
subd_triangle_patch.exit.i.i35:
58+
%arrayidx.i27.i = getelementptr float, ptr %kg, i64 %idxprom.i
59+
store float 0.000000e+00, ptr %arrayidx.i27.i, align 4
60+
br label %while.cond
61+
62+
sw.bb92:
63+
%insert = insertelement <3 x i32> zeroinitializer, i32 %load, i64 0
64+
%splat.splatinsert.i = bitcast <3 x i32> %insert to <3 x float>
65+
%shfl = shufflevector <3 x float> %splat.splatinsert.i, <3 x float> zeroinitializer, <4 x i32> zeroinitializer
66+
%load.1 = load i32, ptr %num_closure.i26.i, align 4
67+
%idxprom.i27.i = sext i32 %load.1 to i64
68+
%arrayidx.i28.i = getelementptr [64 x %struct.ShaderClosure], ptr %closure.i25.i, i64 0, i64 %idxprom.i27.i
69+
store <4 x float> %shfl, ptr %arrayidx.i28.i, align 16
70+
%inc.i30.i = or i32 %load.1, 1
71+
store i32 %inc.i30.i, ptr %num_closure.i26.i, align 4
72+
br label %while.cond
73+
}
74+
75+
define amdgpu_kernel void @kernel() #0 {
76+
; CHECK-LABEL: define amdgpu_kernel void @kernel(
77+
; CHECK-SAME: ) #[[ATTR1:[0-9]+]] {
78+
; CHECK-NEXT: [[ENTRY:.*:]]
79+
; CHECK-NEXT: [[SD:%.*]] = alloca [[STRUCT_SHADERDATA:%.*]], align 16, addrspace(5)
80+
; CHECK-NEXT: [[KGLOBALS_ASCAST1:%.*]] = addrspacecast ptr addrspace(5) [[SD]] to ptr
81+
; CHECK-NEXT: [[NUM_CLOSURE_I_I:%.*]] = getelementptr i8, ptr addrspace(5) [[SD]], i32 276
82+
; CHECK-NEXT: store <2 x i32> zeroinitializer, ptr addrspace(5) [[NUM_CLOSURE_I_I]], align 4
83+
; CHECK-NEXT: call fastcc void @foo(ptr [[KGLOBALS_ASCAST1]])
84+
; CHECK-NEXT: ret void
85+
;
86+
entry:
87+
%sd = alloca %struct.ShaderData, align 16, addrspace(5)
88+
%kglobals.ascast1 = addrspacecast ptr addrspace(5) %sd to ptr
89+
%num_closure.i.i = getelementptr i8, ptr addrspace(5) %sd, i32 276
90+
store <2 x i32> zeroinitializer, ptr addrspace(5) %num_closure.i.i, align 4
91+
call fastcc void @foo(ptr %kglobals.ascast1)
92+
ret void
93+
}
94+
95+
attributes #0 = { norecurse }

0 commit comments

Comments
 (0)