Skip to content

Commit f11b5e7

Browse files
akyrtzibenlangmuir
authored andcommitted
[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 (cherry picked from commit bacb45e)
1 parent c0b1a05 commit f11b5e7

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
@@ -1502,6 +1502,13 @@ class ModuleSummaryIndex {
15021502
return &*It;
15031503
}
15041504

1505+
/// Return module entry for module with the given \p ModPath.
1506+
const ModuleInfo *getModule(StringRef ModPath) const {
1507+
auto It = ModulePathStringTable.find(ModPath);
1508+
assert(It != ModulePathStringTable.end() && "Module not registered");
1509+
return &*It;
1510+
}
1511+
15051512
/// Check if the given Module has any functions available for exporting
15061513
/// in the index. We consider any module present in the ModulePathStringTable
15071514
/// to have exported functions.

llvm/lib/LTO/LTO.cpp

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,22 +165,38 @@ void llvm::computeLTOCacheKey(
165165
// imported symbols for each module may affect code generation and is
166166
// sensitive to link order, so include that as well.
167167
using ImportMapIteratorTy = FunctionImporter::ImportMapTy::const_iterator;
168-
std::vector<ImportMapIteratorTy> ImportModulesVector;
168+
struct ImportModule {
169+
ImportMapIteratorTy ModIt;
170+
const ModuleSummaryIndex::ModuleInfo *ModInfo;
171+
172+
StringRef getIdentifier() const { return ModIt->getKey(); }
173+
const FunctionImporter::FunctionsToImportTy &getFunctions() const {
174+
return ModIt->second;
175+
}
176+
177+
const ModuleHash &getHash() const { return ModInfo->second.second; }
178+
uint64_t getId() const { return ModInfo->second.first; }
179+
};
180+
181+
std::vector<ImportModule> ImportModulesVector;
169182
ImportModulesVector.reserve(ImportList.size());
170183

171184
for (ImportMapIteratorTy It = ImportList.begin(); It != ImportList.end();
172185
++It) {
173-
ImportModulesVector.push_back(It);
186+
ImportModulesVector.push_back({It, Index.getModule(It->getKey())});
174187
}
188+
// Order using moduleId integer which is based on the order the module was
189+
// added.
175190
llvm::sort(ImportModulesVector,
176-
[](const ImportMapIteratorTy &Lhs, const ImportMapIteratorTy &Rhs)
177-
-> bool { return Lhs->getKey() < Rhs->getKey(); });
178-
for (const ImportMapIteratorTy &EntryIt : ImportModulesVector) {
179-
auto ModHash = Index.getModuleHash(EntryIt->first());
191+
[](const ImportModule &Lhs, const ImportModule &Rhs) -> bool {
192+
return Lhs.getId() < Rhs.getId();
193+
});
194+
for (const ImportModule &Entry : ImportModulesVector) {
195+
auto ModHash = Entry.getHash();
180196
Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
181197

182-
AddUint64(EntryIt->second.size());
183-
for (auto &Fn : EntryIt->second)
198+
AddUint64(Entry.getFunctions().size());
199+
for (auto &Fn : Entry.getFunctions())
184200
AddUint64(Fn);
185201
}
186202

@@ -250,9 +266,10 @@ void llvm::computeLTOCacheKey(
250266

251267
// Imported functions may introduce new uses of type identifier resolutions,
252268
// so we need to collect their used resolutions as well.
253-
for (auto &ImpM : ImportList)
254-
for (auto &ImpF : ImpM.second) {
255-
GlobalValueSummary *S = Index.findSummaryInModule(ImpF, ImpM.first());
269+
for (const ImportModule &ImpM : ImportModulesVector)
270+
for (auto &ImpF : ImpM.getFunctions()) {
271+
GlobalValueSummary *S =
272+
Index.findSummaryInModule(ImpF, ImpM.getIdentifier());
256273
AddUsedThings(S);
257274
// If this is an alias, we also care about any types/etc. that the aliasee
258275
// 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)