-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
fix handling of ZST in win64 ABI on windows-msvc targets #135204
Conversation
r? @SparrowLii rustbot has assigned @SparrowLii. Use |
cc @mati865 as this makes a change to the windows-gnu target. Does it sound right to you? |
I don't remember the details about win64 ABI but following what GCC does is the right approach. |
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.
This fix makes sense!
These commits modify compiler targets. |
tests/codegen/abi-win64-zst.rs
Outdated
#[lang = "sized"] | ||
trait Sized {} | ||
|
||
// Make sure the argument is always ignored when explicitly requesting "win64" or "sysv64" ABI. |
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.
clang has the pointer to ZST argument for win64, fastcall, and vectorcall, but not for sysv64:
https://clang.godbolt.org/z/Wr4jMWq3P
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.
Wait, I thought the point of #132893 was that ZST arguments should get ignored for win64?
Can you translate "rustc's f reads from rcx whereas clang's f reads from rdx" into something understandable that does not involve register names?
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.
The test passes as-is on windows-msvc. So if "win64" on windows-gnu is wrong, then it must be the case that we should be ignoring ZST everywhere. Now you say ZST are not ignored by clang. Something does not make sense here.
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.
yeah, I'm saying both the test and rustc ABI lowering code are wrong aka. don't match clang.
Can you translate "rustc's f reads from rcx whereas clang's f reads from rdx" into something understandable that does not involve register names?
that was mostly so I could show we generate wrong assembly, it's much easier to instead match the llvm-ir that is produced:
https://clang.godbolt.org/z/en55T7Tnj
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.
The test passes as-is on windows-msvc. So if "win64" on windows-gnu is wrong
it's the handling on rustc's msvc target that's wrong. I was using clang's gnu target merely so I could obtain a ZST to demonstrate. (rustc's gnu target may also be wrong).
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.
Hm, that doesn't seem right to me. I'm pretty sure struct Z {}
should be 1 byte. And struct Z { ty v[]; }
should have the same layout as struct Z { ty v[1]; }
.
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 you're referencing https://clang.godbolt.org/z/en55T7Tnj from above,
No, I am talking about Rust targets.
The status quo is that extern "C"
on windows-msvc skips ZST, but on windows-gnu it does not. I don't know where that comes from, but your bugreport is only about extern "win64"
so I assume extern "C"
is correct. If extern "win64"
is meant to be the same cross-target then it should match either windows-gnu-C or windows-msvc-C. It seems you are saying that it should match windows-gnu-C, which seems rather surprising to me -- but that is what this PR does now.
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.
The latest version of this PR always passes ZST by-ref, except for windows-msvc extern "C"
functions. This makes no sense at all to me.
Should we just always use by-ref passing x86_win64? Does anyone know why we skip ZST on windows-msvc extern "C"
?
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 was able to track this back to e177207, which landed in #32080 which is a huge PR and I can't tell if this is new logic or carried over from earlier. The PR description indicates that this PR is where we started skipping ZST arguments, and then an exception was added to not do that on 64bit-windows-gnu. Since then the ABI adjustment logic has been refactored many times and now it looks like windows-msvc is the exception, not windows-gnu... so it seems to me like we could just consistently do this for all windows targets.
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.
When it comes to windows-gnu*
, matching whatever Clang does when targeting windows-gnu
is the best approach. Dunno about msvc.
618c2cf
to
84624b2
Compare
e61af85
to
fa92272
Compare
Cc @beetrees |
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.
lgtm assuming this matches clang. I'm just reviewing the test cases since i'm not that familiar with the compiler's internals.
Ok so to sum up because I was confused:
This seems like it should be fine. It's the sort of situation where I'd love if we could do a crater run just in case but that's not an option. However, considering it's a portability hazard, I would doubt people are relying on the old behaviour. I've been wrong before though. |
I hope it matches clang but I am relying on what others tell me about this. |
df7bcd4
to
0b1d7d0
Compare
spec::abi::Abi::SysV64 { .. } => x86_64::compute_abi_info(cx, self), | ||
spec::abi::Abi::Win64 { .. } => x86_win64::compute_abi_info(cx, self), | ||
ExternAbi::SysV64 { .. } => x86_64::compute_abi_info(cx, self), | ||
ExternAbi::Win64 { .. } | ExternAbi::Vectorcall { .. } => { |
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.
This is the one actual change in this file: we treat Vectorcall
as x86_win64
even on non-windows targets.
42c8afd
to
34bd445
Compare
Okay this should be ready then. |
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.
LGTM. You can r=me if @programmerjake has no concerns
@bors r=SparrowLii |
☀️ Test successful - checks-actions |
Finished benchmarking commit (7a202a9): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)Results (primary 2.7%, secondary -1.4%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResults (secondary -6.4%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 764.23s -> 763.215s (-0.13%) |
The Microsoft calling conventions do not really say anything about ZST since they do not seem to exist in MSVC. However, both GCC and clang allow passing ZST over
__attribute__((ms_abi))
functions (which matches ourextern "win64" fn
) onwindows-gnu
targets, and therefore implicitly define a de-facto ABI for these types (and lucky enough they seem to define the same ABI). This ABI should be the same for windows-msvc and windows-gnu targets, so we use this as a hint for how to implement this ABI everywhere: we always pass ZST by-ref.The best alternative would be to just reject compiling functions which cannot exist in MSVC, but that would be a breaking change.
Cc @programmerjake @ChrisDenton
Fixes #132893