[PATCH] D80416: [RFC][OpenCL] Set fp contract flag on -cl-mad-enable

Anastasia Stulova via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu May 21 15:43:54 PDT 2020


Anastasia created this revision.
Anastasia added reviewers: rjmccall, arsenm.
Herald added subscribers: ebevhan, yaxunl, wdng.
Anastasia marked an inline comment as done.
Anastasia added inline comments.
Anastasia retitled this revision from "[RCF][OpenCL] Set fp contract flag on -cl-mad-enable" to "[RFC][OpenCL] Set fp contract flag on -cl-mad-enable".


================
Comment at: clang/test/CodeGenOpenCL/relaxed-fpmath.cl:21
+float fused_mad(float a, float b, float c) {
+  // NORMAL: @llvm.fmuladd.f32
+  // FAST: fmul fast float
----------------
I don't find this behavior "NORMAL". I don't believe we should contract expressions by default in OpenCL...


I think setting `contract` flag on fp instructions with `-cl-mad-enable` should be sensible considering spec wording:

> -cl-mad-enable Allow a * b + c to be replaced by a mad. The mad computes a * b + c with reduced accuracy. For example, some OpenCL devices implement mad as truncate the result of a * b before adding it to c

However, I am unclear how it impacts `fdiv` instructions and etc, as I am not sure how exactly it is optimized. LLVM reference manual says:

> contract
> 
>   Allow floating-point contraction (e.g. fusing a multiply followed by an addition into a fused multiply-and-add).

Presumably `contract` makes no effect in `fdiv`?

Now another question is whether we could remove `LessPreciseFPMAD` from CodeGen options as I don't feel it has actual uses. Although I might be misunderstanding this.

TODO: the same applies to `-cl-unsafe-math-optimizations`


https://reviews.llvm.org/D80416

Files:
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/test/CodeGenOpenCL/relaxed-fpmath.cl


Index: clang/test/CodeGenOpenCL/relaxed-fpmath.cl
===================================================================
--- clang/test/CodeGenOpenCL/relaxed-fpmath.cl
+++ clang/test/CodeGenOpenCL/relaxed-fpmath.cl
@@ -12,10 +12,32 @@
   // FAST: fdiv fast float
   // FINITE: fdiv nnan ninf float
   // UNSAFE: fdiv nnan nsz float
-  // MAD: fdiv float
+  // MAD: fdiv contract float
   // NOSIGNED: fdiv nsz float
   return a / b;
 }
+
+float fused_mad(float a, float b, float c) {
+  // NORMAL: @llvm.fmuladd.f32
+  // FAST: fmul fast float
+  // FAST: fadd fast float
+  // MAD: fmul contract float
+  // MAD: fadd contract float
+  return a*b+c;
+}
+
+float fused_mad_accross_stmts(float a, float b, float c) {
+  // NORMAL: fmul float
+  // NORMAL: fadd float
+  // FAST: fmul fast float
+  // FAST: fadd fast float
+  // MAD: fmul contract float
+  // MAD: fadd contract float
+  float t = a*b;
+  return t+c;
+}
+
+
 // CHECK: attributes
 
 // NORMAL: "less-precise-fpmad"="false"
Index: clang/lib/Frontend/CompilerInvocation.cpp
===================================================================
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -2944,7 +2944,7 @@
   Opts.NoBitFieldTypeAlign = Args.hasArg(OPT_fno_bitfield_type_align);
   Opts.SinglePrecisionConstants = Args.hasArg(OPT_cl_single_precision_constant);
   Opts.FastRelaxedMath = Args.hasArg(OPT_cl_fast_relaxed_math);
-  if (Opts.FastRelaxedMath)
+  if (Opts.FastRelaxedMath || Args.hasArg(OPT_cl_mad_enable))
     Opts.setDefaultFPContractMode(LangOptions::FPM_Fast);
   Opts.HexagonQdsp6Compat = Args.hasArg(OPT_mqdsp6_compat);
   Opts.FakeAddressSpaceMap = Args.hasArg(OPT_ffake_address_space_map);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80416.265628.patch
Type: text/x-patch
Size: 1720 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200521/3662dafe/attachment-0001.bin>


More information about the cfe-commits mailing list