Skip to content

Commit 0a8d421

Browse files
author
Sjoerd Meijer
committedAug 30, 2016
This adds new options -fdenormal-fp-math and passes through option -ffast-math
to CC1, which are translated to function attributes and can e.g. be mapped on build attributes FP_exceptions and FP_denormal. Setting these build attributes allows better selection of floating point libraries. Differential Revision: https://reviews.llvm.org/D23840 llvm-svn: 280064
1 parent 5a99207 commit 0a8d421

File tree

10 files changed

+79
-2
lines changed

10 files changed

+79
-2
lines changed
 

Diff for: ‎clang/docs/UsersManual.rst

+9
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,15 @@ are listed below.
10751075
``Inf``, and
10761076
* ``+0`` and ``-0`` are interchangeable.
10771077

1078+
.. option:: -fdenormal-fp-math=[values]
1079+
1080+
Select which denormal numbers the code is permitted to require.
1081+
1082+
Valid values are: ``ieee``, ``preserve-sign``, and ``positive-zero``,
1083+
which correspond to IEEE 754 denormal numbers, the sign of a
1084+
flushed-to-zero number is preserved in the sign of 0, denormals are
1085+
flushed to positive zero, respectively.
1086+
10781087
.. option:: -fwhole-program-vtables
10791088

10801089
Enable whole-program vtable optimizations, such as single-implementation

Diff for: ‎clang/include/clang/Driver/Options.td

+3-2
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ def : Flag<["-"], "fno-defer-pop">, Group<clang_ignored_gcc_optimization_f_Group
626626
def : Flag<["-"], "fextended-identifiers">, Group<clang_ignored_f_Group>;
627627
def : Flag<["-"], "fno-extended-identifiers">, Group<f_Group>, Flags<[Unsupported]>;
628628
def fhosted : Flag<["-"], "fhosted">, Group<f_Group>;
629+
def fdenormal_fp_math_EQ : Joined<["-"], "fdenormal-fp-math=">, Group<f_Group>, Flags<[CC1Option]>;
629630
def ffast_math : Flag<["-"], "ffast-math">, Group<f_Group>, Flags<[CC1Option]>,
630631
HelpText<"Allow aggressive, lossy floating-point optimizations">;
631632
def fno_fast_math : Flag<["-"], "fno-fast-math">, Group<f_Group>;
@@ -740,8 +741,8 @@ def fno_honor_infinities : Flag<["-"], "fno-honor-infinities">, Group<f_Group>;
740741
// This option was originally misspelt "infinites" [sic].
741742
def : Flag<["-"], "fhonor-infinites">, Alias<fhonor_infinities>;
742743
def : Flag<["-"], "fno-honor-infinites">, Alias<fno_honor_infinities>;
743-
def ftrapping_math : Flag<["-"], "ftrapping-math">, Group<f_Group>;
744-
def fno_trapping_math : Flag<["-"], "fno-trapping-math">, Group<f_Group>;
744+
def ftrapping_math : Flag<["-"], "ftrapping-math">, Group<f_Group>, Flags<[CC1Option]>;
745+
def fno_trapping_math : Flag<["-"], "fno-trapping-math">, Group<f_Group>, Flags<[CC1Option]>;
745746
def ffp_contract : Joined<["-"], "ffp-contract=">, Group<f_Group>,
746747
Flags<[CC1Option]>, HelpText<"Form fused FP ops (e.g. FMAs): fast (everywhere)"
747748
" | on (according to FP_CONTRACT pragma, default) | off (never fuse)">;

