Skip to content

[interop][SwiftToCxx] Gather initial struct layout information and em… #59119

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 2 commits into from
Jun 3, 2022

Conversation

hyp
Copy link
Contributor

@hyp hyp commented May 27, 2022

…it struct stubs with storage in C++

This change extends the clang header printer to start emitting C++ classes for Swift struct types with the correct struct layout in them (size + alignment)

Note: the current header emission logic does not yet force SILGen/IRGen, so we will only emit types when the compiler gets to IRGen. This is something that will be addressed in the future (we should probably require SILGen/IRGen for header emission?)

@hyp hyp requested review from rjmccall, egorzhdan and zoecarver May 27, 2022 00:45
@hyp
Copy link
Contributor Author

hyp commented May 27, 2022

@swift-ci please test

@zoecarver zoecarver added the c++ interop Feature: Interoperability with C++ label May 27, 2022
@@ -25,9 +24,22 @@
using namespace swift;

void ClangValueTypePrinter::printStructDecl(const StructDecl *SD) {
auto structABIInfo = interopContext.getStructDeclInfo(SD);
if (!structABIInfo) {
// FIXME: handle non-fixed layout structs.
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it make sense to print out something here like <resilient-struct>?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe print out an unavailable forward class declaration?

return;
}
if (structABIInfo->size == 0) {
// FIXME: How to represent 0 sized structs?
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be an assertion?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A user-facing error might be better. Or alternatively we can emit a class that's marked as unavailable, wdyt?

@@ -1,20 +1,26 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -typecheck -module-name Structs -clang-header-expose-public-decls -emit-clang-header-path %t/structs.h
// RUN: %target-swift-frontend %s -typecheck -emit-ir -module-name Structs -clang-header-expose-public-decls -emit-clang-header-path %t/structs.h
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does this need the IR now?

Copy link
Contributor Author

@hyp hyp May 31, 2022

Choose a reason for hiding this comment

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

Right now I'm getting the layout info after canonical SIL is constructed. I definitely need SIL to gather the layout info. That's what I mentioned in my description:

Note: the current header emission logic does not yet force SILGen/IRGen, so we will only emit types when the compiler gets to IRGen. This is something that will be addressed in the future (we should probably require SILGen/IRGen for header emission?)

I think that's something I should ask John in the sync-up as well.

@@ -0,0 +1,61 @@
//===--- SwiftIRDetailsCollector.h - Get ABI details for decls --*- C++ -*-===//
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we could just expose the type info from IRGen? It would be a big-ish refactor, but maybe we could just factor that API out of IRGen? Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking about that, but I would need to pull out a lot of logic out of IRGen right now, and it's unclear how much I actually need just to get the layout and ABI right. Perhaps it's the right thing to do, but it's not obvious to me just yet. This is something I can discuss with @rjmccall in our sync up. For now I can actually refactor this to pull out specific query interface instead of having a visitor .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We decided to provide a high level interface for now that's pretty much decoupled from IRGen / SILGen but implemented using IRGen by setting up the required context, so that we can avoid depending on running SILGen/IRGen in order to emit the header.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will update the patch to reflect that.

@hyp
Copy link
Contributor Author

hyp commented May 31, 2022

@swift-ci please test linux platform

@hyp
Copy link
Contributor Author

hyp commented Jun 1, 2022

@swift-ci please test

…it struct stubs with storage in C++

This change extends the clang header printer to start emitting C++ classes for Swift struct types with the correct struct layout in them (size + alignment)
@hyp hyp force-pushed the eng/swift-struct-layout-in-cxx branch from b89c0ea to 42c6ff6 Compare June 1, 2022 21:06
@hyp
Copy link
Contributor Author

hyp commented Jun 1, 2022

Updated.

@hyp
Copy link
Contributor Author

hyp commented Jun 1, 2022

@swift-ci please test

@hyp
Copy link
Contributor Author

hyp commented Jun 1, 2022

@swift-ci please test

@hyp
Copy link
Contributor Author

hyp commented Jun 1, 2022

@swift-ci please test source compatibility

3 similar comments
@hyp
Copy link
Contributor Author

hyp commented Jun 2, 2022

@swift-ci please test source compatibility

@hyp
Copy link
Contributor Author

hyp commented Jun 2, 2022

@swift-ci please test source compatibility

@hyp
Copy link
Contributor Author

hyp commented Jun 2, 2022

@swift-ci please test source compatibility

@hyp hyp merged commit c7eaeb0 into swiftlang:main Jun 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants