Skip to content

Commit c2a879e

Browse files
lukel97fhahn
andauthored
[VPlan] Fix VPTypeAnalysis cache clobbering in EVL transform (#120252)
When building SPEC CPU 2017 with RISC-V and EVL tail folding, this assertion in VPTypeAnalysis would trigger during the transformation to EVL recipes: https://github.com/llvm/llvm-project/blob/d8a0709b1090350a7fe3604d8ab78c7d62f10698/llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp#L135-L142 It was caused by this recipe: ``` WIDEN ir<%shr> = vp.or ir<%add33>, ir<0>, vp<%6> ``` Having its type inferred as i16, when ir<%add33> and ir<0> had inferred types of i32 somehow. The cause of this turned out to be because the VPTypeAnalysis cache was getting clobbered: In this transform we were erasing recipes but keeping around the same mapping from VPValue* to Type*. In the meantime, new recipes would be created which would have the same address as the old value. They would then incorrectly get the old erased VPValue*'s cached type: ``` --- before --- 0x600001ec5030: WIDEN ir<%mul21.neg> = vp.mul vp<%11>, ir<0>, vp<%6> 0x600001ec5450: <badref> <- some value that was erased --- after --- 0x600001ec5030: WIDEN ir<%mul21.neg> = vp.mul vp<%11>, ir<0>, vp<%6> 0x600001ec5450: WIDEN ir<%shr> = vp.or ir<%add33>, ir<0>, vp<%6> <- a new value that happens to have the same address ``` This fixes this by deferring the erasing of recipes till after the transformation. The test case might be a bit flakey since it just happens to have the right conditions to recreate this. I tried to add an assert in inferScalarType that every VPValue in the cache was valid, but couldn't find a way of telling if a VPValue had been erased. --------- Co-authored-by: Florian Hahn <[email protected]>
1 parent 4a7f60d commit c2a879e

File tree

2 files changed

+133
-2
lines changed

2 files changed

+133
-2
lines changed

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,8 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) {
14551455
R->setOperand(1, &EVL);
14561456
}
14571457

