Skip to content

Commit 7a276ba

Browse files
[ThinLTO] Track definitions only in export-set
1 parent 9b94056 commit 7a276ba

File tree

3 files changed

+38
-37
lines changed

3 files changed

+38
-37
lines changed

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,9 @@ class FunctionImporter {
104104
/// index's module path string table).
105105
using ImportMapTy = DenseMap<StringRef, FunctionsToImportTy>;
106106

107-
/// The map contains an entry for every global value the module exports.
108-
/// The key is ValueInfo, and the value indicates whether the definition
109-
/// or declaration is visible to another module. If a function's definition is
110-
/// visible to other modules, the global values this function referenced are
111-
/// visible and shouldn't be internalized.
112-
/// TODO: Rename to `ExportMapTy`.
113-
using ExportSetTy = DenseMap<ValueInfo, GlobalValueSummary::ImportKind>;
107+
/// The set contains an entry for every global value the module exports as
108+
// a definition.
109+
using ExportSetTy = DenseSet<ValueInfo>;
114110

115111
/// A function of this type is used to load modules referenced by the index.
116112
using ModuleLoaderTy =

llvm/lib/LTO/LTO.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,19 +161,15 @@ void llvm::computeLTOCacheKey(
161161
auto ModHash = Index.getModuleHash(ModuleID);
162162
Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
163163

164-
std::vector<std::pair<uint64_t, uint8_t>> ExportsGUID;
164+
std::vector<uint64_t> ExportsGUID;
165165
ExportsGUID.reserve(ExportList.size());
166-
for (const auto &[VI, ExportType] : ExportList)
167-
ExportsGUID.push_back(
168-
std::make_pair(VI.getGUID(), static_cast<uint8_t>(ExportType)));
166+
for (const auto &VI : ExportList)
167+
ExportsGUID.push_back(VI.getGUID());
169168

170169
// Sort the export list elements GUIDs.
171170
llvm::sort(ExportsGUID);
172-
for (auto [GUID, ExportType] : ExportsGUID) {
173-
// The export list can impact the internalization, be conservative here
171+
for (auto GUID : ExportsGUID)
174172
Hasher.update(ArrayRef<uint8_t>((uint8_t *)&GUID, sizeof(GUID)));
175-
AddUint8(ExportType);
176-
}
177173

178174
// Include the hash for every module we import functions from. The set of
179175
// imported symbols for each module may affect code generation and is

llvm/lib/Transforms/IPO/FunctionImport.cpp

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,7 @@ class GlobalsImporter final {
400400
// later, in ComputeCrossModuleImport, after import decisions are
401401
// complete, which is more efficient than adding them here.
402402
if (ExportLists)
403-
(*ExportLists)[RefSummary->modulePath()][VI] =
404-
GlobalValueSummary::Definition;
403+
(*ExportLists)[RefSummary->modulePath()].insert(VI);
405404

406405
// If variable is not writeonly we attempt to recursively analyze
407406
// its references in order to import referenced constants.
@@ -582,7 +581,7 @@ class WorkloadImportsManager : public ModuleImportsManager {
582581
GlobalValueSummary::Definition;
583582
GVI.onImportingSummary(*GVS);
584583
if (ExportLists)
585-
(*ExportLists)[ExportingModule][VI] = GlobalValueSummary::Definition;
584+
(*ExportLists)[ExportingModule].insert(VI);
586585
}
587586
LLVM_DEBUG(dbgs() << "[Workload] Done\n");
588587
}
@@ -819,9 +818,6 @@ static void computeImportForFunction(
819818
// try emplace <VI, declaration> pair without checking insert result.
820819
// If insert doesn't happen, there must be an existing entry keyed by
821820
// VI.
822-
if (ExportLists)
823-
(*ExportLists)[DeclSourceModule].try_emplace(
824-
VI, GlobalValueSummary::Declaration);
825821
ImportList[DeclSourceModule].try_emplace(
826822
VI.getGUID(), GlobalValueSummary::Declaration);
827823
}
@@ -892,7 +888,7 @@ static void computeImportForFunction(
892888
// later, in ComputeCrossModuleImport, after import decisions are
893889
// complete, which is more efficient than adding them here.
894890
if (ExportLists)
895-
(*ExportLists)[ExportModulePath][VI] = GlobalValueSummary::Definition;
891+
(*ExportLists)[ExportModulePath].insert(VI);
896892
}
897893

898894
auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) {
@@ -998,10 +994,27 @@ static bool isGlobalVarSummary(const ModuleSummaryIndex &Index,
998994
return false;
999995
}
1000996

1001-
template <class T>
1002-
static unsigned numGlobalVarSummaries(const ModuleSummaryIndex &Index, T &Cont,
1003-
unsigned &DefinedGVS,
1004-
unsigned &DefinedFS) {
997+
static unsigned
998+
numGlobalVarSummariesInSet(const ModuleSummaryIndex &Index,
999+
FunctionImporter::ExportSetTy &ExportSet,
1000+
unsigned &DefinedGVS, unsigned &DefinedFS) {
1001+
unsigned NumGVS = 0;
1002+
DefinedGVS = 0;
1003+
DefinedFS = 0;
1004+
for (auto &VI : ExportSet) {
1005+
if (isGlobalVarSummary(Index, VI.getGUID())) {
1006+
++DefinedGVS;
1007+
++NumGVS;
1008+
} else
1009+
++DefinedFS;
1010+
}
1011+
return NumGVS;
1012+
}
1013+
1014+
static unsigned
1015+
numGlobalVarSummaries(const ModuleSummaryIndex &Index,
1016+
FunctionImporter::FunctionsToImportTy &Cont,
1017+
unsigned &DefinedGVS, unsigned &DefinedFS) {
10051018
unsigned NumGVS = 0;
10061019
DefinedGVS = 0;
10071020
DefinedFS = 0;
@@ -1046,7 +1059,7 @@ static bool checkVariableImport(
10461059
};
10471060

10481061
for (auto &ExportPerModule : ExportLists)
1049-
for (auto &[VI, Unused] : ExportPerModule.second)
1062+
for (auto &VI : ExportPerModule.second)
10501063
if (!FlattenedImports.count(VI.getGUID()) &&
10511064
IsReadOrWriteOnlyVarNeedingImporting(ExportPerModule.first, VI))
10521065
return false;
@@ -1082,11 +1095,7 @@ void llvm::ComputeCrossModuleImport(
10821095
FunctionImporter::ExportSetTy NewExports;
10831096
const auto &DefinedGVSummaries =
10841097
ModuleToDefinedGVSummaries.lookup(ELI.first);
1085-
for (auto &[EI, Type] : ELI.second) {
1086-
// If a variable is exported as a declaration, its 'refs' and 'calls' are
1087-
// not further exported.
1088-
if (Type == GlobalValueSummary::Declaration)
1089-
continue;
1098+
for (auto &EI : ELI.second) {
10901099
// Find the copy defined in the exporting module so that we can mark the
10911100
// values it references in that specific definition as exported.
10921101
// Below we will add all references and called values, without regard to
@@ -1108,19 +1117,19 @@ void llvm::ComputeCrossModuleImport(
11081117
for (const auto &VI : GVS->refs()) {
11091118
// Try to emplace the declaration entry. If a definition entry
11101119
// already exists for key `VI`, this is a no-op.
1111-
NewExports.try_emplace(VI, GlobalValueSummary::Declaration);
1120+
NewExports.insert(VI);
11121121
}
11131122
} else {
11141123
auto *FS = cast<FunctionSummary>(S);
11151124
for (const auto &Edge : FS->calls()) {
11161125
// Try to emplace the declaration entry. If a definition entry
11171126
// already exists for key `VI`, this is a no-op.
1118-
NewExports.try_emplace(Edge.first, GlobalValueSummary::Declaration);
1127+
NewExports.insert(Edge.first);
11191128
}
11201129
for (const auto &Ref : FS->refs()) {
11211130
// Try to emplace the declaration entry. If a definition entry
11221131
// already exists for key `VI`, this is a no-op.
1123-
NewExports.try_emplace(Ref, GlobalValueSummary::Declaration);
1132+
NewExports.insert(Ref);
11241133
}
11251134
}
11261135
}
@@ -1129,7 +1138,7 @@ void llvm::ComputeCrossModuleImport(
11291138
// the same ref/call target multiple times in above loop, and it is more
11301139
// efficient to avoid a set lookup each time.
11311140
for (auto EI = NewExports.begin(); EI != NewExports.end();) {
1132-
if (!DefinedGVSummaries.count(EI->first.getGUID()))
1141+
if (!DefinedGVSummaries.count(EI->getGUID()))
11331142
NewExports.erase(EI++);
11341143
else
11351144
++EI;
@@ -1146,7 +1155,7 @@ void llvm::ComputeCrossModuleImport(
11461155
auto &Exports = ExportLists[ModName];
11471156
unsigned DefinedGVS = 0, DefinedFS = 0;
11481157
unsigned NumGVS =
1149-
numGlobalVarSummaries(Index, Exports, DefinedGVS, DefinedFS);
1158+
numGlobalVarSummariesInSet(Index, Exports, DefinedGVS, DefinedFS);
11501159
LLVM_DEBUG(dbgs() << "* Module " << ModName << " exports " << DefinedFS
11511160
<< " function as definitions, "
11521161
<< Exports.size() - NumGVS - DefinedFS

0 commit comments

Comments
 (0)