Skip to content

Conversation

simeonschaub
Copy link
Member

Allows other backends to pass additional hidden arguments that can be accessed through intrinsics. Required for OpenCL device-side RNG support, where additional shared memory must be passed as arguments to the kernel.

Replaces #717

Copy link
Contributor

github-actions bot commented Sep 9, 2025

Your PR requires formatting changes to meet the project's style guidelines.
Please consider running Runic (git runic tb/kernel_state_reference) to apply these changes.

Click here to view the suggested changes.
diff --git a/src/irgen.jl b/src/irgen.jl
index 2d19961..d8ce883 100644
--- a/src/irgen.jl
+++ b/src/irgen.jl
@@ -922,8 +922,10 @@ function kernel_state_to_reference!(@nospecialize(job::CompilerJob), mod::LLVM.M
     end
 end
 
-function add_input_arguments!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
-                              entry::LLVM.Function, kernel_intrinsics::Dict)
+function add_input_arguments!(
+        @nospecialize(job::CompilerJob), mod::LLVM.Module,
+        entry::LLVM.Function, kernel_intrinsics::Dict
+    )
     entry_fn = LLVM.name(entry)
 
     # figure out which intrinsics are used and need to be added as arguments
@@ -976,7 +978,7 @@ function add_input_arguments!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
         for (arg, new_arg) in zip(parameters(f), parameters(new_f))
             LLVM.name!(new_arg, LLVM.name(arg))
         end
-        for (intr_fn, new_arg) in zip(used_intrinsics, parameters(new_f)[end-nargs+1:end])
+        for (intr_fn, new_arg) in zip(used_intrinsics, parameters(new_f)[(end - nargs + 1):end])
             LLVM.name!(new_arg, kernel_intrinsics[intr_fn].name)
         end
 
@@ -994,8 +996,10 @@ function add_input_arguments!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
         end
 
         value_map[f] = new_f
-        clone_into!(new_f, f; value_map,
-                    changes=LLVM.API.LLVMCloneFunctionChangeTypeLocalChangesOnly)
+        clone_into!(
+            new_f, f; value_map,
+            changes = LLVM.API.LLVMCloneFunctionChangeTypeLocalChangesOnly
+        )
 
         # we can't remove this function yet, as we might still need to rewrite any called,
         # but remove the IR already
@@ -1011,7 +1015,7 @@ function add_input_arguments!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
     # update other uses of the old function, modifying call sites to pass the arguments
     function rewrite_uses!(f, new_f)
         # update uses
-        @dispose builder=IRBuilder() begin
+        return @dispose builder = IRBuilder() begin
             for use in uses(f)
                 val = user(use)
                 if val isa LLVM.CallInst || val isa LLVM.InvokeInst || val isa LLVM.CallBrInst
@@ -1019,9 +1023,11 @@ function add_input_arguments!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
                     # forward the arguments
                     position!(builder, val)
                     new_val = if val isa LLVM.CallInst
-                        call!(builder, function_type(new_f), new_f,
-                              [arguments(val)..., parameters(callee_f)[end-nargs+1:end]...],
-                              operand_bundles(val))
+                        call!(
+                            builder, function_type(new_f), new_f,
+                            [arguments(val)..., parameters(callee_f)[(end - nargs + 1):end]...],
+                            operand_bundles(val)
+                        )
                     else
                         # TODO: invoke and callbr
                         error("Rewrite of $(typeof(val))-based calls is not implemented: $val")
@@ -1064,7 +1070,7 @@ function add_input_arguments!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
             val = user(use)
             callee_f = LLVM.parent(LLVM.parent(val))
             if val isa LLVM.CallInst || val isa LLVM.InvokeInst || val isa LLVM.CallBrInst
-                replace_uses!(val, parameters(callee_f)[end-nargs+i])
+                replace_uses!(val, parameters(callee_f)[end - nargs + i])
             else
                 error("Cannot rewrite unknown use of function: $val")
             end

simeonschaub added a commit to JuliaGPU/OpenCL.jl that referenced this pull request Sep 9, 2025
@simeonschaub simeonschaub requested a review from maleadt September 9, 2025 15:07
Copy link

codecov bot commented Sep 9, 2025

Codecov Report

❌ Patch coverage is 77.88462% with 23 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.73%. Comparing base (e9ad136) to head (26defb9).
⚠️ Report is 1 commits behind head on tb/kernel_state_reference.

Files with missing lines Patch % Lines
src/irgen.jl 77.66% 23 Missing ⚠️
Additional details and impacted files
@@                      Coverage Diff                      @@
##           tb/kernel_state_reference     #718      +/-   ##
=============================================================
- Coverage                      74.30%   72.73%   -1.57%     
=============================================================
  Files                             24       24              
  Lines                           3585     3584       -1     
=============================================================
- Hits                            2664     2607      -57     
- Misses                           921      977      +56     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@maleadt
Copy link
Member

maleadt commented Sep 11, 2025

custom_noarg_readnone_intr sounds a bit specific, and is probably not worth the effort to make reusable. It's only a handful lines of code that could probably even be replaced by a simple llvmcall.

Otherwise LGTM.

Allows other backends to pass additional hidden arguments that can be accessed through intrinsics. Required for OpenCL device-side RNG support, where additional shared memory must be passed as arguments to the kernel.

Replaces #717
@maleadt maleadt merged commit fe41395 into tb/kernel_state_reference Sep 12, 2025
19 of 22 checks passed
@maleadt maleadt deleted the sds/add_input_args branch September 12, 2025 07:00
simeonschaub added a commit that referenced this pull request Sep 30, 2025
Allows other backends to pass additional hidden arguments that can be accessed through intrinsics. Required for OpenCL device-side RNG support, where additional shared memory must be passed as arguments to the kernel.
maleadt pushed a commit that referenced this pull request Oct 6, 2025
Allows other backends to pass additional hidden arguments that can be accessed through intrinsics. Required for OpenCL device-side RNG support, where additional shared memory must be passed as arguments to the kernel.
maleadt added a commit that referenced this pull request Oct 6, 2025
* Convert the kernel state back to a reference when needed.

We're currently passing the kernel state object by value, disregarding the typical Julia calling convention, because there's known issues with `byval` lowering on NVPTX.

For compatibility with back-ends that do not support passing kernel arguments by actual values, provide a pass that's conceptually the inverse of `lower_byval`, instead rewriting the kernel state object to be passed by reference, and loading from it at the beginning of the kernel.

* `add_input_arguments!` for other backends (#718)

Allows other backends to pass additional hidden arguments that can be accessed through intrinsics. Required for OpenCL device-side RNG support, where additional shared memory must be passed as arguments to the kernel.

Co-authored-by: Simeon David Schaub <[email protected]>
simeonschaub added a commit to JuliaGPU/OpenCL.jl that referenced this pull request Oct 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants