Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 8e9263d

Browse files
committed
[Driver, CodeGen] pass through and apply -fassociative-math
There are 2 parts to getting the -fassociative-math command-line flag translated to LLVM FMF: 1. In the driver/frontend, we accept the flag and its 'no' inverse and deal with the interactions with other flags like -ffast-math -fno-signed-zeros -fno-trapping-math. This was mostly already done - we just need to translate the flag as a codegen option. The test file is complicated because there are many potential combinations of flags here. Note that we are matching gcc's behavior that requires 'nsz' and no-trapping-math. 2. In codegen, we map the codegen option to FMF in the IR builder. This is simple code and corresponding test. For the motivating example from PR27372: float foo(float a, float x) { return ((a + x) - x); } $ ./clang -O2 27372.c -S -o - -ffast-math -fno-associative-math -emit-llvm | egrep 'fadd|fsub' %add = fadd nnan ninf nsz arcp contract float %0, %1 %sub = fsub nnan ninf nsz arcp contract float %add, %2 So 'reassoc' is off as expected (and so is the new 'afn' but that's a different patch). This case now works as expected end-to-end although the underlying logic is still wrong: $ ./clang -O2 27372.c -S -o - -ffast-math -fno-associative-math | grep xmm addss %xmm1, %xmm0 subss %xmm1, %xmm0 We're not done because the case where 'reassoc' is set is ignored by optimizer passes. Example: $ ./clang -O2 27372.c -S -o - -fassociative-math -fno-signed-zeros -fno-trapping-math -emit-llvm | grep fadd %add = fadd reassoc float %0, %1 $ ./clang -O2 27372.c -S -o - -fassociative-math -fno-signed-zeros -fno-trapping-math | grep xmm addss %xmm1, %xmm0 subss %xmm1, %xmm0 Differential Revision: https://reviews.llvm.org/D39812 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@320920 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent d073eee commit 8e9263d

File tree

7 files changed

+62
-13
lines changed

7 files changed

+62
-13
lines changed

include/clang/Driver/CC1Options.td

+2
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ def menable_no_nans : Flag<["-"], "menable-no-nans">,
263263
def menable_unsafe_fp_math : Flag<["-"], "menable-unsafe-fp-math">,
264264
HelpText<"Allow unsafe floating-point math optimizations which may decrease "
265265
"precision">;
266+
def mreassociate : Flag<["-"], "mreassociate">,
267+
HelpText<"Allow reassociation transformations for floating-point instructions">;
266268
def mfloat_abi : Separate<["-"], "mfloat-abi">,
267269
HelpText<"The float ABI to use">;
268270
def mtp : Separate<["-"], "mtp">,

include/clang/Frontend/CodeGenOptions.def

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ CODEGENOPT(EnableSegmentedStacks , 1, 0) ///< Set when -fsplit-stack is enabled.
117117
CODEGENOPT(NoImplicitFloat , 1, 0) ///< Set when -mno-implicit-float is enabled.
118118
CODEGENOPT(NoInfsFPMath , 1, 0) ///< Assume FP arguments, results not +-Inf.
119119
CODEGENOPT(NoSignedZeros , 1, 0) ///< Allow ignoring the signedness of FP zero
120+
CODEGENOPT(Reassociate , 1, 0) ///< Allow reassociation of FP math ops
120121
CODEGENOPT(ReciprocalMath , 1, 0) ///< Allow FP divisions to be reassociated.
121122
CODEGENOPT(NoTrappingMath , 1, 0) ///< Set when -fno-trapping-math is enabled.
122123
CODEGENOPT(NoNaNsFPMath , 1, 0) ///< Assume FP arguments, results not NaN.

lib/CodeGen/CodeGenFunction.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ CodeGenFunction::CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext)
103103
if (CGM.getCodeGenOpts().ReciprocalMath) {
104104
FMF.setAllowReciprocal();
105105
}
106+
if (CGM.getCodeGenOpts().Reassociate) {
107+
FMF.setAllowReassoc();
108+
}
106109
Builder.setFastMathFlags(FMF);
107110
}
108111

lib/Driver/ToolChains/Clang.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -2170,6 +2170,9 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
21702170
if (!SignedZeros)
21712171
CmdArgs.push_back("-fno-signed-zeros");
21722172

2173+
if (AssociativeMath && !SignedZeros && !TrappingMath)
2174+
CmdArgs.push_back("-mreassociate");
2175+
21732176
if (ReciprocalMath)
21742177
CmdArgs.push_back("-freciprocal-math");
21752178

lib/Frontend/CompilerInvocation.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
639639
Args.hasArg(OPT_cl_no_signed_zeros) ||
640640
Args.hasArg(OPT_cl_unsafe_math_optimizations) ||
641641
Args.hasArg(OPT_cl_fast_relaxed_math));
642+
Opts.Reassociate = Args.hasArg(OPT_mreassociate);
642643
Opts.FlushDenorm = Args.hasArg(OPT_cl_denorms_are_zero);
643644
Opts.CorrectlyRoundedDivSqrt =
644645
Args.hasArg(OPT_cl_fp32_correctly_rounded_divide_sqrt);

