Skip to content

Commit b4b06d8

Browse files
committed
__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
1 parent 42e371b commit b4b06d8

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

clang/lib/Basic/Targets/SPIR.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ class LLVM_LIBRARY_VISIBILITY SPIRTargetInfo : public BaseSPIRTargetInfo {
191191
bool hasFeature(StringRef Feature) const override {
192192
return Feature == "spir";
193193
}
194+
195+
bool checkArithmeticFenceSupported() const override { return true; }
194196
};
195197

196198
class LLVM_LIBRARY_VISIBILITY SPIR32TargetInfo : public SPIRTargetInfo {

clang/test/CodeGen/arithmetic-fence-builtin.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
// RUN: %clang_cc1 -triple i386-pc-linux-gnu -emit-llvm -fprotect-parens\
1313
// RUN: -o - %s | FileCheck --implicit-check-not="llvm.arithmetic.fence" %s
1414
//
15+
// Test with fast math on spir target
16+
// RUN: %clang_cc1 -triple spir64 -emit-llvm -DFAST \
17+
// RUN: -mreassociate -o - %s \
18+
// RUN: | FileCheck --check-prefixes CHECK,CHECKFAST,CHECKNP %s
19+
//
20+
1521
int v;
1622
int addit(float a, float b) {
1723
// CHECK: define {{.*}}@addit(float noundef %a, float noundef %b) #0 {

clang/test/Sema/arithmetic-fence-builtin.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// RUN: %clang_cc1 -triple ppc64le -DPPC -emit-llvm -o - -verify -x c++ %s
33
// RUN: not %clang_cc1 -triple ppc64le -DPPC -emit-llvm -o - -x c++ %s \
44
// RUN: -fprotect-parens 2>&1 | FileCheck -check-prefix=PPC %s
5+
// RUN: %clang_cc1 -triple spir64 -emit-llvm -o - -verify -x c++ %s
56
#ifndef PPC
67
int v;
78
template <typename T> T addT(T a, T b) {

0 commit comments

Comments
 (0)