Skip to content

Commit 3343020

Browse files
[ModuleInliner] Fix the heap maintenance (#69251)
With expensive checks enabled but without this patch, std::pop_heap triggers an assertion failure. This is because: updateAndCheckDecreased(Heap.front()) updates the priority associated with Heap.front(), so Heap may no longer be a valid heap. The libstdc++ version of std::pop_heap requires that the entire range be a valid heap even though the core task of std::pop_heap is just to swap the Heap.front() and Heap.back(). This patch fixes the problem by: - calling std::pop_heap to swap Heap.front() and Heap.back(), - updating the priority of Heap.back(), and - inserting Heap.back() back into the heap. We could reduce the number of calls to updateAndCheckDecreased or the number of elements being moved, but I think it's important to fix the crash first. Credit to Ivan Kosarev for identifying the problem and Liqiang Tao for the analysis and initial version of the patch.
1 parent 279c155 commit 3343020

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

llvm/lib/Analysis/InlineOrder.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,12 @@ class PriorityInlineOrder : public InlineOrder<std::pair<CallBase *, int>> {
223223
// pushed right back into the heap. For simplicity, those cases where
224224
// the desirability of a call site increases are ignored here.
225225
void adjust() {
226-
while (updateAndCheckDecreased(Heap.front())) {
227-
std::pop_heap(Heap.begin(), Heap.end(), isLess);
226+
std::pop_heap(Heap.begin(), Heap.end(), isLess);
227+
while (updateAndCheckDecreased(Heap.back())) {
228228
std::push_heap(Heap.begin(), Heap.end(), isLess);
229+
std::pop_heap(Heap.begin(), Heap.end(), isLess);
229230
}
231+
std::push_heap(Heap.begin(), Heap.end(), isLess);
230232
}
231233

232234
public:

0 commit comments

Comments
 (0)