Skip to content

Commit cff7a77

Browse files
nikictru
authored andcommitted
[ThinLTO] Use module hash instead of module ID for cache key
This is a followup to D151165. Instead of using the module ID, use the module hash for sorting the import list. The module hash is what will actually be included in the hash. This has the advantage of being independent of the module order, which is something that Rust relies on. A caveat here is that the test doesn't quite work for linkonce_odr functions, because the function may be imported from two different modules, and the first one on the llvm-lto2 command line gets picked (rather than, say, the prevailing copy). This doesn't really matter for Rust's purposes (because it does not use linkonce_odr linkage), but may still be worth addressing. For now I'm using a variant of the test using internal instead of linkonce_odr functions. Differential Revision: https://reviews.llvm.org/D156525 (cherry picked from commit 279c297)
1 parent bafedb1 commit cff7a77

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

llvm/lib/LTO/LTO.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ void llvm::computeLTOCacheKey(
184184
}
185185

186186
const ModuleHash &getHash() const { return ModInfo->second.second; }
187-
uint64_t getId() const { return ModInfo->second.first; }
188187
};
189188

190189
std::vector<ImportModule> ImportModulesVector;
@@ -194,11 +193,11 @@ void llvm::computeLTOCacheKey(
194193
++It) {
195194
ImportModulesVector.push_back({It, Index.getModule(It->getKey())});
196195
}
197-
// Order using moduleId integer which is based on the order the module was
198-
// added.
196+
// Order using module hash, to be both independent of module name and
197+
// module order.
199198
llvm::sort(ImportModulesVector,
200199
[](const ImportModule &Lhs, const ImportModule &Rhs) -> bool {
201-
return Lhs.getId() < Rhs.getId();
200+
return Lhs.getHash() < Rhs.getHash();
202201
});
203202
for (const ImportModule &Entry : ImportModulesVector) {
204203
auto ModHash = Entry.getHash();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
2+
target triple = "x86_64-unknown-linux-gnu"
3+
4+
define void @f1() {
5+
call void @internal()
6+
ret void
7+
}
8+
9+
define internal void @internal() {
10+
ret void
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
2+
target triple = "x86_64-unknown-linux-gnu"
3+
4+
define void @f2() {
5+
call void @internal()
6+
ret void
7+
}
8+
9+
define internal void @internal() {
10+
ret void
11+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
; RUN: rm -rf %t && mkdir -p %t
2+
; RUN: opt -module-hash -module-summary %s -o %t/t.bc
3+
; RUN: opt -module-hash -module-summary %S/Inputs/cache-import-lists3.ll -o %t/a.bc
4+
; RUN: opt -module-hash -module-summary %S/Inputs/cache-import-lists4.ll -o %t/b.bc
5+
6+
; Tests that the hash for t is insensitive to the order of the modules.
7+
8+
; RUN: llvm-lto2 run -cache-dir %t/cache -o %t.o %t/t.bc %t/a.bc %t/b.bc -r=%t/t.bc,main,plx -r=%t/t.bc,f1,lx -r=%t/t.bc,f2,lx -r=%t/a.bc,f1,plx -r=%t/b.bc,f2,plx
9+
; RUN: ls %t/cache | count 3
10+
11+
; RUN: llvm-lto2 run -cache-dir %t/cache -o %t.o %t/b.bc %t/a.bc %t/t.bc -r=%t/t.bc,main,plx -r=%t/t.bc,f1,lx -r=%t/t.bc,f2,lx -r=%t/a.bc,f1,plx -r=%t/b.bc,f2,plx
12+
; RUN: ls %t/cache | count 3
13+
14+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
15+
target triple = "x86_64-unknown-linux-gnu"
16+
17+
define void @main() {
18+
call void @f1()
19+
call void @f2()
20+
ret void
21+
}
22+
23+
declare void @f1()
24+
declare void @f2()

0 commit comments

Comments
 (0)