Skip to content

Commit 1a3a536

Browse files
Andrew Savonichevromanovvlad
Andrew Savonichev
authored andcommitted
[SYCL] Optionally override addrspace map for SYCL device code
Default address space (applies when no explicit address space was specified) now maps to generic (4) address space. Set ENABLE_INFER_AS=1 to enable this mode. Signed-off-by: Andrew Savonichev <andrew.savonichev@intel.com>
1 parent 8ff4805 commit 1a3a536

File tree

5 files changed

+77
-6
lines changed

5 files changed

+77
-6
lines changed

clang/lib/Basic/Targets/SPIR.h

+23-1
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,36 @@ static const unsigned SPIRAddrSpaceMap[] = {
3838
4, // sycl_generic
3939
};
4040

41+
static const unsigned SYCLAddrSpaceMap[] = {
42+
4, // Default
43+
1, // opencl_global
44+
3, // opencl_local
45+
2, // opencl_constant
46+
0, // opencl_private
47+
4, // opencl_generic
48+
0, // cuda_device
49+
0, // cuda_constant
50+
0, // cuda_shared
51+
1, // sycl_global
52+
3, // sycl_local
53+
2, // sycl_constant
54+
0, // sycl_private
55+
4, // sycl_generic
56+
};
57+
4158
class LLVM_LIBRARY_VISIBILITY SPIRTargetInfo : public TargetInfo {
4259
public:
4360
SPIRTargetInfo(const llvm::Triple &Triple, const TargetOptions &)
4461
: TargetInfo(Triple) {
4562
TLSSupported = false;
4663
VLASupported = false;
4764
LongWidth = LongAlign = 64;
48-
AddrSpaceMap = &SPIRAddrSpaceMap;
65+
if (Triple.getEnvironment() == llvm::Triple::SYCLDevice &&
66+
getenv("ENABLE_INFER_AS")) {
67+
AddrSpaceMap = &SYCLAddrSpaceMap;
68+
} else {
69+
AddrSpaceMap = &SPIRAddrSpaceMap;
70+
}
4971
UseAddrSpaceMapMangling = true;
5072
HasLegalHalfType = true;
5173
HasFloat16 = true;

clang/lib/CodeGen/CGCall.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -4278,6 +4278,22 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
42784278
if (!CallArgs.getCleanupsToDeactivate().empty())
42794279
deactivateArgCleanupsBeforeCall(*this, CallArgs);
42804280

4281+
// Addrspace cast to generic if necessary
4282+
if (getenv("ENABLE_INFER_AS")) {
4283+
for (unsigned i = 0; i < IRFuncTy->getNumParams(); ++i) {
4284+
if (auto *PtrTy = dyn_cast<llvm::PointerType>(IRCallArgs[i]->getType())) {
4285+
auto *ExpectedPtrType =
4286+
cast<llvm::PointerType>(IRFuncTy->getParamType(i));
4287+
unsigned ValueAS = PtrTy->getAddressSpace();
4288+
unsigned ExpectedAS = ExpectedPtrType->getAddressSpace();
4289+
if (ValueAS != ExpectedAS) {
4290+
IRCallArgs[i] = Builder.CreatePointerBitCastOrAddrSpaceCast(
4291+
IRCallArgs[i], ExpectedPtrType);
4292+
}
4293+
}
4294+
}
4295+
}
4296+
42814297
// Assert that the arguments we computed match up. The IR verifier
42824298
// will catch this, but this is a common enough source of problems
42834299
// during IRGen changes that it's way better for debugging to catch

clang/lib/CodeGen/CGClass.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ Address CodeGenFunction::GetAddressOfBaseClass(
316316
EmitTypeCheck(TCK_Upcast, Loc, Value.getPointer(),
317317
DerivedTy, DerivedAlign, SkippedChecks);
318318
}
319-
return Builder.CreateBitCast(Value, BasePtrTy);
319+
return Builder.CreatePointerBitCastOrAddrSpaceCast(Value, BasePtrTy);
320320
}
321321

322322
llvm::BasicBlock *origBB = nullptr;

clang/lib/CodeGen/CGExpr.cpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -1089,10 +1089,8 @@ Address CodeGenFunction::EmitPointerWithAlignment(const Expr *E,
10891089
CodeGenFunction::CFITCK_UnrelatedCast,
10901090
CE->getBeginLoc());
10911091
}
1092-
return CE->getCastKind() != CK_AddressSpaceConversion
1093-
? Builder.CreateBitCast(Addr, ConvertType(E->getType()))
1094-
: Builder.CreateAddrSpaceCast(Addr,
1095-
ConvertType(E->getType()));
1092+
return Builder.CreatePointerBitCastOrAddrSpaceCast(
1093+
Addr, ConvertType(E->getType()));
10961094
}
10971095
break;
10981096

@@ -1741,6 +1739,18 @@ void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, Address Addr,
17411739
return;
17421740
}
17431741

1742+
if (getenv("ENABLE_INFER_AS")) {
1743+
if (auto *PtrTy = dyn_cast<llvm::PointerType>(Value->getType())) {
1744+
auto *ExpectedPtrType =
1745+
cast<llvm::PointerType>(Addr.getType()->getElementType());
1746+
unsigned ValueAS = PtrTy->getAddressSpace();
1747+
unsigned ExpectedAS = ExpectedPtrType->getAddressSpace();
1748+
if (ValueAS != ExpectedAS) {
1749+
Value =
1750+
Builder.CreatePointerBitCastOrAddrSpaceCast(Value, ExpectedPtrType);
1751+
}
1752+
}
1753+
}
17441754
llvm::StoreInst *Store = Builder.CreateStore(Value, Addr, Volatile);
17451755
if (isNontemporal) {
17461756
llvm::MDNode *Node =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %clang_cc1 -triple spir64-unknown-linux-sycldevice -std=c++11 -fsycl-is-device -disable-llvm-passes -emit-llvm -x c++ %s -o - | FileCheck %s --check-prefix=CHECK-DEFAULT
2+
// RUN: ENABLE_INFER_AS=1 %clang_cc1 -triple spir64-unknown-linux-sycldevice -std=c++11 -fsycl-is-device -disable-llvm-passes -emit-llvm -x c++ %s -o - | FileCheck %s --check-prefix=CHECK-NEW
3+
4+
5+
void test() {
6+
int i = 0;
7+
int *pptr = &i;
8+
// CHECK-DEFAULT: store i32* %i, i32** %pptr
9+
// CHECK-NEW: %[[GEN:[0-9]+]] = addrspacecast i32* %i to i32 addrspace(4)*
10+
// CHECK-NEW: store i32 addrspace(4)* %[[GEN]], i32 addrspace(4)** %pptr
11+
}
12+
13+
14+
template <typename name, typename Func>
15+
__attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
16+
kernelFunc();
17+
}
18+
19+
20+
int main() {
21+
kernel_single_task<class fake_kernel>([]() { test(); });
22+
return 0;
23+
}

0 commit comments

Comments
 (0)