This is an archive of the discontinued LLVM Phabricator instance.

[AArch64][SVE] Restore SP from FP when SVE CSRs and variable sized objects are present
ClosedPublic

Authored by bsmith on Apr 28 2022, 7:14 AM.

Details

Summary

Without SVE, after a dynamic stack allocation has modified the SP, it is
presumed that a frame pointer restoration will revert the SP back to
it's correct value prior to any caller stack being restored. However the
SVE frame is restored using the stack pointer directly, as it is located
after the frame pointer. This means that in the presence of a dynamic
stack allocation, any SVE callee state gets corrupted as SP has the
incorrect value when the SVE state is restored.

To address this issue, when variable sized objects and SVE CSRs are
present, treat the stack as having been realigned, hence restoring the
stack pointer from the frame pointerr prior to restoring the SVE state.

Diff Detail

Event Timeline

bsmith created this revision.Apr 28 2022, 7:14 AM
Herald added a project: Restricted Project. · View Herald Transcript
bsmith requested review of this revision.Apr 28 2022, 7:14 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 28 2022, 7:14 AM
efriedma added inline comments.Apr 28 2022, 12:32 PM
llvm/lib/Target/AArch64/AArch64FrameLowering.cpp
1961

Do we need to use emitFrameOffset() here?

llvm/test/CodeGen/AArch64/framelayout-sve.mir
632

Can we optimize the stack computation in this case? I can see why you need to set the stack pointer in general, but it doesn't seem necessary here. I guess we can leave that for a followup, though.

bsmith added inline comments.Apr 29 2022, 3:47 AM
llvm/lib/Target/AArch64/AArch64FrameLowering.cpp
1961

I'm not sure we should/need to, the allocation of the variable length array that leads to the changing of the stack pointer does not happen during frame lowering, but rather during isel lowering, hence it does not have the usual frame-setup flag etc. As such I'm not sure this, which is just reverting that, should have the frame-destroy flag etc either, however I'm not sure about this.

bsmith updated this revision to Diff 426012.Apr 29 2022, 3:55 AM
  • Only restore from base pointer when SVE CSRs are present
paulwalker-arm added inline comments.Apr 29 2022, 5:10 AM
llvm/lib/Target/AArch64/AArch64FrameLowering.cpp
1961

Should we be doing this when AFI->isStackRealigned() is true? It looks like for that case we recalculate SP from the frame pointer so this'll kick out a redundant instruction. I guess there's no test to show the negative affect of this, it's just something I spotted whilst working on another bug.

bsmith updated this revision to Diff 426057.EditedApr 29 2022, 7:43 AM
bsmith retitled this revision from [AArch64][SVE] Restore SP from base pointer when it and SVE frame are present to [AArch64][SVE] Restore SP from FP when SVE CSRs and variable sized objects are present.
bsmith edited the summary of this revision. (Show Details)
  • Rework patch to use same mechanism as stack realignment, rather than using base pointer.
efriedma accepted this revision.Apr 29 2022, 11:44 AM

LGTM with a minor comment.

llvm/lib/Target/AArch64/AArch64FrameLowering.cpp
1958

This comment is a bit misleading. For some reading this for the first time, there isn't any presumption that this is for realignment specifically.

Maybe just "If we have stack realignment or variable sized objects on the stack, we must restore the stack pointer using the frame pointer". Or something like that.

This revision is now accepted and ready to land.Apr 29 2022, 11:44 AM
paulwalker-arm accepted this revision.May 1 2022, 3:36 AM
Matt added a subscriber: Matt.May 1 2022, 10:36 AM
This revision was landed with ongoing or failed builds.May 4 2022, 5:57 AM
This revision was automatically updated to reflect the committed changes.