Skip to content

Commit c0bf825

Browse files
authored
Merge pull request #6 from Hellblazer/docs/build-options-improvements
Improve documentation and error messages for build options support
2 parents 7ed9fd3 + c38fb37 commit c0bf825

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

resource/src/main/java/com/hellblazer/luciferase/resource/compute/ComputeKernel.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,14 @@ public interface ComputeKernel extends AutoCloseable {
5454
* <li>Architecture-specific tuning: {@code "-D__GCN_REV__=2"}</li>
5555
* </ul>
5656
*
57+
* <p><b>Security Note:</b> Build options are passed directly to the backend compiler without
58+
* sanitization. Ensure options originate from trusted sources only to prevent compiler-based
59+
* denial-of-service or unexpected behavior.
60+
*
5761
* @param source Kernel source code (Metal or OpenCL)
5862
* @param entryPoint Kernel entry point function name
59-
* @param buildOptions Compiler flags and preprocessor defines (null or empty for defaults)
63+
* @param buildOptions Compiler flags and preprocessor defines (null or empty for defaults).
64+
* Passed directly to the backend compiler without validation.
6065
* @throws KernelCompilationException if compilation fails
6166
* @see #recompile(String, String, String)
6267
*/
@@ -69,8 +74,7 @@ default void compile(String source, String entryPoint, String buildOptions)
6974
* Recompile an already-compiled kernel with different build options.
7075
*
7176
* <p>Enables runtime GPU auto-tuning by recompiling kernels with different optimization
72-
* parameters without clearing existing kernel state. Useful for performance experiments
73-
* and adaptive optimization strategies.
77+
* parameters. Useful for performance experiments and adaptive optimization strategies.
7478
*
7579
* <h3>Recompilation Workflow:</h3>
7680
* <pre>{@code
@@ -83,8 +87,13 @@ default void compile(String source, String entryPoint, String buildOptions)
8387
* kernel.execute(globalSize); // Compare performance
8488
* }</pre>
8589
*
86-
* <p><b>Note:</b> Recompilation creates a fresh kernel. The old kernel reference remains
87-
* valid until explicitly closed, allowing multiple kernel variants to coexist.
90+
* <p><b>Note:</b> Recompilation releases the old kernel and program resources, then compiles
91+
* a fresh kernel. The kernel object itself remains valid and usable after recompilation.
92+
*
93+
* <p><b>Thread Safety:</b> During recompilation, {@link #isCompiled()} may briefly return false
94+
* as resources are released and replaced. Concurrent kernel execution from other threads during
95+
* recompilation will fail with IllegalStateException. Callers must ensure exclusive access to
96+
* the kernel object during recompilation.
8897
*
8998
* @param source Kernel source code (must match original source for consistency)
9099
* @param entryPoint Kernel entry point function name

resource/src/main/java/com/hellblazer/luciferase/resource/compute/opencl/OpenCLKernel.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ public void compile(String source, String entryPoint) throws KernelCompilationEx
8989
public void compile(String source, String entryPoint, String buildOptions) throws KernelCompilationException {
9090
checkNotClosed();
9191
if (compiled.get()) {
92-
throw new KernelCompilationException("Kernel already compiled");
92+
throw new KernelCompilationException(
93+
"Kernel already compiled. Use recompile() to recompile with different build options.");
9394
}
9495

9596
compileInternal(source, entryPoint, buildOptions);
@@ -150,12 +151,8 @@ private void compileInternal(String source, String entryPoint, String buildOptio
150151
checkCLError(errcode.get(0), "Failed to create OpenCL kernel: " + entryPoint);
151152

152153
compiled.set(true);
153-
if (buildOptions != null && !buildOptions.isEmpty()) {
154-
log.debug("Compiled OpenCL kernel: {} (entry point: {}, options: {})",
155-
name, entryPoint, buildOptions);
156-
} else {
157-
log.debug("Compiled OpenCL kernel: {} (entry point: {})", name, entryPoint);
158-
}
154+
log.debug("Compiled OpenCL kernel: {} (entry point: {}, options: {})",
155+
name, entryPoint, buildOptions != null ? buildOptions : "(none)");
159156

160157
} catch (Exception e) {
161158
cleanup();

resource/src/test/java/com/hellblazer/luciferase/resource/compute/ComputeKernelBuildOptionsTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ static void checkOpenCL() {
101101

102102
// --- Basic Build Options Tests ---
103103

104+
/**
105+
* Validates that preprocessor defines work with kernel compilation.
106+
* Tests basic -D option passing to the OpenCL compiler.
107+
*/
104108
@Test
105109
void testCompileWithDefine() {
106110
if (!openCLAvailable) return;
@@ -152,6 +156,10 @@ void testCompileWithoutFeatureFlag() {
152156

153157
// --- Compiler Flags Tests ---
154158

159+
/**
160+
* Validates that OpenCL compiler flags (e.g., -cl-fast-relaxed-math) work correctly.
161+
* These flags enable performance optimizations and are critical for GPU auto-tuning.
162+
*/
155163
@Test
156164
void testCompileWithCompilerFlags() {
157165
if (!openCLAvailable) return;
@@ -196,6 +204,11 @@ void testRecompileWithDifferentOptions() {
196204
}
197205
}
198206

207+
/**
208+
* Validates that recompilation changes the runtime behavior of the kernel.
209+
* First execution multiplies by 2, second by 5, demonstrating that the
210+
* new build options take effect. Critical for GPU auto-tuning workflows.
211+
*/
199212
@Test
200213
void testRecompileChangesDefineValue() throws Exception {
201214
if (!openCLAvailable) return;

0 commit comments

Comments
 (0)