Skip to content

Conversation

winstonallo
Copy link
Contributor

@winstonallo winstonallo commented Aug 14, 2025

r? @tgross35

As discussed in #145309 with @tgross35 and @ojeda, I added assembly tests for the -Zreg-struct-return option verifying that it changes the ABI from hidden pointer to register-return on x86_32.

The test covers:

  • Direct struct construction, showing register return vs hidden pointer
  • External function calls returning structs, showing ABI mismatch handling

Different memory layouts affect ABI mismatch handling, but register returns use the same register allocation regardless of struct field layout (apart from the fact that they use smaller registers for smaller structs, of course).

Here is a compiler explorer with 2 examples. Let me know if there is anything more I could add. Since register returns only happen for structs up to the size of 2 registers, I figured testing the pivot value (8 bytes) would be most critical.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 14, 2025
@ojeda
Copy link
Contributor

ojeda commented Aug 14, 2025

Thanks for this!

  • Direct struct construction, showing register return vs hidden pointer

  • External function calls returning structs, showing ABI mismatch handling

I would suggest preserving these two lines in the tests themselves.

Since register returns only happen for structs up to the size of 2 registers, I figured testing the pivot value (8 bytes) would be most critical.

Yeah. It would also be a good idea to test the other side of that threshold, i.e. testing a size where it does not happen even with the flag enabled.

(Also, a smaller size that puts both fields in the same register like in your CE example wouldn't hurt either, but I am not sure what the threshold for "too many tests" is in Rust)

@winstonallo
Copy link
Contributor Author

Yeah. It would also be a good idea to test the other side of that threshold, i.e. testing a size where it does not happen even with the flag enabled.

(Also, a smaller size that puts both fields in the same register like in your CE example wouldn't hurt either, but I am not sure what the threshold for "too many tests" is in Rust)

Added tests for both of those cases, we can still decide to remove them if they turn out to be too much
Add tests for < 8 and > 8 bytes structs

@ojeda
Copy link
Contributor

ojeda commented Aug 14, 2025

The tests look great now with the comments and the extra cases.

@winstonallo
Copy link
Contributor Author

I now also adapted the 8 bytes tests to be less rigid on the instruction stream, thank you for all the feedback!

@winstonallo
Copy link
Contributor Author

Hey @tgross35, thank you for the feedback. I separated the tests into the caller/callee sides, and I added the CHECK-DAG directives for instruction streams where ordering may vary in the future.

Copy link
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

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

Sorry for the delay here, looks pretty great but I noticed a few more things. I think things should be fine assuming my suggestions work, could you please squash?

View changes since this review

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 27, 2025
Copy link
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

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

Thanks for the updates, looks great! Could you please squash?

View changes since this review

@winstonallo winstonallo force-pushed the reg-struct-return-asm-test branch from b7c5825 to 0860ac5 Compare August 27, 2025 18:09
@winstonallo
Copy link
Contributor Author

Thanks for the updates, looks great! Could you please squash?

View changes since this review

Done :)

…n` for structs of different sizes.

This test covers:
* The callee side, making sure that the structs are correctly loaded into registers when `-Zreg-struct-return` is enabled
* The caller side, making sure that callers do receive returned structs in registers when `-Zreg-struct-return` is enabled

Structs of the size of up to 2 registers (8 bytes) can be returned in registers in x86_32.
Therefore, the tests are done with 3 different struct sizes:
* 2 bytes (register returns should happen)
* 8 bytes (last value where register returns should happen)
* 12 bytes (register returns should not happen even when `-Zreg-struct-return` is enabled)
@winstonallo winstonallo force-pushed the reg-struct-return-asm-test branch from 0860ac5 to 5f39612 Compare August 27, 2025 18:17
@tgross35
Copy link
Contributor

Thank you!

@bors r+

@bors
Copy link
Collaborator

bors commented Aug 27, 2025

📌 Commit 5f39612 has been approved by tgross35

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 27, 2025
bors added a commit that referenced this pull request Aug 28, 2025
Rollup of 5 pull requests

Successful merges:

 - #145382 (Add assembly test for `-Zreg-struct-return` option)
 - #145746 (Fix STD build failing for target_os = "espidf")
 - #145826 (Use AcceptContext in AttribueParser::check_target)
 - #145894 (Ensure the coordinator thread terminates before its channels drop)
 - #145946 (Remove unnecessary `[dependencies.unicode-properties]` entries.)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit a1c543e into rust-lang:master Aug 28, 2025
10 checks passed
@rustbot rustbot added this to the 1.91.0 milestone Aug 28, 2025
rust-timer added a commit that referenced this pull request Aug 28, 2025
Rollup merge of #145382 - winstonallo:reg-struct-return-asm-test, r=tgross35

Add assembly test for `-Zreg-struct-return` option

r? `@tgross35`

As discussed in #145309 with `@tgross35` and `@ojeda,` I added assembly tests for the `-Zreg-struct-return` option verifying that it changes the ABI from hidden pointer to register-return on x86_32.

The test covers:
- Direct struct construction, showing register return vs hidden pointer
- External function calls returning structs, showing ABI mismatch handling

Different memory layouts affect ABI mismatch handling, but register returns use the same register allocation regardless of struct field layout (apart from the fact that they use smaller registers for smaller structs, of course).

[Here](https://godbolt.org/z/dcW6rnMG3) is a compiler explorer with 2 examples. Let me know if there is anything more I could add. Since register returns only happen for structs up to the size of 2 registers, I figured testing the pivot value (8 bytes) would be most critical.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants