On Tue, 10 Nov 2020 14:16:22 GMT, Maurizio Cimadamore <mcimadam...@openjdk.org> 
wrote:

>> This patch contains the changes associated with the first incubation round 
>> of the foreign linker access API incubation
>> (see JEP 389 [1]). This work is meant to sit on top of the foreign memory 
>> access support (see JEP 393 [2] and associated pull request [3]).
>> 
>> The main goal of this API is to provide a way to call native functions from 
>> Java code without the need of intermediate JNI glue code. In order to do 
>> this, native calls are modeled through the MethodHandle API. I suggest 
>> reading the writeup [4] I put together few weeks ago, which illustrates what 
>> the foreign linker support is, and how it should be used by clients.
>> 
>> Disclaimer: the pull request mechanism isn't great at managing *dependent* 
>> reviews. For this reasons, I'm attaching a webrev which contains only the 
>> differences between this PR and the memory access PR. I will be periodically 
>> uploading new webrevs, as new iterations come out, to try and make the life 
>> of reviewers as simple as possible.
>> 
>> A big thank to Jorn Vernee and Vladimir Ivanov - they are the main 
>> architects of all the hotspot changes you see here, and without their help, 
>> the foreign linker support wouldn't be what it is today. As usual, a big 
>> thank to Paul Sandoz, who provided many insights (often by trying the bits 
>> first hand).
>> 
>> Thanks
>> Maurizio
>> 
>> Webrev:
>> http://cr.openjdk.java.net/~mcimadamore/8254231_v1/webrev
>> 
>> Javadoc:
>> 
>> http://cr.openjdk.java.net/~mcimadamore/8254231_v1/javadoc/jdk/incubator/foreign/package-summary.html
>> 
>> Specdiff (relative to [3]):
>> 
>> http://cr.openjdk.java.net/~mcimadamore/8254231_v1/specdiff_delta/overview-summary.html
>> 
>> CSR:
>> 
>> https://bugs.openjdk.java.net/browse/JDK-8254232
>> 
>> 
>> 
>> ### API Changes
>> 
>> The API changes are actually rather slim:
>> 
>> * `LibraryLookup`
>>   * This class allows clients to lookup symbols in native libraries; the 
>> interface is fairly simple; you can load a library by name, or absolute 
>> path, and then lookup symbols on that library.
>> * `FunctionDescriptor`
>>   * This is an abstraction that is very similar, in spirit, to `MethodType`; 
>> it is, at its core, an aggregate of memory layouts for the function 
>> arguments/return type. A function descriptor is used to describe the 
>> signature of a native function.
>> * `CLinker`
>>   * This is the real star of the show. A `CLinker` has two main methods: 
>> `downcallHandle` and `upcallStub`; the first takes a native symbol (as 
>> obtained from `LibraryLookup`), a `MethodType` and a `FunctionDescriptor` 
>> and returns a `MethodHandle` instance which can be used to call the target 
>> native symbol. The second takes an existing method handle, and a 
>> `FunctionDescriptor` and returns a new `MemorySegment` corresponding to a 
>> code stub allocated by the VM which acts as a trampoline from native code to 
>> the user-provided method handle. This is very useful for implementing 
>> upcalls.
>>    * This class also contains the various layout constants that should be 
>> used by clients when describing native signatures (e.g. `C_LONG` and 
>> friends); these layouts contain additional ABI classfication information (in 
>> the form of layout attributes) which is used by the runtime to *infer* how 
>> Java arguments should be shuffled for the native call to take place.
>>   * Finally, this class provides some helper functions e.g. so that clients 
>> can convert Java strings into C strings and back.
>> * `NativeScope`
>>   * This is an helper class which allows clients to group together logically 
>> related allocations; that is, rather than allocating separate memory 
>> segments using separate *try-with-resource* constructs, a `NativeScope` 
>> allows clients to use a _single_ block, and allocate all the required 
>> segments there. This is not only an usability boost, but also a performance 
>> boost, since not all allocation requests will be turned into `malloc` calls.
>> * `MemorySegment`
>>   * Only one method added here - namely `handoff(NativeScope)` which allows 
>> a segment to be transferred onto an existing native scope.
>> 
>> ### Safety
>> 
>> The foreign linker API is intrinsically unsafe; many things can go wrong 
>> when requesting a native method handle. For instance, the description of the 
>> native signature might be wrong (e.g. have too many arguments) - and the 
>> runtime has, in the general case, no way to detect such mismatches. For 
>> these reasons, obtaining a `CLinker` instance is a *restricted* operation, 
>> which can be enabled by specifying the usual JDK property 
>> `-Dforeign.restricted=permit` (as it's the case for other restricted method 
>> in the foreign memory API).
>> 
>> ### Implementation changes
>> 
>> The Java changes associated with `LibraryLookup` are relative 
>> straightforward; the only interesting thing to note here is that library 
>> loading does _not_ depend on class loaders, so `LibraryLookup` is not 
>> subject to the same restrictions which apply to JNI library loading (e.g. 
>> same library cannot be loaded by different classloaders).
>> 
>> As for `NativeScope` the changes are again relatively straightforward; it is 
>> an API which sits neatly on top of the foreign meory access API, providing 
>> some kind of allocation service which shares the same underlying memory 
>> segment(s), and turns an allocation request into a segment slice, which is a 
>> much less expensive operation. `NativeScope` comes in two variants: there 
>> are native scopes for which the allocation size is known a priori, and 
>> native scopes which can grow - these two schemes are implemented by two 
>> separate subclasses of `AbstractNativeScopeImpl`.
>> 
>> Of course the bulk of the changes are to support the `CLinker` 
>> downcall/upcall routines. These changes cut pretty deep into the JVM; I'll 
>> briefly summarize the goal of some of this changes - for further details, 
>> Jorn has put together a detailed writeup which explains the rationale behind 
>> the VM support, with some references to the code [5].
>> 
>> The main idea behind foreign linker is to infer, given a Java method type 
>> (expressed as a `MethodType` instance) and the description of the signature 
>> of a native function (expressed as a `FunctionDescriptor` instance) a 
>> _recipe_ that can be used to turn a Java call into the corresponding native 
>> call targeting the requested native function.
>> 
>> This inference scheme can be defined in a pretty straightforward fashion by 
>> looking at the various ABI specifications (for instance, see [6] for the 
>> SysV ABI, which is the one used on Linux/Mac). The various `CallArranger` 
>> classes, of which we have a flavor for each supported platform, do exactly 
>> that kind of inference.
>> 
>> For the inference process to work, we need to attach extra information to 
>> memory layouts; it is no longer sufficient to know e.g. that a layout is 
>> 32/64 bits - we need to know whether it is meant to represent a floating 
>> point value, or an integral value; this knowledge is required because 
>> floating points are passed in different registers by most ABIs. For this 
>> reason, `CLinker` offers a set of pre-baked, platform-dependent layout 
>> constants which contain the required classification attributes (e.g. a 
>> `Clinker.TypeKind` enum value). The runtime extracts this attribute, and 
>> performs classification accordingly.
>> 
>> A native call is decomposed into a sequence of basic, primitive operations, 
>> called `Binding` (see the great javadoc on the `Binding.java` class for more 
>> info). There are many such bindings - for instance the `Move` binding is 
>> used to move a value into a specific machine register/stack slot. So, the 
>> main job of the various `CallingArranger` classes is to determine, given a 
>> Java `MethodType` and `FunctionDescriptor` what is the set of bindings 
>> associated with the downcall/upcall.
>> 
>> At the heart of the foreign linker support is the `ProgrammableInvoker` 
>> class. This class effectively generates a `MethodHandle` which follows the 
>> steps described by the various bindings obtained by `CallArranger`. There 
>> are actually various strategies to interpret these bindings - listed below:
>> 
>> * basic intepreted mode; in this mode, all bindings are interpreted using a 
>> stack-based machine written in Java (see `BindingInterpreter`), except for 
>> the `Move` bindings. For these bindings, the move is implemented by 
>> allocating a *buffer* (whose size is ABI specific) and by moving all the 
>> lowered values into positions within this buffer. The buffer is then passed 
>> to a piece of assembly code inside the VM which takes values from the buffer 
>> and moves them in their expected registers/stack slots (note that each 
>> position in the buffer corresponds to a different register). This is the 
>> most general invocation mode, the more "customizable" one, but also the 
>> slowest - since for every call there is some extra allocation which takes 
>> place.
>> 
>> * specialized interpreted mode; same as before, but instead of interpreting 
>> the bindings with a stack-based interpreter, we generate a method handle 
>> chain which effectively interprets all the bindings (again, except `Move` 
>> ones).
>> 
>> * intrinsified mode; this is typically used in combination with the 
>> specialized interpreted mode described above (although it can also be used 
>> with the Java-based binding interpreter). The goal here is to remove the 
>> buffer allocation and copy by introducing an additional JVM intrinsic. If a 
>> native call recipe is constant (e.g. the set of bindings is constant, which 
>> is probably the case if the native method handle is stored in a `static`, 
>> `final` field), then the VM can generate specialized assembly code which 
>> interprets the `Move` binding without the need to go for an intermediate 
>> buffer. This gives us back performances that are on par with JNI.
>> 
>> For upcalls, the support is not (yet) as advanced, and only the basic 
>> interpreted mode is available there. We plan to add support for intrinsified 
>> modes there as well, which should considerably boost perfomances (probably 
>> well beyond what JNI can offer at the moment, since the upcall support in 
>> JNI is not very well optimized).
>> 
>> Again, for more readings on the internals of the foreign linker support, 
>> please refer to [5].
>> 
>> #### Test changes
>> 
>> Many new tests have been added to validate the foreign linker support; we 
>> have high level tests (see `StdLibTest`) which aim at testing the linker 
>> from the perspective of code that clients could write. But we also have 
>> deeper combinatorial tests (see `TestUpcall` and `TestDowncall`) which are 
>> meant to stress every corner of the ABI implementation. There are also some 
>> great tests (see the `callarranger` folder) which test the various 
>> `CallArranger`s for all the possible platforms; these tests adopt more of a 
>> white-box approach - that is, instead of treating the linker machinery as a 
>> black box and verify that the support works by checking that the native call 
>> returned the results we expected, these tests aims at checking that the set 
>> of bindings generated by the call arranger is correct. This also mean that 
>> we can test the classification logic for Windows, Mac and Linux regardless 
>> of the platform we're executing on.
>> 
>> Some additional microbenchmarks have been added to compare the performances 
>> of downcall/upcall with JNI.
>> 
>> [1] - https://openjdk.java.net/jeps/389
>> [2] - https://openjdk.java.net/jeps/393
>> [3] - https://git.openjdk.java.net/jdk/pull/548
>> [4] - 
>> https://github.com/openjdk/panama-foreign/blob/foreign-jextract/doc/panama_ffi.md
>> [5] - 
>> http://cr.openjdk.java.net/~jvernee/docs/Foreign-abi%20downcall%20intrinsics%20technical%20description.html
>
> Maurizio Cimadamore has updated the pull request incrementally with 10 
> additional commits since the last revision:
> 
>  - Merge pull request #7 from JornVernee/Additional_Review_Comments
>    
>    Additional review comments
>  - Revert System.java changes
>  - Set copyright year for added files to 2020
>  - Check result of AttachCurrentThread
>  - Sort includes alphabetically
>  - Relax ret_addr_offset() assert
>  - Extra space after if
>  - remove excessive asserts in ProgrammableInvoker::invoke_native
>  - Remove os::is_MP() check
>  - remove blank line in thread.hpp

