Skip to content

Commit dc94bb7

Browse files
committed
[HIP] fix host-used external kernel
In -fgpu-rdc mode, when an external kernel is used by a host function with weak_odr linkage (e.g. explicitly instantiated template function), the kernel should not be marked as host-used external kernel, since the host function may be dropped by the linker. Mark the external kernel as host-used external kernel will force a reference to the external kernel, which the user may not define in other TU. Fixes: #83771
1 parent 4d80df0 commit dc94bb7

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

clang/lib/Sema/SemaCUDA.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,9 @@ bool Sema::CheckCUDACall(SourceLocation Loc, FunctionDecl *Callee) {
895895
if (DiagKind == SemaDiagnosticBuilder::K_Nop) {
896896
// For -fgpu-rdc, keep track of external kernels used by host functions.
897897
if (LangOpts.CUDAIsDevice && LangOpts.GPURelocatableDeviceCode &&
898-
Callee->hasAttr<CUDAGlobalAttr>() && !Callee->isDefined())
898+
Callee->hasAttr<CUDAGlobalAttr>() && !Callee->isDefined() &&
899+
!Caller->getDescribedFunctionTemplate() &&
900+
getASTContext().GetGVALinkageForFunction(Caller) == GVA_StrongExternal)
899901
getASTContext().CUDAExternalDeviceDeclODRUsedByHost.insert(Callee);
900902
return true;
901903
}

clang/lib/Sema/SemaExpr.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19211,7 +19211,10 @@ MarkVarDeclODRUsed(ValueDecl *V, SourceLocation Loc, Sema &SemaRef,
1921119211
} else if (VarTarget == Sema::CVT_Device &&
1921219212
!Var->hasAttr<CUDASharedAttr>() &&
1921319213
(UserTarget == Sema::CFT_Host ||
19214-
UserTarget == Sema::CFT_HostDevice)) {
19214+
UserTarget == Sema::CFT_HostDevice) &&
19215+
!FD->getDescribedFunctionTemplate() &&
19216+
SemaRef.getASTContext().GetGVALinkageForFunction(FD) ==
19217+
GVA_StrongExternal) {
1921519218
// Record a CUDA/HIP device side variable if it is ODR-used
1921619219
// by host code. This is done conservatively, when the variable is
1921719220
// referenced in any of the following contexts:

clang/test/CodeGenCUDA/host-used-extern.cu

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
// NEG-NOT: @__clang_gpu_used_external = {{.*}} @_Z7kernel2v
2626
// NEG-NOT: @__clang_gpu_used_external = {{.*}} @_Z7kernel3v
27+
// NEG-NOT: @__clang_gpu_used_external = {{.*}} @_Z7kernel5v
2728
// NEG-NOT: @__clang_gpu_used_external = {{.*}} @var2
2829
// NEG-NOT: @__clang_gpu_used_external = {{.*}} @var3
2930
// NEG-NOT: @__clang_gpu_used_external = {{.*}} @ext_shvar
@@ -44,6 +45,10 @@ __global__ void kernel3();
4445
// kernel4 is marked as used even though it is not called.
4546
__global__ void kernel4();
4647

48+
// kernel5 is not marked as used since it is called by host function
49+
// with linkonce_odr linkage, which may be dropped by linker.
50+
__global__ void kernel5();
51+
4752
extern __device__ int var1;
4853

4954
__device__ int var2;
@@ -67,3 +72,11 @@ __global__ void test_lambda_using_extern_shared() {
6772
};
6873
lambda();
6974
}
75+
76+
template<class T>
77+
void template_caller() {
78+
kernel5<<<1, 1>>>();
79+
var1 = 1;
80+
}
81+
82+
template void template_caller<int>();

0 commit comments

Comments
 (0)