Skip to content

Commit 1eb8258

Browse files
committed
[clang][OpenMP][SPIR-V] Fix addrspace of globals and global constants
Signed-off-by: Sarnie, Nick <[email protected]>
1 parent 5fbd065 commit 1eb8258

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5384,6 +5384,11 @@ LangAS CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D) {
53845384
LangAS AS;
53855385
if (OpenMPRuntime->hasAllocateAttributeForGlobalVar(D, AS))
53865386
return AS;
5387+
if (LangOpts.OpenMPIsTargetDevice && getTriple().isSPIRV())
5388+
// SPIR-V globals should map to CrossWorkGroup instead of default
5389+
// AS, as generic/no address space is invalid. This is similar
5390+
// to what is done for HIPSPV.
5391+
return LangAS::opencl_global;
53875392
}
53885393
return getTargetCodeGenInfo().getGlobalVarAddressSpace(*this, D);
53895394
}
@@ -5402,6 +5407,10 @@ LangAS CodeGenModule::GetGlobalConstantAddressSpace() const {
54025407
// UniformConstant storage class is not viable as pointers to it may not be
54035408
// casted to Generic pointers which are used to model HIP's "flat" pointers.
54045409
return LangAS::cuda_device;
5410+
if (LangOpts.OpenMPIsTargetDevice && getTriple().isSPIRV())
5411+
// OpenMP SPIR-V global constants should map to UniformConstant, different
5412+
// from the HIPSPV case above.
5413+
return LangAS::opencl_constant;
54055414
if (auto AS = getTarget().getConstantAddressSpace())
54065415
return *AS;
54075416
return LangAS::Default;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %clang_cc1 -fopenmp -x c++ -triple x86_64-unknown-unknown -fopenmp-targets=spirv64 -emit-llvm-bc %s -o %t-host.bc
2+
// RUN: %clang_cc1 -O0 -fopenmp -fopenmp-targets=spirv64 -fopenmp-is-target-device -triple spirv64 -fopenmp-host-ir-file-path %t-host.bc -emit-llvm %s -o - | FileCheck %s
3+
4+
extern int printf(char[]);
5+
6+
#pragma omp declare target
7+
// CHECK: @global = addrspace(1) global i32 0, align 4
8+
// CHECK: @.str = private unnamed_addr addrspace(2) constant [4 x i8] c"foo\00", align 1
9+
int global = 0;
10+
#pragma omp end declare target
11+
int main() {
12+
// CHECK: = call i32 @__kmpc_target_init(ptr addrspacecast (ptr addrspace(1) @__omp_offloading_{{.*}}_kernel_environment to ptr), ptr %{{.*}})
13+
#pragma omp target
14+
{
15+
for(int i = 0; i < 5; i++)
16+
global++;
17+
printf("foo");
18+
}
19+
20+
21+
return global;
22+
}

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6295,6 +6295,12 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createTargetInit(
62956295
: ConstantExpr::getAddrSpaceCast(KernelEnvironmentGV,
62966296
KernelEnvironmentPtr);
62976297
Value *KernelLaunchEnvironment = DebugKernelWrapper->getArg(0);
6298+
Type *KernelLaunchEnvParamTy = Fn->getFunctionType()->getParamType(1);
6299+
KernelLaunchEnvironment =
6300+
KernelLaunchEnvironment->getType() == KernelLaunchEnvParamTy
6301+
? KernelLaunchEnvironment
6302+
: Builder.CreateAddrSpaceCast(KernelLaunchEnvironment,
6303+
KernelLaunchEnvParamTy);
62986304
CallInst *ThreadKind =
62996305
Builder.CreateCall(Fn, {KernelEnvironment, KernelLaunchEnvironment});
63006306

0 commit comments

Comments
 (0)