-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[OpenMP][clang][HIP][CUDA] fix weak alias emit on device compilation #164326
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
26b90e0
16c1a68
73f219f
7fd51cc
2949485
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 | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4043,6 +4043,53 @@ template <typename AttrT> static bool hasImplicitAttr(const ValueDecl *D) { | |||||||||||||||||||
| return D->isImplicit(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| static bool shouldSkipAliasEmission(const CodeGenModule &CGM, | ||||||||||||||||||||
| const ValueDecl *Global) { | ||||||||||||||||||||
| const LangOptions &LangOpts = CGM.getLangOpts(); | ||||||||||||||||||||
| if (!(LangOpts.OpenMPIsTargetDevice || LangOpts.CUDA)) | ||||||||||||||||||||
| return false; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const auto *AA = Global->getAttr<AliasAttr>(); | ||||||||||||||||||||
| GlobalDecl AliaseeGD; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Check if the aliasee exists. | ||||||||||||||||||||
| if (!CGM.lookupRepresentativeDecl(AA->getAliasee(), AliaseeGD)) { | ||||||||||||||||||||
| // If the aliasee is not found, skip the alias emission. | ||||||||||||||||||||
| // This is not a hard error as this branch is executed for both the host | ||||||||||||||||||||
| // and device, with no respect to where the aliasee is defined. | ||||||||||||||||||||
| // For some OpenMP cases (functions) this will return true even if the | ||||||||||||||||||||
| // aliasee is not on the device, which is handled by the case below | ||||||||||||||||||||
| return true; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| const auto *AliaseeDecl = dyn_cast<ValueDecl>(AliaseeGD.getDecl()); | ||||||||||||||||||||
| if (LangOpts.OpenMPIsTargetDevice) { | ||||||||||||||||||||
| if (!AliaseeDecl || | ||||||||||||||||||||
| !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(AliaseeDecl)) | ||||||||||||||||||||
| // On OpenMP device, skip alias emission if the aliasee is not marked | ||||||||||||||||||||
| // with declare target. | ||||||||||||||||||||
| return true; | ||||||||||||||||||||
| return false; | ||||||||||||||||||||
|
||||||||||||||||||||
| if (!AliaseeDecl || | |
| !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(AliaseeDecl)) | |
| // On OpenMP device, skip alias emission if the aliasee is not marked | |
| // with declare target. | |
| return true; | |
| return false; | |
| return !AliaseeDecl || !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(AliaseeDecl); |
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.
Done in 2949485
Outdated
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.
| if (LangOpts.CUDAIsDevice) { | |
| if (!HasDeviceAttr || !AliaseeHasDeviceAttr) | |
| // On device, skip alias emission if either the alias or the aliasee | |
| // is not marked with __device__. | |
| return true; | |
| return false; | |
| } | |
| if (LangOpts.CUDAIsDevice) | |
| return !HasDeviceAttr || !AliaseeHasDeviceAttr; |
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.
Done in 2949485
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,24 @@ | ||||
| // NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --check-globals all --version 6 | ||||
| // REQUIRES: nvptx-registered-target | ||||
|
||||
| // REQUIRES: nvptx-registered-target |
This shouldn't be necessary since we're not hitting PTX codegen, here and elsewhere. Also I know these tests are autogenerated, but we don't need to check the attributes. Totally find to trim things from the autogenerated output.
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.
Trimmed up the checks, in 2949485, I kept the function definitions as they technically relate to the issue, but I also see an argument to also removing those.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| // NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --check-globals all --version 6 | ||
| // REQUIRES: amdgpu-registered-target | ||
| // RUN: %clang_cc1 -x hip -triple x86_64-unknown-linux-gnu -aux-triple amdgcn-amd-amdhsa -emit-llvm-bc %s -o %t-host.bc | ||
| // RUN: %clang_cc1 -x hip -triple x86_64-unknown-linux-gnu -aux-triple amdgcn-amd-amdhsa -emit-llvm %s -o - | FileCheck %s --check-prefix=HOST | ||
| // RUN: %clang_cc1 -x hip -triple amdgcn-amd-amdhsa -aux-triple x86_64-unknown-linux-gnu -emit-llvm %s -fcuda-is-device -o - | FileCheck %s --check-prefix=DEVICE | ||
|
|
||
| #define __device__ __attribute__((device)) | ||
| #define __host__ __attribute__((host)) | ||
|
|
||
| extern "C" { | ||
| //. | ||
| // HOST: @__HostVar = global i32 1, align 4 | ||
| // HOST: @__hip_cuid_ = global i8 0 | ||
| // HOST: @llvm.compiler.used = appending global [1 x ptr] [ptr @__hip_cuid_], section "llvm.metadata" | ||
| // HOST: @HostFunc = weak alias i32 (), ptr @__HostFunc | ||
| // HOST: @HostFunc_ = alias i32 (), ptr @__HostFunc | ||
| // HOST: @HostVar = weak alias i32, ptr @__HostVar | ||
| // HOST: @HostVar_ = alias i32, ptr @__HostVar | ||
| // HOST: @Two = weak alias i32 (), ptr @__Two | ||
| // HOST: @Two_ = alias i32 (), ptr @__Two | ||
| // HOST: @_Z5Threev = weak alias i32 (), ptr @_Z7__Threev | ||
| // HOST: @_Z6Three_v = alias i32 (), ptr @_Z7__Threev | ||
| // HOST: @_Z4Fourv = weak alias i32 (), ptr @_Z6__Fourv | ||
| // HOST: @_Z4Fourf = weak alias float (float), ptr @_Z6__Fourf | ||
| //. | ||
| // DEVICE: @__hip_cuid_ = addrspace(1) global i8 0 | ||
| // DEVICE: @llvm.compiler.used = appending addrspace(1) global [1 x ptr] [ptr addrspacecast (ptr addrspace(1) @__hip_cuid_ to ptr)], section "llvm.metadata" | ||
| // DEVICE: @One = weak alias i32 (), ptr @__One | ||
| // DEVICE: @One_ = alias i32 (), ptr @__One | ||
| // DEVICE: @Two = weak alias i32 (), ptr @__Two | ||
| // DEVICE: @Two_ = alias i32 (), ptr @__Two | ||
| // DEVICE: @_Z5Threev = weak alias i32 (), ptr @_Z7__Threev | ||
| // DEVICE: @_Z6Three_v = alias i32 (), ptr @_Z7__Threev | ||
| // DEVICE: @_Z4Fourv = weak alias i32 (), ptr @_Z6__Fourv | ||
| // DEVICE: @_Z4Fourf = weak alias float (float), ptr @_Z6__Fourf | ||
| //. | ||
| // HOST-LABEL: define dso_local i32 @__HostFunc( | ||
| // HOST-SAME: ) #[[ATTR0:[0-9]+]] { | ||
| // HOST-NEXT: [[ENTRY:.*:]] | ||
| // HOST-NEXT: ret i32 42 | ||
| // | ||
| int __HostFunc(void) { return 42; } | ||
| int __HostVar = 1; | ||
| int HostFunc(void) __attribute__((weak, alias("__HostFunc"))); | ||
| int HostFunc_(void) __attribute__((alias("__HostFunc"))); | ||
| extern int __attribute__((weak, alias("__HostVar"))) HostVar; | ||
| extern int __attribute__((alias("__HostVar"))) HostVar_; | ||
|
|
||
| // DEVICE-LABEL: define dso_local i32 @__One( | ||
| // DEVICE-SAME: ) #[[ATTR0:[0-9]+]] { | ||
| // DEVICE-NEXT: [[ENTRY:.*:]] | ||
| // DEVICE-NEXT: [[RETVAL:%.*]] = alloca i32, align 4, addrspace(5) | ||
| // DEVICE-NEXT: [[RETVAL_ASCAST:%.*]] = addrspacecast ptr addrspace(5) [[RETVAL]] to ptr | ||
| // DEVICE-NEXT: ret i32 1 | ||
| // | ||
| __device__ int __One(void) { return 1; } | ||
| __device__ int One(void) __attribute__((weak, alias("__One"))); | ||
| __device__ int One_(void) __attribute__((alias("__One"))); | ||
|
|
||
|
Collaborator
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. can we add lit tests for:
Author
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. I added the cases for HIP and amdgcn for OpenMP in 16c1a68, The cases work without modification of the patch. |
||
| // HOST-LABEL: define dso_local i32 @__Two( | ||
| // HOST-SAME: ) #[[ATTR0]] { | ||
| // HOST-NEXT: [[ENTRY:.*:]] | ||
| // HOST-NEXT: ret i32 2 | ||
| // | ||
| // DEVICE-LABEL: define dso_local i32 @__Two( | ||
| // DEVICE-SAME: ) #[[ATTR0]] { | ||
| // DEVICE-NEXT: [[ENTRY:.*:]] | ||
| // DEVICE-NEXT: [[RETVAL:%.*]] = alloca i32, align 4, addrspace(5) | ||
| // DEVICE-NEXT: [[RETVAL_ASCAST:%.*]] = addrspacecast ptr addrspace(5) [[RETVAL]] to ptr | ||
| // DEVICE-NEXT: ret i32 2 | ||
| // | ||
| __host__ __device__ int __Two(void) { return 2; } | ||
| __host__ __device__ int Two(void) __attribute__((weak, alias("__Two"))); | ||
| __host__ __device__ int Two_(void) __attribute__((alias("__Two"))); | ||
| } | ||
|
|
||
| // HOST-LABEL: define linkonce_odr noundef i32 @_Z7__Threev( | ||
| // HOST-SAME: ) #[[ATTR0]] comdat { | ||
| // HOST-NEXT: [[ENTRY:.*:]] | ||
| // HOST-NEXT: ret i32 5 | ||
| // | ||
| // DEVICE-LABEL: define linkonce_odr noundef i32 @_Z7__Threev( | ||
| // DEVICE-SAME: ) #[[ATTR0]] comdat { | ||
| // DEVICE-NEXT: [[ENTRY:.*:]] | ||
| // DEVICE-NEXT: [[RETVAL:%.*]] = alloca i32, align 4, addrspace(5) | ||
| // DEVICE-NEXT: [[RETVAL_ASCAST:%.*]] = addrspacecast ptr addrspace(5) [[RETVAL]] to ptr | ||
| // DEVICE-NEXT: ret i32 5 | ||
| // | ||
| __host__ __device__ constexpr int __Three(void) { return 5; } | ||
| __host__ __device__ int Three(void) __attribute__((weak, alias("_Z7__Threev"))); | ||
| __host__ __device__ int Three_(void) __attribute__((alias("_Z7__Threev"))); | ||
|
|
||
|
|
||
| // HOST-LABEL: define dso_local noundef i32 @_Z6__Fourv( | ||
| // HOST-SAME: ) #[[ATTR0]] { | ||
| // HOST-NEXT: [[ENTRY:.*:]] | ||
| // HOST-NEXT: ret i32 2 | ||
| // | ||
| // DEVICE-LABEL: define dso_local noundef i32 @_Z6__Fourv( | ||
| // DEVICE-SAME: ) #[[ATTR0]] { | ||
| // DEVICE-NEXT: [[ENTRY:.*:]] | ||
| // DEVICE-NEXT: [[RETVAL:%.*]] = alloca i32, align 4, addrspace(5) | ||
| // DEVICE-NEXT: [[RETVAL_ASCAST:%.*]] = addrspacecast ptr addrspace(5) [[RETVAL]] to ptr | ||
| // DEVICE-NEXT: ret i32 2 | ||
| // | ||
| __host__ __device__ int __Four(void) { return 2; } | ||
| // HOST-LABEL: define dso_local noundef float @_Z6__Fourf( | ||
| // HOST-SAME: float noundef [[F:%.*]]) #[[ATTR0]] { | ||
| // HOST-NEXT: [[ENTRY:.*:]] | ||
| // HOST-NEXT: [[F_ADDR:%.*]] = alloca float, align 4 | ||
| // HOST-NEXT: store float [[F]], ptr [[F_ADDR]], align 4 | ||
| // HOST-NEXT: [[TMP0:%.*]] = load float, ptr [[F_ADDR]], align 4 | ||
| // HOST-NEXT: [[MUL:%.*]] = fmul contract float 2.000000e+00, [[TMP0]] | ||
| // HOST-NEXT: ret float [[MUL]] | ||
| // | ||
| // DEVICE-LABEL: define dso_local noundef float @_Z6__Fourf( | ||
| // DEVICE-SAME: float noundef [[F:%.*]]) #[[ATTR0]] { | ||
| // DEVICE-NEXT: [[ENTRY:.*:]] | ||
| // DEVICE-NEXT: [[RETVAL:%.*]] = alloca float, align 4, addrspace(5) | ||
| // DEVICE-NEXT: [[F_ADDR:%.*]] = alloca float, align 4, addrspace(5) | ||
| // DEVICE-NEXT: [[RETVAL_ASCAST:%.*]] = addrspacecast ptr addrspace(5) [[RETVAL]] to ptr | ||
| // DEVICE-NEXT: [[F_ADDR_ASCAST:%.*]] = addrspacecast ptr addrspace(5) [[F_ADDR]] to ptr | ||
| // DEVICE-NEXT: store float [[F]], ptr [[F_ADDR_ASCAST]], align 4 | ||
| // DEVICE-NEXT: [[TMP0:%.*]] = load float, ptr [[F_ADDR_ASCAST]], align 4 | ||
| // DEVICE-NEXT: [[MUL:%.*]] = fmul contract float 2.000000e+00, [[TMP0]] | ||
| // DEVICE-NEXT: ret float [[MUL]] | ||
| // | ||
| __host__ __device__ float __Four(float f) { return 2.0f * f; } | ||
| __host__ __device__ int Four(void) __attribute__((weak, alias("_Z6__Fourv"))); | ||
| __host__ __device__ float Four(float f) __attribute__((weak, alias("_Z6__Fourf"))); | ||
|
|
||
| //. | ||
| // HOST: attributes #[[ATTR0]] = { mustprogress noinline nounwind optnone "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-features"="+cx8,+mmx,+sse,+sse2,+x87" } | ||
| //. | ||
| // DEVICE: attributes #[[ATTR0]] = { convergent mustprogress noinline nounwind optnone "no-trapping-math"="true" "stack-protector-buffer-size"="8" } | ||
| //. | ||
| // HOST: [[META0:![0-9]+]] = !{i32 1, !"wchar_size", i32 4} | ||
| // HOST: [[META1:![0-9]+]] = !{!"{{.*}}clang version {{.*}}"} | ||
| //. | ||
| // DEVICE: [[META0:![0-9]+]] = !{i32 1, !"amdhsa_code_object_version", i32 600} | ||
| // DEVICE: [[META1:![0-9]+]] = !{i32 1, !"amdgpu_printf_kind", !"hostcall"} | ||
| // DEVICE: [[META2:![0-9]+]] = !{i32 1, !"wchar_size", i32 4} | ||
| // DEVICE: [[META3:![0-9]+]] = !{!"{{.*}}clang version {{.*}}"} | ||
| //. | ||
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.
Clearer.
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.
Done in 2949485