@@ -516,6 +516,7 @@ class ModuleImportsManager {
516516 const ModuleSummaryIndex &Index,
517517 DenseMap<StringRef, FunctionImporter::ExportSetTy> *ExportLists = nullptr )
518518 : IsPrevailing(IsPrevailing), Index(Index), ExportLists(ExportLists) {}
519+ virtual bool canImport (ValueInfo VI) { return true ; }
519520
520521public:
521522 virtual ~ModuleImportsManager () = default ;
@@ -544,6 +545,11 @@ class WorkloadImportsManager : public ModuleImportsManager {
544545 // determine if a module's import list should be done by the base
545546 // ModuleImportsManager or by us.
546547 StringMap<DenseSet<ValueInfo>> Workloads;
548+ // Track the roots to avoid importing them due to other callers. We want there
549+ // to be only one variant), for which we optimize according to the contextual
550+ // profile. "Variants" refers to copies due to importing - we want there to be
551+ // just one instance of this function.
552+ DenseSet<ValueInfo> Roots;
547553
548554 void
549555 computeImportForModule (const GVSummaryMapTy &DefinedGVSummaries,
@@ -783,12 +789,15 @@ class WorkloadImportsManager : public ModuleImportsManager {
783789 }
784790 auto &Set = Workloads[RootDefiningModule];
785791 Root.getContainedGuids (ContainedGUIDs);
792+ Roots.insert (RootVI);
786793 for (auto Guid : ContainedGUIDs)
787794 if (auto VI = Index.getValueInfo (Guid))
788795 Set.insert (VI);
789796 }
790797 }
791798
799+ bool canImport (ValueInfo VI) override { return !Roots.contains (VI); }
800+
792801public:
793802 WorkloadImportsManager (
794803 function_ref<bool (GlobalValue::GUID, const GlobalValueSummary *)>
@@ -886,6 +895,15 @@ void ModuleImportsManager::computeImportForFunction(
886895 continue ;
887896 }
888897
898+ if (!canImport (VI)) {
899+ LLVM_DEBUG (
900+ dbgs () << " Skipping over " << VI.getGUID ()
901+ << " because its import is handled in a different module." );
902+ assert (VI.getSummaryList ().size () == 1 &&
903+ " The root was expected to be an external symbol" );
904+ continue ;
905+ }
906+
889907 auto GetBonusMultiplier = [](CalleeInfo::HotnessType Hotness) -> float {
890908 if (Hotness == CalleeInfo::HotnessType::Hot)
891909 return ImportHotMultiplier;
0 commit comments