Skip to content

Commit 1113224

Browse files
committed
[VectorCombine] Account for IRBuilder simplification in translateExt.
After #146350, CreateExtractElement may return a folded value and not create an ExtractElement instruction. Replace cast with dyn_cast. Note that the function returns nullptr already earlier if the extract may be constant folded. Fixes #147218
1 parent 6daf2b9 commit 1113224

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,8 @@ static ExtractElementInst *translateExtract(ExtractElementInst *ExtElt,
563563

564564
Value *Shuf = createShiftShuffle(X, cast<ConstantInt>(C)->getZExtValue(),
565565
NewIndex, Builder);
566-
return cast<ExtractElementInst>(Builder.CreateExtractElement(Shuf, NewIndex));
566+
return dyn_cast<ExtractElementInst>(
567+
Builder.CreateExtractElement(Shuf, NewIndex));
567568
}
568569

569570
/// Try to reduce extract element costs by converting scalar compares to vector

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,3 +567,22 @@ define float @constant_fold_crash_commute(<4 x float> %x) {
567567
%c = fadd float %b, %a
568568
ret float %c
569569
}
570+
571+
; Test case for https://github.com/llvm/llvm-project/issues/147218.
572+
define i64 @instsimplify_folder_crash(<4 x i64> %in) {
573+
; CHECK-LABEL: @instsimplify_folder_crash(
574+
; CHECK-NEXT: entry:
575+
; CHECK-NEXT: [[SHUFFLE_1:%.*]] = shufflevector <4 x i64> [[IN:%.*]], <4 x i64> zeroinitializer, <4 x i32> <i32 4, i32 5, i32 2, i32 3>
576+
; CHECK-NEXT: [[E_0:%.*]] = extractelement <4 x i64> zeroinitializer, i64 0
577+
; CHECK-NEXT: [[E_1:%.*]] = extractelement <4 x i64> [[SHUFFLE_1]], i64 1
578+
; CHECK-NEXT: [[OR:%.*]] = or i64 [[E_1]], [[E_0]]
579+
; CHECK-NEXT: ret i64 [[OR]]
580+
;
581+
entry:
582+
%shuffle.1 = shufflevector <4 x i64> %in, <4 x i64> zeroinitializer, <4 x i32> <i32 4, i32 5, i32 2, i32 3>
583+
%e.0 = extractelement <4 x i64> zeroinitializer, i64 0
584+
%e.1 = extractelement <4 x i64> %shuffle.1, i64 1
585+
%shift = shufflevector <4 x i64> %shuffle.1, <4 x i64> poison, <4 x i32> <i32 1, i32 poison, i32 poison, i32 poison>
586+
%or = or i64 %e.1, %e.0
587+
ret i64 %or
588+
}

0 commit comments

Comments
 (0)