[PATCH] D106191: [clang] Option control afn flag

2021-10-08 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman accepted this revision.
aaron.ballman added a comment.
This revision is now accepted and ready to land.

LGTM




Comment at: clang/docs/UsersManual.rst:1395
+   to be replaced with an approximately equivalent set of instructions
+   or alternative math function calls. For example, a ``pow(x, 0.25)``
+   may be replaced with ``sqrt(sqrt(x))``, despite being an inexact result

erichkeane wrote:
> Should this be 'alternate math function calls'?  I'm second guessing myself 
> now.
`alternative` is correct. `alternate` as an adjective means "every other, every 
second".


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D106191: [clang] Option control afn flag

2021-10-08 Thread Erich Keane via Phabricator via cfe-commits
erichkeane added a comment.

1 small 'english' question, hopefully Aaron can answer, otherwise LGTM.




Comment at: clang/docs/UsersManual.rst:1395
+   to be replaced with an approximately equivalent set of instructions
+   or alternative math function calls. For example, a ``pow(x, 0.25)``
+   may be replaced with ``sqrt(sqrt(x))``, despite being an inexact result

Should this be 'alternate math function calls'?  I'm second guessing myself now.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D106191: [clang] Option control afn flag

2021-10-08 Thread Masoud Ataei via Phabricator via cfe-commits
masoud.ataei updated this revision to Diff 378277.
masoud.ataei added a comment.

Update the documentation.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

Files:
  clang/docs/UsersManual.rst
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/BackendUtil.cpp
  clang/lib/CodeGen/CGCall.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/test/CodeGen/afn-flag-test.c
  clang/test/Driver/fast-math.c
  llvm/include/llvm/Target/TargetOptions.h

Index: llvm/include/llvm/Target/TargetOptions.h
===
--- llvm/include/llvm/Target/TargetOptions.h
+++ llvm/include/llvm/Target/TargetOptions.h
@@ -126,7 +126,7 @@
 TargetOptions()
 : UnsafeFPMath(false), NoInfsFPMath(false), NoNaNsFPMath(false),
   NoTrappingFPMath(true), NoSignedZerosFPMath(false),
-  EnableAIXExtendedAltivecABI(false),
+  ApproxFuncFPMath(false), EnableAIXExtendedAltivecABI(false),
   HonorSignDependentRoundingFPMathOption(false), NoZerosInBSS(false),
   GuaranteedTailCallOpt(false), StackSymbolOrdering(true),
   EnableFastISel(false), EnableGlobalISel(false), UseInitArray(false),
@@ -183,6 +183,12 @@
 /// argument or result as insignificant.
 unsigned NoSignedZerosFPMath : 1;
 
+/// ApproxFuncFPMath - This flag is enabled when the
+/// -enable-approx-func-fp-math is specified on the command line. This
+/// specifies that optimizations are allowed to substitute math functions
+/// with approximate calculations
+unsigned ApproxFuncFPMath : 1;
+
 /// EnableAIXExtendedAltivecABI - This flag returns true when -vec-extabi is
 /// specified. The code generator is then able to use both volatile and
 /// nonvolitle vector registers. When false, the code generator only uses
Index: clang/test/Driver/fast-math.c
===
--- clang/test/Driver/fast-math.c
+++ clang/test/Driver/fast-math.c
@@ -70,6 +70,11 @@
 // CHECK-NO-NANS-NO-FAST-MATH: "-cc1"
 // CHECK-NO-NANS-NO-FAST-MATH-NOT: "-menable-no-nans"
 //
+// RUN: %clang -### -fapprox-func -c %s 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-APPROX-FUNC %s
+// CHECK-APPROX-FUNC: "-cc1"
+// CHECK-APPROX-FUNC: "-fapprox-func"
+//
 // RUN: %clang -### -fmath-errno -c %s 2>&1 \
 // RUN:   | FileCheck --check-prefix=CHECK-MATH-ERRNO %s
 // CHECK-MATH-ERRNO: "-cc1"
Index: clang/test/CodeGen/afn-flag-test.c
===
--- /dev/null
+++ clang/test/CodeGen/afn-flag-test.c
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -fapprox-func  %s -emit-llvm -o - | FileCheck --check-prefix=CHECK-AFN %s
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck --check-prefix=CHECK-NO-AFN %s
+
+extern double exp(double);
+double afn_option_test(double x) {
+  return exp(x);
+  // CHECK-LABEL:  define{{.*}} double @afn_option_test(double %x) #0 {
+
+  // CHECK-AFN:  %{{.*}} = call afn double @{{.*}}exp{{.*}}(double %{{.*}})
+  // CHECK-AFN:  attributes #0 ={{.*}} "approx-func-fp-math"="true" {{.*}}
+
+  // CHECK-NO-AFN:   %{{.*}} = call double @{{.*}}exp{{.*}}(double %{{.*}})
+  // CHECK-NO-AFN-NOT:  attributes #0 ={{.*}} "approx-func-fp-math"="true" {{.*}}
+}
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -2618,6 +2618,7 @@
   // LLVM flags based on the final state.
   bool HonorINFs = true;
   bool HonorNaNs = true;
+  bool ApproxFunc = false;
   // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
   bool MathErrno = TC.IsMathErrnoDefault();
   bool AssociativeMath = false;
@@ -2722,6 +2723,8 @@
 case options::OPT_fno_honor_infinities: HonorINFs = false;break;
 case options::OPT_fhonor_nans:  HonorNaNs = true; break;
 case options::OPT_fno_honor_nans:   HonorNaNs = false;break;
+case options::OPT_fapprox_func: ApproxFunc = true;break;
+case options::OPT_fno_approx_func:  ApproxFunc = false;   break;
 case options::OPT_fmath_errno:  MathErrno = true; break;
 case options::OPT_fno_math_errno:   MathErrno = false;break;
 case options::OPT_fassociative_math:AssociativeMath = true;   break;
@@ -2917,6 +2920,9 @@
   if (!HonorNaNs)
 CmdArgs.push_back("-menable-no-nans");
 
+  if (ApproxFunc)
+CmdArgs.push_back("-fapprox-func");
+
   if (MathErrno)
 CmdArgs.push_back("-fmath-errno");
 
Index: clang/lib/CodeGen/CGCall.cpp
===
--- clang/lib/CodeGen/CGCall.cpp
+++ clang/lib/CodeGen/CGCall.cpp
@@ -1828,6 +1828,8 @@
   FuncAttrs.addAttribute("no-infs-fp-math", "true");
 if (LangOpts.NoHonorNaNs)
   

[PATCH] D106191: [clang] Option control afn flag

2021-10-08 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added inline comments.



Comment at: clang/docs/UsersManual.rst:1393
+
+   Allow substitution of approximate calculations for functions.
+   Defaults to ``-fno-approx-func``.

