[llvm-reduce] Fix crash when reducing integer vectors to 1
authorFraser Cormack <fraser@codeplay.com>
Wed, 13 Jul 2022 10:10:41 +0000 (11:10 +0100)
committerFraser Cormack <fraser@codeplay.com>
Wed, 13 Jul 2022 15:56:55 +0000 (16:56 +0100)
Integer vectors were previously ignored when reducing operands. When
6b8bd0f72 introduced support for reducing floating-point
scalars/vectors, the vector case was written to only handle
floating-point values. It would crash when creating an invalid
ConstantFP from the integer element type.

Instead of reinstating the old integer vector behaviour, we might as
well reduce integer vectors to all-one splats.

A couple of existing tests has also been renamed from "remove" to
"reduce" to better reflect the deltas they test.

Reviewed By: arsenm

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

llvm/test/tools/llvm-reduce/reduce-operands-fp.ll [moved from llvm/test/tools/llvm-reduce/remove-operands-fp.ll with 91% similarity]
llvm/test/tools/llvm-reduce/reduce-operands-int.ll [new file with mode: 0644]
llvm/test/tools/llvm-reduce/reduce-operands.ll [moved from llvm/test/tools/llvm-reduce/remove-operands.ll with 100% similarity]
llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp

@@ -25,6 +25,7 @@
 ; CHECK-INTERESTINGNESS: = fadd <2 x float>
 ; CHECK-INTERESTINGNESS: = fadd <2 x float>
 ; CHECK-INTERESTINGNESS: = fadd <2 x float>
