[llvm] [CodeGen] Move rollback capabilities outside of the rematerializer (PR #184341)

Lucas Ramirez via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 2 07:06:56 PDT 2026


================
@@ -520,12 +499,50 @@ class Rematerializer {
   /// Deletes rematerializable register \p RegIdx from the DAG and relevant
   /// internal state.
   void deleteReg(RegisterIdx RegIdx);
+};
 
-  /// If \p MI's first operand defines a register and that register is a
-  /// rematerializable register tracked by the rematerializer, returns its
-  /// index in the \ref Regs vector. Otherwise returns \ref
-  /// Rematerializer::NoReg.
-  RegisterIdx getDefRegIdx(const MachineInstr &MI) const;
+/// Rematerializer listener with the ability to re-create deleted registers and
+/// rollback rematerializations. Starts recording register deletions and
+/// rematerializations as soon as it is attached to the rematerializer.
+class Rollbacker : public Rematerializer::Listener {
+public:
+  Rollbacker() = default;
+
+  /// Re-creates all deleted registers and rolls back all rematerializations
+  /// that were recorded.
+  void rollback(Rematerializer &Remater);
+
+  void rematerializerNoteRegCreated(const Rematerializer &Remater,
+                                    RegisterIdx RegIdx) override;
+
+  void rematerializerNoteRegDeleted(const Rematerializer &Remater,
+                                    RegisterIdx RegIdx) override;
+
+private:
+  struct RollbackInfo {
+    /// Original register.
+    Register DefReg;
+    /// Original defining region.
+    unsigned DefRegion;
+    /// Original dependencies.
+    SmallVector<Rematerializer::Reg::Dependency, 2> Dependencies;
+    /// Position to re-insert the defining MI before in case of rollback.
+    MachineBasicBlock::iterator InsertPos;
----------------
lucas-rami wrote:

It's a bit convoluted to explain but I think the current logic is sound. I'm gonna try to provide a worst-case example to show that this works (and I added an equivalent unit test).

The `InsertPos` maintained by `RollbackInfo` may indeed become invalid for a deleted register if and only if both following conditions are met:
1. At the moment of deletion, the next instruction is rematerializable.
2. After register deletion, the next instruction is rematerialized and becomes dead.

If (1) doesn't hold, then the instruction cannot be deleted by the rematerializer. If (2) doesn't hold, then the instruction still exists in its original position (the rematerializer doesn't move/delete instruction unless they are rematerialized).

Now consider the following instruction order in a region. Let A,B,C be rematerializable; D's rematerializability is irrelevant, it might as well be the upper region boundary.

```
A = ...
B = ...
C = ...
D = ...
```

If we fully rematerialize A, then B, then C, to other regions, we get the following. 

```
D = ...
```

We also get (`RollbackInfo` data):
- `A_InsertPos == B` (invalid), `A_NextRegIdx == B_RegIdx`
- `B_InsertPos == C` (invalid), `B_NextRegIdx == C_RegIdx`
- `C_InsertPos == D` (valid), `C_NextRegIdx == ?`

We will recreate registers in the same order that we deleted them in (`DeadRegs` is a `MapVector` so this is guaranteed, though now I am not sure it is necessary).

1. Recreate A. Initially `InsertPos = B` (invalid).

    1. `A_NextRegIdx != Rematerializer::NoReg && DeadRegs.contains(A_NextRegIdx)` so we recurse on B's rollback info (`InsertPos = C`, still invalid).
    2. `B_NextRegIdx != Rematerializer::NoReg && DeadRegs.contains(B_NextRegIdx)` so we recurse on C's rollback info (`InsertPos = D`, **now valid**).
    3. `C_NextRegIdx ?= Rematerializer::NoReg && !DeadRegs.contains(C_NextRegIdx)` so we either don't meet the loop's condition or break from the inner if with the latest `InsertPos == D`.

```
A = ...
D = ...
```

2. Recreate B. Initially `InsertPos = C` (invalid).
    1. `B_NextRegIdx != Rematerializer::NoReg && DeadRegs.contains(B_NextRegIdx)` so we recurse on C's rollback info (`InsertPos = D`, **now valid**).
    2. `C_NextRegIdx ?= Rematerializer::NoReg && !DeadRegs.contains(C_NextRegIdx)` so we either don't meet the loop's condition or break from the inner if with the latest `InsertPos == D`.

```
A = ...
B = ...
D = ...
```

3. Recreate C. Initially `InsertPos = D` (**already valid**).
    1. `C_NextRegIdx ?= Rematerializer::NoReg && !DeadRegs.contains(C_NextRegIdx)` so we either don't meet the loop's condition or break from the inner if with the latest `InsertPos == D`.

```
A = ...
B = ...
C = ...
D = ...
```

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


More information about the llvm-commits mailing list