@@ -400,8 +400,7 @@ class GlobalsImporter final {
400
400
// later, in ComputeCrossModuleImport, after import decisions are
401
401
// complete, which is more efficient than adding them here.
402
402
if (ExportLists)
403
- (*ExportLists)[RefSummary->modulePath ()][VI] =
404
- GlobalValueSummary::Definition;
403
+ (*ExportLists)[RefSummary->modulePath ()].insert (VI);
405
404
406
405
// If variable is not writeonly we attempt to recursively analyze
407
406
// its references in order to import referenced constants.
@@ -582,7 +581,7 @@ class WorkloadImportsManager : public ModuleImportsManager {
582
581
GlobalValueSummary::Definition;
583
582
GVI.onImportingSummary (*GVS);
584
583
if (ExportLists)
585
- (*ExportLists)[ExportingModule][VI] = GlobalValueSummary::Definition ;
584
+ (*ExportLists)[ExportingModule]. insert (VI) ;
586
585
}
587
586
LLVM_DEBUG (dbgs () << " [Workload] Done\n " );
588
587
}
@@ -818,10 +817,8 @@ static void computeImportForFunction(
818
817
// Since definition takes precedence over declaration for the same VI,
819
818
// try emplace <VI, declaration> pair without checking insert result.
820
819
// 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.
825
822
ImportList[DeclSourceModule].try_emplace (
826
823
VI.getGUID (), GlobalValueSummary::Declaration);
827
824
}
@@ -892,7 +889,7 @@ static void computeImportForFunction(
892
889
// later, in ComputeCrossModuleImport, after import decisions are
893
890
// complete, which is more efficient than adding them here.
894
891
if (ExportLists)
895
- (*ExportLists)[ExportModulePath][VI] = GlobalValueSummary::Definition ;
892
+ (*ExportLists)[ExportModulePath]. insert (VI) ;
896
893
}
897
894
898
895
auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) {
@@ -998,19 +995,29 @@ static bool isGlobalVarSummary(const ModuleSummaryIndex &Index,
998
995
return false ;
999
996
}
1000
997
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) {
1005
1015
unsigned NumGVS = 0 ;
1006
- DefinedGVS = 0 ;
1007
1016
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))
1012
1019
++NumGVS;
1013
- } else if (Type == GlobalValueSummary::Definition)
1020
+ else if (Type == GlobalValueSummary::Definition)
1014
1021
++DefinedFS;
1015
1022
}
1016
1023
return NumGVS;
@@ -1046,7 +1053,7 @@ static bool checkVariableImport(
1046
1053
};
1047
1054
1048
1055
for (auto &ExportPerModule : ExportLists)
1049
- for (auto &[VI, Unused] : ExportPerModule.second )
1056
+ for (auto &VI : ExportPerModule.second )
1050
1057
if (!FlattenedImports.count (VI.getGUID ()) &&
1051
1058
IsReadOrWriteOnlyVarNeedingImporting (ExportPerModule.first , VI))
1052
1059
return false ;
@@ -1079,14 +1086,12 @@ void llvm::ComputeCrossModuleImport(
1079
1086
// since we may import the same values multiple times into different modules
1080
1087
// during the import computation.
1081
1088
for (auto &ELI : ExportLists) {
1089
+ // `NewExports` tracks the VI that gets exported because the full definition
1090
+ // of its user/referencer gets exported.
1082
1091
FunctionImporter::ExportSetTy NewExports;
1083
1092
const auto &DefinedGVSummaries =
1084
1093
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 ) {
1090
1095
// Find the copy defined in the exporting module so that we can mark the
1091
1096
// values it references in that specific definition as exported.
1092
1097
// Below we will add all references and called values, without regard to
@@ -1105,31 +1110,22 @@ void llvm::ComputeCrossModuleImport(
1105
1110
// we convert such variables initializers to "zeroinitializer".
1106
1111
// See processGlobalForThinLTO.
1107
1112
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);
1113
1115
} else {
1114
1116
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);
1125
1121
}
1126
1122
}
1127
1123
// Prune list computed above to only include values defined in the
1128
1124
// exporting module. We do this after the above insertion since we may hit
1129
1125
// the same ref/call target multiple times in above loop, and it is more
1130
1126
// efficient to avoid a set lookup each time.
1131
1127
for (auto EI = NewExports.begin (); EI != NewExports.end ();) {
1132
- if (!DefinedGVSummaries.count (EI->first . getGUID ()))
1128
+ if (!DefinedGVSummaries.count (EI->getGUID ()))
1133
1129
NewExports.erase (EI++);
1134
1130
else
1135
1131
++EI;
@@ -1144,29 +1140,22 @@ void llvm::ComputeCrossModuleImport(
1144
1140
for (auto &ModuleImports : ImportLists) {
1145
1141
auto ModName = ModuleImports.first ;
1146
1142
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 " );
1157
1148
for (auto &Src : ModuleImports.second ) {
1158
1149
auto SrcModName = Src.first ;
1159
- unsigned DefinedGVS = 0 , DefinedFS = 0 ;
1150
+ unsigned DefinedFS = 0 ;
1160
1151
unsigned NumGVSPerMod =
1161
- numGlobalVarSummaries (Index, Src.second , DefinedGVS, DefinedFS);
1152
+ numGlobalVarSummaries (Index, Src.second , DefinedFS);
1162
1153
LLVM_DEBUG (dbgs () << " - " << DefinedFS << " function definitions and "
1163
1154
<< Src.second .size () - NumGVSPerMod - DefinedFS
1164
1155
<< " function declarations imported from " << SrcModName
1165
1156
<< " \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 " );
1170
1159
}
1171
1160
}
1172
1161
#endif
@@ -1180,17 +1169,14 @@ static void dumpImportListForModule(const ModuleSummaryIndex &Index,
1180
1169
<< ImportList.size () << " modules.\n " );
1181
1170
for (auto &Src : ImportList) {
1182
1171
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);
1186
1174
LLVM_DEBUG (dbgs () << " - " << DefinedFS << " function definitions and "
1187
1175
<< Src.second .size () - DefinedFS - NumGVSPerMod
1188
1176
<< " function declarations imported from " << SrcModName
1189
1177
<< " \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 " );
1194
1180
}
1195
1181
}
1196
1182
#endif
0 commit comments