jextract woes

Michael Zucchi notzed at gmail.com
Thu Jan 30 05:07:27 UTC 2020


On 29/1/20 9:16 pm, Maurizio Cimadamore wrote:
>
>
> On 29/01/2020 03:24, Michael Zucchi wrote:
>> i.e. how do i solve the problem i've described in a way that isn't 
>> just the same as simply doing this (assuming such a variation of 
>> downcallHandle()  call existed):
>
> Describe 'context' - what is 'context' ? You could make that part of 
> the "recipe" too, and pass it as a _dynamic_ argument to an invokedynamic.
>

For opencl is it the platform, or specifically cl_platform_id. There may 
be 0 or more available platforms on any given operating system.

I showed you an example of what I need, but this is what i am actually 
now using:

public class GLext {
         public final  MethodHandle clCreateEventFromGLsyncKHR;
         public final  MethodHandle clCreateFromGLBuffer;
...

         public GLext(Function<String,MemoryAddress> ctx) {
                 clCreateEventFromGLsyncKHR = Native.downcallHandle(ctx, 
"clCreateEventFromGLsyncKHR", 
"(u64:${_cl_context}u64:${__GLsync}u64:i32)u64:${_cl_event}");
                 clCreateFromGLBuffer = Native.downcallHandle(ctx, 
"clCreateFromGLBuffer", "(u64:${_cl_context}u64u32u64:i32)u64:${_cl_mem}");
...

         }

         public  MemoryAddress clCreateEventFromGLsyncKHR(MemoryAddress 
arg_0, MemoryAddress arg_1, MemoryAddress arg_2) throws Throwable {
                 return 
(MemoryAddress)clCreateEventFromGLsyncKHR.invokeExact(arg_0, arg_1, arg_2);
         }

         public  MemoryAddress clCreateFromGLBuffer(MemoryAddress arg_0, 
long arg_1, int arg_2, MemoryAddress arg_3) throws Throwable {
                 return 
(MemoryAddress)clCreateFromGLBuffer.invokeExact(arg_0, arg_1, arg_2, arg_3);
         }
...
}

This is created on-demand but only once per platform "wrapper" using:

  new GLext(platform::clGetPlatformExtensionAddress);

For gl_ext it's about 10 functions, but there are more in the rest of 
the api.

For vulkan it's about 350 functions (only 4 are linkable), and there are 
two context types (and >=0 context instances for each type).

https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetInstanceProcAddr.html
https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetDeviceProcAddr.html

> An invokedynamic instruction has some _static_ arguments, which are 
> fixed, for every call. And some _dynamic_ arguments. For instance, the 
> javac compiler uses invokedynamic instructions to compile lambda 
> expressions. The invokedynamic that javac generates has a static part:
>
> * what is the type of the lambda
> * what is the method that implements it (a method handle)
> * what is the functional interface the lambda should be converted to
>
> But there's also a _dynamic_ part:
>
> * additional variables the lambda captures from the enclosing scope
>
> Do, if you have something like:
>
> for (int i = 0 ; i < 100 ; i++) {
>    Runnable r = () -> System.out.println(i);
>    r.run();
> }
>
> The lambda creation will be turned into an invokedynamic instruction 
> whose static arguments point to Runnable, and an implementation method 
> which does the
>
Well your example wouldn't compile but it's clear what you mean.
>
> Ok, enough talking about lambdas... what I'm trying to say here is 
> that what you need is a _factory_ of native method handles; I guess 
> this factory will have some static parts (which will not change across 
> calls) and some dynamic parts (e.g. the parts you say depend on the 
> context). Invokedynamic is, albeit low level, perhaps the most 
> efficient way to map these kind of idioms in the Java bytecode in a 
> way that the VM can optimize.
>

I guess from jorn's email you're talking about creating a an 
implementation of an interface through which the calls operate.

I think that's frankly beyond the scope of what should be required to 
use panama.

> Or maybe it's overkill, and if your context is 'simple' enough, you 
> can just use a map<Context, MethodHandle> :-)
>

If jextract can't do the functional equivalent of the above i'll just 
have to use the code I have already.




More information about the panama-dev mailing list