@@ -874,6 +874,24 @@ Attribute ModuleImport::getConstantAsAttr(llvm::Constant *constant) {
874874 return {};
875875}
876876
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+
877895LogicalResult ModuleImport::convertGlobal (llvm::GlobalVariable *globalVar) {
878896 // Insert the global after the last one or at the start of the module.
879897 OpBuilder::InsertionGuard guard (builder);
@@ -907,17 +925,10 @@ LogicalResult ModuleImport::convertGlobal(llvm::GlobalVariable *globalVar) {
907925
908926 // Workaround to support LLVM's nameless globals. MLIR, in contrast to LLVM,
909927 // 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+
921932 GlobalOp globalOp = builder.create <GlobalOp>(
922933 mlirModule.getLoc (), type, globalVar->isConstant (),
923934 convertLinkageFromLLVM (globalVar->getLinkage ()), StringRef (globalName),
@@ -1100,13 +1111,14 @@ FailureOr<Value> ModuleImport::convertConstant(llvm::Constant *constant) {
11001111 }
11011112
11021113 // 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 ();
11061117 FlatSymbolRefAttr symbolRef;
11071118 // Empty names are only allowed for global variables.
11081119 if (globalName.empty ())
1109- symbolRef = namelessGlobals[cast<llvm::GlobalVariable>(globalVar)];
1120+ symbolRef =
1121+ getOrCreateNamelessSymbolName (cast<llvm::GlobalVariable>(globalObj));
11101122 else
11111123 symbolRef = FlatSymbolRefAttr::get (context, globalName);
11121124 return builder.create <AddressOfOp>(loc, type, symbolRef).getResult ();
0 commit comments