@@ -334,6 +334,25 @@ using EdgeInfo = std::tuple<const FunctionSummary *, unsigned /* Threshold */>;
334334
335335} // anonymous namespace
336336
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+
337356// / Import globals referenced by a function or other globals that are being
338357// / imported, if importing such global is possible.
339358class GlobalsImporter final {
@@ -392,17 +411,13 @@ class GlobalsImporter final {
392411
393412 // If there isn't an entry for GUID, insert <GUID, Definition> pair.
394413 // 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+
398419 // Only update stat and exports if we haven't already imported this
399420 // 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- }
406421 NumImportedGlobalVarsThinLink++;
407422 // Any references made by this variable will be marked exported
408423 // later, in ComputeCrossModuleImport, after import decisions are
@@ -882,13 +897,10 @@ static void computeImportForFunction(
882897 if (ImportDeclaration && SummaryForDeclImport) {
883898 StringRef DeclSourceModule = SummaryForDeclImport->modulePath ();
884899
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
889901 // definitions.
890- ImportList[ DeclSourceModule]. try_emplace (
891- VI.getGUID (), GlobalValueSummary::Declaration );
902+ FunctionImporter::maybeAddDeclaration ( ImportList, DeclSourceModule,
903+ VI.getGUID ());
892904 }
893905 // Update with new larger threshold if this was a retry (otherwise
894906 // we would have already inserted with NewThreshold above). Also
@@ -937,22 +949,16 @@ static void computeImportForFunction(
937949
938950 // Try emplace the definition entry, and update stats based on insertion
939951 // 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) {
946955 NumImportedFunctionsThinLink++;
947956 if (IsHotCallsite)
948957 NumImportedHotFunctionsThinLink++;
949958 if (IsCriticalCallsite)
950959 NumImportedCriticalFunctionsThinLink++;
951960 }
952961
953- if (Iter->second == GlobalValueSummary::Declaration)
954- Iter->second = GlobalValueSummary::Definition;
955-
956962 // Any calls/references made by this function will be marked exported
957963 // later, in ComputeCrossModuleImport, after import decisions are
958964 // complete, which is more efficient than adding them here.
@@ -1300,13 +1306,8 @@ static void ComputeCrossModuleImportForModuleFromIndexForTest(
13001306 if (Summary->modulePath () == ModulePath)
13011307 continue ;
13021308 // 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 ());
13101311 }
13111312#ifndef NDEBUG
13121313 dumpImportListForModule (Index, ModulePath, ImportList);
0 commit comments