Skip to content

Commit b4d5cbe

Browse files
committed
[CIR] Add handling for static data members
This adds some trivial handling to force emitting of child decls inside C++ records.
1 parent 8359513 commit b4d5cbe

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ struct MissingFeatures {
239239
static bool ctorConstLvalueToRvalueConversion() { return false; }
240240
static bool ctorMemcpyizer() { return false; }
241241
static bool cudaSupport() { return false; }
242-
static bool cxxRecordStaticMembers() { return false; }
243242
static bool dataLayoutTypeIsSized() { return false; }
244243
static bool dataLayoutTypeAllocSize() { return false; }
245244
static bool dataLayoutTypeStoreSize() { return false; }

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,10 +1556,14 @@ void CIRGenModule::emitTopLevelDecl(Decl *decl) {
15561556
break;
15571557

15581558
case Decl::ClassTemplateSpecialization:
1559-
case Decl::CXXRecord:
1559+
case Decl::CXXRecord: {
1560+
CXXRecordDecl *crd = cast<CXXRecordDecl>(decl);
15601561
assert(!cir::MissingFeatures::generateDebugInfo());
1561-
assert(!cir::MissingFeatures::cxxRecordStaticMembers());
1562+
for (auto *childDecl : crd->decls())
1563+
if (isa<VarDecl>(childDecl) || isa<CXXRecordDecl>(childDecl))
1564+
emitTopLevelDecl(childDecl);
15621565
break;
1566+
}
15631567

15641568
case Decl::FileScopeAsm:
15651569
// File-scope asm is ignored during device-side CUDA compilation.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck %s -check-prefix=CIR --input-file=%t.cir
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
4+
// RUN: FileCheck %s -check-prefix=LLVM --input-file=%t-cir.ll
5+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
6+
// RUN: FileCheck %s -check-prefix=OGCG --input-file=%t.ll
7+
8+
struct HasDtor {
9+
~HasDtor();
10+
};
11+
struct S {
12+
static inline HasDtor hd;
13+
};
14+
15+
// CIR: cir.global linkonce_odr comdat @_ZN1S2hdE = #cir.zero : !rec_HasDtor
16+
17+
// CIR: cir.func internal private @__cxx_global_var_init() {
18+
// CIR: %[[HD:.*]] = cir.get_global @_ZN1S2hdE : !cir.ptr<!rec_HasDtor>
19+
// CIR: %[[DTOR:.*]] = cir.get_global @_ZN7HasDtorD1Ev : !cir.ptr<!cir.func<(!cir.ptr<!rec_HasDtor>)>>
20+
// CIR: %[[DTOR_CAST:.*]] = cir.cast bitcast %[[DTOR]] : !cir.ptr<!cir.func<(!cir.ptr<!rec_HasDtor>)>> -> !cir.ptr<!cir.func<(!cir.ptr<!void>)>>
21+
// CIR: %[[HD_CAST:.*]] = cir.cast bitcast %[[HD]] : !cir.ptr<!rec_HasDtor> -> !cir.ptr<!void>
22+
// CIR: %[[HANDLE:.*]] = cir.get_global @__dso_handle : !cir.ptr<i8>
23+
// CIR: cir.call @__cxa_atexit(%[[DTOR_CAST]], %[[HD_CAST]], %[[HANDLE]])
24+
25+
// CIR: cir.func private @_GLOBAL__sub_I_static_members.cpp()
26+
// CIR: cir.call @__cxx_global_var_init()
27+
28+
// LLVM: @_ZN1S2hdE = linkonce_odr global %struct.HasDtor zeroinitializer, comdat
29+
// LLVM: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_static_members.cpp, ptr null }]
30+
// LLVM: define internal void @__cxx_global_var_init()
31+
// LLVM: call void @__cxa_atexit(ptr @_ZN7HasDtorD1Ev, ptr @_ZN1S2hdE, ptr @__dso_handle)
32+
// LLVM: define void @_GLOBAL__sub_I_static_members.cpp()
33+
// LLVM: call void @__cxx_global_var_init()
34+
35+
// FIXME(cir): OGCG has a guard variable for this case that we don't generate in CIR.
36+
// This is needed because the variable linkonce_odr linkage.
37+
38+
// OGCG: @_ZN1S2hdE = linkonce_odr global %struct.HasDtor zeroinitializer, comdat
39+
// OGCG: @_ZGVN1S2hdE = linkonce_odr global i64 0, comdat($_ZN1S2hdE)
40+
// OGCG: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init, ptr @_ZN1S2hdE }]
41+
// OGCG: define internal void @__cxx_global_var_init() {{.*}} section ".text.startup" comdat($_ZN1S2hdE) {
42+
// OGCG: %[[GUARD:.*]] = load atomic i8, ptr @_ZGVN1S2hdE acquire
43+
// OGCG: %[[UNINIT:.*]] = icmp eq i8 %[[GUARD]], 0
44+
// OGCG: br i1 %[[UNINIT]], label %[[INIT_CHECK:.*]], label %[[INIT_END:.*]]
45+
// OGCG: [[INIT_CHECK:.*]]:
46+
// OGCG: %[[GUARD_ACQUIRE:.*]] = call i32 @__cxa_guard_acquire(ptr @_ZGVN1S2hdE)
47+
// OGCG: %[[TOBOOL:.*]] = icmp ne i32 %[[GUARD_ACQUIRE]], 0
48+
// OGCG: br i1 %[[TOBOOL]], label %[[INIT:.*]], label %[[INIT_END]]
49+
// OGCG: [[INIT:.*]]:
50+
// OGCG: %[[ATEXIT:.*]] = call i32 @__cxa_atexit(ptr @_ZN7HasDtorD1Ev, ptr @_ZN1S2hdE, ptr @__dso_handle)
51+
// OGCG: call void @__cxa_guard_release(ptr @_ZGVN1S2hdE)
52+
// OGCG: br label %[[INIT_END]]
53+
// OGCG: [[INIT_END]]:

0 commit comments

Comments
 (0)