-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Reference to ZST occupies slot in function calling convention #141049
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
Comments
Making reference-to-ZST zero-sized instead of a pointer has been discussed and rejected in the past because it’s a breaking change in several ways. See rust-lang/rfcs#2040 (comment) for example. |
Thanks for the context and previous discussion. If we limited this optimization to Rust functions, leaving extern "C" and struct representations alone, I wonder whether the optimization is feasible. |
The optimization is only feasible under Stacked Borrows, and it is illegal under Tree Borrows, so I think it is unlikely that this optimization will exist. The use pattern is casting that &ZST to a raw pointer to a non-ZST, then using that to do reads. |
See also rust-lang/unsafe-code-guidelines#256 for more discussions on this usage pattern. |
Happy to close as duplicate. Alas. |
Note that LLVM will optimize out such arguments, but only for non-exported functions. For example: https://godbolt.org/z/3Mo8xbYz6. (Depending on complexity, you may need LTO to ensure this happens.) The functions in the original Godbolt example are exported (due to So depending on your use-case (do you need these arguments in the toplevel of interrupt handlers?) it may not be an issue in practice. |
Please forgive if redundant - I did a search but could not find relevant discussion.
I work on embedded Rust and we use ZSTs and traits for hardware abstraction layers (HAL).
If we change call sites to accept HAL objects by reference (to support unit tests with stateful HALs), I noticed Rust will occupy an argument passing slot even though
&ZST
could also be zero-sized.Notice the differences in https://gcc.godbolt.org/z/s6ocP1bds
Reproducing example here:
Is there a fundamental reason or is this simply a missed optimization opportunity?
The text was updated successfully, but these errors were encountered: