Skip to content

Commit 359b105

Browse files
committedApr 9, 2015
Process the -freciprocal-math optimization flag (PR20912)
The driver currently accepts but ignores the -freciprocal-math flag. This patch passes the flag through and enables 'arcp' fast-math-flag generation in IR. Note that this change does not actually enable the optimization for any target. The reassociation optimization that this flag specifies was implemented by http://reviews.llvm.org/D6334 : http://llvm.org/viewvc/llvm-project?view=revision&revision=222510 Because the optimization is done in the backend rather than IR, the backend must be modified to understand instruction-level fast-math-flags or a new function-level attribute must be created. Also note that -freciprocal-math is independent of any target-specific usage of reciprocal estimate hardware instructions. That requires its own flag ('-mrecip'). https://llvm.org/bugs/show_bug.cgi?id=20912 llvm-svn: 234493
1 parent f4db546 commit 359b105

File tree

7 files changed

+29
-2
lines changed

7 files changed

+29
-2
lines changed
 

‎clang/include/clang/Driver/Options.td

+3-1
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,9 @@ def fno_unsafe_math_optimizations : Flag<["-"], "fno-unsafe-math-optimizations">
561561
Group<f_Group>;
562562
def fassociative_math : Flag<["-"], "fassociative-math">, Group<f_Group>;
563563
def fno_associative_math : Flag<["-"], "fno-associative-math">, Group<f_Group>;
564-
def freciprocal_math : Flag<["-"], "freciprocal-math">, Group<f_Group>;
564+
def freciprocal_math :
565+
Flag<["-"], "freciprocal-math">, Group<f_Group>, Flags<[CC1Option]>,
566+
HelpText<"Allow division operations to be reassociated">;
565567
def fno_reciprocal_math : Flag<["-"], "fno-reciprocal-math">, Group<f_Group>;
566568
def ffinite_math_only : Flag<["-"], "ffinite-math-only">, Group<f_Group>, Flags<[CC1Option]>;
567569
def fno_finite_math_only : Flag<["-"], "fno-finite-math-only">, Group<f_Group>;

‎clang/include/clang/Frontend/CodeGenOptions.def

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ CODEGENOPT(NoGlobalMerge , 1, 0) ///< Set when -mno-global-merge is enabled.
8181
CODEGENOPT(NoImplicitFloat , 1, 0) ///< Set when -mno-implicit-float is enabled.
8282
CODEGENOPT(NoInfsFPMath , 1, 0) ///< Assume FP arguments, results not +-Inf.
8383
CODEGENOPT(NoSignedZeros , 1, 0) ///< Allow ignoring the signedness of FP zero
84-
CODEGENOPT(NoInline , 1, 0) ///< Set when -fno-inline is enabled.
84+
CODEGENOPT(ReciprocalMath , 1, 0) ///< Allow FP divisions to be reassociated.
85+
CODEGENOPT(NoInline , 1, 0) ///< Set when -fno-inline is enabled.
8586
///< Disables use of the inline keyword.
8687
CODEGENOPT(NoNaNsFPMath , 1, 0) ///< Assume FP arguments, results not NaN.
8788
CODEGENOPT(NoZeroInitializedInBSS , 1, 0) ///< -fno-zero-initialized-in-bss.

‎clang/lib/CodeGen/CodeGenFunction.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ CodeGenFunction::CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext)
7070
if (CGM.getCodeGenOpts().NoSignedZeros) {
7171
FMF.setNoSignedZeros();
7272
}
73+
if (CGM.getCodeGenOpts().ReciprocalMath) {
74+
FMF.setAllowReciprocal();
75+
}
7376
Builder.SetFastMathFlags(FMF);
7477
}
7578

‎clang/lib/Driver/Tools.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -3062,6 +3062,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
30623062
if (!SignedZeros)
30633063
CmdArgs.push_back("-fno-signed-zeros");
30643064

3065+
if (ReciprocalMath)
3066+
CmdArgs.push_back("-freciprocal-math");
3067+
30653068
// Validate and pass through -fp-contract option.
30663069
if (Arg *A = Args.getLastArg(options::OPT_ffast_math, FastMathAliasOption,
30673070
options::OPT_fno_fast_math,

‎clang/lib/Frontend/CompilerInvocation.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
451451
Args.hasArg(OPT_cl_finite_math_only) ||
452452
Args.hasArg(OPT_cl_fast_relaxed_math));
453453
Opts.NoSignedZeros = Args.hasArg(OPT_fno_signed_zeros);
454+
Opts.ReciprocalMath = Args.hasArg(OPT_freciprocal_math);
454455
Opts.NoZeroInitializedInBSS = Args.hasArg(OPT_mno_zero_initialized_in_bss);
455456
Opts.BackendOptions = Args.getAllArgValues(OPT_backend_option);
456457
Opts.NumRegisterParameters = getLastArgIntValue(Args, OPT_mregparm, 0, Diags);

‎clang/test/CodeGen/finite-math.c

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %clang_cc1 -ffinite-math-only -emit-llvm -o - %s | FileCheck %s -check-prefix=CHECK -check-prefix=FINITE
22
// RUN: %clang_cc1 -fno-signed-zeros -emit-llvm -o - %s | FileCheck %s -check-prefix=CHECK -check-prefix=NSZ
3+
// RUN: %clang_cc1 -freciprocal-math -emit-llvm -o - %s | FileCheck %s -check-prefix=CHECK -check-prefix=RECIP
34

45
float f0, f1, f2;
56

@@ -8,6 +9,7 @@ void foo(void) {
89

910
// FINITE: fadd nnan ninf
1011
// NSZ: fadd nsz
12+
// RECIP: fadd arcp
1113
f0 = f1 + f2;
1214

1315
// CHECK: ret

‎clang/test/Driver/fast-math.c

+15
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@
4040
// CHECK-NO-SIGNED-ZEROS-NO-FAST-MATH: "-cc1"
4141
// CHECK-NO-SIGNED-ZEROS-NO-FAST-MATH-NOT: "-fno-signed-zeros"
4242
//
43+
// RUN: %clang -### -freciprocal-math -c %s 2>&1 \
44+
// RUN: | FileCheck --check-prefix=CHECK-RECIPROCAL-MATH %s
45+
// CHECK-RECIPROCAL-MATH: "-cc1"
46+
// CHECK-RECIPROCAL-MATH: "-freciprocal-math"
47+
//
48+
// RUN: %clang -### -fno-fast-math -freciprocal-math -c %s 2>&1 \
49+
// RUN: | FileCheck --check-prefix=CHECK-NO-FAST-MATH-RECIPROCAL-MATH %s
50+
// CHECK-NO-FAST-MATH-RECIPROCAL-MATH: "-cc1"
51+
// CHECK-NO-FAST-MATH-RECIPROCAL-MATH: "-freciprocal-math"
52+
//
53+
// RUN: %clang -### -freciprocal-math -fno-fast-math -c %s 2>&1 \
54+
// RUN: | FileCheck --check-prefix=CHECK-RECIPROCAL-MATH-NO-FAST-MATH %s
55+
// CHECK-RECIPROCAL-MATH-NO-FAST-MATH: "-cc1"
56+
// CHECK-RECIPROCAL-MATH-NO-FAST-MATH-NOT: "-freciprocal-math"
57+
//
4358
// RUN: %clang -### -fno-honor-nans -c %s 2>&1 \
4459
// RUN: | FileCheck --check-prefix=CHECK-NO-NANS %s
4560
// CHECK-NO-NANS: "-cc1"

0 commit comments

Comments
 (0)