[clang] b4b06d8 - __arithmetic_fence enforces ordering on expression evaluation when fast-math

Zahira Ammarguellat via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 26 11:18:35 PST 2023


Author: Zahira Ammarguellat
Date: 2023-01-26T14:18:28-05:00
New Revision: b4b06d8ff82647824a658356e1e8f7dc9d1ac7d2

URL: https://github.com/llvm/llvm-project/commit/b4b06d8ff82647824a658356e1e8f7dc9d1ac7d2
DIFF: https://github.com/llvm/llvm-project/commit/b4b06d8ff82647824a658356e1e8f7dc9d1ac7d2.diff

LOG: __arithmetic_fence enforces ordering on expression evaluation when fast-math
is enabled.
In fast math mode some floating-point optimizations are performed such as
reassociation and distribution.

For example, the compiler may transform (a+b)+c into a+(b+c). Although these
two expressions are equivalent in integer arithmetic, they may not be in
floating-point arithmetic. The builtin tells the compiler that the expression
in parenthesis can’t be re-associated or distributed.
__arithmetic_fence(a+b)+c is not equivalent to a+(b+c).

This patch adds the support of the builtin to SPIR target.

Differential Revision: https://reviews.llvm.org/D142583

Added: 
    

Modified: 
    clang/lib/Basic/Targets/SPIR.h
    clang/test/CodeGen/arithmetic-fence-builtin.c
    clang/test/Sema/arithmetic-fence-builtin.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Basic/Targets/SPIR.h b/clang/lib/Basic/Targets/SPIR.h
index 69596c6eb6fec..a65a479945cfe 100644
--- a/clang/lib/Basic/Targets/SPIR.h
+++ b/clang/lib/Basic/Targets/SPIR.h
@@ -191,6 +191,8 @@ class LLVM_LIBRARY_VISIBILITY SPIRTargetInfo : public BaseSPIRTargetInfo {
   bool hasFeature(StringRef Feature) const override {
     return Feature == "spir";
   }
+
+  bool checkArithmeticFenceSupported() const override { return true; }
 };
 
 class LLVM_LIBRARY_VISIBILITY SPIR32TargetInfo : public SPIRTargetInfo {

diff  --git a/clang/test/CodeGen/arithmetic-fence-builtin.c b/clang/test/CodeGen/arithmetic-fence-builtin.c
index 61d0b65277424..a9920c60e6a70 100644
--- a/clang/test/CodeGen/arithmetic-fence-builtin.c
+++ b/clang/test/CodeGen/arithmetic-fence-builtin.c
@@ -12,6 +12,12 @@
 // RUN: %clang_cc1 -triple i386-pc-linux-gnu -emit-llvm -fprotect-parens\
 // RUN: -o - %s | FileCheck --implicit-check-not="llvm.arithmetic.fence" %s
 //
+// Test with fast math on spir target
+// RUN: %clang_cc1 -triple spir64  -emit-llvm -DFAST \
+// RUN: -mreassociate -o - %s \
+// RUN: | FileCheck --check-prefixes CHECK,CHECKFAST,CHECKNP %s
+//
+
 int v;
 int addit(float a, float b) {
   // CHECK: define {{.*}}@addit(float noundef %a, float noundef %b) #0 {

diff  --git a/clang/test/Sema/arithmetic-fence-builtin.c b/clang/test/Sema/arithmetic-fence-builtin.c
index 4f4f0a02cde9e..a1941970edb53 100644
--- a/clang/test/Sema/arithmetic-fence-builtin.c
+++ b/clang/test/Sema/arithmetic-fence-builtin.c
@@ -2,6 +2,7 @@
 // RUN: %clang_cc1 -triple ppc64le -DPPC     -emit-llvm -o - -verify -x c++ %s
 // RUN: not %clang_cc1 -triple ppc64le -DPPC     -emit-llvm -o - -x c++ %s \
 // RUN:            -fprotect-parens 2>&1 | FileCheck -check-prefix=PPC %s
+// RUN: %clang_cc1 -triple spir64 -emit-llvm -o - -verify -x c++ %s
 #ifndef PPC
 int v;
 template <typename T> T addT(T a, T b) {


        


More information about the cfe-commits mailing list