[llvm] [MLGO] Switch regalloc log context once per function (PR #229453)

Arseniy Obolenskiy via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 9 01:52:35 PDT 2026


https://github.com/aobolensk updated https://github.com/llvm/llvm-project/pull/229453

>From 81f3999a3e989fca88812f74b3b9efc07f8e8b97 Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Tue, 6 Oct 2026 17:12:34 +0200
Subject: [PATCH 1/4] [MLGO] Switch regalloc log context once per function

AMDGPU runs greedy several times per function and each run wrote a new context record

This splitted an observation from its outcome and broke log_reader
---
 llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp   |  2 +-
 .../lib/CodeGen/MLRegAllocPriorityAdvisor.cpp |  2 +-
 .../CodeGen/MLRegAlloc/dev-mode-log-amdgpu.ll | 33 +++++++++++++++++++
 3 files changed, 35 insertions(+), 2 deletions(-)
 create mode 100644 llvm/test/CodeGen/MLRegAlloc/dev-mode-log-amdgpu.ll

diff --git a/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp b/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp
index 67cf3305f3b992..78e01370f35203 100644
--- a/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp
+++ b/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp
@@ -570,7 +570,7 @@ class DevelopmentModeEvictionAdvisorProvider final
              MachineBlockFrequencyInfo *MBFI, MachineLoopInfo *Loops) override {
     if (!Runner)
       return nullptr;
-    if (Log)
+    if (Log && Log->currentContext() != MF.getName())
       Log->switchContext(MF.getName());
     assert(MBFI && Loops &&
            "Invalid provider state: must have analysis available");
diff --git a/llvm/lib/CodeGen/MLRegAllocPriorityAdvisor.cpp b/llvm/lib/CodeGen/MLRegAllocPriorityAdvisor.cpp
index 73987e2ba221c9..deb060ae39ab28 100644
--- a/llvm/lib/CodeGen/MLRegAllocPriorityAdvisor.cpp
+++ b/llvm/lib/CodeGen/MLRegAllocPriorityAdvisor.cpp
@@ -269,7 +269,7 @@ class DevelopmentModePriorityAdvisorProvider final
              SlotIndexes &SI) override {
     if (!Runner)
       return nullptr;
-    if (Log) {
+    if (Log && Log->currentContext() != MF.getName()) {
       Log->switchContext(MF.getName());
     }
     return std::make_unique<DevelopmentModePriorityAdvisor>(
diff --git a/llvm/test/CodeGen/MLRegAlloc/dev-mode-log-amdgpu.ll b/llvm/test/CodeGen/MLRegAlloc/dev-mode-log-amdgpu.ll
new file mode 100644
index 00000000000000..f5b3bee1fd802b
--- /dev/null
+++ b/llvm/test/CodeGen/MLRegAlloc/dev-mode-log-amdgpu.ll
@@ -0,0 +1,33 @@
+; REQUIRES: have_tflite
+; REQUIRES: x86_64-linux
+; REQUIRES: amdgpu-registered-target
+;
+; AMDGPU runs the register allocator more than once per function. Check that
+; each function gets a single context record.
+;
+; RUN: llc -o /dev/null -mtriple=amdgpu9.0a-amd-amdhsa \
+; RUN:   -regalloc-enable-advisor=development -mlregalloc-num-allocatable-regs=256 \
+; RUN:   -regalloc-training-log=%t1 < %s
+; RUN: FileCheck --input-file %t1 %s
+
+; RUN: llc -o /dev/null -mtriple=amdgpu9.0a-amd-amdhsa \
+; RUN:   -regalloc-enable-priority-advisor=development \
+; RUN:   -regalloc-priority-training-log=%t2 < %s
+; RUN: FileCheck --input-file %t2 %s
+
+; CHECK:     {"context":"f"}
+; CHECK-NOT: {"context"
+; CHECK:     {"context":"g"}
+; CHECK-NOT: {"context"
+
+define amdgpu_kernel void @f(ptr addrspace(1) %p) {
+  %v = load volatile i32, ptr addrspace(1) %p
+  store volatile i32 %v, ptr addrspace(1) %p
+  ret void
+}
+
+define amdgpu_kernel void @g(ptr addrspace(1) %p) {
+  %v = load volatile i32, ptr addrspace(1) %p
+  store volatile i32 %v, ptr addrspace(1) %p
+  ret void
+}

>From 5aacc102efec3e4539c1539edab27219fd74ad69 Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Wed, 7 Oct 2026 06:08:18 +0200
Subject: [PATCH 2/4] use FunctionNumber

---
 llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp      |  5 ++++-
 llvm/lib/CodeGen/MLRegAllocPriorityAdvisor.cpp   |  4 +++-
 .../CodeGen/MLRegAlloc/dev-mode-log-amdgpu.ll    | 16 ++++++++++++----
 3 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp b/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp
index 78e01370f35203..4c4ef7e1f82a2c 100644
--- a/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp
+++ b/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp
@@ -570,8 +570,10 @@ class DevelopmentModeEvictionAdvisorProvider final
              MachineBlockFrequencyInfo *MBFI, MachineLoopInfo *Loops) override {
     if (!Runner)
       return nullptr;
-    if (Log && Log->currentContext() != MF.getName())
+    if (Log && LastFunctionNumber != MF.getFunctionNumber()) {
+      LastFunctionNumber = MF.getFunctionNumber();
       Log->switchContext(MF.getName());
+    }
     assert(MBFI && Loops &&
            "Invalid provider state: must have analysis available");
     return std::make_unique<DevelopmentModeEvictAdvisor>(
@@ -584,6 +586,7 @@ class DevelopmentModeEvictionAdvisorProvider final
 
   std::unique_ptr<MLModelRunner> Runner;
   std::unique_ptr<Logger> Log;
+  std::optional<unsigned> LastFunctionNumber;
 };
 
 class DevelopmentModeEvictionAdvisorAnalysisLegacy final
diff --git a/llvm/lib/CodeGen/MLRegAllocPriorityAdvisor.cpp b/llvm/lib/CodeGen/MLRegAllocPriorityAdvisor.cpp
index deb060ae39ab28..d3898421518e82 100644
--- a/llvm/lib/CodeGen/MLRegAllocPriorityAdvisor.cpp
+++ b/llvm/lib/CodeGen/MLRegAllocPriorityAdvisor.cpp
@@ -269,7 +269,8 @@ class DevelopmentModePriorityAdvisorProvider final
              SlotIndexes &SI) override {
     if (!Runner)
       return nullptr;
-    if (Log && Log->currentContext() != MF.getName()) {
+    if (Log && LastFunctionNumber != MF.getFunctionNumber()) {
+      LastFunctionNumber = MF.getFunctionNumber();
       Log->switchContext(MF.getName());
     }
     return std::make_unique<DevelopmentModePriorityAdvisor>(
@@ -278,6 +279,7 @@ class DevelopmentModePriorityAdvisorProvider final
 
   std::unique_ptr<MLModelRunner> Runner;
   std::unique_ptr<Logger> Log;
+  std::optional<unsigned> LastFunctionNumber;
 };
 
 class DevelopmentModePriorityAdvisorAnalysisLegacy final
diff --git a/llvm/test/CodeGen/MLRegAlloc/dev-mode-log-amdgpu.ll b/llvm/test/CodeGen/MLRegAlloc/dev-mode-log-amdgpu.ll
index f5b3bee1fd802b..07f57b015197fd 100644
--- a/llvm/test/CodeGen/MLRegAlloc/dev-mode-log-amdgpu.ll
+++ b/llvm/test/CodeGen/MLRegAlloc/dev-mode-log-amdgpu.ll
@@ -15,18 +15,26 @@
 ; RUN:   -regalloc-priority-training-log=%t2 < %s
 ; RUN: FileCheck --input-file %t2 %s
 
-; CHECK:     {"context":"f"}
+; CHECK:     {"context":""}
+; CHECK-NOT: {"context"
+; CHECK:     {"context":""}
 ; CHECK-NOT: {"context"
-; CHECK:     {"context":"g"}
+; CHECK:     {"context":"f"}
 ; CHECK-NOT: {"context"
 
-define amdgpu_kernel void @f(ptr addrspace(1) %p) {
+define amdgpu_kernel void @0(ptr addrspace(1) %p) {
+  %v = load volatile i32, ptr addrspace(1) %p
+  store volatile i32 %v, ptr addrspace(1) %p
+  ret void
+}
+
+define amdgpu_kernel void @1(ptr addrspace(1) %p) {
   %v = load volatile i32, ptr addrspace(1) %p
   store volatile i32 %v, ptr addrspace(1) %p
   ret void
 }
 
-define amdgpu_kernel void @g(ptr addrspace(1) %p) {
+define amdgpu_kernel void @f(ptr addrspace(1) %p) {
   %v = load volatile i32, ptr addrspace(1) %p
   store volatile i32 %v, ptr addrspace(1) %p
   ret void

>From 6de89e86cbc92c2410597fccf658b176d7d1c86a Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Thu, 8 Oct 2026 07:29:02 +0200
Subject: [PATCH 3/4] address comments

---
 llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp   | 15 +++++--
 .../lib/CodeGen/MLRegAllocPriorityAdvisor.cpp | 15 +++++--
 .../CodeGen/MLRegAlloc/dev-mode-log-amdgpu.ll | 42 +++++++++++++++----
 3 files changed, 57 insertions(+), 15 deletions(-)

diff --git a/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp b/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp
index 4c4ef7e1f82a2c..aa3eaf6d6f651b 100644
--- a/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp
+++ b/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp
@@ -551,13 +551,16 @@ class DevelopmentModeEvictionAdvisorProvider final
 
   void logRewardIfNeeded(const MachineFunction &MF,
                          llvm::function_ref<float()> GetReward) override {
-    if (!Log || !Log->hasAnyObservationForContext(MF.getName()))
+    if (!Log)
+      return;
+    std::string Ctx = getContextName(MF);
+    if (!Log->hasAnyObservationForContext(Ctx))
       return;
     // The function pass manager would run all the function passes for a
     // function, so we assume the last context belongs to this function. If
     // this invariant ever changes, we can implement at that time switching
     // contexts. At this point, it'd be an error
-    if (Log->currentContext() != MF.getName()) {
+    if (Log->currentContext() != Ctx) {
       MF.getFunction().getContext().emitError(
           "The training log context shouldn't have had changed.");
     }
@@ -572,7 +575,7 @@ class DevelopmentModeEvictionAdvisorProvider final
       return nullptr;
     if (Log && LastFunctionNumber != MF.getFunctionNumber()) {
       LastFunctionNumber = MF.getFunctionNumber();
-      Log->switchContext(MF.getName());
+      Log->switchContext(getContextName(MF));
     }
     assert(MBFI && Loops &&
            "Invalid provider state: must have analysis available");
@@ -587,6 +590,12 @@ class DevelopmentModeEvictionAdvisorProvider final
   std::unique_ptr<MLModelRunner> Runner;
   std::unique_ptr<Logger> Log;
   std::optional<unsigned> LastFunctionNumber;
+
+  static std::string getContextName(const MachineFunction &MF) {
+    if (!MF.getName().empty())
+      return MF.getName().str();
+    return ("__unnamed_" + Twine(MF.getFunctionNumber())).str();
+  }
 };
 
 class DevelopmentModeEvictionAdvisorAnalysisLegacy final
diff --git a/llvm/lib/CodeGen/MLRegAllocPriorityAdvisor.cpp b/llvm/lib/CodeGen/MLRegAllocPriorityAdvisor.cpp
index d3898421518e82..907cc00876dff8 100644
--- a/llvm/lib/CodeGen/MLRegAllocPriorityAdvisor.cpp
+++ b/llvm/lib/CodeGen/MLRegAllocPriorityAdvisor.cpp
@@ -250,13 +250,16 @@ class DevelopmentModePriorityAdvisorProvider final
 
   void logRewardIfNeeded(const MachineFunction &MF,
                          llvm::function_ref<float()> GetReward) override {
-    if (!Log || !Log->hasAnyObservationForContext(MF.getName()))
+    if (!Log)
+      return;
+    std::string Ctx = getContextName(MF);
+    if (!Log->hasAnyObservationForContext(Ctx))
       return;
     // The function pass manager would run all the function passes for a
     // function, so we assume the last context belongs to this function. If
     // this invariant ever changes, we can implement at that time switching
     // contexts. At this point, it'd be an error
-    if (Log->currentContext() != MF.getName()) {
+    if (Log->currentContext() != Ctx) {
       MF.getFunction().getContext().emitError(
           "The training log context shouldn't have had changed.");
     }
@@ -271,7 +274,7 @@ class DevelopmentModePriorityAdvisorProvider final
       return nullptr;
     if (Log && LastFunctionNumber != MF.getFunctionNumber()) {
       LastFunctionNumber = MF.getFunctionNumber();
-      Log->switchContext(MF.getName());
+      Log->switchContext(getContextName(MF));
     }
     return std::make_unique<DevelopmentModePriorityAdvisor>(
         MF, RA, &SI, Runner.get(), Log.get());
@@ -280,6 +283,12 @@ class DevelopmentModePriorityAdvisorProvider final
   std::unique_ptr<MLModelRunner> Runner;
   std::unique_ptr<Logger> Log;
   std::optional<unsigned> LastFunctionNumber;
+
+  static std::string getContextName(const MachineFunction &MF) {
+    if (!MF.getName().empty())
+      return MF.getName().str();
+    return ("__unnamed_" + Twine(MF.getFunctionNumber())).str();
+  }
 };
 
 class DevelopmentModePriorityAdvisorAnalysisLegacy final
diff --git a/llvm/test/CodeGen/MLRegAlloc/dev-mode-log-amdgpu.ll b/llvm/test/CodeGen/MLRegAlloc/dev-mode-log-amdgpu.ll
index 07f57b015197fd..b1b24f3a8f2fca 100644
--- a/llvm/test/CodeGen/MLRegAlloc/dev-mode-log-amdgpu.ll
+++ b/llvm/test/CodeGen/MLRegAlloc/dev-mode-log-amdgpu.ll
@@ -8,32 +8,56 @@
 ; RUN: llc -o /dev/null -mtriple=amdgpu9.0a-amd-amdhsa \
 ; RUN:   -regalloc-enable-advisor=development -mlregalloc-num-allocatable-regs=256 \
 ; RUN:   -regalloc-training-log=%t1 < %s
-; RUN: FileCheck --input-file %t1 %s
+; RUN: grep -a '^{"\(context\|observation\|outcome\)"' %t1 \
+; RUN:   | FileCheck %s --check-prefix=EVICT --match-full-lines
 
 ; RUN: llc -o /dev/null -mtriple=amdgpu9.0a-amd-amdhsa \
 ; RUN:   -regalloc-enable-priority-advisor=development \
 ; RUN:   -regalloc-priority-training-log=%t2 < %s
-; RUN: FileCheck --input-file %t2 %s
-
-; CHECK:     {"context":""}
-; CHECK-NOT: {"context"
-; CHECK:     {"context":""}
-; CHECK-NOT: {"context"
-; CHECK:     {"context":"f"}
-; CHECK-NOT: {"context"
+; RUN: grep -a '^{"\(context\|observation\|outcome\)"' %t2 \
+; RUN:   | FileCheck %s --check-prefix=PRIO --match-full-lines
 
+; EVICT:      {"context":"__unnamed_0"}
+; PRIO:      {"context":"__unnamed_0"}
+; PRIO-NEXT: {"observation":0}
+; PRIO-NEXT: {"outcome":0}
+; PRIO-NEXT: {"observation":1}
+; PRIO-NEXT: {"outcome":1}
+; PRIO-NEXT: {"observation":2}
+; PRIO-NEXT: {"outcome":2}
+; PRIO-NEXT: {"observation":3}
 define amdgpu_kernel void @0(ptr addrspace(1) %p) {
   %v = load volatile i32, ptr addrspace(1) %p
   store volatile i32 %v, ptr addrspace(1) %p
   ret void
 }
 
+; EVICT-NEXT: {"context":"__unnamed_1"}
+; PRIO-NEXT: {"context":"__unnamed_1"}
+; PRIO-NEXT: {"observation":0}
+; PRIO-NEXT: {"outcome":0}
+; PRIO-NEXT: {"observation":1}
+; PRIO-NEXT: {"outcome":1}
+; PRIO-NEXT: {"observation":2}
+; PRIO-NEXT: {"outcome":2}
+; PRIO-NEXT: {"observation":3}
 define amdgpu_kernel void @1(ptr addrspace(1) %p) {
   %v = load volatile i32, ptr addrspace(1) %p
   store volatile i32 %v, ptr addrspace(1) %p
   ret void
 }
 
+; EVICT-NEXT: {"context":"f"}
+; PRIO-NEXT: {"context":"f"}
+; PRIO-NEXT: {"observation":0}
+; PRIO-NEXT: {"outcome":0}
+; PRIO-NEXT: {"observation":1}
+; PRIO-NEXT: {"outcome":1}
+; PRIO-NEXT: {"observation":2}
+; PRIO-NEXT: {"outcome":2}
+; PRIO-NEXT: {"observation":3}
+; EVICT-NOT:  {{.}}
+; PRIO-NOT:  {{.}}
 define amdgpu_kernel void @f(ptr addrspace(1) %p) {
   %v = load volatile i32, ptr addrspace(1) %p
   store volatile i32 %v, ptr addrspace(1) %p

>From 7ac996bc8dceae7c644d6203f3d60c15f3b8258b Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Fri, 9 Oct 2026 10:12:29 +0200
Subject: [PATCH 4/4] address comments

---
 llvm/include/llvm/Analysis/Utils/TrainingLogger.h   | 9 +++++++++
 llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp         | 4 +---
 llvm/lib/CodeGen/MLRegAllocPriorityAdvisor.cpp      | 4 +---
 llvm/test/CodeGen/MLRegAlloc/dev-mode-log-amdgpu.ll | 8 ++++----
 4 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/llvm/include/llvm/Analysis/Utils/TrainingLogger.h b/llvm/include/llvm/Analysis/Utils/TrainingLogger.h
index f8c653e3e3c76d..18d3d7dbe84724 100644
--- a/llvm/include/llvm/Analysis/Utils/TrainingLogger.h
+++ b/llvm/include/llvm/Analysis/Utils/TrainingLogger.h
@@ -57,6 +57,7 @@
 #include "llvm/Support/Compiler.h"
 
 #include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/Twine.h"
 #include "llvm/Analysis/TensorSpec.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/Support/JSON.h"
@@ -141,5 +142,13 @@ class Logger final {
   }
 };
 
+/// Context name for \p Name. An empty name gets a placeholder built from
+/// \p Number, braced so it does not clash with ordinary symbol names.
+inline std::string getLoggerContextName(StringRef Name, unsigned Number) {
+  if (!Name.empty())
+    return Name.str();
+  return ("{__unnamed_" + Twine(Number) + "}").str();
+}
+
 } // namespace llvm
 #endif // LLVM_ANALYSIS_UTILS_TRAININGLOGGER_H
diff --git a/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp b/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp
index aa3eaf6d6f651b..cdc558e5ee3abc 100644
--- a/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp
+++ b/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp
@@ -592,9 +592,7 @@ class DevelopmentModeEvictionAdvisorProvider final
   std::optional<unsigned> LastFunctionNumber;
 
   static std::string getContextName(const MachineFunction &MF) {
-    if (!MF.getName().empty())
-      return MF.getName().str();
-    return ("__unnamed_" + Twine(MF.getFunctionNumber())).str();
+    return getLoggerContextName(MF.getName(), MF.getFunctionNumber());
   }
 };
 
diff --git a/llvm/lib/CodeGen/MLRegAllocPriorityAdvisor.cpp b/llvm/lib/CodeGen/MLRegAllocPriorityAdvisor.cpp
index 907cc00876dff8..69be275be06a46 100644
--- a/llvm/lib/CodeGen/MLRegAllocPriorityAdvisor.cpp
+++ b/llvm/lib/CodeGen/MLRegAllocPriorityAdvisor.cpp
@@ -285,9 +285,7 @@ class DevelopmentModePriorityAdvisorProvider final
   std::optional<unsigned> LastFunctionNumber;
 
   static std::string getContextName(const MachineFunction &MF) {
-    if (!MF.getName().empty())
-      return MF.getName().str();
-    return ("__unnamed_" + Twine(MF.getFunctionNumber())).str();
+    return getLoggerContextName(MF.getName(), MF.getFunctionNumber());
   }
 };
 
diff --git a/llvm/test/CodeGen/MLRegAlloc/dev-mode-log-amdgpu.ll b/llvm/test/CodeGen/MLRegAlloc/dev-mode-log-amdgpu.ll
index b1b24f3a8f2fca..65c948df41caa3 100644
--- a/llvm/test/CodeGen/MLRegAlloc/dev-mode-log-amdgpu.ll
+++ b/llvm/test/CodeGen/MLRegAlloc/dev-mode-log-amdgpu.ll
@@ -17,8 +17,8 @@
 ; RUN: grep -a '^{"\(context\|observation\|outcome\)"' %t2 \
 ; RUN:   | FileCheck %s --check-prefix=PRIO --match-full-lines
 
-; EVICT:      {"context":"__unnamed_0"}
-; PRIO:      {"context":"__unnamed_0"}
+; EVICT:      {"context":"{__unnamed_0}"}
+; PRIO:      {"context":"{__unnamed_0}"}
 ; PRIO-NEXT: {"observation":0}
 ; PRIO-NEXT: {"outcome":0}
 ; PRIO-NEXT: {"observation":1}
@@ -32,8 +32,8 @@ define amdgpu_kernel void @0(ptr addrspace(1) %p) {
   ret void
 }
 
-; EVICT-NEXT: {"context":"__unnamed_1"}
-; PRIO-NEXT: {"context":"__unnamed_1"}
+; EVICT-NEXT: {"context":"{__unnamed_1}"}
+; PRIO-NEXT: {"context":"{__unnamed_1}"}
 ; PRIO-NEXT: {"observation":0}
 ; PRIO-NEXT: {"outcome":0}
 ; PRIO-NEXT: {"observation":1}



More information about the llvm-commits mailing list