Skip to content

[pocl] Back @private Scratchpad with GPUCompiler.alloca#714

Merged
christiangnrd merged 5 commits into
mainfrom
vc/alloca_intrinsic
Jul 17, 2026
Merged

[pocl] Back @private Scratchpad with GPUCompiler.alloca#714
christiangnrd merged 5 commits into
mainfrom
vc/alloca_intrinsic

Conversation

@vchuravy

Copy link
Copy Markdown
Member

Summary

Replaces the POCL back-end's MArray-backed @private scratchpad with a direct per-workitem stack allocation via GPUCompiler.alloca. The returned Ptr is wrapped in a CLDeviceArray over OpenCL "Function" storage (LLVM addrspace 0), which is where the SPIR-V target places allocas.

@device_override @inline function KA.Scratchpad(ctx, ::Type{T}, ::Val{Dims}) where {T, Dims}
    ptr = POCL.GPUCompiler.alloca(T, Val(prod(Dims)))
    CLDeviceArray(Dims, reinterpret(POCL.LLVMPtr{T, POCL.AS.Function}, ptr))
end

This drops the StaticArrays dependency from the POCL back-end (StaticArrays is still used by the CPU back-end).

Why

GPUCompiler.alloca emits a real entry-block alloca that the optimizer can promote, in the target's alloca address space — avoiding the unsoundness of llvmcall + alloca and the overhead/semantics of MArray. See the motivation in the companion GPUCompiler PR.

Alignment

The alloca is aligned to Base.datatype_alignment(T), which is exactly the alignment CLDeviceArray uses for its element loads/stores (alignment(::CLDeviceArray{T})), so accesses are consistent. isbits-union element types are intentionally unsupported (GPUCompiler.alloca guards on isbitstype(T)).

Status

Draft — depends on JuliaGPU/GPUCompiler.jl#859 (adds the alloca intrinsic). Project.toml compat is bumped to GPUCompiler = "1.23"; this can be un-drafted once that is merged and released.

Testing

Verified end-to-end against the local GPUCompiler branch: @private Float32 (4,) lowers to alloca [16 x i8], align 4 in addrspace 0 with no surviving julia.gpu.alloca, and the kernel runs correctly on the POCL CPU device.

🤖 Generated with Claude Code

@codecov

codecov Bot commented Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 64.73%. Comparing base (e0e969a) to head (92135c8).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #714      +/-   ##
==========================================
- Coverage   64.76%   64.73%   -0.04%     
==========================================
  Files          23       23              
  Lines        2021     2022       +1     
==========================================
  Hits         1309     1309              
- Misses        712      713       +1     

☔ View full report in Codecov by Harness.
📢 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.

@vchuravy vchuravy added this to the 0.10.0 milestone Jul 4, 2026
vchuravy and others added 2 commits July 15, 2026 12:02
Back the POCL `Scratchpad` (`@private`) with `GPUCompiler.alloca`, a direct
per-workitem stack allocation, instead of a StaticArrays `MArray`. The returned
`Ptr` is wrapped in a `CLDeviceArray` over OpenCL "Function" storage (LLVM
addrspace 0), where the SPIR-V target places allocas. Its alignment
(`Base.datatype_alignment(T)`) matches `CLDeviceArray`'s element accesses.

