llvm-reduce: Support replacing FP values with 1.0

This commit is contained in:
Matt Arsenault
2022-06-09 09:43:09 -04:00
committed by Matt Arsenault
parent 2e0c46044a
commit 6b8bd0f72d
2 changed files with 114 additions and 6 deletions

View File

@@ -0,0 +1,87 @@
; Test that llvm-reduce can reduce floating point operands
;
; 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
; RUN: FileCheck --check-prefixes=CHECK,UNDEF %s < %t
; 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 --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: = fadd float %
; CHECK-INTERESTINGNESS: = fadd float
; CHECK-INTERESTINGNESS: = fadd float
; CHECK-INTERESTINGNESS: = fadd float
; CHECK-INTERESTINGNESS: = fadd float
; CHECK-INTERESTINGNESS: = fadd float
; 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-INTERESTINGNESS: = fadd <2 x float>
; CHECK-INTERESTINGNESS: = fadd <2 x float>
; CHECK-LABEL: define void @foo(
; UNDEF: %fadd0 = fadd float %arg0, undef
; UNDEF: %fadd1 = fadd float undef, undef
; UNDEF: %fadd2 = fadd float undef, 0.000000e+00
; UNDEF: %fadd3 = fadd float undef, 1.000000e+00
; UNDEF: %fadd4 = fadd float undef, 0x7FF8000000000000
; UNDEF: %fadd5 = fadd float undef, undef
; UNDEF: %fadd6 = fadd <2 x float> %arg2, undef
; UNDEF: %fadd7 = fadd <2 x float> undef, undef
; UNDEF: %fadd8 = fadd <2 x float> undef, zeroinitializer
; UNDEF: %fadd9 = fadd <2 x float> undef, <float 1.000000e+00, float 1.000000e+00>
; UNDEF: %fadd10 = fadd <2 x float> undef, undef
; UNDEF: %fadd11 = fadd <2 x float> undef, <float 0x7FF8000000000000, float 0x7FF8000000000000>
; ONE: %fadd0 = fadd float %arg0, 1.000000e+00
; ONE: %fadd1 = fadd float 1.000000e+00, 1.000000e+00
; ONE: %fadd2 = fadd float 1.000000e+00, 0.000000e+00
; ONE: %fadd3 = fadd float 1.000000e+00, 1.000000e+00
; ONE: %fadd4 = fadd float 1.000000e+00, 1.000000e+00
; ONE: %fadd5 = fadd float 1.000000e+00, 1.000000e+00
; ONE: %fadd6 = fadd <2 x float> %arg2, <float 1.000000e+00, float 1.000000e+00>
; ONE: %fadd7 = fadd <2 x float> <float 1.000000e+00, float 1.000000e+00>, <float 1.000000e+00, float 1.000000e+00>
; ONE: %fadd8 = fadd <2 x float> <float 1.000000e+00, float 1.000000e+00>, zeroinitializer
; 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>
; ZERO: %fadd0 = fadd float %arg0, 0.000000e+00
; ZERO: %fadd1 = fadd float 0.000000e+00, 0.000000e+00
; ZERO: %fadd2 = fadd float 0.000000e+00, 0.000000e+00
; ZERO: %fadd3 = fadd float 0.000000e+00, 0.000000e+00
; ZERO: %fadd4 = fadd float 0.000000e+00, 0.000000e+00
; ZERO: %fadd5 = fadd float 0.000000e+00, 0.000000e+00
; ZERO: %fadd6 = fadd <2 x float> %arg2, zeroinitializer
; ZERO: %fadd7 = fadd <2 x float> zeroinitializer, zeroinitializer
; ZERO: %fadd8 = fadd <2 x float> zeroinitializer, zeroinitializer
; ZERO: %fadd9 = fadd <2 x float> zeroinitializer, zeroinitializer
; ZERO: %fadd10 = fadd <2 x float> zeroinitializer, zeroinitializer
; ZERO: %fadd11 = fadd <2 x float> zeroinitializer, zeroinitializer
define void @foo(float %arg0, float %arg1, <2 x float> %arg2, <2 x float> %arg3) {
bb0:
%fadd0 = fadd float %arg0, %arg1
%fadd1 = fadd float %arg0, %arg1
%fadd2 = fadd float %arg0, 0.0
%fadd3 = fadd float %arg0, 1.0
%fadd4 = fadd float %arg0, 0x7FF8000000000000
%fadd5 = fadd float %arg0, undef
%fadd6 = fadd <2 x float> %arg2, %arg3
%fadd7 = fadd <2 x float> %arg2, %arg3
%fadd8 = fadd <2 x float> %arg2, zeroinitializer
%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>
ret void
}

View File

@@ -11,9 +11,11 @@
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/IR/Type.h"
using namespace llvm;
using namespace PatternMatch;
static void
extractOperandsFromModule(Oracle &O, Module &Program,
@@ -39,6 +41,12 @@ static bool isZero(Use &Op) {
return C && C->isNullValue();
}
static bool isZeroOrOneFP(Value *Op) {
const APFloat *C;
return match(Op, m_APFloat(C)) &&
((C->isZero() && !C->isNegative()) || C->isExactlyValue(1.0));
}
static bool shouldReduceOperand(Use &Op) {
Type *Ty = Op->getType();
if (Ty->isLabelTy() || Ty->isMetadataTy())
@@ -70,14 +78,27 @@ void llvm::reduceOperandsUndefDeltaPass(TestRunner &Test) {
void llvm::reduceOperandsOneDeltaPass(TestRunner &Test) {
errs() << "*** Reducing Operands to one...\n";
auto ReduceValue = [](Use &Op) -> Value * {
// TODO: support floats
if (!shouldReduceOperand(Op))
return nullptr;
auto *Ty = dyn_cast<IntegerType>(Op->getType());
if (!Ty)
return nullptr;
// Don't replace existing ones and zeroes.
return (isOne(Op) || isZero(Op)) ? nullptr : ConstantInt::get(Ty, 1);
Type *Ty = Op->getType();
if (auto *IntTy = dyn_cast<IntegerType>(Ty)) {
// Don't replace existing ones and zeroes.
return (isOne(Op) || isZero(Op)) ? nullptr : ConstantInt::get(IntTy, 1);
}
if (Ty->isFloatingPointTy())
return isZeroOrOneFP(Op) ? nullptr : ConstantFP::get(Ty, 1.0);
if (VectorType *VT = dyn_cast<VectorType>(Ty)) {
if (isZeroOrOneFP(Op))
return nullptr;
return ConstantVector::getSplat(
VT->getElementCount(), ConstantFP::get(VT->getElementType(), 1.0));
}
return nullptr;
};
runDeltaPass(Test, [ReduceValue](Oracle &O, Module &Program) {
extractOperandsFromModule(O, Program, ReduceValue);