Skip to content

Commit 60eb640

Browse files
kazutakahiratacjdb
authored andcommitted
[LTO] Turn ImportMapTy into a proper class (NFC) (llvm#105748)
This patch turns type alias ImportMapTy into a proper class to provide a more intuitive interface like: ImportList.addDefinition(...) as opposed to: FunctionImporter::addDefinition(ImportList, ...) Also, this patch requires all non-const accesses to go through addDefinition, maybeAddDeclaration, and addGUID while providing const accesses via: const ImportMapTyImpl &getImportMap() const { return ImportMap; } I realize ImportMapTy may not be the best name as a class (maybe OK as a type alias). I am not renaming ImportMapTy in this patch at least because there are 47 mentions of ImportMapTy under llvm/.
1 parent dadd725 commit 60eb640

File tree

5 files changed

+84
-75
lines changed

5 files changed

+84
-75
lines changed

llvm/include/llvm/Transforms/IPO/FunctionImport.h

+48-34
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,54 @@ class FunctionImporter {
9696
std::tuple<unsigned, const GlobalValueSummary *,
9797
std::unique_ptr<ImportFailureInfo>>>;
9898

99-
/// The map contains an entry for every module to import from, the key being
100-
/// the module identifier to pass to the ModuleLoader. The value is the set of
101-
/// functions to import. The module identifier strings must be owned
102-
/// elsewhere, typically by the in-memory ModuleSummaryIndex the importing
103-
/// decisions are made from (the module path for each summary is owned by the
104-
/// index's module path string table).
105-
using ImportMapTy = DenseMap<StringRef, FunctionsToImportTy>;
99+
/// The map maintains the list of imports. Conceptually, it is a collection
100+
/// of tuples of the form:
101+
///
102+
/// (The name of the source module, GUID, Definition/Declaration)
103+
///
104+
/// The name of the source module is the module identifier to pass to the
105+
/// ModuleLoader. The module identifier strings must be owned elsewhere,
106+
/// typically by the in-memory ModuleSummaryIndex the importing decisions are
107+
/// made from (the module path for each summary is owned by the index's module
108+
/// path string table).
109+
class ImportMapTy {
110+
public:
111+
using ImportMapTyImpl = DenseMap<StringRef, FunctionsToImportTy>;
112+
113+
enum class AddDefinitionStatus {
114+
// No change was made to the list of imports or whether each import should
115+
// be imported as a declaration or definition.
116+
NoChange,
117+
// Successfully added the given GUID to be imported as a definition. There
118+
// was no existing entry with the same GUID as a declaration.
119+
Inserted,
120+
// An existing with the given GUID was changed to a definition.
121+
ChangedToDefinition,
122+
};
123+
124+
// Add the given GUID to ImportList as a definition. If the same GUID has
125+
// been added as a declaration previously, that entry is overridden.
126+
AddDefinitionStatus addDefinition(StringRef FromModule,
127+
GlobalValue::GUID GUID);
128+
129+
// Add the given GUID to ImportList as a declaration. If the same GUID has
130+
// been added as a definition previously, that entry takes precedence, and
131+
// no change is made.
132+
void maybeAddDeclaration(StringRef FromModule, GlobalValue::GUID GUID);
133+
134+
void addGUID(StringRef FromModule, GlobalValue::GUID GUID,
135+
GlobalValueSummary::ImportKind ImportKind) {
136+
if (ImportKind == GlobalValueSummary::Definition)
137+
addDefinition(FromModule, GUID);
138+
else
139+
maybeAddDeclaration(FromModule, GUID);
140+
}
141+
142+
const ImportMapTyImpl &getImportMap() const { return ImportMap; }
143+
144+
private:
145+
ImportMapTyImpl ImportMap;
146+
};
106147

107148
/// The set contains an entry for every global value that the module exports.
108149
/// Depending on the user context, this container is allowed to contain
@@ -122,33 +163,6 @@ class FunctionImporter {
122163
/// Import functions in Module \p M based on the supplied import list.
123164
Expected<bool> importFunctions(Module &M, const ImportMapTy &ImportList);
124165

125-
enum class AddDefinitionStatus {
126-
NoChange,
127-
Inserted,
128-
ChangedToDefinition,
129-
};
130-
131-
// Add the given GUID to ImportList as a definition. If the same GUID has
132-
// been added as a declaration previously, that entry is overridden.
133-
static AddDefinitionStatus addDefinition(ImportMapTy &ImportList,
134-
StringRef FromModule,
135-
GlobalValue::GUID GUID);
136-
137-
// Add the given GUID to ImportList as a declaration. If the same GUID has
138-
// been added as a definition previously, that entry takes precedence, and no
139-
// change is made.
140-
static void maybeAddDeclaration(ImportMapTy &ImportList, StringRef FromModule,
141-
GlobalValue::GUID GUID);
142-
143-
static void addGUID(ImportMapTy &ImportList, StringRef FromModule,
144-
GlobalValue::GUID GUID,
145-
GlobalValueSummary::ImportKind ImportKind) {
146-
if (ImportKind == GlobalValueSummary::Definition)
147-
addDefinition(ImportList, FromModule, GUID);
148-
else
149-
maybeAddDeclaration(ImportList, FromModule, GUID);
150-
}
151-
152166
private:
153167
/// The summaries index used to trigger importing.
154168
const ModuleSummaryIndex &Index;

llvm/lib/LTO/LTO.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ std::string llvm::computeLTOCacheKey(
177177
// Include the hash for every module we import functions from. The set of
178178
// imported symbols for each module may affect code generation and is
179179
// sensitive to link order, so include that as well.
180-
using ImportMapIteratorTy = FunctionImporter::ImportMapTy::const_iterator;
180+
using ImportMapIteratorTy =
181+
FunctionImporter::ImportMapTy::ImportMapTyImpl::const_iterator;
181182
struct ImportModule {
182183
ImportMapIteratorTy ModIt;
183184
const ModuleSummaryIndex::ModuleInfo *ModInfo;
@@ -191,10 +192,10 @@ std::string llvm::computeLTOCacheKey(
191192
};
192193

193194
std::vector<ImportModule> ImportModulesVector;
194-
ImportModulesVector.reserve(ImportList.size());
195+
ImportModulesVector.reserve(ImportList.getImportMap().size());
195196

196-
for (ImportMapIteratorTy It = ImportList.begin(); It != ImportList.end();
197-
++It) {
197+
for (ImportMapIteratorTy It = ImportList.getImportMap().begin();
198+
It != ImportList.getImportMap().end(); ++It) {
198199
ImportModulesVector.push_back({It, Index.getModule(It->getFirst())});
199200
}
200201
// Order using module hash, to be both independent of module name and

llvm/lib/LTO/LTOBackend.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,7 @@ bool lto::initImportList(const Module &M,
726726
if (Summary->modulePath() == M.getModuleIdentifier())
727727
continue;
728728
// Add an entry to provoke importing by thinBackend.
729-
FunctionImporter::addGUID(ImportList, Summary->modulePath(), GUID,
730-
Summary->importType());
729+
ImportList.addGUID(Summary->modulePath(), GUID, Summary->importType());
731730
}
732731
}
733732
return true;

llvm/lib/Transforms/IPO/FunctionImport.cpp

+28-32
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,11 @@ 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) {
337+
FunctionImporter::ImportMapTy::AddDefinitionStatus
338+
FunctionImporter::ImportMapTy::addDefinition(StringRef FromModule,
339+
GlobalValue::GUID GUID) {
340340
auto [It, Inserted] =
341-
ImportList[FromModule].try_emplace(GUID, GlobalValueSummary::Definition);
341+
ImportMap[FromModule].try_emplace(GUID, GlobalValueSummary::Definition);
342342
if (Inserted)
343343
return AddDefinitionStatus::Inserted;
344344
if (It->second == GlobalValueSummary::Definition)
@@ -347,10 +347,9 @@ FunctionImporter::addDefinition(ImportMapTy &ImportList, StringRef FromModule,
347347
return AddDefinitionStatus::ChangedToDefinition;
348348
}
349349

350-
void FunctionImporter::maybeAddDeclaration(ImportMapTy &ImportList,
351-
StringRef FromModule,
352-
GlobalValue::GUID GUID) {
353-
ImportList[FromModule].try_emplace(GUID, GlobalValueSummary::Declaration);
350+
void FunctionImporter::ImportMapTy::maybeAddDeclaration(
351+
StringRef FromModule, GlobalValue::GUID GUID) {
352+
ImportMap[FromModule].try_emplace(GUID, GlobalValueSummary::Declaration);
354353
}
355354

356355
/// Import globals referenced by a function or other globals that are being
@@ -411,9 +410,8 @@ class GlobalsImporter final {
411410

412411
// If there isn't an entry for GUID, insert <GUID, Definition> pair.
413412
// Otherwise, definition should take precedence over declaration.
414-
if (FunctionImporter::addDefinition(
415-
ImportList, RefSummary->modulePath(), VI.getGUID()) !=
416-
FunctionImporter::AddDefinitionStatus::Inserted)
413+
if (ImportList.addDefinition(RefSummary->modulePath(), VI.getGUID()) !=
414+
FunctionImporter::ImportMapTy::AddDefinitionStatus::Inserted)
417415
break;
418416

419417
// Only update stat and exports if we haven't already imported this
@@ -600,8 +598,7 @@ class WorkloadImportsManager : public ModuleImportsManager {
600598
LLVM_DEBUG(dbgs() << "[Workload][Including]" << VI.name() << " from "
601599
<< ExportingModule << " : "
602600
<< Function::getGUID(VI.name()) << "\n");
603-
FunctionImporter::addDefinition(ImportList, ExportingModule,
604-
VI.getGUID());
601+
ImportList.addDefinition(ExportingModule, VI.getGUID());
605602
GVI.onImportingSummary(*GVS);
606603
if (ExportLists)
607604
(*ExportLists)[ExportingModule].insert(VI);
@@ -899,8 +896,7 @@ static void computeImportForFunction(
899896

900897
// Note `ExportLists` only keeps track of exports due to imported
901898
// definitions.
902-
FunctionImporter::maybeAddDeclaration(ImportList, DeclSourceModule,
903-
VI.getGUID());
899+
ImportList.maybeAddDeclaration(DeclSourceModule, VI.getGUID());
904900
}
905901
// Update with new larger threshold if this was a retry (otherwise
906902
// we would have already inserted with NewThreshold above). Also
@@ -949,9 +945,8 @@ static void computeImportForFunction(
949945

950946
// Try emplace the definition entry, and update stats based on insertion
951947
// status.
952-
if (FunctionImporter::addDefinition(ImportList, ExportModulePath,
953-
VI.getGUID()) !=
954-
FunctionImporter::AddDefinitionStatus::NoChange) {
948+
if (ImportList.addDefinition(ExportModulePath, VI.getGUID()) !=
949+
FunctionImporter::ImportMapTy::AddDefinitionStatus::NoChange) {
955950
NumImportedFunctionsThinLink++;
956951
if (IsHotCallsite)
957952
NumImportedHotFunctionsThinLink++;
@@ -1084,7 +1079,7 @@ numGlobalVarSummaries(const ModuleSummaryIndex &Index,
10841079
// the number of defined function summaries as output parameter.
10851080
static unsigned
10861081
numGlobalVarSummaries(const ModuleSummaryIndex &Index,
1087-
FunctionImporter::FunctionsToImportTy &ImportMap,
1082+
const FunctionImporter::FunctionsToImportTy &ImportMap,
10881083
unsigned &DefinedFS) {
10891084
unsigned NumGVS = 0;
10901085
DefinedFS = 0;
@@ -1105,9 +1100,9 @@ static bool checkVariableImport(
11051100
DenseMap<StringRef, FunctionImporter::ExportSetTy> &ExportLists) {
11061101
DenseSet<GlobalValue::GUID> FlattenedImports;
11071102

1108-
for (auto &ImportPerModule : ImportLists)
1109-
for (auto &ExportPerModule : ImportPerModule.second)
1110-
for (auto &[GUID, Type] : ExportPerModule.second)
1103+
for (const auto &ImportPerModule : ImportLists)
1104+
for (const auto &ExportPerModule : ImportPerModule.second.getImportMap())
1105+
for (const auto &[GUID, Type] : ExportPerModule.second)
11111106
FlattenedImports.insert(GUID);
11121107

11131108
// Checks that all GUIDs of read/writeonly vars we see in export lists
@@ -1217,9 +1212,10 @@ void llvm::ComputeCrossModuleImport(
12171212
unsigned NumGVS = numGlobalVarSummaries(Index, Exports);
12181213
LLVM_DEBUG(dbgs() << "* Module " << ModName << " exports "
12191214
<< Exports.size() - NumGVS << " functions and " << NumGVS
1220-
<< " vars. Imports from " << ModuleImports.second.size()
1215+
<< " vars. Imports from "
1216+
<< ModuleImports.second.getImportMap().size()
12211217
<< " modules.\n");
1222-
for (auto &Src : ModuleImports.second) {
1218+
for (const auto &Src : ModuleImports.second.getImportMap()) {
12231219
auto SrcModName = Src.first;
12241220
unsigned DefinedFS = 0;
12251221
unsigned NumGVSPerMod =
@@ -1240,8 +1236,8 @@ static void dumpImportListForModule(const ModuleSummaryIndex &Index,
12401236
StringRef ModulePath,
12411237
FunctionImporter::ImportMapTy &ImportList) {
12421238
LLVM_DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
1243-
<< ImportList.size() << " modules.\n");
1244-
for (auto &Src : ImportList) {
1239+
<< ImportList.getImportMap().size() << " modules.\n");
1240+
for (const auto &Src : ImportList.getImportMap()) {
12451241
auto SrcModName = Src.first;
12461242
unsigned DefinedFS = 0;
12471243
unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second, DefinedFS);
@@ -1306,8 +1302,7 @@ static void ComputeCrossModuleImportForModuleFromIndexForTest(
13061302
if (Summary->modulePath() == ModulePath)
13071303
continue;
13081304
// Add an entry to provoke importing by thinBackend.
1309-
FunctionImporter::addGUID(ImportList, Summary->modulePath(), GUID,
1310-
Summary->importType());
1305+
ImportList.addGUID(Summary->modulePath(), GUID, Summary->importType());
13111306
}
13121307
#ifndef NDEBUG
13131308
dumpImportListForModule(Index, ModulePath, ImportList);
@@ -1496,7 +1491,7 @@ void llvm::gatherImportedSummariesForModule(
14961491
ModuleToSummariesForIndex[std::string(ModulePath)] =
14971492
ModuleToDefinedGVSummaries.lookup(ModulePath);
14981493
// Include summaries for imports.
1499-
for (const auto &ILI : ImportList) {
1494+
for (const auto &ILI : ImportList.getImportMap()) {
15001495
auto &SummariesForIndex = ModuleToSummariesForIndex[std::string(ILI.first)];
15011496

15021497
const auto &DefinedGVSummaries =
@@ -1777,7 +1772,7 @@ Expected<bool> FunctionImporter::importFunctions(
17771772
IRMover Mover(DestModule);
17781773
// Do the actual import of functions now, one Module at a time
17791774
std::set<StringRef> ModuleNameOrderedList;
1780-
for (const auto &FunctionsToImportPerModule : ImportList) {
1775+
for (const auto &FunctionsToImportPerModule : ImportList.getImportMap()) {
17811776
ModuleNameOrderedList.insert(FunctionsToImportPerModule.first);
17821777
}
17831778

@@ -1792,8 +1787,9 @@ Expected<bool> FunctionImporter::importFunctions(
17921787

17931788
for (const auto &Name : ModuleNameOrderedList) {
17941789
// Get the module for the import
1795-
const auto &FunctionsToImportPerModule = ImportList.find(Name);
1796-
assert(FunctionsToImportPerModule != ImportList.end());
1790+
const auto &FunctionsToImportPerModule =
1791+
ImportList.getImportMap().find(Name);
1792+
assert(FunctionsToImportPerModule != ImportList.getImportMap().end());
17971793
Expected<std::unique_ptr<Module>> SrcModuleOrErr = ModuleLoader(Name);
17981794
if (!SrcModuleOrErr)
17991795
return SrcModuleOrErr.takeError();

llvm/tools/llvm-link/llvm-link.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,8 @@ static bool importFunctions(const char *argv0, Module &DestModule) {
381381
// definition, so make the import type definition directly.
382382
// FIXME: A follow-up patch should add test coverage for import declaration
383383
// in `llvm-link` CLI (e.g., by introducing a new command line option).
384-
FunctionImporter::addDefinition(
385-
ImportList, FileNameStringCache.insert(FileName).first->getKey(),
386-
F->getGUID());
384+
ImportList.addDefinition(
385+
FileNameStringCache.insert(FileName).first->getKey(), F->getGUID());
387386
}
388387
auto CachedModuleLoader = [&](StringRef Identifier) {
389388
return ModuleLoaderCache.takeModule(std::string(Identifier));

0 commit comments

Comments
 (0)