Skip to content

Commit 6682753

Browse files
authored
[MLIR][LLVM] Refactor globals insertion point in import (#127490)
Unifies imports to use a single insertion point, `globalInsertionOp`, for global values. Refactors insertion point setup into `setGlobalInsertionPoint`, which sets insertion point after `globalInsertionOp` or defaults to the start of the module if it is not set.
1 parent 9a4bf98 commit 6682753

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

mlir/include/mlir/Target/LLVMIR/ModuleImport.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,10 @@ class ModuleImport {
407407
/// always requires a symbol name.
408408
FlatSymbolRefAttr
409409
getOrCreateNamelessSymbolName(llvm::GlobalVariable *globalVar);
410+
/// Returns the global insertion point for the next global operation. If the
411+
/// `globalInsertionOp` is set, the insertion point is placed after the
412+
/// specified operation. Otherwise, it defaults to the start of the module.
413+
OpBuilder::InsertionGuard setGlobalInsertionPoint();
410414

411415
/// Builder pointing at where the next instruction should be generated.
412416
OpBuilder builder;
@@ -416,8 +420,6 @@ class ModuleImport {
416420
Operation *constantInsertionOp = nullptr;
417421
/// Operation to insert the next global after.
418422
Operation *globalInsertionOp = nullptr;
419-
/// Operation to insert the next alias after.
420-
Operation *aliasInsertionOp = nullptr;
421423
/// Operation to insert comdat selector operations into.
422424
ComdatOp globalComdatOp = nullptr;
423425
/// The current context.

mlir/lib/Target/LLVMIR/ModuleImport.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -962,13 +962,18 @@ ModuleImport::getOrCreateNamelessSymbolName(llvm::GlobalVariable *globalVar) {
962962
return symbolRef;
963963
}
964964

965-
LogicalResult ModuleImport::convertAlias(llvm::GlobalAlias *alias) {
966-
// Insert the global after the last one or at the start of the module.
965+
OpBuilder::InsertionGuard ModuleImport::setGlobalInsertionPoint() {
967966
OpBuilder::InsertionGuard guard(builder);
968-
if (!aliasInsertionOp)
969-
builder.setInsertionPointToStart(mlirModule.getBody());
967+
if (globalInsertionOp)
968+
builder.setInsertionPointAfter(globalInsertionOp);
970969
else
971-
builder.setInsertionPointAfter(aliasInsertionOp);
970+
builder.setInsertionPointToStart(mlirModule.getBody());
971+
return guard;
972+
}
973+
974+
LogicalResult ModuleImport::convertAlias(llvm::GlobalAlias *alias) {
975+
// Insert the alias after the last one or at the start of the module.
976+
OpBuilder::InsertionGuard guard = setGlobalInsertionPoint();
972977

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

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

9971002
LogicalResult ModuleImport::convertGlobal(llvm::GlobalVariable *globalVar) {
9981003
// Insert the global after the last one or at the start of the module.
999-
OpBuilder::InsertionGuard guard(builder);
1000-
if (!globalInsertionOp)
1001-
builder.setInsertionPointToStart(mlirModule.getBody());
1002-
else
1003-
builder.setInsertionPointAfter(globalInsertionOp);
1004+
OpBuilder::InsertionGuard guard = setGlobalInsertionPoint();
10041005

10051006
Attribute valueAttr;
10061007
if (globalVar->hasInitializer())
@@ -1096,11 +1097,8 @@ ModuleImport::convertGlobalCtorsAndDtors(llvm::GlobalVariable *globalVar) {
10961097
priorities.push_back(priority->getValue().getZExtValue());
10971098
}
10981099

1099-
OpBuilder::InsertionGuard guard(builder);
1100-
if (!globalInsertionOp)
1101-
builder.setInsertionPointToStart(mlirModule.getBody());
1102-
else
1103-
builder.setInsertionPointAfter(globalInsertionOp);
1100+
// Insert the global after the last one or at the start of the module.
1101+
OpBuilder::InsertionGuard guard = setGlobalInsertionPoint();
11041102

11051103
if (globalVar->getName() == getGlobalCtorsVarName()) {
11061104
globalInsertionOp = builder.create<LLVM::GlobalCtorsOp>(

mlir/test/Target/LLVMIR/Import/alias.ll

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,6 @@ entry:
6868
@a1 = private alias i32, ptr @g1
6969
@a2 = private alias ptr, ptr @a1
7070

71-
; CHECK: llvm.mlir.alias private @a1 {dso_local} : i32 {
72-
; CHECK: %[[ADDR:.*]] = llvm.mlir.addressof @g1 : !llvm.ptr
73-
; CHECK: llvm.return %[[ADDR]] : !llvm.ptr
74-
; CHECK: }
75-
; CHECK: llvm.mlir.alias private @a2 {dso_local} : !llvm.ptr {
76-
; CHECK-NEXT: %[[ADDR:.*]] = llvm.mlir.addressof @a1 : !llvm.ptr
77-
; CHECK-NEXT: llvm.return %[[ADDR]] : !llvm.ptr
78-
; CHECK-NEXT: }
7971

8072
; CHECK: llvm.mlir.global internal constant @g2() {addr_space = 0 : i32, dso_local} : !llvm.ptr {
8173
; CHECK-NEXT: %[[ADDR:.*]] = llvm.mlir.addressof @a1 : !llvm.ptr
@@ -86,3 +78,13 @@ entry:
8678
; CHECK-NEXT: %[[ADDR:.*]] = llvm.mlir.addressof @a2 : !llvm.ptr
8779
; CHECK-NEXT: llvm.return %[[ADDR]] : !llvm.ptr
8880
; CHECK-NEXT: }
81+
82+
; CHECK: llvm.mlir.alias private @a1 {dso_local} : i32 {
83+
; CHECK-NEXT: %[[ADDR:.*]] = llvm.mlir.addressof @g1 : !llvm.ptr
84+
; CHECK-NEXT: llvm.return %[[ADDR]] : !llvm.ptr
85+
; CHECK-NEXT: }
86+
87+
; CHECK: llvm.mlir.alias private @a2 {dso_local} : !llvm.ptr {
88+
; CHECK-NEXT: %[[ADDR:.*]] = llvm.mlir.addressof @a1 : !llvm.ptr
89+
; CHECK-NEXT: llvm.return %[[ADDR]] : !llvm.ptr
90+
; CHECK-NEXT: }

0 commit comments

Comments
 (0)