Skip to content

Commit 0045bfc

Browse files
authored
[CIR} Add support for static member variable instantiation (#153200)
This patch handles both implicit and explicit template instantiations of template class static member variables.
1 parent e9b4e68 commit 0045bfc

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,20 @@ void CIRGenModule::emitGlobalFunctionDefinition(clang::GlobalDecl gd,
438438
errorNYI(funcDecl->getSourceRange(), "deferredAnnotations");
439439
}
440440

441+
void CIRGenModule::handleCXXStaticMemberVarInstantiation(VarDecl *vd) {
442+
VarDecl::DefinitionKind dk = vd->isThisDeclarationADefinition();
443+
if (dk == VarDecl::Definition && vd->hasAttr<DLLImportAttr>())
444+
return;
445+
446+
TemplateSpecializationKind tsk = vd->getTemplateSpecializationKind();
447+
// If we have a definition, this might be a deferred decl. If the
448+
// instantiation is explicit, make sure we emit it at the end.
449+
if (vd->getDefinition() && tsk == TSK_ExplicitInstantiationDefinition)
450+
getAddrOfGlobalVar(vd);
451+
452+
emitTopLevelDecl(vd);
453+
}
454+
441455
mlir::Operation *CIRGenModule::getGlobalValue(StringRef name) {
442456
return mlir::SymbolTable::lookupSymbolIn(theModule, name);
443457
}

clang/lib/CIR/CodeGen/CIRGenModule.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ class CIRGenModule : public CIRGenTypeCache {
120120

121121
mlir::Operation *lastGlobalOp = nullptr;
122122

123+
/// Tell the consumer that this variable has been instantiated.
124+
void handleCXXStaticMemberVarInstantiation(VarDecl *vd);
125+
123126
llvm::DenseMap<const Decl *, cir::GlobalOp> staticLocalDeclMap;
124127

125128
mlir::Operation *getGlobalValue(llvm::StringRef ref);

clang/lib/CIR/CodeGen/CIRGenerator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ void CIRGenerator::HandleCXXStaticMemberVarInstantiation(VarDecl *D) {
163163
if (diags.hasErrorOccurred())
164164
return;
165165

166-
cgm->errorNYI(D->getSourceRange(), "HandleCXXStaticMemberVarInstantiation");
166+
cgm->handleCXXStaticMemberVarInstantiation(D);
167167
}
168168

169169
void CIRGenerator::CompleteTentativeDefinition(VarDecl *d) {

clang/test/CIR/CodeGen/static-vars.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,37 @@
22
// RUN: FileCheck --input-file=%t.cir %s
33
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t1.ll
44
// RUN: FileCheck --check-prefix=LLVM --input-file=%t1.ll %s
5+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t1.ll
6+
// RUN: FileCheck --check-prefix=OGCG --input-file=%t1.ll %s
7+
8+
template<typename T>
9+
struct implicitly_instantiated {
10+
static T member;
11+
};
12+
13+
template<typename T>
14+
T implicitly_instantiated<T>::member = 12345;
15+
16+
int use_implicitly_instantiated() {
17+
return implicitly_instantiated<int>::member;
18+
}
19+
20+
// CHECK-DAG: cir.global linkonce_odr comdat @_ZN23implicitly_instantiatedIiE6memberE = #cir.int<12345> : !s32i
21+
// LLVM-DAG: @_ZN23implicitly_instantiatedIiE6memberE = linkonce_odr global i32 12345, comdat
22+
// OGCG-DAG: @_ZN23implicitly_instantiatedIiE6memberE = linkonce_odr global i32 12345, comdat
23+
24+
template<typename T>
25+
struct explicitly_instantiated {
26+
static T member;
27+
};
28+
29+
template<typename T>
30+
T explicitly_instantiated<T>::member = 54321;
31+
32+
template int explicitly_instantiated<int>::member;
33+
// CHECK-DAG: cir.global weak_odr comdat @_ZN23explicitly_instantiatedIiE6memberE = #cir.int<54321> : !s32i
34+
// LLVM-DAG: @_ZN23explicitly_instantiatedIiE6memberE = weak_odr global i32 54321, comdat
35+
// OGCG-DAG: @_ZN23explicitly_instantiatedIiE6memberE = weak_odr global i32 54321, comdat
536

637
void func1(void) {
738
// Should lower default-initialized static vars.
@@ -42,6 +73,8 @@ void func2(void) {
4273

4374
// LLVM-DAG: $_ZZ4testvE1c = comdat any
4475
// LLVM-DAG: @_ZZ4testvE1c = linkonce_odr global i32 0, comdat, align 4
76+
// OGCG-DAG: $_ZZ4testvE1c = comdat any
77+
// OGCG-DAG: @_ZZ4testvE1c = linkonce_odr global i32 0, comdat, align 4
4578

4679
inline void test() { static int c; }
4780
// CHECK-LABEL: @_Z4testv

0 commit comments

Comments
 (0)