erichkeane wrote:
> masoud.ataei wrote:
> > erichkeane wrote:
> > > masoud.ataei wrote:
> > > > erichkeane wrote:
> > > > > This seems pretty incomplete to me, I have no idea what it does from 
> > > > > this description.  Can you elaborate?
> > > > If I didn't miss to say it is about math function calls, then I guess 
> > > > it would be more clear. 
> > > > ```
> > > > Allow substitution of approximate calculations for math function calls.
> > > > ```
> > > > This option just adds `afn` fast-math flag to the function calls and in 
> > > > backend the math function call can be replaced by an approximate 
> > > > calculation (either another math function call or not). 
> > > > https://llvm.org/docs/LangRef.html#fast-math-flags
> > > allow substitution of approximate calculations for math function with 
> > > what?  I'm not clear as to what this does still.  I'm not sure that I get 
> > > what the 'afn' documentation means there either.  Typically we'd want the 
> > > clang frontend flags to be more accurate/descriptive than the LLVM-IR 
> > > documentation.
> > This option adds `afn` fast-math flag to function calls in IR. And in the 
> > case of math function calls (like `log`, `sqrt`, `pow`, ...), `afn` flag 
> > allows LLVM to substitute the math calls with an "approximate calculation". 
> > What an approximate calculation is may differ based on which math call it 
> > is, what other fast-math flags are set, what is the target machine, and 
> > other factors.
> > 
> > An approximate calculation can be
> >  1. sequence of instructions (inlining): for example, inlining sqrt call 
> > needs `afn` flag to be present in the call.
> >  2. a substituted math function call (which may be less accurate but 
> > faster): 
> >  this substitution can be 
> >  (a) for general targets: for example: `pow(x,0.25)` to 
> > `sqrt(sqrt(x))`. or
> >  (b) for a specific target: for example, in the case of PowerPC, we are 
> > proposing to substitute math calls to MASS library calls 
> > https://reviews.llvm.org/D101759
> > 
> > Note that, `afn` flag is necessary for these approximate calculation but 
> > may not be sufficient. Most of them need at lease one or two other 
> > fast-math flags to be set too. 
> > 
> > I agree the term "approximate calculation" is a very general term. But I 
> > guess it is needed to describe this general situation. 
> Perhaps then something like:
> `Allow certain math function calls (such as log, sqrt, pow, etc) to be 
> replaced with an approximately equivalent set of instructions or alternative 
> math function calls.  For example, a pow(x, 0.25) may be replaced with 
> sqrt(sqrt(x), despite being an inexact result in cases where  little>.`
> 
> 
> For Options.td we can be a little more terse, so perhaps something a little 
> more like:
> 
> `Allow certain math function calls to be replaced with an approximately 
> equivalent calculation.`
> 
> @aaron.ballman  WDYT?
> 
I think the docs suggestion makes sense (though please be sure to add backticks 
to all the syntax components, correct the missing paren on `sqrt(sqrt(x))`, and 
finish the  bit.

I think the suggested wording for Options.td looks good. Thanks!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D106191: [clang] Option control afn flag

2021-10-08 Thread Erich Keane via Phabricator via cfe-commits
erichkeane added inline comments.



Comment at: clang/docs/UsersManual.rst:1393
+
+   Allow substitution of approximate calculations for functions.
+   Defaults to ``-fno-approx-func``.

masoud.ataei wrote:
> erichkeane wrote:
> > masoud.ataei wrote:
> > > erichkeane wrote:
> > > > This seems pretty incomplete to me, I have no idea what it does from 
> > > > this description.  Can you elaborate?
> > > If I didn't miss to say it is about math function calls, then I guess it 
> > > would be more clear. 
> > > ```
> > > Allow substitution of approximate calculations for math function calls.
> > > ```
> > > This option just adds `afn` fast-math flag to the function calls and in 
> > > backend the math function call can be replaced by an approximate 
> > > calculation (either another math function call or not). 
> > > https://llvm.org/docs/LangRef.html#fast-math-flags
> > allow substitution of approximate calculations for math function with what? 
> >  I'm not clear as to what this does still.  I'm not sure that I get what 
> > the 'afn' documentation means there either.  Typically we'd want the clang 
> > frontend flags to be more accurate/descriptive than the LLVM-IR 
> > documentation.
> This option adds `afn` fast-math flag to function calls in IR. And in the 
> case of math function calls (like `log`, `sqrt`, `pow`, ...), `afn` flag 
> allows LLVM to substitute the math calls with an "approximate calculation". 
> What an approximate calculation is may differ based on which math call it is, 
> what other fast-math flags are set, what is the target machine, and other 
> factors.
> 
> An approximate calculation can be
>  1. sequence of instructions (inlining): for example, inlining sqrt call 
> needs `afn` flag to be present in the call.
>  2. a substituted math function call (which may be less accurate but faster): 
>  this substitution can be 
>  (a) for general targets: for example: `pow(x,0.25)` to `sqrt(sqrt(x))`. 
> or
>  (b) for a specific target: for example, in the case of PowerPC, we are 
> proposing to substitute math calls to MASS library calls 
> https://reviews.llvm.org/D101759
> 
> Note that, `afn` flag is necessary for these approximate calculation but may 
> not be sufficient. Most of them need at lease one or two other fast-math 
> flags to be set too. 
> 
> I agree the term "approximate calculation" is a very general term. But I 
> guess it is needed to describe this general situation. 
Perhaps then something like:
`Allow certain math function calls (such as log, sqrt, pow, etc) to be replaced 
with an approximately equivalent set of instructions or alternative math 
function calls.  For example, a pow(x, 0.25) may be replaced with sqrt(sqrt(x), 
despite being an inexact result in cases where .`


For Options.td we can be a little more terse, so perhaps something a little 
more like:

`Allow certain math function calls to be replaced with an approximately 
equivalent calculation.`

@aaron.ballman  WDYT?



Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D106191: [clang] Option control afn flag

2021-10-08 Thread Masoud Ataei via Phabricator via cfe-commits
masoud.ataei marked an inline comment as not done.
masoud.ataei added inline comments.



Comment at: clang/docs/UsersManual.rst:1393
+
+   Allow substitution of approximate calculations for functions.
+   Defaults to ``-fno-approx-func``.

erichkeane wrote:
> masoud.ataei wrote:
> > erichkeane wrote:
> > > This seems pretty incomplete to me, I have no idea what it does from this 
> > > description.  Can you elaborate?
> > If I didn't miss to say it is about math function calls, then I guess it 
> > would be more clear. 
> > ```
> > Allow substitution of approximate calculations for math function calls.
> > ```
> > This option just adds `afn` fast-math flag to the function calls and in 
> > backend the math function call can be replaced by an approximate 
> > calculation (either another math function call or not). 
> > https://llvm.org/docs/LangRef.html#fast-math-flags
> allow substitution of approximate calculations for math function with what?  
> I'm not clear as to what this does still.  I'm not sure that I get what the 
> 'afn' documentation means there either.  Typically we'd want the clang 
> frontend flags to be more accurate/descriptive than the LLVM-IR documentation.
This option adds `afn` fast-math flag to function calls in IR. And in the case 
of math function calls (like `log`, `sqrt`, `pow`, ...), `afn` flag allows LLVM 
to substitute the math calls with an "approximate calculation". What an 
approximate calculation is may differ based on which math call it is, what 
other fast-math flags are set, what is the target machine, and other factors.

An approximate calculation can be
 1. sequence of instructions (inlining): for example, inlining sqrt call needs 
`afn` flag to be present in the call.
 2. a substituted math function call (which may be less accurate but faster): 
 this substitution can be 
 (a) for general targets: for example: `pow(x,0.25)` to `sqrt(sqrt(x))`. or
 (b) for a specific target: for example, in the case of PowerPC, we are 
proposing to substitute math calls to MASS library calls 
https://reviews.llvm.org/D101759

Note that, `afn` flag is necessary for these approximate calculation but may 
not be sufficient. Most of them need at lease one or two other fast-math flags 
to be set too. 

I agree the term "approximate calculation" is a very general term. But I guess 
it is needed to describe this general situation. 


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D106191: [clang] Option control afn flag

2021-10-07 Thread Erich Keane via Phabricator via cfe-commits
erichkeane added inline comments.



Comment at: clang/docs/UsersManual.rst:1393
+
+   Allow substitution of approximate calculations for functions.
+   Defaults to ``-fno-approx-func``.

masoud.ataei wrote:
> erichkeane wrote:
> > This seems pretty incomplete to me, I have no idea what it does from this 
> > description.  Can you elaborate?
> If I didn't miss to say it is about math function calls, then I guess it 
> would be more clear. 
> ```
> Allow substitution of approximate calculations for math function calls.
> ```
> This option just adds `afn` fast-math flag to the function calls and in 
> backend the math function call can be replaced by an approximate calculation 
> (either another math function call or not). 
> https://llvm.org/docs/LangRef.html#fast-math-flags
allow substitution of approximate calculations for math function with what?  
I'm not clear as to what this does still.  I'm not sure that I get what the 
'afn' documentation means there either.  Typically we'd want the clang frontend 
flags to be more accurate/descriptive than the LLVM-IR documentation.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D106191: [clang] Option control afn flag

2021-10-07 Thread Masoud Ataei via Phabricator via cfe-commits
masoud.ataei updated this revision to Diff 378005.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

Files:
  clang/docs/UsersManual.rst
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/BackendUtil.cpp
  clang/lib/CodeGen/CGCall.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/test/CodeGen/afn-flag-test.c
  clang/test/Driver/fast-math.c
  llvm/include/llvm/Target/TargetOptions.h

Index: llvm/include/llvm/Target/TargetOptions.h
===
--- llvm/include/llvm/Target/TargetOptions.h
+++ llvm/include/llvm/Target/TargetOptions.h
@@ -126,7 +126,7 @@
 TargetOptions()
 : UnsafeFPMath(false), NoInfsFPMath(false), NoNaNsFPMath(false),
   NoTrappingFPMath(true), NoSignedZerosFPMath(false),
-  EnableAIXExtendedAltivecABI(false),
+  ApproxFuncFPMath(false), EnableAIXExtendedAltivecABI(false),
   HonorSignDependentRoundingFPMathOption(false), NoZerosInBSS(false),
   GuaranteedTailCallOpt(false), StackSymbolOrdering(true),
   EnableFastISel(false), EnableGlobalISel(false), UseInitArray(false),
@@ -183,6 +183,12 @@
 /// argument or result as insignificant.
 unsigned NoSignedZerosFPMath : 1;
 
+/// ApproxFuncFPMath - This flag is enabled when the
+/// -enable-approx-func-fp-math is specified on the command line. This
+/// specifies that optimizations are allowed to substitute math functions
+/// with approximate calculations
+unsigned ApproxFuncFPMath : 1;
+
 /// EnableAIXExtendedAltivecABI - This flag returns true when -vec-extabi is
 /// specified. The code generator is then able to use both volatile and
 /// nonvolitle vector registers. When false, the code generator only uses
Index: clang/test/Driver/fast-math.c
===
--- clang/test/Driver/fast-math.c
+++ clang/test/Driver/fast-math.c
@@ -70,6 +70,11 @@
 // CHECK-NO-NANS-NO-FAST-MATH: "-cc1"
 // CHECK-NO-NANS-NO-FAST-MATH-NOT: "-menable-no-nans"
 //
+// RUN: %clang -### -fapprox-func -c %s 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-APPROX-FUNC %s
+// CHECK-APPROX-FUNC: "-cc1"
+// CHECK-APPROX-FUNC: "-fapprox-func"
+//
 // RUN: %clang -### -fmath-errno -c %s 2>&1 \
 // RUN:   | FileCheck --check-prefix=CHECK-MATH-ERRNO %s
 // CHECK-MATH-ERRNO: "-cc1"
Index: clang/test/CodeGen/afn-flag-test.c
===
--- /dev/null
+++ clang/test/CodeGen/afn-flag-test.c
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -fapprox-func  %s -emit-llvm -o - | FileCheck --check-prefix=CHECK-AFN %s
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck --check-prefix=CHECK-NO-AFN %s
+
+extern double exp(double);
+double afn_option_test(double x) {
+  return exp(x);
+  // CHECK-LABEL:  define{{.*}} double @afn_option_test(double %x) #0 {
+
+  // CHECK-AFN:  %{{.*}} = call afn double @{{.*}}exp{{.*}}(double %{{.*}})
+  // CHECK-AFN:  attributes #0 ={{.*}} "approx-func-fp-math"="true" {{.*}}
+
+  // CHECK-NO-AFN:   %{{.*}} = call double @{{.*}}exp{{.*}}(double %{{.*}})
+  // CHECK-NO-AFN-NOT:  attributes #0 ={{.*}} "approx-func-fp-math"="true" {{.*}}
+}
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -2618,6 +2618,7 @@
   // LLVM flags based on the final state.
   bool HonorINFs = true;
   bool HonorNaNs = true;
+  bool ApproxFunc = false;
   // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
   bool MathErrno = TC.IsMathErrnoDefault();
   bool AssociativeMath = false;
@@ -2722,6 +2723,8 @@
 case options::OPT_fno_honor_infinities: HonorINFs = false;break;
 case options::OPT_fhonor_nans:  HonorNaNs = true; break;
 case options::OPT_fno_honor_nans:   HonorNaNs = false;break;
+case options::OPT_fapprox_func: ApproxFunc = true;break;
+case options::OPT_fno_approx_func:  ApproxFunc = false;   break;
 case options::OPT_fmath_errno:  MathErrno = true; break;
 case options::OPT_fno_math_errno:   MathErrno = false;break;
 case options::OPT_fassociative_math:AssociativeMath = true;   break;
@@ -2917,6 +2920,9 @@
   if (!HonorNaNs)
 CmdArgs.push_back("-menable-no-nans");
 
+  if (ApproxFunc)
+CmdArgs.push_back("-fapprox-func");
+
   if (MathErrno)
 CmdArgs.push_back("-fmath-errno");
 
Index: clang/lib/CodeGen/CGCall.cpp
===
--- clang/lib/CodeGen/CGCall.cpp
+++ clang/lib/CodeGen/CGCall.cpp
@@ -1828,6 +1828,8 @@
   FuncAttrs.addAttribute("no-infs-fp-math", "true");
 if (LangOpts.NoHonorNaNs)
   FuncAttrs.addAttribute("no-nans-fp-math", "true");
+if 

[PATCH] D106191: [clang] Option control afn flag

2021-10-07 Thread Masoud Ataei via Phabricator via cfe-commits
masoud.ataei added inline comments.



Comment at: clang/docs/UsersManual.rst:1393
+
+   Allow substitution of approximate calculations for functions.
+   Defaults to ``-fno-approx-func``.

erichkeane wrote:
> This seems pretty incomplete to me, I have no idea what it does from this 
> description.  Can you elaborate?
If I didn't miss to say it is about math function calls, then I guess it would 
be more clear. 
```
Allow substitution of approximate calculations for math function calls.
```
This option just adds `afn` fast-math flag to the function calls and in backend 
the math function call can be replaced by an approximate calculation (either 
another math function call or not). 
https://llvm.org/docs/LangRef.html#fast-math-flags


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D106191: [clang] Option control afn flag

2021-10-07 Thread Erich Keane via Phabricator via cfe-commits
erichkeane added inline comments.



Comment at: clang/docs/UsersManual.rst:1393
+
+   Allow substitution of approximate calculations for functions.
+   Defaults to ``-fno-approx-func``.

This seems pretty incomplete to me, I have no idea what it does from this 
description.  Can you elaborate?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D106191: [clang] Option control afn flag

2021-10-07 Thread Masoud Ataei via Phabricator via cfe-commits
masoud.ataei updated this revision to Diff 377909.
masoud.ataei added a comment.

Update the documentation.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

Files:
  clang/docs/UsersManual.rst
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/BackendUtil.cpp
  clang/lib/CodeGen/CGCall.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/test/CodeGen/afn-flag-test.c
  clang/test/Driver/fast-math.c
  llvm/include/llvm/Target/TargetOptions.h

Index: llvm/include/llvm/Target/TargetOptions.h
===
--- llvm/include/llvm/Target/TargetOptions.h
+++ llvm/include/llvm/Target/TargetOptions.h
@@ -126,7 +126,7 @@
 TargetOptions()
 : UnsafeFPMath(false), NoInfsFPMath(false), NoNaNsFPMath(false),
   NoTrappingFPMath(true), NoSignedZerosFPMath(false),
-  EnableAIXExtendedAltivecABI(false),
+  ApproxFuncFPMath(false), EnableAIXExtendedAltivecABI(false),
   HonorSignDependentRoundingFPMathOption(false), NoZerosInBSS(false),
   GuaranteedTailCallOpt(false), StackSymbolOrdering(true),
   EnableFastISel(false), EnableGlobalISel(false), UseInitArray(false),
@@ -183,6 +183,12 @@
 /// argument or result as insignificant.
 unsigned NoSignedZerosFPMath : 1;
 
+/// ApproxFuncFPMath - This flag is enabled when the
+/// -enable-approx-func-fp-math is specified on the command line. This
+/// specifies that optimizations are allowed to substitute math functions
+/// with approximate calculations
+unsigned ApproxFuncFPMath : 1;
+
 /// EnableAIXExtendedAltivecABI - This flag returns true when -vec-extabi is
 /// specified. The code generator is then able to use both volatile and
 /// nonvolitle vector registers. When false, the code generator only uses
Index: clang/test/Driver/fast-math.c
===
--- clang/test/Driver/fast-math.c
+++ clang/test/Driver/fast-math.c
@@ -70,6 +70,11 @@
 // CHECK-NO-NANS-NO-FAST-MATH: "-cc1"
 // CHECK-NO-NANS-NO-FAST-MATH-NOT: "-menable-no-nans"
 //
+// RUN: %clang -### -fapprox-func -c %s 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-APPROX-FUNC %s
+// CHECK-APPROX-FUNC: "-cc1"
+// CHECK-APPROX-FUNC: "-fapprox-func"
+//
 // RUN: %clang -### -fmath-errno -c %s 2>&1 \
 // RUN:   | FileCheck --check-prefix=CHECK-MATH-ERRNO %s
 // CHECK-MATH-ERRNO: "-cc1"
Index: clang/test/CodeGen/afn-flag-test.c
===
--- /dev/null
+++ clang/test/CodeGen/afn-flag-test.c
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -fapprox-func  %s -emit-llvm -o - | FileCheck --check-prefix=CHECK-AFN %s
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck --check-prefix=CHECK-NO-AFN %s
+
+extern double exp(double);
+double afn_option_test(double x) {
+  return exp(x);
+  // CHECK-LABEL:  define{{.*}} double @afn_option_test(double %x) #0 {
+
+  // CHECK-AFN:  %{{.*}} = call afn double @{{.*}}exp{{.*}}(double %{{.*}})
+  // CHECK-AFN:  attributes #0 ={{.*}} "approx-func-fp-math"="true" {{.*}}
+
+  // CHECK-NO-AFN:   %{{.*}} = call double @{{.*}}exp{{.*}}(double %{{.*}})
+  // CHECK-NO-AFN-NOT:  attributes #0 ={{.*}} "approx-func-fp-math"="true" {{.*}}
+}
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -2618,6 +2618,7 @@
   // LLVM flags based on the final state.
   bool HonorINFs = true;
   bool HonorNaNs = true;
+  bool ApproxFunc = false;
   // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
   bool MathErrno = TC.IsMathErrnoDefault();
   bool AssociativeMath = false;
@@ -2722,6 +2723,8 @@
 case options::OPT_fno_honor_infinities: HonorINFs = false;break;
 case options::OPT_fhonor_nans:  HonorNaNs = true; break;
 case options::OPT_fno_honor_nans:   HonorNaNs = false;break;
+case options::OPT_fapprox_func: ApproxFunc = true;break;
+case options::OPT_fno_approx_func:  ApproxFunc = false;   break;
 case options::OPT_fmath_errno:  MathErrno = true; break;
 case options::OPT_fno_math_errno:   MathErrno = false;break;
 case options::OPT_fassociative_math:AssociativeMath = true;   break;
@@ -2917,6 +2920,9 @@
   if (!HonorNaNs)
 CmdArgs.push_back("-menable-no-nans");
 
+  if (ApproxFunc)
+CmdArgs.push_back("-fapprox-func");
+
   if (MathErrno)
 CmdArgs.push_back("-fmath-errno");
 
Index: clang/lib/CodeGen/CGCall.cpp
===
--- clang/lib/CodeGen/CGCall.cpp
+++ clang/lib/CodeGen/CGCall.cpp
@@ -1828,6 +1828,8 @@
   FuncAttrs.addAttribute("no-infs-fp-math", "true");
 if (LangOpts.NoHonorNaNs)
   

[PATCH] D106191: [clang] Option control afn flag

2021-10-07 Thread Masoud Ataei via Phabricator via cfe-commits
masoud.ataei updated this revision to Diff 377904.
masoud.ataei added a comment.

Description and driver test are added.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/BackendUtil.cpp
  clang/lib/CodeGen/CGCall.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/test/CodeGen/afn-flag-test.c
  clang/test/Driver/fast-math.c
  llvm/include/llvm/Target/TargetOptions.h

Index: llvm/include/llvm/Target/TargetOptions.h
===
--- llvm/include/llvm/Target/TargetOptions.h
+++ llvm/include/llvm/Target/TargetOptions.h
@@ -126,7 +126,7 @@
 TargetOptions()
 : UnsafeFPMath(false), NoInfsFPMath(false), NoNaNsFPMath(false),
   NoTrappingFPMath(true), NoSignedZerosFPMath(false),
-  EnableAIXExtendedAltivecABI(false),
+  ApproxFuncFPMath(false), EnableAIXExtendedAltivecABI(false),
   HonorSignDependentRoundingFPMathOption(false), NoZerosInBSS(false),
   GuaranteedTailCallOpt(false), StackSymbolOrdering(true),
   EnableFastISel(false), EnableGlobalISel(false), UseInitArray(false),
@@ -183,6 +183,12 @@
 /// argument or result as insignificant.
 unsigned NoSignedZerosFPMath : 1;
 
+/// ApproxFuncFPMath - This flag is enabled when the
+/// -enable-approx-func-fp-math is specified on the command line. This
+/// specifies that optimizations are allowed to substitute math functions
+/// with approximate calculations
+unsigned ApproxFuncFPMath : 1;
+
 /// EnableAIXExtendedAltivecABI - This flag returns true when -vec-extabi is
 /// specified. The code generator is then able to use both volatile and
 /// nonvolitle vector registers. When false, the code generator only uses
Index: clang/test/Driver/fast-math.c
===
--- clang/test/Driver/fast-math.c
+++ clang/test/Driver/fast-math.c
@@ -70,6 +70,11 @@
 // CHECK-NO-NANS-NO-FAST-MATH: "-cc1"
 // CHECK-NO-NANS-NO-FAST-MATH-NOT: "-menable-no-nans"
 //
+// RUN: %clang -### -fapprox-func -c %s 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-APPROX-FUNC %s
+// CHECK-APPROX-FUNC: "-cc1"
+// CHECK-APPROX-FUNC: "-fapprox-func"
+//
 // RUN: %clang -### -fmath-errno -c %s 2>&1 \
 // RUN:   | FileCheck --check-prefix=CHECK-MATH-ERRNO %s
 // CHECK-MATH-ERRNO: "-cc1"
Index: clang/test/CodeGen/afn-flag-test.c
===
--- /dev/null
+++ clang/test/CodeGen/afn-flag-test.c
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -fapprox-func  %s -emit-llvm -o - | FileCheck --check-prefix=CHECK-AFN %s
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck --check-prefix=CHECK-NO-AFN %s
+
+extern double exp(double);
+double afn_option_test(double x) {
+  return exp(x);
+  // CHECK-LABEL:  define{{.*}} double @afn_option_test(double %x) #0 {
+
+  // CHECK-AFN:  %{{.*}} = call afn double @{{.*}}exp{{.*}}(double %{{.*}})
+  // CHECK-AFN:  attributes #0 ={{.*}} "approx-func-fp-math"="true" {{.*}}
+
+  // CHECK-NO-AFN:   %{{.*}} = call double @{{.*}}exp{{.*}}(double %{{.*}})
+  // CHECK-NO-AFN-NOT:  attributes #0 ={{.*}} "approx-func-fp-math"="true" {{.*}}
+}
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -2618,6 +2618,7 @@
   // LLVM flags based on the final state.
   bool HonorINFs = true;
   bool HonorNaNs = true;
+  bool ApproxFunc = false;
   // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
   bool MathErrno = TC.IsMathErrnoDefault();
   bool AssociativeMath = false;
@@ -2722,6 +2723,8 @@
 case options::OPT_fno_honor_infinities: HonorINFs = false;break;
 case options::OPT_fhonor_nans:  HonorNaNs = true; break;
 case options::OPT_fno_honor_nans:   HonorNaNs = false;break;
+case options::OPT_fapprox_func: ApproxFunc = true;break;
+case options::OPT_fno_approx_func:  ApproxFunc = false;   break;
 case options::OPT_fmath_errno:  MathErrno = true; break;
 case options::OPT_fno_math_errno:   MathErrno = false;break;
 case options::OPT_fassociative_math:AssociativeMath = true;   break;
@@ -2917,6 +2920,9 @@
   if (!HonorNaNs)
 CmdArgs.push_back("-menable-no-nans");
 
+  if (ApproxFunc)
+CmdArgs.push_back("-fapprox-func");
+
   if (MathErrno)
 CmdArgs.push_back("-fmath-errno");
 
Index: clang/lib/CodeGen/CGCall.cpp
===
--- clang/lib/CodeGen/CGCall.cpp
+++ clang/lib/CodeGen/CGCall.cpp
@@ -1828,6 +1828,8 @@
   FuncAttrs.addAttribute("no-infs-fp-math", "true");
 if (LangOpts.NoHonorNaNs)
   

[PATCH] D106191: [clang] Option control afn flag

2021-10-07 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added a comment.

Agreed on the request for description and driver test coverage. I'm also 
wondering if there should be a release note and documentation because it's 
lifting a private flag to be a public one. e.g., should we update 
https://clang.llvm.org/docs/UsersManual.html#controlling-floating-point-behavior
 ?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D106191: [clang] Option control afn flag

2021-10-07 Thread Erich Keane via Phabricator via cfe-commits
erichkeane added a comment.

In D106191#3046757 , @lebedev.ri 
wrote:

> Looks reasonable to me.
> @aaron.ballman @erichkeane does it seem complete or are we missing some infra 
> piece?

At least from the CFE's side I think this is complete as far as I can see.  I 
agree we need to have the description.  I would also like to see a Driver test 
on this as well.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D106191: [clang] Option control afn flag

2021-10-06 Thread Roman Lebedev via Phabricator via cfe-commits
lebedev.ri added reviewers: aaron.ballman, erichkeane.
lebedev.ri added subscribers: erichkeane, aaron.ballman.
lebedev.ri added a comment.

Looks reasonable to me.
@aaron.ballman @erichkeane does it seem complete or are we missing some infra 
piece?




Comment at: clang/include/clang/Driver/Options.td:1733
+defm approx_func : BoolFOption<"approx-func", LangOpts<"ApproxFunc">, 
DefaultFalse,
+  PosFlag, 
NegFlag>;
 defm finite_math_only : BoolFOption<"finite-math-only",

Missing description


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D106191: [clang] Option control afn flag

2021-10-06 Thread Masoud Ataei via Phabricator via cfe-commits
masoud.ataei added a comment.

Reminder for reviewers.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D106191: [clang] Option control afn flag

2021-09-28 Thread Nemanja Ivanovic via Phabricator via cfe-commits
nemanjai added a comment.

Added @jansvoboda11 to the review as it appears he was the one that added the 
original option.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D106191: [clang] Option control afn flag

2021-09-28 Thread Masoud Ataei via Phabricator via cfe-commits
masoud.ataei marked 4 inline comments as done.
masoud.ataei added a comment.

Sorry that it took me so long to reply reviews. Thank you for reviewing this 
patch.




Comment at: clang/include/clang/Driver/Options.td:1732-1733
   NegFlag>;
-def fapprox_func : Flag<["-"], "fapprox-func">, Group, 
Flags<[CC1Option, NoDriverOption]>,
-  MarshallingInfoFlag>, 
ImpliedByAnyOf<[menable_unsafe_fp_math.KeyPath]>;
 defm finite_math_only : BoolFOption<"finite-math-only",

bmahjour wrote:
> So this option already exists and seems to behave the way we want it to. Does 
> anyone know why it was made `NoDriverOption`?
> 
> ```
> > cat fp.c
> #include 
> void foo(float *f1, float *f2)
> {
>   *f1 = sin(*f2) + *f1;
> }
> > clang -c fp.c -S -emit-llvm -mllvm -disable-llvm-optzns -O3 -Xclang 
> > -fapprox-func && grep afn fp.ll
>   %call = call afn double @sin(double %conv) #2
>   %add = fadd afn double %call, %conv1
> ```
> 
> Could we just expose it as a supported option and call it done. ie make it 
> more like `fhonor_nans` below but without introducing a new function 
> attribute:
> 
> ```def fapprox_func : Flag<["-"], "fapprox-func">, Group;```
> 
> so that instead of having `-Xclang -fapprox-func ` in the command above we 
> could just have `-fapprox-func `?
With the change that I proposed for `approx_func` option, we don't need to pass 
it with `-Xclang`. So, in your example, we can simply remove `-Xclang` and it 
will work the same:
```
> clang -c fp.c -S -emit-llvm -mllvm -disable-llvm-optzns -O3 -fapprox-func && 
> grep afn fp.ll
  %call = call afn double @sin(double %conv) #2
  %add = fadd afn double %call, %conv1
```
(even no need for ` -mllvm -disable-llvm-optzns -O3`)

Example for `-fapprox-func` and `-fno-approx-func`:
```
$ clang -c fp.c -S -emit-llvm -fapprox-func && grep afn fp.ll
  %call = call afn double @sin(double %conv) #2
  %add = fadd afn double %call, %conv1
$ clang -c fp.c -S -emit-llvm -fno-approx-func && grep afn fp.ll
$
```



Comment at: clang/lib/CodeGen/CGCall.cpp:1818
+if (LangOpts.ApproxFunc)
+  FuncAttrs.addAttribute("approx-func-fp-math", "true");
 if (LangOpts.UnsafeFPMath)

Regarding the attributes discussion below, here is place that attributes are 
added. 



Comment at: clang/test/CodeGen/afn-flag-test.c:10
+  // CHECK-AFN:  %{{.*}} = call afn double @{{.*}}exp{{.*}}(double %{{.*}})
+  // CHECK-AFN:  attributes #0 ={{.*}} "approx-func-fp-math"="true" {{.*}}
+

bmahjour wrote:
> can we avoid these attributes?
I understand that attributes are less desirable now in LLVM. But all other 
options for fast-math flags (`-fhonor-nans`, `-fhonor_infinities`, ...) add the 
attributes too. So, to be consistent with others I added this attribute too. 

Example: This will add `"no-nans-fp-math"="true"` attribute along with `nnan` 
flag. 
```
$ clang -c fp.c -S -emit-llvm -fno-honor-nans
```




Comment at: clang/test/CodeGen/afn-flag-test.c:13
+  // CHECK-NO-AFN:   %{{.*}} = call double @{{.*}}exp{{.*}}(double %{{.*}})
+  // CHECK-NO-AFN-NOT:  attributes #0 ={{.*}} "approx-func-fp-math"="true" 
{{.*}}
+}

