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
45 changes: 45 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,51 @@ def CIR_VisibilityAttr : CIR_EnumAttr<CIR_VisibilityKind, "visibility"> {
}];
}

//===----------------------------------------------------------------------===//
// GloblCtorAttr
//===----------------------------------------------------------------------===//

class CIR_GlobalCtorDtor<string name, string attrMnemonic>
: CIR_Attr<"Global" # name, "global_" # attrMnemonic> {
let parameters = (ins "mlir::StringAttr":$name, "int":$priority);

let skipDefaultBuilders = 1;
let builders = [
AttrBuilder<(ins
"llvm::StringRef":$name,
CArg<"int", "65535">:$priority), [{
return $_get($_ctxt, mlir::StringAttr::get($_ctxt, name), priority);
}]>,
AttrBuilderWithInferredContext<(ins
"mlir::StringAttr":$name,
CArg<"int", "65535">:$priority), [{
return $_get(name.getContext(), name, priority);
}]>
];

let assemblyFormat = [{
`<` $name `,` $priority `>`
}];

let extraClassDeclaration = [{
bool isDefaultPriority() const {
return getPriority() == getDefaultPriority();
};

static int getDefaultPriority() {
return 65535;
}
}];
}

def CIR_GlobalCtorAttr : CIR_GlobalCtorDtor<"Ctor", "ctor"> {
let summary = "Marks a function as a global constructor";
let description = [{
Marks the function as a global constructor in the module's constructor list.
It will be executed before main() is called.
}];
}

