[PATCH] D153566: [InstSimplify] Fix a scalable-vector crash

Fraser Cormack via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 22 08:57:19 PDT 2023


frasercrmck created this revision.
frasercrmck added reviewers: spatel, jyknight.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
frasercrmck requested review of this revision.
Herald added subscribers: llvm-commits, alextsao1999.
Herald added a project: LLVM.

D143505 <https://reviews.llvm.org/D143505> fixed/simplified folding of operations with SNaN operands. In
doing so it introduced a crash when handling scalable vector types,
wherein the scalable-vector ConstantVector was cast to a ConstantFP.

Since we know by that point in the code that if we've found a NaN, we're
dealing with a scalable-vector splat (as there are no other kinds of
scalable-vector constant for which that holds), we can grab the splatted
value and re-use the existing code, which will automatically splat the
new NaN back to a scalable vector for us.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153566

Files:
  llvm/lib/Analysis/InstructionSimplify.cpp
  llvm/test/Transforms/InstSimplify/fp-nan.ll


Index: llvm/test/Transforms/InstSimplify/fp-nan.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/fp-nan.ll
+++ llvm/test/Transforms/InstSimplify/fp-nan.ll
@@ -49,6 +49,14 @@
   ret <2 x float> %r
 }
 
+define <vscale x 1 x float> @fsub_nan_op1_scalable_vec(<vscale x 1 x float> %x) {
+; CHECK-LABEL: @fsub_nan_op1_scalable_vec(
+; CHECK-NEXT:    ret <vscale x 1 x float> shufflevector (<vscale x 1 x float> insertelement (<vscale x 1 x float> poison, float 0x7FF9000000000000, i64 0), <vscale x 1 x float> poison, <vscale x 1 x i32> zeroinitializer)
+;
+  %r = fsub <vscale x 1 x float> %x, shufflevector (<vscale x 1 x float> insertelement (<vscale x 1 x float> poison, float 0x7FF1000000000000, i64 0), <vscale x 1 x float> poison, <vscale x 1 x i32> zeroinitializer)
+  ret <vscale x 1 x float> %r
+}
+
 ; Signaling and signed - make quiet and preserve the payload and signbit
 
 define double @fmul_nan_op0(double %x) {
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5467,6 +5467,15 @@
   if (!In->isNaN())
     return ConstantFP::getNaN(Ty);
 
+  // If we known this is a NaN, and it's scalable vector, we must have a splat
+  // on our hands. Grab that before splatting a QNaN constant.
+  if (isa<ScalableVectorType>(Ty)) {
+    auto *Splat = In->getSplatValue();
+    assert(Splat && Splat->isNaN() &&
+           "Found a scalable-vector NaN but not a splat");
+    In = Splat;
+  }
+
   // Propagate an existing QNaN constant. If it is an SNaN, make it quiet, but
   // preserve the sign/payload.
   return ConstantFP::get(Ty, cast<ConstantFP>(In)->getValue().makeQuiet());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153566.533639.patch
Type: text/x-patch
Size: 1810 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230622/71986340/attachment.bin>


More information about the llvm-commits mailing list