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

Merged
merged 2 commits into from
Jan 13, 2025

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.

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!

@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 RalfJung force-pushed the win64-zst branch 2 times, most recently from e61af85 to fa92272 Compare January 8, 2025 14:11
@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.

@RalfJung
Copy link
Member Author

RalfJung commented Jan 9, 2025

lgtm assuming this matches clang

I hope it matches clang but I am relying on what others tell me about this.

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 { .. } => {
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 is the one actual change in this file: we treat Vectorcall as x86_win64 even on non-windows targets.

@RalfJung RalfJung force-pushed the win64-zst branch 2 times, most recently from 42c8afd to 34bd445 Compare January 10, 2025 11:07
@RalfJung
Copy link
Member Author

Okay this should be ready then.

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.

LGTM. You can r=me if @programmerjake has no concerns

@RalfJung
Copy link
Member Author

@bors r=SparrowLii

@bors
Copy link
Contributor

bors commented Jan 13, 2025

📌 Commit 675a103 has been approved by SparrowLii

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-review Status: Awaiting review from the assignee but also interested parties. labels Jan 13, 2025
@bors
Copy link
Contributor

bors commented Jan 13, 2025

⌛ Testing commit 675a103 with merge 7a202a9...

@bors
Copy link
Contributor

bors commented Jan 13, 2025

☀️ Test successful - checks-actions
Approved by: SparrowLii
Pushing 7a202a9 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jan 13, 2025
@bors bors merged commit 7a202a9 into rust-lang:master Jan 13, 2025
7 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Jan 13, 2025
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (7a202a9): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

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

mean range count
Regressions ❌
(primary)
2.7% [1.5%, 3.9%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.4% [-1.4%, -1.4%] 1
All ❌✅ (primary) 2.7% [1.5%, 3.9%] 2

Cycles

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

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-6.4% [-6.4%, -6.4%] 1
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 764.23s -> 763.215s (-0.13%)
Artifact size: 326.05 MiB -> 326.11 MiB (0.02%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. O-windows-gnu Toolchain: GNU, Operating system: Windows 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.

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