bmahjour wrote:
> avoid attributes.
Same as above.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D106191: [clang] Option control afn flag

2021-08-30 Thread Bardia Mahjour via Phabricator via cfe-commits
bmahjour added inline comments.



Comment at: clang/include/clang/Driver/Options.td:1732-1733
   NegFlag>;
-def fapprox_func : Flag<["-"], "fapprox-func">, Group, 
Flags<[CC1Option, NoDriverOption]>,
-  MarshallingInfoFlag>, 
ImpliedByAnyOf<[menable_unsafe_fp_math.KeyPath]>;
 defm finite_math_only : BoolFOption<"finite-math-only",

So this option already exists and seems to behave the way we want it to. Does 
anyone know why it was made `NoDriverOption`?

```
> cat fp.c
#include 
void foo(float *f1, float *f2)
{
  *f1 = sin(*f2) + *f1;
}
> clang -c fp.c -S -emit-llvm -mllvm -disable-llvm-optzns -O3 -Xclang 
> -fapprox-func && grep afn fp.ll
  %call = call afn double @sin(double %conv) #2
  %add = fadd afn double %call, %conv1
```

Could we just expose it as a supported option and call it done. ie make it more 
like `fhonor_nans` below but without introducing a new function attribute:

```def fapprox_func : Flag<["-"], "fapprox-func">, Group;```