Requires GPUCompiler 1.23 (JuliaGPU/GPUCompiler.jl#859), which adds the
`alloca` intrinsic. Drops the now-unused StaticArrays import from the POCL
back-end (StaticArrays is still used by the CPU back-end).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Benchmark Results

Show table
main 92135c8... main / 92135c8...
saxpy/default/Float32/1024 0.0343 ± 0.027 ms 0.0342 ± 0.027 ms 1 ± 1.1
saxpy/default/Float32/1048576 0.411 ± 0.047 ms 0.424 ± 0.062 ms 0.969 ± 0.18
saxpy/default/Float32/16384 0.0591 ± 0.028 ms 0.0564 ± 0.026 ms 1.05 ± 0.69
saxpy/default/Float32/2048 0.0472 ± 0.029 ms 0.043 ± 0.029 ms 1.1 ± 0.99
saxpy/default/Float32/256 31.3 ± 26 μs 30.6 ± 25 μs 1.02 ± 1.2
saxpy/default/Float32/262144 0.125 ± 0.018 ms 0.122 ± 0.018 ms 1.02 ± 0.21
saxpy/default/Float32/32768 0.0636 ± 0.029 ms 0.0637 ± 0.028 ms 0.998 ± 0.64
saxpy/default/Float32/4096 0.0594 ± 0.023 ms 0.0521 ± 0.029 ms 1.14 ± 0.77
saxpy/default/Float32/512 0.0335 ± 0.026 ms 31.5 ± 25 μs 1.07 ± 1.2
saxpy/default/Float32/64 30.8 ± 25 μs 29.8 ± 22 μs 1.03 ± 1.1
saxpy/default/Float32/65536 0.0732 ± 0.029 ms 0.0683 ± 0.029 ms 1.07 ± 0.63
saxpy/default/Float64/1024 0.0495 ± 0.027 ms 0.0348 ± 0.027 ms 1.42 ± 1.4
saxpy/default/Float64/1048576 0.764 ± 0.072 ms 0.79 ± 0.084 ms 0.967 ± 0.14
saxpy/default/Float64/16384 0.0586 ± 0.029 ms 0.0556 ± 0.027 ms 1.05 ± 0.72
saxpy/default/Float64/2048 0.0507 ± 0.029 ms 0.0428 ± 0.03 ms 1.18 ± 1.1
saxpy/default/Float64/256 0.0321 ± 0.026 ms 29.3 ± 20 μs 1.09 ± 1.1
saxpy/default/Float64/262144 0.21 ± 0.027 ms 0.227 ± 0.033 ms 0.925 ± 0.18
saxpy/default/Float64/32768 0.0658 ± 0.031 ms 0.0642 ± 0.03 ms 1.02 ± 0.68
saxpy/default/Float64/4096 0.0573 ± 0.029 ms 0.0511 ± 0.03 ms 1.12 ± 0.86
saxpy/default/Float64/512 0.0327 ± 0.024 ms 30.1 ± 24 μs 1.09 ± 1.2
saxpy/default/Float64/64 30.7 ± 22 μs 29.4 ± 15 μs 1.05 ± 0.92
saxpy/default/Float64/65536 0.0837 ± 0.03 ms 0.0795 ± 0.029 ms 1.05 ± 0.54
saxpy/static workgroup=(1024,)/Float32/1024 0.0367 ± 0.024 ms 0.0346 ± 0.024 ms 1.06 ± 1
saxpy/static workgroup=(1024,)/Float32/1048576 0.476 ± 0.041 ms 0.505 ± 0.048 ms 0.943 ± 0.12
saxpy/static workgroup=(1024,)/Float32/16384 0.062 ± 0.026 ms 0.0597 ± 0.027 ms 1.04 ± 0.65
saxpy/static workgroup=(1024,)/Float32/2048 0.0546 ± 0.021 ms 0.045 ± 0.028 ms 1.21 ± 0.88
saxpy/static workgroup=(1024,)/Float32/256 0.0376 ± 0.024 ms 0.0356 ± 0.022 ms 1.06 ± 0.94
saxpy/static workgroup=(1024,)/Float32/262144 0.159 ± 0.014 ms 0.157 ± 0.015 ms 1.01 ± 0.13
saxpy/static workgroup=(1024,)/Float32/32768 0.0674 ± 0.027 ms 0.0646 ± 0.027 ms 1.04 ± 0.61
saxpy/static workgroup=(1024,)/Float32/4096 0.0619 ± 0.024 ms 0.0505 ± 0.027 ms 1.23 ± 0.8
saxpy/static workgroup=(1024,)/Float32/512 0.0393 ± 0.023 ms 0.0345 ± 0.021 ms 1.14 ± 0.97
saxpy/static workgroup=(1024,)/Float32/64 0.0375 ± 0.023 ms 0.0364 ± 0.023 ms 1.03 ± 0.92
saxpy/static workgroup=(1024,)/Float32/65536 0.0805 ± 0.026 ms 0.0758 ± 0.025 ms 1.06 ± 0.48
saxpy/static workgroup=(1024,)/Float64/1024 0.0491 ± 0.024 ms 0.0474 ± 0.024 ms 1.04 ± 0.74
saxpy/static workgroup=(1024,)/Float64/1048576 0.818 ± 0.089 ms 0.862 ± 0.1 ms 0.949 ± 0.15
saxpy/static workgroup=(1024,)/Float64/16384 0.0594 ± 0.027 ms 0.0575 ± 0.024 ms 1.03 ± 0.64
saxpy/static workgroup=(1024,)/Float64/2048 0.0489 ± 0.028 ms 0.0433 ± 0.027 ms 1.13 ± 0.96
saxpy/static workgroup=(1024,)/Float64/256 0.039 ± 0.023 ms 0.038 ± 0.023 ms 1.03 ± 0.87
saxpy/static workgroup=(1024,)/Float64/262144 0.223 ± 0.026 ms 0.251 ± 0.032 ms 0.887 ± 0.15
saxpy/static workgroup=(1024,)/Float64/32768 0.0701 ± 0.028 ms 0.0665 ± 0.026 ms 1.05 ± 0.59
saxpy/static workgroup=(1024,)/Float64/4096 0.0571 ± 0.025 ms 0.0573 ± 0.025 ms 0.996 ± 0.62
saxpy/static workgroup=(1024,)/Float64/512 0.0392 ± 0.024 ms 0.0342 ± 0.02 ms 1.15 ± 0.97
saxpy/static workgroup=(1024,)/Float64/64 0.0375 ± 0.024 ms 0.0386 ± 0.023 ms 0.972 ± 0.84
saxpy/static workgroup=(1024,)/Float64/65536 0.0886 ± 0.024 ms 0.0815 ± 0.022 ms 1.09 ± 0.42
time_to_load 1.15 ± 0.013 s 0.95 ± 0.013 s 1.21 ± 0.021

Benchmark Plots

A plot of the benchmark results have been uploaded as an artifact to the workflow run for this PR.
Go to "Actions"->"Benchmark a pull request"->[the most recent run]->"Artifacts" (at the bottom).

@christiangnrd
christiangnrd marked this pull request as ready for review July 15, 2026 15:59
@christiangnrd

christiangnrd commented Jul 15, 2026

Copy link
Copy Markdown
Member

Julia 1.11/1.13 on linux arm test failures unrelated

@christiangnrd
christiangnrd merged commit c01bd4a into main Jul 17, 2026
90 of 95 checks passed
@christiangnrd
christiangnrd deleted the vc/alloca_intrinsic branch July 17, 2026 21:37
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.

2 participants