Skip to content

Commit 3a4a60d

Browse files
authored
[VectorCombine] Apply InstSimplify in scalarizeOpOrCmp to avoid infinite loop (#153069)
Fixes #153012 As we tolerate unfoldable constant expressions in `scalarizeOpOrCmp`, we may fold ```llvm define void @bug(ptr %ptr1, ptr %ptr2, i64 %idx) #0 { entry: %158 = insertelement <2 x i64> <i64 5, i64 ptrtoint (ptr @Val to i64)>, i64 %idx, i32 0 %159 = or disjoint <2 x i64> splat (i64 2), %158 store <2 x i64> %159, ptr %ptr2 ret void } ``` to ```llvm define void @bug(ptr %ptr1, ptr %ptr2, i64 %idx) { entry: %.scalar = or disjoint i64 2, %idx %0 = or <2 x i64> splat (i64 2), <i64 5, i64 ptrtoint (ptr @Val to i64)> %1 = insertelement <2 x i64> %0, i64 %.scalar, i64 0 store <2 x i64> %1, ptr %ptr2, align 16 ret void } ``` And it would be folded back in `foldInsExtBinop`, resulting in an infinite loop. This patch forces scalarization iff InstSimplify can fold the constant expression.
1 parent 1dc0005 commit 3a4a60d

File tree

4 files changed

+42
-41
lines changed

4 files changed

+42
-41
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class VectorCombine {
7878
const DataLayout *DL, TTI::TargetCostKind CostKind,
7979
bool TryEarlyFoldsOnly)
8080
: F(F), Builder(F.getContext(), InstSimplifyFolder(*DL)), TTI(TTI),
81-
DT(DT), AA(AA), AC(AC), DL(DL), CostKind(CostKind),
81+
DT(DT), AA(AA), AC(AC), DL(DL), CostKind(CostKind), SQ(*DL),
8282
TryEarlyFoldsOnly(TryEarlyFoldsOnly) {}
8383

8484
bool run();
@@ -92,6 +92,7 @@ class VectorCombine {
9292
AssumptionCache &AC;
9393
const DataLayout *DL;
9494
TTI::TargetCostKind CostKind;
95+
const SimplifyQuery SQ;
9596

9697
/// If true, only perform beneficial early IR transforms. Do not introduce new
9798
/// vector operations.
@@ -1238,24 +1239,26 @@ bool VectorCombine::scalarizeOpOrCmp(Instruction &I) {
12381239
// Fold the vector constants in the original vectors into a new base vector to
12391240
// get more accurate cost modelling.
12401241
Value *NewVecC = nullptr;
1241-
TargetFolder Folder(*DL);
12421242
if (CI)
1243-
NewVecC = Folder.FoldCmp(CI->getPredicate(), VecCs[0], VecCs[1]);
1243+
NewVecC = simplifyCmpInst(CI->getPredicate(), VecCs[0], VecCs[1], SQ);
12441244
else if (UO)
12451245
NewVecC =
1246-
Folder.FoldUnOpFMF(UO->getOpcode(), VecCs[0], UO->getFastMathFlags());
1246+
simplifyUnOp(UO->getOpcode(), VecCs[0], UO->getFastMathFlags(), SQ);
12471247
else if (BO)
1248-
NewVecC = Folder.FoldBinOp(BO->getOpcode(), VecCs[0], VecCs[1]);
1249-
else if (II->arg_size() == 2)
1250-
NewVecC = Folder.FoldBinaryIntrinsic(II->getIntrinsicID(), VecCs[0],
1251-
VecCs[1], II->getType(), &I);
1248+
NewVecC = simplifyBinOp(BO->getOpcode(), VecCs[0], VecCs[1], SQ);
1249+
else if (II)
1250+
NewVecC = simplifyCall(II, II->getCalledOperand(), VecCs, SQ);
1251+
1252+
if (!NewVecC)
1253+
return false;
12521254

12531255
// Get cost estimate for the insert element. This cost will factor into
12541256
// both sequences.
12551257
InstructionCost OldCost = VectorOpCost;
12561258
InstructionCost NewCost =
12571259
ScalarOpCost + TTI.getVectorInstrCost(Instruction::InsertElement, VecTy,
12581260
CostKind, *Index, NewVecC);
1261+
12591262
for (auto [Idx, Op, VecC, Scalar] : enumerate(Ops, VecCs, ScalarOps)) {
12601263
if (!Scalar || (II && isVectorIntrinsicWithScalarOpAtArg(
12611264
II->getIntrinsicID(), Idx, &TTI)))
@@ -1300,15 +1303,6 @@ bool VectorCombine::scalarizeOpOrCmp(Instruction &I) {
13001303
if (auto *ScalarInst = dyn_cast<Instruction>(Scalar))
13011304
ScalarInst->copyIRFlags(&I);
13021305

1303-
// Create a new base vector if the constant folding failed.
1304-
if (!NewVecC) {
1305-
if (CI)
1306-
NewVecC = Builder.CreateCmp(CI->getPredicate(), VecCs[0], VecCs[1]);
1307-
else if (UO || BO)
1308-
NewVecC = Builder.CreateNAryOp(Opcode, VecCs);
1309-
else
1310-
NewVecC = Builder.CreateIntrinsic(VecTy, II->getIntrinsicID(), VecCs);
1311-
}
13121306
Value *Insert = Builder.CreateInsertElement(NewVecC, Scalar, *Index);
13131307
replaceValue(I, *Insert);
13141308
return true;

llvm/test/Transforms/VectorCombine/X86/intrinsic-scalarize.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ define <2 x float> @maxnum(float %x, float %y) {
1313
; AVX2-LABEL: define <2 x float> @maxnum(
1414
; AVX2-SAME: float [[X:%.*]], float [[Y:%.*]]) #[[ATTR0:[0-9]+]] {
1515
; AVX2-NEXT: [[V_SCALAR:%.*]] = call float @llvm.maxnum.f32(float [[X]], float [[Y]])
16-
; AVX2-NEXT: [[TMP1:%.*]] = call <2 x float> @llvm.maxnum.v2f32(<2 x float> poison, <2 x float> poison)
17-
; AVX2-NEXT: [[V:%.*]] = insertelement <2 x float> [[TMP1]], float [[V_SCALAR]], i64 0
16+
; AVX2-NEXT: [[V:%.*]] = insertelement <2 x float> poison, float [[V_SCALAR]], i64 0
1817
; AVX2-NEXT: ret <2 x float> [[V]]
1918
;
2019
%x.insert = insertelement <2 x float> poison, float %x, i32 0

llvm/test/Transforms/VectorCombine/binop-scalarize.ll

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,22 @@ define <4 x i8> @udiv_ub(i8 %x, i8 %y) {
2020
%v = udiv <4 x i8> %x.insert, %y.insert
2121
ret <4 x i8> %v
2222
}
23+
24+
25+
; Unfoldable constant expression may cause infinite loop between
26+
; scalarizing insertelement and folding binop(insert(x,a,idx),insert(y,b,idx))
27+
@val = external hidden global ptr, align 8
28+
29+
define <2 x i64> @pr153012(i64 %idx) #0 {
30+
; CHECK-LABEL: define <2 x i64> @pr153012(
31+
; CHECK-SAME: i64 [[IDX:%.*]]) {
32+
; CHECK-NEXT: [[ENTRY:.*:]]
33+
; CHECK-NEXT: [[A:%.*]] = insertelement <2 x i64> <i64 5, i64 ptrtoint (ptr @val to i64)>, i64 [[IDX]], i32 0
34+
; CHECK-NEXT: [[B:%.*]] = or disjoint <2 x i64> splat (i64 2), [[A]]
35+
; CHECK-NEXT: ret <2 x i64> [[B]]
36+
;
37+
entry:
38+
%a = insertelement <2 x i64> <i64 5, i64 ptrtoint (ptr @val to i64)>, i64 %idx, i32 0
39+
%b = or disjoint <2 x i64> splat (i64 2), %a
40+
ret <2 x i64> %b
41+
}

llvm/test/Transforms/VectorCombine/intrinsic-scalarize.ll

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ define <4 x i32> @umax_fixed(i32 %x, i32 %y) {
55
; CHECK-LABEL: define <4 x i32> @umax_fixed(
66
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
77
; CHECK-NEXT: [[V_SCALAR:%.*]] = call i32 @llvm.umax.i32(i32 [[X]], i32 [[Y]])
8-
; CHECK-NEXT: [[TMP1:%.*]] = call <4 x i32> @llvm.umax.v4i32(<4 x i32> poison, <4 x i32> poison)
9-
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x i32> [[TMP1]], i32 [[V_SCALAR]], i64 0
8+
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x i32> poison, i32 [[V_SCALAR]], i64 0
109
; CHECK-NEXT: ret <4 x i32> [[V]]
1110
;
1211
%x.insert = insertelement <4 x i32> poison, i32 %x, i32 0
@@ -19,8 +18,7 @@ define <vscale x 4 x i32> @umax_scalable(i32 %x, i32 %y) {
1918
; CHECK-LABEL: define <vscale x 4 x i32> @umax_scalable(
2019
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
2120
; CHECK-NEXT: [[V_SCALAR:%.*]] = call i32 @llvm.umax.i32(i32 [[X]], i32 [[Y]])
22-
; CHECK-NEXT: [[TMP1:%.*]] = call <vscale x 4 x i32> @llvm.umax.nxv4i32(<vscale x 4 x i32> poison, <vscale x 4 x i32> poison)
23-
; CHECK-NEXT: [[V:%.*]] = insertelement <vscale x 4 x i32> [[TMP1]], i32 [[V_SCALAR]], i64 0
21+
; CHECK-NEXT: [[V:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[V_SCALAR]], i64 0
2422
; CHECK-NEXT: ret <vscale x 4 x i32> [[V]]
2523
;
2624
%x.insert = insertelement <vscale x 4 x i32> poison, i32 %x, i32 0
@@ -33,8 +31,7 @@ define <4 x i32> @umax_fixed_lhs_const(i32 %x) {
3331
; CHECK-LABEL: define <4 x i32> @umax_fixed_lhs_const(
3432
; CHECK-SAME: i32 [[X:%.*]]) {
3533
; CHECK-NEXT: [[V_SCALAR:%.*]] = call i32 @llvm.umax.i32(i32 1, i32 [[X]])
36-
; CHECK-NEXT: [[TMP1:%.*]] = call <4 x i32> @llvm.umax.v4i32(<4 x i32> <i32 1, i32 2, i32 3, i32 4>, <4 x i32> poison)
37-
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x i32> [[TMP1]], i32 [[V_SCALAR]], i64 0
34+
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x i32> poison, i32 [[V_SCALAR]], i64 0
3835
; CHECK-NEXT: ret <4 x i32> [[V]]
3936
;
4037
%x.insert = insertelement <4 x i32> poison, i32 %x, i32 0
@@ -46,8 +43,7 @@ define <4 x i32> @umax_fixed_rhs_const(i32 %x) {
4643
; CHECK-LABEL: define <4 x i32> @umax_fixed_rhs_const(
4744
; CHECK-SAME: i32 [[X:%.*]]) {
4845
; CHECK-NEXT: [[V_SCALAR:%.*]] = call i32 @llvm.umax.i32(i32 [[X]], i32 1)
49-
; CHECK-NEXT: [[TMP1:%.*]] = call <4 x i32> @llvm.umax.v4i32(<4 x i32> poison, <4 x i32> <i32 1, i32 2, i32 3, i32 4>)
50-
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x i32> [[TMP1]], i32 [[V_SCALAR]], i64 0
46+
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x i32> poison, i32 [[V_SCALAR]], i64 0
5147
; CHECK-NEXT: ret <4 x i32> [[V]]
5248
;
5349
%x.insert = insertelement <4 x i32> poison, i32 %x, i32 0
@@ -59,8 +55,7 @@ define <vscale x 4 x i32> @umax_scalable_lhs_const(i32 %x) {
5955
; CHECK-LABEL: define <vscale x 4 x i32> @umax_scalable_lhs_const(
6056
; CHECK-SAME: i32 [[X:%.*]]) {
6157
; CHECK-NEXT: [[V_SCALAR:%.*]] = call i32 @llvm.umax.i32(i32 42, i32 [[X]])
62-
; CHECK-NEXT: [[TMP1:%.*]] = call <vscale x 4 x i32> @llvm.umax.nxv4i32(<vscale x 4 x i32> splat (i32 42), <vscale x 4 x i32> poison)
63-
; CHECK-NEXT: [[V:%.*]] = insertelement <vscale x 4 x i32> [[TMP1]], i32 [[V_SCALAR]], i64 0
58+
; CHECK-NEXT: [[V:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[V_SCALAR]], i64 0
6459
; CHECK-NEXT: ret <vscale x 4 x i32> [[V]]
6560
;
6661
%x.insert = insertelement <vscale x 4 x i32> poison, i32 %x, i32 0
@@ -72,8 +67,7 @@ define <vscale x 4 x i32> @umax_scalable_rhs_const(i32 %x) {
7267
; CHECK-LABEL: define <vscale x 4 x i32> @umax_scalable_rhs_const(
7368
; CHECK-SAME: i32 [[X:%.*]]) {
7469
; CHECK-NEXT: [[V_SCALAR:%.*]] = call i32 @llvm.umax.i32(i32 [[X]], i32 42)
75-
; CHECK-NEXT: [[TMP1:%.*]] = call <vscale x 4 x i32> @llvm.umax.nxv4i32(<vscale x 4 x i32> poison, <vscale x 4 x i32> splat (i32 42))
76-
; CHECK-NEXT: [[V:%.*]] = insertelement <vscale x 4 x i32> [[TMP1]], i32 [[V_SCALAR]], i64 0
70+
; CHECK-NEXT: [[V:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[V_SCALAR]], i64 0
7771
; CHECK-NEXT: ret <vscale x 4 x i32> [[V]]
7872
;
7973
%x.insert = insertelement <vscale x 4 x i32> poison, i32 %x, i32 0
@@ -100,8 +94,7 @@ define <4 x float> @fabs_fixed(float %x) {
10094
; CHECK-LABEL: define <4 x float> @fabs_fixed(
10195
; CHECK-SAME: float [[X:%.*]]) {
10296
; CHECK-NEXT: [[V_SCALAR:%.*]] = call float @llvm.fabs.f32(float [[X]])
103-
; CHECK-NEXT: [[TMP1:%.*]] = call <4 x float> @llvm.fabs.v4f32(<4 x float> poison)
104-
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x float> [[TMP1]], float [[V_SCALAR]], i64 0
97+
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x float> poison, float [[V_SCALAR]], i64 0
10598
; CHECK-NEXT: ret <4 x float> [[V]]
10699
;
107100
%x.insert = insertelement <4 x float> poison, float %x, i32 0
@@ -113,8 +106,7 @@ define <vscale x 4 x float> @fabs_scalable(float %x) {
113106
; CHECK-LABEL: define <vscale x 4 x float> @fabs_scalable(
114107
; CHECK-SAME: float [[X:%.*]]) {
115108
; CHECK-NEXT: [[V_SCALAR:%.*]] = call float @llvm.fabs.f32(float [[X]])
116-
; CHECK-NEXT: [[TMP1:%.*]] = call <vscale x 4 x float> @llvm.fabs.nxv4f32(<vscale x 4 x float> poison)
117-
; CHECK-NEXT: [[V:%.*]] = insertelement <vscale x 4 x float> [[TMP1]], float [[V_SCALAR]], i64 0
109+
; CHECK-NEXT: [[V:%.*]] = insertelement <vscale x 4 x float> poison, float [[V_SCALAR]], i64 0
118110
; CHECK-NEXT: ret <vscale x 4 x float> [[V]]
119111
;
120112
%x.insert = insertelement <vscale x 4 x float> poison, float %x, i32 0
@@ -126,8 +118,7 @@ define <4 x float> @fma_fixed(float %x, float %y, float %z) {
126118
; CHECK-LABEL: define <4 x float> @fma_fixed(
127119
; CHECK-SAME: float [[X:%.*]], float [[Y:%.*]], float [[Z:%.*]]) {
128120
; CHECK-NEXT: [[V_SCALAR:%.*]] = call float @llvm.fma.f32(float [[X]], float [[Y]], float [[Z]])
129-
; CHECK-NEXT: [[TMP1:%.*]] = call <4 x float> @llvm.fma.v4f32(<4 x float> poison, <4 x float> poison, <4 x float> poison)
130-
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x float> [[TMP1]], float [[V_SCALAR]], i64 0
121+
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x float> poison, float [[V_SCALAR]], i64 0
131122
; CHECK-NEXT: ret <4 x float> [[V]]
132123
;
133124
%x.insert = insertelement <4 x float> poison, float %x, i32 0
@@ -141,8 +132,7 @@ define <vscale x 4 x float> @fma_scalable(float %x, float %y, float %z) {
141132
; CHECK-LABEL: define <vscale x 4 x float> @fma_scalable(
142133
; CHECK-SAME: float [[X:%.*]], float [[Y:%.*]], float [[Z:%.*]]) {
143134
; CHECK-NEXT: [[V_SCALAR:%.*]] = call float @llvm.fma.f32(float [[X]], float [[Y]], float [[Z]])
144-
; CHECK-NEXT: [[TMP1:%.*]] = call <vscale x 4 x float> @llvm.fma.nxv4f32(<vscale x 4 x float> poison, <vscale x 4 x float> poison, <vscale x 4 x float> poison)
145-
; CHECK-NEXT: [[V:%.*]] = insertelement <vscale x 4 x float> [[TMP1]], float [[V_SCALAR]], i64 0
135+
; CHECK-NEXT: [[V:%.*]] = insertelement <vscale x 4 x float> poison, float [[V_SCALAR]], i64 0
146136
; CHECK-NEXT: ret <vscale x 4 x float> [[V]]
147137
;
148138
%x.insert = insertelement <vscale x 4 x float> poison, float %x, i32 0
@@ -156,8 +146,7 @@ define <4 x float> @scalar_argument(float %x) {
156146
; CHECK-LABEL: define <4 x float> @scalar_argument(
157147
; CHECK-SAME: float [[X:%.*]]) {
158148
; CHECK-NEXT: [[V_SCALAR:%.*]] = call float @llvm.powi.f32.i32(float [[X]], i32 42)
159-
; CHECK-NEXT: [[TMP1:%.*]] = call <4 x float> @llvm.powi.v4f32.i32(<4 x float> poison, i32 42)
160-
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x float> [[TMP1]], float [[V_SCALAR]], i64 0
149+
; CHECK-NEXT: [[V:%.*]] = insertelement <4 x float> poison, float [[V_SCALAR]], i64 0
161150
; CHECK-NEXT: ret <4 x float> [[V]]
162151
;
163152
%x.insert = insertelement <4 x float> poison, float %x, i32 0

0 commit comments

Comments
 (0)