From 8a24e0cb5aeecef63fb77830e437bcc116985af1 Mon Sep 17 00:00:00 2001 From: Mircea Trofin Date: Mon, 26 Sep 2022 12:20:37 -0700 Subject: [PATCH] [nfc][mlgo] Lazily compute the regalloc reward Differential Revision: https://reviews.llvm.org/D134664 --- llvm/lib/CodeGen/MLRegallocEvictAdvisor.cpp | 25 ++++++++++++++++--------- llvm/lib/CodeGen/RegAllocEvictionAdvisor.h | 3 ++- llvm/lib/CodeGen/RegAllocPriorityAdvisor.h | 3 ++- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/llvm/lib/CodeGen/MLRegallocEvictAdvisor.cpp b/llvm/lib/CodeGen/MLRegallocEvictAdvisor.cpp index 86c28ab..e48aac4 100644 --- a/llvm/lib/CodeGen/MLRegallocEvictAdvisor.cpp +++ b/llvm/lib/CodeGen/MLRegallocEvictAdvisor.cpp @@ -461,9 +461,10 @@ public: return I->second.get(); } - void logRewardIfNeeded(const MachineFunction &MF, float Reward) override { + void logRewardIfNeeded(const MachineFunction &MF, + llvm::function_ref GetReward) override { if (auto *Log = this->getLogger(MF)) - Log->logFloatFinalReward(Reward); + Log->logFloatFinalReward(GetReward()); } private: @@ -1067,13 +1068,19 @@ int64_t DevelopmentModeEvictAdvisor::tryFindEvictionCandidatePosition( } bool RegAllocScoring::runOnMachineFunction(MachineFunction &MF) { - float Reward = static_cast( - calculateRegAllocScore(MF, getAnalysis()) - .getScore()); - - getAnalysis().logRewardIfNeeded(MF, Reward); - getAnalysis().logRewardIfNeeded(MF, Reward); - + Optional CachedReward; + auto GetReward = [&]() { + if (!CachedReward) + CachedReward = static_cast( + calculateRegAllocScore(MF, getAnalysis()) + .getScore()); + return *CachedReward; + }; + + getAnalysis().logRewardIfNeeded(MF, + GetReward); + getAnalysis().logRewardIfNeeded(MF, + GetReward); return false; } #endif // #ifdef LLVM_HAVE_TF_API diff --git a/llvm/lib/CodeGen/RegAllocEvictionAdvisor.h b/llvm/lib/CodeGen/RegAllocEvictionAdvisor.h index c7f48bc..a3936ea 100644 --- a/llvm/lib/CodeGen/RegAllocEvictionAdvisor.h +++ b/llvm/lib/CodeGen/RegAllocEvictionAdvisor.h @@ -177,7 +177,8 @@ public: virtual std::unique_ptr getAdvisor(const MachineFunction &MF, const RAGreedy &RA) = 0; AdvisorMode getAdvisorMode() const { return Mode; } - virtual void logRewardIfNeeded(const MachineFunction &MF, float Reward){}; + virtual void logRewardIfNeeded(const MachineFunction &MF, + llvm::function_ref GetReward){}; protected: // This analysis preserves everything, and subclasses may have additional diff --git a/llvm/lib/CodeGen/RegAllocPriorityAdvisor.h b/llvm/lib/CodeGen/RegAllocPriorityAdvisor.h index 520d8d8..1e9fa96 100644 --- a/llvm/lib/CodeGen/RegAllocPriorityAdvisor.h +++ b/llvm/lib/CodeGen/RegAllocPriorityAdvisor.h @@ -68,7 +68,8 @@ public: virtual std::unique_ptr getAdvisor(const MachineFunction &MF, const RAGreedy &RA) = 0; AdvisorMode getAdvisorMode() const { return Mode; } - virtual void logRewardIfNeeded(const MachineFunction &MF, float Reward){}; + virtual void logRewardIfNeeded(const MachineFunction &MF, + llvm::function_ref GetReward){}; protected: // This analysis preserves everything, and subclasses may have additional -- 2.7.4