-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
explicit tail calls: support indirect arguments #151143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
folkertdev
wants to merge
2
commits into
rust-lang:main
Choose a base branch
from
folkertdev:tail-call-indirect
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+305
−70
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| //@ add-minicore | ||
| //@ assembly-output: emit-asm | ||
| //@ needs-llvm-components: x86 | ||
| //@ compile-flags: --target=x86_64-unknown-linux-gnu | ||
| //@ compile-flags: -Copt-level=3 -C llvm-args=-x86-asm-syntax=intel | ||
|
|
||
| #![feature(no_core, explicit_tail_calls)] | ||
| #![expect(incomplete_features)] | ||
| #![no_core] | ||
| #![crate_type = "lib"] | ||
|
|
||
| // Test tail calls with `PassMode::Indirect { on_stack: false, .. }` arguments. | ||
| // | ||
| // Normally an indirect argument with `on_stack: false` would be passed as a pointer to the | ||
| // caller's stack frame. For tail calls, that would be unsound, because the caller's stack | ||
| // frame is overwritten by the callee's stack frame. | ||
| // | ||
| // The solution is to write the argument into the caller's argument place (stored somewhere further | ||
| // up the stack), and forward that place. | ||
|
|
||
| extern crate minicore; | ||
| use minicore::*; | ||
|
|
||
| #[repr(C)] | ||
| struct S { | ||
| x: u64, | ||
| y: u64, | ||
| z: u64, | ||
| } | ||
|
|
||
| unsafe extern "C" { | ||
| safe fn force_usage(_: u64, _: u64, _: u64) -> u64; | ||
| } | ||
|
|
||
| // CHECK-LABEL: callee: | ||
| // CHECK-NEXT: .cfi_startproc | ||
| // | ||
| // CHECK-NEXT: mov rax, qword ptr [rdi] | ||
| // CHECK-NEXT: mov rsi, qword ptr [rdi + 8] | ||
| // CHECK-NEXT: mov rdx, qword ptr [rdi + 16] | ||
| // CHECK-NEXT: mov rdi, rax | ||
| // | ||
| // CHECK-NEXT: jmp qword ptr [rip + force_usage@GOTPCREL] | ||
| #[inline(never)] | ||
| #[unsafe(no_mangle)] | ||
| fn callee(s: S) -> u64 { | ||
| force_usage(s.x, s.y, s.z) | ||
| } | ||
|
|
||
| // CHECK-LABEL: caller1: | ||
| // CHECK-NEXT: .cfi_startproc | ||
| // | ||
| // Just forward the argument: | ||
| // | ||
| // CHECK-NEXT: jmp qword ptr [rip + callee@GOTPCREL] | ||
| #[unsafe(no_mangle)] | ||
| fn caller1(s: S) -> u64 { | ||
| become callee(s); | ||
| } | ||
|
|
||
| // CHECK-LABEL: caller2: | ||
| // CHECK-NEXT: .cfi_startproc | ||
| // | ||
| // Construct the S value directly into the argument slot: | ||
| // | ||
| // CHECK-NEXT: mov qword ptr [rdi], 1 | ||
| // CHECK-NEXT: mov qword ptr [rdi + 8], 2 | ||
| // CHECK-NEXT: mov qword ptr [rdi + 16], 3 | ||
| // | ||
| // CHECK-NEXT: jmp qword ptr [rip + callee@GOTPCREL] | ||
| #[unsafe(no_mangle)] | ||
| fn caller2(_: S) -> u64 { | ||
| let s = S { x: 1, y: 2, z: 3 }; | ||
| become callee(s); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| //@ add-minicore | ||
| //@ revisions: win linux | ||
| // | ||
| //@ compile-flags: -Copt-level=3 | ||
| //@[linux] compile-flags: --target x86_64-unknown-linux-gnu | ||
| //@[linux] needs-llvm-components: x86 | ||
| //@[win] compile-flags: --target x86_64-pc-windows-msvc | ||
| //@[win] needs-llvm-components: x86 | ||
|
|
||
| #![crate_type = "lib"] | ||
| #![feature(no_core, lang_items, explicit_tail_calls)] | ||
| #![allow(incomplete_features)] | ||
| #![no_core] | ||
|
|
||
| extern crate minicore; | ||
| use minicore::*; | ||
|
|
||
| // linux: define noundef i128 @foo(i128 noundef %a, i128 noundef %b) | ||
| // win: define <16 x i8> @foo(ptr {{.*}} %a, ptr {{.*}} %b) | ||
| #[unsafe(no_mangle)] | ||
| #[inline(never)] | ||
| extern "C" fn foo(a: u128, b: u128) -> u128 { | ||
| // linux: start: | ||
| // linux-NEXT: musttail call noundef i128 @bar(i128 noundef %b, i128 noundef %a) | ||
| // | ||
| // | ||
| // win: start: | ||
| // win-NEXT: %0 = load i128, ptr %b | ||
| // win-NEXT: %1 = load i128, ptr %a | ||
| // win-NEXT: store i128 %0, ptr %a | ||
| // win-NEXT: store i128 %1, ptr %b | ||
|
Comment on lines
+28
to
+31
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this swapping is a little unfortunate of course. I suspect this is a very rare pattern in practice though, so we just need a correct implementation, not necessarily the most optimal one from the get-go. |
||
| // win-NEXT: musttail call <16 x i8> @bar(ptr {{.*}} %a, ptr {{.*}} %b) | ||
| become bar(b, a) | ||
| } | ||
|
|
||
| unsafe extern "C" { | ||
| safe fn bar(a: u128, b: u128) -> u128; | ||
| } | ||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've kept the
span_bug!for now.It looks like a fix for x86 might get cherry-picked into LLVM 22. If so, I think that is enough support to allow this variant too. At that point riscv would be the next most commonly used target that would miscompile.