Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9f4e8bc

Browse files
authored
[lld][BP] Fix nondeterministic function order by using MapVector (#179841)
There are two places where the nondeterministic iteration order of `DenseMap` (the original type of `startupSectionIdxUNs`) causes the output function order to be nondeterministic. https://github.com/llvm/llvm-project/blob/16e2e7f59134e63810228c9a0dc990bfd96f9a1f/lld/include/lld/Common/BPSectionOrdererBase.inc#L240-L245 https://github.com/llvm/llvm-project/blob/16e2e7f59134e63810228c9a0dc990bfd96f9a1f/lld/include/lld/Common/BPSectionOrdererBase.inc#L267-L268 The fix is to use `MapVector` whose iteration order is guaranteed to be deterministic. To test, I built a large binary several times and observed this value no longer changes. https://github.com/llvm/llvm-project/blob/16e2e7f59134e63810228c9a0dc990bfd96f9a1f/lld/include/lld/Common/BPSectionOrdererBase.inc#L410-L411 It seems that this regresses linktime by a few seconds, but I believe the tradeoff is worthwhile.
1 parent 544caa6 commit 9f4e8bc

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

lld/include/lld/Common/BPSectionOrdererBase.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ auto BPOrderer<D>::computeOrder(
161161
sectionToIdx.try_emplace(isec, i);
162162

163163
BPFunctionNode::UtilityNodeT maxUN = 0;
164-
DenseMap<unsigned, UtilityNodes> startupSectionIdxUNs;
164+
MapVector<unsigned, UtilityNodes> startupSectionIdxUNs;
165165
// Used to define the initial order for startup functions.
166166
DenseMap<unsigned, size_t> sectionIdxToTimestamp;
167167
std::unique_ptr<InstrProfReader> reader;
@@ -177,7 +177,7 @@ auto BPOrderer<D>::computeOrder(
177177
}
178178
auto &traces = reader->getTemporalProfTraces();
179179

180-
DenseMap<unsigned, BPFunctionNode::UtilityNodeT> sectionIdxToFirstUN;
180+
MapVector<unsigned, BPFunctionNode::UtilityNodeT> sectionIdxToFirstUN;
181181
for (size_t traceIdx = 0; traceIdx < traces.size(); traceIdx++) {
182182
uint64_t currentSize = 0, cutoffSize = 1;
183183
size_t cutoffTimestamp = 1;

0 commit comments

Comments
 (0)