so that instead of having `-Xclang -fapprox-func ` in the command above we 
could just have `-fapprox-func `?



Comment at: clang/test/CodeGen/afn-flag-test.c:10
+  // CHECK-AFN:  %{{.*}} = call afn double @{{.*}}exp{{.*}}(double %{{.*}})
+  // CHECK-AFN:  attributes #0 ={{.*}} "approx-func-fp-math"="true" {{.*}}
+

can we avoid these attributes?



Comment at: clang/test/CodeGen/afn-flag-test.c:13
+  // CHECK-NO-AFN:   %{{.*}} = call double @{{.*}}exp{{.*}}(double %{{.*}})
+  // CHECK-NO-AFN-NOT:  attributes #0 ={{.*}} "approx-func-fp-math"="true" 
{{.*}}
+}

avoid attributes.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D106191: [clang] Option control afn flag

2021-08-27 Thread Roman Lebedev via Phabricator via cfe-commits
lebedev.ri added a comment.

FWIW i think this part is fine in principle.
Not sure about errno.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D106191: [clang] Option control afn flag

2021-08-26 Thread Masoud Ataei via Phabricator via cfe-commits
masoud.ataei added a comment.

In D106191#2966852 , @qiucf wrote:

> Making a new option mapped to another float op flag looks reasonable, but is 
> there any clearer motivation for this? (such as the need for `-Ofast 
> -fno-approx-func`)

