[llvm] [ModuleInliner] use std::make_heap to construct the heap after element updated (PR #69206)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 16 13:53:39 PDT 2023


kazutakahirata wrote:

@taoliq, the following seems to work even with the expensive checks enabled.  I'll post this as separate PR myself:

```
    std::pop_heap(Heap.begin(), Heap.end(), isLess);
    while (updateAndCheckDecreased(Heap.back())) {
      std::push_heap(Heap.begin(), Heap.end(), isLess);
      std::pop_heap(Heap.begin(), Heap.end(), isLess);
    }
    std::push_heap(Heap.begin(), Heap.end(), isLess);
```

Although `pop_heap()` simply swaps the first and last element of the heap, the libstdc++ version (and possibly the C++ standard) requires that the entire range be a valid heap when expensive checks are enabled.

The code snippet above:

- swaps the first and last element while `Heap` is a valid heap,
- calls `updateAndCheckDecreased` on the last element, which is outside the valid heap, and
- put the last element back into the heap. 

In other words, we use the last element as a safe work area while we are updating its priority.

We could reduce the number of calls to `updateAndCheckDecreased` in certain situations, but let's take care of the crash first.

https://github.com/llvm/llvm-project/pull/69206


More information about the llvm-commits mailing list