Skip to content

Commit 5010ba6

Browse files
committed
[OpenMP] Prevent AMDGPU from overriding visibility on DT_nohost variables
Summary: There's some logic in the AMDGPU target that manually resets the requested visibility of certain variables. This was triggering when we set a constant variable in OpenMP. However, we shouldn't do this for OpenMP when the variable has the `nohost` type. That implies that the variable is not visible to the host and therefore does not need to be visible, so we should respect the original value of it.
1 parent 0ca7e60 commit 5010ba6

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,19 @@ void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
13901390
}
13911391
if (!D)
13921392
return;
1393+
1394+
// OpenMP declare target variables must be visible to the host so they can
1395+
// be registered. We require protected visibility unless the variable has
1396+
// the DT_nohost modifier and does not need to be registered.
1397+
if (Context.getLangOpts().OpenMP &&
1398+
Context.getLangOpts().OpenMPIsTargetDevice && isa<VarDecl>(D) &&
1399+
D->hasAttr<OMPDeclareTargetDeclAttr>() &&
1400+
D->getAttr<OMPDeclareTargetDeclAttr>()->getDevType() !=
1401+
OMPDeclareTargetDeclAttr::DT_NoHost) {
1402+
GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
1403+
return;
1404+
}
1405+
13931406
// Set visibility for definitions, and for declarations if requested globally
13941407
// or set explicitly.
13951408
LinkageInfo LV = D->getLinkageAndVisibility();

clang/lib/CodeGen/Targets/AMDGPU.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,13 @@ static bool requiresAMDGPUProtectedVisibility(const Decl *D,
308308
if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility)
309309
return false;
310310

311-
return D->hasAttr<OpenCLKernelAttr>() ||
312-
(isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) ||
313-
(isa<VarDecl>(D) &&
314-
(D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
315-
cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinSurfaceType() ||
316-
cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinTextureType()));
311+
return !D->hasAttr<OMPDeclareTargetDeclAttr>() &&
312+
(D->hasAttr<OpenCLKernelAttr>() ||
313+
(isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) ||
314+
(isa<VarDecl>(D) &&
315+
(D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
316+
cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinSurfaceType() ||
317+
cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinTextureType())));
317318
}
318319

319320
void AMDGPUTargetCodeGenInfo::setFunctionDeclAttributes(

clang/test/OpenMP/declare_target_codegen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
// CHECK-DAG: @dy = {{protected | }}global i32 0,
3232
// CHECK-DAG: @bbb = {{protected | }}global i32 0,
3333
// CHECK-DAG: weak constant %struct.__tgt_offload_entry { ptr @bbb,
34-
// CHECK-DAG: @ccc = external global i32,
34+
// CHECK-DAG: @ccc = external {{protected | }}global i32,
3535
// CHECK-DAG: @ddd = {{protected | }}global i32 0,
3636
// CHECK-DAG: @hhh_decl_tgt_ref_ptr = weak global ptr null
3737
// CHECK-DAG: @ggg_decl_tgt_ref_ptr = weak global ptr null

clang/test/OpenMP/declare_target_constexpr_codegen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class A {
1616
public:
1717
static constexpr double pi = 3.141592653589793116;
1818
//.
19-
// CHECK: @_ZN1A2piE = linkonce_odr constant double 0x400921FB54442D18, comdat, align 8
19+
// CHECK: @_ZN1A2piE = linkonce_odr protected constant double 0x400921FB54442D18, comdat, align 8
2020
// CHECK: @_ZL9anotherPi = internal constant double 3.140000e+00, align 8
2121
// CHECK: @llvm.compiler.used = appending global [2 x ptr] [ptr @"__ZN1A2piE$ref", ptr @"__ZL9anotherPi$ref"], section "llvm.metadata"
2222
//.

clang/test/OpenMP/target_visibility.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// 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
2-
// 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
2+
// 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
33
// expected-no-diagnostics
44

55

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

24+
[[gnu::visibility("hidden")]] extern const int x = 0;
25+
#pragma omp declare target to(x) device_type(nohost)
26+
27+
[[gnu::visibility("hidden")]] int y = 0;
28+
#pragma omp declare target to(y)
29+
30+
// CHECK-DAG: @x = hidden{{.*}} constant i32 0
31+
// CHECK-DAG: @y = protected{{.*}} i32 0
2432
// CHECK-DAG: define hidden void @_ZN1B4sbarEv()
2533
// CHECK-DAG: define linkonce_odr hidden void @_ZN1A4sfooEv()
2634
// CHECK-DAG: define hidden void @_ZN1B3barEv(

0 commit comments

Comments
 (0)