diff --git a/llvm/lib/CodeGen/MLRegallocEvictAdvisor.cpp b/llvm/lib/CodeGen/MLRegallocEvictAdvisor.cpp --- a/llvm/lib/CodeGen/MLRegallocEvictAdvisor.cpp +++ b/llvm/lib/CodeGen/MLRegallocEvictAdvisor.cpp @@ -461,9 +461,10 @@ 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 @@ } 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 --- a/llvm/lib/CodeGen/RegAllocEvictionAdvisor.h +++ b/llvm/lib/CodeGen/RegAllocEvictionAdvisor.h @@ -177,7 +177,8 @@ 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 --- a/llvm/lib/CodeGen/RegAllocPriorityAdvisor.h +++ b/llvm/lib/CodeGen/RegAllocPriorityAdvisor.h @@ -68,7 +68,8 @@ 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