Skip to content

[OpenMP] Prevent AMDGPU from overriding visibility on DT_nohost variables #68264

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

Merged
merged 1 commit into from
Oct 5, 2023
Merged
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
13 changes: 13 additions & 0 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,19 @@ void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
}
if (!D)
return;

// OpenMP declare target variables must be visible to the host so they can
// be registered. We require protected visibility unless the variable has
// the DT_nohost modifier and does not need to be registered.
if (Context.getLangOpts().OpenMP &&
Context.getLangOpts().OpenMPIsTargetDevice && isa<VarDecl>(D) &&
D->hasAttr<OMPDeclareTargetDeclAttr>() &&
D->getAttr<OMPDeclareTargetDeclAttr>()->getDevType() !=
OMPDeclareTargetDeclAttr::DT_NoHost) {
GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
return;
}

// Set visibility for definitions, and for declarations if requested globally
// or set explicitly.
LinkageInfo LV = D->getLinkageAndVisibility();
Expand Down
13 changes: 7 additions & 6 deletions clang/lib/CodeGen/Targets/AMDGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,13 @@ static bool requiresAMDGPUProtectedVisibility(const Decl *D,
if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility)
return false;

return D->hasAttr<OpenCLKernelAttr>() ||
(isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) ||
(isa<VarDecl>(D) &&
(D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinSurfaceType() ||
cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinTextureType()));
return !D->hasAttr<OMPDeclareTargetDeclAttr>() &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a spurious whitespace change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not here, above there's an extra line. I'll fix that before I land, not worth force pushing.

(D->hasAttr<OpenCLKernelAttr>() ||
(isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) ||
(isa<VarDecl>(D) &&
(D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinSurfaceType() ||
cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinTextureType())));
}

void AMDGPUTargetCodeGenInfo::setFunctionDeclAttributes(
Expand Down
2 changes: 1 addition & 1 deletion clang/test/OpenMP/declare_target_codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
// CHECK-DAG: @dy = {{protected | }}global i32 0,
// CHECK-DAG: @bbb = {{protected | }}global i32 0,
// CHECK-DAG: weak constant %struct.__tgt_offload_entry { ptr @bbb,
// CHECK-DAG: @ccc = external global i32,
// CHECK-DAG: @ccc = external {{protected | }}global i32,
// CHECK-DAG: @ddd = {{protected | }}global i32 0,
// CHECK-DAG: @hhh_decl_tgt_ref_ptr = weak global ptr null
// CHECK-DAG: @ggg_decl_tgt_ref_ptr = weak global ptr null
Expand Down
2 changes: 1 addition & 1 deletion clang/test/OpenMP/declare_target_constexpr_codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class A {
public:
static constexpr double pi = 3.141592653589793116;
//.
// CHECK: @_ZN1A2piE = linkonce_odr constant double 0x400921FB54442D18, comdat, align 8
// CHECK: @_ZN1A2piE = linkonce_odr protected constant double 0x400921FB54442D18, comdat, align 8
// CHECK: @_ZL9anotherPi = internal constant double 3.140000e+00, align 8
// CHECK: @llvm.compiler.used = appending global [2 x ptr] [ptr @"__ZN1A2piE$ref", ptr @"__ZL9anotherPi$ref"], section "llvm.metadata"
//.
Expand Down
10 changes: 9 additions & 1 deletion clang/test/OpenMP/target_visibility.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %clang_cc1 -debug-info-kind=limited -verify -fopenmp -x c++ -triple nvptx64-unknown-unknown -fopenmp-targets=nvptx64-nvidia-cuda -emit-llvm %s -fopenmp-is-target-device -o - | FileCheck %s
// RUN: %clang_cc1 -debug-info-kind=limited -verify -fopenmp -x c++ -triple nvptx-unknown-unknown -fopenmp-targets=nvptx-nvidia-cuda -emit-llvm %s -fopenmp-is-target-device -o - | FileCheck %s
// RUN: %clang_cc1 -debug-info-kind=limited -verify -fopenmp -x c++ -triple amdgcn-amd-amdhsa -fopenmp-targets=amdgcn-amd-amdhsa -emit-llvm %s -fopenmp-is-target-device -o - | FileCheck %s
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it is safer to have tests for both amdgcn and nvptx since they behave differently

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added --check-prefixes=CHECK,AMDGPU for the differences. Figured it's not worth making a new test just for this.

// expected-no-diagnostics


Expand All @@ -21,6 +21,14 @@ void B::bar() { A a; a.foo(); }
void B::sbar() { A::sfoo(); }
#pragma omp declare target to(B::bar, B::sbar)

[[gnu::visibility("hidden")]] extern const int x = 0;
#pragma omp declare target to(x) device_type(nohost)

[[gnu::visibility("hidden")]] int y = 0;
#pragma omp declare target to(y)

// CHECK-DAG: @x = hidden{{.*}} constant i32 0
// CHECK-DAG: @y = protected{{.*}} i32 0
// CHECK-DAG: define hidden void @_ZN1B4sbarEv()
// CHECK-DAG: define linkonce_odr hidden void @_ZN1A4sfooEv()
// CHECK-DAG: define hidden void @_ZN1B3barEv(
Expand Down