[llvm] [AMDGPU][Scheduler] Fix non-monotonic SlotIndex after schedule revert (PR #192039)

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 14 04:49:51 PDT 2026


michaelselehov wrote:

`modifyRegionSchedule` restores the original instruction order by splicing MIs one by one before `RegionEnd`. When an MI is already at the expected position (`MII == RegionEnd`), the code advances `RegionEnd` past it without updating the MI's `SlotIndex`. However, earlier splices in the same loop may have shifted neighboring slot indices, leaving a stale, lower-numbered slot on the non-moved MI. This breaks `SlotIndex` monotonicity with respect to physical MBB order.

The broken monotonicity corrupts `LiveIntervals`: a use can end up with a slot numerically before its def, so interval queries return wrong results. In practice this manifests as a `register isn't live` assertion in `GCNDownwardRPTracker::advanceBeforeNext`, triggered when `PreRARematStage::finalizeGCNSchedStage` globally reverts regions that were already locally reverted by `checkScheduling`.

Concrete example from the reproducer:

```
V_PERM_B32 (def %209.sub0):  spliced → new slot 4644
V_AND_B32  (def %209.sub1):  spliced → new slot 4648
[non-moved MI]:               ++RegionEnd → keeps old slot 4420  ← stale
DS_WRITE_B64 (use %209):     spliced after non-moved MI → gets slot 4424
```

Result: use at 4424, defs at 4644/4648 — non-monotonic. `LiveIntervals` computes `%209` live in `[4744r, 4752r)`, the use at 4432 falls outside it, and the RP tracker asserts.

The fix has two parts:

1. In the `else` branch of `modifyRegionSchedule` (MI already at expected position), check whether the MI's `SlotIndex` has become non-monotonic (`PrevIdx >= MI_Idx`). If so, call `LIS->handleMove` to re-seat it. The monotonicity guard is important — calling `handleMove` unconditionally would perturb `LiveIntervals` even when slots are fine, changing scheduling decisions and causing register pressure regressions.

2. Track whether `checkScheduling` already reverted a region (`ScheduleReverted` flag propagated via `AlreadyReverted` in `RegionSchedRevert`). Skip the redundant global revert in `finalizeGCNSchedStage` for such regions, avoiding unnecessary `handleMove` calls and `LiveInterval` perturbations from double reverts.

A MIR regression test is included that triggers the non-monotonic slot condition and crashes without the fix.


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


More information about the llvm-commits mailing list