Skip to content

Commit 8f299fd

Browse files
romanova-ekaterinatstellar
authored andcommitted
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 (cherry picked from commit 252892f)
1 parent a8eb6a5 commit 8f299fd

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
@@ -147,21 +147,41 @@ void llvm::computeLTOCacheKey(
147147
// Include the hash for the current module
148148
auto ModHash = Index.getModuleHash(ModuleID);
149149
Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
150+
151+
std::vector<uint64_t> ExportsGUID;
152+
ExportsGUID.reserve(ExportList.size());
150153
for (const auto &VI : ExportList) {
151154
auto GUID = VI.getGUID();
155+
ExportsGUID.push_back(GUID);
156+
}
157+
158+
// Sort the export list elements GUIDs.
159+
llvm::sort(ExportsGUID);
160+
for (uint64_t GUID : ExportsGUID) {
152161
// The export list can impact the internalization, be conservative here
153162
Hasher.update(ArrayRef<uint8_t>((uint8_t *)&GUID, sizeof(GUID)));
154163
}
155164

156165
// Include the hash for every module we import functions from. The set of
157166
// imported symbols for each module may affect code generation and is
158167
// sensitive to link order, so include that as well.
159-
for (auto &Entry : ImportList) {
160-
auto ModHash = Index.getModuleHash(Entry.first());
168+
using ImportMapIteratorTy = FunctionImporter::ImportMapTy::const_iterator;
169+
std::vector<ImportMapIteratorTy> ImportModulesVector;
170+
ImportModulesVector.reserve(ImportList.size());
171+
172+
for (ImportMapIteratorTy It = ImportList.begin(); It != ImportList.end();
173+
++It) {
174+
ImportModulesVector.push_back(It);
175+
}
176+
llvm::sort(ImportModulesVector,
177+
[](const ImportMapIteratorTy &Lhs, const ImportMapIteratorTy &Rhs)
178+
-> bool { return Lhs->getKey() < Rhs->getKey(); });
179+
for (const ImportMapIteratorTy &EntryIt : ImportModulesVector) {
180+
auto ModHash = Index.getModuleHash(EntryIt->first());
161181
Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
162182

163-
AddUint64(Entry.second.size());
164-
for (auto &Fn : Entry.second)
183+
AddUint64(EntryIt->second.size());
184+
for (auto &Fn : EntryIt->second)
165185
AddUint64(Fn);
166186
}
167187

0 commit comments

Comments
 (0)