Skip to content

Avoid copying the frame for tail calls on riscv64#8383

Merged
elliottt merged 4 commits intobytecodealliance:mainfrom
elliottt:trevor/tail-calls-riscv64
Apr 18, 2024
Merged

Avoid copying the frame for tail calls on riscv64#8383
elliottt merged 4 commits intobytecodealliance:mainfrom
elliottt:trevor/tail-calls-riscv64

Conversation

@elliottt
Copy link
Member

@elliottt elliottt commented Apr 16, 2024

To mirror the implementation in the x64 backend, switch to eagerly reserving enough space in the incoming argument area for any tail call present in the function being compiled.

The diff here is large due to the change of representation for incoming arguments in the riscv64 specific AMode type. Now that we reference incoming arguments with a negative offset from the beginning of the incoming argument area, all references to incoming arguments changed in the riscv64 filetests. These would have ended up being no-op changes in the disassembly, though I took this opportunity to make them SP relative now, to move closer to frame pointers not being strictly required for tail calls.

As this is the last backend that was waiting to be migrated to the approach of resizing the incoming argument area in the function prologue, I've also deleted the shared code in machinst/abi.rs that's no longer needed.

NOTE: this doesn't make the change to enable callee-save registers with the tail calling convention on aarch64, but that will be a relatively small change following this PR.

@github-actions github-actions bot added cranelift Issues related to the Cranelift code generator isle Related to the ISLE domain-specific language labels Apr 16, 2024
@github-actions
Copy link

Subscribe to Label Action

cc @cfallin, @fitzgen

Details This issue or pull request has been labeled: "cranelift", "isle"

Thus the following users have been cc'd because of the following labels:

  • cfallin: isle
  • fitzgen: isle

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

@elliottt elliottt force-pushed the trevor/tail-calls-riscv64 branch from 8bb7ee8 to 6a72c2b Compare April 16, 2024 20:43
@github-actions github-actions bot added the cranelift:area:machinst Issues related to instruction selection and the new MachInst backend. label Apr 16, 2024
@elliottt elliottt force-pushed the trevor/tail-calls-riscv64 branch from 6a72c2b to 3350226 Compare April 16, 2024 21:04
@elliottt elliottt marked this pull request as ready for review April 16, 2024 21:09
@elliottt elliottt requested a review from a team as a code owner April 16, 2024 21:09
@elliottt elliottt requested review from afonso360, fitzgen and jameysharp and removed request for a team April 16, 2024 21:09
Copy link
Contributor

@afonso360 afonso360 left a comment

Choose a reason for hiding this comment

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

👋 Hey,

I don't think I can do a very good review here, I don't remember a lot about how our frames are set up and haven't been following the rest of the tail calls work very closely. So I'd prefer if someone else could review this.

let new_stack_words = new_stack_arg_size / 8;
let insts = 4 + 2 + 2 * new_stack_words;
let space_needed = insts * u32::try_from(Inst::UNCOMPRESSED_INSTRUCTION_SIZE).unwrap();
if sink.island_needed(space_needed) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this island check now done in some common code, or do we no longer need it? I remember having some issues with missing islands when we enabled compressed jumps since with the compressed extensions we have a very short jump range.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for catching this, that check isn't being done in any generic code. I'll add this back in!

Copy link
Contributor

Choose a reason for hiding this comment

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

On further inspection, there is generic code in VCode::emit that emits islands based on the backend's Inst::worst_case_size() estimate. Both gen_epilogue and these ReturnCall/ReturnCallInd pseudo-instructions need to fit within that worst-case size. These pseudo-instructions might be slightly longer than gen_epilogue right now but they're almost the same, so I think they don't need an explicit island check.

Copy link
Member Author

Choose a reason for hiding this comment

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

I did some more refactoring, and the resulting code generated for a return_call or return_call_indirect should now be pretty close to the same size as gen_epilogue. Lots of chatting with @jameysharp has me convinced that we shouldn't need to check for island emission at this point anymore, and I've removed the manual resets at the call-sites for emit_return_call_common_sequence to ensure that we trigger an assert if this isn't true.

@elliottt elliottt force-pushed the trevor/tail-calls-riscv64 branch from e381d21 to 2d9fc23 Compare April 17, 2024 00:27
jameysharp added a commit to jameysharp/wasmtime that referenced this pull request Apr 18, 2024
This reverts the key parts of e3a08d4
(bytecodealliance#8151), because it turns out that we didn't need that abstraction.

Several changes in the last month have enabled this:

- bytecodealliance#8292 and then bytecodealliance#8316 allow us to refer to either incoming or outgoing
  argument areas in a (mostly) consistent way

- bytecodealliance#8327, bytecodealliance#8377, and bytecodealliance#8383 demonstrate that we never need to delay
  writing stack arguments directly to their final location
@elliottt elliottt force-pushed the trevor/tail-calls-riscv64 branch from d993630 to 301524d Compare April 18, 2024 16:40
jameysharp added a commit to jameysharp/wasmtime that referenced this pull request Apr 18, 2024
This reverts the key parts of e3a08d4
(bytecodealliance#8151), because it turns out that we didn't need that abstraction.

Several changes in the last month have enabled this:

- bytecodealliance#8292 and then bytecodealliance#8316 allow us to refer to either incoming or outgoing
  argument areas in a (mostly) consistent way

- bytecodealliance#8327, bytecodealliance#8377, and bytecodealliance#8383 demonstrate that we never need to delay
  writing stack arguments directly to their final location

prtest:full
github-merge-queue bot pushed a commit that referenced this pull request Apr 18, 2024
This reverts the key parts of e3a08d4
(#8151), because it turns out that we didn't need that abstraction.

Several changes in the last month have enabled this:

- #8292 and then #8316 allow us to refer to either incoming or outgoing
  argument areas in a (mostly) consistent way

- #8327, #8377, and #8383 demonstrate that we never need to delay
  writing stack arguments directly to their final location

prtest:full
@elliottt elliottt force-pushed the trevor/tail-calls-riscv64 branch from 301524d to ba40e76 Compare April 18, 2024 21:20
Copy link
Contributor

@jameysharp jameysharp left a comment

Choose a reason for hiding this comment

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

I'm convinced this is all correct. Ship it!

@elliottt elliottt added this pull request to the merge queue Apr 18, 2024
Merged via the queue into bytecodealliance:main with commit f52f8f8 Apr 18, 2024
@elliottt elliottt deleted the trevor/tail-calls-riscv64 branch April 18, 2024 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift:area:machinst Issues related to instruction selection and the new MachInst backend. cranelift Issues related to the Cranelift code generator isle Related to the ISLE domain-specific language

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants