@@ -400,8 +400,7 @@ class GlobalsImporter final {
400400 // later, in ComputeCrossModuleImport, after import decisions are
401401 // complete, which is more efficient than adding them here.
402402 if (ExportLists)
403- (*ExportLists)[RefSummary->modulePath ()][VI] =
404- GlobalValueSummary::Definition;
403+ (*ExportLists)[RefSummary->modulePath ()].insert (VI);
405404
406405 // If variable is not writeonly we attempt to recursively analyze
407406 // its references in order to import referenced constants.
@@ -582,7 +581,7 @@ class WorkloadImportsManager : public ModuleImportsManager {
582581 GlobalValueSummary::Definition;
583582 GVI.onImportingSummary (*GVS);
584583 if (ExportLists)
585- (*ExportLists)[ExportingModule][VI] = GlobalValueSummary::Definition ;
584+ (*ExportLists)[ExportingModule]. insert (VI) ;
586585 }
587586 LLVM_DEBUG (dbgs () << " [Workload] Done\n " );
588587 }
@@ -818,10 +817,8 @@ static void computeImportForFunction(
818817 // Since definition takes precedence over declaration for the same VI,
819818 // try emplace <VI, declaration> pair without checking insert result.
820819 // If insert doesn't happen, there must be an existing entry keyed by
821- // VI.
822- if (ExportLists)
823- (*ExportLists)[DeclSourceModule].try_emplace (
824- VI, GlobalValueSummary::Declaration);
820+ // VI. Note `ExportLists` only keeps track of exports due to imported
821+ // definitions.
825822 ImportList[DeclSourceModule].try_emplace (
826823 VI.getGUID (), GlobalValueSummary::Declaration);
827824 }
@@ -892,7 +889,7 @@ static void computeImportForFunction(
892889 // later, in ComputeCrossModuleImport, after import decisions are
893890 // complete, which is more efficient than adding them here.
894891 if (ExportLists)
895- (*ExportLists)[ExportModulePath][VI] = GlobalValueSummary::Definition ;
892+ (*ExportLists)[ExportModulePath]. insert (VI) ;
896893 }
897894
898895 auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) {
@@ -998,19 +995,29 @@ static bool isGlobalVarSummary(const ModuleSummaryIndex &Index,
998995 return false ;
999996}
1000997
1001- template <class T >
1002- static unsigned numGlobalVarSummaries (const ModuleSummaryIndex &Index, T &Cont,
1003- unsigned &DefinedGVS,
1004- unsigned &DefinedFS) {
998+ // Return the number of global variable summaries in ExportSet.
999+ static unsigned
1000+ numGlobalVarSummaries (const ModuleSummaryIndex &Index,
1001+ FunctionImporter::ExportSetTy &ExportSet) {
1002+ unsigned NumGVS = 0 ;
1003+ for (auto &VI : ExportSet)
1004+ if (isGlobalVarSummary (Index, VI.getGUID ()))
1005+ ++NumGVS;
1006+ return NumGVS;
1007+ }
1008+
1009+ // Given ImportMap, return the number of global variable summaries and record
1010+ // the number of defined function summaries as output parameter.
1011+ static unsigned
1012+ numGlobalVarSummaries (const ModuleSummaryIndex &Index,
1013+ FunctionImporter::FunctionsToImportTy &ImportMap,
1014+ unsigned &DefinedFS) {
10051015 unsigned NumGVS = 0 ;
1006- DefinedGVS = 0 ;
10071016 DefinedFS = 0 ;
1008- for (auto &[GUID, Type] : Cont) {
1009- if (isGlobalVarSummary (Index, GUID)) {
1010- if (Type == GlobalValueSummary::Definition)
1011- ++DefinedGVS;
1017+ for (auto &[GUID, Type] : ImportMap) {
1018+ if (isGlobalVarSummary (Index, GUID))
10121019 ++NumGVS;
1013- } else if (Type == GlobalValueSummary::Definition)
1020+ else if (Type == GlobalValueSummary::Definition)
10141021 ++DefinedFS;
10151022 }
10161023 return NumGVS;
@@ -1046,7 +1053,7 @@ static bool checkVariableImport(
10461053 };
10471054
10481055 for (auto &ExportPerModule : ExportLists)
1049- for (auto &[VI, Unused] : ExportPerModule.second )
1056+ for (auto &VI : ExportPerModule.second )
10501057 if (!FlattenedImports.count (VI.getGUID ()) &&
10511058 IsReadOrWriteOnlyVarNeedingImporting (ExportPerModule.first , VI))
10521059 return false ;
@@ -1079,14 +1086,12 @@ void llvm::ComputeCrossModuleImport(
10791086 // since we may import the same values multiple times into different modules
10801087 // during the import computation.
10811088 for (auto &ELI : ExportLists) {
1089+ // `NewExports` tracks the VI that gets exported because the full definition
1090+ // of its user/referencer gets exported.
10821091 FunctionImporter::ExportSetTy NewExports;
10831092 const auto &DefinedGVSummaries =
10841093 ModuleToDefinedGVSummaries.lookup (ELI.first );
1085- for (auto &[EI, Type] : ELI.second ) {
1086- // If a variable is exported as a declaration, its 'refs' and 'calls' are
1087- // not further exported.
1088- if (Type == GlobalValueSummary::Declaration)
1089- continue ;
1094+ for (auto &EI : ELI.second ) {
10901095 // Find the copy defined in the exporting module so that we can mark the
10911096 // values it references in that specific definition as exported.
10921097 // Below we will add all references and called values, without regard to
@@ -1105,31 +1110,22 @@ void llvm::ComputeCrossModuleImport(
11051110 // we convert such variables initializers to "zeroinitializer".
11061111 // See processGlobalForThinLTO.
11071112 if (!Index.isWriteOnly (GVS))
1108- for (const auto &VI : GVS->refs ()) {
1109- // Try to emplace the declaration entry. If a definition entry
1110- // already exists for key `VI`, this is a no-op.
1111- NewExports.try_emplace (VI, GlobalValueSummary::Declaration);
1112- }
1113+ for (const auto &VI : GVS->refs ())
1114+ NewExports.insert (VI);
11131115 } else {
11141116 auto *FS = cast<FunctionSummary>(S);
1115- for (const auto &Edge : FS->calls ()) {
1116- // Try to emplace the declaration entry. If a definition entry
1117- // already exists for key `VI`, this is a no-op.
1118- NewExports.try_emplace (Edge.first , GlobalValueSummary::Declaration);
1119- }
1120- for (const auto &Ref : FS->refs ()) {
1121- // Try to emplace the declaration entry. If a definition entry
1122- // already exists for key `VI`, this is a no-op.
1123- NewExports.try_emplace (Ref, GlobalValueSummary::Declaration);
1124- }
1117+ for (const auto &Edge : FS->calls ())
1118+ NewExports.insert (Edge.first );
1119+ for (const auto &Ref : FS->refs ())
1120+ NewExports.insert (Ref);
11251121 }
11261122 }
11271123 // Prune list computed above to only include values defined in the
11281124 // exporting module. We do this after the above insertion since we may hit
11291125 // the same ref/call target multiple times in above loop, and it is more
11301126 // efficient to avoid a set lookup each time.
11311127 for (auto EI = NewExports.begin (); EI != NewExports.end ();) {
1132- if (!DefinedGVSummaries.count (EI->first . getGUID ()))
1128+ if (!DefinedGVSummaries.count (EI->getGUID ()))
11331129 NewExports.erase (EI++);
11341130 else
11351131 ++EI;
@@ -1144,29 +1140,22 @@ void llvm::ComputeCrossModuleImport(
11441140 for (auto &ModuleImports : ImportLists) {
11451141 auto ModName = ModuleImports.first ;
11461142 auto &Exports = ExportLists[ModName];
1147- unsigned DefinedGVS = 0 , DefinedFS = 0 ;
1148- unsigned NumGVS =
1149- numGlobalVarSummaries (Index, Exports, DefinedGVS, DefinedFS);
1150- LLVM_DEBUG (dbgs () << " * Module " << ModName << " exports " << DefinedFS
1151- << " function as definitions, "
1152- << Exports.size () - NumGVS - DefinedFS
1153- << " functions as declarations, " << DefinedGVS
1154- << " var definitions and " << NumGVS - DefinedGVS
1155- << " var declarations. Imports from "
1156- << ModuleImports.second .size () << " modules.\n " );
1143+ unsigned NumGVS = numGlobalVarSummaries (Index, Exports);
1144+ LLVM_DEBUG (dbgs () << " * Module " << ModName << " exports "
1145+ << Exports.size () - NumGVS << " functions and " << NumGVS
1146+ << " vars. Imports from " << ModuleImports.second .size ()
1147+ << " modules.\n " );
11571148 for (auto &Src : ModuleImports.second ) {
11581149 auto SrcModName = Src.first ;
1159- unsigned DefinedGVS = 0 , DefinedFS = 0 ;
1150+ unsigned DefinedFS = 0 ;
11601151 unsigned NumGVSPerMod =
1161- numGlobalVarSummaries (Index, Src.second , DefinedGVS, DefinedFS);
1152+ numGlobalVarSummaries (Index, Src.second , DefinedFS);
11621153 LLVM_DEBUG (dbgs () << " - " << DefinedFS << " function definitions and "
11631154 << Src.second .size () - NumGVSPerMod - DefinedFS
11641155 << " function declarations imported from " << SrcModName
11651156 << " \n " );
1166- LLVM_DEBUG (dbgs () << " - " << DefinedGVS << " global vars definition and "
1167- << NumGVSPerMod - DefinedGVS
1168- << " global vars declaration imported from "
1169- << SrcModName << " \n " );
1157+ LLVM_DEBUG (dbgs () << " - " << NumGVSPerMod
1158+ << " global vars imported from " << SrcModName << " \n " );
11701159 }
11711160 }
11721161#endif
@@ -1180,17 +1169,14 @@ static void dumpImportListForModule(const ModuleSummaryIndex &Index,
11801169 << ImportList.size () << " modules.\n " );
11811170 for (auto &Src : ImportList) {
11821171 auto SrcModName = Src.first ;
1183- unsigned DefinedGVS = 0 , DefinedFS = 0 ;
1184- unsigned NumGVSPerMod =
1185- numGlobalVarSummaries (Index, Src.second , DefinedGVS, DefinedFS);
1172+ unsigned DefinedFS = 0 ;
1173+ unsigned NumGVSPerMod = numGlobalVarSummaries (Index, Src.second , DefinedFS);
11861174 LLVM_DEBUG (dbgs () << " - " << DefinedFS << " function definitions and "
11871175 << Src.second .size () - DefinedFS - NumGVSPerMod
11881176 << " function declarations imported from " << SrcModName
11891177 << " \n " );
1190- LLVM_DEBUG (dbgs () << " - " << DefinedGVS << " var definitions and "
1191- << NumGVSPerMod - DefinedGVS
1192- << " var declarations imported from " << SrcModName
1193- << " \n " );
1178+ LLVM_DEBUG (dbgs () << " - " << NumGVSPerMod << " vars imported from "
1179+ << SrcModName << " \n " );
11941180 }
11951181}
11961182#endif
0 commit comments