Skip to content

Commit 7e9c44a

Browse files
committed
[MLIR][LLVM] Refactor globals insertion point translation (NFC)
1 parent 4c4fc46 commit 7e9c44a

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

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

Lines changed: 4 additions & 0 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 an
411+
/// operation is set, the insertion point is placed after the specified
412+
/// operation. Otherwise, it defaults to the start of the module.
413+
OpBuilder::InsertionGuard setGlobalInsertionPoint(Operation *op);
410414

411415
/// Builder pointing at where the next instruction should be generated.
412416
OpBuilder builder;

mlir/lib/Target/LLVMIR/ModuleImport.cpp

Lines changed: 13 additions & 15 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(Operation *op) {
967966
OpBuilder::InsertionGuard guard(builder);
968-
if (!aliasInsertionOp)
969-
builder.setInsertionPointToStart(mlirModule.getBody());
967+
if (op)
968+
builder.setInsertionPointAfter(op);
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(aliasInsertionOp);
972977

973978
Type type = convertType(alias->getValueType());
974979
AliasOp aliasOp = builder.create<AliasOp>(
@@ -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(globalInsertionOp);
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(globalInsertionOp);
11041102

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

0 commit comments

Comments
 (0)