Skip to content

Reland "[ThinLTO] Populate declaration import status except for distributed ThinLTO under a default-off new option" #92718

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 25 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
78fa2e0
[ThinLTO]Record import type (declaration or definition) in GlobalValu…
mingmingl-llvm Apr 4, 2024
4b4c33f
[ThinLTO]Generate import type in bitcode writer
mingmingl-llvm Apr 4, 2024
cfb63d7
function import changes
mingmingl-llvm Apr 4, 2024
b0a4060
'git merge main' and resolve merge conflict
mingmingl-llvm Apr 11, 2024
e07d423
Merge branch 'main' into users/minglotus-6/spr/summary1
mingmingl-llvm Apr 11, 2024
175febc
rename Dec to Decl
mingmingl-llvm Apr 16, 2024
8bc4d7a
Merge branch 'main' into users/minglotus-6/spr/summary1
mingmingl-llvm Apr 16, 2024
7252e11
Merge branch 'users/minglotus-6/spr/summary1' into users/minglotus-6/…
mingmingl-llvm Apr 16, 2024
23a3fa4
simplify code
mingmingl-llvm Apr 17, 2024
a0277b4
Flag gate the import-declaration change.
mingmingl-llvm Apr 17, 2024
5edeccc
stage changes
mingmingl-llvm Apr 23, 2024
a139e4a
Add test coverage for import delcaration
mingmingl-llvm May 2, 2024
916dc96
add test coverage for function alias
mingmingl-llvm May 3, 2024
248fbfb
llvm-lto test coverage
mingmingl-llvm May 5, 2024
5e731ec
add test coverage for debugging log, and a RUN line for internalization
mingmingl-llvm May 6, 2024
c5e168e
Merge branch 'main' into users/minglotus-6/spr/summary1
mingmingl-llvm May 7, 2024
2c38b47
resolve review feedback
mingmingl-llvm May 7, 2024
3da9b8b
Merge branch 'users/minglotus-6/spr/summary1' into users/minglotus-6/…
mingmingl-llvm May 7, 2024
f1d22e1
Diffbase is updated (https://github.com/llvm/llvm-project/pull/87600/…
mingmingl-llvm May 7, 2024
86f3f44
resolve review feedback
mingmingl-llvm May 10, 2024
ce353d0
resolve feedback
mingmingl-llvm May 15, 2024
ee91670
use 'DAG' for log lines and resolve review feedback
mingmingl-llvm May 16, 2024
dd35621
merge main to pick up PR 92622
mingmingl-llvm May 19, 2024
3456c50
clang-format; and use the lexicographic comparison of std::pair
mingmingl-llvm May 20, 2024
b9f04d1
Use one thread for in-process ThinLTO to test debug logs
mingmingl-llvm May 20, 2024
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
7 changes: 7 additions & 0 deletions llvm/include/llvm/IR/ModuleSummaryIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,10 @@ class GlobalValueSummary {

void setImportKind(ImportKind IK) { Flags.ImportType = IK; }

GlobalValueSummary::ImportKind importType() const {
return static_cast<ImportKind>(Flags.ImportType);
}

GlobalValue::VisibilityTypes getVisibility() const {
return (GlobalValue::VisibilityTypes)Flags.Visibility;
}
Expand Down Expand Up @@ -1272,6 +1276,9 @@ using ModulePathStringTableTy = StringMap<ModuleHash>;
/// a particular module, and provide efficient access to their summary.
using GVSummaryMapTy = DenseMap<GlobalValue::GUID, GlobalValueSummary *>;

/// A set of global value summary pointers.
using GVSummaryPtrSet = SmallPtrSet<GlobalValueSummary *, 4>;

/// Map of a type GUID to type id string and summary (multimap used
/// in case of GUID conflicts).
using TypeIdSummaryMapTy =
Expand Down
15 changes: 10 additions & 5 deletions llvm/include/llvm/Transforms/IPO/FunctionImport.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class Module;
/// based on the provided summary informations.
class FunctionImporter {
public:
/// Set of functions to import from a source module. Each entry is a set
/// containing all the GUIDs of all functions to import for a source module.
using FunctionsToImportTy = std::unordered_set<GlobalValue::GUID>;
/// The functions to import from a source module and their import type.
using FunctionsToImportTy =
DenseMap<GlobalValue::GUID, GlobalValueSummary::ImportKind>;

/// The different reasons selectCallee will chose not to import a
/// candidate.
Expand Down Expand Up @@ -99,8 +99,13 @@ class FunctionImporter {
/// index's module path string table).
using ImportMapTy = DenseMap<StringRef, FunctionsToImportTy>;

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

/// A function of this type is used to load modules referenced by the index.
using ModuleLoaderTy =
Expand Down
32 changes: 20 additions & 12 deletions llvm/lib/LTO/LTO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ void llvm::computeLTOCacheKey(
support::endian::write64le(Data, I);
Hasher.update(Data);
};
auto AddUint8 = [&](const uint8_t I) {
Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&I, 1));
};
AddString(Conf.CPU);
// FIXME: Hash more of Options. For now all clients initialize Options from
// command-line flags (which is unsupported in production), but may set
Expand Down Expand Up @@ -156,18 +159,18 @@ void llvm::computeLTOCacheKey(
auto ModHash = Index.getModuleHash(ModuleID);
Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));

std::vector<uint64_t> ExportsGUID;
std::vector<std::pair<uint64_t, uint8_t>> ExportsGUID;
ExportsGUID.reserve(ExportList.size());
for (const auto &VI : ExportList) {
auto GUID = VI.getGUID();
ExportsGUID.push_back(GUID);
}
for (const auto &[VI, ExportType] : ExportList)
ExportsGUID.push_back(
std::make_pair(VI.getGUID(), static_cast<uint8_t>(ExportType)));

// Sort the export list elements GUIDs.
llvm::sort(ExportsGUID);
for (uint64_t GUID : ExportsGUID) {
for (auto [GUID, ExportType] : ExportsGUID) {
// The export list can impact the internalization, be conservative here
Hasher.update(ArrayRef<uint8_t>((uint8_t *)&GUID, sizeof(GUID)));
AddUint8(ExportType);
}

// Include the hash for every module we import functions from. The set of
Expand Down Expand Up @@ -199,19 +202,21 @@ void llvm::computeLTOCacheKey(
[](const ImportModule &Lhs, const ImportModule &Rhs) -> bool {
return Lhs.getHash() < Rhs.getHash();
});
std::vector<uint64_t> ImportedGUIDs;
std::vector<std::pair<uint64_t, uint8_t>> ImportedGUIDs;
for (const ImportModule &Entry : ImportModulesVector) {
auto ModHash = Entry.getHash();
Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));

AddUint64(Entry.getFunctions().size());

ImportedGUIDs.clear();
for (auto &Fn : Entry.getFunctions())
ImportedGUIDs.push_back(Fn);
for (auto &[Fn, ImportType] : Entry.getFunctions())
ImportedGUIDs.push_back(std::make_pair(Fn, ImportType));
llvm::sort(ImportedGUIDs);
for (auto &GUID : ImportedGUIDs)
for (auto &[GUID, Type] : ImportedGUIDs) {
AddUint64(GUID);
AddUint8(Type);
}
}

