[SystemZ] Fix compile time regression in adjustInliningThreshold(). (#137527)

Instead of always iterating over all GlobalVariable:s in the Module to
find the case where both Caller and Callee is using the same GV heavily,
first scan Callee (only if less than 200 instructions) for all GVs used
more than 10 times, and then do the counting for the Caller for just
those relevant GVs.

The limit of 200 instructions makes sense as this aims to inline a
relatively small function using a GV +10 times.

This resolves the compile time problem with zig where it is on main
(compared to removing the heuristic) a 380% increase, but with this
change <0.5% increase (total user compile time with opt).

Fixes #134714.

(cherry picked from commit 98b895da30c03b7061b8740d91c0e7998e69d091)
This commit is contained in:
Jonas Paulsson 2025-04-28 07:04:07 -06:00 committed by Tom Stellard
parent c877757659
commit 02afcbf63f

View File

@ -18,6 +18,7 @@
#include "llvm/CodeGen/BasicTTIImpl.h" #include "llvm/CodeGen/BasicTTIImpl.h"
#include "llvm/CodeGen/TargetLowering.h" #include "llvm/CodeGen/TargetLowering.h"
#include "llvm/IR/DerivedTypes.h" #include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h" #include "llvm/IR/Intrinsics.h"
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"
@ -80,7 +81,6 @@ unsigned SystemZTTIImpl::adjustInliningThreshold(const CallBase *CB) const {
const Function *Callee = CB->getCalledFunction(); const Function *Callee = CB->getCalledFunction();
if (!Callee) if (!Callee)
return 0; return 0;
const Module *M = Caller->getParent();
// Increase the threshold if an incoming argument is used only as a memcpy // Increase the threshold if an incoming argument is used only as a memcpy
// source. // source.
@ -92,25 +92,37 @@ unsigned SystemZTTIImpl::adjustInliningThreshold(const CallBase *CB) const {
} }
} }
// Give bonus for globals used much in both caller and callee. // Give bonus for globals used much in both caller and a relatively small
std::set<const GlobalVariable *> CalleeGlobals; // callee.
std::set<const GlobalVariable *> CallerGlobals; unsigned InstrCount = 0;
for (const GlobalVariable &Global : M->globals()) SmallDenseMap<const Value *, unsigned> Ptr2NumUses;
for (const User *U : Global.users()) for (auto &I : instructions(Callee)) {
if (const Instruction *User = dyn_cast<Instruction>(U)) { if (++InstrCount == 200) {
if (User->getParent()->getParent() == Callee) Ptr2NumUses.clear();
CalleeGlobals.insert(&Global); break;
if (User->getParent()->getParent() == Caller) }
CallerGlobals.insert(&Global); if (const auto *SI = dyn_cast<StoreInst>(&I)) {
if (!SI->isVolatile())
if (auto *GV = dyn_cast<GlobalVariable>(SI->getPointerOperand()))
Ptr2NumUses[GV]++;
} else if (const auto *LI = dyn_cast<LoadInst>(&I)) {
if (!LI->isVolatile())
if (auto *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand()))
Ptr2NumUses[GV]++;
} else if (const auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
if (auto *GV = dyn_cast<GlobalVariable>(GEP->getPointerOperand())) {
unsigned NumStores = 0, NumLoads = 0;
countNumMemAccesses(GEP, NumStores, NumLoads, Callee);
Ptr2NumUses[GV] += NumLoads + NumStores;
} }
for (auto *GV : CalleeGlobals) }
if (CallerGlobals.count(GV)) { }
unsigned CalleeStores = 0, CalleeLoads = 0;
for (auto [Ptr, NumCalleeUses] : Ptr2NumUses)
if (NumCalleeUses > 10) {
unsigned CallerStores = 0, CallerLoads = 0; unsigned CallerStores = 0, CallerLoads = 0;
countNumMemAccesses(GV, CalleeStores, CalleeLoads, Callee); countNumMemAccesses(Ptr, CallerStores, CallerLoads, Caller);
countNumMemAccesses(GV, CallerStores, CallerLoads, Caller); if (CallerStores + CallerLoads > 10) {
if ((CalleeStores + CalleeLoads) > 10 &&
(CallerStores + CallerLoads) > 10) {
Bonus = 1000; Bonus = 1000;
break; break;
} }