Skip to content

Commit 252892f

Browse files
Fixed false ThinLTO cache misses problem (PR 45819).
We relied on the fact that the iterators walks through the elements of a DenseSet in a deterministic order (which is not true). This caused ThinLTO cache misses. This patch addresses this problem. See PR 45819 for additional information https://bugs.llvm.org/show_bug.cgi?id=45819 Differential Revision: https://reviews.llvm.org/D79772
1 parent 60fe25c commit 252892f

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

llvm/lib/LTO/LTO.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,41 @@ void llvm::computeLTOCacheKey(
139139
// Include the hash for the current module
140140
auto ModHash = Index.getModuleHash(ModuleID);
141141
Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
142+
143+
std::vector<uint64_t> ExportsGUID;
144+
ExportsGUID.reserve(ExportList.size());
142145
for (const auto &VI : ExportList) {
143146
auto GUID = VI.getGUID();
147+
ExportsGUID.push_back(GUID);
148+
}
149+
150+
// Sort the export list elements GUIDs.
151+
llvm::sort(ExportsGUID);
152+
for (uint64_t GUID : ExportsGUID) {
144153
// The export list can impact the internalization, be conservative here
145154
Hasher.update(ArrayRef<uint8_t>((uint8_t *)&GUID, sizeof(GUID)));
146155
}
147156

148157
// Include the hash for every module we import functions from. The set of
149158
// imported symbols for each module may affect code generation and is
150159
// sensitive to link order, so include that as well.
151-
for (auto &Entry : ImportList) {
152-
auto ModHash = Index.getModuleHash(Entry.first());
160+
using ImportMapIteratorTy = FunctionImporter::ImportMapTy::const_iterator;
161+
std::vector<ImportMapIteratorTy> ImportModulesVector;
162+
ImportModulesVector.reserve(ImportList.size());
163+
164+
for (ImportMapIteratorTy It = ImportList.begin(); It != ImportList.end();
165+
++It) {
166+
ImportModulesVector.push_back(It);
167+
}
168+
llvm::sort(ImportModulesVector,
169+
[](const ImportMapIteratorTy &Lhs, const ImportMapIteratorTy &Rhs)
170+
-> bool { return Lhs->getKey() < Rhs->getKey(); });
171+
for (const ImportMapIteratorTy &EntryIt : ImportModulesVector) {
172+
auto ModHash = Index.getModuleHash(EntryIt->first());
153173
Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
154174

155-
AddUint64(Entry.second.size());
156-
for (auto &Fn : Entry.second)
175+
AddUint64(EntryIt->second.size());
176+
for (auto &Fn : EntryIt->second)
157177
AddUint64(Fn);
158178
}
159179

0 commit comments

Comments
 (0)