[llvm] 83c53d3 - [NFC][MLGO] Introduce logRewardIfNeeded method

Eric Wang via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 22 17:38:30 PDT 2022


Author: Eric Wang
Date: 2022-09-22T19:22:32-05:00
New Revision: 83c53d346f0515c65c579ab0cfef8f5afdbd2beb

URL: https://github.com/llvm/llvm-project/commit/83c53d346f0515c65c579ab0cfef8f5afdbd2beb
DIFF: https://github.com/llvm/llvm-project/commit/83c53d346f0515c65c579ab0cfef8f5afdbd2beb.diff

LOG: [NFC][MLGO] Introduce logRewardIfNeeded method

This patch introduces a logRewardIfNeeded method to reuse regallocscoring.

Differential Revision: https://reviews.llvm.org/D134232

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/MLRegallocEvictAdvisor.cpp b/llvm/lib/CodeGen/MLRegallocEvictAdvisor.cpp
index 0345efaf72c6..86c28ab0d309 100644
--- a/llvm/lib/CodeGen/MLRegallocEvictAdvisor.cpp
+++ b/llvm/lib/CodeGen/MLRegallocEvictAdvisor.cpp
@@ -98,6 +98,7 @@ class RegAllocScoring : public MachineFunctionPass {
   void getAnalysisUsage(AnalysisUsage &AU) const override {
     AU.setPreservesAll();
     AU.addRequired<RegAllocEvictionAdvisorAnalysis>();
+    AU.addRequired<RegAllocPriorityAdvisorAnalysis>();
     AU.addRequired<MachineBlockFrequencyInfo>();
     MachineFunctionPass::getAnalysisUsage(AU);
   }
@@ -460,6 +461,11 @@ class DevelopmentModeEvictionAdvisorAnalysis final
     return I->second.get();
   }
 
+  void logRewardIfNeeded(const MachineFunction &MF, float Reward) override {
+    if (auto *Log = this->getLogger(MF))
+      Log->logFloatFinalReward(Reward);
+  }
+
 private:
   std::vector<TensorSpec> InputFeatures;
   std::vector<TensorSpec> TrainingInputFeatures;
@@ -1061,12 +1067,12 @@ int64_t DevelopmentModeEvictAdvisor::tryFindEvictionCandidatePosition(
 }
 
 bool RegAllocScoring::runOnMachineFunction(MachineFunction &MF) {
-  if (auto *DevModeAnalysis = dyn_cast<DevelopmentModeEvictionAdvisorAnalysis>(
-          &getAnalysis<RegAllocEvictionAdvisorAnalysis>()))
-    if (auto *Log = DevModeAnalysis->getLogger(MF))
-      Log->logFloatFinalReward(static_cast<float>(
-          calculateRegAllocScore(MF, getAnalysis<MachineBlockFrequencyInfo>())
-              .getScore()));
+  float Reward = static_cast<float>(
+      calculateRegAllocScore(MF, getAnalysis<MachineBlockFrequencyInfo>())
+          .getScore());
+
+  getAnalysis<RegAllocEvictionAdvisorAnalysis>().logRewardIfNeeded(MF, Reward);
+  getAnalysis<RegAllocPriorityAdvisorAnalysis>().logRewardIfNeeded(MF, Reward);
 
   return false;
 }

diff  --git a/llvm/lib/CodeGen/RegAllocEvictionAdvisor.h b/llvm/lib/CodeGen/RegAllocEvictionAdvisor.h
index d6a3997e4b70..c7f48bc9ea9d 100644
--- a/llvm/lib/CodeGen/RegAllocEvictionAdvisor.h
+++ b/llvm/lib/CodeGen/RegAllocEvictionAdvisor.h
@@ -177,6 +177,7 @@ class RegAllocEvictionAdvisorAnalysis : public ImmutablePass {
   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){};
 
 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 5f501fff320c..520d8d836fe4 100644
--- a/llvm/lib/CodeGen/RegAllocPriorityAdvisor.h
+++ b/llvm/lib/CodeGen/RegAllocPriorityAdvisor.h
@@ -68,6 +68,7 @@ class RegAllocPriorityAdvisorAnalysis : public ImmutablePass {
   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){};
 
 protected:
   // This analysis preserves everything, and subclasses may have additional


        


More information about the llvm-commits mailing list