[llvm] [StackColoring] Declare BitVector outside the loop (PR #95688)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 15 21:32:16 PDT 2024


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

The StackColoring pass creates two instances of BitVector per basic
block until the fixed point is reached.  Each instance may involve a
heap allocation depending on its size.

This patch declares them outside the loop, saving 1.40% of heap
allocations during the compilation of CGBuiltin.cpp.ii, a preprocessed
version of CGBuiltin.cpp.

>From 7482ebb5cc9835d5a2aafafd06b1d555be8390b6 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 15 Jun 2024 06:54:51 -0700
Subject: [PATCH] [StackColoring] Declare BitVector outside the loop

The StackColoring pass creates two instances of BitVector per basic
block until the fixed point is reached.  Each instance may involve a
heap allocation depending on its size.

This patch declares them outside the loop, saving 1.40% of heap
allocations during the compilation of CGBuiltin.cpp.ii, a preprocessed
version of CGBuiltin.cpp.
---
 llvm/lib/CodeGen/StackColoring.cpp | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/StackColoring.cpp b/llvm/lib/CodeGen/StackColoring.cpp
index 37f7aa9290054..36414e742c327 100644
--- a/llvm/lib/CodeGen/StackColoring.cpp
+++ b/llvm/lib/CodeGen/StackColoring.cpp
@@ -773,6 +773,10 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
 void StackColoring::calculateLocalLiveness() {
   unsigned NumIters = 0;
   bool changed = true;
+  // Create BitVector outside the loop and reuse them to avoid repeated heap
+  // allocations.
+  BitVector LocalLiveIn;
+  BitVector LocalLiveOut;
   while (changed) {
     changed = false;
     ++NumIters;
@@ -784,7 +788,7 @@ void StackColoring::calculateLocalLiveness() {
       BlockLifetimeInfo &BlockInfo = BI->second;
 
       // Compute LiveIn by unioning together the LiveOut sets of all preds.
-      BitVector LocalLiveIn;
+      LocalLiveIn.clear();
       for (MachineBasicBlock *Pred : BB->predecessors()) {
         LivenessMap::const_iterator I = BlockLiveness.find(Pred);
         // PR37130: transformations prior to stack coloring can
@@ -801,7 +805,7 @@ void StackColoring::calculateLocalLiveness() {
       // because we already handle the case where the BEGIN comes
       // before the END when collecting the markers (and building the
       // BEGIN/END vectors).
-      BitVector LocalLiveOut = LocalLiveIn;
+      LocalLiveOut = LocalLiveIn;
       LocalLiveOut.reset(BlockInfo.End);
       LocalLiveOut |= BlockInfo.Begin;
 



More information about the llvm-commits mailing list