[PATCH] D39812: [Driver, CodeGen] pass through and apply -fassociative-math

Sanjay Patel via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 8 14:29:39 PST 2017


spatel updated this revision to Diff 122155.
spatel added a comment.

Patch updated:
Match gcc's handling of -fassociative-math with -fno-signed-zeros and -fno-trapping-math. I've created a new codegen option (-mreassociate) that maps directly to LLVM's 'reassoc' flag, so we limit the mix-and-match logic of these flags to the driver/frontend.

So now we have:

  $ ./clang  27372.c -S -o -  -fassociative-math -O2 -emit-llvm | egrep 'fadd|fsub'
    %add = fadd float %a, %x
    %sub = fsub float %add, %x
  $ ./clang  27372.c -S -o -  -fassociative-math -fno-signed-zeros -O2 -emit-llvm | egrep 'fadd|fsub'
    %add = fadd nsz float %a, %x
    %sub = fsub nsz float %add, %x
  $ ./clang  27372.c -S -o -  -fassociative-math -fno-signed-zeros -fno-trapping-math -O2 -emit-llvm | egrep 'fadd|fsub'
    %add = fadd reassoc nsz float %a, %x
    %sub = fsub reassoc nsz float %add, %x


https://reviews.llvm.org/D39812

Files:
  include/clang/Driver/CC1Options.td
  include/clang/Frontend/CodeGenOptions.def
  lib/CodeGen/CodeGenFunction.cpp
  lib/Driver/ToolChains/Clang.cpp
  lib/Frontend/CompilerInvocation.cpp
  test/CodeGen/finite-math.c


Index: test/CodeGen/finite-math.c
===================================================================
--- test/CodeGen/finite-math.c
+++ test/CodeGen/finite-math.c
@@ -1,6 +1,7 @@
 // RUN: %clang_cc1 -ffinite-math-only -emit-llvm -o - %s | FileCheck %s -check-prefix=CHECK -check-prefix=FINITE
 // RUN: %clang_cc1 -fno-signed-zeros -emit-llvm -o - %s | FileCheck %s -check-prefix=CHECK  -check-prefix=NSZ
 // RUN: %clang_cc1 -freciprocal-math -emit-llvm -o - %s | FileCheck %s -check-prefix=CHECK  -check-prefix=RECIP
+// RUN: %clang_cc1 -mreassociate -emit-llvm -o - %s | FileCheck %s -check-prefix=CHECK  -check-prefix=REASSOC
 
 float f0, f1, f2;
 
@@ -10,6 +11,7 @@
   // FINITE: fadd nnan ninf
   // NSZ: fadd nsz
   // RECIP: fadd arcp
+  // REASSOC: fadd reassoc
   f0 = f1 + f2;
 
   // CHECK: ret
Index: lib/Frontend/CompilerInvocation.cpp
===================================================================
--- lib/Frontend/CompilerInvocation.cpp
+++ lib/Frontend/CompilerInvocation.cpp
@@ -637,6 +637,7 @@
                         Args.hasArg(OPT_cl_no_signed_zeros) ||
                         Args.hasArg(OPT_cl_unsafe_math_optimizations) ||
                         Args.hasArg(OPT_cl_fast_relaxed_math));
+  Opts.Reassociate = Args.hasArg(OPT_mreassociate);
   Opts.FlushDenorm = Args.hasArg(OPT_cl_denorms_are_zero);
   Opts.CorrectlyRoundedDivSqrt =
       Args.hasArg(OPT_cl_fp32_correctly_rounded_divide_sqrt);
Index: lib/Driver/ToolChains/Clang.cpp
===================================================================
--- lib/Driver/ToolChains/Clang.cpp
+++ lib/Driver/ToolChains/Clang.cpp
@@ -2156,6 +2156,9 @@
   if (!SignedZeros)
     CmdArgs.push_back("-fno-signed-zeros");
 
+  if (AssociativeMath && !SignedZeros && !TrappingMath)
+    CmdArgs.push_back("-mreassociate");
+
   if (ReciprocalMath)
     CmdArgs.push_back("-freciprocal-math");
 
Index: lib/CodeGen/CodeGenFunction.cpp
===================================================================
--- lib/CodeGen/CodeGenFunction.cpp
+++ lib/CodeGen/CodeGenFunction.cpp
@@ -101,6 +101,9 @@
   if (CGM.getCodeGenOpts().ReciprocalMath) {
     FMF.setAllowReciprocal();
   }
+  if (CGM.getCodeGenOpts().Reassociate) {
+    FMF.setAllowReassoc();
+  }
   Builder.setFastMathFlags(FMF);
 }
 
Index: include/clang/Frontend/CodeGenOptions.def
===================================================================
--- include/clang/Frontend/CodeGenOptions.def
+++ include/clang/Frontend/CodeGenOptions.def
@@ -110,6 +110,7 @@
 CODEGENOPT(NoImplicitFloat   , 1, 0) ///< Set when -mno-implicit-float is enabled.
 CODEGENOPT(NoInfsFPMath      , 1, 0) ///< Assume FP arguments, results not +-Inf.
 CODEGENOPT(NoSignedZeros     , 1, 0) ///< Allow ignoring the signedness of FP zero
+CODEGENOPT(Reassociate       , 1, 0) ///< Allow reassociation of FP math ops
 CODEGENOPT(ReciprocalMath    , 1, 0) ///< Allow FP divisions to be reassociated.
 CODEGENOPT(NoTrappingMath    , 1, 0) ///< Set when -fno-trapping-math is enabled.
 CODEGENOPT(NoNaNsFPMath      , 1, 0) ///< Assume FP arguments, results not NaN.
Index: include/clang/Driver/CC1Options.td
===================================================================
--- include/clang/Driver/CC1Options.td
+++ include/clang/Driver/CC1Options.td
@@ -261,6 +261,8 @@
 def menable_unsafe_fp_math : Flag<["-"], "menable-unsafe-fp-math">,
   HelpText<"Allow unsafe floating-point math optimizations which may decrease "
            "precision">;
+def mreassociate : Flag<["-"], "mreassociate">,
+  HelpText<"Allow reassociation transformations for floating-point instructions">;
 def mfloat_abi : Separate<["-"], "mfloat-abi">,
   HelpText<"The float ABI to use">;
 def mtp : Separate<["-"], "mtp">,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D39812.122155.patch
Type: text/x-patch
Size: 3707 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171108/d10d3531/attachment.bin>


More information about the cfe-commits mailing list