[llvm] [LiveDebugVariables] Stop holding SlotIndexes for erased instructions (PR #224467)

Petar Jovanovic via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 28 03:50:27 PDT 2026


================
@@ -1549,6 +1567,122 @@ splitRegister(Register OldReg, ArrayRef<Register> NewRegs, LiveIntervals &LIS) {
     PImpl->splitRegister(OldReg, NewRegs);
 }
 
+//===----------------------------------------------------------------------===//
+//                        Stale Index Canonicalization
+//===----------------------------------------------------------------------===//
+
+void UserValue::canonicalizeIndexes(const SlotIndexes &SI) {
+  unsigned NumStale = 0;
+  for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
+    NumStale += SI.isStaleIndex(I.start()) + SI.isStaleIndex(I.stop());
+  for (SlotIndex Idx : trimmedDefs)
+    NumStale += SI.isStaleIndex(Idx);
+  NumStaleIndexes += NumStale;
+
+  if (NumStale) {
+    // trimmedDefs is looked up by interval start. Remapping it here is safe:
+    // trimmed starts are block slots, so the Stop < Start case below cannot
+    // reach them, and a merge drops a start that then matches nothing.
+    if (!trimmedDefs.empty()) {
+      SmallVector<SlotIndex, 2> Defs(trimmedDefs.begin(), trimmedDefs.end());
+      trimmedDefs.clear();
+      for (SlotIndex Idx : Defs)
+        trimmedDefs.insert(SI.canonicalizeIndex(Idx));
+    }
+
+    // Rebuild rather than move the keys of the existing map: it has to stay
+    // ordered and non-empty at every step, which canonicalization does not
+    // respect.
+    struct CanonicalInterval {
+      SlotIndex Start;
+      SlotIndex Stop;
+      DbgVariableValue Value;
+    };
+    SmallVector<CanonicalInterval, 8> Intervals;
+
+    for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
+      SlotIndex Start = SI.canonicalizeIndex(I.start());
+      SlotIndex Stop = SI.canonicalizeIndex(I.stop());
+
+      // A stale stop can land below a start that sat on the same instruction's
+      // dead slot. Both resolve to the same insert location.
+      if (Stop < Start)
+        Start = Stop;
+
+      if (!Intervals.empty()) {
+        CanonicalInterval &Prev = Intervals.back();
+        if (Start <= Prev.Start) {
+          // Both DBG_VALUEs would be emitted at the same position, where the
+          // later one overrides the earlier before it covers anything.
+          Prev.Stop = std::max(Prev.Stop, Stop);
+          Prev.Value = I.value();
+          ++NumMergedIntervals;
+          continue;
+        }
+        Prev.Stop = std::min(Prev.Stop, Start);
----------------
petar-jovanovic wrote:

> Is there an example of where this can happen, i.e. a newly canonicalised range ends up in front of an earlier one? I would have thought that, before this function runs, we're guaranteed that start/stop pairs increase monotonically, and that canonicalisation can't make slotindexes go "back" past the first available canonical index. Thus guaranteeing the ranges remain monotonic (but perhaps zero lengthed).

Yes, the test here debug-spilled-snippet.ll hits it. Baseline has [208B;216r): 0 [216r;224r): 1; the instruction at 216 was erased with nothing surviving between it and the block start, so it resolves back to 208B, the same as the previous start, and the two merge into [208B;224r): 1.

https://github.com/llvm/llvm-project/pull/224467


More information about the llvm-commits mailing list