test/CodeGen/finite-math.c

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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
33
// RUN: %clang_cc1 -freciprocal-math -emit-llvm -o - %s | FileCheck %s -check-prefix=CHECK -check-prefix=RECIP
4+
// RUN: %clang_cc1 -mreassociate -emit-llvm -o - %s | FileCheck %s -check-prefix=CHECK -check-prefix=REASSOC
45

56
float f0, f1, f2;
67

@@ -10,6 +11,7 @@ void foo(void) {
1011
// FINITE: fadd nnan ninf
1112
// NSZ: fadd nsz
1213
// RECIP: fadd arcp
14+
// REASSOC: fadd reassoc
1315
f0 = f1 + f2;
1416

1517
// CHECK: ret

test/Driver/fast-math.c

+50-13
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,23 @@
121121
// RUN: | FileCheck --check-prefix=CHECK-UNSAFE-MATH %s
122122
// CHECK-UNSAFE-MATH: "-cc1"
123123
// CHECK-UNSAFE-MATH: "-menable-unsafe-fp-math"
124+
// CHECK-UNSAFE-MATH: "-mreassociate"
124125
//
125126
// RUN: %clang -### -fno-fast-math -fno-math-errno -fassociative-math -freciprocal-math \
126127
// RUN: -fno-signed-zeros -fno-trapping-math -c %s 2>&1 \
127128
// RUN: | FileCheck --check-prefix=CHECK-NO-FAST-MATH-UNSAFE-MATH %s
128129
// CHECK-NO-FAST-MATH-UNSAFE-MATH: "-cc1"
129130
// CHECK-NO-FAST-MATH-UNSAFE-MATH: "-menable-unsafe-fp-math"
130-
//
131+
// CHECK-NO-FAST-MATH-UNSAFE-MATH: "-mreassociate"
132+
133+
// The 2nd -fno-fast-math overrides -fassociative-math.
134+
131135
// RUN: %clang -### -fno-fast-math -fno-math-errno -fassociative-math -freciprocal-math \
132136
// RUN: -fno-fast-math -fno-signed-zeros -fno-trapping-math -c %s 2>&1 \
133137
// RUN: | FileCheck --check-prefix=CHECK-UNSAFE-MATH-NO-FAST-MATH %s
134138
// CHECK-UNSAFE-MATH-NO-FAST-MATH: "-cc1"
135139
// CHECK-UNSAFE-MATH-NO-FAST-MATH-NOT: "-menable-unsafe-fp-math"
140+
// CHECK-UNSAFE-MATH-NO-FAST-MATH-NOT: "-mreassociate"
136141
//
137142
// Check that various umbrella flags also enable these frontend options.
138143
// RUN: %clang -### -ffast-math -c %s 2>&1 \
@@ -151,7 +156,7 @@
151156
// One umbrella flag is *really* weird and also changes the semantics of the
152157
// program by adding a special preprocessor macro. Check that the frontend flag
153158
// modeling this semantic change is provided. Also check that the flag is not
154-
// present if any of the optimization is disabled.
159+
// present if any of the optimizations are disabled.
155160
// RUN: %clang -### -ffast-math -c %s 2>&1 \
156161
// RUN: | FileCheck --check-prefix=CHECK-FAST-MATH %s
157162
// RUN: %clang -### -fno-fast-math -ffast-math -c %s 2>&1 \
@@ -175,8 +180,11 @@
175180
// RUN: | FileCheck --check-prefix=CHECK-NO-FAST-MATH %s
176181
// RUN: %clang -### -ffast-math -fmath-errno -c %s 2>&1 \
177182
// RUN: | FileCheck --check-prefix=CHECK-NO-FAST-MATH %s
183+
// RUN: %clang -### -ffast-math -fno-associative-math -c %s 2>&1 \
184+
// RUN: | FileCheck --check-prefix=CHECK-NO-FAST-MATH --check-prefix=CHECK-ASSOC-MATH %s
178185
// CHECK-NO-FAST-MATH: "-cc1"
179186
// CHECK-NO-FAST-MATH-NOT: "-ffast-math"
187+
// CHECK-ASSOC-MATH-NOT: "-mreassociate"
180188
//
181189
// Check various means of disabling these flags, including disabling them after
182190
// they've been enabled via an umbrella flag.
@@ -209,21 +217,16 @@
209217
// CHECK-NO-NO-NANS-NOT: "-menable-no-nans"
210218
// CHECK-NO-NO-NANS-NOT: "-ffinite-math-only"
211219
// CHECK-NO-NO-NANS: "-o"
212-
//
220+
221+
// A later inverted option overrides an earlier option.
222+
213223
// RUN: %clang -### -fassociative-math -freciprocal-math -fno-signed-zeros \
214224
// RUN: -fno-trapping-math -fno-associative-math -c %s 2>&1 \
215225
// RUN: | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
216-
// RUN: %clang -### -fassociative-math -freciprocal-math -fno-signed-zeros \
217-
// RUN: -fno-trapping-math -fno-reciprocal-math -c %s 2>&1 \
218-
// RUN: | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
219-
// RUN: %clang -### -fassociative-math -freciprocal-math -fno-signed-zeros \
220-
// RUN: -fno-trapping-math -fsigned-zeros -c %s 2>&1 \
221-
// RUN: | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
222-
// RUN: %clang -### -fassociative-math -freciprocal-math -fno-signed-zeros \
223-
// RUN: -fno-trapping-math -ftrapping-math -c %s 2>&1 \
224-
// RUN: | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
226+
225227
// RUN: %clang -### -funsafe-math-optimizations -fno-associative-math -c %s \
226228
// RUN: 2>&1 | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
229+
227230
// RUN: %clang -### -funsafe-math-optimizations -fno-reciprocal-math -c %s \
228231
// RUN: 2>&1 | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
229232
// RUN: %clang -### -funsafe-math-optimizations -fsigned-zeros -c %s 2>&1 \
@@ -235,6 +238,7 @@
235238
// RUN: | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
236239
// RUN: %clang -### -ffast-math -fno-associative-math -c %s 2>&1 \
237240
// RUN: | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
241+
238242
// RUN: %clang -### -ffast-math -fno-reciprocal-math -c %s 2>&1 \
239243
// RUN: | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
240244
// RUN: %clang -### -ffast-math -fsigned-zeros -c %s 2>&1 \
@@ -243,10 +247,43 @@
243247
// RUN: | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
244248
// RUN: %clang -### -ffast-math -fno-unsafe-math-optimizations -c %s 2>&1 \
245249
// RUN: | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
250+
246251
// CHECK-NO-UNSAFE-MATH: "-cc1"
247252
// CHECK-NO-UNSAFE-MATH-NOT: "-menable-unsafe-fp-math"
253+
// CHECK-NO_UNSAFE-MATH-NOT: "-mreassociate"
248254
// CHECK-NO-UNSAFE-MATH: "-o"
249-
//
255+
256+
257+
// Reassociate is allowed because it does not require reciprocal-math.
258+
259+
// RUN: %clang -### -fassociative-math -freciprocal-math -fno-signed-zeros \
260+
// RUN: -fno-trapping-math -fno-reciprocal-math -c %s 2>&1 \
261+
// RUN: | FileCheck --check-prefix=CHECK-REASSOC-NO-UNSAFE-MATH %s
262+
263+
// CHECK-REASSOC-NO-UNSAFE-MATH: "-cc1"
264+
// CHECK-REASSOC-NO-UNSAFE-MATH-NOT: "-menable-unsafe-fp-math"
265+
// CHECK-REASSOC-NO_UNSAFE-MATH: "-mreassociate"
266+
// CHECK-REASSOC-NO-UNSAFE-MATH-NOT: "-menable-unsafe-fp-math"
267+
// CHECK-REASSOC-NO-UNSAFE-MATH: "-o"
268+
269+
270+
// In these runs, reassociate is not allowed because both no-signed-zeros and no-trapping-math are required.
271+
272+
// RUN: %clang -### -fassociative-math -freciprocal-math -fno-signed-zeros \
273+
// RUN: -fno-trapping-math -fsigned-zeros -c %s 2>&1 \
274+
// RUN: | FileCheck --check-prefix=CHECK-NO-REASSOC-NO-UNSAFE-MATH %s
275+
276+
// RUN: %clang -### -fassociative-math -freciprocal-math -fno-signed-zeros \
277+
// RUN: -fno-trapping-math -ftrapping-math -c %s 2>&1 \
278+
// RUN: | FileCheck --check-prefix=CHECK-NO-REASSOC-NO-UNSAFE-MATH %s
279+
280+
// CHECK-NO-REASSOC-NO-UNSAFE-MATH: "-cc1"
281+
// CHECK-NO-REASSOC-NO-UNSAFE-MATH-NOT: "-menable-unsafe-fp-math"
282+
// CHECK-NO-REASSOC-NO_UNSAFE-MATH-NOT: "-mreassociate"
283+
// CHECK-NO-REASSOC-NO-UNSAFE-MATH-NOT: "-menable-unsafe-fp-math"
284+
// CHECK-NO-REASSOC-NO-UNSAFE-MATH: "-o"
285+
286+
250287
// RUN: %clang -### -ftrapping-math -fno-trapping-math -c %s 2>&1 \
251288
// RUN: | FileCheck --check-prefix=CHECK-NO-TRAPPING-MATH %s
252289
// CHECK-NO-TRAPPING-MATH: "-fno-trapping-math"

0 commit comments

Comments
 (0)