Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

RalfJung
Copy link
Member

@RalfJung RalfJung commented Jan 7, 2025

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 our extern "win64" fn) on windows-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

@rustbot
Copy link
Collaborator

rustbot commented Jan 7, 2025

r? @SparrowLii

rustbot has assigned @SparrowLii.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@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 Jan 7, 2025
@ChrisDenton
Copy link
Member

cc @mati865 as this makes a change to the windows-gnu target. Does it sound right to you?

@jieyouxu jieyouxu added the O-windows-gnu Toolchain: GNU, Operating system: Windows label Jan 7, 2025
@mati865
Copy link
Contributor

mati865 commented Jan 7, 2025

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
Copy link
Member

@programmerjake programmerjake Jan 7, 2025

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

Copy link
Contributor

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...

Copy link
Member

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...

Copy link
Member Author

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?

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member Author

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.

Copy link
Member Author

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.

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Member

@SparrowLii SparrowLii left a 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!

tests/codegen/abi-win64-zst.rs Show resolved Hide resolved
@rustbot
Copy link
Collaborator

rustbot commented Jan 8, 2025

These commits modify compiler targets.
(See the Target Tier Policy.)

#[lang = "sized"]
trait Sized {}

// Make sure the argument is always ignored when explicitly requesting "win64" or "sysv64" ABI.
Copy link
Member

@programmerjake programmerjake Jan 8, 2025

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

Copy link
Member Author

@RalfJung RalfJung Jan 8, 2025

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?

Copy link
Member Author

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.

Copy link
Member

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

Copy link
Member

@programmerjake programmerjake Jan 8, 2025

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).

Copy link
Member

@ChrisDenton ChrisDenton Jan 8, 2025

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]; }.

Copy link
Member Author

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.

Copy link
Member Author

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"?

Copy link
Member Author

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.

Copy link
Contributor

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.

@RalfJung RalfJung force-pushed the win64-zst branch 2 times, most recently from 618c2cf to 84624b2 Compare January 8, 2025 11:58
@RalfJung RalfJung changed the title fix handling of ZST in win64 ABI on windows-gnu targets fix handling of ZST in win64 ABI on windows-msvc targets Jan 8, 2025
@RalfJung
Copy link
Member Author

RalfJung commented Jan 8, 2025

Cc @beetrees

Copy link
Member

@programmerjake programmerjake left a 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.

@ChrisDenton
Copy link
Member

Ok so to sum up because I was confused:

  • this only affects MSVC
  • this changes function argument passing:
    • previously ZSTs were simply ignored
    • now they're passed by pointer indirection
  • passing ZSTs is not possible with MSVC so we're using the clang behaviour

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-windows-gnu Toolchain: GNU, Operating system: Windows 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ABI mismatch between rustc and clang for passing ZSTs using the win64 ABI.
7 participants