zahiraam created this revision.
zahiraam added reviewers: bader, andrew.w.kaylor.
Herald added subscribers: Naghasan, Anastasia, ebevhan, yaxunl.
Herald added a project: All.
zahiraam requested review of this revision.
Herald added a project: clang.

__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-V.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D142583

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


Index: clang/test/Sema/arithmetic-fence-builtin.c
===================================================================
--- clang/test/Sema/arithmetic-fence-builtin.c
+++ clang/test/Sema/arithmetic-fence-builtin.c
@@ -2,6 +2,8 @@
 // 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 -fsycl-is-device \
+// RUN: -o - -verify -x c++ %s
 #ifndef PPC
 int v;
 template <typename T> T addT(T a, T b) {
Index: clang/test/CodeGen/arithmetic-fence-builtin.c
===================================================================
--- clang/test/CodeGen/arithmetic-fence-builtin.c
+++ clang/test/CodeGen/arithmetic-fence-builtin.c
@@ -1,17 +1,23 @@
 // Test with fast math
 // RUN: %clang_cc1 -triple i386-pc-linux-gnu -emit-llvm -DFAST \
-// RUN: -mreassociate \
-// RUN: -o - %s | FileCheck --check-prefixes CHECK,CHECKFAST,CHECKNP %s
+// RUN: -mreassociate -o - %s \
+// RUN: | FileCheck --check-prefixes CHECK,CHECKFAST,CHECKPRECISE,CHECKNP %s
 //
 // Test with fast math and fprotect-parens
 // RUN: %clang_cc1 -triple i386-pc-linux-gnu -emit-llvm -DFAST \
-// RUN: -mreassociate -fprotect-parens -ffp-contract=on\
-// RUN: -o - %s | FileCheck --check-prefixes CHECK,CHECKFAST,CHECKPP %s
+// RUN: -mreassociate -fprotect-parens -ffp-contract=on -o - %s \
+// RUN: | FileCheck --check-prefixes CHECK,CHECKFAST,CHECKPRECISE,CHECKPP %s
 //
 // Test without fast math: llvm intrinsic not created
 // 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 -fsycl-is-device \
+// 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 {
@@ -65,7 +71,7 @@
 #ifdef FAST
 #pragma float_control(precise, on)
 int subit(float a, float b, float *fp) {
-  // CHECKFAST: define {{.*}}@subit(float noundef %a, float noundef %b{{.*}}
+  // CHECKPRECISE: define {{.*}}@subit(float noundef %a, float noundef %b{{.*}}
   *fp = __arithmetic_fence(a - b);
   *fp = (a + b);
   // CHECK-NOT: call{{.*}} float @llvm.arithmetic.fence.f32(float noundef %add)
Index: clang/lib/Basic/Targets/SPIR.h
===================================================================
--- clang/lib/Basic/Targets/SPIR.h
+++ clang/lib/Basic/Targets/SPIR.h
@@ -191,6 +191,8 @@
   bool hasFeature(StringRef Feature) const override {
     return Feature == "spir";
   }
+
+  bool checkArithmeticFenceSupported() const override { return true; }
 };
 
 class LLVM_LIBRARY_VISIBILITY SPIR32TargetInfo : public SPIRTargetInfo {


Index: clang/test/Sema/arithmetic-fence-builtin.c
===================================================================
--- clang/test/Sema/arithmetic-fence-builtin.c
+++ clang/test/Sema/arithmetic-fence-builtin.c
@@ -2,6 +2,8 @@
 // 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 -fsycl-is-device \
+// RUN: -o - -verify -x c++ %s
 #ifndef PPC
 int v;
 template <typename T> T addT(T a, T b) {
Index: clang/test/CodeGen/arithmetic-fence-builtin.c
===================================================================
--- clang/test/CodeGen/arithmetic-fence-builtin.c
+++ clang/test/CodeGen/arithmetic-fence-builtin.c
@@ -1,17 +1,23 @@
 // Test with fast math
 // RUN: %clang_cc1 -triple i386-pc-linux-gnu -emit-llvm -DFAST \
-// RUN: -mreassociate \
-// RUN: -o - %s | FileCheck --check-prefixes CHECK,CHECKFAST,CHECKNP %s
+// RUN: -mreassociate -o - %s \
+// RUN: | FileCheck --check-prefixes CHECK,CHECKFAST,CHECKPRECISE,CHECKNP %s
 //
 // Test with fast math and fprotect-parens
 // RUN: %clang_cc1 -triple i386-pc-linux-gnu -emit-llvm -DFAST \
-// RUN: -mreassociate -fprotect-parens -ffp-contract=on\
-// RUN: -o - %s | FileCheck --check-prefixes CHECK,CHECKFAST,CHECKPP %s
+// RUN: -mreassociate -fprotect-parens -ffp-contract=on -o - %s \
+// RUN: | FileCheck --check-prefixes CHECK,CHECKFAST,CHECKPRECISE,CHECKPP %s
 //
 // Test without fast math: llvm intrinsic not created
 // 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 -fsycl-is-device \
+// 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 {
@@ -65,7 +71,7 @@
 #ifdef FAST
 #pragma float_control(precise, on)
 int subit(float a, float b, float *fp) {
-  // CHECKFAST: define {{.*}}@subit(float noundef %a, float noundef %b{{.*}}
+  // CHECKPRECISE: define {{.*}}@subit(float noundef %a, float noundef %b{{.*}}
   *fp = __arithmetic_fence(a - b);
   *fp = (a + b);
   // CHECK-NOT: call{{.*}} float @llvm.arithmetic.fence.f32(float noundef %add)
Index: clang/lib/Basic/Targets/SPIR.h
===================================================================
--- clang/lib/Basic/Targets/SPIR.h
+++ clang/lib/Basic/Targets/SPIR.h
@@ -191,6 +191,8 @@
   bool hasFeature(StringRef Feature) const override {
     return Feature == "spir";
   }
+
+  bool checkArithmeticFenceSupported() const override { return true; }
 };
 
 class LLVM_LIBRARY_VISIBILITY SPIR32TargetInfo : public SPIRTargetInfo {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D142583: [SPIR... Zahira Ammarguellat via Phabricator via cfe-commits

Reply via email to