Skip to content

[ConstantHoisting] Cache OptForSize. #79170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2024
Merged

[ConstantHoisting] Cache OptForSize. #79170

merged 1 commit into from
Jan 23, 2024

Conversation

alinas
Copy link
Contributor

@alinas alinas commented Jan 23, 2024

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.

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.
@llvmbot
Copy link
Member

llvmbot commented Jan 23, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Alina Sbirlea (alinas)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/79170.diff

2 Files Affected:

  • (modified) llvm/include/llvm/Transforms/Scalar/ConstantHoisting.h (+1)
  • (modified) llvm/lib/Transforms/Scalar/ConstantHoisting.cpp (+4-3)
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);
 

@alinas alinas requested a review from hjyamauchi January 23, 2024 17:36
@alinas
Copy link
Contributor Author

alinas commented Jan 23, 2024

The change adding this was: https://reviews.llvm.org/D59514.

@alinas alinas merged commit edeaf41 into llvm:main Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants