[llvm] AMDGPU: Don't use PressureDiffs with implicit allocatable physregs (PR #230916)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 11 00:57:07 PDT 2026


https://github.com/arsenm created https://github.com/llvm/llvm-project/pull/230916

canUsePressureDiffs skipped all implicit operands, so instructions
reading an allocatable physreg implicitly (e.g., vcc on v_cndmask_b32)
used the cached PressureDiff. PressureDiffs assume a single use for
physregs (see the FIXME in ScheduleDAGMILive::updatePressureDiffs), so
the pressure was added for each user. With several users in a region, the
SGPR pressure was overestimated and failed the expensive checks pressure
validation.

Fall back to the RegPressureTracker for implicit operands of
allocatable physregs, which are the ones it tracks.

Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>

>From ede9be4cd6c1c983baf48e597f21c7f295aab4ee Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Sat, 10 Oct 2026 10:42:11 +0200
Subject: [PATCH] AMDGPU: Don't use PressureDiffs with implicit allocatable
 physregs

canUsePressureDiffs skipped all implicit operands, so instructions
reading an allocatable physreg implicitly (e.g., vcc on v_cndmask_b32)
used the cached PressureDiff. PressureDiffs assume a single use for
physregs (see the FIXME in ScheduleDAGMILive::updatePressureDiffs), so
the pressure was added for each user. With several users in a region, the
SGPR pressure was overestimated and failed the expensive checks pressure
validation.

Fall back to the RegPressureTracker for implicit operands of
allocatable physregs, which are the ones it tracks.

Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>
---
 llvm/lib/Target/AMDGPU/GCNSchedStrategy.cpp   | 18 ++++---
 .../CodeGen/AMDGPU/early-if-convert-cost.ll   | 52 +++++++++++++++++++
 ...schedule-pressure-implicit-physreg-use.mir | 51 ++++++++++++++++++
 3 files changed, 115 insertions(+), 6 deletions(-)
 create mode 100644 llvm/test/CodeGen/AMDGPU/schedule-pressure-implicit-physreg-use.mir

