[PATCH] D134670: [IR][InstCombine] Support scalable vector splats ConstantExprs in Constant::getUniqueInteger().

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 26 13:18:27 PDT 2022


craig.topper created this revision.
craig.topper added reviewers: spatel, nikic, RKSimon, compnerd.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
craig.topper requested review of this revision.
Herald added a subscriber: alextsao1999.
Herald added a project: LLVM.

I've left the getAggregateElement as a fast path for non-ConstantExprs
to avoid a call to getSplatValue in release builds.

Fixes PR57989.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D134670

Files:
  llvm/lib/IR/Constants.cpp
  llvm/test/Transforms/InstCombine/select.ll


Index: llvm/test/Transforms/InstCombine/select.ll
===================================================================
--- llvm/test/Transforms/InstCombine/select.ll
+++ llvm/test/Transforms/InstCombine/select.ll
@@ -3395,3 +3395,16 @@
   %v = select i1 %cond, i32 %s, i32 %z
   ret i32 %v
 }
+
+; This previously crashed due to Constant::getUniqueInteger not handling
+; scalable vector splat ConstantExprs.
+define <vscale x 2 x i32> @and_constant_select_svec(<vscale x 2 x i32> %x, <vscale x 2 x i1> %cond) {
+; CHECK-LABEL: @and_constant_select_svec(
+; CHECK-NEXT:    [[A:%.*]] = and <vscale x 2 x i32> [[X:%.*]], shufflevector (<vscale x 2 x i32> insertelement (<vscale x 2 x i32> poison, i32 1, i32 0), <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer)
+; CHECK-NEXT:    [[B:%.*]] = select <vscale x 2 x i1> [[COND:%.*]], <vscale x 2 x i32> [[A]], <vscale x 2 x i32> [[X]]
+; CHECK-NEXT:    ret <vscale x 2 x i32> [[B]]
+;
+  %a = and <vscale x 2 x i32> %x, shufflevector (<vscale x 2 x i32> insertelement (<vscale x 2 x i32> poison, i32 1, i32 0), <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer)
+  %b = select <vscale x 2 x i1> %cond, <vscale x 2 x i32> %a, <vscale x 2 x i32> %x
+  ret <vscale x 2 x i32> %b
+}
Index: llvm/lib/IR/Constants.cpp
===================================================================
--- llvm/lib/IR/Constants.cpp
+++ llvm/lib/IR/Constants.cpp
@@ -1682,6 +1682,9 @@
 const APInt &Constant::getUniqueInteger() const {
   if (const ConstantInt *CI = dyn_cast<ConstantInt>(this))
     return CI->getValue();
+  // Scalable vectors can use a ConstantExpr to build a splat.
+  if (isa<ConstantExpr>(this))
+    return cast<ConstantInt>(this->getSplatValue())->getValue();
   assert(this->getSplatValue() && "Doesn't contain a unique integer!");
   const Constant *C = this->getAggregateElement(0U);
   assert(C && isa<ConstantInt>(C) && "Not a vector of numbers!");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134670.463004.patch
Type: text/x-patch
Size: 1926 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220926/f160afa6/attachment.bin>


More information about the llvm-commits mailing list