CodeGen: Add -denormal-fp-math-f32 flag

Make the set of FP related attributes and command flags closer.
This commit is contained in:
Matt Arsenault 2019-12-04 14:34:14 +05:30
parent a6dfd827e5
commit a8cc9047de
7 changed files with 122 additions and 23 deletions

View File

@ -64,6 +64,7 @@ bool getEnableNoSignedZerosFPMath();
bool getEnableNoTrappingFPMath();
DenormalMode::DenormalModeKind getDenormalFPMath();
DenormalMode::DenormalModeKind getDenormalFP32Math();
bool getEnableHonorSignDependentRoundingFPMath();

View File

@ -55,6 +55,7 @@ CGOPT(bool, EnableNoNaNsFPMath)
CGOPT(bool, EnableNoSignedZerosFPMath)
CGOPT(bool, EnableNoTrappingFPMath)
CGOPT(DenormalMode::DenormalModeKind, DenormalFPMath)
CGOPT(DenormalMode::DenormalModeKind, DenormalFP32Math)
CGOPT(bool, EnableHonorSignDependentRoundingFPMath)
CGOPT(FloatABI::ABIType, FloatABIForCalls)
CGOPT(FPOpFusion::FPOpFusionMode, FuseFPOps)
@ -212,20 +213,30 @@ codegen::RegisterCodeGenFlags::RegisterCodeGenFlags() {
cl::init(false));
CGBINDOPT(EnableNoTrappingFPMath);
static const auto DenormFlagEnumOptions =
cl::values(clEnumValN(DenormalMode::IEEE, "ieee",
"IEEE 754 denormal numbers"),
clEnumValN(DenormalMode::PreserveSign, "preserve-sign",
"the sign of a flushed-to-zero number is preserved "
"in the sign of 0"),
clEnumValN(DenormalMode::PositiveZero, "positive-zero",
"denormals are flushed to positive zero"));
// FIXME: Doesn't have way to specify separate input and output modes.
static cl::opt<DenormalMode::DenormalModeKind> DenormalFPMath(
"denormal-fp-math",
cl::desc(
"Select which denormal numbers the code is permitted to require"),
cl::init(DenormalMode::IEEE),
cl::values(
clEnumValN(DenormalMode::IEEE, "ieee", "IEEE 754 denormal numbers"),
clEnumValN(DenormalMode::PreserveSign, "preserve-sign",
"the sign of a flushed-to-zero number is preserved "
"in the sign of 0"),
clEnumValN(DenormalMode::PositiveZero, "positive-zero",
"denormals are flushed to positive zero")));
"denormal-fp-math",
cl::desc("Select which denormal numbers the code is permitted to require"),
cl::init(DenormalMode::IEEE),
DenormFlagEnumOptions);
CGBINDOPT(DenormalFPMath);
static cl::opt<DenormalMode::DenormalModeKind> DenormalFP32Math(
"denormal-fp-math-f32",
cl::desc("Select which denormal numbers the code is permitted to require for float"),
cl::init(DenormalMode::Invalid),
DenormFlagEnumOptions);
CGBINDOPT(DenormalFP32Math);
static cl::opt<bool> EnableHonorSignDependentRoundingFPMath(
"enable-sign-dependent-rounding-fp-math", cl::Hidden,
cl::desc("Force codegen to assume rounding mode can change dynamically"),
@ -577,6 +588,16 @@ void codegen::setFunctionAttributes(StringRef CPU, StringRef Features,
DenormalMode(DenormKind, DenormKind).str());
}
if (DenormalFP32MathView->getNumOccurrences() > 0 &&
!F.hasFnAttribute("denormal-fp-math-f32")) {
// FIXME: Command line flag should expose separate input/output modes.
DenormalMode::DenormalModeKind DenormKind = getDenormalFP32Math();
NewAttrs.addAttribute(
"denormal-fp-math-f32",
DenormalMode(DenormKind, DenormKind).str());
}
if (TrapFuncNameView->getNumOccurrences() > 0)
for (auto &B : F)
for (auto &I : B)

View File

