[llvm] [SSAUpdater] Don't use large SmallSets for IDFcalc (PR #97823)

Jeremy Morse via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 5 06:00:23 PDT 2024


https://github.com/jmorse created https://github.com/llvm/llvm-project/pull/97823

As per the LLVM programmers manual, SmallSets do linear scans on insertion and then turn into a hash-table if the set gets big. Here in the IDFCalculator, the SmallSets have been configured to have 32 elements in each static allocation... which means that we linearly scan for all problems with up to 32 elements, which I feel is quite a large N.

Replace these SmallSets with a plain DenseSet (and use reserve to avoid any repeated allocations). Doing this yields a 0.13% compile-time improvement for debug-info builds, as we hit IDFCalculator pretty hard in InstrRefBasedLDV. Adding @nikic as this could affect more than debug-info things, although apparently not in a visible way on CTMark.

http://llvm-compile-time-tracker.com/compare.php?from=4ce6cc488083a931e795dafe30e69a0e4a96a3e4&to=a9f1d377013b1d208ac58f6f75548f1f24d174d2&stat=instructions%3Au

(tracker link is across a few commits, with this patch being the summary).

>From 33666499d5b99b9e50305047ce52cf440fd5dfb0 Mon Sep 17 00:00:00 2001
From: Jeremy Morse <jeremy.morse at sony.com>
Date: Fri, 5 Jul 2024 13:15:43 +0100
Subject: [PATCH] [SSAUpdater] Don't use large SmallSets for IDFcalc

As per the LLVM programmers manual, SmallSets do linear scans on insertion
and then turn into a hash-table if the set gets big. Here in the
IDFCalculator, the SmallSets have been configured to have 32 elements in
each static allocation... which means that we linearly scan for all
problems with up to 32 elements.

Replace these SmallSets with a plain DenseSet (and use reserve to avoid any
repeated allocations). Doing this yields a 0.13% compile-time improvement
for debug-info builds, as we hit IDFCalculator pretty hard in
InstrRefBasedLDV.
---
 .../llvm/Support/GenericIteratedDominanceFrontier.h       | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/Support/GenericIteratedDominanceFrontier.h b/llvm/include/llvm/Support/GenericIteratedDominanceFrontier.h
index 0dc58e37c821d..c218d33c3eb30 100644
--- a/llvm/include/llvm/Support/GenericIteratedDominanceFrontier.h
+++ b/llvm/include/llvm/Support/GenericIteratedDominanceFrontier.h
@@ -144,8 +144,12 @@ void IDFCalculatorBase<NodeTy, IsPostDom>::calculate(
   DT.updateDFSNumbers();
 
   SmallVector<DomTreeNodeBase<NodeTy> *, 32> Worklist;
-  SmallPtrSet<DomTreeNodeBase<NodeTy> *, 32> VisitedPQ;
-  SmallPtrSet<DomTreeNodeBase<NodeTy> *, 32> VisitedWorklist;
+  SmallDenseSet<DomTreeNodeBase<NodeTy> *, 16> VisitedPQ;
+  SmallDenseSet<DomTreeNodeBase<NodeTy> *, 16> VisitedWorklist;
+  if (useLiveIn) {
+    VisitedPQ.reserve(LiveInBlocks->size());
+    VisitedWorklist.reserve(LiveInBlocks->size());
+  }
 
   for (NodeTy *BB : *DefBlocks)
     if (DomTreeNodeBase<NodeTy> *Node = DT.getNode(BB)) {



More information about the llvm-commits mailing list