Skip to content

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

Open
vchuravy wants to merge 4 commits into
mainfrom
vc/alloca_intrinsic
Open

[pocl] Back @private Scratchpad with GPUCompiler.alloca#714
vchuravy wants to merge 4 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

❌ Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 0.00%. Comparing base (a8022b2) to head (0b58f9b).

Files with missing lines Patch % Lines
src/pocl/backend.jl 0.00% 2 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (a8022b2) and HEAD (0b58f9b). Click for more details.

HEAD has 28 uploads less than BASE
Flag BASE (a8022b2) HEAD (0b58f9b)
48 20
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #714       +/-   ##
==========================================
- Coverage   62.51%   0.00%   -62.52%     
==========================================
  Files          23      22        -1     
  Lines        1926    1737      -189     
==========================================
- Hits         1204       0     -1204     
- Misses        722    1737     +1015     

☔ 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 c342d87... main / c342d87...
saxpy/default/Float32/1024 0.085 ± 0.01 ms 0.0854 ± 0.013 ms 0.996 ± 0.19
saxpy/default/Float32/1048576 0.505 ± 0.025 ms 0.502 ± 0.025 ms 1.01 ± 0.07
saxpy/default/Float32/16384 0.0673 ± 0.034 ms 0.0665 ± 0.032 ms 1.01 ± 0.71
saxpy/default/Float32/2048 0.0827 ± 0.028 ms 0.085 ± 0.027 ms 0.973 ± 0.45
saxpy/default/Float32/256 0.0853 ± 0.011 ms 0.0862 ± 0.012 ms 0.99 ± 0.19
saxpy/default/Float32/262144 0.179 ± 0.031 ms 0.179 ± 0.031 ms 1 ± 0.24
saxpy/default/Float32/32768 0.0732 ± 0.031 ms 0.073 ± 0.03 ms 1 ± 0.59
saxpy/default/Float32/4096 0.0808 ± 0.031 ms 0.0854 ± 0.031 ms 0.946 ± 0.5
saxpy/default/Float32/512 0.0847 ± 0.01 ms 0.0859 ± 0.0099 ms 0.986 ± 0.16
saxpy/default/Float32/64 0.0853 ± 0.013 ms 0.0856 ± 0.017 ms 0.996 ± 0.25
saxpy/default/Float32/65536 0.0893 ± 0.031 ms 0.0889 ± 0.031 ms 1 ± 0.5
saxpy/default/Float64/1024 0.0849 ± 0.013 ms 0.0862 ± 0.015 ms 0.985 ± 0.23
saxpy/default/Float64/1048576 0.577 ± 0.086 ms 0.515 ± 0.059 ms 1.12 ± 0.21
saxpy/default/Float64/16384 0.0672 ± 0.03 ms 0.0681 ± 0.029 ms 0.988 ± 0.61
saxpy/default/Float64/2048 0.0821 ± 0.031 ms 0.0858 ± 0.028 ms 0.957 ± 0.48
saxpy/default/Float64/256 0.0854 ± 0.011 ms 0.0873 ± 0.011 ms 0.978 ± 0.18
saxpy/default/Float64/262144 0.195 ± 0.039 ms 0.182 ± 0.033 ms 1.07 ± 0.29
saxpy/default/Float64/32768 0.0788 ± 0.03 ms 0.0779 ± 0.03 ms 1.01 ± 0.55
saxpy/default/Float64/4096 0.0801 ± 0.03 ms 0.079 ± 0.031 ms 1.01 ± 0.55
saxpy/default/Float64/512 0.0849 ± 0.011 ms 0.0863 ± 0.012 ms 0.984 ± 0.18
saxpy/default/Float64/64 0.0854 ± 0.01 ms 0.0866 ± 0.011 ms 0.986 ± 0.17
saxpy/default/Float64/65536 0.101 ± 0.031 ms 0.0964 ± 0.031 ms 1.05 ± 0.47
saxpy/static workgroup=(1024,)/Float32/1024 0.0824 ± 0.01 ms 0.0837 ± 0.013 ms 0.984 ± 0.2
saxpy/static workgroup=(1024,)/Float32/1048576 0.432 ± 0.027 ms 0.428 ± 0.031 ms 1.01 ± 0.098
saxpy/static workgroup=(1024,)/Float32/16384 0.064 ± 0.032 ms 0.0634 ± 0.031 ms 1.01 ± 0.71
saxpy/static workgroup=(1024,)/Float32/2048 0.0805 ± 0.028 ms 0.0824 ± 0.028 ms 0.977 ± 0.47
saxpy/static workgroup=(1024,)/Float32/256 0.0826 ± 0.014 ms 0.0839 ± 0.015 ms 0.986 ± 0.24
saxpy/static workgroup=(1024,)/Float32/262144 0.158 ± 0.032 ms 0.153 ± 0.031 ms 1.03 ± 0.3
saxpy/static workgroup=(1024,)/Float32/32768 0.0675 ± 0.028 ms 0.0685 ± 0.028 ms 0.985 ± 0.57
saxpy/static workgroup=(1024,)/Float32/4096 0.0797 ± 0.03 ms 0.0839 ± 0.03 ms 0.949 ± 0.49
saxpy/static workgroup=(1024,)/Float32/512 0.0825 ± 0.012 ms 0.0838 ± 0.013 ms 0.984 ± 0.21
saxpy/static workgroup=(1024,)/Float32/64 0.0826 ± 0.019 ms 0.0831 ± 0.019 ms 0.994 ± 0.32
saxpy/static workgroup=(1024,)/Float32/65536 0.0808 ± 0.031 ms 0.082 ± 0.031 ms 0.986 ± 0.53
saxpy/static workgroup=(1024,)/Float64/1024 0.0822 ± 0.022 ms 0.0832 ± 0.022 ms 0.988 ± 0.37
saxpy/static workgroup=(1024,)/Float64/1048576 0.54 ± 0.095 ms 0.493 ± 0.077 ms 1.09 ± 0.26
saxpy/static workgroup=(1024,)/Float64/16384 0.0654 ± 0.029 ms 0.0648 ± 0.027 ms 1.01 ± 0.62
saxpy/static workgroup=(1024,)/Float64/2048 0.0801 ± 0.031 ms 0.0829 ± 0.029 ms 0.967 ± 0.51
saxpy/static workgroup=(1024,)/Float64/256 0.0825 ± 0.018 ms 0.0835 ± 0.013 ms 0.989 ± 0.26
saxpy/static workgroup=(1024,)/Float64/262144 0.191 ± 0.037 ms 0.179 ± 0.036 ms 1.06 ± 0.3
saxpy/static workgroup=(1024,)/Float64/32768 0.075 ± 0.027 ms 0.0738 ± 0.026 ms 1.02 ± 0.52
saxpy/static workgroup=(1024,)/Float64/4096 0.0775 ± 0.029 ms 0.0729 ± 0.031 ms 1.06 ± 0.61
saxpy/static workgroup=(1024,)/Float64/512 0.0825 ± 0.014 ms 0.0839 ± 0.0097 ms 0.983 ± 0.2
saxpy/static workgroup=(1024,)/Float64/64 0.0829 ± 0.015 ms 0.0837 ± 0.015 ms 0.99 ± 0.25
saxpy/static workgroup=(1024,)/Float64/65536 0.0935 ± 0.028 ms 0.0923 ± 0.027 ms 1.01 ± 0.43
time_to_load 1.08 ± 0.042 s 0.89 ± 0.0083 s 1.22 ± 0.049

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

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