@@ -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
9971002LogicalResult 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>(
0 commit comments