Issue 235697
Summary [RISCV] -msave-restore: save/restore libcalls don't declare their t1 clobber, so IPRA keeps values in t1 across calls
Labels new issue
Assignees
Reporter kdupontbe
    With `-msave-restore`, both instructions that call the libcalls write `t1` (x6) without declaring it:

- `PseudoCALLReg` for `call t0, __riscv_save_N` only defines `x5` (plus `x2` and the implicit CSR uses). The libcall itself writes `t1`. In compiler-rt `lib/builtins/riscv/save.S`, the RV64 versions of `__riscv_save_2`…`__riscv_save_12` (and the RV32 versions from `__riscv_save_4` up) compute the stack adjustment in `t1` (`li t1, 64` … `add sp, sp, t1`).
- `PseudoTAIL` for `tail __riscv_restore_N` only defines `x2` and the restored CSRs. `tail` expands to `auipc t1, …` / `jr t1`, which writes `t1`.

With `-enable-ipra`, `RegUsageInfoCollector` builds the callee's clobber mask from the registers its machine code defines. `t1` doesn't appear, so callers treat it as preserved by any function that uses save/restore libcalls and may keep a live value in `t1` across the call.

See also #NNNN (shrink-wrapping can place `__riscv_save_N` in a block where `t1` is live). Both issues have the same root cause, `t1` being used by the libcall, but need separate fixes.

## Reproducer

`llc -mtriple=riscv64 -mattr=+save-restore -enable-ipra`, https://godbolt.org/z/K81fhrae8

```llvm
define internal void @callee() noinline nounwind {
  call void asm sideeffect "# clobber s0-s4", "~{x8},~{x9},~{x18},~{x19},~{x20}"()
  ret void
}

define void @caller() nounwind {
  %x = call i64 asm sideeffect "li $0, 42", "={x6}"()
  call void @callee()
  call void asm sideeffect "# use $0", "{x6}"(i64 %x)
  ret void
}
```

Output from `llc` trunk:

```asm
callee:
        call    t0, __riscv_save_5      # clobbers t1
        # clobber s0-s4
        tail    __riscv_restore_5       # auipc t1 / jr t1: clobbers t1 again
caller:
        call    t0, __riscv_save_0
        li      t1, 42
        call    callee                  # IPRA: callee "preserves" t1
        # use t1                        # reads the clobbered value
        tail    __riscv_restore_0
```

The inline asm only forces the situation so the reproducer stays small. Ordinary register allocation in the caller can also pick `t1` for a value that is live across the call. Without `-enable-ipra` the call clobbers all caller-saved registers, so the bug only shows up with IPRA.

## Expected

The clobber mask of a function that uses save/restore libcalls should include `t1`, and the caller should keep `%x` out of `t1` across `call callee`.

## Possible fix

We carry this downstream: declare the hidden writes as implicit defs on the two instructions in `RISCVFrameLowering::spillCalleeSavedRegisters` and `restoreCalleeSavedRegisters`, next to the implicit operands that are already there:

```diff
     MachineInstrBuilder NewMI =
         BuildMI(MBB, MI, DL, TII.get(RISCV::PseudoCALLReg), RISCV::X5)
             .addExternalSymbol(SpillLibCall, RISCVII::MO_CALL)
             .setMIFlag(MachineInstr::FrameSetup)
             .addUse(RISCV::X2, RegState::Implicit)
-            .addDef(RISCV::X2, RegState::ImplicitDefine);
+            .addDef(RISCV::X2, RegState::ImplicitDefine)
+            // The save libcalls use t1 to adjust sp.
+            .addDef(RISCV::X6, RegState::ImplicitDefine);
```

```diff
     MachineInstrBuilder NewMI =
         BuildMI(MBB, MI, DL, TII.get(RISCV::PseudoTAIL))
             .addExternalSymbol(RestoreLibCall, RISCVII::MO_CALL)
             .setMIFlag(MachineInstr::FrameDestroy)
-            .addDef(RISCV::X2, RegState::ImplicitDefine);
+            .addDef(RISCV::X2, RegState::ImplicitDefine)
+            // `tail` expands to auipc t1 / jr t1.
+            .addDef(RISCV::X6, RegState::ImplicitDefine);
```

Downstream, the restore tail call defines `x5` as well, and uses `x7` (t2) instead of `x6` when Zicfilp is enabled, because our tree still expands `tail` with t2 in that case. On trunk, the restore call always uses `PseudoTAIL`, while the t2 form is now the separate `PseudoTAILX7`. So `x6` should be the right register here unless the restore libcall is switched to `PseudoTAILX7`.

With this change the caller copies `%x` out of `t1` across the call (to a register the callee is known to preserve) and `t1` is no longer used across it.


Assisted by AI in creating the issue text
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to