@@ -334,6 +334,25 @@ using EdgeInfo = std::tuple<const FunctionSummary *, unsigned /* Threshold */>;
334
334
335
335
} // anonymous namespace
336
336
337
+ FunctionImporter::AddDefinitionStatus
338
+ FunctionImporter::addDefinition (ImportMapTy &ImportList, StringRef FromModule,
339
+ GlobalValue::GUID GUID) {
340
+ auto [It, Inserted] =
341
+ ImportList[FromModule].try_emplace (GUID, GlobalValueSummary::Definition);
342
+ if (Inserted)
343
+ return AddDefinitionStatus::Inserted;
344
+ if (It->second == GlobalValueSummary::Definition)
345
+ return AddDefinitionStatus::NoChange;
346
+ It->second = GlobalValueSummary::Definition;
347
+ return AddDefinitionStatus::ChangedToDefinition;
348
+ }
349
+
350
+ void FunctionImporter::maybeAddDeclaration (ImportMapTy &ImportList,
351
+ StringRef FromModule,
352
+ GlobalValue::GUID GUID) {
353
+ ImportList[FromModule].try_emplace (GUID, GlobalValueSummary::Declaration);
354
+ }
355
+
337
356
// / Import globals referenced by a function or other globals that are being
338
357
// / imported, if importing such global is possible.
339
358
class GlobalsImporter final {
@@ -392,17 +411,13 @@ class GlobalsImporter final {
392
411
393
412
// If there isn't an entry for GUID, insert <GUID, Definition> pair.
394
413
// Otherwise, definition should take precedence over declaration.
395
- auto [Iter, Inserted] =
396
- ImportList[RefSummary->modulePath ()].try_emplace (
397
- VI.getGUID (), GlobalValueSummary::Definition);
414
+ if (FunctionImporter::addDefinition (
415
+ ImportList, RefSummary->modulePath (), VI.getGUID ()) !=
416
+ FunctionImporter::AddDefinitionStatus::Inserted)
417
+ break ;
418
+
398
419
// Only update stat and exports if we haven't already imported this
399
420
// variable.
400
- if (!Inserted) {
401
- // Set the value to 'std::min(existing-value, new-value)' to make
402
- // sure a definition takes precedence over a declaration.
403
- Iter->second = std::min (GlobalValueSummary::Definition, Iter->second );
404
- break ;
405
- }
406
421
NumImportedGlobalVarsThinLink++;
407
422
// Any references made by this variable will be marked exported
408
423
// later, in ComputeCrossModuleImport, after import decisions are
@@ -882,13 +897,10 @@ static void computeImportForFunction(
882
897
if (ImportDeclaration && SummaryForDeclImport) {
883
898
StringRef DeclSourceModule = SummaryForDeclImport->modulePath ();
884
899
885
- // Since definition takes precedence over declaration for the same VI,
886
- // try emplace <VI, declaration> pair without checking insert result.
887
- // If insert doesn't happen, there must be an existing entry keyed by
888
- // VI. Note `ExportLists` only keeps track of exports due to imported
900
+ // Note `ExportLists` only keeps track of exports due to imported
889
901
// definitions.
890
- ImportList[ DeclSourceModule]. try_emplace (
891
- VI.getGUID (), GlobalValueSummary::Declaration );
902
+ FunctionImporter::maybeAddDeclaration ( ImportList, DeclSourceModule,
903
+ VI.getGUID ());
892
904
}
893
905
// Update with new larger threshold if this was a retry (otherwise
894
906
// we would have already inserted with NewThreshold above). Also
@@ -937,22 +949,16 @@ static void computeImportForFunction(
937
949
938
950
// Try emplace the definition entry, and update stats based on insertion
939
951
// status.
940
- auto [Iter, Inserted] = ImportList[ExportModulePath].try_emplace (
941
- VI.getGUID (), GlobalValueSummary::Definition);
942
-
943
- // We previously decided to import this GUID definition if it was already
944
- // inserted in the set of imports from the exporting module.
945
- if (Inserted || Iter->second == GlobalValueSummary::Declaration) {
952
+ if (FunctionImporter::addDefinition (ImportList, ExportModulePath,
953
+ VI.getGUID ()) !=
954
+ FunctionImporter::AddDefinitionStatus::NoChange) {
946
955
NumImportedFunctionsThinLink++;
947
956
if (IsHotCallsite)
948
957
NumImportedHotFunctionsThinLink++;
949
958
if (IsCriticalCallsite)
950
959
NumImportedCriticalFunctionsThinLink++;
951
960
}
952
961
953
- if (Iter->second == GlobalValueSummary::Declaration)
954
- Iter->second = GlobalValueSummary::Definition;
955
-
956
962
// Any calls/references made by this function will be marked exported
957
963
// later, in ComputeCrossModuleImport, after import decisions are
958
964
// complete, which is more efficient than adding them here.
@@ -1300,13 +1306,8 @@ static void ComputeCrossModuleImportForModuleFromIndexForTest(
1300
1306
if (Summary->modulePath () == ModulePath)
1301
1307
continue ;
1302
1308
// Add an entry to provoke importing by thinBackend.
1303
- auto [Iter, Inserted] = ImportList[Summary->modulePath ()].try_emplace (
1304
- GUID, Summary->importType ());
1305
- if (!Inserted) {
1306
- // Use 'std::min' to make sure definition (with enum value 0) takes
1307
- // precedence over declaration (with enum value 1).
1308
- Iter->second = std::min (Iter->second , Summary->importType ());
1309
- }
1309
+ FunctionImporter::addGUID (ImportList, Summary->modulePath (), GUID,
1310
+ Summary->importType ());
1310
1311
}
1311
1312
#ifndef NDEBUG
1312
1313
dumpImportListForModule (Index, ModulePath, ImportList);
0 commit comments