I made a pass over hotspot code. Overall, it looks good. Some comments follow.

src/hotspot/cpu/aarch64/vmreg_aarch64.cpp line 57:

> 55: #define INTEGER_TYPE 0
> 56: #define VECTOR_TYPE 1
> 57: #define X87_TYPE 2

Unused.

src/hotspot/cpu/aarch64/foreign_globals_aarch64.hpp line 31:

> 29: #include "utilities/growableArray.hpp"
> 30: 
> 31: #define __ _masm->

Should be declared in cpp file instead.

src/hotspot/cpu/x86/foreign_globals_x86.hpp line 30:

> 28: #include "utilities/growableArray.hpp"
> 29: 
> 30: #define __ _masm->

Same here (move to cpp file).

src/hotspot/share/opto/lcm.cpp line 867:

> 865:     case Op_CallNative:
> 866:       // FIXME compute actual save policy based on nep->abi
> 867:       save_policy = _matcher._c_reg_save_policy;

Please, elaborate here why it's OK for now to use ` _c_reg_save_policy`. And 
then turn `FIXME` into `TODO`. 
If possible, would be nice to introduce some asserts to back the claim.

src/hotspot/share/opto/machnode.cpp line 831:

> 829:   st->print("%s ",_name);
> 830:   st->print("_arg_regs: ");
> 831:   _arg_regs.print_on(st);