diff --git a/llvm/lib/Target/AMDGPU/GCNSchedStrategy.cpp b/llvm/lib/Target/AMDGPU/GCNSchedStrategy.cpp
index 4e48f72b4bbf4..de59f3478ea53 100644
--- a/llvm/lib/Target/AMDGPU/GCNSchedStrategy.cpp
+++ b/llvm/lib/Target/AMDGPU/GCNSchedStrategy.cpp
@@ -230,16 +230,22 @@ void GCNSchedStrategy::initialize(ScheduleDAGMI *DAG) {
 /// already allows 80% of SUs to take the fast path without changing scheduling
 /// at all. Further changes would either change scheduling, or require a lot
 /// more logic to recover an accurate pressure estimate from the PressureDiffs.
-static bool canUsePressureDiffs(const SUnit &SU) {
+static bool canUsePressureDiffs(const SUnit &SU,
+                                const MachineRegisterInfo &MRI) {
   if (!SU.isInstr())
     return false;
 
-  // Cannot use pressure diffs for subregister defs or with physregs, it's
-  // imprecise in both cases. For a bundle, check the instructions inside it:
-  // the BUNDLE header only has implicit operands.
+  // Cannot use pressure diffs for subregister defs or with tracked physregs,
+  // it's imprecise in both cases. For a bundle, check the instructions inside
+  // it: the BUNDLE header only has implicit operands.
   for (const auto &Op : const_mi_bundle_ops(*SU.getInstr())) {
-    if (!Op.isReg() || Op.isImplicit())
+    if (!Op.isReg())
       continue;
+    if (Op.isImplicit()) {
+      if (Op.getReg().isPhysical() && MRI.isAllocatable(Op.getReg()))
+        return false;
+      continue;
+    }
     if (Op.getReg().isPhysical() ||
         (Op.isDef() && Op.getSubReg() != AMDGPU::NoSubRegister))
       return false;
@@ -308,7 +314,7 @@ void GCNSchedStrategy::initCandidate(SchedCandidate &Cand, SUnit *SU,
   //
   // In EXPENSIVE_CHECKS, we always query RPTracker to verify the results of
   // PressureDiffs.
-  if (AtTop || !canUsePressureDiffs(*SU) || useGCNTrackers()) {
+  if (AtTop || !canUsePressureDiffs(*SU, DAG->MRI) || useGCNTrackers()) {
     getRegisterPressures(AtTop, RPTracker, SU, Pressure, MaxPressure,
                          DownwardTracker, UpwardTracker, DAG, SRI);
   } else {
diff --git a/llvm/test/CodeGen/AMDGPU/early-if-convert-cost.ll b/llvm/test/CodeGen/AMDGPU/early-if-convert-cost.ll
index 3cf4a37c34992..e640a095e2b40 100644
--- a/llvm/test/CodeGen/AMDGPU/early-if-convert-cost.ll
+++ b/llvm/test/CodeGen/AMDGPU/early-if-convert-cost.ll
@@ -110,3 +110,55 @@ endif:
   store <4 x i32> %r, ptr addrspace(1) %out
   ret void
 }
+
+; GCN-LABEL: {{^}}test_vccnz_ifcvt_triangle96_func:
+; GCN: v_cmp_neq_f32_e64 vcc, s{{[0-9]+}}, 1.0
+
+; GCN: v_add_i32_e64
+; GCN: v_add_i32_e64
+; GCN: v_add_i32_e64
+
+; GCN: v_cndmask_b32_e32 v{{[0-9]+}}, v{{[0-9]+}}, v{{[0-9]+}}, vcc
+; GCN: v_cndmask_b32_e32 v{{[0-9]+}}, v{{[0-9]+}}, v{{[0-9]+}}, vcc
+; GCN: v_cndmask_b32_e32 v{{[0-9]+}}, v{{[0-9]+}}, v{{[0-9]+}}, vcc
+
+define <3 x i32> @test_vccnz_ifcvt_triangle96_func(<3 x i32> %v, float inreg %cnd) #0 {
+entry:
+  %cc = fcmp oeq float %cnd, 1.000000e+00
+  br i1 %cc, label %if, label %endif
+
+if:
+  %u = add <3 x i32> %v, %v
+  br label %endif
+
+endif:
+  %r = phi <3 x i32> [ %v, %entry ], [ %u, %if ]
+  ret <3 x i32> %r
+}
+
+; GCN-LABEL: {{^}}test_vccnz_ifcvt_triangle128_func:
+; GCN: v_cmp_neq_f32_e64 vcc, s{{[0-9]+}}, 1.0
+
+; GCN: v_add_i32_e64
+; GCN: v_add_i32_e64
+; GCN: v_add_i32_e64
+; GCN: v_add_i32_e64
+
+; GCN: v_cndmask_b32_e32 v{{[0-9]+}}, v{{[0-9]+}}, v{{[0-9]+}}, vcc
+; GCN: v_cndmask_b32_e32 v{{[0-9]+}}, v{{[0-9]+}}, v{{[0-9]+}}, vcc
+; GCN: v_cndmask_b32_e32 v{{[0-9]+}}, v{{[0-9]+}}, v{{[0-9]+}}, vcc
+; GCN: v_cndmask_b32_e32 v{{[0-9]+}}, v{{[0-9]+}}, v{{[0-9]+}}, vcc
+
+define <4 x i32> @test_vccnz_ifcvt_triangle128_func(<4 x i32> %v, float inreg %cnd) #0 {
+entry:
+  %cc = fcmp oeq float %cnd, 1.000000e+00
+  br i1 %cc, label %if, label %endif
+
+if:
+  %u = add <4 x i32> %v, %v
+  br label %endif
+
+endif:
+  %r = phi <4 x i32> [ %v, %entry ], [ %u, %if ]
+  ret <4 x i32> %r
+}
diff --git a/llvm/test/CodeGen/AMDGPU/schedule-pressure-implicit-physreg-use.mir b/llvm/test/CodeGen/AMDGPU/schedule-pressure-implicit-physreg-use.mir
new file mode 100644
index 0000000000000..dbe437a4ea99e
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/schedule-pressure-implicit-physreg-use.mir
@@ -0,0 +1,51 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
+# RUN: llc -mtriple=amdgpu6.01 -run-pass=machine-scheduler %s -o - | FileCheck %s
+
+# PressureDiffs assume a single use of a physreg, so each implicit
+# $vcc reader was counted as making $vcc live, adding its pressure
+# once per reader instead of once. Make sure this doesn't fail the
+# expensive checks pressure consistency check.
+
+---
+name: multiple_implicit_vcc_uses
+tracksRegLiveness: true
+body: |
+  bb.0:
+    liveins: $vgpr0, $vgpr1, $vgpr2, $sgpr16
+
+    ; CHECK-LABEL: name: multiple_implicit_vcc_uses
+    ; CHECK: liveins: $vgpr0, $vgpr1, $vgpr2, $sgpr16
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: [[COPY:%[0-9]+]]:sgpr_32 = COPY $sgpr16
+    ; CHECK-NEXT: [[COPY1:%[0-9]+]]:vgpr_32 = COPY $vgpr2
+    ; CHECK-NEXT: [[COPY2:%[0-9]+]]:vgpr_32 = COPY $vgpr1
+    ; CHECK-NEXT: [[COPY3:%[0-9]+]]:vgpr_32 = COPY $vgpr0
+    ; CHECK-NEXT: [[V_CMP_NEQ_F32_e64_:%[0-9]+]]:sreg_64 = nofpexcept V_CMP_NEQ_F32_e64 0, 1065353216, 0, [[COPY]], 0, implicit $mode, implicit $exec
+    ; CHECK-NEXT: [[V_ADD_CO_U32_e64_:%[0-9]+]]:vgpr_32, dead [[V_ADD_CO_U32_e64_1:%[0-9]+]]:sreg_64 = V_ADD_CO_U32_e64 [[COPY1]], [[COPY1]], 0, implicit $exec
+    ; CHECK-NEXT: [[V_ADD_CO_U32_e64_2:%[0-9]+]]:vgpr_32, dead [[V_ADD_CO_U32_e64_3:%[0-9]+]]:sreg_64 = V_ADD_CO_U32_e64 [[COPY2]], [[COPY2]], 0, implicit $exec
+    ; CHECK-NEXT: [[V_ADD_CO_U32_e64_4:%[0-9]+]]:vgpr_32, dead [[V_ADD_CO_U32_e64_5:%[0-9]+]]:sreg_64 = V_ADD_CO_U32_e64 [[COPY3]], [[COPY3]], 0, implicit $exec
+    ; CHECK-NEXT: $vcc = COPY [[V_CMP_NEQ_F32_e64_]]
+    ; CHECK-NEXT: [[V_CNDMASK_B32_e32_:%[0-9]+]]:vgpr_32 = V_CNDMASK_B32_e32 [[V_ADD_CO_U32_e64_4]], [[COPY3]], implicit $vcc, implicit $exec
+    ; CHECK-NEXT: [[V_CNDMASK_B32_e32_1:%[0-9]+]]:vgpr_32 = V_CNDMASK_B32_e32 [[V_ADD_CO_U32_e64_2]], [[COPY2]], implicit $vcc, implicit $exec
+    ; CHECK-NEXT: [[V_CNDMASK_B32_e32_2:%[0-9]+]]:vgpr_32 = V_CNDMASK_B32_e32 [[V_ADD_CO_U32_e64_]], [[COPY1]], implicit $vcc, implicit $exec
+    ; CHECK-NEXT: $vgpr0 = COPY [[V_CNDMASK_B32_e32_]]
+    ; CHECK-NEXT: $vgpr1 = COPY [[V_CNDMASK_B32_e32_1]]
+    ; CHECK-NEXT: $vgpr2 = COPY [[V_CNDMASK_B32_e32_2]]
+    ; CHECK-NEXT: SI_RETURN implicit $vgpr0, implicit $vgpr1, implicit $vgpr2
+    %0:sgpr_32 = COPY $sgpr16
+    %1:vgpr_32 = COPY $vgpr2
+    %2:vgpr_32 = COPY $vgpr1
+    %3:vgpr_32 = COPY $vgpr0
+    %4:sreg_64 = nofpexcept V_CMP_NEQ_F32_e64 0, 1065353216, 0, %0, 0, implicit $mode, implicit $exec
+    $vcc = COPY %4
+    %5:vgpr_32, dead %6:sreg_64 = V_ADD_CO_U32_e64 %1, %1, 0, implicit $exec
+    %7:vgpr_32, dead %8:sreg_64 = V_ADD_CO_U32_e64 %2, %2, 0, implicit $exec
+    %9:vgpr_32, dead %10:sreg_64 = V_ADD_CO_U32_e64 %3, %3, 0, implicit $exec
+    %11:vgpr_32 = V_CNDMASK_B32_e32 %9, %3, implicit $vcc, implicit $exec
+    %12:vgpr_32 = V_CNDMASK_B32_e32 %7, %2, implicit $vcc, implicit $exec
+    %13:vgpr_32 = V_CNDMASK_B32_e32 %5, %1, implicit $vcc, implicit $exec
+    $vgpr0 = COPY %11
+    $vgpr1 = COPY %12
+    $vgpr2 = COPY %13
+    SI_RETURN implicit $vgpr0, implicit $vgpr1, implicit $vgpr2
+...



More information about the llvm-commits mailing list