[llvm] 9e01927 - [MLGO] Avoid NaN regalloc features for unspillable live ranges (#229751)

via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 8 07:58:47 PDT 2026


Author: Arseniy Obolenskiy
Date: 2026-10-08T16:58:41+02:00
New Revision: 9e01927d0ab198ba32d15fde4e3817819b3865a0

URL: https://github.com/llvm/llvm-project/commit/9e01927d0ab198ba32d15fde4e3817819b3865a0
DIFF: https://github.com/llvm/llvm-project/commit/9e01927d0ab198ba32d15fde4e3817819b3865a0.diff

LOG: [MLGO] Avoid NaN regalloc features for unspillable live ranges (#229751)

Unspillable ranges have infinite weight, so use_def_density became NaN
for them and 0 for all other candidates

That makes use_def_density unusable for training in every case with
unspillable candidates

Added: 
    llvm/test/CodeGen/MLRegAlloc/unspillable-use-def-density.ll

Modified: 
    llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp b/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp
index 67cf3305f3b99..7592db52971a1 100644
--- a/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp
+++ b/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp
@@ -39,6 +39,7 @@
 #include "llvm/Support/ErrorHandling.h"
 
 #include <bitset>
+#include <cmath>
 #include <memory>
 
 using namespace llvm;
@@ -817,7 +818,8 @@ MCRegister MLEvictAdvisor::tryFindEvictionCandidate(
     if (DoNotNormalize.test(FeatureIndex))
       continue;
     for (size_t Pos = 0; Pos < NumColumns; ++Pos) {
-      Runner->getTensor<float>(FeatureIndex)[Pos] /= Largest[FeatureIndex];
+      float &V = Runner->getTensor<float>(FeatureIndex)[Pos];
+      V = std::isinf(V) ? 1.0f : V / Largest[FeatureIndex];
     }
   }
   *Runner->getTensor<float>(FeatureIDs::progress) =
@@ -969,9 +971,9 @@ void MLEvictAdvisor::extractFeatures(
 #define SET(ID, TYPE, VAL)                                                     \
   do {                                                                         \
     Runner->getTensor<TYPE>(FeatureIDs::ID)[Pos] = static_cast<TYPE>(VAL);     \
-    if (!DoNotNormalize.test(FeatureIDs::ID))                                  \
-      Largest[FeatureIDs::ID] =                                                \
-          std::max(Largest[FeatureIDs::ID], static_cast<float>(VAL));          \
+    float F = static_cast<float>(VAL);                                         \
+    if (!DoNotNormalize.test(FeatureIDs::ID) && !std::isinf(F))                \
+      Largest[FeatureIDs::ID] = std::max(Largest[FeatureIDs::ID], F);          \
   } while (false)
   SET(mask, int64_t, 1);
   SET(is_free, int64_t, Intervals.empty());

diff  --git a/llvm/test/CodeGen/MLRegAlloc/unspillable-use-def-density.ll b/llvm/test/CodeGen/MLRegAlloc/unspillable-use-def-density.ll
new file mode 100644
index 0000000000000..1c5b978bff205
--- /dev/null
+++ b/llvm/test/CodeGen/MLRegAlloc/unspillable-use-def-density.ll
@@ -0,0 +1,33 @@
+; REQUIRES: x86_64-linux, amdgpu-registered-target
+; RUN: rm -rf %t.rundir
+; RUN: rm -rf %t.channel-basename.*
+; RUN: mkdir %t.rundir
+; RUN: cp %S/../../../lib/Analysis/models/log_reader.py %t.rundir
+; RUN: cp %S/../../../lib/Analysis/models/interactive_host.py %t.rundir
+; RUN: cp %S/Inputs/interactive_main.py %t.rundir
+; RUN: %python %t.rundir/interactive_main.py %t.channel-basename \
+; RUN:    llc -mtriple=amdgpu9.08-amd-amdhsa -sgpr-regalloc=fast -wwm-regalloc=fast \
+; RUN:    -regalloc-enable-advisor=release -interactive-model-runner-echo-reply \
+; RUN:    -regalloc-evict-interactive-channel-base=%t.channel-basename %s -o /dev/null | \
+; RUN:    FileCheck %s --implicit-check-not=nan
+
+;; Unspillable live ranges have infinite weight. Their use_def_density is
+;; normalized to 1.0 and does not affect the normalization of the others.
+;; The fast SGPR and WWM allocators keep greedy to a single context.
+
+; CHECK:      observation: 0
+; CHECK-NEXT: mask: 1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
+; CHECK:      use_def_density: 1.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0
+
+define amdgpu_kernel void @test_spill_av_class(<4 x i32> %arg) #0 {
+  %v0 = call i32 asm sideeffect "; def $0", "=v"()
+  %tmp = insertelement <2 x i32> poison, i32 %v0, i32 0
+  %mai = tail call <4 x i32> @llvm.amdgcn.mfma.i32.4x4x4i8(i32 1, i32 2, <4 x i32> %arg, i32 0, i32 0, i32 0)
+  store volatile <4 x i32> %mai, ptr addrspace(1) poison
+  call void asm sideeffect "; use $0", "v"(<2 x i32> %tmp)
+  ret void
+}
+
+declare <4 x i32> @llvm.amdgcn.mfma.i32.4x4x4i8(i32, i32, <4 x i32>, i32, i32, i32)
+
+attributes #0 = { nounwind "amdgpu-num-vgpr"="5" }


        


More information about the llvm-commits mailing list