[PATCH] D134664: [nfc][mlgo] Lazily compute the regalloc reward

Mircea Trofin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 26 15:35:23 PDT 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG8a24e0cb5aee: [nfc][mlgo] Lazily compute the regalloc reward (authored by mtrofin).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D134664/new/

https://reviews.llvm.org/D134664

Files:
  llvm/lib/CodeGen/MLRegallocEvictAdvisor.cpp
  llvm/lib/CodeGen/RegAllocEvictionAdvisor.h
  llvm/lib/CodeGen/RegAllocPriorityAdvisor.h


Index: llvm/lib/CodeGen/RegAllocPriorityAdvisor.h
===================================================================
--- llvm/lib/CodeGen/RegAllocPriorityAdvisor.h
+++ llvm/lib/CodeGen/RegAllocPriorityAdvisor.h
@@ -68,7 +68,8 @@
   virtual std::unique_ptr<RegAllocPriorityAdvisor>
   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<float()> GetReward){};
 
 protected:
   // This analysis preserves everything, and subclasses may have additional
Index: llvm/lib/CodeGen/RegAllocEvictionAdvisor.h
===================================================================
--- llvm/lib/CodeGen/RegAllocEvictionAdvisor.h
+++ llvm/lib/CodeGen/RegAllocEvictionAdvisor.h
@@ -177,7 +177,8 @@
   virtual std::unique_ptr<RegAllocEvictionAdvisor>
   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<float()> GetReward){};
 
 protected:
   // This analysis preserves everything, and subclasses may have additional
Index: llvm/lib/CodeGen/MLRegallocEvictAdvisor.cpp
===================================================================
--- llvm/lib/CodeGen/MLRegallocEvictAdvisor.cpp
+++ 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<float()> 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<float>(
-      calculateRegAllocScore(MF, getAnalysis<MachineBlockFrequencyInfo>())
-          .getScore());
-
-  getAnalysis<RegAllocEvictionAdvisorAnalysis>().logRewardIfNeeded(MF, Reward);
-  getAnalysis<RegAllocPriorityAdvisorAnalysis>().logRewardIfNeeded(MF, Reward);
-
+  Optional<float> CachedReward;
+  auto GetReward = [&]() {
+    if (!CachedReward)
+      CachedReward = static_cast<float>(
+          calculateRegAllocScore(MF, getAnalysis<MachineBlockFrequencyInfo>())
+              .getScore());
+    return *CachedReward;
+  };
+
+  getAnalysis<RegAllocEvictionAdvisorAnalysis>().logRewardIfNeeded(MF,
+                                                                   GetReward);
+  getAnalysis<RegAllocPriorityAdvisorAnalysis>().logRewardIfNeeded(MF,
+                                                                   GetReward);
   return false;
 }
 #endif // #ifdef LLVM_HAVE_TF_API


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134664.463039.patch
Type: text/x-patch
Size: 3076 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220926/e7dfecb4/attachment.bin>


More information about the llvm-commits mailing list