[llvm] [AMDGPU] Allow SCC-only implicit defs and mark SOP2 SALU as rematerializable (PR #187084)

Romanov Vlad via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 24 02:42:06 PDT 2026


================
@@ -197,6 +198,14 @@ class LLVM_ABI TargetInstrInfo : public MCInstrInfo {
     return false;
   }
 
+  /// Returns true if this instruction can be safely rematerialized at UseIdx
+  /// even if it has implicit physical register defs that are live.
+  virtual bool isImplicitDefRematerializableAt(const MachineInstr &MI,
----------------
romanovvlad wrote:

Sorry for the long reply:

In canRematerializeAt (https://github.com/llvm/llvm-project/blob/main/llvm/lib/CodeGen/LiveRangeEdit.cpp#L71), we have the following checks to determine whether an instruction can be rematerialized:

  1. TII.isReMaterializable checks whether the instruction is marked as IsReMaterializable in TableGen (.td) files and calls SIInstrInfo::isReMaterializableImpl, which bails out if an instruction has implicit defs.
  2. VirtRegAuxInfo::allUsesAvailableAt (https://github.com/llvm/llvm-project/blob/main/llvm/lib/CodeGen/CalcSpillWeights.cpp#L149) checks that all uses are available at the remat point. It ignores the SCC def because it is a definition, not a use.

  TII.isReMaterializable and SIInstrInfo::isReMaterializableImpl do not have access to information about the remat point, so they cannot check whether SCC is live there. This makes them unsuitable for this check.

I tried modifying VirtRegAuxInfo::allUsesAvailableAt to also check implicit defs. However, this caused a regression in the x86 backend, which allows certain implicit defs in the x86-specific version of TII.isReMaterializable. These are then handled in X86InstrInfo::reMaterialize (https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/X86/X86InstrInfo.cpp#L967). The problem is that in the x86 backend, it is possible to use an alternative version of the instruction to avoid the implicit def if it is live at the remat point. Therefore, adding these checks to VirtRegAuxInfo::allUsesAvailableAt breaks this x86 rematerialization case.
                                                                                                                                                                                                                                                  In the AMDGPU backend, there are no alternative instructions that avoid defining SCC. Additionally, we cannot abort rematerialization in AMDGPU::reMaterialize, it's too late.                                                Therefore, I decided to introduce a new hook to handle implicit defs specifically. This approach leaves the x86 backend unaffected, and x86 can later migrate its implicit def checks to this hook if needed.

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


More information about the llvm-commits mailing list