Skip to content

Ensure that BridgedTypeArray is indirectly returned #76433

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

Merged
merged 1 commit into from
Sep 16, 2024

Conversation

hjyamauchi
Copy link
Contributor

On Windows ARM64, how a struct value type is returned is sensitive to conditions including whether a user-defined constructor exists, etc. See

https://learn.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions?view=msvc-170#return-values

That caused a calling convention mismatch between the non-USED_IN_CPP_SOURCE (Swift) side and the USE_IN_CPP_SOURCE (C++) side and a crash.

Add this constructor so that the calling convention matches.

This is a fix for the OnoneSimplification crash in

#74866 (comment)

and is a partial fix for

#74866 (comment)

On Windows ARM64, how a struct value type is returned is sensitive to
conditions including whether a user-defined constructor exists,
etc. See

https://learn.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions?view=msvc-170#return-values

That caused a calling convention mismatch between the
non-USED_IN_CPP_SOURCE (Swift) side and the USE_IN_CPP_SOURCE (C++)
side and a crash.

Add this constructor so that the calling convention matches.

This is a fix for the OnoneSimplification crash in

swiftlang#74866 (comment)

and is a partial fix for

swiftlang#74866 (comment)
@hjyamauchi
Copy link
Contributor Author

@swift-ci please test

@DougGregor
Copy link
Member

Excellent detective work tracking down this issue. I expect this is going to affect pretty much any C++ class type that we bridge over to Swift, so the risk of regressing here is fairly high. Can we think of any way to make sure that these types are handled uniformly, even if it's just fixing them all and documenting the pattern so we know what to do in the future (and why)?

@hjyamauchi
Copy link
Contributor Author

Yeah, a very good point!

I'm curious why we need to use #ifdef USED_IN_CPP_SOURCE to hide certain types (the LLVM types?) from the Swift side (I just haven't understood that yet). I assume that there are good reasons for that but I wonder if there could be some alternative ways now that we know about this issue.

I also wonder if there is a way to statically force this constraint (struct is indirectly returned as value) using static_assert or something but I'm not sure if there are ways to express the constraint in C++ itself.

If there are no alternative solution, I'd at least vote for the low-tech approach of "just fixing them all and documenting the pattern so we know what to do in the future (and why)". If that requires a lot of manual editing, I'd be happy to help with that (or I may just end up doing that :)

@hjyamauchi
Copy link
Contributor Author

@swift-ci please test macOS platform

@hjyamauchi hjyamauchi marked this pull request as ready for review September 16, 2024 17:54
@hjyamauchi hjyamauchi requested review from eeckstein, hyp, compnerd and DougGregor and removed request for jckarter, hyp, egorzhdan and zoecarver September 16, 2024 17:54
Copy link
Member

@DougGregor DougGregor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Please feel free to merge. Do we also need this for 6.0?

@hjyamauchi hjyamauchi merged commit 7633c00 into swiftlang:main Sep 16, 2024
5 checks passed
@hjyamauchi
Copy link
Contributor Author

Thanks. Merged! Yes, I think 6.0 also needs this because SwiftCompilerSources is enabled on 6.0 for all OSes/CPUs including Windows ARM64. Unless the alternative seems better, I'd like to defer cherrypicking this to 6.0 until SwiftCompilerSources becomes fully functional (that is, #74866 is resolved)

@DougGregor
Copy link
Member

I agree that it makes sense to defer cherry-picking until we have all of the changes needed to make it fully functional

@eeckstein
Copy link
Contributor

I'm curious why we need to use #ifdef USED_IN_CPP_SOURCE to hide certain types

This is the "PURE" bridging mode. To workaround some problems with C++ interop. See #69039.
Last time I tried, I could only build with the pure mode on windows.

hjyamauchi added a commit to hjyamauchi/swift that referenced this pull request Sep 19, 2024
On Windows ARM64, how a struct value type is returned is sensitive to
conditions including whether a user-defined constructor exists,
etc. See

https://learn.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions?view=msvc-170#return-values

That caused a calling convention mismatch between the
non-USED_IN_CPP_SOURCE (Swift) side and the USE_IN_CPP_SOURCE (C++)
side and a crash.

Following swiftlang#76433 add
constructors to several bridged C++ struct/class types so that the
calling convention matches.

This is a partial fix for swiftlang#74866
hjyamauchi added a commit to hjyamauchi/swift that referenced this pull request Oct 28, 2024
On Windows ARM64, how a struct value type is returned is sensitive to
conditions including whether a user-defined constructor exists,
etc. See

https://learn.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions?view=msvc-170#return-values

That caused a calling convention mismatch between the
non-USED_IN_CPP_SOURCE (Swift) side and the USE_IN_CPP_SOURCE (C++)
side and a crash.

Following swiftlang#76433 add
constructors to several bridged C++ struct/class types so that the
calling convention matches.

This is a partial fix for swiftlang#74866

Cherrypick swiftlang#76589
hjyamauchi added a commit to hjyamauchi/swift that referenced this pull request Oct 28, 2024
On Windows ARM64, how a struct value type is returned is sensitive to
conditions including whether a user-defined constructor exists,
etc. See

https://learn.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions?view=msvc-170#return-values

That caused a calling convention mismatch between the
non-USED_IN_CPP_SOURCE (Swift) side and the USE_IN_CPP_SOURCE (C++)
side and a crash.

Add this constructor so that the calling convention matches.

This is a fix for the OnoneSimplification crash in

and is a partial fix for

Cherrypick swiftlang#76433
hjyamauchi added a commit to hjyamauchi/swift that referenced this pull request Oct 28, 2024
On Windows ARM64, how a struct value type is returned is sensitive to
conditions including whether a user-defined constructor exists,
etc. See

https://learn.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions?view=msvc-170#return-values

That caused a calling convention mismatch between the
non-USED_IN_CPP_SOURCE (Swift) side and the USE_IN_CPP_SOURCE (C++)
side and a crash.

Following swiftlang#76433 add constructors to several bridged C++ struct/class
types so that the calling convention matches.

This is a partial fix for swiftlang#74866

Cherrypick swiftlang#76589
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants