Skip to content

Commit 260599e

Browse files
authored
[CIR] Implement init priority attribute (#1907)
Fixes #1743.
1 parent 955d33f commit 260599e

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ let cppNamespace = "::cir" in {
2828
/*defaultImplementation=*/ [{
2929
return $_attr.getAst()->template hasAttr<clang::InitPriorityAttr>();
3030
}]
31+
>,
32+
33+
// Requires hasInitPriorityAttr before dereferencing
34+
InterfaceMethod<"", "const clang::InitPriorityAttr*", "getInitPriorityAttr", (ins), [{}],
35+
/*defaultImplementation=*/ [{
36+
return $_attr.getAst()->template getAttr<clang::InitPriorityAttr>();
37+
}]
3138
>
3239
];
3340
}

clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -905,10 +905,9 @@ void LoweringPreparePass::lowerGlobalOp(GlobalOp op) {
905905
ctorRegion.getBlocks().clear();
906906
dtorRegion.getBlocks().clear();
907907

908-
// Add a function call to the variable initialization function.
909-
assert(!hasAttr<clang::InitPriorityAttr>(
910-
mlir::cast<ASTDeclInterface>(*op.getAst())) &&
911-
"custom initialization priority NYI");
908+
auto astDecl = mlir::cast<ASTDeclInterface>(*op.getAst());
909+
if (astDecl.hasInitPriorityAttr())
910+
f.setGlobalCtorPriority(astDecl.getInitPriorityAttr()->getPriority());
912911
dynamicInitializers.push_back(f);
913912
}
914913

@@ -955,8 +954,9 @@ void LoweringPreparePass::buildCXXGlobalInitFunc() {
955954
for (auto &f : dynamicInitializers) {
956955
// TODO: handle globals with a user-specified initialzation priority.
957956
// TODO: handle defaule priority more nicely.
958-
globalCtorList.emplace_back(f.getName(),
959-
cir::DefaultGlobalCtorDtorPriority);
957+
globalCtorList.emplace_back(
958+
f.getName(),
959+
f.getGlobalCtorPriority().value_or(cir::DefaultGlobalCtorDtorPriority));
960960
}
961961

962962
SmallString<256> fnName;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
3+
4+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.cir
5+
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=LLVM
6+
7+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.cir
8+
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=OGCG
9+
10+
// CIR: attributes {cir.global_ctors = [#cir.global_ctor<"__cxx_global_var_init", 101>]
11+
// LLVM: @llvm.global_ctors = appending constant{{.*}}{ i32 101, ptr @__cxx_global_var_init, ptr null }
12+
// OGCG: @llvm.global_ctors = appending global{{.*}}{ i32 101, ptr @_GLOBAL__I_000101, ptr null }
13+
class A {
14+
public:
15+
A(int, int);
16+
} A __attribute((init_priority(101)))(0, 0);
17+
18+
// CIR-LABEL: cir.func internal private @__cxx_global_var_init() global_ctor(101) {
19+
// LLVM-LABEL: define internal void @__cxx_global_var_init() {
20+
// OGCG-LABEL: define internal void @_GLOBAL__I_000101() {{.*}} section ".text.startup" {

0 commit comments

Comments
 (0)