[PATCH] D80440: [OpenCL] Prevent fused mul and add by default

Anastasia Stulova via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri May 22 08:01:45 PDT 2020


Anastasia created this revision.
Anastasia added reviewers: rjmccall, arsenm.
Herald added subscribers: ebevhan, yaxunl, wdng.

Currently, clang will generate fused operation `fmuladd` for expressions like `a*b+c`. However, I can't find anything in the spec that explains this behavior. But clang was doing this for almost 10 years....

I looked at:
**s6.13.2 **where it details fp conract pragma:

  // on-off-switch is one of ON, OFF, or DEFAULT.
  // The DEFAULT value is ON.
  #pragma OPENCL FP_CONTRACT on-off-switch

The default behavior here refers to the default value of the pragma when used in the source code.

**s7.4** table 38

  x * y + z
  Implemented either as a correctly rounded fma or as a multiply and an add both of which are correctly rounded.

This table described behavior when `-cl-unsafe-math-optimizations` flag is passed.

I can't find anything else that would justify allowing fused operations, therefore I assume this is not safe to do and can cause issues on some applications/targets


https://reviews.llvm.org/D80440

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


Index: clang/test/CodeGenOpenCL/single-precision-constant.cl
===================================================================
--- clang/test/CodeGenOpenCL/single-precision-constant.cl
+++ clang/test/CodeGenOpenCL/single-precision-constant.cl
@@ -1,6 +1,7 @@
 // RUN: %clang_cc1 %s -cl-single-precision-constant -emit-llvm -o - | FileCheck %s
 
 float fn(float f) {
-  // CHECK: tail call float @llvm.fmuladd.f32(float %f, float 2.000000e+00, float 1.000000e+00)
+  // CHECK: fmul float %f, 2.000000e+00
+  // CHECK: fadd float %mul, 1.000000e+00
   return f*2. + 1.;
 }
Index: clang/test/CodeGenOpenCL/relaxed-fpmath.cl
===================================================================
--- clang/test/CodeGenOpenCL/relaxed-fpmath.cl
+++ clang/test/CodeGenOpenCL/relaxed-fpmath.cl
@@ -17,7 +17,8 @@
 }
 
 float fused_mad(float a, float b, float c) {
-  // NORMAL: tail call float @llvm.fmuladd.f32(float %a, float %b, float %c)
+  // NORMAL: fmul float
+  // NORMAL: fadd float
   // FAST: fmul fast float
   // FAST: fadd fast float
   // MAD: fmul contract float
Index: clang/lib/Frontend/CompilerInvocation.cpp
===================================================================
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -2322,7 +2322,6 @@
     Opts.AltiVec = 0;
     Opts.ZVector = 0;
     Opts.setLaxVectorConversions(LangOptions::LaxVectorConversionKind::None);
-    Opts.setDefaultFPContractMode(LangOptions::FPM_On);
     Opts.NativeHalfType = 1;
     Opts.NativeHalfArgsAndReturns = 1;
     Opts.OpenCLCPlusPlus = Opts.CPlusPlus;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80440.265738.patch
Type: text/x-patch
Size: 1596 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200522/edfdf69d/attachment-0001.bin>


More information about the cfe-commits mailing list