[code-reflection] RFR: [fix][hat] Codegen fixed when passing the kernel context to multiple invoke functions

Juan Fumero duke at openjdk.org
Tue Aug 12 10:03:41 UTC 2025


This PR fixes the code generator when passing the `kernelContext` object to new invoke functions within the main compute kernel. 

Code like this:
```java 
@CodeReflection
public static float compute(@RO KernelContext kc, @RO F32Array matrixA, @RO F32Array matrixB, int size, int j) {
    float acc = 0;
    for (int k = 0; k < size; k++) {
        acc += (matrixA.array(kc.x * size + k) * matrixB.array(k * size + j));
    }
    return acc;
}

@CodeReflection
public static void matrixMultiplyKernel1D(@RO KernelContext kc, @RO F32Array matrixA, @RO F32Array matrixB, @RW F32Array matrixC, int size) {
    if (kc.x < kc.maxX) {
        for (int j = 0; j < size; j++) {
            float acc = compute(kc, matrixA, matrixB, size, j);
            matrixC.array(kc.x * size + j, acc);
        }
    }
}


Generated the following error when compiling the kernel:


UNSUPPORTED (log once): buildComputeProgram: cl2Metal failed
buildStatus =failed
logLen = 1993 log  = program_source:33:5: error: unknown type name 'hat'
    hat.KernelContext kc, __global F32Array_t* matrixA, __global F32Array_t* matrixB, int size, int j
    ^


This is due to the wrong naming from the `kernelContext` when it gets compiled to OpenCL or CUDA.
This PR patches this by checking the argument type and replacing with the appropriate CUDA/OpenCL struct. 

### How to check?


HAT=INFO,SHOW_CODE java @hat/run ffi-opencl matmul 1DFC


This patch has been tested for both the OpenCL and the CUDA HAT Backends.

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

Commit messages:
 - Add todo for checking param types in HAT
 - [fix][hat] Codegen fixed when passing the kernel context to multple inner function calls

Changes: https://git.openjdk.org/babylon/pull/519/files
  Webrev: https://webrevs.openjdk.org/?repo=babylon&pr=519&range=00
  Stats: 50 lines in 2 files changed: 46 ins; 0 del; 4 mod
  Patch: https://git.openjdk.org/babylon/pull/519.diff
  Fetch: git fetch https://git.openjdk.org/babylon.git pull/519/head:pull/519

PR: https://git.openjdk.org/babylon/pull/519


More information about the babylon-dev mailing list