This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Set how many bytes load from or store to stack slot
ClosedPublic

Authored by Jim on Mar 6 2023, 10:10 PM.

Details

Summary

Refer from: https://reviews.llvm.org/D44782

After https://reviews.llvm.org/D130302, LW+SEXT.B can be folded into LB
as partially reload stack slot. This gains incorrect optimization result
from StackSlotColoring without given the number of bytes exactly load
from stack. LB+SW are mis-interpreted as fully reload/restore from stack
slot without the sign-extension. SW would be considered as a redundant store.

The testcase is copied from llvm/test/CodeGen/X86/pr30821.mir.

Diff Detail

Event Timeline

Jim created this revision.Mar 6 2023, 10:10 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 6 2023, 10:10 PM
Jim requested review of this revision.Mar 6 2023, 10:10 PM
This revision is now accepted and ready to land.Mar 7 2023, 8:38 AM
jrtc27 added inline comments.Mar 7 2023, 11:27 AM
llvm/test/CodeGen/RISCV/stack-slot-coloring.mir
1

Can we update_mir_test_checks.py this, precommit and see the diff in this patch?

kito-cheng added inline comments.Mar 7 2023, 6:43 PM
llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
82

We might need to init MemByte to 0?

/// Optional extension of isLoadFromStackSlot that returns the number of
/// bytes loaded from the stack. This must be implemented if a backend
/// supports partial stack slot spills/loads to further disambiguate
/// what the load does.
virtual unsigned isLoadFromStackSlot(const MachineInstr &MI, 
                                     int &FrameIndex,
                                     unsigned &MemBytes) const {
  MemBytes = 0; 
  return isLoadFromStackSlot(MI, FrameIndex);
}
craig.topper added inline comments.Mar 7 2023, 6:59 PM
llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
82

Do the callers care in the cases this function returns 0? Looks like MemBytes will be explicitly set on any path that returns a non-zero value.

Jim updated this revision to Diff 503239.Mar 7 2023, 10:59 PM

Add pre-commit test case

Jim marked 3 inline comments as done.Mar 7 2023, 11:02 PM
Jim added inline comments.
llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
82

It seems caller doesn't care MemBytes if the return value is zero (means it's not a load from or store to stack slot instruction).

llvm/test/CodeGen/RISCV/stack-slot-coloring.mir
1

Add pre-commit test case

kito-cheng added inline comments.Mar 7 2023, 11:13 PM
llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
82

I am OK without init that to 0

This revision was landed with ongoing or failed builds.Mar 9 2023, 6:25 PM
This revision was automatically updated to reflect the committed changes.
Jim marked an inline comment as done.

This patch caused a code size regression in our ci tests https://github.com/dtcxzyw/llvm-ci/issues/8. And part of the extra codes it generates is as follows:

I think the root cause is this patch makes the original function

unsigned isLoadFromStackSlot(const MachineInstr &MI,
                               int &FrameIndex) const override;

no longer be overridden.

This patch caused a code size regression in our ci tests https://github.com/dtcxzyw/llvm-ci/issues/8. And part of the extra codes it generates is as follows:

I think the root cause is this patch makes the original function

unsigned isLoadFromStackSlot(const MachineInstr &MI,
                               int &FrameIndex) const override;

no longer be overridden.

I have restored the original signatures and mapped them to the new signatures the same way x86 does.