Skip to content

Commit 6b8bd0f

Browse files
committed
llvm-reduce: Support replacing FP values with 1.0
1 parent 2e0c460 commit 6b8bd0f

File tree

2 files changed

+114
-6
lines changed

2 files changed

+114
-6
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
; Test that llvm-reduce can reduce floating point operands
2+
;
3+
; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=operands-undef --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
4+
; RUN: FileCheck --check-prefixes=CHECK,UNDEF %s < %t
5+
6+
; 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
7+
; RUN: FileCheck --check-prefixes=CHECK,ONE %s < %t
8+
9+
; 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
10+
; RUN: FileCheck --check-prefixes=CHECK,ZERO %s < %t
11+
12+
; RUN: llvm-reduce --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
13+
; RUN: FileCheck --check-prefixes=CHECK,ZERO %s < %t
14+
15+
; CHECK-INTERESTINGNESS: = fadd float %
16+
; CHECK-INTERESTINGNESS: = fadd float
17+
; CHECK-INTERESTINGNESS: = fadd float
18+
; CHECK-INTERESTINGNESS: = fadd float
19+
; CHECK-INTERESTINGNESS: = fadd float
20+
; CHECK-INTERESTINGNESS: = fadd float
21+
22+
; CHECK-INTERESTINGNESS: = fadd <2 x float> %
23+
; CHECK-INTERESTINGNESS: = fadd <2 x float>
24+
; CHECK-INTERESTINGNESS: = fadd <2 x float>
25+
; CHECK-INTERESTINGNESS: = fadd <2 x float>
26+
; CHECK-INTERESTINGNESS: = fadd <2 x float>
27+
; CHECK-INTERESTINGNESS: = fadd <2 x float>
28+
29+
; CHECK-LABEL: define void @foo(
30+
31+
; UNDEF: %fadd0 = fadd float %arg0, undef
32+
; UNDEF: %fadd1 = fadd float undef, undef
33+
; UNDEF: %fadd2 = fadd float undef, 0.000000e+00
34+
; UNDEF: %fadd3 = fadd float undef, 1.000000e+00
35+
; UNDEF: %fadd4 = fadd float undef, 0x7FF8000000000000
36+
; UNDEF: %fadd5 = fadd float undef, undef
37+
; UNDEF: %fadd6 = fadd <2 x float> %arg2, undef
38+
; UNDEF: %fadd7 = fadd <2 x float> undef, undef
39+
; UNDEF: %fadd8 = fadd <2 x float> undef, zeroinitializer
40+
; UNDEF: %fadd9 = fadd <2 x float> undef, <float 1.000000e+00, float 1.000000e+00>
41+
; UNDEF: %fadd10 = fadd <2 x float> undef, undef
42+
; UNDEF: %fadd11 = fadd <2 x float> undef, <float 0x7FF8000000000000, float 0x7FF8000000000000>
43+
44+
45+
; ONE: %fadd0 = fadd float %arg0, 1.000000e+00
46+
; ONE: %fadd1 = fadd float 1.000000e+00, 1.000000e+00
47+
; ONE: %fadd2 = fadd float 1.000000e+00, 0.000000e+00
48+
; ONE: %fadd3 = fadd float 1.000000e+00, 1.000000e+00
49+
; ONE: %fadd4 = fadd float 1.000000e+00, 1.000000e+00
50+
; ONE: %fadd5 = fadd float 1.000000e+00, 1.000000e+00
51+
; ONE: %fadd6 = fadd <2 x float> %arg2, <float 1.000000e+00, float 1.000000e+00>
52+
; ONE: %fadd7 = fadd <2 x float> <float 1.000000e+00, float 1.000000e+00>, <float 1.000000e+00, float 1.000000e+00>
53+
; ONE: %fadd8 = fadd <2 x float> <float 1.000000e+00, float 1.000000e+00>, zeroinitializer
54+
; ONE: %fadd9 = fadd <2 x float> <float 1.000000e+00, float 1.000000e+00>, <float 1.000000e+00, float 1.000000e+00>
55+
; ONE: %fadd10 = fadd <2 x float> <float 1.000000e+00, float 1.000000e+00>, <float 1.000000e+00, float 1.000000e+00>
56+
; ONE: %fadd11 = fadd <2 x float> <float 1.000000e+00, float 1.000000e+00>, <float 1.000000e+00, float 1.000000e+00>
57+
58+
59+
; ZERO: %fadd0 = fadd float %arg0, 0.000000e+00
60+
; ZERO: %fadd1 = fadd float 0.000000e+00, 0.000000e+00
61+
; ZERO: %fadd2 = fadd float 0.000000e+00, 0.000000e+00
62+
; ZERO: %fadd3 = fadd float 0.000000e+00, 0.000000e+00
63+
; ZERO: %fadd4 = fadd float 0.000000e+00, 0.000000e+00
64+
; ZERO: %fadd5 = fadd float 0.000000e+00, 0.000000e+00
65+
; ZERO: %fadd6 = fadd <2 x float> %arg2, zeroinitializer
66+
; ZERO: %fadd7 = fadd <2 x float> zeroinitializer, zeroinitializer
67+
; ZERO: %fadd8 = fadd <2 x float> zeroinitializer, zeroinitializer
68+
; ZERO: %fadd9 = fadd <2 x float> zeroinitializer, zeroinitializer
69+
; ZERO: %fadd10 = fadd <2 x float> zeroinitializer, zeroinitializer
70+
; ZERO: %fadd11 = fadd <2 x float> zeroinitializer, zeroinitializer
71+
72+
define void @foo(float %arg0, float %arg1, <2 x float> %arg2, <2 x float> %arg3) {
73+
bb0:
74+
%fadd0 = fadd float %arg0, %arg1
75+
%fadd1 = fadd float %arg0, %arg1
76+
%fadd2 = fadd float %arg0, 0.0
77+
%fadd3 = fadd float %arg0, 1.0
78+
%fadd4 = fadd float %arg0, 0x7FF8000000000000
79+
%fadd5 = fadd float %arg0, undef
80+
%fadd6 = fadd <2 x float> %arg2, %arg3
81+
%fadd7 = fadd <2 x float> %arg2, %arg3
82+
%fadd8 = fadd <2 x float> %arg2, zeroinitializer
83+
%fadd9 = fadd <2 x float> %arg2, <float 1.0, float 1.0>
84+
%fadd10 = fadd <2 x float> %arg2, undef
85+
%fadd11 = fadd <2 x float> %arg2, <float 0x7FF8000000000000, float 0x7FF8000000000000>
86+
ret void
87+
}

llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
#include "llvm/IR/InstIterator.h"
1212
#include "llvm/IR/InstrTypes.h"
1313
#include "llvm/IR/Operator.h"
14+
#include "llvm/IR/PatternMatch.h"
1415
#include "llvm/IR/Type.h"
1516

1617
using namespace llvm;
18+
using namespace PatternMatch;
1719

1820
static void
1921
extractOperandsFromModule(Oracle &O, Module &Program,
@@ -39,6 +41,12 @@ static bool isZero(Use &Op) {
3941
return C && C->isNullValue();
4042
}
4143

44+
static bool isZeroOrOneFP(Value *Op) {
45+
const APFloat *C;
46+
return match(Op, m_APFloat(C)) &&
47+
((C->isZero() && !C->isNegative()) || C->isExactlyValue(1.0));
48+
}
49+
4250
static bool shouldReduceOperand(Use &Op) {
4351
Type *Ty = Op->getType();
4452
if (Ty->isLabelTy() || Ty->isMetadataTy())
@@ -70,14 +78,27 @@ void llvm::reduceOperandsUndefDeltaPass(TestRunner &Test) {
7078
void llvm::reduceOperandsOneDeltaPass(TestRunner &Test) {
7179
errs() << "*** Reducing Operands to one...\n";
7280
auto ReduceValue = [](Use &Op) -> Value * {
73-
// TODO: support floats
7481
if (!shouldReduceOperand(Op))
7582
return nullptr;
76-
auto *Ty = dyn_cast<IntegerType>(Op->getType());
77-
if (!Ty)
78-
return nullptr;
79-
// Don't replace existing ones and zeroes.
80-
return (isOne(Op) || isZero(Op)) ? nullptr : ConstantInt::get(Ty, 1);
83+
84+
Type *Ty = Op->getType();
85+
if (auto *IntTy = dyn_cast<IntegerType>(Ty)) {
86+
// Don't replace existing ones and zeroes.
87+
return (isOne(Op) || isZero(Op)) ? nullptr : ConstantInt::get(IntTy, 1);
88+
}
89+
90+
if (Ty->isFloatingPointTy())
91+
return isZeroOrOneFP(Op) ? nullptr : ConstantFP::get(Ty, 1.0);
92+
93+
if (VectorType *VT = dyn_cast<VectorType>(Ty)) {
94+
if (isZeroOrOneFP(Op))
95+
return nullptr;
96+
97+
return ConstantVector::getSplat(
98+
VT->getElementCount(), ConstantFP::get(VT->getElementType(), 1.0));
99+
}
100+
101+
return nullptr;
81102
};
82103
runDeltaPass(Test, [ReduceValue](Oracle &O, Module &Program) {
83104
extractOperandsFromModule(O, Program, ReduceValue);

0 commit comments

Comments
 (0)