Skip to content

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

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 4 commits into from
Apr 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "llvm/CodeGen/BasicTTIImpl.h"
#include "llvm/CodeGen/TargetLowering.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/Support/Debug.h"
Expand Down Expand Up @@ -80,7 +81,6 @@ unsigned SystemZTTIImpl::adjustInliningThreshold(const CallBase *CB) const {
const Function *Callee = CB->getCalledFunction();
if (!Callee)
return 0;
const Module *M = Caller->getParent();

// Increase the threshold if an incoming argument is used only as a memcpy
// source.
Expand All @@ -92,25 +92,37 @@ unsigned SystemZTTIImpl::adjustInliningThreshold(const CallBase *CB) const {
}
}

// Give bonus for globals used much in both caller and callee.
std::set<const GlobalVariable *> CalleeGlobals;
std::set<const GlobalVariable *> CallerGlobals;
for (const GlobalVariable &Global : M->globals())
for (const User *U : Global.users())
if (const Instruction *User = dyn_cast<Instruction>(U)) {
if (User->getParent()->getParent() == Callee)
CalleeGlobals.insert(&Global);
if (User->getParent()->getParent() == Caller)
CallerGlobals.insert(&Global);
// Give bonus for globals used much in both caller and a relatively small
// callee.
unsigned InstrCount = 0;
SmallDenseMap<const Value *, unsigned> Ptr2NumUses;
for (auto &I : instructions(Callee)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to watch out for debug instructions here? We don't want the count == 200 check to differ between a debug and a non-debug compile ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started out with that actually, but @nikic suggested this not being needed with the new debug refs, which was committed for SystemZ a month ago. (haven't double-checked though)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is due to the switch from debug intrinsics to debug records. It's a separate change from debug instr ref (which is a backend thing).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks. Would this still be the case for a LLVM 20 backport? (Not sure when this change came in ...)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's already in LLVM 20.

if (++InstrCount == 200) {
Ptr2NumUses.clear();
break;
}
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;
countNumMemAccesses(GV, CalleeStores, CalleeLoads, Callee);
countNumMemAccesses(GV, CallerStores, CallerLoads, Caller);
if ((CalleeStores + CalleeLoads) > 10 &&
(CallerStores + CallerLoads) > 10) {
countNumMemAccesses(Ptr, CallerStores, CallerLoads, Caller);
if (CallerStores + CallerLoads > 10) {
Bonus = 1000;
break;
}
Expand Down
Loading