Skip to content

Commit a4451d8

Browse files
committed
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.
1 parent 592de00 commit a4451d8

File tree

32 files changed

+280
-137
lines changed

32 files changed

+280
-137
lines changed

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ CODEGENOPT(Reassociate , 1, 0) ///< Allow reassociation of FP math ops
157157
CODEGENOPT(ReciprocalMath , 1, 0) ///< Allow FP divisions to be reassociated.
158158
CODEGENOPT(NoTrappingMath , 1, 0) ///< Set when -fno-trapping-math is enabled.
159159
CODEGENOPT(NoNaNsFPMath , 1, 0) ///< Assume FP arguments, results not NaN.
160-
CODEGENOPT(FlushDenorm , 1, 0) ///< Allow FP denorm numbers to be flushed to zero
161160
CODEGENOPT(CorrectlyRoundedDivSqrt, 1, 0) ///< -cl-fp32-correctly-rounded-divide-sqrt
162161

163162
/// When false, this attempts to generate code as if the result of an

clang/include/clang/Basic/CodeGenOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ class CodeGenOptions : public CodeGenOptionsBase {
166166
/// The floating-point denormal mode to use.
167167
llvm::DenormalMode FPDenormalMode = llvm::DenormalMode::Invalid;
168168

169+
/// The floating-point subnormal mode to use, for float.
170+
llvm::DenormalMode FP32DenormalMode = llvm::DenormalMode::Invalid;
171+
169172
/// The float precision limit to use, if non-empty.
170173
std::string LimitFloatPrecision;
171174

clang/include/clang/Driver/CC1Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,9 @@ def cfguard_no_checks : Flag<["-"], "cfguard-no-checks">,
405405
def cfguard : Flag<["-"], "cfguard">,
406406
HelpText<"Emit Windows Control Flow Guard tables and checks">;
407407

408+
def fdenormal_fp_math_f32_EQ : Joined<["-"], "fdenormal-fp-math-f32=">,
409+
Group<f_Group>;
410+
408411
//===----------------------------------------------------------------------===//
409412
// Dependency Output Options
410413
//===----------------------------------------------------------------------===//

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ def cl_no_signed_zeros : Flag<["-"], "cl-no-signed-zeros">, Group<opencl_Group>,
523523
HelpText<"OpenCL only. Allow use of less precise no signed zeros computations in the generated binary.">;
524524
def cl_std_EQ : Joined<["-"], "cl-std=">, Group<opencl_Group>, Flags<[CC1Option]>,
525525
HelpText<"OpenCL language standard to compile for.">, Values<"cl,CL,cl1.1,CL1.1,cl1.2,CL1.2,cl2.0,CL2.0,clc++,CLC++">;
526-
def cl_denorms_are_zero : Flag<["-"], "cl-denorms-are-zero">, Group<opencl_Group>, Flags<[CC1Option]>,
526+
def cl_denorms_are_zero : Flag<["-"], "cl-denorms-are-zero">, Group<opencl_Group>,
527527
HelpText<"OpenCL only. Allow denormals to be flushed to zero.">;
528528
def cl_fp32_correctly_rounded_divide_sqrt : Flag<["-"], "cl-fp32-correctly-rounded-divide-sqrt">, Group<opencl_Group>, Flags<[CC1Option]>,
529529
HelpText<"OpenCL only. Specify that single precision floating-point divide and sqrt used in the program source are correctly rounded.">;
@@ -581,7 +581,7 @@ def cuda_path_ignore_env : Flag<["--"], "cuda-path-ignore-env">, Group<i_Group>,
581581
def ptxas_path_EQ : Joined<["--"], "ptxas-path=">, Group<i_Group>,
582582
HelpText<"Path to ptxas (used for compiling CUDA code)">;
583583
def fcuda_flush_denormals_to_zero : Flag<["-"], "fcuda-flush-denormals-to-zero">,
584-
Flags<[CC1Option]>, HelpText<"Flush denormal floating point values to zero in CUDA device mode.">;
584+
HelpText<"Flush denormal floating point values to zero in CUDA device mode.">;
585585
def fno_cuda_flush_denormals_to_zero : Flag<["-"], "fno-cuda-flush-denormals-to-zero">;
586586
def fcuda_approx_transcendentals : Flag<["-"], "fcuda-approx-transcendentals">,
587587
Flags<[CC1Option]>, HelpText<"Use approximate transcendental functions">;

clang/include/clang/Driver/ToolChain.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
#include "clang/Driver/Action.h"
1717
#include "clang/Driver/Multilib.h"
1818
#include "clang/Driver/Types.h"
19+
#include "llvm/ADT/APFloat.h"
1920
#include "llvm/ADT/ArrayRef.h"
21+
#include "llvm/ADT/FloatingPointMode.h"
2022
#include "llvm/ADT/SmallVector.h"
2123
#include "llvm/ADT/StringRef.h"
2224
#include "llvm/ADT/Triple.h"
@@ -606,6 +608,17 @@ class ToolChain {
606608
/// Returns true when it's possible to split LTO unit to use whole
607609
/// program devirtualization and CFI santiizers.
608610
virtual bool canSplitThinLTOUnit() const { return true; }
611+
612+
/// Returns the output denormal handling type in the default floating point
613+
/// environment for the given \p FPType if given. Otherwise, the default
614+
/// assumed mode for any floating point type.
615+
virtual llvm::DenormalMode getDefaultDenormalModeForType(
616+
const llvm::opt::ArgList &DriverArgs,
617+
Action::OffloadKind DeviceOffloadKind,
618+
const llvm::fltSemantics *FPType = nullptr) const {
619+
// FIXME: This should be IEEE when default handling is fixed.
620+
return llvm::DenormalMode::Invalid;
621+
}
609622
};
610623

611624
/// Set a ToolChain's effective triple. Reset it when the registration object

clang/lib/Basic/Targets/AMDGPU.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ void AMDGPUTargetInfo::adjustTargetOptions(const CodeGenOptions &CGOpts,
245245
}
246246
if (!hasFP32Denormals)
247247
TargetOpts.Features.push_back(
248-
(Twine(hasFastFMAF() && hasFullRateDenormalsF32() && !CGOpts.FlushDenorm
248+
(Twine(hasFastFMAF() && hasFullRateDenormalsF32() &&
249+
CGOpts.FP32DenormalMode == llvm::DenormalMode::IEEE
249250
? '+' : '-') + Twine("fp32-denormals"))
250251
.str());
251252
// Always do not flush fp64 or fp16 denorms.

clang/lib/CodeGen/CGCall.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,10 +1747,17 @@ void CodeGenModule::ConstructDefaultFnAttrList(StringRef Name, bool HasOptnone,
17471747

17481748
if (CodeGenOpts.NullPointerIsValid)
17491749
FuncAttrs.addAttribute("null-pointer-is-valid", "true");
1750+
1751+
// TODO: Omit attribute when the default is IEEE.
17501752
if (CodeGenOpts.FPDenormalMode != llvm::DenormalMode::Invalid)
17511753
FuncAttrs.addAttribute("denormal-fp-math",
17521754
llvm::denormalModeName(CodeGenOpts.FPDenormalMode));
17531755

1756+
if (CodeGenOpts.FP32DenormalMode != llvm::DenormalMode::Invalid)
1757+
FuncAttrs.addAttribute(
1758+
"denormal-fp-math-f32",
1759+
llvm::denormalModeName(CodeGenOpts.FP32DenormalMode));
1760+
17541761
FuncAttrs.addAttribute("no-trapping-math",
17551762
llvm::toStringRef(CodeGenOpts.NoTrappingMath));
17561763

@@ -1777,10 +1784,6 @@ void CodeGenModule::ConstructDefaultFnAttrList(StringRef Name, bool HasOptnone,
17771784
"correctly-rounded-divide-sqrt-fp-math",
17781785
llvm::toStringRef(CodeGenOpts.CorrectlyRoundedDivSqrt));
17791786

1780-
if (getLangOpts().OpenCL)
1781-
FuncAttrs.addAttribute("denorms-are-zero",
1782-
llvm::toStringRef(CodeGenOpts.FlushDenorm));
1783-
17841787
// TODO: Reciprocal estimate codegen options should apply to instructions?
17851788
const std::vector<std::string> &Recips = CodeGenOpts.Reciprocals;
17861789
if (!Recips.empty())
@@ -1813,10 +1816,6 @@ void CodeGenModule::ConstructDefaultFnAttrList(StringRef Name, bool HasOptnone,
18131816
if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice) {
18141817
// Exceptions aren't supported in CUDA device code.
18151818
FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1816-
1817-
// Respect -fcuda-flush-denormals-to-zero.
1818-
if (CodeGenOpts.FlushDenorm)
1819-
FuncAttrs.addAttribute("nvptx-f32ftz", "true");
18201819
}
18211820

18221821
for (StringRef Attr : CodeGenOpts.DefaultFunctionAttrs) {

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,8 @@ void CodeGenModule::Release() {
567567
// floating point values to 0. (This corresponds to its "__CUDA_FTZ"
568568
// property.)
569569
getModule().addModuleFlag(llvm::Module::Override, "nvvm-reflect-ftz",
570-
CodeGenOpts.FlushDenorm ? 1 : 0);
570+
CodeGenOpts.FP32DenormalMode !=
571+
llvm::DenormalMode::IEEE);
571572
}
572573

573574
// Emit OpenCL specific module metadata: OpenCL/SPIR version.

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "clang/Driver/Compilation.h"
1313
#include "clang/Driver/DriverDiagnostic.h"
1414
#include "llvm/Option/ArgList.h"
15+
#include "llvm/Support/TargetParser.h"
1516

1617
using namespace clang::driver;
1718
using namespace clang::driver::tools;
@@ -102,6 +103,40 @@ AMDGPUToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
102103
return DAL;
103104
}
104105

106+
llvm::DenormalMode AMDGPUToolChain::getDefaultDenormalModeForType(
107+
const llvm::opt::ArgList &DriverArgs, Action::OffloadKind DeviceOffloadKind,
108+
const llvm::fltSemantics *FPType) const {
109+
// Denormals should always be enabled for f16 and f64.
110+
if (!FPType || FPType != &llvm::APFloat::IEEEsingle())
111+
return llvm::DenormalMode::IEEE;
112+
113+
if (DeviceOffloadKind == Action::OFK_Cuda) {
114+
if (FPType && FPType == &llvm::APFloat::IEEEsingle() &&
115+
DriverArgs.hasFlag(options::OPT_fcuda_flush_denormals_to_zero,
116+
options::OPT_fno_cuda_flush_denormals_to_zero,
117+
false))
118+
return llvm::DenormalMode::PreserveSign;
119+
}
120+
121+
const StringRef GpuArch = DriverArgs.getLastArgValue(options::OPT_mcpu_EQ);
122+
auto Kind = llvm::AMDGPU::parseArchAMDGCN(GpuArch);
123+
124+
// Default to enabling f32 denormals by default on subtargets where fma is
125+
// fast with denormals
126+
127+
const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN(Kind);
128+
const bool DefaultDenormsAreZeroForTarget =
129+
(ArchAttr & llvm::AMDGPU::FEATURE_FAST_FMA_F32) &&
130+
(ArchAttr & llvm::AMDGPU::FEATURE_FAST_DENORMAL_F32);
131+
132+
// TODO: There are way too many flags that change this. Do we need to check
133+
// them all?
134+
bool DAZ = DriverArgs.hasArg(options::OPT_cl_denorms_are_zero) ||
135+
!DefaultDenormsAreZeroForTarget;
136+
// Outputs are flushed to zero, preserving sign
137+
return DAZ ? llvm::DenormalMode::PreserveSign : llvm::DenormalMode::IEEE;
138+
}
139+
105140
void AMDGPUToolChain::addClangTargetOptions(
106141
const llvm::opt::ArgList &DriverArgs,
107142
llvm::opt::ArgStringList &CC1Args,

clang/lib/Driver/ToolChains/AMDGPU.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUToolChain : public Generic_ELF {
6666
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
6767
llvm::opt::ArgStringList &CC1Args,
6868
Action::OffloadKind DeviceOffloadKind) const override;
69+
70+
llvm::DenormalMode getDefaultDenormalModeForType(
71+
const llvm::opt::ArgList &DriverArgs,
72+
Action::OffloadKind DeviceOffloadKind,
73+
const llvm::fltSemantics *FPType = nullptr) const override;
6974
};
7075

7176
} // end namespace toolchains

0 commit comments

Comments
 (0)