[llvm] [RegisterCoalescer] Fix compile-time blowup with partially-reserved physregs (PR #181728)
Brian Cain via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 16 21:52:34 PST 2026
https://github.com/androm3da updated https://github.com/llvm/llvm-project/pull/181728
>From 89741d54892359d09a6a2cf5395e4a13b18d32a7 Mon Sep 17 00:00:00 2001
From: Brian Cain <brian.cain at oss.qualcomm.com>
Date: Mon, 16 Feb 2026 11:21:16 -0800
Subject: [PATCH 1/2] [RegisterCoalescer] Fix compile-time blowup with
partially-reserved physregs
canJoinPhys checks isReserved on the super-register but
joinReservedPhysReg additionally requires every register-unit root to
be reserved. When a target reserves only part of a super-register
(e.g. Hexagon -ffixed-r19 reserves R19 and D9, but not R18), the
mismatch causes joinReservedPhysReg to fail, reMaterializeDef to fail,
and the copy to be unconditionally retried (Again = true) every
worklist round. In large functions this O(N^2) retry behaviour leads
to multi-hour compile times.
Add the same regunit-root-reserved check to canJoinPhys so that it
returns false early, routing through the !canJoinPhys path where Again
is only set when IsDefCopy is true.
---
llvm/lib/CodeGen/RegisterCoalescer.cpp | 14 +++++++++++++
.../coalescer-partial-reserved-physreg.mir | 20 +++++++++++++++++++
2 files changed, 34 insertions(+)
create mode 100644 llvm/test/CodeGen/Hexagon/coalescer-partial-reserved-physreg.mir
diff --git a/llvm/lib/CodeGen/RegisterCoalescer.cpp b/llvm/lib/CodeGen/RegisterCoalescer.cpp
index 586c27b7e3baf..6db602ee5fb2a 100644
--- a/llvm/lib/CodeGen/RegisterCoalescer.cpp
+++ b/llvm/lib/CodeGen/RegisterCoalescer.cpp
@@ -1982,6 +1982,20 @@ bool RegisterCoalescer::canJoinPhys(const CoalescerPair &CP) {
return false;
}
+ // Verify all register unit roots are reserved. joinReservedPhysReg requires
+ // this, so reject early to route the copy through the !canJoinPhys path
+ // where Again is only set when IsDefCopy is true.
+ if (!MRI->isConstantPhysReg(CP.getDstReg())) {
+ for (MCRegUnit Unit : TRI->regunits(CP.getDstReg())) {
+ for (MCRegUnitRootIterator RI(Unit, TRI); RI.isValid(); ++RI) {
+ if (!MRI->isReserved(*RI)) {
+ LLVM_DEBUG(dbgs() << "\tNot all register unit roots reserved.\n");
+ return false;
+ }
+ }
+ }
+ }
+
LiveInterval &JoinVInt = LIS->getInterval(CP.getSrcReg());
if (JoinVInt.containsOneValue())
return true;
diff --git a/llvm/test/CodeGen/Hexagon/coalescer-partial-reserved-physreg.mir b/llvm/test/CodeGen/Hexagon/coalescer-partial-reserved-physreg.mir
new file mode 100644
index 0000000000000..1b3c2f306edec
--- /dev/null
+++ b/llvm/test/CodeGen/Hexagon/coalescer-partial-reserved-physreg.mir
@@ -0,0 +1,20 @@
+# RUN: llc -mtriple=hexagon -mattr=+reserved-r19 \
+# RUN: -run-pass=register-coalescer -o - %s | FileCheck %s
+#
+# Regression test for a compile-time blowup in the register coalescer with
+# partially-reserved physical registers.
+#
+# CHECK-LABEL: name: test
+# CHECK: bb.0:
+---
+name: test
+tracksRegLiveness: true
+body: |
+ bb.0:
+ liveins: $r31
+
+ %0:doubleregs = A2_tfrpi 1
+ $r18 = COPY %0.isub_lo:doubleregs
+ J2_jumpr $r31, implicit-def dead $pc, implicit $r18
+
+...
>From 172206735a92bb2cec3b071002851ebdef0dc303 Mon Sep 17 00:00:00 2001
From: Brian Cain <brian.cain at oss.qualcomm.com>
Date: Mon, 16 Feb 2026 21:52:20 -0800
Subject: [PATCH 2/2] fixes
---
llvm/lib/CodeGen/RegisterCoalescer.cpp | 17 +++++++
.../coalescer-partial-reserved-physreg.mir | 49 +++++++++++++++++++
2 files changed, 66 insertions(+)
diff --git a/llvm/lib/CodeGen/RegisterCoalescer.cpp b/llvm/lib/CodeGen/RegisterCoalescer.cpp
index 6db602ee5fb2a..ff2b47acda5eb 100644
--- a/llvm/lib/CodeGen/RegisterCoalescer.cpp
+++ b/llvm/lib/CodeGen/RegisterCoalescer.cpp
@@ -1311,6 +1311,23 @@ bool RegisterCoalescer::reMaterializeDef(const CoalescerPair &CP,
return false;
if (DefMI->isCopyLike()) {
IsDefCopy = true;
+
+ // The retry triggered by IsDefCopy is only useful if the defining
+ // copy could be coalesced in a later round, exposing a rematerializable
+ // definition. When the defining copy's source is a physical register,
+ // that copy typically cannot itself be coalesced: canJoinPhys rejects
+ // non-reserved destinations and reMaterializeDef rejects physical
+ // sources, so the copy is dropped from the worklist on the first
+ // attempt. Retrying the current copy is then futile since the defining
+ // instruction will never change. Suppress IsDefCopy to avoid permanent,
+ // O(N^2) worklist retries in functions with many such copies
+ // (e.g. Hexagon with -ffixed-r19 and packed bitfield struct stores).
+ if (DefMI->isCopy()) {
+ Register DefSrcReg = DefMI->getOperand(1).getReg();
+ if (DefSrcReg.isPhysical())
+ IsDefCopy = false;
+ }
+
return false;
}
if (!TII->isAsCheapAsAMove(*DefMI))
diff --git a/llvm/test/CodeGen/Hexagon/coalescer-partial-reserved-physreg.mir b/llvm/test/CodeGen/Hexagon/coalescer-partial-reserved-physreg.mir
index 1b3c2f306edec..769a7876533e6 100644
--- a/llvm/test/CodeGen/Hexagon/coalescer-partial-reserved-physreg.mir
+++ b/llvm/test/CodeGen/Hexagon/coalescer-partial-reserved-physreg.mir
@@ -4,8 +4,30 @@
# Regression test for a compile-time blowup in the register coalescer with
# partially-reserved physical registers.
#
+# With -ffixed-r19, R19 is reserved and D9 = R18:R19 is partially reserved.
+# canJoinPhys sees D9 as reserved (isReserved(D9) = true) but
+# joinReservedPhysReg requires all register-unit roots to be reserved.
+# R18 is not reserved, so joinReservedPhysReg fails. Without the
+# canJoinPhys regunit-root check, this routes through the joinIntervals
+# failure path where Again = true unconditionally, causing O(N^2) retries.
+#
+# Additionally, when the source virtual register is defined by a COPY
+# from a physical register (test_isdefcopy), IsDefCopy would normally
+# be set, causing retries in the !canJoinPhys path. Since the defining
+# COPY from a physical register cannot itself be coalesced through the
+# normal worklist, retrying is futile.
+#
# CHECK-LABEL: name: test
# CHECK: bb.0:
+# CHECK-LABEL: name: test_isdefcopy
+# CHECK: bb.0:
+--- |
+ target triple = "hexagon"
+ declare void @foo()
+ define void @test() #0 { ret void }
+ define void @test_isdefcopy() #0 { ret void }
+ attributes #0 = { "target-features"="+reserved-r19" }
+...
---
name: test
tracksRegLiveness: true
@@ -18,3 +40,30 @@ body: |
J2_jumpr $r31, implicit-def dead $pc, implicit $r18
...
+---
+# Test that IsDefCopy retries are suppressed when the defining
+# copy's source is a physical register.
+name: test_isdefcopy
+tracksRegLiveness: true
+body: |
+ bb.0:
+ liveins: $r31, $d0, $r19
+
+ ; %0 is defined by COPY from a physical register.
+ ; This COPY is dropped from the worklist (canJoinPhys fails for
+ ; non-reserved $d0, reMaterializeDef rejects physical sources).
+ ; Without the IsDefCopy suppression, the dependent extraction below
+ ; would permanently retry since the defining instruction never changes.
+ %0:doubleregs = COPY $d0
+
+ ; Extract isub_lo to $r18, which is part of D9 = R18:R19.
+ ; canJoinPhys(D9) returns false (R18 root not reserved).
+ ; reMaterializeDef sees DefMI = COPY $d0 (copy-like), but since the
+ ; source is physical, IsDefCopy is suppressed -> no retry.
+ $r18 = COPY %0.isub_lo:doubleregs
+
+ S2_storeri_io $r18, 0, $r19
+ $r0 = A2_tfrsi 0
+ PS_jmpret $r31, implicit-def dead $pc, implicit $r0
+
+...
More information about the llvm-commits
mailing list