-
-
Notifications
You must be signed in to change notification settings - Fork 14.6k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| //@ add-minicore | ||
| //@ min-llvm-version: 22 | ||
| //@ 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); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| //@ add-minicore | ||
| //@ revisions: win linux | ||
| //@ min-llvm-version: 22 | ||
| // | ||
| //@ 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] | ||
|
|
||
| // Test passing of i128/u128, which is passed directly on x86, but indirectly on win64. | ||
|
|
||
| extern crate minicore; | ||
| use minicore::*; | ||
|
Comment on lines
11
to
19
Member
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. why is
Member
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. Looks like to test both the Linux and Windows call conv without having to pass said target to
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. well yes it's so that |
||
|
|
||
| // 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
+31
to
+34
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.
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.