+; CHECK-INTERESTINGNESS: = fadd <2 x float>
 
 ; CHECK-LABEL: define void @foo(
 
@@ -41,6 +42,7 @@
 ; ONE: %fadd9 = fadd <2 x float> <float 1.000000e+00, float 1.000000e+00>, <float 1.000000e+00, float 1.000000e+00>
 ; ONE: %fadd10 = fadd <2 x float> <float 1.000000e+00, float 1.000000e+00>, <float 1.000000e+00, float 1.000000e+00>
 ; ONE: %fadd11 = fadd <2 x float> <float 1.000000e+00, float 1.000000e+00>, <float 1.000000e+00, float 1.000000e+00>
+; ONE: %fadd12 = fadd <2 x float> <float 1.000000e+00, float 1.000000e+00>, <float 1.000000e+00, float 1.000000e+00>
 
 
 ; ZERO: %fadd0 = fadd float %arg0, 0.000000e+00
@@ -55,6 +57,7 @@
 ; ZERO: %fadd9 = fadd <2 x float> zeroinitializer, zeroinitializer
 ; ZERO: %fadd10 = fadd <2 x float> zeroinitializer, zeroinitializer
 ; ZERO: %fadd11 = fadd <2 x float> zeroinitializer, zeroinitializer
+; ZERO: %fadd12 = fadd <2 x float> zeroinitializer, zeroinitializer
 
 
 ; NAN: %fadd0 = fadd float %arg0, 0x7FF8000000000000
@@ -69,6 +72,7 @@
 ; NAN: %fadd9 = fadd <2 x float> <float 0x7FF8000000000000, float 0x7FF8000000000000>, <float 1.000000e+00, float 1.000000e+00>
 ; NAN: %fadd10 = fadd <2 x float> <float 0x7FF8000000000000, float 0x7FF8000000000000>, <float 0x7FF8000000000000, float 0x7FF8000000000000>
 ; NAN: %fadd11 = fadd <2 x float> <float 0x7FF8000000000000, float 0x7FF8000000000000>, <float 0x7FF8000000000000, float 0x7FF8000000000000>
+; NAN: %fadd12 = fadd <2 x float> <float 0x7FF8000000000000, float 0x7FF8000000000000>, <float 0x7FF8000000000000, float 0x7FF8000000000000>
 
 define void @foo(float %arg0, float %arg1, <2 x float> %arg2, <2 x float> %arg3) {
 bb0:
@@ -84,5 +88,6 @@ bb0:
   %fadd9 = fadd <2 x float> %arg2, <float 1.0, float 1.0>
   %fadd10 = fadd <2 x float> %arg2, undef
   %fadd11 = fadd <2 x float> %arg2, <float 0x7FF8000000000000, float 0x7FF8000000000000>
+  %fadd12 = fadd <2 x float> %arg2, <float 0x7FF8000000000000, float 2.0>
   ret void
 }
diff --git a/llvm/test/tools/llvm-reduce/reduce-operands-int.ll b/llvm/test/tools/llvm-reduce/reduce-operands-int.ll
new file mode 100644 (file)
index 0000000..3005058
--- /dev/null
@@ -0,0 +1,67 @@
+; Test that llvm-reduce can reduce floating point operands
+;
+; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=operands-one --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
+; RUN: FileCheck --check-prefixes=CHECK,ONE %s < %t
+
+; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=operands-zero --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
+; RUN: FileCheck --check-prefixes=CHECK,ZERO %s < %t
+
+; RUN: llvm-reduce --abort-on-invalid-reduction --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
+; RUN: FileCheck --check-prefixes=CHECK,ZERO %s < %t
+
+; CHECK-INTERESTINGNESS: = add i32 %
+; CHECK-INTERESTINGNESS: = add i32
+; CHECK-INTERESTINGNESS: = add i32
+; CHECK-INTERESTINGNESS: = add i32
+; CHECK-INTERESTINGNESS: = add i32
+
+; CHECK-INTERESTINGNESS: = add <2 x i32> %
+; CHECK-INTERESTINGNESS: = add <2 x i32>
+; CHECK-INTERESTINGNESS: = add <2 x i32>
+; CHECK-INTERESTINGNESS: = add <2 x i32>
+; CHECK-INTERESTINGNESS: = add <2 x i32>
+; CHECK-INTERESTINGNESS: = add <2 x i32>
+
+; CHECK-LABEL: define void @foo(
+
+
+; ONE: %add0 = add i32 %arg0, 1
+; ONE: %add1 = add i32 1, 1
+; ONE: %add2 = add i32 1, 0
+; ONE: %add3 = add i32 1, 1
+; ONE: %add4 = add i32 1, 1
+; ONE: %add5 = add <2 x i32> %arg2, <i32 1, i32 1>
+; ONE: %add6 = add <2 x i32> <i32 1, i32 1>, <i32 1, i32 1>
+; ONE: %add7 = add <2 x i32> <i32 1, i32 1>, zeroinitializer
+; ONE: %add8 = add <2 x i32> <i32 1, i32 1>, <i32 1, i32 1>
+; ONE: %add9 = add <2 x i32> <i32 1, i32 1>, <i32 1, i32 1>
+; ONE: %add10 = add <2 x i32> <i32 1, i32 1>, <i32 1, i32 1>
+
+
+; ZERO: %add0 = add i32 %arg0, 0
+; ZERO: %add1 = add i32 0, 0
+; ZERO: %add2 = add i32 0, 0
+; ZERO: %add3 = add i32 0, 0
+; ZERO: %add4 = add i32 0, 0
+; ZERO: %add5 = add <2 x i32> %arg2, zeroinitializer
+; ZERO: %add6 = add <2 x i32> zeroinitializer, zeroinitializer
+; ZERO: %add7 = add <2 x i32> zeroinitializer, zeroinitializer
+; ZERO: %add8 = add <2 x i32> zeroinitializer, zeroinitializer
+; ZERO: %add9 = add <2 x i32> zeroinitializer, zeroinitializer
+; ZERO: %add10 = add <2 x i32> zeroinitializer, zeroinitializer
+
+define void @foo(i32 %arg0, i32 %arg1, <2 x i32> %arg2, <2 x i32> %arg3) {
+bb0:
+  %add0 = add i32 %arg0, %arg1
+  %add1 = add i32 %arg0, %arg1
+  %add2 = add i32 %arg0, 0
+  %add3 = add i32 %arg0, 1
+  %add4 = add i32 %arg0, undef
+  %add5 = add <2 x i32> %arg2, %arg3
+  %add6 = add <2 x i32> %arg2, %arg3
+  %add7 = add <2 x i32> %arg2, zeroinitializer
+  %add8 = add <2 x i32> %arg2, <i32 1, i32 1>
+  %add9 = add <2 x i32> %arg2, undef
+  %add10 = add <2 x i32> %arg2, <i32 4, i32 6>
+  ret void
+}
index d008a39..7c144eb 100644 (file)
@@ -79,9 +79,13 @@ void llvm::reduceOperandsOneDeltaPass(TestRunner &Test) {
       return isZeroOrOneFP(Op) ? nullptr : ConstantFP::get(Ty, 1.0);
 
     if (VectorType *VT = dyn_cast<VectorType>(Ty)) {
-      if (isZeroOrOneFP(Op))
+      if (isOne(Op) || isZero(Op) || isZeroOrOneFP(Op))
         return nullptr;
 
+      if (auto *IntTy = dyn_cast<IntegerType>(VT->getElementType()))
+        return ConstantVector::getSplat(VT->getElementCount(),
+                                        ConstantInt::get(IntTy, 1));
+
       return ConstantVector::getSplat(
           VT->getElementCount(), ConstantFP::get(VT->getElementType(), 1.0));
     }