From 3325e3ec2c671c21b0455fbf2bbc5c0403810b56 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Tue, 17 Jun 2025 12:10:43 +0200 Subject: [PATCH 1/5] Use stacked method tables --- Project.toml | 4 ++++ src/pocl/compiler/compilation.jl | 4 ++-- src/pocl/pocl.jl | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 51859d2e..1f58cc55 100644 --- a/Project.toml +++ b/Project.toml @@ -53,3 +53,7 @@ pocl_jll = "7" EnzymeCore = "f151be2c-9106-41f4-ab19-57ee4f262869" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" + +[sources] +GPUCompiler = {url="https://github.com/JuliaGPU/GPUCompiler.jl", rev="vc/mtv"} +SPIRVIntrinsics = {url="https://github/JuliaGPU/OpenCL.jl", rev="vc/mtv", subdir="lib/intrinsics"} \ No newline at end of file diff --git a/src/pocl/compiler/compilation.jl b/src/pocl/compiler/compilation.jl index 5f88fba3..7e4ef5c1 100644 --- a/src/pocl/compiler/compilation.jl +++ b/src/pocl/compiler/compilation.jl @@ -6,7 +6,7 @@ const OpenCLCompilerJob = CompilerJob{SPIRVCompilerTarget, OpenCLCompilerParams} GPUCompiler.runtime_module(::CompilerJob{<:Any, OpenCLCompilerParams}) = POCL -GPUCompiler.method_table(::OpenCLCompilerJob) = method_table +GPUCompiler.method_table_view(job::OpenCLCompilerJob) = GPUCompiler.StackedMethodTable(job.world, method_table, spirv_method_table) # filter out OpenCL built-ins # TODO: eagerly lower these using the translator API @@ -50,7 +50,7 @@ end # create GPUCompiler objects - target = SPIRVCompilerTarget(; supports_fp16, supports_fp64, version = v"1.2", kwargs...) + target = SPIRVCompilerTarget(; supports_fp16, supports_fp64, validate = true, kwargs...) params = OpenCLCompilerParams() return CompilerConfig(target, params; kernel, name, always_inline) end diff --git a/src/pocl/pocl.jl b/src/pocl/pocl.jl index b16114d5..501e8b28 100644 --- a/src/pocl/pocl.jl +++ b/src/pocl/pocl.jl @@ -44,10 +44,42 @@ using GPUCompiler import LLVM using Adapt +## device overrides + +# local method table for device functions +Base.Experimental.@MethodTable(method_table) + +macro device_override(ex) + return esc( + quote + Base.Experimental.@overlay($method_table, $ex) + end + ) +end + +macro device_function(ex) + ex = macroexpand(__module__, ex) + def = ExprTools.splitdef(ex) + + # generate a function that errors + def[:body] = quote + error("This function is not intended for use on the CPU") + end + + return esc( + quote + $(ExprTools.combinedef(def)) + @device_override $ex + end + ) +end + import SPIRVIntrinsics SPIRVIntrinsics.@import_all SPIRVIntrinsics.@reexport_public +const spirv_method_table = SPIRVIntrinsics.method_table + include("compiler/compilation.jl") include("compiler/execution.jl") include("compiler/reflection.jl") From 9c6d43159612880acd24b00d0058691c24f6a04b Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Tue, 17 Jun 2025 12:11:37 +0200 Subject: [PATCH 2/5] fixup! Use stacked method tables --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 1f58cc55..601641bd 100644 --- a/Project.toml +++ b/Project.toml @@ -56,4 +56,4 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" [sources] GPUCompiler = {url="https://github.com/JuliaGPU/GPUCompiler.jl", rev="vc/mtv"} -SPIRVIntrinsics = {url="https://github/JuliaGPU/OpenCL.jl", rev="vc/mtv", subdir="lib/intrinsics"} \ No newline at end of file +SPIRVIntrinsics = {url="https://github.com/JuliaGPU/OpenCL.jl", rev="vc/mtv", subdir="lib/intrinsics"} \ No newline at end of file From ce4cac823e06db1b6d7e498a077ec1aae6dd1bbb Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Fri, 11 Jul 2025 10:16:49 +0200 Subject: [PATCH 3/5] cleanup --- Project.toml | 8 ++------ src/pocl/compiler/compilation.jl | 2 +- src/pocl/pocl.jl | 33 ++------------------------------ 3 files changed, 5 insertions(+), 38 deletions(-) diff --git a/Project.toml b/Project.toml index 601641bd..7b97ad34 100644 --- a/Project.toml +++ b/Project.toml @@ -34,13 +34,13 @@ SparseArraysExt = "SparseArrays" Adapt = "0.4, 1.0, 2.0, 3.0, 4" Atomix = "0.1, 1" EnzymeCore = "0.7, 0.8.1" -GPUCompiler = "1.2" +GPUCompiler = "1.6" InteractiveUtils = "1.6" LLVM = "9" LinearAlgebra = "1.6" MacroTools = "0.5" PrecompileTools = "1" -SPIRVIntrinsics = "0.3" +SPIRVIntrinsics = "0.4" SPIRV_LLVM_Backend_jll = "20" SPIRV_Tools_jll = "2024.4, 2025.1" SparseArrays = "<0.0.1, 1.6" @@ -53,7 +53,3 @@ pocl_jll = "7" EnzymeCore = "f151be2c-9106-41f4-ab19-57ee4f262869" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" - -[sources] -GPUCompiler = {url="https://github.com/JuliaGPU/GPUCompiler.jl", rev="vc/mtv"} -SPIRVIntrinsics = {url="https://github.com/JuliaGPU/OpenCL.jl", rev="vc/mtv", subdir="lib/intrinsics"} \ No newline at end of file diff --git a/src/pocl/compiler/compilation.jl b/src/pocl/compiler/compilation.jl index 7e4ef5c1..4b4a4fb4 100644 --- a/src/pocl/compiler/compilation.jl +++ b/src/pocl/compiler/compilation.jl @@ -6,7 +6,7 @@ const OpenCLCompilerJob = CompilerJob{SPIRVCompilerTarget, OpenCLCompilerParams} GPUCompiler.runtime_module(::CompilerJob{<:Any, OpenCLCompilerParams}) = POCL -GPUCompiler.method_table_view(job::OpenCLCompilerJob) = GPUCompiler.StackedMethodTable(job.world, method_table, spirv_method_table) +GPUCompiler.method_table_view(job::OpenCLCompilerJob) = GPUCompiler.StackedMethodTable(job.world, method_table, SPIRVIntrinsics.method_table) # filter out OpenCL built-ins # TODO: eagerly lower these using the translator API diff --git a/src/pocl/pocl.jl b/src/pocl/pocl.jl index 501e8b28..1cc693c8 100644 --- a/src/pocl/pocl.jl +++ b/src/pocl/pocl.jl @@ -45,40 +45,11 @@ import LLVM using Adapt ## device overrides - -# local method table for device functions -Base.Experimental.@MethodTable(method_table) - -macro device_override(ex) - return esc( - quote - Base.Experimental.@overlay($method_table, $ex) - end - ) -end - -macro device_function(ex) - ex = macroexpand(__module__, ex) - def = ExprTools.splitdef(ex) - - # generate a function that errors - def[:body] = quote - error("This function is not intended for use on the CPU") - end - - return esc( - quote - $(ExprTools.combinedef(def)) - @device_override $ex - end - ) -end - import SPIRVIntrinsics SPIRVIntrinsics.@import_all SPIRVIntrinsics.@reexport_public - -const spirv_method_table = SPIRVIntrinsics.method_table +# local method table for device functions +Base.Experimental.@MethodTable(method_table) include("compiler/compilation.jl") include("compiler/execution.jl") From 6bbeabbe483ba4339c280a872b9fde7b4f8ca91d Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Fri, 11 Jul 2025 10:24:32 +0200 Subject: [PATCH 4/5] fixup! cleanup --- src/pocl/backend.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pocl/backend.jl b/src/pocl/backend.jl index 70b16671..663e904b 100644 --- a/src/pocl/backend.jl +++ b/src/pocl/backend.jl @@ -1,7 +1,7 @@ module POCLKernels using ..POCL -using ..POCL: @device_override, SPIRVIntrinsics, cl +using ..POCL: @device_override, SPIRVIntrinsics, cl, method_table using ..POCL: device import KernelAbstractions as KA From cecfd9b714516c59c3bdf02698464829aa110f8f Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Fri, 11 Jul 2025 10:32:16 +0200 Subject: [PATCH 5/5] Apply suggestion from @vchuravy --- src/pocl/compiler/compilation.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pocl/compiler/compilation.jl b/src/pocl/compiler/compilation.jl index 4b4a4fb4..1a561b79 100644 --- a/src/pocl/compiler/compilation.jl +++ b/src/pocl/compiler/compilation.jl @@ -50,7 +50,7 @@ end # create GPUCompiler objects - target = SPIRVCompilerTarget(; supports_fp16, supports_fp64, validate = true, kwargs...) + target = SPIRVCompilerTarget(; supports_fp16, supports_fp64, kwargs...) params = OpenCLCompilerParams() return CompilerConfig(target, params; kernel, name, always_inline) end