Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ let cppNamespace = "::cir" in {
/*defaultImplementation=*/ [{
return $_attr.getAst()->template hasAttr<clang::InitPriorityAttr>();
}]
>,

// Requires hasInitPriorityAttr before dereferencing
InterfaceMethod<"", "const clang::InitPriorityAttr*", "getInitPriorityAttr", (ins), [{}],
/*defaultImplementation=*/ [{
return $_attr.getAst()->template getAttr<clang::InitPriorityAttr>();
}]
>
];
}
Expand Down
12 changes: 6 additions & 6 deletions clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -905,10 +905,9 @@ void LoweringPreparePass::lowerGlobalOp(GlobalOp op) {
ctorRegion.getBlocks().clear();
dtorRegion.getBlocks().clear();

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

Expand Down Expand Up @@ -955,8 +954,9 @@ void LoweringPreparePass::buildCXXGlobalInitFunc() {
for (auto &f : dynamicInitializers) {
// TODO: handle globals with a user-specified initialzation priority.
// TODO: handle defaule priority more nicely.
globalCtorList.emplace_back(f.getName(),
cir::DefaultGlobalCtorDtorPriority);
globalCtorList.emplace_back(
f.getName(),
f.getGlobalCtorPriority().value_or(cir::DefaultGlobalCtorDtorPriority));
}

SmallString<256> fnName;
Expand Down
20 changes: 20 additions & 0 deletions clang/test/CIR/CodeGen/init_priority.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR

// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=LLVM

// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s -check-prefix=OGCG

// CIR: attributes {cir.global_ctors = [#cir.global_ctor<"__cxx_global_var_init", 101>]
// LLVM: @llvm.global_ctors = appending constant{{.*}}{ i32 101, ptr @__cxx_global_var_init, ptr null }
// OGCG: @llvm.global_ctors = appending global{{.*}}{ i32 101, ptr @_GLOBAL__I_000101, ptr null }
class A {
public:
A(int, int);
} A __attribute((init_priority(101)))(0, 0);

// CIR-LABEL: cir.func internal private @__cxx_global_var_init() global_ctor(101) {
// LLVM-LABEL: define internal void @__cxx_global_var_init() {
// OGCG-LABEL: define internal void @_GLOBAL__I_000101() {{.*}} section ".text.startup" {
Loading