@ -88,11 +88,6 @@ static cl::opt<bool> UsePrecSqrtF32(
cl::desc("NVPTX Specific: 0 use sqrt.approx, 1 use sqrt.rn."),
cl::init(true));
static cl::opt<bool> FtzEnabled(
"nvptx-f32ftz", cl::ZeroOrMore, cl::Hidden,
cl::desc("NVPTX Specific: Flush f32 subnormals to sign-preserving zero."),
cl::init(false));
int NVPTXTargetLowering::getDivF32Level() const {
if (UsePrecDivF32.getNumOccurrences() > 0) {
// If nvptx-prec-div32=N is used on the command-line, always honor it
@ -117,12 +112,6 @@ bool NVPTXTargetLowering::usePrecSqrtF32() const {
}
bool NVPTXTargetLowering::useF32FTZ(const MachineFunction &MF) const {
// TODO: Get rid of this flag; there can be only one way to do this.
if (FtzEnabled.getNumOccurrences() > 0) {
// If nvptx-f32ftz is used on the command-line, always honor it
return FtzEnabled;
}
return MF.getDenormalMode(APFloat::IEEEsingle()).Output ==
DenormalMode::PreserveSign;
}

View File

@ -5,7 +5,7 @@
; ## Full FP16 with FTZ
; RUN: llc < %s -mtriple=nvptx64-nvidia-cuda -mcpu=sm_53 -asm-verbose=false \
; RUN: -O0 -disable-post-ra -frame-pointer=all -verify-machineinstrs \
; RUN: -nvptx-f32ftz \
; RUN: -denormal-fp-math-f32=preserve-sign \
; RUN: | FileCheck -check-prefixes CHECK,CHECK-F16,CHECK-F16-FTZ %s
; ## FP16 support explicitly disabled.
; RUN: llc < %s -mtriple=nvptx64-nvidia-cuda -mcpu=sm_53 -asm-verbose=false \

View File

@ -0,0 +1,23 @@
; RUN: opt -S -denormal-fp-math-f32=ieee %s | FileCheck -check-prefixes=IEEE,ALL %s
; RUN: opt -S -denormal-fp-math-f32=preserve-sign %s | FileCheck -check-prefixes=PRESERVESIGN,ALL %s
; RUN: opt -S -denormal-fp-math-f32=positive-zero %s | FileCheck -check-prefixes=POSZERO,ALL %s
; ALL: @no_denormal_fp_math_f32_attr() [[NOATTR:#[0-9]+]] {
define i32 @no_denormal_fp_math_f32_attr() #0 {
entry:
ret i32 0
}
; ALL: denormal_fp_math_attr_preserve_sign_ieee() [[ATTR:#[0-9]+]] {
define i32 @denormal_fp_math_attr_preserve_sign_ieee() #1 {
entry:
ret i32 0
}
; ALL-DAG: attributes [[ATTR]] = { nounwind "denormal-fp-math-f32"="preserve-sign,ieee" }
; IEEE-DAG: attributes [[NOATTR]] = { nounwind "denormal-fp-math-f32"="ieee,ieee" }
; PRESERVESIGN-DAG: attributes [[NOATTR]] = { nounwind "denormal-fp-math-f32"="preserve-sign,preserve-sign" }
; POSITIVEZERO-DAG: attributes [[NOATTR]] = { nounwind "denormal-fp-math-f32"="positive-zero,positive-zero" }
attributes #0 = { nounwind }
attributes #1 = { nounwind "denormal-fp-math-f32"="preserve-sign,ieee" }

View File

@ -0,0 +1,42 @@
; RUN: opt -S -denormal-fp-math=ieee %s | FileCheck -check-prefixes=IEEE,ALL %s
; RUN: opt -S -denormal-fp-math=preserve-sign %s | FileCheck -check-prefixes=PRESERVESIGN,ALL %s
; RUN: opt -S -denormal-fp-math=positive-zero %s | FileCheck -check-prefixes=POSZERO,ALL %s
; RUN: opt -S -denormal-fp-math-f32=ieee %s | FileCheck -check-prefixes=IEEEF32,ALL %s
; RUN: opt -S -denormal-fp-math-f32=preserve-sign %s | FileCheck -check-prefixes=PRESERVESIGNF32,ALL %s
; RUN: opt -S -denormal-fp-math-f32=positive-zero %s | FileCheck -check-prefixes=POSZEROF32,ALL %s
; RUN: opt -S -denormal-fp-math=ieee -denormal-fp-math-f32=ieee %s | FileCheck -check-prefixes=IEEE-BOTH,ALL %s
; RUN: opt -S -denormal-fp-math=preserve-sign -denormal-fp-math-f32=preserve-sign %s | FileCheck -check-prefixes=PRESERVESIGN-BOTH,ALL %s
; RUN: opt -S -denormal-fp-math=positive-zero -denormal-fp-math-f32=positive-zero %s | FileCheck -check-prefixes=POSZERO-BOTH,ALL %s
; ALL: @no_denormal_fp_math_attrs() [[NOATTR:#[0-9]+]] {
define i32 @no_denormal_fp_math_attrs() #0 {
entry:
ret i32 0
}
; ALL: both_denormal_fp_math_attrs_preserve_sign_ieee() [[ATTR:#[0-9]+]] {
define i32 @both_denormal_fp_math_attrs_preserve_sign_ieee() #1 {
entry:
ret i32 0
}
; ALL-DAG: attributes [[ATTR]] = { nounwind "denormal-fp-math"="preserve-sign,ieee" "denormal-fp-math-f32"="preserve-sign,ieee" }
; IEEE-DAG: attributes [[NOATTR]] = { nounwind "denormal-fp-math"="ieee,ieee" }
; PRESERVESIGN-DAG: attributes [[NOATTR]] = { nounwind "denormal-fp-math"="preserve-sign,preserve-sign" }
; POSITIVEZERO-DAG: attributes [[NOATTR]] = { nounwind "denormal-fp-math"="positive-zero,positive-zero" }
; IEEEF32-DAG: attributes [[NOATTR]] = { nounwind "denormal-fp-math-f32"="ieee,ieee" }
; PRESERVESIGNF32-DAG: attributes [[NOATTR]] = { nounwind "denormal-fp-math-f32"="preserve-sign,preserve-sign" }
; POSITIVEZEROF32-DAG: attributes [[NOATTR]] = { nounwind "denormal-fp-math-f32"="positive-zero,positive-zero" }
; IEEE-BOTH-DAG: attributes [[NOATTR]] = { nounwind "denormal-fp-math"="ieee,ieee" "denormal-fp-math-f32"="ieee,ieee" }
; PRESERVESIGN-BOTH-DAG: attributes [[NOATTR]] = { nounwind "denormal-fp-math"="preserve-sign,preserve-sign" "denormal-fp-math-f32"="preserve-sign,preserve-sign" }
; POSITIVEZERO-BOTH-DAG: attributes [[NOATTR]] = { nounwind "denormal-fp-math"="positive-zero,positive-zero" "denormal-fp-math-f32"="positive-zero,positive-zero" }
attributes #0 = { nounwind }
attributes #1 = { nounwind "denormal-fp-math"="preserve-sign,ieee" "denormal-fp-math-f32"="preserve-sign,ieee" }

View File

@ -0,0 +1,23 @@
; RUN: opt -S -denormal-fp-math=ieee %s | FileCheck -check-prefixes=IEEE,ALL %s
; RUN: opt -S -denormal-fp-math=preserve-sign %s | FileCheck -check-prefixes=PRESERVESIGN,ALL %s
; RUN: opt -S -denormal-fp-math=positive-zero %s | FileCheck -check-prefixes=POSZERO,ALL %s
; ALL: @no_denormal_fp_math_attr() [[NOATTR:#[0-9]+]] {
define i32 @no_denormal_fp_math_attr() #0 {
entry:
ret i32 0
}
; ALL: denormal_fp_math_attr_preserve_sign_ieee() [[ATTR:#[0-9]+]] {
define i32 @denormal_fp_math_attr_preserve_sign_ieee() #1 {
entry:
ret i32 0
}
; ALL-DAG: attributes [[ATTR]] = { nounwind "denormal-fp-math"="preserve-sign,ieee" }
; IEEE-DAG: attributes [[NOATTR]] = { nounwind "denormal-fp-math"="ieee,ieee" }
; PRESERVESIGN-DAG: attributes [[NOATTR]] = { nounwind "denormal-fp-math"="preserve-sign,preserve-sign" }
; POSITIVEZERO-DAG: attributes [[NOATTR]] = { nounwind "denormal-fp-math"="positive-zero,positive-zero" }
attributes #0 = { nounwind }
attributes #1 = { nounwind "denormal-fp-math"="preserve-sign,ieee" }