Skip to content

Commit 4984242

Browse files
authored
[LV] Fix MinBWs in WidenIntrinsic case (llvm#137005)
There is a bug in the computation and handling of MinBWs in the case of VPWidenIntrinsicRecipe: a crash is observed in VPlanTransforms::truncateToMinimalBitwidths due to a mismatch between the number of recipes processed and the number of entries in MinBWs. Fix handling of calls in llvm::computeMinimumValueSizes, and handle VPWidenIntrinsicRecipe in truncateToMinimalBitwidths, fixing the bug. Fixes llvm#87407.
1 parent 980d027 commit 4984242

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

llvm/lib/Analysis/VectorUtils.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,11 @@ llvm::computeMinimumValueSizes(ArrayRef<BasicBlock *> Blocks, DemandedBits &DB,
827827
if (isa<PHINode>(I))
828828
continue;
829829

830+
// Don't modify the types of operands of a call, as doing that would cause a
831+
// signature mismatch.
832+
if (isa<CallBase>(I))
833+
continue;
834+
830835
if (DBits[Leader] == ~0ULL)
831836
// All bits demanded, no point continuing.
832837
continue;
@@ -882,7 +887,9 @@ llvm::computeMinimumValueSizes(ArrayRef<BasicBlock *> Blocks, DemandedBits &DB,
882887

883888
// If any of M's operands demand more bits than MinBW then M cannot be
884889
// performed safely in MinBW.
885-
if (any_of(MI->operands(), [&DB, MinBW](Use &U) {
890+
auto *Call = dyn_cast<CallBase>(MI);
891+
auto Ops = Call ? Call->args() : MI->operands();
892+
if (any_of(Ops, [&DB, MinBW](Use &U) {
886893
auto *CI = dyn_cast<ConstantInt>(U);
887894
// For constants shift amounts, check if the shift would result in
888895
// poison.

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,7 +1639,8 @@ void VPlanTransforms::truncateToMinimalBitwidths(
16391639
vp_depth_first_deep(Plan.getVectorLoopRegion()))) {
16401640
for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
16411641
if (!isa<VPWidenRecipe, VPWidenCastRecipe, VPReplicateRecipe,
1642-
VPWidenSelectRecipe, VPWidenLoadRecipe>(&R))
1642+
VPWidenSelectRecipe, VPWidenLoadRecipe, VPWidenIntrinsicRecipe>(
1643+
&R))
16431644
continue;
16441645

16451646
VPValue *ResultVPV = R.getVPSingleValue();
@@ -1712,7 +1713,7 @@ void VPlanTransforms::truncateToMinimalBitwidths(
17121713
}
17131714

17141715
assert(!isa<VPWidenStoreRecipe>(&R) && "stores cannot be narrowed");
1715-
if (isa<VPWidenLoadRecipe>(&R))
1716+
if (isa<VPWidenLoadRecipe, VPWidenIntrinsicRecipe>(&R))
17161717
continue;
17171718

17181719
// Shrink operands by introducing truncates as needed.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals none --version 5
2+
; RUN: opt -passes=loop-vectorize -force-vector-width=4 -S %s | FileCheck %s
3+
4+
define i8 @pr87407(i8 %x, i64 %y, i64 %n) {
5+
; CHECK-LABEL: define i8 @pr87407(
6+
; CHECK-SAME: i8 [[X:%.*]], i64 [[Y:%.*]], i64 [[N:%.*]]) {
7+
; CHECK-NEXT: [[ENTRY:.*]]:
8+
; CHECK-NEXT: [[ZEXT_X:%.*]] = zext i8 [[X]] to i64
9+
; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], 4
10+
; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
11+
; CHECK: [[VECTOR_PH]]:
12+
; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i64 [[N]], 4
13+
; CHECK-NEXT: [[N_VEC:%.*]] = sub i64 [[N]], [[N_MOD_VF]]
14+
; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i64> poison, i64 [[Y]], i64 0
15+
; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i64> [[BROADCAST_SPLATINSERT]], <4 x i64> poison, <4 x i32> zeroinitializer
16+
; CHECK-NEXT: [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <4 x i64> poison, i64 [[ZEXT_X]], i64 0
17+
; CHECK-NEXT: [[BROADCAST_SPLAT2:%.*]] = shufflevector <4 x i64> [[BROADCAST_SPLATINSERT1]], <4 x i64> poison, <4 x i32> zeroinitializer
18+
; CHECK-NEXT: [[TMP0:%.*]] = call <4 x i64> @llvm.umax.v4i64(<4 x i64> [[BROADCAST_SPLAT2]], <4 x i64> [[BROADCAST_SPLAT]])
19+
; CHECK-NEXT: [[TMP1:%.*]] = trunc <4 x i64> [[TMP0]] to <4 x i1>
20+
; CHECK-NEXT: [[TMP2:%.*]] = icmp ne <4 x i1> [[TMP1]], zeroinitializer
21+
; CHECK-NEXT: [[TMP3:%.*]] = zext <4 x i1> [[TMP2]] to <4 x i32>
22+
; CHECK-NEXT: [[TMP4:%.*]] = shl <4 x i32> [[TMP3]], splat (i32 8)
23+
; CHECK-NEXT: [[TMP5:%.*]] = trunc <4 x i32> [[TMP4]] to <4 x i8>
24+
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
25+
; CHECK: [[VECTOR_BODY]]:
26+
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
27+
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
28+
; CHECK-NEXT: [[TMP6:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
29+
; CHECK-NEXT: br i1 [[TMP6]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
30+
; CHECK: [[MIDDLE_BLOCK]]:
31+
; CHECK-NEXT: [[TMP7:%.*]] = extractelement <4 x i8> [[TMP5]], i32 3
32+
; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
33+
; CHECK-NEXT: br i1 [[CMP_N]], label %[[EXIT:.*]], label %[[SCALAR_PH]]
34+
; CHECK: [[SCALAR_PH]]:
35+
; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
36+
; CHECK-NEXT: br label %[[LOOP:.*]]
37+
; CHECK: [[LOOP]]:
38+
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ [[IV_NEXT:%.*]], %[[LOOP]] ], [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ]
39+
; CHECK-NEXT: [[MAX:%.*]] = tail call i64 @llvm.umax.i64(i64 [[ZEXT_X]], i64 [[Y]])
40+
; CHECK-NEXT: [[CMP_MAX_0:%.*]] = icmp ne i64 [[MAX]], 0
41+
; CHECK-NEXT: [[ZEXT_CMP:%.*]] = zext i1 [[CMP_MAX_0]] to i64
42+
; CHECK-NEXT: [[TRUNC:%.*]] = trunc i64 [[ZEXT_CMP]] to i32
43+
; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[TRUNC]], 8
44+
; CHECK-NEXT: [[RES:%.*]] = trunc i32 [[SHL]] to i8
45+
; CHECK-NEXT: [[IV_NEXT]] = add i64 [[IV]], 1
46+
; CHECK-NEXT: [[EXIT_COND:%.*]] = icmp ne i64 [[IV_NEXT]], [[N]]
47+
; CHECK-NEXT: br i1 [[EXIT_COND]], label %[[LOOP]], label %[[EXIT]], !llvm.loop [[LOOP3:![0-9]+]]
48+
; CHECK: [[EXIT]]:
49+
; CHECK-NEXT: [[RES_LCSSA:%.*]] = phi i8 [ [[RES]], %[[LOOP]] ], [ [[TMP7]], %[[MIDDLE_BLOCK]] ]
50+
; CHECK-NEXT: ret i8 [[RES_LCSSA]]
51+
;
52+
entry:
53+
%zext.x = zext i8 %x to i64
54+
br label %loop
55+
56+
loop:
57+
%iv = phi i64 [ %iv.next, %loop ], [ 0, %entry ]
58+
%max = tail call i64 @llvm.umax.i64(i64 %zext.x, i64 %y)
59+
%cmp.max.0 = icmp ne i64 %max, 0
60+
%zext.cmp = zext i1 %cmp.max.0 to i64
61+
%trunc = trunc i64 %zext.cmp to i32
62+
%shl = shl i32 %trunc, 8
63+
%res = trunc i32 %shl to i8
64+
%iv.next = add i64 %iv, 1
65+
%exit.cond = icmp ne i64 %iv.next, %n
66+
br i1 %exit.cond, label %loop, label %exit
67+
68+
exit:
69+
ret i8 %res
70+
}

0 commit comments

Comments
 (0)