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

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

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2480,7 +2480,8 @@ static void CollectArgsForIntegratedAssembler(Compilation &C,
24802480

24812481
static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
24822482
bool OFastEnabled, const ArgList &Args,
2483-
ArgStringList &CmdArgs) {
2483+
ArgStringList &CmdArgs,
2484+
Action::OffloadKind DeviceOffloadKind) {
24842485
// Handle various floating point optimization flags, mapping them to the
24852486
// appropriate LLVM code generation flags. This is complicated by several
24862487
// "umbrella" flags, so we do this by stepping through the flags incrementally
@@ -2502,10 +2503,18 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
25022503
StringRef FPModel = "";
25032504
// -ffp-exception-behavior options: strict, maytrap, ignore
25042505
StringRef FPExceptionBehavior = "";
2505-
StringRef DenormalFPMath = "";
2506+
const llvm::DenormalMode DefaultDenormalFPMath =
2507+
TC.getDefaultDenormalModeForType(Args, DeviceOffloadKind);
2508+
const llvm::DenormalMode DefaultDenormalFP32Math =
2509+
TC.getDefaultDenormalModeForType(Args, DeviceOffloadKind,
2510+
&llvm::APFloat::IEEEsingle());
2511+
2512+
llvm::DenormalMode DenormalFPMath = DefaultDenormalFPMath;
2513+
llvm::DenormalMode DenormalFP32Math = DefaultDenormalFP32Math;
25062514
StringRef FPContract = "";
25072515
bool StrictFPModel = false;
25082516

2517+
25092518
if (const Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
25102519
CmdArgs.push_back("-mlimit-float-precision");
25112520
CmdArgs.push_back(A->getValue());
@@ -2527,7 +2536,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
25272536
ReciprocalMath = false;
25282537
SignedZeros = true;
25292538
// -fno_fast_math restores default denormal and fpcontract handling
2530-
DenormalFPMath = "";
2539+
DenormalFPMath = DefaultDenormalFPMath;
25312540
FPContract = "";
25322541
StringRef Val = A->getValue();
25332542
if (OFastEnabled && !Val.equals("fast")) {
@@ -2621,7 +2630,19 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
26212630
break;
26222631

26232632
case options::OPT_fdenormal_fp_math_EQ:
2624-
DenormalFPMath = A->getValue();
2633+
DenormalFPMath = llvm::parseDenormalFPAttribute(A->getValue());
2634+
if (DenormalFPMath == llvm::DenormalMode::Invalid) {
2635+
D.Diag(diag::err_drv_invalid_value)
2636+
<< A->getAsString(Args) << A->getValue();
2637+
}
2638+
break;
2639+
2640+
case options::OPT_fdenormal_fp_math_f32_EQ:
2641+
DenormalFP32Math = llvm::parseDenormalFPAttribute(A->getValue());
2642+
if (DenormalFP32Math == llvm::DenormalMode::Invalid) {
2643+
D.Diag(diag::err_drv_invalid_value)
2644+
<< A->getAsString(Args) << A->getValue();
2645+
}
26252646
break;
26262647

26272648
// Validate and pass through -ffp-contract option.
@@ -2691,7 +2712,8 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
26912712
TrappingMath = true;
26922713
FPExceptionBehavior = "strict";
26932714
// -fno_unsafe_math_optimizations restores default denormal handling
2694-
DenormalFPMath = "";
2715+
DenormalFPMath = DefaultDenormalFPMath;
2716+
DenormalFP32Math = DefaultDenormalFP32Math;
26952717
break;
26962718

26972719
case options::OPT_Ofast:
@@ -2724,17 +2746,20 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
27242746
TrappingMath = false;
27252747
RoundingFPMath = false;
27262748
// -fno_fast_math restores default denormal and fpcontract handling
2727-
DenormalFPMath = "";
2749+
DenormalFPMath = DefaultDenormalFPMath;
2750+
DenormalFP32Math = DefaultDenormalFP32Math;
27282751
FPContract = "";
27292752
break;
27302753
}
27312754
if (StrictFPModel) {
27322755
// If -ffp-model=strict has been specified on command line but
27332756
// subsequent options conflict then emit warning diagnostic.
2757+
// TODO: How should this interact with DenormalFP32Math?
27342758
if (HonorINFs && HonorNaNs &&
27352759
!AssociativeMath && !ReciprocalMath &&
27362760
SignedZeros && TrappingMath && RoundingFPMath &&
2737-
DenormalFPMath.empty() && FPContract.empty())
2761+
DenormalFPMath != llvm::DenormalMode::IEEE &&
2762+
FPContract.empty())
27382763
// OK: Current Arg doesn't conflict with -ffp-model=strict
27392764
;
27402765
else {
@@ -2780,9 +2805,16 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
27802805
} else if (TrappingMathPresent)
27812806
CmdArgs.push_back("-fno-trapping-math");
27822807

2783-
if (!DenormalFPMath.empty())
2784-
CmdArgs.push_back(
2785-
Args.MakeArgString("-fdenormal-fp-math=" + DenormalFPMath));
2808+
// TODO: Omit flag for the default IEEE instead
2809+
if (DenormalFPMath != llvm::DenormalMode::Invalid) {
2810+
CmdArgs.push_back(Args.MakeArgString(
2811+
"-fdenormal-fp-math=" + llvm::denormalModeName(DenormalFPMath)));
2812+
}
2813+
2814+
if (DenormalFP32Math != llvm::DenormalMode::Invalid) {
2815+
CmdArgs.push_back(Args.MakeArgString(
2816+
"-fdenormal-fp-math-f32=" + llvm::denormalModeName(DenormalFP32Math)));
2817+
}
27862818

27872819
if (!FPContract.empty())
27882820
CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + FPContract));
@@ -3002,6 +3034,8 @@ static void RenderTrivialAutoVarInitOptions(const Driver &D,
30023034
}
30033035

