[llvm] 3a223f1 - [InstCombine] Fix crash due to early extractvalue removal

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed May 24 00:56:01 PDT 2023


Author: Nikita Popov
Date: 2023-05-24T09:55:52+02:00
New Revision: 3a223f1eafe331508d171b519df8a4984791ab48

URL: https://github.com/llvm/llvm-project/commit/3a223f1eafe331508d171b519df8a4984791ab48
DIFF: https://github.com/llvm/llvm-project/commit/3a223f1eafe331508d171b519df8a4984791ab48.diff

LOG: [InstCombine] Fix crash due to early extractvalue removal

Fixes the issue reported at https://github.com/llvm/llvm-project/commit/4b8320868c9e32d1448c81ca76dba2a6b9f374cd#commitcomment-114671248.

The extractvalue instructions may still be used by the calling code
in some cases. Rather than trying to figure out which extracts are
safe to remove and which aren't, add them to the worklist so they
will get DCEd by the main loop.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
    llvm/test/Transforms/InstCombine/extractelement.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
index 5bcbf8fec8408..98a66db47f637 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
@@ -751,7 +751,9 @@ static void replaceExtractElements(InsertElementInst *InsElt,
     auto *NewExt = ExtractElementInst::Create(WideVec, OldExt->getOperand(1));
     NewExt->insertAfter(OldExt);
     IC.replaceInstUsesWith(*OldExt, NewExt);
-    IC.eraseInstFromFunction(*OldExt);
+    // Add the old extracts to the worklist for DCE. We can't remove the
+    // extracts directly, because they may still be used by the calling code.
+    IC.addToWorklist(OldExt);
   }
 }
 

diff  --git a/llvm/test/Transforms/InstCombine/extractelement.ll b/llvm/test/Transforms/InstCombine/extractelement.ll
index 9f8795b83b688..e40225b9026e9 100644
--- a/llvm/test/Transforms/InstCombine/extractelement.ll
+++ b/llvm/test/Transforms/InstCombine/extractelement.ll
@@ -902,3 +902,27 @@ define i32 @extelt_select_const_operands_vector_extra_use_2(i1 %c) {
   %r = extractelement <3 x i32> %s, i64 0
   ret i32 %r
 }
+
+define float @crash_4b8320(<2 x float> %i1, float %i12) {
+; ANY-LABEL: @crash_4b8320(
+; ANY-NEXT:    [[I6:%.*]] = fmul reassoc <2 x float> [[I1:%.*]], <float 0.000000e+00, float poison>
+; ANY-NEXT:    [[TMP1:%.*]] = extractelement <2 x float> [[I6]], i64 0
+; ANY-NEXT:    [[TMP2:%.*]] = extractelement <2 x float> [[I6]], i64 0
+; ANY-NEXT:    [[TMP3:%.*]] = fadd float [[TMP1]], [[TMP2]]
+; ANY-NEXT:    [[I29:%.*]] = fadd float [[TMP3]], 0.000000e+00
+; ANY-NEXT:    ret float [[I29]]
+;
+  %i5 = fmul <2 x float> zeroinitializer, %i1
+  %i6 = fmul reassoc <2 x float> zeroinitializer, %i5
+  %i147 = extractelement <2 x float> %i6, i64 0
+  %i15 = extractelement <2 x float> %i6, i64 0
+  %i16 = insertelement <4 x float> zeroinitializer, float %i147, i64 0
+  %i17 = insertelement <4 x float> %i16, float %i15, i64 1
+  %i18 = insertelement <4 x float> %i17, float %i12, i64 2
+  %i19 = shufflevector <4 x float> %i18, <4 x float> zeroinitializer, <4 x i32> <i32 1, i32 1, i32 1, i32 1>
+  %i23 = fadd <4 x float> %i19, %i18
+  %i24 = shufflevector <4 x float> %i18, <4 x float> zeroinitializer, <4 x i32> <i32 3, i32 3, i32 3, i32 3>
+  %i26 = fadd <4 x float> %i23, %i24
+  %i29 = extractelement <4 x float> %i26, i64 0
+  ret float %i29
+}


        


More information about the llvm-commits mailing list