1458+
SmallVector<VPRecipeBase *> ToErase;
1459+
14581460
for (VPValue *HeaderMask : collectAllHeaderMasks(Plan)) {
14591461
for (VPUser *U : collectUsersRecursively(HeaderMask)) {
14601462
auto *CurRecipe = cast<VPRecipeBase>(U);
@@ -1564,9 +1566,17 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) {
15641566
VPValue *CurVPV = CurRecipe->getVPSingleValue();
15651567
CurVPV->replaceAllUsesWith(NewRecipe->getVPSingleValue());
15661568
}
1567-
CurRecipe->eraseFromParent();
1569+
// Defer erasing recipes till the end so that we don't invalidate the
1570+
// VPTypeAnalysis cache.
1571+
ToErase.push_back(CurRecipe);
15681572
}
1569-
recursivelyDeleteDeadRecipes(HeaderMask);
1573+
}
1574+
1575+
for (VPRecipeBase *R : reverse(ToErase)) {
1576+
SmallVector<VPValue *> PossiblyDead(R->operands());
1577+
R->eraseFromParent();
1578+
for (VPValue *Op : PossiblyDead)
1579+
recursivelyDeleteDeadRecipes(Op);
15701580
}
15711581
}
15721582

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -passes=loop-vectorize -force-tail-folding-style=data-with-evl -prefer-predicate-over-epilogue=predicate-dont-vectorize -mtriple=riscv64 -mattr=+v -S %s | FileCheck %s
3+
4+
; This test tries to recreate the conditions for a crash that occurred when the
5+
; VPTypeAnalysis cache wasn't cleared after a recipe was erased and clobbered
6+
; with a new one.
7+
8+
define void @type_info_cache_clobber(ptr %dstv, ptr %src, i64 %wide.trip.count) {
9+
; CHECK-LABEL: define void @type_info_cache_clobber(
10+
; CHECK-SAME: ptr [[DSTV:%.*]], ptr [[SRC:%.*]], i64 [[WIDE_TRIP_COUNT:%.*]]) #[[ATTR0:[0-9]+]] {
11+
; CHECK-NEXT: [[ENTRY:.*]]:
12+
; CHECK-NEXT: [[TMP0:%.*]] = add i64 [[WIDE_TRIP_COUNT]], 1
13+
; CHECK-NEXT: [[TMP1:%.*]] = sub i64 -1, [[TMP0]]
14+
; CHECK-NEXT: [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
15+
; CHECK-NEXT: [[TMP3:%.*]] = mul i64 [[TMP2]], 8
16+
; CHECK-NEXT: [[TMP4:%.*]] = icmp ult i64 [[TMP1]], [[TMP3]]
17+
; CHECK-NEXT: br i1 [[TMP4]], label %[[SCALAR_PH:.*]], label %[[VECTOR_MEMCHECK:.*]]
18+
; CHECK: [[VECTOR_MEMCHECK]]:
19+
; CHECK-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr [[DSTV]], i64 1
20+
; CHECK-NEXT: [[TMP5:%.*]] = add i64 [[WIDE_TRIP_COUNT]], 1
21+
; CHECK-NEXT: [[SCEVGEP1:%.*]] = getelementptr i8, ptr [[SRC]], i64 [[TMP5]]
22+
; CHECK-NEXT: [[BOUND0:%.*]] = icmp ult ptr [[DSTV]], [[SCEVGEP1]]
23+
; CHECK-NEXT: [[BOUND1:%.*]] = icmp ult ptr [[SRC]], [[SCEVGEP]]
24+
; CHECK-NEXT: [[FOUND_CONFLICT:%.*]] = and i1 [[BOUND0]], [[BOUND1]]
25+
; CHECK-NEXT: br i1 [[FOUND_CONFLICT]], label %[[SCALAR_PH]], label %[[VECTOR_PH:.*]]
26+
; CHECK: [[VECTOR_PH]]:
27+
; CHECK-NEXT: [[TMP6:%.*]] = call i64 @llvm.vscale.i64()
28+
; CHECK-NEXT: [[TMP7:%.*]] = mul i64 [[TMP6]], 8
29+
; CHECK-NEXT: [[TMP8:%.*]] = sub i64 [[TMP7]], 1
30+
; CHECK-NEXT: [[N_RND_UP:%.*]] = add i64 [[TMP0]], [[TMP8]]
31+
; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i64 [[N_RND_UP]], [[TMP7]]
32+
; CHECK-NEXT: [[N_VEC:%.*]] = sub i64 [[N_RND_UP]], [[N_MOD_VF]]
33+
; CHECK-NEXT: [[TMP9:%.*]] = call i64 @llvm.vscale.i64()
34+
; CHECK-NEXT: [[TMP10:%.*]] = mul i64 [[TMP9]], 8
35+
; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 8 x ptr> poison, ptr [[DSTV]], i64 0
36+
; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 8 x ptr> [[BROADCAST_SPLATINSERT]], <vscale x 8 x ptr> poison, <vscale x 8 x i32> zeroinitializer
37+
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
38+
; CHECK: [[VECTOR_BODY]]:
39+
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
40+
; CHECK-NEXT: [[EVL_BASED_IV:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_EVL_NEXT:%.*]], %[[VECTOR_BODY]] ]
41+
; CHECK-NEXT: [[AVL:%.*]] = sub i64 [[TMP0]], [[EVL_BASED_IV]]
42+
; CHECK-NEXT: [[TMP11:%.*]] = call i32 @llvm.experimental.get.vector.length.i64(i64 [[AVL]], i32 8, i1 true)
43+
; CHECK-NEXT: [[TMP12:%.*]] = add i64 [[EVL_BASED_IV]], 0
44+
; CHECK-NEXT: [[TMP13:%.*]] = getelementptr i8, ptr [[SRC]], i64 [[TMP12]]
45+
; CHECK-NEXT: [[TMP14:%.*]] = getelementptr i8, ptr [[TMP13]], i32 0
46+
; CHECK-NEXT: [[VP_OP_LOAD:%.*]] = call <vscale x 8 x i8> @llvm.vp.load.nxv8i8.p0(ptr align 1 [[TMP14]], <vscale x 8 x i1> splat (i1 true), i32 [[TMP11]]), !alias.scope [[META0:![0-9]+]]
47+
; CHECK-NEXT: [[TMP15:%.*]] = call <vscale x 8 x i32> @llvm.vp.zext.nxv8i32.nxv8i8(<vscale x 8 x i8> [[VP_OP_LOAD]], <vscale x 8 x i1> splat (i1 true), i32 [[TMP11]])
48+
; CHECK-NEXT: [[VP_OP:%.*]] = call <vscale x 8 x i32> @llvm.vp.mul.nxv8i32(<vscale x 8 x i32> [[TMP15]], <vscale x 8 x i32> zeroinitializer, <vscale x 8 x i1> splat (i1 true), i32 [[TMP11]])
49+
; CHECK-NEXT: [[VP_OP2:%.*]] = call <vscale x 8 x i32> @llvm.vp.ashr.nxv8i32(<vscale x 8 x i32> [[TMP15]], <vscale x 8 x i32> zeroinitializer, <vscale x 8 x i1> splat (i1 true), i32 [[TMP11]])
50+
; CHECK-NEXT: [[VP_OP3:%.*]] = call <vscale x 8 x i32> @llvm.vp.or.nxv8i32(<vscale x 8 x i32> [[VP_OP2]], <vscale x 8 x i32> zeroinitializer, <vscale x 8 x i1> splat (i1 true), i32 [[TMP11]])
51+
; CHECK-NEXT: [[TMP16:%.*]] = icmp ult <vscale x 8 x i32> [[TMP15]], zeroinitializer
52+
; CHECK-NEXT: [[TMP17:%.*]] = call <vscale x 8 x i32> @llvm.vp.select.nxv8i32(<vscale x 8 x i1> [[TMP16]], <vscale x 8 x i32> [[VP_OP3]], <vscale x 8 x i32> zeroinitializer, i32 [[TMP11]])
53+
; CHECK-NEXT: [[TMP18:%.*]] = call <vscale x 8 x i8> @llvm.vp.trunc.nxv8i8.nxv8i32(<vscale x 8 x i32> [[TMP17]], <vscale x 8 x i1> splat (i1 true), i32 [[TMP11]])
54+
; CHECK-NEXT: call void @llvm.vp.scatter.nxv8i8.nxv8p0(<vscale x 8 x i8> [[TMP18]], <vscale x 8 x ptr> align 1 [[BROADCAST_SPLAT]], <vscale x 8 x i1> splat (i1 true), i32 [[TMP11]]), !alias.scope [[META3:![0-9]+]], !noalias [[META0]]
55+
; CHECK-NEXT: [[TMP19:%.*]] = call <vscale x 8 x i16> @llvm.vp.trunc.nxv8i16.nxv8i32(<vscale x 8 x i32> [[VP_OP]], <vscale x 8 x i1> splat (i1 true), i32 [[TMP11]])
56+
; CHECK-NEXT: call void @llvm.vp.scatter.nxv8i16.nxv8p0(<vscale x 8 x i16> [[TMP19]], <vscale x 8 x ptr> align 2 zeroinitializer, <vscale x 8 x i1> splat (i1 true), i32 [[TMP11]])
57+
; CHECK-NEXT: [[TMP20:%.*]] = zext i32 [[TMP11]] to i64
58+
; CHECK-NEXT: [[INDEX_EVL_NEXT]] = add i64 [[TMP20]], [[EVL_BASED_IV]]
59+
; CHECK-NEXT: [[INDEX_NEXT]] = add i64 [[INDEX]], [[TMP10]]
60+
; CHECK-NEXT: [[TMP21:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
61+
; CHECK-NEXT: br i1 [[TMP21]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP5:![0-9]+]]
62+
; CHECK: [[MIDDLE_BLOCK]]:
63+
; CHECK-NEXT: br i1 true, label %[[EXIT:.*]], label %[[SCALAR_PH]]
64+
; CHECK: [[SCALAR_PH]]:
65+
; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ], [ 0, %[[VECTOR_MEMCHECK]] ]
66+
; CHECK-NEXT: br label %[[LOOP:.*]]
67+
; CHECK: [[LOOP]]:
68+
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
69+
; CHECK-NEXT: [[ARRAYIDX13:%.*]] = getelementptr i8, ptr [[SRC]], i64 [[IV]]
70+
; CHECK-NEXT: [[TMP22:%.*]] = load i8, ptr [[ARRAYIDX13]], align 1
71+
; CHECK-NEXT: [[CONV14:%.*]] = zext i8 [[TMP22]] to i32
72+
; CHECK-NEXT: [[MUL21_NEG:%.*]] = mul i32 [[CONV14]], 0
73+
; CHECK-NEXT: [[ADD33:%.*]] = ashr i32 [[CONV14]], 0
74+
; CHECK-NEXT: [[SHR:%.*]] = or i32 [[ADD33]], 0
75+
; CHECK-NEXT: [[TOBOOL_NOT_I:%.*]] = icmp ult i32 [[CONV14]], 0
76+
; CHECK-NEXT: [[COND_I:%.*]] = select i1 [[TOBOOL_NOT_I]], i32 [[SHR]], i32 0
77+
; CHECK-NEXT: [[CONV_I:%.*]] = trunc i32 [[COND_I]] to i8
78+
; CHECK-NEXT: store i8 [[CONV_I]], ptr [[DSTV]], align 1
79+
; CHECK-NEXT: [[CONV36:%.*]] = trunc i32 [[MUL21_NEG]] to i16
80+
; CHECK-NEXT: store i16 [[CONV36]], ptr null, align 2
81+
; CHECK-NEXT: [[IV_NEXT]] = add i64 [[IV]], 1
82+
; CHECK-NEXT: [[EC:%.*]] = icmp eq i64 [[IV]], [[WIDE_TRIP_COUNT]]
83+
; CHECK-NEXT: br i1 [[EC]], label %[[EXIT]], label %[[LOOP]], !llvm.loop [[LOOP8:![0-9]+]]
84+
; CHECK: [[EXIT]]:
85+
; CHECK-NEXT: ret void
86+
;
87+
entry:
88+
br label %loop
89+
90+
loop:
91+
%iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
92+
%arrayidx13 = getelementptr i8, ptr %src, i64 %iv
93+
%0 = load i8, ptr %arrayidx13, align 1
94+
%conv14 = zext i8 %0 to i32
95+
%mul21.neg = mul i32 %conv14, 0
96+
%add33 = ashr i32 %conv14, 0
97+
%shr = or i32 %add33, 0
98+
%tobool.not.i = icmp ult i32 %conv14, 0
99+
%cond.i = select i1 %tobool.not.i, i32 %shr, i32 0
100+
%conv.i = trunc i32 %cond.i to i8
101+
store i8 %conv.i, ptr %dstv, align 1
102+
%conv36 = trunc i32 %mul21.neg to i16
103+
store i16 %conv36, ptr null, align 2
104+
%iv.next = add i64 %iv, 1
105+
%ec = icmp eq i64 %iv, %wide.trip.count
106+
br i1 %ec, label %exit, label %loop
107+
108+
exit:
109+
ret void
110+
}
111+
;.
112+
; CHECK: [[META0]] = !{[[META1:![0-9]+]]}
113+
; CHECK: [[META1]] = distinct !{[[META1]], [[META2:![0-9]+]]}
114+
; CHECK: [[META2]] = distinct !{[[META2]], !"LVerDomain"}
115+
; CHECK: [[META3]] = !{[[META4:![0-9]+]]}
116+
; CHECK: [[META4]] = distinct !{[[META4]], [[META2]]}
117+
; CHECK: [[LOOP5]] = distinct !{[[LOOP5]], [[META6:![0-9]+]], [[META7:![0-9]+]]}
118+
; CHECK: [[META6]] = !{!"llvm.loop.isvectorized", i32 1}
119+
; CHECK: [[META7]] = !{!"llvm.loop.unroll.runtime.disable"}
120+
; CHECK: [[LOOP8]] = distinct !{[[LOOP8]], [[META6]]}
121+
;.

0 commit comments

Comments
 (0)