@@ -874,6 +874,24 @@ Attribute ModuleImport::getConstantAsAttr(llvm::Constant *constant) {
874
874
return {};
875
875
}
876
876
877
+ FlatSymbolRefAttr
878
+ ModuleImport::getOrCreateNamelessSymbolName (llvm::GlobalVariable *globalVar) {
879
+ assert (globalVar->getName ().empty () &&
880
+ " expected to work with a nameless global" );
881
+ auto [it, success] = namelessGlobals.try_emplace (globalVar);
882
+ if (!success)
883
+ return it->second ;
884
+
885
+ // Make sure the symbol name does not clash with an existing symbol.
886
+ SmallString<128 > globalName = SymbolTable::generateSymbolName<128 >(
887
+ getNamelessGlobalPrefix (),
888
+ [this ](StringRef newName) { return llvmModule->getNamedValue (newName); },
889
+ namelessGlobalId);
890
+ auto symbolRef = FlatSymbolRefAttr::get (context, globalName);
891
+ it->getSecond () = symbolRef;
892
+ return symbolRef;
893
+ }
894
+
877
895
LogicalResult ModuleImport::convertGlobal (llvm::GlobalVariable *globalVar) {
878
896
// Insert the global after the last one or at the start of the module.
879
897
OpBuilder::InsertionGuard guard (builder);
@@ -907,17 +925,10 @@ LogicalResult ModuleImport::convertGlobal(llvm::GlobalVariable *globalVar) {
907
925
908
926
// Workaround to support LLVM's nameless globals. MLIR, in contrast to LLVM,
909
927
// always requires a symbol name.
910
- SmallString<128 > globalName (globalVar->getName ());
911
- if (globalName.empty ()) {
912
- // Make sure the symbol name does not clash with an existing symbol.
913
- globalName = SymbolTable::generateSymbolName<128 >(
914
- getNamelessGlobalPrefix (),
915
- [this ](StringRef newName) {
916
- return llvmModule->getNamedValue (newName);
917
- },
918
- namelessGlobalId);
919
- namelessGlobals[globalVar] = FlatSymbolRefAttr::get (context, globalName);
920
- }
928
+ StringRef globalName = globalVar->getName ();
929
+ if (globalName.empty ())
930
+ globalName = getOrCreateNamelessSymbolName (globalVar).getValue ();
931
+
921
932
GlobalOp globalOp = builder.create <GlobalOp>(
922
933
mlirModule.getLoc (), type, globalVar->isConstant (),
923
934
convertLinkageFromLLVM (globalVar->getLinkage ()), StringRef (globalName),
@@ -1100,13 +1111,14 @@ FailureOr<Value> ModuleImport::convertConstant(llvm::Constant *constant) {
1100
1111
}
1101
1112
1102
1113
// Convert global variable accesses.
1103
- if (auto *globalVar = dyn_cast<llvm::GlobalObject>(constant)) {
1104
- Type type = convertType (globalVar ->getType ());
1105
- StringRef globalName = globalVar ->getName ();
1114
+ if (auto *globalObj = dyn_cast<llvm::GlobalObject>(constant)) {
1115
+ Type type = convertType (globalObj ->getType ());
1116
+ StringRef globalName = globalObj ->getName ();
1106
1117
FlatSymbolRefAttr symbolRef;
1107
1118
// Empty names are only allowed for global variables.
1108
1119
if (globalName.empty ())
1109
- symbolRef = namelessGlobals[cast<llvm::GlobalVariable>(globalVar)];
1120
+ symbolRef =
1121
+ getOrCreateNamelessSymbolName (cast<llvm::GlobalVariable>(globalObj));
1110
1122
else
1111
1123
symbolRef = FlatSymbolRefAttr::get (context, globalName);
1112
1124
return builder.create <AddressOfOp>(loc, type, symbolRef).getResult ();
0 commit comments