Diff for: ‎clang/include/clang/Frontend/CodeGenOptions.def

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ CODEGENOPT(NoImplicitFloat , 1, 0) ///< Set when -mno-implicit-float is enable
107107
CODEGENOPT(NoInfsFPMath , 1, 0) ///< Assume FP arguments, results not +-Inf.
108108
CODEGENOPT(NoSignedZeros , 1, 0) ///< Allow ignoring the signedness of FP zero
109109
CODEGENOPT(ReciprocalMath , 1, 0) ///< Allow FP divisions to be reassociated.
110+
CODEGENOPT(NoTrappingMath , 1, 0) ///< Set when -fno-trapping-math is enabled.
110111
CODEGENOPT(NoInline , 1, 0) ///< Set when -fno-inline is enabled.
111112
///< Disables use of the inline keyword.
112113
CODEGENOPT(NoNaNsFPMath , 1, 0) ///< Assume FP arguments, results not NaN.

Diff for: ‎clang/include/clang/Frontend/CodeGenOptions.h

+3
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ class CodeGenOptions : public CodeGenOptionsBase {
121121
/// The ABI to use for passing floating point arguments.
122122
std::string FloatABI;
123123

124+
/// The floating-point denormal mode to use.
125+
std::string FPDenormalMode;
126+
124127
/// The float precision limit to use, if non-empty.
125128
std::string LimitFloatPrecision;
126129

Diff for: ‎clang/lib/CodeGen/CGCall.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,13 @@ void CodeGenModule::ConstructAttributeList(
17221722

17231723
FuncAttrs.addAttribute("less-precise-fpmad",
17241724
llvm::toStringRef(CodeGenOpts.LessPreciseFPMAD));
1725+
1726+
if (!CodeGenOpts.FPDenormalMode.empty())
1727+
FuncAttrs.addAttribute("denormal-fp-math",
1728+
CodeGenOpts.FPDenormalMode);
1729+
1730+
FuncAttrs.addAttribute("no-trapping-math",
1731+
llvm::toStringRef(CodeGenOpts.NoTrappingMath));
17251732
FuncAttrs.addAttribute("no-infs-fp-math",
17261733
llvm::toStringRef(CodeGenOpts.NoInfsFPMath));
17271734
FuncAttrs.addAttribute("no-nans-fp-math",

Diff for: ‎clang/lib/Driver/Tools.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -4369,6 +4369,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
43694369
if (ReciprocalMath)
43704370
CmdArgs.push_back("-freciprocal-math");
43714371

4372+
if (!TrappingMath)
4373+
CmdArgs.push_back("-fno-trapping-math");
4374+
4375+
if (Args.hasArg(options::OPT_fdenormal_fp_math_EQ))
4376+
Args.AddLastArg(CmdArgs, options::OPT_fdenormal_fp_math_EQ);
4377+
43724378
// Validate and pass through -fp-contract option.
43734379
if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
43744380
options::OPT_fno_fast_math,

Diff for: ‎clang/lib/Frontend/CompilerInvocation.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
576576
Opts.CorrectlyRoundedDivSqrt =
577577
Args.hasArg(OPT_cl_fp32_correctly_rounded_divide_sqrt);
578578
Opts.ReciprocalMath = Args.hasArg(OPT_freciprocal_math);
579+
Opts.NoTrappingMath = Args.hasArg(OPT_fno_trapping_math);
579580
Opts.NoZeroInitializedInBSS = Args.hasArg(OPT_mno_zero_initialized_in_bss);
580581
Opts.BackendOptions = Args.getAllArgValues(OPT_backend_option);
581582
Opts.NumRegisterParameters = getLastArgIntValue(Args, OPT_mregparm, 0, Diags);
@@ -794,6 +795,18 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
794795
Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Val;
795796
}
796797

798+
if (Arg *A = Args.getLastArg(OPT_fdenormal_fp_math_EQ)) {
799+
StringRef Val = A->getValue();
800+
if (Val == "ieee")
801+
Opts.FPDenormalMode = "ieee";
802+
else if (Val == "preserve-sign")
803+
Opts.FPDenormalMode = "preserve-sign";
804+
else if (Val == "positive-zero")
805+
Opts.FPDenormalMode = "positive-zero";
806+
else
807+
Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Val;
808+
}
809+
797810
if (Arg *A = Args.getLastArg(OPT_fpcc_struct_return, OPT_freg_struct_return)) {
798811
if (A->getOption().matches(OPT_fpcc_struct_return)) {
799812
Opts.setStructReturnConvention(CodeGenOptions::SRCK_OnStack);

Diff for: ‎clang/test/CodeGen/denormalfpmode.c

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %clang_cc1 -S -fdenormal-fp-math=ieee %s -emit-llvm -o - | FileCheck %s --check-prefix=CHECK-IEEE
2+
// RUN: %clang_cc1 -S -fdenormal-fp-math=preserve-sign %s -emit-llvm -o - | FileCheck %s --check-prefix=CHECK-PS
3+
// RUN: %clang_cc1 -S -fdenormal-fp-math=positive-zero %s -emit-llvm -o - | FileCheck %s --check-prefix=CHECK-PZ
4+
5+
// CHECK-LABEL: main
6+
// CHECK-IEEE: attributes #0 = {{.*}}"denormal-fp-math"="ieee"{{.*}}
7+
// CHECK-PS: attributes #0 = {{.*}}"denormal-fp-math"="preserve-sign"{{.*}}
8+
// CHECK-PZ: attributes #0 = {{.*}}"denormal-fp-math"="positive-zero"{{.*}}
9+
10+
int main() {
11+
return 0;
12+
}

Diff for: ‎clang/test/CodeGen/noexceptionsfpmath.c

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %clang_cc1 -S -fno-trapping-math %s -emit-llvm -o - | FileCheck %s
2+
3+
// CHECK-LABEL: main
4+
// CHECK: attributes #0 = {{.*}}"no-trapping-math"="true"{{.*}}
5+
6+
int main() {
7+
return 0;
8+
}

Diff for: ‎clang/test/Driver/fast-math.c

+17
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,20 @@
231231
// CHECK-NO-UNSAFE-MATH: "-cc1"
232232
// CHECK-NO-UNSAFE-MATH-NOT: "-menable-unsafe-fp-math"
233233
// CHECK-NO-UNSAFE-MATH: "-o"
234+
//
235+
// RUN: %clang -### -fexceptions-fp-math -fno-exceptions-fp-math -c %s 2>&1 \
236+
// RUN: | FileCheck --check-prefix=CHECK-NO-FP-EXCEPTIONS %s
237+
// RUN: %clang -### -fno-exceptions-fp-math -fexceptions-fp-math -c %s 2>&1 \
238+
// RUN: | FileCheck --check-prefix=CHECK-FP-EXCEPTIONS %s
239+
// CHECK-NO-FP-EXCEPTIONS: "-fno-exceptions-fp-math"
240+
// CHECK-FP-EXCEPTIONS-NOT: "-fno-exceptions-fp-math"
241+
//
242+
// RUN: %clang -### -fdenormal-fp-math=ieee -c %s 2>&1 \
243+
// RUN: | FileCheck --check-prefix=CHECK-FP-DENORMAL-IEEE %s
244+
// RUN: %clang -### -fdenormal-fp-math=preserve-sign -c %s 2>&1 \
245+
// RUN: | FileCheck --check-prefix=CHECK-FP-DENORMAL-PS %s
246+
// RUN: %clang -### -fdenormal-fp-math=positive-zero -c %s 2>&1 \
247+
// RUN: | FileCheck --check-prefix=CHECK-FP-DENORMAL-PZ %s
248+
// CHECK-FP-DENORMAL-IEEE: "-fdenormal-fp-math=ieee"
249+
// CHECK-FP-DENORMAL-PS: "-fdenormal-fp-math=preserve-sign"
250+
// CHECK-FP-DENORMAL-PZ: "-fdenormal-fp-math=positive-zero"

0 commit comments

Comments
 (0)
Please sign in to comment.