Skip to content

8307253: Make FunctionDescriptor::toMethodType an instance method of Linker #830

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

Closed

Conversation

mcimadamore
Copy link
Collaborator

@mcimadamore mcimadamore commented May 2, 2023

This patch moves FunctionDescriptor::toMethodType as an instance method of Linker. This allows linker implementations to be more opinionated on how the function descriptor -> method type conversion should behave.


Progress

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

Issue

  • JDK-8307253: Make FunctionDescriptor::toMethodType an instance method of Linker

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 830

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

Using diff file

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

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented May 2, 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.

for (StorageCalculator.StructStorage(
long offset, Class<?> ca, int byteWidth, VMStorage storage
) : structStorages) {
for (StorageCalculator.StructStorage structStorage : structStorages) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This change is not strictly required, but I was getting weird errors when building javadoc from IntelliJ w/o it. Since patterns in for-each are going away anyways, I thought perhaps we could just take care of this.

@openjdk openjdk bot added the rfr Ready for review label May 2, 2023
@mlbridge
Copy link

mlbridge bot commented May 2, 2023

Webrevs

Fix javadoc and impl for FunctionDescriptor::toMethodType to allow sequence layouts.
@mcimadamore
Copy link
Collaborator Author

mcimadamore commented May 4, 2023

After some offline discussion, we realized that the current API is actually good enough: FunctionDescriptor::toMethodType defines the "lowest common denominator" for turning a function descriptor into a MethodType. In doing this conversion, we follow the carrier types indicated by value layouts. For structs/unions/sequences we assume the carrier is MemorySegment (it is possible that in the future we will also add a carrier() accessor on composite layouts that can be overridden). Finally, for padding layout we just throw (after all, a padding argument/return doesn't seem to make much sense). Capturing this conversion at the level of FunctionDescription still seems like the right thing to do.

However, there is a small detail which this patch fixes: FunctionDescriptor::toMethodType currently rejects sequence layouts. While it is certainly the case that the C ABI does not allow to pass arrays by value. And even if this is the most common choice, this is not the only possible choice (for instance, Pascal has special convention for passing arrays to functions where the size of the array is known). As such, I think we should not bias the conversion with what we know the native linker will and will not support.

(sidebar: I've personally always been of the opinion that, even if C ABI do not allow passing arrays by value, since we have a relatively straightforward way to turn a sequence layout into an equivalent struct layout, perhaps the native linker should also be not too biased about which layouts it accepts - after all struct Point { int x; int y; } and int[2] are not so far apart)

For this reason, I've tweaked the checks in the conversion, and then made some checks stricter in the linker - so that now sequence layouts are rejected at the level of the linker.

@openjdk
Copy link

openjdk bot commented May 4, 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:

8307253: Make FunctionDescriptor::toMethodType an instance method of Linker

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 no new commits pushed to the foreign-memaccess+abi branch. If another commit should be pushed before you perform the /integrate command, your PR will be automatically rebased. If you prefer to avoid any potential 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 May 4, 2023
Copy link
Member

@JornVernee JornVernee left a comment

Choose a reason for hiding this comment

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

Nice! Couple of minor comments.

if (resLayout instanceof PaddingLayout) {
throw new IllegalArgumentException("Unsupported padding layout return in function descriptor: " + resLayout);
}
Optional<MemoryLayout> paddingLayout = argLayouts.stream().filter(l -> l instanceof PaddingLayout).findAny();
Copy link
Member

Choose a reason for hiding this comment

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

This could use Stream::anyMatch.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Uhm - I thought about that when writing the code, but then I can't print out the problematic layout.

Copy link
Member

Choose a reason for hiding this comment

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

Ok.

@mcimadamore
Copy link
Collaborator Author

/integrate

@openjdk
Copy link

openjdk bot commented May 8, 2023

Going to push as commit 139a8d2.

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

openjdk bot commented May 8, 2023

@mcimadamore Pushed as commit 139a8d2.

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

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.

2 participants