This patch https://reviews.llvm.org/D101759 is the real motivation for option 
controlling afn flag. We want to have a way to distinguishes getting `_finite` 
or `non-finite` version of MASS functions. Only with afn flag (on O3 
), we want to get `non-finite` 
version of MASS functions. (finite version need extra fast-math flags.)




Comment at: llvm/include/llvm/Target/TargetOptions.h:179
+/// with approximate calculations
+unsigned ApproxFuncFPMath : 1;
+

qiucf wrote:
> `-enable-no-signed-zeros-fp-math` is an `llc` flag.
> 
> Do we really have `-enable-approx-func-fp-math` for `llc` now?
I am adding and using this option in https://reviews.llvm.org/D101759 patch. 


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D106191: [clang] Option control afn flag

2021-08-26 Thread Qiu Chaofan via Phabricator via cfe-commits
qiucf added a comment.

Making a new option mapped to another float op flag looks reasonable, but is 
there any clearer motivation for this? (such as the need for `-Ofast 
-fno-approx-func`)




Comment at: llvm/include/llvm/Target/TargetOptions.h:179
+/// with approximate calculations
+unsigned ApproxFuncFPMath : 1;
+

`-enable-no-signed-zeros-fp-math` is an `llc` flag.

Do we really have `-enable-approx-func-fp-math` for `llc` now?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D106191: [clang] Option control afn flag

