Skip to content

Commit bacb45e

Browse files
committed
[ThinLTO] Make the cache key independent of the module identifier paths
Otherwise there are cache misses just from changing the name of a path, even though the input modules did not change. rdar://109672225 Differential Revision: https://reviews.llvm.org/D151165
1 parent 61bc3ad commit bacb45e

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

llvm/include/llvm/IR/ModuleSummaryIndex.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,6 +1728,13 @@ class ModuleSummaryIndex {
17281728
return &*It;
17291729
}
17301730

1731+
/// Return module entry for module with the given \p ModPath.
1732+
const ModuleInfo *getModule(StringRef ModPath) const {
1733+
auto It = ModulePathStringTable.find(ModPath);
1734+
assert(It != ModulePathStringTable.end() && "Module not registered");
1735+
return &*It;
1736+
}
1737+
17311738
/// Check if the given Module has any functions available for exporting
17321739
/// in the index. We consider any module present in the ModulePathStringTable
17331740
/// to have exported functions.

llvm/lib/LTO/LTO.cpp

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -174,22 +174,38 @@ void llvm::computeLTOCacheKey(
174174
// imported symbols for each module may affect code generation and is
175175
// sensitive to link order, so include that as well.
176176
using ImportMapIteratorTy = FunctionImporter::ImportMapTy::const_iterator;
177-
std::vector<ImportMapIteratorTy> ImportModulesVector;
177+
struct ImportModule {
178+
ImportMapIteratorTy ModIt;
179+
const ModuleSummaryIndex::ModuleInfo *ModInfo;
180+
181+
StringRef getIdentifier() const { return ModIt->getKey(); }
182+
const FunctionImporter::FunctionsToImportTy &getFunctions() const {
183+
return ModIt->second;
184+
}
185+
186+
const ModuleHash &getHash() const { return ModInfo->second.second; }
187+
uint64_t getId() const { return ModInfo->second.first; }
188+
};
189+
190+
std::vector<ImportModule> ImportModulesVector;
178191
ImportModulesVector.reserve(ImportList.size());
179192

180193
for (ImportMapIteratorTy It = ImportList.begin(); It != ImportList.end();
181194
++It) {
182-
ImportModulesVector.push_back(It);
195+
ImportModulesVector.push_back({It, Index.getModule(It->getKey())});
183196
}
197+
// Order using moduleId integer which is based on the order the module was
198+
// added.
184199
llvm::sort(ImportModulesVector,
185-
[](const ImportMapIteratorTy &Lhs, const ImportMapIteratorTy &Rhs)
186-
-> bool { return Lhs->getKey() < Rhs->getKey(); });
187-
for (const ImportMapIteratorTy &EntryIt : ImportModulesVector) {
188-
auto ModHash = Index.getModuleHash(EntryIt->first());
200+
[](const ImportModule &Lhs, const ImportModule &Rhs) -> bool {
201+
return Lhs.getId() < Rhs.getId();
202+
});
203+
for (const ImportModule &Entry : ImportModulesVector) {
204+
auto ModHash = Entry.getHash();
189205
Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
190206

191-
AddUint64(EntryIt->second.size());
192-
for (auto &Fn : EntryIt->second)
207+
AddUint64(Entry.getFunctions().size());
208+
for (auto &Fn : Entry.getFunctions())
193209
AddUint64(Fn);
194210
}
195211

@@ -259,9 +275,10 @@ void llvm::computeLTOCacheKey(
259275

260276
// Imported functions may introduce new uses of type identifier resolutions,
261277
// so we need to collect their used resolutions as well.
262-
for (auto &ImpM : ImportList)
263-
for (auto &ImpF : ImpM.second) {
264-
GlobalValueSummary *S = Index.findSummaryInModule(ImpF, ImpM.first());
278+
for (const ImportModule &ImpM : ImportModulesVector)
279+
for (auto &ImpF : ImpM.getFunctions()) {
280+
GlobalValueSummary *S =
281+
Index.findSummaryInModule(ImpF, ImpM.getIdentifier());
265282
AddUsedThings(S);
266283
// If this is an alias, we also care about any types/etc. that the aliasee
267284
// may reference.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
; RUN: rm -rf %t && mkdir -p %t/1 %t/2 %t/3 %t/4
2+
; RUN: opt -module-hash -module-summary %s -o %t/t.bc
3+
; RUN: opt -module-hash -module-summary %S/Inputs/cache-import-lists1.ll -o %t/1/a.bc
4+
; RUN: opt -module-hash -module-summary %S/Inputs/cache-import-lists2.ll -o %t/2/b.bc
5+
6+
; Tests that the hash for t is insensitive to the bitcode module filenames.
7+
8+
; RUN: rm -rf %t/cache
9+
; RUN: llvm-lto2 run -cache-dir %t/cache -o %t.o %t/t.bc %t/1/a.bc %t/2/b.bc -r=%t/t.bc,main,plx -r=%t/t.bc,f1,lx -r=%t/t.bc,f2,lx -r=%t/1/a.bc,f1,plx -r=%t/1/a.bc,linkonce_odr,plx -r=%t/2/b.bc,f2,plx -r=%t/2/b.bc,linkonce_odr,lx
10+
; RUN: ls %t/cache | count 3
11+
12+
; RUN: cp %t/1/a.bc %t/4/d.bc
13+
; RUN: cp %t/2/b.bc %t/3/k.bc
14+
; RUN: llvm-lto2 run -cache-dir %t/cache -o %t.o %t/t.bc %t/4/d.bc %t/3/k.bc -r=%t/t.bc,main,plx -r=%t/t.bc,f1,lx -r=%t/t.bc,f2,lx -r=%t/4/d.bc,f1,plx -r=%t/4/d.bc,linkonce_odr,plx -r=%t/3/k.bc,f2,plx -r=%t/3/k.bc,linkonce_odr,lx
15+
; RUN: ls %t/cache | count 3
16+
17+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
18+
target triple = "x86_64-unknown-linux-gnu"
19+
20+
define void @main() {
21+
call void @f1()
22+
call void @f2()
23+
ret void
24+
}
25+
26+
declare void @f1()
27+
declare void @f2()

0 commit comments

Comments
 (0)