Skip to content

Commit edeaf41

Browse files
authored
[ConstantHoisting] Cache OptForSize. (#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.
1 parent 3428c28 commit edeaf41

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

llvm/include/llvm/Transforms/Scalar/ConstantHoisting.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ class ConstantHoistingPass : public PassInfoMixin<ConstantHoistingPass> {
153153
const DataLayout *DL;
154154
BasicBlock *Entry;
155155
ProfileSummaryInfo *PSI;
156+
bool OptForSize;
156157

157158
/// Keeps track of constant candidates found in the function.
158159
using ConstCandVecType = std::vector<consthoist::ConstantCandidate>;

llvm/lib/Transforms/Scalar/ConstantHoisting.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -576,9 +576,6 @@ ConstantHoistingPass::maximizeConstantsInRange(ConstCandVecType::iterator S,
576576
ConstCandVecType::iterator &MaxCostItr) {
577577
unsigned NumUses = 0;
578578

579-
bool OptForSize = Entry->getParent()->hasOptSize() ||
580-
llvm::shouldOptimizeForSize(Entry->getParent(), PSI, BFI,
581-
PGSOQueryType::IRPass);
582579
if (!OptForSize || std::distance(S,E) > 100) {
583580
for (auto ConstCand = S; ConstCand != E; ++ConstCand) {
584581
NumUses += ConstCand->Uses.size();
@@ -948,6 +945,10 @@ bool ConstantHoistingPass::runImpl(Function &Fn, TargetTransformInfo &TTI,
948945
this->Ctx = &Fn.getContext();
949946
this->Entry = &Entry;
950947
this->PSI = PSI;
948+
this->OptForSize = Entry.getParent()->hasOptSize() ||
949+
llvm::shouldOptimizeForSize(Entry.getParent(), PSI, BFI,
950+
PGSOQueryType::IRPass);
951+
951952
// Collect all constant candidates.
952953
collectConstantCandidates(Fn);
953954

0 commit comments

Comments
 (0)