Skip to content

Commit 77fd30f

Browse files
authored
[CUDA][HIP] Fix template static member (#98580)
Should check host/device attributes before emitting static member of template instantiation. Fixes: #98151
1 parent e616fa5 commit 77fd30f

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5933,7 +5933,8 @@ static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
59335933

59345934
void CodeGenModule::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
59355935
auto DK = VD->isThisDeclarationADefinition();
5936-
if (DK == VarDecl::Definition && VD->hasAttr<DLLImportAttr>())
5936+
if ((DK == VarDecl::Definition && VD->hasAttr<DLLImportAttr>()) ||
5937+
(LangOpts.CUDA && !shouldEmitCUDAGlobalVar(VD)))
59375938
return;
59385939

59395940
TemplateSpecializationKind TSK = VD->getTemplateSpecializationKind();
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fcuda-is-device \
2+
// RUN: -emit-llvm -o - -x hip %s | FileCheck -check-prefix=DEV %s
3+
4+
// RUN: %clang_cc1 -triple x86_64-gnu-linux -std=c++11 \
5+
// RUN: -emit-llvm -o - -x hip %s | FileCheck -check-prefix=HOST %s
6+
7+
// Negative tests.
8+
9+
// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fcuda-is-device \
10+
// RUN: -emit-llvm -o - -x hip %s | FileCheck -check-prefix=DEV-NEG %s
11+
12+
#include "Inputs/cuda.h"
13+
14+
template <class T>
15+
class A {
16+
static int h_member;
17+
__device__ static int d_member;
18+
__constant__ static int c_member;
19+
__managed__ static int m_member;
20+
const static int const_member = 0;
21+
};
22+
23+
template <class T>
24+
int A<T>::h_member;
25+
26+
template <class T>
27+
__device__ int A<T>::d_member;
28+
29+
template <class T>
30+
__constant__ int A<T>::c_member;
31+
32+
template <class T>
33+
__managed__ int A<T>::m_member;
34+
35+
template <class T>
36+
const int A<T>::const_member;
37+
38+
template class A<int>;
39+
40+
//DEV-DAG: @_ZN1AIiE8d_memberE = internal addrspace(1) global i32 0, comdat, align 4
41+
//DEV-DAG: @_ZN1AIiE8c_memberE = internal addrspace(4) global i32 0, comdat, align 4
42+
//DEV-DAG: @_ZN1AIiE8m_memberE = internal addrspace(1) externally_initialized global ptr addrspace(1) null
43+
//DEV-DAG: @_ZN1AIiE12const_memberE = internal addrspace(4) constant i32 0, comdat, align 4
44+
//DEV-NEG-NOT: @_ZN1AIiE8h_memberE
45+
46+
//HOST-DAG: @_ZN1AIiE8h_memberE = weak_odr global i32 0, comdat, align 4
47+
//HOST-DAG: @_ZN1AIiE8d_memberE = internal global i32 undef, comdat, align 4
48+
//HOST-DAG: @_ZN1AIiE8c_memberE = internal global i32 undef, comdat, align 4
49+
//HOST-DAG: @_ZN1AIiE8m_memberE = internal externally_initialized global ptr null
50+
//HOST-DAG: @_ZN1AIiE12const_memberE = weak_odr constant i32 0, comdat, align 4

0 commit comments

Comments
 (0)