Skip to content

Always reserve space for outgoing args#8319

Merged
elliottt merged 4 commits intobytecodealliance:mainfrom
elliottt:trevor/function-call-arg-space
Apr 11, 2024
Merged

Always reserve space for outgoing args#8319
elliottt merged 4 commits intobytecodealliance:mainfrom
elliottt:trevor/function-call-arg-space

Conversation

@elliottt
Copy link
Member

@elliottt elliottt commented Apr 9, 2024

Instead of reserving stack space for stack arguments and return values at the call site that requires them, eagerly reserve the maximum amount of space needed in the function prologue. This change minimizes manipulation of the stack pointer, and in the case of tail calls only modifies the stack pointer to recover the outgoing argument space that is released by the tail callee. Additionally, eagerly reserving space for stack arguments in the non-s390x backends moves us closer to agreement on how to handle calls across all backends, as it already follows this frame layout.

This change sets us up to be able to remove the notion of a nominal SP from all backends, as the stack pointer will now be a stable point to address everything within the frame with a positive offset.

Co-authored-by: Jamey Sharp jsharp@fastly.com

@github-actions github-actions bot added cranelift Issues related to the Cranelift code generator cranelift:area:machinst Issues related to instruction selection and the new MachInst backend. cranelift:area:x64 Issues related to x64 codegen labels Apr 9, 2024
@elliottt elliottt force-pushed the trevor/function-call-arg-space branch 4 times, most recently from 1691790 to fdaaf62 Compare April 10, 2024 15:49
@github-actions github-actions bot added the cranelift:area:aarch64 Issues related to AArch64 backend. label Apr 10, 2024
@elliottt elliottt force-pushed the trevor/function-call-arg-space branch from fdaaf62 to 3f8cea2 Compare April 10, 2024 18:56
@elliottt elliottt force-pushed the trevor/function-call-arg-space branch from 3f8cea2 to fb5f583 Compare April 10, 2024 20:47
@elliottt elliottt marked this pull request as ready for review April 10, 2024 21:03
@elliottt elliottt requested review from a team as code owners April 10, 2024 21:03
@elliottt elliottt requested review from a team, abrown and fitzgen and removed request for a team April 10, 2024 21:03
@abrown
Copy link
Member

abrown commented Apr 11, 2024

I'm offline until next week if someone else can review this.

@cfallin
Copy link
Member

cfallin commented Apr 11, 2024

I can take a look tomorrow when back in office!

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with some minor nitpicks below

Comment on lines -178 to -179
abi.emit_stack_pre_adjust(ctx);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woo!

for inst in retval_insts {
ctx.emit(inst);
}
abi.emit_stack_post_adjust(ctx);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\o/

Comment on lines +478 to +483
/// When setting up for a call, ensure that `space` bytes are available in the outgoing
/// argument area on the stack for arguments to that function, and any values returned through
/// the stack.
fn gen_reserve_argument_area(_space: u32) -> SmallInstVec<Self::I> {
smallvec![]
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this ever get overwritten? At a glance, it seems like a pretty sus default to just ignore the requested space! Especially since the dual restore method is not a no-op! I feel like a comment explaining that the space is pre-reserved when the stack frame is pushed, and that restoring the argument area is only used for tail calls in practice since they are otherwise preserved, would be helpful somewhere in this viscinity.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default implementations are not overridden in this PR, but we want to allow individual backends to implement an optimization where they minimize the amount of outgoing argument area they use across a function call. To do that, they need to know how much stack is actually needed for the current call. So we've ensured that at least this much space is available during the prologue, which is why a no-op is okay here, and overriding can arrange that there is less space available if desired. #8327 illustrates roughly how that would work, except that's for return-calls, where this optimization is actually required.

Yeah, we should write better comments here.

elliottt and others added 2 commits April 11, 2024 11:12
Co-authored-by: Jamey Sharp <jamey@minilop.net>
@elliottt elliottt added this pull request to the merge queue Apr 11, 2024
Merged via the queue into bytecodealliance:main with commit aebcbb4 Apr 11, 2024
@elliottt elliottt deleted the trevor/function-call-arg-space branch April 11, 2024 19:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift:area:aarch64 Issues related to AArch64 backend. cranelift:area:machinst Issues related to instruction selection and the new MachInst backend. cranelift:area:x64 Issues related to x64 codegen cranelift Issues related to the Cranelift code generator

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants