From: Mircea Trofin Date: Mon, 26 Sep 2022 19:20:37 +0000 (-0700) Subject: [nfc][mlgo] Lazily compute the regalloc reward X-Git-Tag: upstream/17.0.6~32442 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8a24e0cb5aeecef63fb77830e437bcc116985af1;p=platform%2Fupstream%2Fllvm.git [nfc][mlgo] Lazily compute the regalloc reward Differential Revision: https://reviews.llvm.org/D134664 --- 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