Skip to content

Commit 5187482

Browse files
authored
IR: handle FP predicates in CmpPredicate::getMatching (#122924)
CmpPredicate::getMatching implicitly assumes that both predicates are integer-predicates, and this has led to a crash being reported in VectorCombine after e409204 (VectorCombine: teach foldExtractedCmps about samesign). FP predicates are simple enough to handle as there is never any samesign information associated with them: hence handle them in CmpPredicate::getMatching, fixing the VectorCombine crash and guarding against future incorrect usages.
1 parent fed817a commit 5187482

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

llvm/lib/IR/Instructions.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3939,6 +3939,8 @@ std::optional<CmpPredicate> CmpPredicate::getMatching(CmpPredicate A,
39393939
CmpPredicate B) {
39403940
if (A.Pred == B.Pred)
39413941
return A.HasSameSign == B.HasSameSign ? A : CmpPredicate(A.Pred);
3942+
if (CmpInst::isFPPredicate(A) || CmpInst::isFPPredicate(B))
3943+
return {};
39423944
if (A.HasSameSign &&
39433945
A.Pred == ICmpInst::getFlippedSignednessPredicate(B.Pred))
39443946
return B.Pred;

llvm/test/Transforms/VectorCombine/X86/extract-cmp-binop.ll

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,25 @@ define i1 @different_preds(<4 x i32> %a) {
221221
ret i1 %r
222222
}
223223

224+
; Negative test with integer and fp predicates.
225+
226+
define i1 @different_preds_typemismtach(<4 x float> %a, <4 x i32> %b) {
227+
; CHECK-LABEL: @different_preds_typemismtach(
228+
; CHECK-NEXT: [[E1:%.*]] = extractelement <4 x float> [[A:%.*]], i32 1
229+
; CHECK-NEXT: [[E2:%.*]] = extractelement <4 x i32> [[B:%.*]], i32 2
230+
; CHECK-NEXT: [[CMP1:%.*]] = fcmp ogt float [[E1]], 4.200000e+01
231+
; CHECK-NEXT: [[CMP2:%.*]] = icmp samesign ugt i32 [[E2]], -8
232+
; CHECK-NEXT: [[R:%.*]] = and i1 [[CMP1]], [[CMP2]]
233+
; CHECK-NEXT: ret i1 [[R]]
234+
;
235+
%e1 = extractelement <4 x float> %a, i32 1
236+
%e2 = extractelement <4 x i32> %b, i32 2
237+
%cmp1 = fcmp ogt float %e1, 42.0
238+
%cmp2 = icmp samesign ugt i32 %e2, -8
239+
%r = and i1 %cmp1, %cmp2
240+
ret i1 %r
241+
}
242+
224243
; Negative test - need 1 source vector.
225244

226245
define i1 @different_source_vec(<4 x i32> %a, <4 x i32> %b) {

llvm/unittests/IR/InstructionsTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,6 +1951,7 @@ TEST(InstructionsTest, CmpPredicate) {
19511951
EXPECT_EQ(*CmpPredicate::getMatching(P1, P2), CmpInst::ICMP_SLE);
19521952
EXPECT_EQ(CmpPredicate::getMatching(P1, P2)->hasSameSign(), false);
19531953
EXPECT_EQ(CmpPredicate::getMatching(P1, P3), std::nullopt);
1954+
EXPECT_EQ(CmpPredicate::getMatching(P1, CmpInst::FCMP_ULE), std::nullopt);
19541955
EXPECT_FALSE(Q0.hasSameSign());
19551956
EXPECT_TRUE(Q1.hasSameSign());
19561957
EXPECT_FALSE(Q2.hasSameSign());

0 commit comments

Comments
 (0)