Skip to content

8308293: A linker should expose the layouts it supports#839

Closed
mcimadamore wants to merge 5 commits intoopenjdk:foreign-memaccess+abifrom
mcimadamore:linker_canonical_types
Closed

8308293: A linker should expose the layouts it supports#839
mcimadamore wants to merge 5 commits intoopenjdk:foreign-memaccess+abifrom
mcimadamore:linker_canonical_types

Conversation

@mcimadamore
Copy link
Collaborator

@mcimadamore mcimadamore commented Jun 15, 2023

This is another stab at openjdk/jdk#14037

I believe, after some offline discussion, that we have found a more satisfying solution to the problem of JAVA_CHAR being exposed. Jorn suggested that linkers should also provide mappings for JNI types such as jint, jshort and such (which are aliases for our layout constants anyway). I think that's a great way to bring JAVA_CHAR back into the fold.

For now, I decided not to specify support for JNI canonical layouts (but I could do so, if that's preferred). I think the highest priority is to provide some stable mappings for C builtin types (+ size_t) as that's what 99% of developers will be struggling with.

API-wise, we just expose a map. In the preovious PR there were questions as to whether the map should be split into two methods. In general I see the following options:

  1. Just expose a map (that's the primitive, other things can derived from it)
  2. Expose a map, plus a method to get a canonical layout from a type name (that's the Charset approach, which has both availableCharsets and forName)
  3. Expose a method to get canonical layout from name, plus a method that returns the set of supported canonical layout names

My (not so strong) preference would be for either (1) or (2).


Progress

  • Change must not contain extraneous whitespace
  • Change must be properly reviewed (1 review required, with at least 1 Committer)

Issues

  • JDK-8308293: A linker should expose the layouts it supports (Bug - P3)
  • JDK-8308304: A linker should expose the layouts it supports (CSR) (Withdrawn)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/panama-foreign.git pull/839/head:pull/839
$ git checkout pull/839

Update a local copy of the PR:
$ git checkout pull/839
$ git pull https://git.openjdk.org/panama-foreign.git pull/839/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 839

View PR using the GUI difftool:
$ git pr show -t 839

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/panama-foreign/pull/839.diff

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Jun 15, 2023

👋 Welcome back mcimadamore! A progress list of the required criteria for merging this PR into foreign-memaccess+abi will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Ready for review label Jun 15, 2023
@mlbridge
Copy link

mlbridge bot commented Jun 15, 2023

Webrevs

@minborg
Copy link
Collaborator

minborg commented Jun 15, 2023

Maybe we could add a test that ensures basic mapping is in place?

@mcimadamore
Copy link
Collaborator Author

Maybe we could add a test that ensures basic mapping is in place?

Good idea.

Add test
Add fallback linker support
/*
* @test
* @enablePreview
* @requires jdk.foreign.linker != "UNSUPPORTED"
Copy link
Member

@JornVernee JornVernee Jun 16, 2023

Choose a reason for hiding this comment

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

This shouldn't be removed I think? (Note that the fallback linker is FALLBACK, so this test was already enabled for it)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Apologies - I was fighting with jtreg to get the fallback linker enabled (so I could test). This line got in between :-)

@openjdk
Copy link

openjdk bot commented Jun 16, 2023

@mcimadamore This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8308293: A linker should expose the layouts it supports

Reviewed-by: jvernee

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 33 new commits pushed to the foreign-memaccess+abi branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the foreign-memaccess+abi branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Ready to be integrated label Jun 16, 2023
@mcimadamore
Copy link
Collaborator Author

Following some offline discussion, I've also decided to add support for wchar_t which is a type with platform-dependent size that can be useful when working with wide strings (esp. given the new, more general string methods).

static final MemorySegment FLOAT_TYPE = MemorySegment.ofAddress(ffi_type_float());
static final MemorySegment DOUBLE_TYPE = MemorySegment.ofAddress(ffi_type_double());
static final MemorySegment POINTER_TYPE = MemorySegment.ofAddress(ffi_type_pointer());
static final int SIZEOF_SHORT = ffi_sizeof_short();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Decided to change this a bit - e.g. instead of returning FFI type (which we don't use), I return sizeof instead, which seems more directly usable. This leads to some code simplifications (see definition of SIZE_T in FFIType).

Copy link
Member

@JornVernee JornVernee Jun 16, 2023

Choose a reason for hiding this comment

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

This looks good. ffi_type also has an alignment field, which we could also theoretically look at, but I think it's fine to assume that we always have naturally aligned types here.

@mcimadamore
Copy link
Collaborator Author

So, now that we got the implementation nailed, any further opinion on the API bikeshed? E.g. is a Map enough? Do we want to add other methods? Given this is a rather niche functionality (e.g. mostly for clients that want to dynamically discover layouts), my general feeling is to keep it simple and just expose a map. But I don't have a super strong opinion.

@JornVernee
Copy link
Member

I'm in favor of using a map as well. I don't think there are any issue with doing that?

@mcimadamore
Copy link
Collaborator Author

/integrate

@openjdk
Copy link

openjdk bot commented Jun 19, 2023

Going to push as commit 7a6fc80.
Since your change was applied there have been 33 commits pushed to the foreign-memaccess+abi branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Jun 19, 2023
@openjdk openjdk bot closed this Jun 19, 2023
@openjdk openjdk bot removed ready Ready to be integrated rfr Ready for review labels Jun 19, 2023
@openjdk
Copy link

openjdk bot commented Jun 19, 2023

@mcimadamore Pushed as commit 7a6fc80.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.


@Override
public Map<String, MemoryLayout> canonicalLayouts() {
return CANONICAL_LAYOUTS;
Copy link

Choose a reason for hiding this comment

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

CANONICAL_LAYOUTS is set to a HashMap, which is not unmodifiable.

Suggested change
return CANONICAL_LAYOUTS;
return Collections.unmodifiableMap(CANONICAL_LAYOUTS);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

4 participants