[llvm] [CodeGen] Notify LRE delegate before foldAsLoad erases UseMI (PR #226250)

Sam Valladares via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 24 10:54:55 PDT 2026


https://github.com/samvallad33 created https://github.com/llvm/llvm-project/pull/226250

## Summary

Fixes #222905

`foldAsLoad` erases `UseMI` without calling `LRE_WillEraseInstruction`. `eliminateDeadDef` already does that notify before erase. This path just missed it, so the register coalescer keeps a stale `UseMI*` and crashes later looking like heap corruption.

## Change

In `LiveRangeEdit::foldAsLoad`, call `TheDelegate->LRE_WillEraseInstruction(UseMI)` before `UseMI->eraseFromParent()`, matching the sibling path.

## Testing

No MIR test added. The change mirrors the existing `eliminateDeadDef` notify-before-erase pattern. A full ASan repro needs a backend where `foldMemoryOperandImpl` succeeds for the load being folded while the erased `UseMI` remains on the coalescer worklist; there is not yet a reliably upstream-triggerable MIR case for that.

Assisted-by: Cursor


>From a72f72f544255580a65675a2bb4178b325d1d7d5 Mon Sep 17 00:00:00 2001
From: Sam Valladares <liverpool1.fan10 at gmail.com>
Date: Thu, 24 Sep 2026 10:54:34 -0700
Subject: [PATCH] [CodeGen] Notify LRE delegate before foldAsLoad erases UseMI

foldAsLoad erased UseMI without LRE_WillEraseInstruction, unlike
eliminateDeadDef. RegisterCoalescer then kept a stale worklist pointer
and could hit use-after-poison in joinCopy.

Fixes #222905
---
 llvm/lib/CodeGen/LiveRangeEdit.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/llvm/lib/CodeGen/LiveRangeEdit.cpp b/llvm/lib/CodeGen/LiveRangeEdit.cpp
index 1d6b835f16468..3d25e204743e8 100644
--- a/llvm/lib/CodeGen/LiveRangeEdit.cpp
+++ b/llvm/lib/CodeGen/LiveRangeEdit.cpp
@@ -161,6 +161,8 @@ bool LiveRangeEdit::foldAsLoad(LiveInterval *LI,
   // Update the call info.
   if (UseMI->shouldUpdateAdditionalCallInfo())
     UseMI->getMF()->moveAdditionalCallInfo(UseMI, FoldMI);
+  if (TheDelegate)
+    TheDelegate->LRE_WillEraseInstruction(UseMI);
   UseMI->eraseFromParent();
   DefMI->addRegisterDead(LI->reg(), nullptr);
   Dead.push_back(DefMI);



More information about the llvm-commits mailing list