2021-07-16 Thread Masoud Ataei via Phabricator via cfe-commits
masoud.ataei updated this revision to Diff 359454.
masoud.ataei added a comment.

Remove extra function deceleration.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106191/new/

https://reviews.llvm.org/D106191

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/BackendUtil.cpp
  clang/lib/CodeGen/CGCall.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/test/CodeGen/afn-flag-test.c
  llvm/include/llvm/Target/TargetOptions.h

Index: llvm/include/llvm/Target/TargetOptions.h
===
--- llvm/include/llvm/Target/TargetOptions.h
+++ llvm/include/llvm/Target/TargetOptions.h
@@ -115,7 +115,7 @@
 TargetOptions()
 : UnsafeFPMath(false), NoInfsFPMath(false), NoNaNsFPMath(false),
   NoTrappingFPMath(true), NoSignedZerosFPMath(false),
-  EnableAIXExtendedAltivecABI(false),
+  ApproxFuncFPMath(false), EnableAIXExtendedAltivecABI(false),
   HonorSignDependentRoundingFPMathOption(false), NoZerosInBSS(false),
   GuaranteedTailCallOpt(false), StackSymbolOrdering(true),
   EnableFastISel(false), EnableGlobalISel(false), UseInitArray(false),
@@ -172,6 +172,12 @@
 /// argument or result as insignificant.
 unsigned NoSignedZerosFPMath : 1;
 
