From: Mehdi Amini Date: Sat, 8 Oct 2016 04:44:23 +0000 (+0000) Subject: ThinLTO: don't perform incremental LTO on module without a hash X-Git-Tag: llvmorg-4.0.0-rc1~7731 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f82bda0a7ac91c18051f346b719c7cf6faadbc00;p=platform%2Fupstream%2Fllvm.git ThinLTO: don't perform incremental LTO on module without a hash Clang always emit a hash for ThinLTO, but as other frontend are starting to use ThinLTO, this could be a serious bug. Differential Revision: https://reviews.llvm.org/D25379 llvm-svn: 283655 --- diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp index 94a4abf..8d23f53 100644 --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -542,8 +542,12 @@ public: }; auto ModuleID = MBRef.getBufferIdentifier(); - if (!Cache || !CombinedIndex.modulePaths().count(ModuleID)) - // Cache disabled or no entry for this module in the combined index + + if (!Cache || !CombinedIndex.modulePaths().count(ModuleID) || + all_of(CombinedIndex.getModuleHash(ModuleID), + [](uint32_t V) { return V == 0; })) + // Cache disabled or no entry for this module in the combined index or + // no module hash. return RunThinBackend(AddStream); SmallString<40> Key; diff --git a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp index 8afe13d..10af987 100644 --- a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp +++ b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp @@ -243,6 +243,13 @@ public: // export list, the hash for every single module in the import list, the // list of ResolvedODR for the module, and the list of preserved symbols. + // Include the hash for the current module + auto ModHash = Index.getModuleHash(ModuleID); + + if (all_of(ModHash, [](uint32_t V) { return V == 0; })) + // No hash entry, no caching! + return; + SHA1 Hasher; // Start with the compiler revision @@ -251,8 +258,6 @@ public: Hasher.update(LLVM_REVISION); #endif - // Include the hash for the current module - auto ModHash = Index.getModuleHash(ModuleID); Hasher.update(ArrayRef((uint8_t *)&ModHash[0], sizeof(ModHash))); for (auto F : ExportList) // The export list can impact the internalization, be conservative here diff --git a/llvm/test/ThinLTO/X86/cache.ll b/llvm/test/ThinLTO/X86/cache.ll index 239cfdf..b796b00 100644 --- a/llvm/test/ThinLTO/X86/cache.ll +++ b/llvm/test/ThinLTO/X86/cache.ll @@ -1,6 +1,28 @@ +; Verify first that *without* hash, we don't use the cache. + ; RUN: opt -module-summary %s -o %t.bc ; RUN: opt -module-summary %p/Inputs/cache.ll -o %t2.bc +; Verify that enabling caching is ignoring module without hash +; RUN: rm -Rf %t.cache && mkdir %t.cache +; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache +; RUN: ls %t.cache/llvmcache.timestamp +; RUN: ls %t.cache | count 1 + +; Verify that enabling caching is ignoring module without hash with llvm-lto2 +; RUN: rm -Rf %t.cache && mkdir %t.cache +; RUN: llvm-lto2 -o %t.o %t2.bc %t.bc -cache-dir %t.cache \ +; RUN: -r=%t2.bc,_main,plx \ +; RUN: -r=%t2.bc,_globalfunc,lx \ +; RUN: -r=%t.bc,_globalfunc,plx +; RUN: ls %t.cache | count 0 + + +; Repeat again, *with* hash this time. + +; RUN: opt -module-hash -module-summary %s -o %t.bc +; RUN: opt -module-hash -module-summary %p/Inputs/cache.ll -o %t2.bc + ; Verify that enabling caching is working ; RUN: rm -Rf %t.cache && mkdir %t.cache ; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache diff --git a/llvm/test/ThinLTO/X86/empty_module_with_cache.ll b/llvm/test/ThinLTO/X86/empty_module_with_cache.ll index c627451..3e16c39 100644 --- a/llvm/test/ThinLTO/X86/empty_module_with_cache.ll +++ b/llvm/test/ThinLTO/X86/empty_module_with_cache.ll @@ -22,13 +22,13 @@ ; RUN: rm -Rf %t.cache && mkdir %t.cache ; RUN: llvm-lto -thinlto-action=run %t2.bc %t.bc -exported-symbol=main -thinlto-cache-dir %t.cache ; RUN: ls %t.cache/llvmcache.timestamp -; RUN: ls %t.cache | count 2 +; RUN: ls %t.cache | count 1 ; Verify that caching is disabled for module without hash, with llvm-lto2 ; RUN: rm -Rf %t.cache && mkdir %t.cache ; RUN: llvm-lto2 -o %t.o %t2.bc %t.bc -cache-dir %t.cache \ ; RUN: -r=%t2.bc,_main,plx -; RUN: ls %t.cache | count 1 +; RUN: ls %t.cache | count 0 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"