[llvm] [ModuleInliner] Remove an extraneous pair of std::push_heap and std::pop_heap (NFC) (PR #69672)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 19 19:00:49 PDT 2023


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/69672

Immediately after the "while" loop in adjust, Heap.back() is
guaranteed to be the highest priority item for the current values of
Priorities.  std::push_back() at the end of adjust moves the highest
priority item to Heap.front(), but std::pop_heap() in pop moves it
right back to Heap.back().

This roundtrip is wasteful.  This patch removes the extraneous pair of
std::push_heap and std::pop_heap.  This patch cuts down about 45% of
calls to std::push_heap and std::pop_heap in InlineOrder.cpp while
building clang with FDO+ThinLTO.

Strictly speaking, removing the pair of calls may change the order in
which call sites with identical priorities are removed from the
priority queue, but we do not need to worry about that.

Since the functionality of adjust becomes more like a smart version of
pop_heap, this patch renames adjust to pop_heap_adjust.


>From 33354a11497e808f99e203513540d5379dd27811 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Thu, 19 Oct 2023 14:10:31 -0700
Subject: [PATCH] [ModuleInliner] Remove an extraneous pair of std::push_heap
 and std::pop_heap (NFC)

Immediately after the "while" loop in adjust, Heap.back() is
guaranteed to be the highest priority item for the current values of
Priorities.  std::push_back() at the end of adjust moves the highest
priority item to Heap.front(), but std::pop_heap() in pop moves it
right back to Heap.back().

This roundtrip is wasteful.  This patch removes the extraneous pair of
std::push_heap and std::pop_heap.  This patch cuts down about 45% of
calls to std::push_heap and std::pop_heap in InlineOrder.cpp while
building clang with FDO+ThinLTO.

Strictly speaking, removing the pair of calls may change the order in
which call sites with identical priorities are removed from the
priority queue, but we do not need to worry about that.

Since the functionality of adjust becomes more like a smart version of
pop_heap, this patch renames adjust to pop_heap_adjust.
---
 llvm/lib/Analysis/InlineOrder.cpp | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Analysis/InlineOrder.cpp b/llvm/lib/Analysis/InlineOrder.cpp
index e6157580e25237b..d6acafdc6ab8bc6 100644
--- a/llvm/lib/Analysis/InlineOrder.cpp
+++ b/llvm/lib/Analysis/InlineOrder.cpp
@@ -222,13 +222,12 @@ class PriorityInlineOrder : public InlineOrder<std::pair<CallBase *, int>> {
   // desirability of the front call site decreases, an updated one would be
   // pushed right back into the heap. For simplicity, those cases where the
   // desirability of a call site increases are ignored here.
-  void adjust() {
+  void pop_heap_adjust() {
     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);
   }
 
 public:
@@ -253,9 +252,8 @@ class PriorityInlineOrder : public InlineOrder<std::pair<CallBase *, int>> {
 
   T pop() override {
     assert(size() > 0);
-    adjust();
+    pop_heap_adjust();
 
-    std::pop_heap(Heap.begin(), Heap.end(), isLess);
     CallBase *CB = Heap.pop_back_val();
     T Result = std::make_pair(CB, InlineHistoryMap[CB]);
     InlineHistoryMap.erase(CB);



More information about the llvm-commits mailing list