Skip to content

[MLIR][LLVM] Refactor globals insertion point in import #127490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2025
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
6 changes: 4 additions & 2 deletions mlir/include/mlir/Target/LLVMIR/ModuleImport.h
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@ class ModuleImport {
/// always requires a symbol name.
FlatSymbolRefAttr
getOrCreateNamelessSymbolName(llvm::GlobalVariable *globalVar);
/// Returns the global insertion point for the next global operation. If the
/// `globalInsertionOp` is set, the insertion point is placed after the
/// specified operation. Otherwise, it defaults to the start of the module.
OpBuilder::InsertionGuard setGlobalInsertionPoint();

/// Builder pointing at where the next instruction should be generated.
OpBuilder builder;
Expand All @@ -416,8 +420,6 @@ class ModuleImport {
Operation *constantInsertionOp = nullptr;
/// Operation to insert the next global after.
Operation *globalInsertionOp = nullptr;
/// Operation to insert the next alias after.
Operation *aliasInsertionOp = nullptr;
/// Operation to insert comdat selector operations into.
ComdatOp globalComdatOp = nullptr;
/// The current context.
Expand Down
30 changes: 14 additions & 16 deletions mlir/lib/Target/LLVMIR/ModuleImport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -962,13 +962,18 @@ ModuleImport::getOrCreateNamelessSymbolName(llvm::GlobalVariable *globalVar) {
return symbolRef;
}

LogicalResult ModuleImport::convertAlias(llvm::GlobalAlias *alias) {
// Insert the global after the last one or at the start of the module.
OpBuilder::InsertionGuard ModuleImport::setGlobalInsertionPoint() {
OpBuilder::InsertionGuard guard(builder);
if (!aliasInsertionOp)
builder.setInsertionPointToStart(mlirModule.getBody());
if (globalInsertionOp)
builder.setInsertionPointAfter(globalInsertionOp);
else
builder.setInsertionPointAfter(aliasInsertionOp);
builder.setInsertionPointToStart(mlirModule.getBody());
return guard;
}

LogicalResult ModuleImport::convertAlias(llvm::GlobalAlias *alias) {
// Insert the alias after the last one or at the start of the module.
OpBuilder::InsertionGuard guard = setGlobalInsertionPoint();

Type type = convertType(alias->getValueType());
AliasOp aliasOp = builder.create<AliasOp>(
Expand All @@ -977,7 +982,7 @@ LogicalResult ModuleImport::convertAlias(llvm::GlobalAlias *alias) {
/*dso_local=*/alias->isDSOLocal(),
/*thread_local=*/alias->isThreadLocal(),
/*attrs=*/ArrayRef<NamedAttribute>());
aliasInsertionOp = aliasOp;
globalInsertionOp = aliasOp;

clearRegionState();
Block *block = builder.createBlock(&aliasOp.getInitializerRegion());
Expand All @@ -996,11 +1001,7 @@ LogicalResult ModuleImport::convertAlias(llvm::GlobalAlias *alias) {

LogicalResult ModuleImport::convertGlobal(llvm::GlobalVariable *globalVar) {
// Insert the global after the last one or at the start of the module.
OpBuilder::InsertionGuard guard(builder);
if (!globalInsertionOp)
builder.setInsertionPointToStart(mlirModule.getBody());
else
builder.setInsertionPointAfter(globalInsertionOp);
OpBuilder::InsertionGuard guard = setGlobalInsertionPoint();

Attribute valueAttr;
if (globalVar->hasInitializer())
Expand Down Expand Up @@ -1096,11 +1097,8 @@ ModuleImport::convertGlobalCtorsAndDtors(llvm::GlobalVariable *globalVar) {
priorities.push_back(priority->getValue().getZExtValue());
}

OpBuilder::InsertionGuard guard(builder);
if (!globalInsertionOp)
builder.setInsertionPointToStart(mlirModule.getBody());
else
builder.setInsertionPointAfter(globalInsertionOp);
// Insert the global after the last one or at the start of the module.
OpBuilder::InsertionGuard guard = setGlobalInsertionPoint();

if (globalVar->getName() == getGlobalCtorsVarName()) {
globalInsertionOp = builder.create<LLVM::GlobalCtorsOp>(
Expand Down
18 changes: 10 additions & 8 deletions mlir/test/Target/LLVMIR/Import/alias.ll
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,6 @@ entry:
@a1 = private alias i32, ptr @g1
@a2 = private alias ptr, ptr @a1

; CHECK: llvm.mlir.alias private @a1 {dso_local} : i32 {
; CHECK: %[[ADDR:.*]] = llvm.mlir.addressof @g1 : !llvm.ptr
; CHECK: llvm.return %[[ADDR]] : !llvm.ptr
; CHECK: }
; CHECK: llvm.mlir.alias private @a2 {dso_local} : !llvm.ptr {
; CHECK-NEXT: %[[ADDR:.*]] = llvm.mlir.addressof @a1 : !llvm.ptr
; CHECK-NEXT: llvm.return %[[ADDR]] : !llvm.ptr
; CHECK-NEXT: }

; CHECK: llvm.mlir.global internal constant @g2() {addr_space = 0 : i32, dso_local} : !llvm.ptr {
; CHECK-NEXT: %[[ADDR:.*]] = llvm.mlir.addressof @a1 : !llvm.ptr
Expand All @@ -86,3 +78,13 @@ entry:
; CHECK-NEXT: %[[ADDR:.*]] = llvm.mlir.addressof @a2 : !llvm.ptr
; CHECK-NEXT: llvm.return %[[ADDR]] : !llvm.ptr
; CHECK-NEXT: }

; CHECK: llvm.mlir.alias private @a1 {dso_local} : i32 {
; CHECK-NEXT: %[[ADDR:.*]] = llvm.mlir.addressof @g1 : !llvm.ptr
; CHECK-NEXT: llvm.return %[[ADDR]] : !llvm.ptr
; CHECK-NEXT: }

; CHECK: llvm.mlir.alias private @a2 {dso_local} : !llvm.ptr {
; CHECK-NEXT: %[[ADDR:.*]] = llvm.mlir.addressof @a1 : !llvm.ptr
; CHECK-NEXT: llvm.return %[[ADDR]] : !llvm.ptr
; CHECK-NEXT: }