+/// ApproxFuncFPMath - This flag is enabled when the
+/// -enable-approx-func-fp-math is specified on the command line. This
+/// specifies that optimizations are allowed to substitute math functions
+/// with approximate calculations
+unsigned ApproxFuncFPMath : 1;
+
 /// EnableAIXExtendedAltivecABI - This flag returns true when -vec-extabi is
 /// specified. The code generator is then able to use both volatile and
 /// nonvolitle vector regisers. When false, the code generator only uses
Index: clang/test/CodeGen/afn-flag-test.c
===
--- /dev/null
+++ clang/test/CodeGen/afn-flag-test.c
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -fapprox-func  %s -emit-llvm -o - | FileCheck --check-prefix=CHECK-AFN %s
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck --check-prefix=CHECK-NO-AFN %s
+
+extern double exp(double);
+double afn_option_test(double x) {
+  return exp(x);
+  // CHECK-LABEL:  define{{.*}} double @afn_option_test(double %x) #0 {
+
+  // CHECK-AFN:  %{{.*}} = call afn double @{{.*}}exp{{.*}}(double %{{.*}})
+  // CHECK-AFN:  attributes #0 ={{.*}} "approx-func-fp-math"="true" {{.*}}
+
+  // CHECK-NO-AFN:   %{{.*}} = call double @{{.*}}exp{{.*}}(double %{{.*}})
+  // CHECK-NO-AFN-NOT:  attributes #0 ={{.*}} "approx-func-fp-math"="true" {{.*}}
+}
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -2616,6 +2616,7 @@
   // LLVM flags based on the final state.
   bool HonorINFs = true;
   bool HonorNaNs = true;
+  bool ApproxFunc = false;
   // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
   bool MathErrno = TC.IsMathErrnoDefault();
   bool AssociativeMath = false;
@@ -2720,6 +2721,8 @@
 case options::OPT_fno_honor_infinities: HonorINFs = false;break;
 case options::OPT_fhonor_nans:  HonorNaNs = true; break;
 case options::OPT_fno_honor_nans:   HonorNaNs = false;break;
+case options::OPT_fapprox_func: ApproxFunc = true;break;
+case options::OPT_fno_approx_func:  ApproxFunc = false;   break;
 case options::OPT_fmath_errno:  MathErrno = true; break;
 case options::OPT_fno_math_errno:   MathErrno = false;break;
 case options::OPT_fassociative_math:AssociativeMath = true;   break;
@@ -2915,6 +2918,9 @@
   if (!HonorNaNs)
 CmdArgs.push_back("-menable-no-nans");
 
