-
Notifications
You must be signed in to change notification settings - Fork 13k
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
base: master
Are you sure you want to change the base?
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. |
if cx.target_spec().os == "windows" | ||
&& cx.target_spec().env == "gnu" | ||
&& arg.layout.is_zst() | ||
&& fn_abi.conv != Conv::X86_64Win64 |
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 needs to also cover at least fastcall and vectorcall which have basically the same issue: https://clang.godbolt.org/z/75fo4cbhn
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.
Using fastcall and vectorcall with GCC causes the binaries to segfault, so GCC might be wrong 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.
hmm, at least gcc matches clang for the short functions i tested above...
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.
Do we have anything matching __attribute__((sysv_abi))
?
If the binaries segfault then maybe it's not so clear what fastcall and vectorcall should do?
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.
Do we have anything matching
__attribute__((sysv_abi))
?
yes, sysv64
, however it isn't broken afaict.
If the binaries segfault then maybe it's not so clear what fastcall and vectorcall should do?
perhaps, however I think it's at least worth matching clang where they do work.
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.
Strangely, the test fails for fastcall on windows-gnu (i.e., we do pass the ZST argument there). I don't yet understand why.
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.
Ah, we currently have explicit logic in adjust_abi
which says that on x86-64, "fastcall" should be like "C". This is obviously in contradiction with saying that "fastcall" should ignore ZST while "C" should pass them by-pointer.
I'm not a Windows expert so I don't know what is correct 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.
this needs to also cover at least fastcall and vectorcall which have basically the same issue: https://clang.godbolt.org/z/75fo4cbhn
Odd, so vectorcall was correct before this PR already? Not quite sure how that is happening.
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.
Anyway the latest version should handle vectorcall and fastcall, hopefully.
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.
perhaps, however I think it's at least worth matching clang where they do work.
Sure, just pointed out this is a mess with GCC.
A couple years ago I noticed some fastcall tests disabled on windows-gnu because they were failing when objects were built with GCC but worked fine when built with Clang. So yeah, while GCC de facto defines the standard, it's broken in many ways.
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
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. |
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