-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Add assembly test for -Zreg-struct-return
option
#145382
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
winstonallo
wants to merge
7
commits into
rust-lang:master
Choose a base branch
from
winstonallo:reg-struct-return-asm-test
base: master
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.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d06c2d4
Add test file for reg-struct-return
winstonallo 24a767d
Add assembly tests for `-Zreg-struct-return`
winstonallo fc67ace
Improve test description in comment
winstonallo 427a183
Add better test description
winstonallo f4a9317
Add tests for < 8 and > 8 bytes structs
winstonallo 3518ac1
Simplify WITHOUT test in pivot_call
winstonallo fcf55d5
Generalize target registers in tests
winstonallo 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
//! Tests that -Zreg-struct-return changes ABI for small struct returns | ||
//! from hidden-pointer convention to register-return on x86_32. | ||
//! This test covers: | ||
//! * Direct struct construction, verifying register return versus hidden pointer | ||
//! * External function calls returning structs, verifying ABI mismatch handling | ||
//@ add-core-stubs | ||
//@ assembly-output: emit-asm | ||
//@ compile-flags: -O --target=i686-unknown-linux-gnu -Crelocation-model=static | ||
//@ revisions: WITH WITHOUT | ||
//@[WITH] compile-flags: -Zreg-struct-return | ||
//@ needs-llvm-components: x86 | ||
|
||
#![feature(no_core)] | ||
#![no_std] | ||
#![no_core] | ||
#![crate_type = "lib"] | ||
|
||
extern crate minicore; | ||
use minicore::*; | ||
|
||
// Verifies ABI changes for small struct, where both fields fit into one register. | ||
// WITH is expected to use register return, WITHOUT should use hidden pointer. | ||
mod small { | ||
struct small_t { | ||
a: i8, | ||
b: i8, | ||
} | ||
|
||
unsafe extern "C" { | ||
fn small() -> small_t; | ||
} | ||
|
||
#[unsafe(no_mangle)] | ||
pub unsafe extern "C" fn small_direct_construction() -> small_t { | ||
// (42 << 8) | 42 = 10794 | ||
|
||
// WITH-LABEL: small_direct_construction | ||
// WITH: movw $10794, %ax | ||
// WITH: retl | ||
|
||
// WITHOUT-LABEL: small_direct_construction | ||
// WITHOUT: movl 4(%esp), %e{{.*}} | ||
// WITHOUT: movw $10794, (%e{{.*}}) | ||
// WITHOUT: retl $4 | ||
small_t { a: 42, b: 42 } | ||
} | ||
|
||
#[unsafe(no_mangle)] | ||
pub unsafe extern "C" fn small_call() -> small_t { | ||
// WITH-LABEL: small_call | ||
// WITH: jmp small | ||
|
||
// WITHOUT-LABEL: small_call | ||
// WITHOUT: calll small | ||
// WITHOUT: retl $4 | ||
small() | ||
} | ||
} | ||
|
||
// Verifies ABI changes for a struct of size 8, which is the maximum size | ||
// for reg-struct-return. | ||
// WITH is expected to still use register return, WITHOUT should use hidden | ||
// pointer. | ||
mod pivot { | ||
struct pivot_t { | ||
a: i32, | ||
b: i32, | ||
} | ||
|
||
unsafe extern "C" { | ||
fn pivot() -> pivot_t; | ||
} | ||
|
||
#[unsafe(no_mangle)] | ||
pub unsafe extern "C" fn pivot_direct_construction() -> pivot_t { | ||
// WITH-LABEL: pivot_direct_construction | ||
// WITH: movl $42, %e{{.*}} | ||
// WITH: movl $42, %e{{.*}} | ||
// WITH: retl | ||
|
||
// WITHOUT-LABEL: pivot_direct_construction | ||
// WITHOUT: movl 4(%esp), %e{{.*}} | ||
// WITHOUT: movl $42, (%e{{.*}}) | ||
// WITHOUT: movl $42, 4(%e{{.*}}) | ||
// WITHOUT: retl $4 | ||
pivot_t { a: 42, b: 42 } | ||
} | ||
|
||
#[unsafe(no_mangle)] | ||
pub unsafe extern "C" fn pivot_call() -> pivot_t { | ||
// WITH-LABEL: pivot_call | ||
// WITH: jmp pivot | ||
|
||
// WITHOUT-LABEL: pivot_call | ||
// WITHOUT: calll pivot | ||
// WITHOUT: retl $4 | ||
pivot() | ||
} | ||
} | ||
|
||
// Verifies ABI changes for a struct of size 12, which is larger than the | ||
// maximum size for reg-struct-return (8 bytes). | ||
// Here, both WITH and WITHOUT should use the hidden pointer convention. | ||
mod large { | ||
struct large_t { | ||
a: i32, | ||
b: i32, | ||
c: i32, | ||
} | ||
|
||
unsafe extern "C" { | ||
fn large() -> large_t; | ||
} | ||
|
||
#[unsafe(no_mangle)] | ||
pub unsafe extern "C" fn large_direct_construction() -> large_t { | ||
// WITH-LABEL: large_direct_construction | ||
// WITH: movl 4(%esp), %e{{.*}} | ||
// WITH: movl $42, (%e{{.*}}) | ||
// WITH: movl $42, 4(%e{{.*}}) | ||
// WITH: movl $42, 8(%e{{.*}}) | ||
// WITH: retl $4 | ||
|
||
// WITHOUT-LABEL: large_direct_construction | ||
// WITHOUT: movl 4(%esp), %e{{.*}} | ||
// WITHOUT: movl $42, (%e{{.*}}) | ||
// WITHOUT: movl $42, 4(%e{{.*}}) | ||
// WITHOUT: movl $42, 8(%e{{.*}}) | ||
// WITHOUT: retl $4 | ||
large_t { a: 42, b: 42, c: 42 } | ||
} | ||
|
||
#[unsafe(no_mangle)] | ||
pub unsafe extern "C" fn large_call() -> large_t { | ||
// WITH-LABEL: large_call | ||
// WITH: calll large | ||
// WITH: retl $4 | ||
|
||
// WITHOUT-LABEL: large_call | ||
// WITHOUT: calll large | ||
// WITHOUT: retl $4 | ||
large() | ||
} | ||
} |
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.
If the assembly here is exact then that would indicate that LLVM expects the caller and callee to have the same ABI, which is good. But FileCheck only checks that lines exist in that order, not that there isn't anything between them, so that would need some
WITH-NOT
/WITHOUT-NOT
(or-NEXT)
to assert it isn'tmov
ing more things around.However, for asm tests it's good to try to force the data to be moved in some way so you can observe it. If the function were changed to:
Then you can assert that it does moves from specific registers to memory.