+  if (ApproxFunc)
+CmdArgs.push_back("-fapprox-func");
+
   if (MathErrno)
 CmdArgs.push_back("-fmath-errno");
 
Index: clang/lib/CodeGen/CGCall.cpp
===
--- clang/lib/CodeGen/CGCall.cpp
+++ clang/lib/CodeGen/CGCall.cpp
@@ -1814,6 +1814,8 @@
   FuncAttrs.addAttribute("no-infs-fp-math", "true");
 if (LangOpts.NoHonorNaNs)
   FuncAttrs.addAttribute("no-nans-fp-math", "true");
+if (LangOpts.ApproxFunc)
+  FuncAttrs.addAttribute("approx-func-fp-math", "true");
 if (LangOpts.UnsafeFPMath)
   FuncAttrs.addAttribute("unsafe-fp-math", "true");
 if (CodeGenOpts.SoftFloat)
Index: clang/lib/CodeGen/BackendUtil.cpp
===
--- clang/lib/CodeGen/BackendUtil.cpp
+++ clang/lib/CodeGen/BackendUtil.cpp
@@ -534,6 +534,7 @@
   Options.NoNaNsFPMath = LangOpts.NoHonorNaNs;
   Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
   Options.UnsafeFPMath = LangOpts.UnsafeFPMath;
+  Options.ApproxFuncFPMath = 

[PATCH] D106191: [clang] Option control afn flag

2021-07-16 Thread Masoud Ataei via Phabricator via cfe-commits
masoud.ataei created this revision.
masoud.ataei added reviewers: bmahjour, Whitney, nemanjai, shchenz.
masoud.ataei added projects: clang, LLVM.
Herald added subscribers: ormris, dang.
masoud.ataei requested review of this revision.
Herald added subscribers: llvm-commits, cfe-commits.

Proposing an clang option to control afn flag.

This change is proposed as a support for: https://reviews.llvm.org/D101759


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D106191

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/BackendUtil.cpp
  clang/lib/CodeGen/CGCall.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/test/CodeGen/afn-flag-test.c
  llvm/include/llvm/CodeGen/CommandFlags.h
  llvm/include/llvm/Target/TargetOptions.h

Index: llvm/include/llvm/Target/TargetOptions.h
===
--- llvm/include/llvm/Target/TargetOptions.h
+++ llvm/include/llvm/Target/TargetOptions.h
@@ -115,7 +115,7 @@
 TargetOptions()
 : UnsafeFPMath(false), NoInfsFPMath(false), NoNaNsFPMath(false),
   NoTrappingFPMath(true), NoSignedZerosFPMath(false),
-  EnableAIXExtendedAltivecABI(false),
+  ApproxFuncFPMath(false), EnableAIXExtendedAltivecABI(false),
   HonorSignDependentRoundingFPMathOption(false), NoZerosInBSS(false),
   GuaranteedTailCallOpt(false), StackSymbolOrdering(true),
   EnableFastISel(false), EnableGlobalISel(false), UseInitArray(false),
@@ -172,6 +172,12 @@
 /// argument or result as insignificant.
 unsigned NoSignedZerosFPMath : 1;
 
+/// ApproxFuncFPMath - This flag is enabled when the
+/// -enable-approx-func-fp-math is specified on the command line. This
+/// specifies that optimizations are allowed to substitute math functions
+/// with approximate calculations
+unsigned ApproxFuncFPMath : 1;
+
 /// EnableAIXExtendedAltivecABI - This flag returns true when -vec-extabi is
 /// specified. The code generator is then able to use both volatile and
 /// nonvolitle vector regisers. When false, the code generator only uses
Index: llvm/include/llvm/CodeGen/CommandFlags.h
===
--- llvm/include/llvm/CodeGen/CommandFlags.h
+++ llvm/include/llvm/CodeGen/CommandFlags.h
@@ -63,6 +63,8 @@
 
 bool getEnableNoSignedZerosFPMath();
 
+bool getEnableApproxFuncFPMath();
+
 bool getEnableNoTrappingFPMath();
 
 DenormalMode::DenormalModeKind getDenormalFPMath();
Index: clang/test/CodeGen/afn-flag-test.c
===
--- /dev/null
+++ clang/test/CodeGen/afn-flag-test.c
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -fapprox-func  %s -emit-llvm -o - | FileCheck --check-prefix=CHECK-AFN %s
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck --check-prefix=CHECK-NO-AFN %s
+
+extern double exp(double);
+double afn_option_test(double x) {
+  return exp(x);
+  // CHECK-LABEL:  define{{.*}} double @afn_option_test(double %x) #0 {
+
+  // CHECK-AFN:  %{{.*}} = call afn double @{{.*}}exp{{.*}}(double %{{.*}})
+  // CHECK-AFN:  attributes #0 ={{.*}} "approx-func-fp-math"="true" {{.*}}
+
+  // CHECK-NO-AFN:   %{{.*}} = call double @{{.*}}exp{{.*}}(double %{{.*}})
+  // CHECK-NO-AFN-NOT:  attributes #0 ={{.*}} "approx-func-fp-math"="true" {{.*}}
+}
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -2616,6 +2616,7 @@
   // LLVM flags based on the final state.
   bool HonorINFs = true;
   bool HonorNaNs = true;
+  bool ApproxFunc = false;
   // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
   bool MathErrno = TC.IsMathErrnoDefault();
   bool AssociativeMath = false;
@@ -2720,6 +2721,8 @@
 case options::OPT_fno_honor_infinities: HonorINFs = false;break;
 case options::OPT_fhonor_nans:  HonorNaNs = true; break;
 case options::OPT_fno_honor_nans:   HonorNaNs = false;break;
+case options::OPT_fapprox_func: ApproxFunc = true;break;
+case options::OPT_fno_approx_func:  ApproxFunc = false;   break;
 case options::OPT_fmath_errno:  MathErrno = true; break;
 case options::OPT_fno_math_errno:   MathErrno = false;break;
 case options::OPT_fassociative_math:AssociativeMath = true;   break;
@@ -2915,6 +2918,9 @@
   if (!HonorNaNs)
 CmdArgs.push_back("-menable-no-nans");
 
+  if (ApproxFunc)
+CmdArgs.push_back("-fapprox-func");
+
   if (MathErrno)
 CmdArgs.push_back("-fmath-errno");
 
Index: clang/lib/CodeGen/CGCall.cpp
===
--- clang/lib/CodeGen/CGCall.cpp
+++ clang/lib/CodeGen/CGCall.cpp
@@ -1814,6 +1814,8 @@
   FuncAttrs.addAttribute("no-infs-fp-math", "true");
 if (LangOpts.NoHonorNaNs)