-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang][OpenMP][SPIR-V] Fix addrspace of global constants #134399
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5384,6 +5384,11 @@ LangAS CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D) { | |
| LangAS AS; | ||
| if (OpenMPRuntime->hasAllocateAttributeForGlobalVar(D, AS)) | ||
| return AS; | ||
| if (LangOpts.OpenMPIsTargetDevice && getTriple().isSPIRV()) | ||
| // SPIR-V globals should map to CrossWorkGroup instead of default | ||
| // AS, as generic/no address space is invalid. This is similar | ||
| // to what is done for HIPSPV. | ||
| return LangAS::opencl_global; | ||
| } | ||
| return getTargetCodeGenInfo().getGlobalVarAddressSpace(*this, D); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't this logic fit in here? |
||
| } | ||
|
|
@@ -5402,6 +5407,10 @@ LangAS CodeGenModule::GetGlobalConstantAddressSpace() const { | |
| // UniformConstant storage class is not viable as pointers to it may not be | ||
| // casted to Generic pointers which are used to model HIP's "flat" pointers. | ||
| return LangAS::cuda_device; | ||
| if (LangOpts.OpenMPIsTargetDevice && getTriple().isSPIRV()) | ||
| // OpenMP SPIR-V global constants should map to UniformConstant, different | ||
| // from the HIPSPV case above. | ||
| return LangAS::opencl_constant; | ||
|
||
| if (auto AS = getTarget().getConstantAddressSpace()) | ||
| return *AS; | ||
| return LangAS::Default; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // RUN: %clang_cc1 -fopenmp -x c++ -triple x86_64-unknown-unknown -fopenmp-targets=spirv64 -emit-llvm-bc %s -o %t-host.bc | ||
| // 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 | ||
|
|
||
| extern int printf(char[]); | ||
|
|
||
| #pragma omp declare target | ||
| // CHECK: @global = addrspace(1) global i32 0, align 4 | ||
| // CHECK: @.str = private unnamed_addr addrspace(2) constant [4 x i8] c"foo\00", align 1 | ||
| int global = 0; | ||
| #pragma omp end declare target | ||
| int main() { | ||
| // CHECK: = call i32 @__kmpc_target_init(ptr addrspacecast (ptr addrspace(1) @__omp_offloading_{{.*}}_kernel_environment to ptr), ptr %{{.*}}) | ||
| #pragma omp target | ||
| { | ||
| for(int i = 0; i < 5; i++) | ||
| global++; | ||
| printf("foo"); | ||
| } | ||
| return global; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really OpenMP specific? Sounds like a target info thing to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I am a bit confused as to why this is necessary, DataLayout already encodes that global is AS1. If you're seeing globals end up in generic (I am excluding llvm.used and llvm.compiler.used here, since they are special and should be in generic/0) it might just be a case where CodeGen has a subtle bug. Could you please say a bit more as to what is motivating this change? Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback guys, the SPIR-V address space stuff is a total nightmare so I'll take any feedback I can get.
Here's the problem I'm trying to solve. For the code in the test I have:
Currently we get this IR
Clearly the address space of both is wrong,
addrspace(0)is not valid in SPIR-V for globals.I think doing it in the target itself is much better, let me update the PR doing that, thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels like a spot where we are missing something in Clang - the string should've at least been AS1; some time ago I had a pop at fixing a bunch of places in CodeGen where we just used 0 / unqual rather than getting the GlobalVar AS or the Constant AS, but issues clearly remain - I think we should try to address this in Clang. Are you seeing the above with
spirv64-unknown-unknown?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I see the problem with the string even with pure
spirv64-unknown-unknown. Repro:Here's where we get the addrspace from:
In
GetGlobalConstantAddressSpace, we doand since there's no override for SPIR-V we just get the default:
If you see something wrong in this callstack let me know, I'm happy to fix it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems ok. I would suggest that we consider, in the override, returning
opencl_constant(2) only for OCL, and otherwise returning the global var AS (1), to prevent crashing into the invalid cast problem.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, let me try that. Probably
AS1will be fine for my use case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW codegen already does the right thing because of this code in GetGlobalConstantAddressSpace, and
CodeGenOpenCL/str_literals.clalready locks it down, so the OpenCL part of my change is basically NFC.