This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Fix a bug in RISCVFrameLowering.
ClosedPublic

Authored by HsiangKai on Nov 19 2021, 7:17 AM.

Details

Summary

When we have out-going arguments passing through stack and we do not
reserve the stack space in the prologue. Use BP to access stack objects
after adjusting the stack pointer before function calls.

callseq_start -> sp = sp - reserved_space

Use FP to access fixed stack objects.
Use BP to access non-fixed stack objects.

call @foo
callseq_end -> sp = sp + reserved_space

Diff Detail

Event Timeline

HsiangKai created this revision.Nov 19 2021, 7:17 AM
HsiangKai requested review of this revision.Nov 19 2021, 7:17 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 19 2021, 7:17 AM

Can you describe this issue in more detail, please? I'm not following it at the moment.

We have a dynamic stack change with the sub sp, sp, a1. I don't see any more dynamic changes after that. The test case then does a bunch of BP-relative loads and stores but the BP has the same value as the SP so we could just perform SP-relative accesses... no? Was the BP meant to be copied from the SP before the dynamic SP change?

craig.topper added inline comments.Nov 24 2021, 2:48 PM
llvm/test/CodeGen/RISCV/rvv/no-reserved-frame.ll
34–36

I believe this is the incorrect calculation. It's after the addi sp, sp, -32. The 40 doesn't consider the -32 adjustment that was made.

If I understand correctly, D103622 is related as it disabled the reservedCallFrame if there is a frame pointer and vector objects.

HsiangKai edited the summary of this revision. (Show Details)Nov 27 2021, 5:58 AM

Can you describe this issue in more detail, please? I'm not following it at the moment.

We have a dynamic stack change with the sub sp, sp, a1. I don't see any more dynamic changes after that. The test case then does a bunch of BP-relative loads and stores but the BP has the same value as the SP so we could just perform SP-relative accesses... no? Was the BP meant to be copied from the SP before the dynamic SP change?

As Craig depicted, there is an additional stack change before the function call, gfunc, to reserve a stack space for out-going arguments for the function call. After the stack changes, we should not use sp to access the stack objects. In this patch, it uses bp to access stack objects if the prologue does not preserve the space for out-going arguments and there are any out-going arguments to pass to callee.

I have a patch to add a test case to show the bug in D114245.

frasercrmck accepted this revision.Nov 29 2021, 2:27 AM

LGTM other than nits. Thanks for the fix!

llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
240–246

nit: outgoing

242

nit: can not use
nit: our other comments in this file don't use dollars when referencing SP/BP.

245

You don't need parens around MFI.getMaxCallFrameSize() != 0

This revision is now accepted and ready to land.Nov 29 2021, 2:29 AM
HsiangKai updated this revision to Diff 390529.Nov 29 2021, 5:21 PM

Update comments.

This revision was landed with ongoing or failed builds.Nov 29 2021, 6:40 PM
This revision was automatically updated to reflect the committed changes.