Consolidate internal denormal flushing controls

Currently there are 4 different mechanisms for controlling denormal
flushing behavior, and about as many equivalent frontend controls.

- AMDGPU uses the fp32-denormals and fp64-f16-denormals subtarget features
- NVPTX uses the nvptx-f32ftz attribute
- ARM directly uses the denormal-fp-math attribute
- Other targets indirectly use denormal-fp-math in one DAGCombine
- cl-denorms-are-zero has a corresponding denorms-are-zero attribute

AMDGPU wants a distinct control for f32 flushing from f16/f64, and as
far as I can tell the same is true for NVPTX (based on the attribute
name).

Work on consolidating these into the denormal-fp-math attribute, and a
new type specific denormal-fp-math-f32 variant. Only ARM seems to
support the two different flush modes, so this is overkill for the
other use cases. Ideally we would error on the unsupported
positive-zero mode on other targets from somewhere.

Move the logic for selecting the flush mode into the compiler driver,
instead of handling it in cc1. denormal-fp-math/denormal-fp-math-f32
are now both cc1 flags, but denormal-fp-math-f32 is not yet exposed as
a user flag.

-cl-denorms-are-zero, -fcuda-flush-denormals-to-zero and
-fno-cuda-flush-denormals-to-zero will be mapped to
-fp-denormal-math-f32=ieee or preserve-sign rather than the old
attributes.

Stop emitting the denorms-are-zero attribute for the OpenCL flag. It
has no in-tree users. The meaning would also be target dependent, such
as the AMDGPU choice to treat this as only meaning allow flushing of
f32 and not f16 or f64. The naming is also potentially confusing,
since DAZ in other contexts refers to instructions implicitly treating
input denormals as zero, not necessarily flushing output denormals to
zero.

This also does not attempt to change the behavior for the current
attribute. The LangRef now states that the default is ieee behavior,
but this is inaccurate for the current implementation. The clang
handling is slightly hacky to avoid touching the existing
denormal-fp-math uses. Fixing this will be left for a future patch.

AMDGPU is still using the subtarget feature to control the denormal
mode, but the new attribute are now emitted. A future change will
switch this and remove the subtarget features.
This commit is contained in:
Matt Arsenault 2019-11-01 17:57:29 -07:00 committed by Matt Arsenault
parent 489503d50d
commit 857d566359
8 changed files with 47 additions and 15 deletions

View File

@ -1818,6 +1818,30 @@ example:
mode or that might alter the state of floating-point status flags that
might otherwise be set or cleared by calling this function. LLVM will
not introduce any new floating-point instructions that may trap.
``"denormal-fp-math"``
This indicates the denormal (subnormal) handling that may be assumed
for the default floating-point environment. This may be one of
``"ieee"``, ``"preserve-sign"``, or ``"positive-zero"``. If this
is attribute is not specified, the default is ``"ieee"``. If the
mode is ``"preserve-sign"``, or ``"positive-zero"``, denormal
outputs may be flushed to zero by standard floating point
operations. It is not mandated that flushing to zero occurs, but if
a denormal output is flushed to zero, it must respect the sign
mode. Not all targets support all modes. While this indicates the
expected floating point mode the function will be executed with,
this does not make any attempt to ensure the mode is
consistent. User or platform code is expected to set the floating
point mode appropriately before function entry.
``"denormal-fp-math-f32"``
Same as ``"denormal-fp-math"``, but only controls the behavior of
the 32-bit float type (or vectors of 32-bit floats). If both are
are present, this overrides ``"denormal-fp-math"``. Not all targets
support separately setting the denormal mode per type, and no
attempt is made to diagnose unsupported uses. Currently this
attribute is respected by the AMDGPU and NVPTX backends.
``"thunk"``
This attribute indicates that the function will delegate to some other
function with a tail call. The prototype of a thunk should not be used for

View File