30043036
static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs) {
3037+
// cl-denorms-are-zero is not forwarded. It is translated into a generic flag
3038+
// for denormal flushing handling based on the target.
30053039
const unsigned ForwardedArguments[] = {
30063040
options::OPT_cl_opt_disable,
30073041
options::OPT_cl_strict_aliasing,
@@ -3012,7 +3046,6 @@ static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs) {
30123046
options::OPT_cl_fast_relaxed_math,
30133047
options::OPT_cl_mad_enable,
30143048
options::OPT_cl_no_signed_zeros,
3015-
options::OPT_cl_denorms_are_zero,
30163049
options::OPT_cl_fp32_correctly_rounded_divide_sqrt,
30173050
options::OPT_cl_uniform_work_group_size
30183051
};
@@ -4195,7 +4228,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
41954228
CmdArgs.push_back("-mdisable-tail-calls");
41964229

41974230
RenderFloatingPointOptions(TC, D, isOptimizationLevelFast(Args), Args,
4198-
CmdArgs);
4231+
CmdArgs, JA.getOffloadingDeviceKind());
41994232

42004233
// Render ABI arguments
42014234
switch (TC.getArch()) {
@@ -4495,7 +4528,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
44954528
if (Args.hasArg(options::OPT_fsplit_stack))
44964529
CmdArgs.push_back("-split-stacks");
44974530

4498-
RenderFloatingPointOptions(TC, D, OFastEnabled, Args, CmdArgs);
4531+
RenderFloatingPointOptions(TC, D, OFastEnabled, Args, CmdArgs,
4532+
JA.getOffloadingDeviceKind());
44994533

45004534
if (Arg *A = Args.getLastArg(options::OPT_LongDouble_Group)) {
45014535
if (TC.getTriple().isX86())

clang/lib/Driver/ToolChains/Cuda.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/Support/Path.h"
2222
#include "llvm/Support/Process.h"
2323
#include "llvm/Support/Program.h"
24+
#include "llvm/Support/TargetParser.h"
2425
#include "llvm/Support/VirtualFileSystem.h"
2526
#include <system_error>
2627

@@ -614,10 +615,6 @@ void CudaToolChain::addClangTargetOptions(
614615
if (DeviceOffloadingKind == Action::OFK_Cuda) {
615616
CC1Args.push_back("-fcuda-is-device");
616617

617-
if (DriverArgs.hasFlag(options::OPT_fcuda_flush_denormals_to_zero,
618-
options::OPT_fno_cuda_flush_denormals_to_zero, false))
619-
CC1Args.push_back("-fcuda-flush-denormals-to-zero");
620-
621618
if (DriverArgs.hasFlag(options::OPT_fcuda_approx_transcendentals,
622619
options::OPT_fno_cuda_approx_transcendentals, false))
623620
CC1Args.push_back("-fcuda-approx-transcendentals");
@@ -718,6 +715,21 @@ void CudaToolChain::addClangTargetOptions(
718715
}
719716
}
720717

718+
llvm::DenormalMode CudaToolChain::getDefaultDenormalModeForType(
719+
const llvm::opt::ArgList &DriverArgs, Action::OffloadKind DeviceOffloadKind,
720+
const llvm::fltSemantics *FPType) const {
721+
if (DeviceOffloadKind == Action::OFK_Cuda) {
722+
if (FPType && FPType == &llvm::APFloat::IEEEsingle() &&
723+
DriverArgs.hasFlag(options::OPT_fcuda_flush_denormals_to_zero,
724+
options::OPT_fno_cuda_flush_denormals_to_zero,
725+
false))
726+
return llvm::DenormalMode::PreserveSign;
727+
}
728+
729+
assert(DeviceOffloadKind != Action::OFK_Host);
730+
return llvm::DenormalMode::IEEE;
731+
}
732+
721733
bool CudaToolChain::supportsDebugInfoOption(const llvm::opt::Arg *A) const {
722734
const Option &O = A->getOption();
723735
return (O.matches(options::OPT_gN_Group) &&

clang/lib/Driver/ToolChains/Cuda.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ class LLVM_LIBRARY_VISIBILITY CudaToolChain : public ToolChain {
149149
llvm::opt::ArgStringList &CC1Args,
150150
Action::OffloadKind DeviceOffloadKind) const override;
151151

152+
llvm::DenormalMode getDefaultDenormalModeForType(
153+
const llvm::opt::ArgList &DriverArgs,
154+
Action::OffloadKind DeviceOffloadKind,
155+
const llvm::fltSemantics *FPType = nullptr) const override;
156+
152157
// Never try to use the integrated assembler with CUDA; always fork out to
153158
// ptxas.
154159
bool useIntegratedAs() const override { return false; }

0 commit comments

Comments
 (0)