Skip to content

Commit c4dc3c0

Browse files
committed
[flang] Add -f[no-]associative-math and -mreassociate
Only add the option processing and store the result. No attributes are added to FIR yet. Clang only forwards -mreassociate if (AssociativeMath && !SignedZeros && !TrappingMath) Flang doesn't have -f[no-]trapping-math, so this part of the condition has been omitted. !TrappingMath is the default. Differential Revision: https://reviews.llvm.org/D137329
1 parent b5b8a8c commit c4dc3c0

File tree

7 files changed

+26
-3
lines changed

7 files changed

+26
-3
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5444,9 +5444,6 @@ def mframe_pointer_EQ : Joined<["-"], "mframe-pointer=">,
54445444
HelpText<"Specify which frame pointers to retain.">, Values<"all,non-leaf,none">,
54455445
NormalizedValuesScope<"CodeGenOptions::FramePointerKind">, NormalizedValues<["All", "NonLeaf", "None"]>,
54465446
MarshallingInfoEnum<CodeGenOpts<"FramePointer">, "None">;
5447-
def mreassociate : Flag<["-"], "mreassociate">,
5448-
HelpText<"Allow reassociation transformations for floating-point instructions">,
5449-
MarshallingInfoFlag<LangOpts<"AllowFPReassoc">>, ImpliedByAnyOf<[funsafe_math_optimizations.KeyPath]>;
54505447
def mabi_EQ_ieeelongdouble : Flag<["-"], "mabi=ieeelongdouble">,
54515448
HelpText<"Use IEEE 754 quadruple-precision for long double">,
54525449
MarshallingInfoFlag<LangOpts<"PPCIEEELongDouble">>;
@@ -6054,6 +6051,9 @@ def split_dwarf_output : Separate<["-"], "split-dwarf-output">,
60546051

60556052
let Flags = [CC1Option, FC1Option, NoDriverOption] in {
60566053

6054+
def mreassociate : Flag<["-"], "mreassociate">,
6055+
HelpText<"Allow reassociation transformations for floating-point instructions">,
6056+
MarshallingInfoFlag<LangOpts<"AllowFPReassoc">>, ImpliedByAnyOf<[funsafe_math_optimizations.KeyPath]>;
60576057
def menable_no_nans : Flag<["-"], "menable-no-nans">,
60586058
HelpText<"Allow optimization to assume there are no NaNs.">,
60596059
MarshallingInfoFlag<LangOpts<"NoHonorNaNs">>, ImpliedByAnyOf<[ffinite_math_only.KeyPath]>;

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ static void addFloatingPointOptions(const Driver &D, const ArgList &Args,
8787
bool HonorNaNs = true;
8888
bool ApproxFunc = false;
8989
bool SignedZeros = true;
90+
bool AssociativeMath = false;
9091

9192
if (const Arg *A = Args.getLastArg(options::OPT_ffp_contract)) {
9293
const StringRef Val = A->getValue();
@@ -136,6 +137,12 @@ static void addFloatingPointOptions(const Driver &D, const ArgList &Args,
136137
case options::OPT_fno_signed_zeros:
137138
SignedZeros = false;
138139
break;
140+
case options::OPT_fassociative_math:
141+
AssociativeMath = true;
142+
break;
143+
case options::OPT_fno_associative_math:
144+
AssociativeMath = false;
145+
break;
139146
}
140147

141148
// If we handled this option claim it
@@ -156,6 +163,9 @@ static void addFloatingPointOptions(const Driver &D, const ArgList &Args,
156163

157164
if (!SignedZeros)
158165
CmdArgs.push_back("-fno-signed-zeros");
166+
167+
if (AssociativeMath && !SignedZeros)
168+
CmdArgs.push_back("-mreassociate");
159169
}
160170

161171
void Flang::ConstructJob(Compilation &C, const JobAction &JA,

flang/include/flang/Frontend/LangOptions.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ LANGOPT(NoHonorNaNs, 1, false)
2929
LANGOPT(ApproxFunc, 1, false)
3030
/// Allow optimizations that ignore the sign of floating point zeros
3131
LANGOPT(NoSignedZeros, 1, false)
32+
/// Allow reassociation transformations for floating-point instructions
33+
LANGOPT(AssociativeMath, 1, false)
3234

3335
#undef LANGOPT
3436
#undef ENUM_LANGOPT

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,12 @@ static bool parseFloatingPointArgs(CompilerInvocation &invoc,
720720
opts.NoSignedZeros = true;
721721
}
722722

723+
if (const llvm::opt::Arg *a =
724+
args.getLastArg(clang::driver::options::OPT_mreassociate)) {
725+
diags.Report(diagUnimplemented) << a->getOption().getName();
726+
opts.AssociativeMath = true;
727+
}
728+
723729
return true;
724730
}
725731

flang/test/Driver/driver-help.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
! HELP-FC1-NEXT: -mmlir <value> Additional arguments to forward to MLIR's option processing
141141
! HELP-FC1-NEXT: -module-dir <dir> Put MODULE files in <dir>
142142
! HELP-FC1-NEXT: -module-suffix <suffix> Use <suffix> as the suffix for module files (the default value is `.mod`)
143+
! HELP-FC1-NEXT: -mreassociate Allow reassociation transformations for floating-point instructions
143144
! HELP-FC1-NEXT: -mrelocation-model <value>
144145
! HELP-FC1-NEXT: The relocation model to use
145146
! HELP-FC1-NEXT: -nocpp Disable predefined and command line preprocessor macros

flang/test/Driver/flang_fp_opts.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
! RUN: -menable-no-nans \
77
! RUN: -fapprox-func \
88
! RUN: -fno-signed-zeros \
9+
! RUN: -mreassociate \
910
! RUN: %s 2>&1 | FileCheck %s
1011
! CHECK: ffp-contract= is not currently implemented
1112
! CHECK: menable-no-infs is not currently implemented
1213
! CHECK: menable-no-nans is not currently implemented
1314
! CHECK: fapprox-func is not currently implemented
1415
! CHECK: fno-signed-zeros is not currently implemented
16+
! CHECK: mreassociate is not currently implemented

flang/test/Driver/frontend-forwarding.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
! RUN: -fno-honor-nans \
1414
! RUN: -fapprox-func \
1515
! RUN: -fno-signed-zeros \
16+
! RUN: -fassociative-math \
1617
! RUN: -mllvm -print-before-all\
1718
! RUN: -P \
1819
! RUN: | FileCheck %s
@@ -28,5 +29,6 @@
2829
! CHECK: "-menable-no-nans"
2930
! CHECK: "-fapprox-func"
3031
! CHECK: "-fno-signed-zeros"
32+
! CHECK: "-mreassociate"
3133
! CHECK: "-fconvert=little-endian"
3234
! CHECK: "-mllvm" "-print-before-all"

0 commit comments

Comments
 (0)