It doesn't print any useful info: `_arg_regs: 
AllocatedObj(0x000000011cf5cbe8)`. Please, improve it.

src/hotspot/share/opto/output.cpp line 3394:

> 3392:     }
> 3393: 
> 3394:     address* native_stubs = NULL;

IMO it's worth considering inlining `native_stubs` array into `nmethod` itself. 
That's the way how per-nmethod information is handled now (e.g., dependencies, 
debug info, exception handler table, implicit exception table).

src/hotspot/share/opto/callnode.cpp line 1184:

> 1182: void CallNativeNode::calling_convention( BasicType* sig_bt, VMRegPair 
> *parm_regs, uint argcnt ) const {
> 1183:   assert((tf()->domain()->cnt() - TypeFunc::Parms) == argcnt, "arg 
> counts must match!");
> 1184: #ifndef PRODUCT

Should be `#ifdef ASSERT` instead.

src/hotspot/share/opto/callnode.cpp line 1143:

> 1141:     case TypeFunc::Parms:
> 1142:     default: {
> 1143:       if(tf()->range()->field_at(proj->_con) == Type::HALF) {

That's `TypeFunc::Parms+1` case in `CallNode::match`. Why did you decide to 
move it to `default` case? 

Overall, it looks very similar to `CallNode::match`. Why not just customize 
`OptoRegPair regs` computation for `CallNative` there?

src/hotspot/share/opto/graphKit.cpp line 2665:

> 2663:     for (uint vm_ret_pos = 0; vm_ret_pos < n_returns; vm_ret_pos++) {
> 2664:       if (new_call_type->range()->field_at(TypeFunc::Parms + 
> vm_ret_pos)  == Type::HALF) {
> 2665:         // FIXME is this needed?

Why do you need the projection at all? Please, clarify and remove `FIXME` 
comment.

src/hotspot/share/opto/graphKit.cpp line 2675:

> 2673:     // Unpack native results if needed
> 2674:     // Need this method type since it's unerased
> 2675:     switch (nep->method_type()->rtype()->basic_type()) {

Are calls returning multiple values supported right now? (From what I'm seeing 
in other places, they are not supported.) If not, then you don't need a loop 
over return values and there are other places where it can simplify code.

src/hotspot/share/opto/type.hpp line 678:

> 676:   static const TypeTuple *make_range(ciSignature *sig);
> 677:   static const TypeTuple *make_domain(ciInstanceKlass* recv, ciSignature 
> *sig);
> 678:   static const TypeTuple *make_func(uint arg_cnt, const Type 
> **arg_fields);

I find `make_func` name misleading: it makes an impression you get `TypeFunc` 
out of it, but in reailty it just composes type array with 
`TypeTyple::make`+`TypeTyple::fields`.  I'd prefer to see it as 
`TypeTuple::fields` overload.

Or rewrite `GraphKit::make_native_call` to operate directly on 
`TypeTyple::fields` and get rid fo intermediate arrays.

src/hotspot/share/opto/output.cpp line 1144:

> 1142:     methodHandle null_mh;
> 1143:     bool rethrow_exception = false;
> 1144:     bool is_opt_native = mach->is_MachCallNative();

Please, move it to `MachCall`-related logic (where `is_method_handle_invoke` is 
set).

src/hotspot/cpu/x86/universalNativeInvoker_x86.cpp line 77:

> 75:     XMMRegister reg = _abi->_vector_argument_registers.at(i);
> 76:     size_t offs = _layout->arguments_vector + i * sizeof(VectorRegister);
> 77:     if (UseAVX >= 3) {

Assuming worst-case scenario (w.r.t. value size) is sub-optimal. Considering FP 
values are handled as vectors you end up operating on 32-bit/64-bit values as 
if they were 512-bit in size. And, in addition to wasted memory bandwidth, 
EVEX-encoded instructions may trigger CPU frequency scaling which will penalize 
cases when AVX512 is not used. So, it is worth considering annotating vector 
values with their actual sizes and taking the size into account when operating 
on vectors.

-------------

PR: https://git.openjdk.java.net/jdk/pull/634

Reply via email to