// Include the hash for the resolved ODR.
Expand Down Expand Up @@ -281,9 +286,9 @@ void llvm::computeLTOCacheKey(
// Imported functions may introduce new uses of type identifier resolutions,
// so we need to collect their used resolutions as well.
for (const ImportModule &ImpM : ImportModulesVector)
for (auto &ImpF : ImpM.getFunctions()) {
for (auto &[GUID, UnusedImportType] : ImpM.getFunctions()) {
GlobalValueSummary *S =
Index.findSummaryInModule(ImpF, ImpM.getIdentifier());
Index.findSummaryInModule(GUID, ImpM.getIdentifier());
AddUsedThings(S);
// If this is an alias, we also care about any types/etc. that the aliasee
// may reference.
Expand Down Expand Up @@ -1395,6 +1400,7 @@ class lto::ThinBackendProc {
llvm::StringRef ModulePath,
const std::string &NewModulePath) {
std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;

std::error_code EC;
gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
ImportList, ModuleToSummariesForIndex);
Expand All @@ -1403,6 +1409,8 @@ class lto::ThinBackendProc {
sys::fs::OpenFlags::OF_None);
if (EC)
return errorCodeToError(EC);

// TODO: Serialize declaration bits to bitcode.
writeIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex);

if (ShouldEmitImportsFiles) {
Expand Down
9 changes: 8 additions & 1 deletion llvm/lib/LTO/LTOBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,14 @@ bool lto::initImportList(const Module &M,
if (Summary->modulePath() == M.getModuleIdentifier())
continue;
// Add an entry to provoke importing by thinBackend.
ImportList[Summary->modulePath()].insert(GUID);
// Try emplace the entry first. If an entry with the same key already
// exists, set the value to 'std::min(existing-value, new-value)' to make
// sure a definition takes precedence over a declaration.
auto [Iter, Inserted] = ImportList[Summary->modulePath()].try_emplace(
GUID, Summary->importType());

if (!Inserted)
Iter->second = std::min(Iter->second, Summary->importType());
}
}
return true;
Expand Down
Loading
Loading