//===----------------------------------------------------------------------===//
// BitfieldInfoAttr
//===----------------------------------------------------------------------===//
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRDialect.td
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def CIR_Dialect : Dialect {
static llvm::StringRef getNoThrowAttrName() { return "nothrow"; }
static llvm::StringRef getSideEffectAttrName() { return "side_effect"; }
static llvm::StringRef getModuleLevelAsmAttrName() { return "cir.module_asm"; }
static llvm::StringRef getGlobalCtorsAttrName() { return "cir.global_ctors"; }

void registerAttributes();
void registerTypes();
Expand Down
1 change: 0 additions & 1 deletion clang/include/clang/CIR/MissingFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ struct MissingFeatures {
static bool opGlobalUsedOrCompilerUsed() { return false; }
static bool opGlobalAnnotations() { return false; }
static bool opGlobalDtorLowering() { return false; }
static bool opGlobalCtorAttr() { return false; }
static bool opGlobalCtorPriority() { return false; }
static bool opGlobalCtorList() { return false; }
static bool setDSOLocal() { return false; }
Expand Down
38 changes: 37 additions & 1 deletion clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> {
/// Build a module init function that calls all the dynamic initializers.
void buildCXXGlobalInitFunc();

/// Materialize global ctor/dtor list
void buildGlobalCtorDtorList();

cir::FuncOp buildRuntimeFunction(
mlir::OpBuilder &builder, llvm::StringRef name, mlir::Location loc,
cir::FuncType type,
Expand All @@ -79,6 +82,9 @@ struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> {
llvm::StringMap<uint32_t> dynamicInitializerNames;
llvm::SmallVector<cir::FuncOp> dynamicInitializers;

/// List of ctors and their priorities to be called before main()
llvm::SmallVector<std::pair<std::string, uint32_t>, 4> globalCtorList;

void setASTContext(clang::ASTContext *c) { astCtx = c; }
};

Expand Down Expand Up @@ -689,11 +695,36 @@ void LoweringPreparePass::lowerGlobalOp(GlobalOp op) {
assert(!cir::MissingFeatures::opGlobalAnnotations());
}

template <typename AttributeTy>
static llvm::SmallVector<mlir::Attribute>
prepareCtorDtorAttrList(mlir::MLIRContext *context,
llvm::ArrayRef<std::pair<std::string, uint32_t>> list) {
llvm::SmallVector<mlir::Attribute> attrs;
for (const auto &[name, priority] : list)
attrs.push_back(AttributeTy::get(context, name, priority));
return attrs;
}

void LoweringPreparePass::buildGlobalCtorDtorList() {
if (!globalCtorList.empty()) {
llvm::SmallVector<mlir::Attribute> globalCtors =
prepareCtorDtorAttrList<cir::GlobalCtorAttr>(&getContext(),
globalCtorList);

mlirModule->setAttr(cir::CIRDialect::getGlobalCtorsAttrName(),
mlir::ArrayAttr::get(&getContext(), globalCtors));
}

assert(!cir::MissingFeatures::opGlobalDtorLowering());
}

void LoweringPreparePass::buildCXXGlobalInitFunc() {
if (dynamicInitializers.empty())
return;

assert(!cir::MissingFeatures::opGlobalCtorList());
// TODO: handle globals with a user-specified initialzation priority.
// TODO: handle default priority more nicely.
assert(!cir::MissingFeatures::opGlobalCtorPriority());

SmallString<256> fnName;
// Include the filename in the symbol name. Including "sub_" matches gcc
Expand Down Expand Up @@ -722,6 +753,10 @@ void LoweringPreparePass::buildCXXGlobalInitFunc() {
builder.setInsertionPointToStart(f.addEntryBlock());
for (cir::FuncOp &f : dynamicInitializers)
builder.createCallOp(f.getLoc(), f, {});
// Add the global init function (not the individual ctor functions) to the
// global ctor list.
globalCtorList.emplace_back(fnName,
cir::GlobalCtorAttr::getDefaultPriority());

cir::ReturnOp::create(builder, f.getLoc());
}
Expand Down Expand Up @@ -852,6 +887,7 @@ void LoweringPreparePass::runOnOperation() {
runOnOp(o);

buildCXXGlobalInitFunc();
buildGlobalCtorDtorList();
}

std::unique_ptr<Pass> mlir::createLoweringPreparePass() {
Expand Down
75 changes: 75 additions & 0 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2413,6 +2413,73 @@ static void prepareTypeConverter(mlir::LLVMTypeConverter &converter,
});
}

static void buildCtorDtorList(
mlir::ModuleOp module, StringRef globalXtorName, StringRef llvmXtorName,
llvm::function_ref<std::pair<StringRef, int>(mlir::Attribute)> createXtor) {
llvm::SmallVector<std::pair<StringRef, int>> globalXtors;
for (const mlir::NamedAttribute namedAttr : module->getAttrs()) {
if (namedAttr.getName() == globalXtorName) {
for (auto attr : mlir::cast<mlir::ArrayAttr>(namedAttr.getValue()))
globalXtors.emplace_back(createXtor(attr));
break;
}
}

if (globalXtors.empty())
return;

mlir::OpBuilder builder(module.getContext());
builder.setInsertionPointToEnd(&module.getBodyRegion().back());

// Create a global array llvm.global_ctors with element type of
// struct { i32, ptr, ptr }
auto ctorPFTy = mlir::LLVM::LLVMPointerType::get(builder.getContext());
llvm::SmallVector<mlir::Type> ctorStructFields;
ctorStructFields.push_back(builder.getI32Type());
ctorStructFields.push_back(ctorPFTy);
ctorStructFields.push_back(ctorPFTy);

auto ctorStructTy = mlir::LLVM::LLVMStructType::getLiteral(
builder.getContext(), ctorStructFields);
auto ctorStructArrayTy =
mlir::LLVM::LLVMArrayType::get(ctorStructTy, globalXtors.size());

mlir::Location loc = module.getLoc();
auto newGlobalOp = mlir::LLVM::GlobalOp::create(
builder, loc, ctorStructArrayTy, /*constant=*/false,
mlir::LLVM::Linkage::Appending, llvmXtorName, mlir::Attribute());

builder.createBlock(&newGlobalOp.getRegion());
builder.setInsertionPointToEnd(newGlobalOp.getInitializerBlock());

mlir::Value result =
mlir::LLVM::UndefOp::create(builder, loc, ctorStructArrayTy);

for (auto [index, fn] : llvm::enumerate(globalXtors)) {
mlir::Value structInit =
mlir::LLVM::UndefOp::create(builder, loc, ctorStructTy);
mlir::Value initPriority = mlir::LLVM::ConstantOp::create(
builder, loc, ctorStructFields[0], fn.second);
mlir::Value initFuncAddr = mlir::LLVM::AddressOfOp::create(
builder, loc, ctorStructFields[1], fn.first);
mlir::Value initAssociate =
mlir::LLVM::ZeroOp::create(builder, loc, ctorStructFields[2]);
// Literal zero makes the InsertValueOp::create ambiguous.
llvm::SmallVector<int64_t> zero{0};
structInit = mlir::LLVM::InsertValueOp::create(builder, loc, structInit,
initPriority, zero);
structInit = mlir::LLVM::InsertValueOp::create(builder, loc, structInit,
initFuncAddr, 1);
// TODO: handle associated data for initializers.
structInit = mlir::LLVM::InsertValueOp::create(builder, loc, structInit,
initAssociate, 2);
result = mlir::LLVM::InsertValueOp::create(builder, loc, result, structInit,
index);
}

builder.create<mlir::LLVM::ReturnOp>(loc, result);
}

// The applyPartialConversion function traverses blocks in the dominance order,
// so it does not lower and operations that are not reachachable from the
// operations passed in as arguments. Since we do need to lower such code in
Expand Down Expand Up @@ -2519,6 +2586,14 @@ void ConvertCIRToLLVMPass::runOnOperation() {

if (failed(applyPartialConversion(ops, target, std::move(patterns))))
signalPassFailure();

// Emit the llvm.global_ctors array.
buildCtorDtorList(module, cir::CIRDialect::getGlobalCtorsAttrName(),
"llvm.global_ctors", [](mlir::Attribute attr) {
auto ctorAttr = mlir::cast<cir::GlobalCtorAttr>(attr);
return std::make_pair(ctorAttr.getName(),
ctorAttr.getPriority());
});
}

mlir::LogicalResult CIRToLLVMBrOpLowering::matchAndRewrite(
Expand Down
29 changes: 26 additions & 3 deletions clang/test/CIR/CodeGen/global-init.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir -mmlir --mlir-print-ir-before=cir-lowering-prepare %s -o %t.cir 2> %t-before.cir
// RUN: FileCheck --input-file=%t-before.cir %s --check-prefix=CIR-BEFORE-LPP
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR

// Note: The LoweringPrepare work isn't yet complete. We still need to create
// the global ctor list attribute.
// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefix=LLVM
// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
// RUN: FileCheck --input-file=%t.ll %s --check-prefix=OGCG

struct NeedsCtor {
NeedsCtor();
Expand All @@ -15,6 +16,9 @@ NeedsCtor needsCtor;
// CIR-BEFORE-LPP: %[[THIS:.*]] = cir.get_global @needsCtor : !cir.ptr<!rec_NeedsCtor>
// CIR-BEFORE-LPP: cir.call @_ZN9NeedsCtorC1Ev(%[[THIS]]) : (!cir.ptr<!rec_NeedsCtor>) -> ()

// CIR: module @{{.*}} attributes {
// CIR-SAME: cir.global_ctors = [#cir.global_ctor<"_GLOBAL__sub_I_[[FILENAME:.*]]", 65535>]

// CIR: cir.global external @needsCtor = #cir.zero : !rec_NeedsCtor
// CIR: cir.func internal private @__cxx_global_var_init() {
// CIR: %0 = cir.get_global @needsCtor : !cir.ptr<!rec_NeedsCtor>
Expand All @@ -24,3 +28,22 @@ NeedsCtor needsCtor;
// CIR: cir.call @__cxx_global_var_init() : () -> ()
// CIR: cir.return
// CIR: }

// LLVM: @needsCtor = global %struct.NeedsCtor zeroinitializer, align 1
// LLVM: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_[[FILENAME:.*]], ptr null }]
// LLVM: declare void @_ZN9NeedsCtorC1Ev(ptr)

// LLVM: define internal void @__cxx_global_var_init()
// LLVM: call void @_ZN9NeedsCtorC1Ev(ptr @needsCtor)

// LLVM: define void @_GLOBAL__sub_I_[[FILENAME]]()
// LLVM: call void @__cxx_global_var_init()

// OGCG: @needsCtor = global %struct.NeedsCtor zeroinitializer, align 1
// OGCG: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_[[FILENAME:.*]], ptr null }]

// OGCG: define internal void @__cxx_global_var_init() {{.*}} section ".text.startup" {
// OGCG: call void @_ZN9NeedsCtorC1Ev(ptr noundef nonnull align 1 dereferenceable(1) @needsCtor)

// OGCG: define internal void @_GLOBAL__sub_I_[[FILENAME]]() {{.*}} section ".text.startup" {
// OGCG: call void @__cxx_global_var_init()