@ -271,6 +271,16 @@ getOrCreateJumpTableInfo(unsigned EntryKind) {
}
DenormalMode MachineFunction::getDenormalMode(const fltSemantics &FPType) const {
if (&FPType == &APFloat::IEEEsingle()) {
Attribute Attr = F.getFnAttribute("denormal-fp-math-f32");
StringRef Val = Attr.getValueAsString();
if (!Val.empty())
return parseDenormalFPAttribute(Val);
// If the f32 variant of the attribute isn't specified, try to use the
// generic one.
}
// TODO: Should probably avoid the connection to the IR and store directly
// in the MachineFunction.
Attribute Attr = F.getFnAttribute("denormal-fp-math");

View File

@ -121,14 +121,10 @@ bool NVPTXTargetLowering::useF32FTZ(const MachineFunction &MF) const {
if (FtzEnabled.getNumOccurrences() > 0) {
// If nvptx-f32ftz is used on the command-line, always honor it
return FtzEnabled;
} else {
const Function &F = MF.getFunction();
// Otherwise, check for an nvptx-f32ftz attribute on the function
if (F.hasFnAttribute("nvptx-f32ftz"))
return F.getFnAttribute("nvptx-f32ftz").getValueAsString() == "true";
else
return false;
}
return MF.getDenormalMode(APFloat::IEEEsingle()) ==
DenormalMode::PreserveSign;
}
static bool IsPTXVectorType(MVT VT) {

View File

@ -15,6 +15,7 @@
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/APSInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/FloatingPointMode.h"
#include "llvm/ADT/None.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/STLExtras.h"
@ -1709,9 +1710,10 @@ static Instruction *SimplifyNVVMIntrinsic(IntrinsicInst *II, InstCombiner &IC) {
// intrinsic, we don't have to look up any module metadata, as
// FtzRequirementTy will be FTZ_Any.)
if (Action.FtzRequirement != FTZ_Any) {
bool FtzEnabled =
II->getFunction()->getFnAttribute("nvptx-f32ftz").getValueAsString() ==
"true";
StringRef Attr = II->getFunction()
->getFnAttribute("denormal-fp-math-f32")
.getValueAsString();
bool FtzEnabled = parseDenormalFPAttribute(Attr) != DenormalMode::IEEE;
if (FtzEnabled != (Action.FtzRequirement == FTZ_MustBeOn))
return nullptr;

View File

@ -162,4 +162,4 @@ define float @repeated_div_fast_ftz(i1 %pred, float %a, float %b, float %divisor
}
attributes #0 = { "unsafe-fp-math" = "true" }
attributes #1 = { "nvptx-f32ftz" = "true" }
attributes #1 = { "denormal-fp-math-f32" = "preserve-sign" }

View File

@ -289,4 +289,4 @@ define double @fma_double(double %a, double %b, double %c) {
}
attributes #0 = { nounwind readnone }
attributes #1 = { "nvptx-f32ftz" = "true" }
attributes #1 = { "denormal-fp-math-f32" = "preserve-sign" }

View File

@ -146,5 +146,5 @@ define double @test_sqrt64_refined_ftz(double %a) #0 #1 #2 {
}
attributes #0 = { "unsafe-fp-math" = "true" }
attributes #1 = { "nvptx-f32ftz" = "true" }
attributes #1 = { "denormal-fp-math-f32" = "preserve-sign" }
attributes #2 = { "reciprocal-estimates" = "rsqrtf:1,rsqrtd:1,sqrtf:1,sqrtd:1" }

View File

@ -5,11 +5,11 @@
; hackery:
; RUN: cat %s > %t.ftz
; RUN: echo 'attributes #0 = { "nvptx-f32ftz" = "true" }' >> %t.ftz
; RUN: echo 'attributes #0 = { "denormal-fp-math-f32" = "preserve-sign" }' >> %t.ftz
; RUN: opt < %t.ftz -instcombine -S | FileCheck %s --check-prefix=CHECK --check-prefix=FTZ
; RUN: cat %s > %t.noftz
; RUN: echo 'attributes #0 = { "nvptx-f32ftz" = "false" }' >> %t.noftz
; RUN: echo 'attributes #0 = { "denormal-fp-math-f32" = "ieee" }' >> %t.noftz
; RUN: opt < %t.noftz -instcombine -S | FileCheck %s --check-prefix=CHECK --check-prefix=NOFTZ
; We handle nvvm intrinsics with ftz variants as follows: