[flang] Add -f[no-]associative-math and -mreassociate
authorTom Eccles <tom.eccles@arm.com>
Wed, 19 Oct 2022 10:33:12 +0000 (10:33 +0000)
committerTom Eccles <tom.eccles@arm.com>
Fri, 4 Nov 2022 17:22:35 +0000 (17:22 +0000)
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

clang/include/clang/Driver/Options.td
clang/lib/Driver/ToolChains/Flang.cpp
flang/include/flang/Frontend/LangOptions.def
flang/lib/Frontend/CompilerInvocation.cpp
flang/test/Driver/driver-help.f90
flang/test/Driver/flang_fp_opts.f90
flang/test/Driver/frontend-forwarding.f90

index 9950091..77c8439 100644 (file)
@@ -5444,9 +5444,6 @@ def mframe_pointer_EQ : Joined<["-"], "mframe-pointer=">,
   HelpText<"Specify which frame pointers to retain.">, Values<"all,non-leaf,none">,
   NormalizedValuesScope<"CodeGenOptions::FramePointerKind">, NormalizedValues<["All", "NonLeaf", "None"]>,
   MarshallingInfoEnum<CodeGenOpts<"FramePointer">, "None">;
-def mreassociate : Flag<["-"], "mreassociate">,
-  HelpText<"Allow reassociation transformations for floating-point instructions">,
-  MarshallingInfoFlag<LangOpts<"AllowFPReassoc">>, ImpliedByAnyOf<[funsafe_math_optimizations.KeyPath]>;
 def mabi_EQ_ieeelongdouble : Flag<["-"], "mabi=ieeelongdouble">,
   HelpText<"Use IEEE 754 quadruple-precision for long double">,
   MarshallingInfoFlag<LangOpts<"PPCIEEELongDouble">>;
@@ -6054,6 +6051,9 @@ def split_dwarf_output : Separate<["-"], "split-dwarf-output">,
 
 let Flags = [CC1Option, FC1Option, NoDriverOption] in {
 
+def mreassociate : Flag<["-"], "mreassociate">,
+  HelpText<"Allow reassociation transformations for floating-point instructions">,
+  MarshallingInfoFlag<LangOpts<"AllowFPReassoc">>, ImpliedByAnyOf<[funsafe_math_optimizations.KeyPath]>;
 def menable_no_nans : Flag<["-"], "menable-no-nans">,
   HelpText<"Allow optimization to assume there are no NaNs.">,
   MarshallingInfoFlag<LangOpts<"NoHonorNaNs">>, ImpliedByAnyOf<[ffinite_math_only.KeyPath]>;
index a5b4712..f519727 100644 (file)
@@ -87,6 +87,7 @@ static void addFloatingPointOptions(const Driver &D, const ArgList &Args,
   bool HonorNaNs = true;
   bool ApproxFunc = false;
   bool SignedZeros = true;
+  bool AssociativeMath = false;
 
   if (const Arg *A = Args.getLastArg(options::OPT_ffp_contract)) {
     const StringRef Val = A->getValue();
@@ -136,6 +137,12 @@ static void addFloatingPointOptions(const Driver &D, const ArgList &Args,
     case options::OPT_fno_signed_zeros:
       SignedZeros = false;
       break;
+    case options::OPT_fassociative_math:
+      AssociativeMath = true;
+      break;
+    case options::OPT_fno_associative_math:
+      AssociativeMath = false;
+      break;
     }
 
     // If we handled this option claim it
@@ -156,6 +163,9 @@ static void addFloatingPointOptions(const Driver &D, const ArgList &Args,
 
   if (!SignedZeros)
     CmdArgs.push_back("-fno-signed-zeros");
+
+  if (AssociativeMath && !SignedZeros)
+    CmdArgs.push_back("-mreassociate");
 }
 
 void Flang::ConstructJob(Compilation &C, const JobAction &JA,
index e357182..059e3d1 100644 (file)
@@ -29,6 +29,8 @@ LANGOPT(NoHonorNaNs, 1, false)
 LANGOPT(ApproxFunc, 1, false)
 /// Allow optimizations that ignore the sign of floating point zeros
 LANGOPT(NoSignedZeros, 1, false)
+/// Allow reassociation transformations for floating-point instructions
+LANGOPT(AssociativeMath, 1, false)
 
 #undef LANGOPT
 #undef ENUM_LANGOPT
index eeadb21..8c0bdcd 100644 (file)
@@ -720,6 +720,12 @@ static bool parseFloatingPointArgs(CompilerInvocation &invoc,
     opts.NoSignedZeros = true;
   }
 
+  if (const llvm::opt::Arg *a =
+          args.getLastArg(clang::driver::options::OPT_mreassociate)) {
+    diags.Report(diagUnimplemented) << a->getOption().getName();
+    opts.AssociativeMath = true;
+  }
+
   return true;
 }
 
index 1c48ec5..32b6f76 100644 (file)
 ! HELP-FC1-NEXT: -mmlir <value>         Additional arguments to forward to MLIR's option processing
 ! HELP-FC1-NEXT: -module-dir <dir>      Put MODULE files in <dir>
 ! HELP-FC1-NEXT: -module-suffix <suffix> Use <suffix> as the suffix for module files (the default value is `.mod`)
+! HELP-FC1-NEXT: -mreassociate          Allow reassociation transformations for floating-point instructions
 ! HELP-FC1-NEXT: -mrelocation-model <value>
 ! HELP-FC1-NEXT:                        The relocation model to use
 ! HELP-FC1-NEXT: -nocpp                 Disable predefined and command line preprocessor macros
index a305cdd..bbe886b 100644 (file)
@@ -6,9 +6,11 @@
 ! RUN:      -menable-no-nans \
 ! RUN:      -fapprox-func \
 ! RUN:      -fno-signed-zeros \
+! RUN:      -mreassociate \
 ! RUN:      %s 2>&1 | FileCheck %s
 ! CHECK: ffp-contract= is not currently implemented
 ! CHECK: menable-no-infs is not currently implemented
 ! CHECK: menable-no-nans is not currently implemented
 ! CHECK: fapprox-func is not currently implemented
 ! CHECK: fno-signed-zeros is not currently implemented
+! CHECK: mreassociate is not currently implemented
index a00c45a..de3ed6d 100644 (file)
@@ -13,6 +13,7 @@
 ! RUN:     -fno-honor-nans \
 ! RUN:     -fapprox-func \
 ! RUN:     -fno-signed-zeros \
+! RUN:     -fassociative-math \
 ! RUN:     -mllvm -print-before-all\
 ! RUN:     -P \
 ! RUN:   | FileCheck %s
@@ -28,5 +29,6 @@
 ! CHECK: "-menable-no-nans"
 ! CHECK: "-fapprox-func"
 ! CHECK: "-fno-signed-zeros"
+! CHECK: "-mreassociate"
 ! CHECK: "-fconvert=little-endian"
 ! CHECK:  "-mllvm" "-print-before-all"