[llvm] [RegisterCoalescer] Avoid retrying high-cost joins for live intervals (PR #207976)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 9 00:38:43 PDT 2026
https://github.com/Jolyon0202 updated https://github.com/llvm/llvm-project/pull/207976
>From 4582e5cb72e2bf261e8ee7cf77915d1e135a98a1 Mon Sep 17 00:00:00 2001
From: YangJian <yangjian.c at bytedance.com>
Date: Mon, 6 Jul 2026 15:03:44 +0800
Subject: [PATCH] [RegisterCoalescer] Avoid retrying high-cost joins for large
segmented intervals
Recently we found a compile time problem in the case that contains much high-cost live
interval. And normal interference failures still set `Again=true` and can be retried
(it cost N^2 complexity). We tracks when a join failed only because of the high-cost
guard, so that avoids putting such copies back into the retry worklist.
---
llvm/lib/CodeGen/RegisterCoalescer.cpp | 42 ++++++++++++++++++++------
1 file changed, 33 insertions(+), 9 deletions(-)
diff --git a/llvm/lib/CodeGen/RegisterCoalescer.cpp b/llvm/lib/CodeGen/RegisterCoalescer.cpp
index 4b4ba2144f964..6955bcf2df57a 100644
--- a/llvm/lib/CodeGen/RegisterCoalescer.cpp
+++ b/llvm/lib/CodeGen/RegisterCoalescer.cpp
@@ -194,6 +194,11 @@ class RegisterCoalescer : private LiveRangeEdit::Delegate {
/// has been tried to join with other live interval.
DenseMap<Register, unsigned long> LargeLIVisitCounter;
+ /// True if the last virtual-register join was rejected only because a live
+ /// interval crossed the high-cost threshold. Such copies should not be kept
+ /// for later retry unless another cheap transform can eliminate them now.
+ bool LastJoinSkippedHighCost = false;
+
/// Recursively eliminate dead defs in DeadDefs.
void eliminateDeadDefs(LiveRangeEdit *Edit = nullptr);
@@ -243,10 +248,10 @@ class RegisterCoalescer : private LiveRangeEdit::Delegate {
/// Attempt to join these two intervals. On failure, this
/// returns false. The output "SrcInt" will not have been modified, so we
/// can use this information below to update aliases.
- bool joinIntervals(CoalescerPair &CP);
+ bool joinIntervals(CoalescerPair &CP, bool &JoinSkippedHighCost);
/// Attempt joining two virtual registers. Return true on success.
- bool joinVirtRegs(CoalescerPair &CP);
+ bool joinVirtRegs(CoalescerPair &CP, bool &JoinSkippedHighCost);
/// If a live interval has many valnos and is coalesced with other
/// live intervals many times, we regard such live interval as having
@@ -2181,12 +2186,13 @@ bool RegisterCoalescer::joinCopy(
ShrinkMask = LaneBitmask::getNone();
ShrinkMainRange = false;
+ bool JoinSkippedHighCost = false;
// Okay, attempt to join these two intervals. On failure, this returns false.
// Otherwise, if one of the intervals being joined is a physreg, this method
// always canonicalizes DstInt to be it. The output "SrcInt" will not have
// been modified, so we can use this information below to update aliases.
- if (!joinIntervals(CP)) {
+ if (!joinIntervals(CP, JoinSkippedHighCost)) {
// Coalescing failed.
// Try rematerializing the definition of the source if it is cheap.
@@ -2221,8 +2227,17 @@ bool RegisterCoalescer::joinCopy(
return true;
// Otherwise, we are unable to join the intervals.
- LLVM_DEBUG(dbgs() << "\tInterference!\n");
- Again = true; // May be possible to coalesce later.
+ LLVM_DEBUG(dbgs() << "\tInterference! ");
+ // A high-cost interval is already too expensive to retry. Keeping the copy
+ // in WorkList would make every subsequent successful join rescan it again,
+ // which can dominate compile time.
+ if (JoinSkippedHighCost)
+ LLVM_DEBUG(
+ dbgs()
+ << " Not retrying because a high-cost live interval was reached.\n");
+ else
+ LLVM_DEBUG(dbgs() << " Will retry later.\n");
+ Again = !JoinSkippedHighCost; // May be possible to coalesce later.
return false;
}
@@ -3692,7 +3707,8 @@ bool RegisterCoalescer::isHighCostLiveInterval(LiveInterval &LI) {
return true;
}
-bool RegisterCoalescer::joinVirtRegs(CoalescerPair &CP) {
+bool RegisterCoalescer::joinVirtRegs(CoalescerPair &CP,
+ bool &JoinSkippedHighCost) {
SmallVector<VNInfo *, 16> NewVNInfo;
LiveInterval &RHS = LIS->getInterval(CP.getSrcReg());
LiveInterval &LHS = LIS->getInterval(CP.getDstReg());
@@ -3704,8 +3720,14 @@ bool RegisterCoalescer::joinVirtRegs(CoalescerPair &CP) {
LLVM_DEBUG(dbgs() << "\t\tRHS = " << RHS << "\n\t\tLHS = " << LHS << '\n');
- if (isHighCostLiveInterval(LHS) || isHighCostLiveInterval(RHS))
+ if (isHighCostLiveInterval(LHS) || isHighCostLiveInterval(RHS)) {
+ JoinSkippedHighCost = true;
+ LLVM_DEBUG(dbgs() << "\t\tHigh-cost live interval: RHS valnos="
+ << RHS.valnos.size() << ", segments=" << RHS.size()
+ << "; LHS valnos=" << LHS.valnos.size()
+ << ", segments=" << LHS.size() << '\n');
return false;
+ }
// First compute NewVNInfo and the simple value mappings.
// Detect impossible conflicts early.
@@ -3872,8 +3894,10 @@ bool RegisterCoalescer::joinVirtRegs(CoalescerPair &CP) {
return true;
}
-bool RegisterCoalescer::joinIntervals(CoalescerPair &CP) {
- return CP.isPhys() ? joinReservedPhysReg(CP) : joinVirtRegs(CP);
+bool RegisterCoalescer::joinIntervals(CoalescerPair &CP,
+ bool &JoinSkippedHighCost) {
+ return CP.isPhys() ? joinReservedPhysReg(CP)
+ : joinVirtRegs(CP, JoinSkippedHighCost);
}
void RegisterCoalescer::buildVRegToDbgValueMap(MachineFunction &MF) {
More information about the llvm-commits
mailing list