[InstSimplify] Fix a scalable-vector crash
authorFraser Cormack <fraser@codeplay.com>
Thu, 22 Jun 2023 15:49:39 +0000 (16:49 +0100)
committerFraser Cormack <fraser@codeplay.com>
Wed, 28 Jun 2023 16:07:10 +0000 (17:07 +0100)
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.

Reviewed By: arsenm

Differential Revision: https://reviews.llvm.org/D153566

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

index 27fbff3..54ce626 100644 (file)
@@ -5466,6 +5466,15 @@ static Constant *propagateNaN(Constant *In) {
   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());
index 620bda1..fad7a05 100644 (file)
@@ -49,6 +49,22 @@ define <2 x float> @fsub_nan_op1_vec(<2 x float> %x) {
   ret <2 x float> %r
 }
 
+define <vscale x 1 x float> @fsub_nan_op1_scalable_vec_0(<vscale x 1 x float> %x) {
+; CHECK-LABEL: @fsub_nan_op1_scalable_vec_0(
+; 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
+}
+
+define <vscale x 1 x float> @fsub_nan_op1_scalable_vec_1(<vscale x 1 x float> %x) {
+; CHECK-LABEL: @fsub_nan_op1_scalable_vec_1(
+; CHECK-NEXT:    ret <vscale x 1 x float> shufflevector (<vscale x 1 x float> insertelement (<vscale x 1 x float> poison, float 0xFFF9000000000000, 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 0xFFF1000000000000, 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) {
@@ -67,6 +83,22 @@ define <2 x double> @fmul_nan_op0_vec(<2 x double> %x) {
   ret <2 x double> %r
 }
 
+define <vscale x 1 x double> @fmul_nan_op0_scalable_vec_0(<vscale x 1 x double> %x) {
+; CHECK-LABEL: @fmul_nan_op0_scalable_vec_0(
+; CHECK-NEXT:    ret <vscale x 1 x double> shufflevector (<vscale x 1 x double> insertelement (<vscale x 1 x double> poison, double 0xFFF8000000000001, i64 0), <vscale x 1 x double> poison, <vscale x 1 x i32> zeroinitializer)
+;
+  %r = fmul <vscale x 1 x double> shufflevector (<vscale x 1 x double> insertelement (<vscale x 1 x double> poison, double 0xFFF0000000000001, i64 0), <vscale x 1 x double> poison, <vscale x 1 x i32> zeroinitializer), %x
+  ret <vscale x 1 x double> %r
+}
+
+define <vscale x 1 x double> @fmul_nan_op0_scalable_vec_1(<vscale x 1 x double> %x) {
+; CHECK-LABEL: @fmul_nan_op0_scalable_vec_1(
+; CHECK-NEXT:    ret <vscale x 1 x double> shufflevector (<vscale x 1 x double> insertelement (<vscale x 1 x double> poison, double 0xFFF8DEADDEADDEAD, i64 0), <vscale x 1 x double> poison, <vscale x 1 x i32> zeroinitializer)
+;
+  %r = fmul <vscale x 1 x double> shufflevector (<vscale x 1 x double> insertelement (<vscale x 1 x double> poison, double 0xFFF0DEADDEADDEAD, i64 0), <vscale x 1 x double> poison, <vscale x 1 x i32> zeroinitializer), %x
+  ret <vscale x 1 x double> %r
+}
+
 ; Vector type
 
 define <2 x float> @fmul_nan_op1(<2 x float> %x) {
@@ -77,6 +109,14 @@ define <2 x float> @fmul_nan_op1(<2 x float> %x) {
   ret <2 x float> %r
 }
 
+define <vscale x 1 x double> @fmul_nan_op1_scalable_vec(<vscale x 1 x double> %x) {
+; CHECK-LABEL: @fmul_nan_op1_scalable_vec(
+; CHECK-NEXT:    ret <vscale x 1 x double> shufflevector (<vscale x 1 x double> insertelement (<vscale x 1 x double> poison, double 0x7FF8000000000000, i64 0), <vscale x 1 x double> poison, <vscale x 1 x i32> zeroinitializer)
+;
+  %r = fmul <vscale x 1 x double> %x, shufflevector (<vscale x 1 x double> insertelement (<vscale x 1 x double> poison, double 0x7FF8000000000000, i64 0), <vscale x 1 x double> poison, <vscale x 1 x i32> zeroinitializer)
+  ret <vscale x 1 x double> %r
+}
+
 ; Vector signed and non-zero payload
 
 define <2 x double> @fdiv_nan_op0(<2 x double> %x) {
@@ -87,6 +127,14 @@ define <2 x double> @fdiv_nan_op0(<2 x double> %x) {
   ret <2 x double>  %r
 }
 
+define <vscale x 1 x double> @fdivl_nan_op0_scalable_vec(<vscale x 1 x double> %x) {
+; CHECK-LABEL: @fdivl_nan_op0_scalable_vec(
+; CHECK-NEXT:    ret <vscale x 1 x double> shufflevector (<vscale x 1 x double> insertelement (<vscale x 1 x double> poison, double 0xFFF800000000000F, i64 0), <vscale x 1 x double> poison, <vscale x 1 x i32> zeroinitializer)
+;
+  %r = fdiv <vscale x 1 x double> shufflevector (<vscale x 1 x double> insertelement (<vscale x 1 x double> poison, double 0xFFF800000000000F, i64 0), <vscale x 1 x double> poison, <vscale x 1 x i32> zeroinitializer), %x
+  ret <vscale x 1 x double> %r
+}
+
 ; Vector with different NaN constant elements
 
 define <2 x half> @fdiv_nan_op1(<2 x half> %x) {
@@ -97,6 +145,14 @@ define <2 x half> @fdiv_nan_op1(<2 x half> %x) {
   ret <2 x half> %r
 }
 
+define <vscale x 1 x half> @fdiv_nan_op1_scalable_vec(<vscale x 1 x half> %x) {
+; CHECK-LABEL: @fdiv_nan_op1_scalable_vec(
+; CHECK-NEXT:    ret <vscale x 1 x half> shufflevector (<vscale x 1 x half> insertelement (<vscale x 1 x half> poison, half 0xH7FFF, i64 0), <vscale x 1 x half> poison, <vscale x 1 x i32> zeroinitializer)
+;
+  %r = fdiv <vscale x 1 x half> %x, shufflevector (<vscale x 1 x half> insertelement (<vscale x 1 x half> poison, half 0xH7FFF, i64 0), <vscale x 1 x half> poison, <vscale x 1 x i32> zeroinitializer)
+  ret <vscale x 1 x half> %r
+}
+
 ; Vector with poison element
 
 define <2 x double> @fsub_nan_poison_op1(<2 x double> %x) {
@@ -161,6 +217,14 @@ define <2 x double> @fneg_nan_2(<2 x double> %x) {
   ret <2 x double> %r
 }
 
+define <vscale x 1 x double> @fneg_nan_2_scalable_vec() {
+; CHECK-LABEL: @fneg_nan_2_scalable_vec(
+; CHECK-NEXT:    ret <vscale x 1 x double> shufflevector (<vscale x 1 x double> insertelement (<vscale x 1 x double> poison, double 0xFFF9234567890ABC, i64 0), <vscale x 1 x double> poison, <vscale x 1 x i32> zeroinitializer)
+;
+  %r = fsub <vscale x 1 x double> shufflevector (<vscale x 1 x double> insertelement (<vscale x 1 x double> poison, double -0.0, i64 0), <vscale x 1 x double> poison, <vscale x 1 x i32> zeroinitializer), shufflevector (<vscale x 1 x double> insertelement (<vscale x 1 x double> poison, double 0xFFF1234567890ABC, i64 0), <vscale x 1 x double> poison, <vscale x 1 x i32> zeroinitializer)
+  ret <vscale x 1 x double> %r
+}
+
 define <2 x double> @unary_fneg_nan_2(<2 x double> %x) {
 ; CHECK-LABEL: @unary_fneg_nan_2(
 ; CHECK-NEXT:    ret <2 x double> <double 0x7FF1234567890ABC, double 0xFFF0000000000001>
@@ -169,6 +233,26 @@ define <2 x double> @unary_fneg_nan_2(<2 x double> %x) {
   ret <2 x double> %r
 }
 
+; FIXME: This doesn't behave the same way as the fixed-length vectors above
+define <vscale x 1 x double> @unary_fneg_nan_2_scalable_vec_0() {
+; CHECK-LABEL: @unary_fneg_nan_2_scalable_vec_0(
+; CHECK-NEXT:    [[R:%.*]] = fneg <vscale x 1 x double> shufflevector (<vscale x 1 x double> insertelement (<vscale x 1 x double> poison, double 0xFFF1234567890ABC, i64 0), <vscale x 1 x double> poison, <vscale x 1 x i32> zeroinitializer)
+; CHECK-NEXT:    ret <vscale x 1 x double> [[R]]
+;
+  %r = fneg <vscale x 1 x double> shufflevector (<vscale x 1 x double> insertelement (<vscale x 1 x double> poison, double 0xFFF1234567890ABC, i64 0), <vscale x 1 x double> poison, <vscale x 1 x i32> zeroinitializer)
+  ret <vscale x 1 x double> %r
+}
+
+; FIXME: This doesn't behave the same way as the fixed-length vectors above
+define <vscale x 1 x double> @unary_fneg_nan_2_scalable_vec_1() {
+; CHECK-LABEL: @unary_fneg_nan_2_scalable_vec_1(
+; CHECK-NEXT:    [[R:%.*]] = fneg <vscale x 1 x double> shufflevector (<vscale x 1 x double> insertelement (<vscale x 1 x double> poison, double 0x7FF0000000000001, i64 0), <vscale x 1 x double> poison, <vscale x 1 x i32> zeroinitializer)
+; CHECK-NEXT:    ret <vscale x 1 x double> [[R]]
+;
+  %r = fneg <vscale x 1 x double> shufflevector (<vscale x 1 x double> insertelement (<vscale x 1 x double> poison, double 0x7FF0000000000001, i64 0), <vscale x 1 x double> poison, <vscale x 1 x i32> zeroinitializer)
+  ret <vscale x 1 x double> %r
+}
+
 ; Repeat all tests with fast-math-flags. Alternate 'nnan' and 'fast' for more coverage.
 
 define float @fadd_nan_op0_nnan(float %x) {