Analysis of Project Babylon's Code Reflection API and HAT (Heterogeneous Accelerated Toolkit).
Project Babylon is an OpenJDK initiative to enable:
- Code Reflection - Runtime introspection of Java code structure
- HAT (Heterogeneous Accelerated Toolkit) - GPU programming from Java
- Code Models - Abstract representation of code for transformation
Status: Early access builds available at https://jdk.java.net/babylon/
// Traditional Java - no code introspection
void foo(int x) {
int y = x * 2;
return y;
}
// With Code Reflection - can introspect structure
CodeReflection codeModel = method.getCodeModel();
// Can analyze: variables, operations, control flow, etc.Use cases:
- GPU code generation - Translate Java to GPU kernels
- DSL compilation - Domain-specific optimizations
- Static analysis - Deep code understanding
- Custom transformations - Rewrite code at runtime
Module: jdk.incubator.code
Availability: Custom Babylon JDK builds only
API Status: Experimental, subject to change
Documentation: Limited, evolving rapidly
Java Code (with HAT annotations)
↓
Code Reflection API (introspect structure)
↓
Backend (OpenCL, CUDA, SPIR-V)
↓
GPU Execution
@CodeReflection
static void matmul(float[] a, float[] b, float[] c, int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
float sum = 0;
for (int k = 0; k < size; k++) {
sum += a[i * size + k] * b[k * size + j];
}
c[i * size + j] = sum;
}
}
}
// HAT translates this to GPU kernel automaticallyBenefits:
- Write once in Java
- Runs on CPU or GPU
- No manual kernel writing
- Type-safe
// 1. Write Java code
void matmul(...) { /* CPU code */ }
// 2. Write separate CUDA kernel
__global__ void matmul_kernel(...) { /* GPU code */ }
// 3. Bind with JNI
// 4. Maintain two codebasesProblems:
- Two implementations to maintain
- No type safety between Java and GPU code
- Complex build (nvcc, JNI, etc.)
// Single Java implementation
@CodeReflection
void matmul(...) { /* Java code */ }
// HAT generates GPU kernel automaticallyBenefits:
- Single codebase
- Type-safe
- No manual GPU programming
- Automatic optimization
| Approach | Code Location | GPU Access | Type Safety | Maturity |
|---|---|---|---|---|
| Babylon/HAT | Java (annotated) | Via code reflection | ✅ Full | 🔬 Experimental |
| TornadoVM | Java (@Parallel) | Via Truffle | ✅ Full | |
| JCuda | Java + CUDA | Direct CUDA API | ✅ Stable | |
| Aparapi | Java (annotated) | OpenCL | ❌ Deprecated |
Current limitations:
- Requires custom JDK - Not available via SDKMAN
- No prebuilt binaries - Must compile from source
- Rapidly changing - API not stable
- Limited documentation - Experimental status
This demo only:
- Checks if Code Reflection module exists
- Shows JDK version information
- Doesn't demonstrate actual HAT usage (requires Babylon JDK)
For full HAT demos:
- See
docs/Babylon workflow.md - Requires cloning Babylon repository
- Must build Babylon JDK from source
Both enable GPU programming from Java, but different approaches:
- Approach: Code Reflection → GPU kernel generation
- Backend: OpenCL, CUDA, SPIR-V (pluggable)
- Status: Experimental (OpenJDK research project)
- API:
@CodeReflectionannotation - Control: High (direct kernel control)
- Approach: Truffle JIT compilation
- Backend: OpenCL, PTX, SPIR-V
- Status: Beta (production-ready v2.2.0)
- API:
@Parallel, TaskGraph - Control: Medium (high-level abstractions)
When to choose:
- Babylon: Research, cutting-edge features, direct control
- TornadoVM: Production use, stability, ease of use
Requirements:
- autoconf, make, gcc
- ~30 GB disk space
- ~1-2 hours build time
Process:
git clone --branch code-reflection https://github.com/openjdk/babylon.git
cd babylon
bash configure
make imagesWhy it's hard:
- Build complexity (C++ toolchain)
- Large codebase
- No prebuilt binaries
- Moving target (frequent changes)
Expected timeline:
- 2026-2027: Continued experimentation
- JDK 27-28: Possible first preview
- Beyond: Integration into mainline JDK
Key question: Will Code Reflection become standard?
Possibilities:
- ✅ Integrated - Becomes part of Java SE
⚠️ Separate - Remains optional module- ❌ Abandoned - Research doesn't pan out
- Babylon is research-grade, not production
- API changes frequently
- Limited documentation
- Worth watching, not depending on yet
Multiple projects trying different approaches:
- Babylon (code reflection)
- TornadoVM (Truffle)
- JCuda (direct API bindings)
No clear winner yet.
- Babylon: Hard to install, experimental
- TornadoVM: Easy to use, but limited
- JCuda: Mature, but NVIDIA-only
- docs/Babylon workflow.md - HAT examples
demos/tornadovm/- Alternative GPU approachdemos/jcuda/- Direct CUDA bindingsdemos/valhalla/- Related OpenJDK research