[PATCH] D82049: Fix crash in VectorCombine when attempting to peephole ConstantVector sequences

Chang Lin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 17 13:29:43 PDT 2020


clin1 created this revision.
clin1 added a reviewer: spatel.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

@spatel, are you OK with this fix? If Constants are given to foldExtractExtract, this Builder call:

  Value *NewExt = Builder.CreateExtractElement(Shuf, CheapExtIndex);

will end up with a Constant instead of an Instruction, causing a crash on the later cast.
Rather than try to make this specialized peephole work for constant Extracts, it seems better to just let InstCombine do it later.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D82049

Files:
  llvm/lib/Transforms/Vectorize/VectorCombine.cpp
  llvm/test/Transforms/VectorCombine/X86/ignore-const.ll


Index: llvm/test/Transforms/VectorCombine/X86/ignore-const.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/VectorCombine/X86/ignore-const.ll
@@ -0,0 +1,28 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -S -vector-combine | FileCheck %s
+;
+; extract,extract optimization was crashing with ConstantVector operands.
+; The extracts/inserts can be folded by InstCombine and can be left alone by
+; VectorCombine.
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+; Function Attrs: norecurse nounwind uwtable
+define dso_local i32 @main() local_unnamed_addr #0 {
+; CHECK-LABEL: @main(
+; CHECK-NEXT:    [[A:%.*]] = extractelement <8 x i32> <i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23>, i32 1
+; CHECK-NEXT:    [[B:%.*]] = extractelement <8 x i32> <i32 32, i32 33, i32 34, i32 35, i32 36, i32 37, i32 38, i32 39>, i32 7
+; CHECK-NEXT:    [[C:%.*]] = insertelement <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>, i32 [[B]], i32 2
+; CHECK-NEXT:    [[D:%.*]] = extractelement <8 x i32> [[C]], i64 0
+; CHECK-NEXT:    [[E:%.*]] = add i32 [[A]], [[D]]
+; CHECK-NEXT:    ret i32 [[E]]
+;
+  %a = extractelement <8 x i32> <i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23>, i32 1
+  %b = extractelement <8 x i32> <i32 32, i32 33, i32 34, i32 35, i32 36, i32 37, i32 38, i32 39>, i32 7
+  %c = insertelement <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>, i32 %b, i32 2
+  %d = extractelement <8 x i32> %c, i64 0
+  %e = add i32 %a, %d
+  ret i32 %e
+}
+attributes #0 = { norecurse nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="core-avx2" "target-features"="+avx,+avx2,+bmi,+bmi2,+cx16,+cx8,+f16c,+fma,+fsgsbase,+fxsr,+invpcid,+lzcnt,+mmx,+movbe,+pclmul,+popcnt,+rdrnd,+sahf,+sse,+sse2,+sse3,+sse4.1,+sse4.2,+ssse3,+x87,+xsave,+xsaveopt" "unsafe-fp-math"="false" "use-soft-float"="false" }
Index: llvm/lib/Transforms/Vectorize/VectorCombine.cpp
===================================================================
--- llvm/lib/Transforms/Vectorize/VectorCombine.cpp
+++ llvm/lib/Transforms/Vectorize/VectorCombine.cpp
@@ -218,6 +218,10 @@
       V0->getType() != V1->getType())
     return false;
 
+  // If either extract can be constant-folded, just leave it for InstCombine.
+  if (isa<Constant>(Ext0->getOperand(0)) || isa<Constant>(Ext1->getOperand(0)))
+    return false;
+
   // If the scalar value 'I' is going to be re-inserted into a vector, then try
   // to create an extract to that same element. The extract/insert can be
   // reduced to a "select shuffle".


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82049.271458.patch
Type: text/x-patch
Size: 3053 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200617/6944d0dc/attachment.bin>


More information about the llvm-commits mailing list