[PATCH] D137222: [MachineCSE] Allow CSE for instructions with ignorable operands

Guozhi Wei via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 1 19:00:44 PDT 2022


Carrot created this revision.
Carrot added reviewers: foad, arsenm.
Herald added subscribers: kosarev, kerbowa, hiraditya, tpr, jvesely.
Herald added a project: All.
Carrot requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.

Ignorable operands don't impact instruction's behavior, we can safely do CSE on the instruction.

It is split from D130919 <https://reviews.llvm.org/D130919>. It has big impact to some AMDGPU test cases.
For example in atomic_optimizations_raw_buffer.ll, when trying to check if the following instruction can be CSEed

  %37:vgpr_32 = V_MOV_B32_e32 0, implicit $exec

Function isCallerPreservedOrConstPhysReg is called on operand "implicit $exec", this function is implemented as

  -  return TRI.isCallerPreservedPhysReg(Reg, MF) ||
  +  return TRI.isCallerPreservedPhysReg(Reg, MF) || TII.isIgnorableUse(MO) ||
            (MRI.reservedRegsFrozen() && MRI.isConstantPhysReg(Reg));

Both TRI.isCallerPreservedPhysReg and MRI.isConstantPhysReg return false on this operand, so isCallerPreservedOrConstPhysReg is also false, it causes LLVM failed to CSE this instruction.

With this patch TII.isIgnorableUse returns true for the operand $exec, so isCallerPreservedOrConstPhysReg also returns true, it causes this instruction to be CSEed with previous instruction

  %14:vgpr_32 = V_MOV_B32_e32 0, implicit $exec

So I got different result from here. AMDGPU's implementation of isIgnorableUse is

  bool SIInstrInfo::isIgnorableUse(const MachineOperand &MO) const {
    // Any implicit use of exec by VALU is not a real register read.
    return MO.getReg() == AMDGPU::EXEC && MO.isImplicit() &&
           isVALU(*MO.getParent()) && !resultDependsOnExec(*MO.getParent());
  }

Since the operand $exec is not a real register read, my understanding is it's reasonable to do CSE on such instructions.

Because more instructions are CSEed, so I get less instructions generated for these tests.

  $ git diff | grep "+;" | wc
      920    5616   48113
  $ git diff | grep "\-;" | wc
      980    5979   51462


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D137222

Files:
  llvm/lib/CodeGen/MachineCSE.cpp
  llvm/test/CodeGen/AMDGPU/atomic_optimizations_buffer.ll
  llvm/test/CodeGen/AMDGPU/atomic_optimizations_local_pointer.ll
  llvm/test/CodeGen/AMDGPU/atomic_optimizations_raw_buffer.ll
  llvm/test/CodeGen/AMDGPU/atomic_optimizations_struct_buffer.ll
  llvm/test/CodeGen/AMDGPU/tuple-allocation-failure.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D137222.472471.patch
Type: text/x-patch
Size: 190884 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221102/885c812b/attachment-0001.bin>


More information about the llvm-commits mailing list