[llvm] [ConstantHoisting] Cache OptForSize. (PR #79170)

Alina Sbirlea via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 23 09:10:11 PST 2024


https://github.com/alinas created https://github.com/llvm/llvm-project/pull/79170

CacheOptForSize to remove quadratic behavior.

For each constant analyzed, ConstantHoising calls `shouldOptimizeForSize(F)`, which calls `PSI.getTotalCallCount(F)`. PSI.getTotalCallCount(F) goes through all the instructions in all basic blocks, and checks if each is a call, to count them up.

This reduces `llc` time for a very large IR from ~10min to under 3min. Reproducer testcase is much too large to share.


>From ed5e727d4006684cedc0e486759e0a4bc01a5453 Mon Sep 17 00:00:00 2001
From: Alina Sbirlea <asbirlea at google.com>
Date: Tue, 23 Jan 2024 09:00:38 -0800
Subject: [PATCH] [ConstantHoisting] Cache OptForSize.

CacheOptForSize to remove quadratic behavior.

For each constant analyzed, ConstantHoising calls `shouldOptimizeForSize(F)`, which calls `PSI.getTotalCallCount(F)`. PSI.getTotalCallCount(F) goes through all the instructions in all basic blocks, and checks if each is a call, to count them up.

This reduces `llc` time for a very large IR from ~10min to under 3min.
Reproducer testcase is much too large to share.
---
 llvm/include/llvm/Transforms/Scalar/ConstantHoisting.h | 1 +
 llvm/lib/Transforms/Scalar/ConstantHoisting.cpp        | 7 ++++---
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/Transforms/Scalar/ConstantHoisting.h b/llvm/include/llvm/Transforms/Scalar/ConstantHoisting.h
index fa13ed73d506a44..a4d1653fdf4bc68 100644
--- a/llvm/include/llvm/Transforms/Scalar/ConstantHoisting.h
+++ b/llvm/include/llvm/Transforms/Scalar/ConstantHoisting.h
@@ -153,6 +153,7 @@ class ConstantHoistingPass : public PassInfoMixin<ConstantHoistingPass> {
   const DataLayout *DL;
   BasicBlock *Entry;
   ProfileSummaryInfo *PSI;
+  bool OptForSize;
 
   /// Keeps track of constant candidates found in the function.
   using ConstCandVecType = std::vector<consthoist::ConstantCandidate>;
diff --git a/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp b/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp
index 9e40d94dd73c766..49f8761a1392326 100644
--- a/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstantHoisting.cpp
@@ -576,9 +576,6 @@ ConstantHoistingPass::maximizeConstantsInRange(ConstCandVecType::iterator S,
                                            ConstCandVecType::iterator &MaxCostItr) {
   unsigned NumUses = 0;
 
-  bool OptForSize = Entry->getParent()->hasOptSize() ||
-                    llvm::shouldOptimizeForSize(Entry->getParent(), PSI, BFI,
-                                                PGSOQueryType::IRPass);
   if (!OptForSize || std::distance(S,E) > 100) {
     for (auto ConstCand = S; ConstCand != E; ++ConstCand) {
       NumUses += ConstCand->Uses.size();
@@ -948,6 +945,10 @@ bool ConstantHoistingPass::runImpl(Function &Fn, TargetTransformInfo &TTI,
   this->Ctx = &Fn.getContext();
   this->Entry = &Entry;
   this->PSI = PSI;
+  this->OptForSize = Entry.getParent()->hasOptSize() ||
+                     llvm::shouldOptimizeForSize(Entry.getParent(), PSI, BFI,
+                                                 PGSOQueryType::IRPass);
+
   // Collect all constant candidates.
   collectConstantCandidates(Fn);
 



More information about the llvm-commits mailing list