Skip to content

Model Swift's type metadata and value witness table for memory layout information#42

Merged
DougGregor merged 4 commits into
swiftlang:mainfrom
DougGregor:jextract-value-witness-memory-layout
Oct 5, 2024
Merged

Model Swift's type metadata and value witness table for memory layout information#42
DougGregor merged 4 commits into
swiftlang:mainfrom
DougGregor:jextract-value-witness-memory-layout

Conversation

@DougGregor
Copy link
Copy Markdown
Member

@DougGregor DougGregor commented Oct 4, 2024

Extend the Java SwiftKit with the ability to query the memory layout of an arbitrary Swift type given its Swift type metadata pointer (e.g., Any.Type in the Swift world). Use this to show the size, stride, and alignment of Int? from the Java side, determine the name of a Swift type from its metadata, and produce a java.lang.foreign.MemoryLayout for any Swift type. Here's some example output for the Swift type Int? (mangled name: SiSg):

Memory layout for Swift.Int?:
  size = 9
  stride = 16
  alignment = 8
  Java layout = [8%[9:b1]x7](Swift.Optional<Swift.Int>)

… information

Extend the Java SwiftKit with the ability to query the memory layout of an
arbitrary Swift type given its Swift type metadata pointer (e.g., `Any.Type` in
the Swift world). Use this to show the size, stride, and alignment of `Int?`
from the Java side, which produces:

    Memory layout for Swift.Int?:
      size = 9
      stride = 16
      alignment = 8
Copy link
Copy Markdown
Collaborator

@ktoso ktoso left a comment

Choose a reason for hiding this comment

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

Very nice, might be worth adding a test in java already, even tho most fail on CI they should already be working fine on macOS 😊

* the size of a pointer (aka C's ptrdiff_t).
*/
public static ValueLayout SWIFT_INT = (ValueLayout.ADDRESS.byteSize() == 4) ?
ValueLayout.JAVA_INT : ValueLayout.JAVA_LONG;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Oh nice, instead of guessing by platform. Lgtm

…ft type

Given Swift type metadata, inspect the metadata to produce a memory layout
for the Swift type that covers its size/alignment and can be used to refer
to memory containing a Swift value of that type.
@DougGregor
Copy link
Copy Markdown
Member Author

Tacked on the real API I wanted, which produces a memory layout for a given Swift type

Copy link
Copy Markdown
Collaborator

@ktoso ktoso left a comment

Choose a reason for hiding this comment

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

Very nice, that’ll get us quite far already 🎉

@DougGregor
Copy link
Copy Markdown
Member Author

We're still working on the Java part of the CI; verified it locally.

@DougGregor DougGregor merged commit 8bbe21b into swiftlang:main Oct 5, 2024
@DougGregor DougGregor deleted the jextract-value-witness-memory-layout branch October 5, 2024 05:49
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.

2 participants