From 65bc5bffb350beb26970d6664328f2fa5fa3d53a Mon Sep 17 00:00:00 2001 From: Jonas Paulsson Date: Sun, 27 Apr 2025 11:50:57 +0200 Subject: [PATCH 1/4] IP --- .../SystemZ/SystemZTargetTransformInfo.cpp | 50 +++++++++++-------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp b/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp index ee142ccd20e20..78f5154229f55 100644 --- a/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp +++ b/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp @@ -80,7 +80,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. @@ -92,29 +91,38 @@ unsigned SystemZTTIImpl::adjustInliningThreshold(const CallBase *CB) const { } } - // Give bonus for globals used much in both caller and callee. - std::set CalleeGlobals; - std::set CallerGlobals; - for (const GlobalVariable &Global : M->globals()) - for (const User *U : Global.users()) - if (const Instruction *User = dyn_cast(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. + if (Callee->getInstructionCount() < 200) { + std::map Ptr2NumUses; + for (auto &BB : *Callee) + for (auto &I : BB) { + if (const auto *SI = dyn_cast(&I)) { + if (!SI->isVolatile()) + Ptr2NumUses[SI->getPointerOperand()]++; + } else if (const auto *LI = dyn_cast(&I)) { + if (!LI->isVolatile()) + Ptr2NumUses[LI->getPointerOperand()]++; + } else if (const auto *GEP = dyn_cast(&I)) { + unsigned NumStores = 0, NumLoads = 0; + countNumMemAccesses(GEP, NumStores, NumLoads, Callee); + Ptr2NumUses[GEP->getPointerOperand()] += NumLoads + NumStores; + } } - for (auto *GV : CalleeGlobals) - if (CallerGlobals.count(GV)) { - unsigned CalleeStores = 0, CalleeLoads = 0; - unsigned CallerStores = 0, CallerLoads = 0; - countNumMemAccesses(GV, CalleeStores, CalleeLoads, Callee); - countNumMemAccesses(GV, CallerStores, CallerLoads, Caller); - if ((CalleeStores + CalleeLoads) > 10 && - (CallerStores + CallerLoads) > 10) { - Bonus = 1000; - break; + + for (auto I : Ptr2NumUses) { + const Value *Ptr = I.first; + unsigned NumCalleeUses = I.second; + if (NumCalleeUses > 10 && isa(Ptr)) { + unsigned CallerStores = 0, CallerLoads = 0; + countNumMemAccesses(Ptr, CallerStores, CallerLoads, Caller); + if (CallerStores + CallerLoads > 10) { + Bonus = 1000; + break; + } } } + } // Give bonus when Callee accesses an Alloca of Caller heavily. unsigned NumStores = 0; From be26bc95ebc8b45b1f4432b630a286f8408a5192 Mon Sep 17 00:00:00 2001 From: Jonas Paulsson Date: Sun, 27 Apr 2025 18:59:52 +0200 Subject: [PATCH 2/4] Updated per review. --- .../SystemZ/SystemZTargetTransformInfo.cpp | 49 ++++++++++--------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp b/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp index 78f5154229f55..e26bdd77ee98b 100644 --- a/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp +++ b/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp @@ -93,36 +93,39 @@ unsigned SystemZTTIImpl::adjustInliningThreshold(const CallBase *CB) const { // Give bonus for globals used much in both caller and a relatively small // callee. - if (Callee->getInstructionCount() < 200) { - std::map Ptr2NumUses; - for (auto &BB : *Callee) - for (auto &I : BB) { - if (const auto *SI = dyn_cast(&I)) { - if (!SI->isVolatile()) - Ptr2NumUses[SI->getPointerOperand()]++; - } else if (const auto *LI = dyn_cast(&I)) { - if (!LI->isVolatile()) - Ptr2NumUses[LI->getPointerOperand()]++; - } else if (const auto *GEP = dyn_cast(&I)) { + unsigned InstrCount = 0; + SmallDenseMap Ptr2NumUses; + for (auto &BB : *Callee) + for (auto &I : BB.instructionsWithoutDebug()) { + if (++InstrCount == 200) + goto GlobalsDone; + if (const auto *SI = dyn_cast(&I)) { + if (!SI->isVolatile()) + if (auto GV = dyn_cast(SI->getPointerOperand())) + Ptr2NumUses[GV]++; + } else if (const auto *LI = dyn_cast(&I)) { + if (!LI->isVolatile()) + if (auto GV = dyn_cast(LI->getPointerOperand())) + Ptr2NumUses[GV]++; + } else if (const auto *GEP = dyn_cast(&I)) { + if (auto GV = dyn_cast(GEP->getPointerOperand())) { unsigned NumStores = 0, NumLoads = 0; countNumMemAccesses(GEP, NumStores, NumLoads, Callee); - Ptr2NumUses[GEP->getPointerOperand()] += NumLoads + NumStores; + Ptr2NumUses[GV] += NumLoads + NumStores; } } + } - for (auto I : Ptr2NumUses) { - const Value *Ptr = I.first; - unsigned NumCalleeUses = I.second; - if (NumCalleeUses > 10 && isa(Ptr)) { - unsigned CallerStores = 0, CallerLoads = 0; - countNumMemAccesses(Ptr, CallerStores, CallerLoads, Caller); - if (CallerStores + CallerLoads > 10) { - Bonus = 1000; - break; - } + for (auto [Ptr, NumCalleeUses] : Ptr2NumUses) + if (NumCalleeUses > 10) { + unsigned CallerStores = 0, CallerLoads = 0; + countNumMemAccesses(Ptr, CallerStores, CallerLoads, Caller); + if (CallerStores + CallerLoads > 10) { + Bonus = 1000; + break; } } - } +GlobalsDone: // Give bonus when Callee accesses an Alloca of Caller heavily. unsigned NumStores = 0; From 7e44c5572e6e4bd58dbba314a7891a9c2836239f Mon Sep 17 00:00:00 2001 From: Jonas Paulsson Date: Mon, 28 Apr 2025 09:41:57 +0200 Subject: [PATCH 3/4] Use instructions() and avoid goto. --- .../SystemZ/SystemZTargetTransformInfo.cpp | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp b/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp index e26bdd77ee98b..b12b5494648f1 100644 --- a/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp +++ b/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp @@ -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" @@ -95,26 +96,27 @@ unsigned SystemZTTIImpl::adjustInliningThreshold(const CallBase *CB) const { // callee. unsigned InstrCount = 0; SmallDenseMap Ptr2NumUses; - for (auto &BB : *Callee) - for (auto &I : BB.instructionsWithoutDebug()) { - if (++InstrCount == 200) - goto GlobalsDone; - if (const auto *SI = dyn_cast(&I)) { - if (!SI->isVolatile()) - if (auto GV = dyn_cast(SI->getPointerOperand())) - Ptr2NumUses[GV]++; - } else if (const auto *LI = dyn_cast(&I)) { - if (!LI->isVolatile()) - if (auto GV = dyn_cast(LI->getPointerOperand())) - Ptr2NumUses[GV]++; - } else if (const auto *GEP = dyn_cast(&I)) { - if (auto GV = dyn_cast(GEP->getPointerOperand())) { - unsigned NumStores = 0, NumLoads = 0; - countNumMemAccesses(GEP, NumStores, NumLoads, Callee); - Ptr2NumUses[GV] += NumLoads + NumStores; - } + for (auto &I : instructions(Callee)) { + if (++InstrCount == 200) { + Ptr2NumUses.clear(); + break; + } + if (const auto *SI = dyn_cast(&I)) { + if (!SI->isVolatile()) + if (auto GV = dyn_cast(SI->getPointerOperand())) + Ptr2NumUses[GV]++; + } else if (const auto *LI = dyn_cast(&I)) { + if (!LI->isVolatile()) + if (auto GV = dyn_cast(LI->getPointerOperand())) + Ptr2NumUses[GV]++; + } else if (const auto *GEP = dyn_cast(&I)) { + if (auto GV = dyn_cast(GEP->getPointerOperand())) { + unsigned NumStores = 0, NumLoads = 0; + countNumMemAccesses(GEP, NumStores, NumLoads, Callee); + Ptr2NumUses[GV] += NumLoads + NumStores; } } + } for (auto [Ptr, NumCalleeUses] : Ptr2NumUses) if (NumCalleeUses > 10) { @@ -125,7 +127,6 @@ unsigned SystemZTTIImpl::adjustInliningThreshold(const CallBase *CB) const { break; } } -GlobalsDone: // Give bonus when Callee accesses an Alloca of Caller heavily. unsigned NumStores = 0; From 079b2d5104765f73a983a988d8615e3bcb123e13 Mon Sep 17 00:00:00 2001 From: Jonas Paulsson Date: Mon, 28 Apr 2025 14:12:26 +0200 Subject: [PATCH 4/4] GV -> *GV in auto decl for clarity --- llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp b/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp index b12b5494648f1..2bea85af2bf65 100644 --- a/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp +++ b/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp @@ -103,14 +103,14 @@ unsigned SystemZTTIImpl::adjustInliningThreshold(const CallBase *CB) const { } if (const auto *SI = dyn_cast(&I)) { if (!SI->isVolatile()) - if (auto GV = dyn_cast(SI->getPointerOperand())) + if (auto *GV = dyn_cast(SI->getPointerOperand())) Ptr2NumUses[GV]++; } else if (const auto *LI = dyn_cast(&I)) { if (!LI->isVolatile()) - if (auto GV = dyn_cast(LI->getPointerOperand())) + if (auto *GV = dyn_cast(LI->getPointerOperand())) Ptr2NumUses[GV]++; } else if (const auto *GEP = dyn_cast(&I)) { - if (auto GV = dyn_cast(GEP->getPointerOperand())) { + if (auto *GV = dyn_cast(GEP->getPointerOperand())) { unsigned NumStores = 0, NumLoads = 0; countNumMemAccesses(GEP, NumStores, NumLoads, Callee); Ptr2NumUses[GV] += NumLoads + NumStores;