Skip to content

[nfc][thinlto] Factor common state for computeImportForModule #65427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 34 additions & 17 deletions llvm/lib/Transforms/IPO/FunctionImport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,29 @@ class GlobalsImporter final {
}
};

/// Determine the list of imports and exports for each module.
class ModuleImportsManager final {
function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
IsPrevailing;
const ModuleSummaryIndex &Index;
DenseMap<StringRef, FunctionImporter::ExportSetTy> *const ExportLists;

public:
ModuleImportsManager(
function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
IsPrevailing,
const ModuleSummaryIndex &Index,
DenseMap<StringRef, FunctionImporter::ExportSetTy> *ExportLists = nullptr)
: IsPrevailing(IsPrevailing), Index(Index), ExportLists(ExportLists) {}

/// Given the list of globals defined in a module, compute the list of imports
/// as well as the list of "exports", i.e. the list of symbols referenced from
/// another module (that may require promotion).
void computeImportForModule(const GVSummaryMapTy &DefinedGVSummaries,
StringRef ModName,
FunctionImporter::ImportMapTy &ImportList);
};

static const char *
getFailureName(FunctionImporter::ImportFailureReason Reason) {
switch (Reason) {
Expand Down Expand Up @@ -567,20 +590,13 @@ static void computeImportForFunction(
}
}

/// Given the list of globals defined in a module, compute the list of imports
/// as well as the list of "exports", i.e. the list of symbols referenced from
/// another module (that may require promotion).
static void ComputeImportForModule(
const GVSummaryMapTy &DefinedGVSummaries,
function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
isPrevailing,
const ModuleSummaryIndex &Index, StringRef ModName,
FunctionImporter::ImportMapTy &ImportList,
DenseMap<StringRef, FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
void ModuleImportsManager::computeImportForModule(
const GVSummaryMapTy &DefinedGVSummaries, StringRef ModName,
FunctionImporter::ImportMapTy &ImportList) {
// Worklist contains the list of function imported in this module, for which
// we will analyse the callees and may import further down the callgraph.
SmallVector<EdgeInfo, 128> Worklist;
GlobalsImporter GVI(Index, DefinedGVSummaries, isPrevailing, ImportList,
GlobalsImporter GVI(Index, DefinedGVSummaries, IsPrevailing, ImportList,
ExportLists);
FunctionImporter::ImportThresholdsTy ImportThresholds;

Expand All @@ -603,7 +619,7 @@ static void ComputeImportForModule(
continue;
LLVM_DEBUG(dbgs() << "Initialize import for " << VI << "\n");
computeImportForFunction(*FuncSummary, Index, ImportInstrLimit,
DefinedGVSummaries, isPrevailing, Worklist, GVI,
DefinedGVSummaries, IsPrevailing, Worklist, GVI,
ImportList, ExportLists, ImportThresholds);
}

Expand All @@ -615,7 +631,7 @@ static void ComputeImportForModule(

if (auto *FS = dyn_cast<FunctionSummary>(Summary))
computeImportForFunction(*FS, Index, Threshold, DefinedGVSummaries,
isPrevailing, Worklist, GVI, ImportList,
IsPrevailing, Worklist, GVI, ImportList,
ExportLists, ImportThresholds);
}

Expand Down Expand Up @@ -717,13 +733,14 @@ void llvm::ComputeCrossModuleImport(
isPrevailing,
DenseMap<StringRef, FunctionImporter::ImportMapTy> &ImportLists,
DenseMap<StringRef, FunctionImporter::ExportSetTy> &ExportLists) {
ModuleImportsManager MIS(isPrevailing, Index, &ExportLists);
// For each module that has function defined, compute the import/export lists.
for (const auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
auto &ImportList = ImportLists[DefinedGVSummaries.first];
LLVM_DEBUG(dbgs() << "Computing import for Module '"
<< DefinedGVSummaries.first << "'\n");
ComputeImportForModule(DefinedGVSummaries.second, isPrevailing, Index,
DefinedGVSummaries.first, ImportList, &ExportLists);
MIS.computeImportForModule(DefinedGVSummaries.second,
DefinedGVSummaries.first, ImportList);
}

// When computing imports we only added the variables and functions being
Expand Down Expand Up @@ -839,8 +856,8 @@ static void ComputeCrossModuleImportForModuleForTest(

// Compute the import list for this module.
LLVM_DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n");
ComputeImportForModule(FunctionSummaryMap, isPrevailing, Index, ModulePath,
ImportList);
ModuleImportsManager MIS(isPrevailing, Index);
MIS.computeImportForModule(FunctionSummaryMap, ModulePath, ImportList);

#ifndef NDEBUG
dumpImportListForModule(Index, ModulePath, ImportList);
Expand Down