Skip to content

Commit 889215a

Browse files
committed
[SLP]Followup fix for the poisonous logical op in reductions
If the VectorizedTree still may generate poisonous value, but it is not the original operand of the reduction op, need to check if Res still the operand, to generate correct code. Fixes llvm#114905
1 parent 1b476ec commit 889215a

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19821,21 +19821,21 @@ class HorizontalReduction {
1982119821
Builder.SetCurrentDebugLocation(
1982219822
cast<Instruction>(ReductionOps.front().front())->getDebugLoc());
1982319823
if (AnyBoolLogicOp) {
19824-
19825-
if (auto It = ReducedValsToOps.find(VectorizedTree);
19826-
It == ReducedValsToOps.end() ||
19824+
auto It = ReducedValsToOps.find(VectorizedTree);
19825+
auto It1 = ReducedValsToOps.find(Res);
19826+
if ((It == ReducedValsToOps.end() && It1 == ReducedValsToOps.end()) ||
1982719827
isGuaranteedNotToBePoison(VectorizedTree, AC) ||
19828-
any_of(It->getSecond(), [&](Instruction *I) {
19829-
return isBoolLogicOp(I) &&
19830-
getRdxOperand(I, 0) == VectorizedTree;
19831-
})) {
19828+
(It != ReducedValsToOps.end() &&
19829+
any_of(It->getSecond(), [&](Instruction *I) {
19830+
return isBoolLogicOp(I) &&
19831+
getRdxOperand(I, 0) == VectorizedTree;
19832+
}))) {
1983219833
;
19833-
} else if (auto It = ReducedValsToOps.find(Res);
19834-
It == ReducedValsToOps.end() ||
19835-
isGuaranteedNotToBePoison(Res, AC) ||
19836-
any_of(It->getSecond(), [&](Instruction *I) {
19834+
} else if (isGuaranteedNotToBePoison(Res, AC) ||
19835+
(It1 != ReducedValsToOps.end() &&
19836+
any_of(It1->getSecond(), [&](Instruction *I) {
1983719837
return isBoolLogicOp(I) && getRdxOperand(I, 0) == Res;
19838-
})) {
19838+
}))) {
1983919839
std::swap(VectorizedTree, Res);
1984019840
} else {
1984119841
VectorizedTree = Builder.CreateFreeze(VectorizedTree);

llvm/test/Transforms/SLPVectorizer/X86/reduction-logical.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ define i1 @logical_and_icmp_extra_op(<4 x i32> %x, <4 x i32> %y, i1 %c) {
428428
; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <4 x i32> [[X:%.*]], [[Y:%.*]]
429429
; CHECK-NEXT: [[TMP2:%.*]] = freeze <4 x i1> [[TMP1]]
430430
; CHECK-NEXT: [[TMP3:%.*]] = call i1 @llvm.vector.reduce.and.v4i1(<4 x i1> [[TMP2]])
431-
; CHECK-NEXT: [[OP_RDX:%.*]] = select i1 [[TMP3]], i1 [[C:%.*]], i1 false
431+
; CHECK-NEXT: [[OP_RDX:%.*]] = select i1 [[C:%.*]], i1 [[TMP3]], i1 false
432432
; CHECK-NEXT: ret i1 [[OP_RDX]]
433433
;
434434
%x0 = extractelement <4 x i32> %x, i32 0
@@ -456,7 +456,7 @@ define i1 @logical_or_icmp_extra_op(<4 x i32> %x, <4 x i32> %y, i1 %c) {
456456
; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <4 x i32> [[X:%.*]], [[Y:%.*]]
457457
; CHECK-NEXT: [[TMP2:%.*]] = freeze <4 x i1> [[TMP1]]
458458
; CHECK-NEXT: [[TMP3:%.*]] = call i1 @llvm.vector.reduce.or.v4i1(<4 x i1> [[TMP2]])
459-
; CHECK-NEXT: [[OP_RDX:%.*]] = select i1 [[TMP3]], i1 true, i1 [[C:%.*]]
459+
; CHECK-NEXT: [[OP_RDX:%.*]] = select i1 [[C:%.*]], i1 true, i1 [[TMP3]]
460460
; CHECK-NEXT: ret i1 [[OP_RDX]]
461461
;
462462
%x0 = extractelement <4 x i32> %x, i32 0

llvm/test/Transforms/SLPVectorizer/logical-ops-poisonous-repeated.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ define i1 @test(<4 x i32> %x) {
1414
; CHECK-NEXT: [[C3:%.*]] = icmp slt i32 [[X3]], 0
1515
; CHECK-NEXT: [[TMP2:%.*]] = freeze i1 [[C3]]
1616
; CHECK-NEXT: [[OP_RDX:%.*]] = select i1 [[TMP2]], i1 [[C1]], i1 false
17-
; CHECK-NEXT: [[OP_RDX1:%.*]] = select i1 [[OP_RDX]], i1 [[TMP1]], i1 false
17+
; CHECK-NEXT: [[OP_RDX1:%.*]] = select i1 [[TMP1]], i1 [[OP_RDX]], i1 false
1818
; CHECK-NEXT: ret i1 [[OP_RDX1]]
1919
;
2020
%x0 = extractelement <4 x i32> %x, i32 0

0 commit comments

Comments
 (0)