Skip to content

[SYCL] Add support for __arithmetic_fence builtin for SYCL targets. #8072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/lib/Basic/Targets/SPIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,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 {
Expand Down
36 changes: 29 additions & 7 deletions clang/test/CodeGen/arithmetic-fence-builtin.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
// 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: -o - %s | FileCheck --check-prefixes CHECK,CHECKFAST,CHECKPRECISION,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: -o - %s | FileCheck --check-prefixes CHECK,CHECKFAST,CHECKPRECISION,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
//
int v;
int addit(float a, float b) {
// CHECK: define {{.*}}@addit(float noundef %a, float noundef %b) #0 {
// RUN: %clang_cc1 -triple spir64-unknown-unknown -emit-llvm -fsycl-is-device \
// RUN: -mreassociate -opaque-pointers -DSYCL_DEVICE \
// RUN: -o - %s | FileCheck --check-prefixes CHECKS,CHECKFAST,CHECKPPS %s

#ifdef SYCL_DEVICE
int __attribute__((sycl_device)) addit(float a, float b)
#else
int addit(float a, float b)
#endif
{
int v;
// CHECKS: define dso_local spir_func i32 @addit(float noundef %a, float noundef %b) #0
// CHECK: define {{.*}} @addit(float noundef %a, float noundef %b) #0 {
_Complex double cd, cd1;
cd = __arithmetic_fence(cd1);
// CHECKFAST: call{{.*}} double @llvm.arithmetic.fence.f64({{.*}}real)
Expand All @@ -32,7 +42,11 @@ int addit(float a, float b) {

v = (a + b);
// CHECKPP: call{{.*}} float @llvm.arithmetic.fence.f32(float %add{{.*}})

v = a + (b*b);
// CHECKPPS: fmul
// CHECKPPS-NEXT: fadd

// CHECKPP: fmul reassoc
// CHECKPP-NEXT: call{{.*}} float @llvm.arithmetic.fence.f32(float %mul)
// CHECKNP: fmul
Expand All @@ -56,16 +70,24 @@ int addit(float a, float b) {
return 0;
// CHECK-NEXT ret i32 0
}
int addit1(int a, int b) {

#ifdef SYCL_DEVICE
int __attribute__((sycl_device)) addit1(int a, int b)
#else
int addit1(int a, int b)
#endif
{
int v;
// CHECK: define {{.*}}@addit1(i32 noundef %a, i32 noundef %b{{.*}}
v = (a + b);
// CHECK-NOT: call{{.*}} float @llvm.arithmetic.fence.int(float noundef %add)
return 0;
}

#ifdef FAST
#pragma float_control(precise, on)
int subit(float a, float b, float *fp) {
// CHECKFAST: define {{.*}}@subit(float noundef %a, float noundef %b{{.*}}
// CHECKPRECISION: 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)
Expand Down
19 changes: 16 additions & 3 deletions clang/test/Sema/arithmetic-fence-builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,29 @@
// 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-unknown-unknown -emit-llvm -fsycl-is-device \
// RUN: -DSYCL_DEVICE -o - -verify -x c++ %s

#ifndef PPC
int v;
template <typename T> T addT(T a, T b) {
#ifdef SYCL_DEVICE
template <typename T> T __attribute__((sycl_device)) addT(T a, T b)
#else
template <typename T> T addT(T a, T b)
#endif
{
T *q = __arithmetic_fence(&a);
// expected-error@-1 {{invalid operand of type 'float *' where floating, complex or a vector of such types is required}}
// expected-error@-2 {{invalid operand of type 'int *' where floating, complex or a vector of such types is required}}
return __arithmetic_fence(a + b);
// expected-error@-1 {{invalid operand of type 'int' where floating, complex or a vector of such types is required}}
}
int addit(int a, int b) {
#ifdef SYCL_DEVICE
int __attribute__((sycl_device)) addit(int a, int b)
#else
int addit(int a, int b)
#endif
{
int v;
float x, y;
typedef struct {
int a, b;
Expand Down