diff --git a/.buildkite/downstream.yml b/.buildkite/downstream.yml index 084a8ee0..eba88d77 100644 --- a/.buildkite/downstream.yml +++ b/.buildkite/downstream.yml @@ -4,6 +4,14 @@ # `src/` and `ext/`, `Project.toml` and the CI configuration, except the other back-ends' # target files. The `if:` conditions implement the commit message tags documented in the # Development section of the README; keep both in sync when adding a step. +# +# Every package also gets a "(ports)" step, +# which runs its tests with the ports of Julia's passes (the `julia_passes = "ports"` +# preference, set up by `.ci/juliair.jl` in an environment of its own, as the default one +# persists on some agents) and fails unless JuliaIR.jl's +# ledger shows that they ran. `[only ports]` selects only the ports steps and +# `[skip ports]` skips them; a package's own tags (e.g. `[only cuda]`) select both of its +# steps. Steps are skipped on draft pull requests unless a tag selects them. steps: - label: "CUDA.jl" plugins: @@ -34,6 +42,7 @@ steps: - "ext/**" - "Project.toml" - ".buildkite/**" + - ".ci/**" exclude: - "src/{gcn,spirv,metal,bpf,native}.jl" if: | @@ -44,6 +53,39 @@ steps: soft_fail: - exit_status: 3 + - label: "CUDA.jl (ports)" + plugins: + - JuliaCI/julia#v1: + version: "1.13" + command: | + unset LD_LIBRARY_PATH + echo "--- :julia: Instantiating project" + # an environment of its own, so that the preference doesn't reach the other steps + export JULIA_PROJECT="$$(mktemp -d)" + julia .ci/juliair.jl downstream CUDA + + export JULIAIR_LEDGER="$$PWD/juliair-ledger.tsv" + rm -f "$$JULIAIR_LEDGER" + julia -e 'println("+++ :julia: Running tests") + using Pkg + Pkg.test("CUDA")' + + echo "+++ :julia: Checking that the ports ran" + julia .ci/juliair.jl check-ledger "$$JULIAIR_LEDGER" + agents: + queue: "cuda" + env: + JULIA_CPU_THREADS: "3" + if_changed: + include: *sources + exclude: + - "src/{gcn,spirv,metal,bpf,native}.jl" + if: | + build.message !~ /\[skip [^\]]*(tests|cuda|ptx|ports)/ && + (build.message =~ /\[only [^\]]*(cuda|ptx|ports)/ || + build.message !~ /\[only / && !build.pull_request.draft) + timeout_in_minutes: 120 + - label: "oneAPI.jl" plugins: - JuliaCI/julia#v1: @@ -77,6 +119,39 @@ steps: soft_fail: - exit_status: 3 + - label: "oneAPI.jl (ports)" + plugins: + - JuliaCI/julia#v1: + version: "1.13" + command: | + echo "--- :julia: Instantiating project" + # an environment of its own, so that the preference doesn't reach the other steps + export JULIA_PROJECT="$$(mktemp -d)" + julia .ci/juliair.jl downstream oneAPI + julia -e 'using Pkg; + println("+++ :julia: Building support library") + include(joinpath(Pkg.devdir(), "oneAPI", "deps", "build_ci.jl"))' + + export JULIAIR_LEDGER="$$PWD/juliair-ledger.tsv" + rm -f "$$JULIAIR_LEDGER" + julia -e 'println("+++ :julia: Running tests") + using Pkg + Pkg.test("oneAPI")' + + echo "+++ :julia: Checking that the ports ran" + julia .ci/juliair.jl check-ledger "$$JULIAIR_LEDGER" + agents: + queue: "oneapi" + if_changed: + include: *sources + exclude: + - "src/{ptx,gcn,metal,bpf,native}.jl" + if: | + build.message !~ /\[skip [^\]]*(tests|oneapi|spirv|ports)/ && + (build.message =~ /\[only [^\]]*(oneapi|spirv|ports)/ || + build.message !~ /\[only / && !build.pull_request.draft) + timeout_in_minutes: 60 + - label: "AMDGPU.jl" plugins: - JuliaCI/julia#v1: @@ -106,6 +181,36 @@ steps: soft_fail: - exit_status: 3 + - label: "AMDGPU.jl (ports)" + plugins: + - JuliaCI/julia#v1: + version: "1.13" + command: | + echo "--- :julia: Instantiating project" + # an environment of its own, so that the preference doesn't reach the other steps + export JULIA_PROJECT="$$(mktemp -d)" + julia .ci/juliair.jl downstream AMDGPU + + export JULIAIR_LEDGER="$$PWD/juliair-ledger.tsv" + rm -f "$$JULIAIR_LEDGER" + julia -e 'println("+++ :julia: Running tests") + using Pkg + Pkg.test("AMDGPU")' + + echo "+++ :julia: Checking that the ports ran" + julia .ci/juliair.jl check-ledger "$$JULIAIR_LEDGER" + agents: + queue: "rocm" + if_changed: + include: *sources + exclude: + - "src/{ptx,spirv,metal,bpf,native}.jl" + if: | + build.message !~ /\[skip [^\]]*(tests|amdgpu|gcn|ports)/ && + (build.message =~ /\[only [^\]]*(amdgpu|gcn|ports)/ || + build.message !~ /\[only / && !build.pull_request.draft) + timeout_in_minutes: 60 + - label: "Metal.jl" plugins: - JuliaCI/julia#v1: @@ -146,6 +251,36 @@ steps: soft_fail: - exit_status: 3 + - label: "Metal.jl (ports)" + plugins: + - JuliaCI/julia#v1: + version: "1.13" + command: | + echo "--- :julia: Instantiating project" + # an environment of its own, so that the preference doesn't reach the other steps + export JULIA_PROJECT="$$(mktemp -d)" + JULIA_PKG_PRECOMPILE_AUTO=0 julia .ci/juliair.jl downstream Metal --workspace + + export JULIAIR_LEDGER="$$PWD/juliair-ledger.tsv" + rm -f "$$JULIAIR_LEDGER" + julia -e 'println("+++ :julia: Running tests") + using Pkg + Pkg.test("Metal")' + + echo "+++ :julia: Checking that the ports ran" + julia .ci/juliair.jl check-ledger "$$JULIAIR_LEDGER" + agents: + queue: "metal" + if_changed: + include: *sources + exclude: + - "src/{ptx,gcn,spirv,bpf,native}.jl" + if: | + build.message !~ /\[skip [^\]]*(tests|metal|ports)/ && + (build.message =~ /\[only [^\]]*(metal|ports)/ || + build.message !~ /\[only / && !build.pull_request.draft) + timeout_in_minutes: 120 + - label: "OpenCL.jl" plugins: - JuliaCI/julia#v1: @@ -176,5 +311,35 @@ steps: soft_fail: - exit_status: 3 + - label: "OpenCL.jl (ports)" + plugins: + - JuliaCI/julia#v1: + version: "1.13" + command: | + echo "--- :julia: Instantiating project" + # an environment of its own, so that the preference doesn't reach the other steps + export JULIA_PROJECT="$$(mktemp -d)" + julia .ci/juliair.jl downstream OpenCL SPIRVIntrinsics + + export JULIAIR_LEDGER="$$PWD/juliair-ledger.tsv" + rm -f "$$JULIAIR_LEDGER" + julia -e 'println("+++ :julia: Running tests") + using Pkg + Pkg.test("OpenCL")' + + echo "+++ :julia: Checking that the ports ran" + julia .ci/juliair.jl check-ledger "$$JULIAIR_LEDGER" + agents: + queue: "cuda" + if_changed: + include: *sources + exclude: + - "src/{ptx,gcn,metal,bpf,native}.jl" + if: | + build.message !~ /\[skip [^\]]*(tests|opencl|spirv|ports)/ && + (build.message =~ /\[only [^\]]*(opencl|spirv|ports)/ || + build.message !~ /\[only / && !build.pull_request.draft) + timeout_in_minutes: 120 + env: JULIA_PKG_SERVER_REGISTRY_PREFERENCE: "eager" # see freshly-registered versions without cloning the registry diff --git a/.ci/juliair.jl b/.ci/juliair.jl new file mode 100644 index 00000000..7128a767 --- /dev/null +++ b/.ci/juliair.jl @@ -0,0 +1,124 @@ +# Set up CI environments for the selected Julia pass provider. +# +# julia .ci/juliair.jl test (cpp|ports) +# +# Prepares GPUCompiler's test environment (`test/`), for `julia --project=test +# test/runtests.jl`, with the `julia_passes` preference set to the given provider. +# +# julia .ci/juliair.jl downstream PACKAGE [PACKAGES...] [--workspace] +# +# Develops GPUCompiler and the downstream PACKAGE (and further PACKAGES, e.g. packages of +# PACKAGE's repository) into the active environment, for `Pkg.test(PACKAGE)` with the +# ports. The preference goes into the active environment (in CI, a temporary one), +# which `Pkg.test` passes on to the test environment; GPUCompiler is added to PACKAGE's +# test dependencies so that it applies there. `--workspace` also instantiates PACKAGE's +# workspace against GPUCompiler (Metal.jl's tests run examples in its workspace +# environments). +# +# julia .ci/juliair.jl check-ledger FILE +# +# Fails unless FILE, the JuliaIR.jl ledger (`JULIAIR_LEDGER`) of a run with the ports, +# shows that the core ports ran. + +using Pkg, TOML + +const ROOT = dirname(@__DIR__) +const GPUCOMPILER_UUID = "61eb1bfa-7361-4325-ad38-22787b887f55" + +function write_preferences(dir, mode) + # an environment that doesn't exist yet (Pkg creates it on the first change) + mkpath(dir) + file = joinpath(dir, "LocalPreferences.toml") + open(io -> TOML.print(io, Dict("GPUCompiler" => Dict("julia_passes" => mode))), file, "w") + println("$file:") + print(read(file, String)) +end + +function test(mode) + mode in ("cpp", "ports") || error("Unknown provider $mode; expected cpp or ports") + write_preferences(joinpath(ROOT, "test"), mode) + Pkg.activate(joinpath(ROOT, "test")) + # Julia 1.10 and 1.11 have no workspaces, so the test environment needs GPUCompiler + VERSION < v"1.12" && Pkg.develop(path=ROOT) + Pkg.instantiate() + Pkg.status() +end + +function downstream(packages, workspace) + package = first(packages) + env = dirname(Base.active_project()) + write_preferences(env, "ports") + + Pkg.develop(path=ROOT) + Pkg.develop([PackageSpec(name=name) for name in packages]) + + checkout = joinpath(Pkg.devdir(), package) + if workspace + # before editing the test environment, which is part of the workspace and could + # then not be resolved; the examples that need the workspace run in the test + # process, which has loaded GPUCompiler already + Pkg.activate(checkout) + Pkg.develop(path=ROOT) + Pkg.instantiate(workspace=true) + Pkg.activate(env) + end + test_project = joinpath(checkout, "test", "Project.toml") + isfile(test_project) || error("$package has no test/Project.toml") + add = Dict("GPUCompiler" => GPUCOMPILER_UUID) + # by editing the text, as rewriting it with TOML.print reorders the file + deps = get(TOML.parsefile(test_project), "deps", Dict()) + lines = [k * " = \"" * v * "\"" for (k, v) in add if !haskey(deps, k)] + if !isempty(lines) + text = read(test_project, String) + occursin(r"^\[deps\]$"m, text) || (text = "[deps]\n\n" * text) + text = replace(text, r"^\[deps\]$"m => join(["[deps]"; lines], "\n"); count=1) + write(test_project, text) + end + + Pkg.status() +end + +# the ports that run for every kernel on every target and Julia version, and JuliaLICM, +# which runs for every loop (that test suites have) +const CORE_PORTS = ["jl-alloc-opt", "jl-late-lower-gc-frame", "jl-propagate-addrspaces", + "jl-remove-ni", "jl-demote-float16", "jl-remove-addrspaces", "jl-licm"] + +function check_ledger(file) + isfile(file) || error("No JuliaIR.jl ledger at $file: the ports did not run") + counts = Dict{String,Int}() + pids = Set{String}() + for (i, line) in enumerate(eachline(file)) + # PID, "pass", the pass, the Julia dialect and the LLVM version + fields = split(line, '\t') + if !(length(fields) == 5 && all(isdigit, fields[1]) && fields[2] == "pass" && + startswith(fields[3], "jl-") && startswith(fields[4], "julia=") && + startswith(fields[5], "llvm=")) + error("Malformed line $i in the ledger at $file: $(repr(line))") + end + counts[fields[3]] = get(counts, fields[3], 0) + 1 + push!(pids, fields[1]) + end + for (name, count) in sort!(collect(counts)) + println(rpad(name, 32), count) + end + println("in $(length(pids)) processes") + missing_ports = setdiff(CORE_PORTS, keys(counts)) + isempty(missing_ports) || + error("The ports $(join(missing_ports, ", ")) did not run; is GPUCompiler using the C++ passes?") +end + +function main(args) + isempty(args) && error("Usage: julia .ci/juliair.jl (test MODE | downstream PACKAGE... [--workspace] | check-ledger FILE)") + command = args[1] + if command == "test" + test(args[2]) + elseif command == "downstream" + downstream(filter(!=("--workspace"), args[2:end]), "--workspace" in args) + elseif command == "check-ledger" + check_ledger(args[2]) + else + error("Unknown command $command") + end +end + +main(ARGS) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index d0491497..8c52a5e1 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -13,7 +13,7 @@ concurrency: jobs: release_test: - name: Julia ${{ matrix.version }} ${{ matrix.llvm_args }}${{ matrix.coverage == 'false' && ' (no coverage)' || '' }} - ${{ matrix.os }} + name: Julia ${{ matrix.version }} ${{ matrix.llvm_args }}${{ matrix.coverage == 'false' && ' (no coverage)' || '' }} - ${{ matrix.os }} - ${{ matrix.julia_passes }} runs-on: ${{ matrix.os }} timeout-minutes: 20 strategy: @@ -23,6 +23,8 @@ jobs: os: [ubuntu-24.04, ubuntu-24.04-arm, macOS-15, windows-2025] llvm_args: [''] coverage: ['true'] + # the provider of Julia's passes (the `julia_passes` preference) + julia_passes: [cpp, ports] include: # code coverage recompiles the package instead of using its precompiled code, # which hides bugs in how that code interacts with methods back-ends define @@ -30,32 +32,41 @@ jobs: os: 'ubuntu-24.04' llvm_args: '' coverage: 'false' + julia_passes: cpp # starting with Julia 1.10, we can enable opaque pointers - # from Julia 1.12 on, this is the default. + # from Julia 1.12 on, this is the default. (the ports always use them.) - version: '1.10' os: 'ubuntu-24.04' llvm_args: '--opaque-pointers' + julia_passes: cpp - version: '1.10' os: 'ubuntu-24.04-arm' llvm_args: '--opaque-pointers' + julia_passes: cpp - version: '1.10' os: 'macOS-15' llvm_args: '--opaque-pointers' + julia_passes: cpp - version: '1.10' os: 'windows-2025' llvm_args: '--opaque-pointers' + julia_passes: cpp - version: '1.11' os: 'ubuntu-24.04' llvm_args: '--opaque-pointers' + julia_passes: cpp - version: '1.11' os: 'ubuntu-24.04-arm' llvm_args: '--opaque-pointers' + julia_passes: cpp - version: '1.11' os: 'macOS-15' llvm_args: '--opaque-pointers' + julia_passes: cpp - version: '1.11' os: 'windows-2025' llvm_args: '--opaque-pointers' + julia_passes: cpp steps: - uses: actions/checkout@v7 @@ -67,12 +78,24 @@ jobs: - name: Set-up cache uses: julia-actions/cache@v3 + # Run the suite in the environment with the selected pass-provider preference. + - name: Set up the test environment + run: julia --color=yes .ci/juliair.jl test ${{ matrix.julia_passes }} + + # with the flags `julia-actions/julia-runtest` (`Pkg.test`) would pass - name: Run tests - uses: julia-actions/julia-runtest@v1 - with: - coverage: ${{ matrix.coverage != 'false' }} + run: julia --color=yes --check-bounds=yes --depwarn=yes --code-coverage=${{ matrix.coverage != 'false' && format('@{0}', github.workspace) || 'none' }} --project=test test/runtests.jl env: JULIA_LLVM_ARGS: ${{ matrix.llvm_args }} + JULIAIR_LEDGER: ${{ runner.temp }}/juliair-ledger.tsv + + # the suite also runs the ports explicitly, so check the preference too + - name: Check that the ports were the default + if: matrix.julia_passes == 'ports' + run: julia --project=test -e 'using GPUCompiler; GPUCompiler.JULIA_PASSES === :ports || error("julia_passes preference not applied")' + - name: Check that the ports ran + if: matrix.julia_passes == 'ports' + run: julia .ci/juliair.jl check-ledger '${{ runner.temp }}/juliair-ledger.tsv' - name: Process coverage # CoverageTools 1.4.1 uses a PkgLoadSpec field renamed in Julia 1.14. diff --git a/Project.toml b/Project.toml index a9a9a1d3..dff84877 100644 --- a/Project.toml +++ b/Project.toml @@ -10,6 +10,7 @@ projects = ["test"] CompilerCaching = "9db33cc3-5358-4881-8759-fa4194144afd" ExprTools = "e2ba6199-217a-4e67-a87a-7c52f15ade04" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" +JuliaIR = "3913dba9-7ba9-44aa-bf76-1cfa3b3073b1" LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" @@ -45,6 +46,7 @@ Cthulhu = "2, 3" ExprTools = "0.1" Highlights = "0.6" InteractiveUtils = "1" +JuliaIR = "0.1.4" LLVM = "10.4" LLVMDowngrader_jll = "0.11, 0.12" Libdl = "1" diff --git a/README.md b/README.md index fc8988a2..6cb66243 100644 --- a/README.md +++ b/README.md @@ -51,3 +51,25 @@ last commit of a pull request: Back-ends are named `cuda`, `amdgpu`, `metal`, `oneapi` and `opencl` (all lowercase), or by their target: `ptx` for CUDA.jl, `gcn` for AMDGPU.jl, and `spirv` for both oneAPI.jl and OpenCL.jl. + + +## Julia's LLVM passes + +GPUCompiler's optimization pipeline includes several of the LLVM passes of Julia's code +generator, e.g., to lower GC intrinsics or to optimize allocations. These normally come from +Julia's C++ implementation, but can also be taken from [JuliaIR.jl](https://github.com/JuliaLLVM/JuliaIR.jl), +which ports them to Julia. This is selected per compilation with the `julia_passes` keyword +argument of `CompilerConfig` (`:cpp` or `:ports`), whose default comes from a preference: + +```julia +using Preferences, GPUCompiler +set_preferences!(GPUCompiler, "julia_passes" => "ports") +``` + +The preference takes effect after restarting Julia, and also applies to the code that +packages compile in their precompilation workloads, so that a session with the same +preference uses it. The ports require opaque pointers: on Julia 1.10 and 1.11, contexts +created by `JuliaContext()` use them when the preference selects the ports, and other +contexts must be created with `JuliaContext(; opaque_pointers=true)`. + +To run the test suite with the ports, set the preference in `test/LocalPreferences.toml`. diff --git a/src/GPUCompiler.jl b/src/GPUCompiler.jl index 800c8820..e3997281 100644 --- a/src/GPUCompiler.jl +++ b/src/GPUCompiler.jl @@ -3,6 +3,8 @@ module GPUCompiler using LLVM, LLVM.IR, LLVM.Build, LLVM.Passes, LLVM.ORC using LLVM.Interop +import JuliaIR + using ExprTools: splitdef, combinedef @@ -79,6 +81,7 @@ include("jlgen.jl") include("irgen.jl") include("exception_sites.jl") include("determinism.jl") +include("julia_passes.jl") include("optim.jl") include("validation.jl") include("rtlib.jl") diff --git a/src/driver.jl b/src/driver.jl index 5661bc74..93f04fb5 100644 --- a/src/driver.jl +++ b/src/driver.jl @@ -18,12 +18,15 @@ export JuliaContext # JuliaContext helper below, which returns a local context on Julia 1.9, and the global # unique context on all other versions. Once we only support Julia 1.9, we'll deprecate # this helper to a regular `Context()` call. -function JuliaContext(; opaque_pointers=nothing) +# +# By default, the context uses opaque pointers if configurations select the ports of Julia's +# passes by default, which require them, and LLVM's default otherwise. +function JuliaContext(; opaque_pointers=context_opaque_pointers(JULIA_PASSES)) # XXX: remove ThreadSafeContext(; opaque_pointers) end -function JuliaContext(f; kwargs...) - ThreadSafeContext(; kwargs...) do ts_ctx +function JuliaContext(f; opaque_pointers=context_opaque_pointers(JULIA_PASSES)) + ThreadSafeContext(; opaque_pointers) do ts_ctx # for now, also activate the underlying context # XXX: this is wrong; we can't expose the underlying LLVM context, but should # instead always go through the callback in order to unlock it properly. @@ -108,6 +111,7 @@ function compile_unhooked(output::Symbol, @nospecialize(job::CompilerJob); if context(; throw_error=false) === nothing error("No active LLVM context. Use `JuliaContext()` do-block syntax to create one.") end + check_julia_passes(job) @tracepoint "Validation" begin check_method(job) # not optional diff --git a/src/interface.jl b/src/interface.jl index b142937f..a07fbe87 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -143,7 +143,8 @@ else end const CONFIG_KWARGS = [:kernel, :name, :entry_abi, :always_inline, :opt_level, - :debug_level, :libraries, :optimize, :cleanup, :validate, :strip] + :debug_level, :libraries, :optimize, :cleanup, :validate, :strip, + :julia_passes] """ CompilerConfig(target, params; kernel=true, entry_abi=:specfunc, name=nothing, @@ -177,6 +178,11 @@ Several keyword arguments can be used to customize the compilation process: - `cleanup`: run cleanup passes on the code (default: true) - `validate`: enable optional validation of input and outputs (default: true) - `strip`: strip non-functional metadata and debug information (default: false) +- `julia_passes`: the implementation of Julia's LLVM passes (e.g. GC lowering) that + optimization uses: `:cpp` for the passes of Julia's code generator, or `:ports` for + their ports in JuliaIR.jl. The default comes from the + `julia_passes` preference of GPUCompiler (`:cpp` if unset). See + [`GPUCompiler.julia_pass`](@ref) for which passes are ported. """ struct CompilerConfig{T,P} target::T @@ -193,6 +199,7 @@ struct CompilerConfig{T,P} cleanup::Bool validate::Bool strip::Bool + julia_passes::Symbol # internal toplevel::Bool @@ -206,10 +213,14 @@ struct CompilerConfig{T,P} always_inline=false, opt_level=2, debug_level=Base.JLOptions().debug_level, optimize=toplevel, libraries=toplevel, cleanup=toplevel, validate=toplevel, - strip=false, only_entry=false) + strip=false, julia_passes=JULIA_PASSES, + only_entry=false) if entry_abi ∉ (:specfunc, :func) error("Unknown entry_abi=$entry_abi") end + if julia_passes ∉ (:cpp, :ports) + error("Unknown julia_passes=$julia_passes") + end owner = @static if HAS_INTEGRATED_CACHE cache_owner(target, params, always_inline) else @@ -217,8 +228,8 @@ struct CompilerConfig{T,P} end new{typeof(target), typeof(params)}(target, params, kernel, name, entry_abi, always_inline, opt_level, debug_level, libraries, - optimize, cleanup, validate, strip, toplevel, - only_entry, owner) + optimize, cleanup, validate, strip, + julia_passes, toplevel, only_entry, owner) end end @@ -228,7 +239,8 @@ function CompilerConfig(cfg::CompilerConfig; target=cfg.target, params=cfg.param always_inline=cfg.always_inline, opt_level=cfg.opt_level, debug_level=cfg.debug_level, libraries=cfg.libraries, optimize=cfg.optimize, cleanup=cfg.cleanup, - validate=cfg.validate, strip=cfg.strip, toplevel=cfg.toplevel, + validate=cfg.validate, strip=cfg.strip, + julia_passes=cfg.julia_passes, toplevel=cfg.toplevel, only_entry=cfg.only_entry) # deriving a non-toplevel job disables certain features # XXX: should we keep track if any of these were set explicitly in the first place? @@ -240,8 +252,8 @@ function CompilerConfig(cfg::CompilerConfig; target=cfg.target, params=cfg.param validate = false end CompilerConfig(target, params; kernel, entry_abi, name, always_inline, opt_level, - debug_level, libraries, optimize, cleanup, validate, strip, toplevel, - only_entry) + debug_level, libraries, optimize, cleanup, validate, strip, + julia_passes, toplevel, only_entry) end function Base.show(io::IO, @nospecialize(cfg::CompilerConfig{T})) where {T} @@ -263,6 +275,7 @@ function Base.hash(cfg::CompilerConfig, h::UInt) h = hash(cfg.cleanup, h) h = hash(cfg.validate, h) h = hash(cfg.strip, h) + h = hash(cfg.julia_passes, h) h = hash(cfg.toplevel, h) h = hash(cfg.only_entry, h) # `cache_owner` deliberately is not hashed: its target/params/inlining components were diff --git a/src/irgen.jl b/src/irgen.jl index a6162e97..9377a96b 100644 --- a/src/irgen.jl +++ b/src/irgen.jl @@ -666,6 +666,14 @@ function classify_arguments(@nospecialize(job::CompilerJob), codegen_ft::LLVM.Fu codegen_i += 1 end + # codegen omits ghost arguments, and `Type{T}` ones only if the signature uses + # `Core.TypeEgal` keys like GPUCompiler's `methodinstance` does; otherwise the arguments + # would be classified against the wrong parameters. after optimization, back-ends may + # have added parameters. + if !post_optimization && codegen_i - 1 != length(codegen_types) + error("Cannot classify the arguments of $(job.source): the signature has $(codegen_i - 1) non-ghost arguments, but the function generated for it has $(length(codegen_types)) parameters") + end + return args end diff --git a/src/julia_passes.jl b/src/julia_passes.jl new file mode 100644 index 00000000..69f4effa --- /dev/null +++ b/src/julia_passes.jl @@ -0,0 +1,69 @@ +# Julia's LLVM passes, from Julia's code generator or as ported to Julia by JuliaIR.jl + +# The default of `CompilerConfig`'s `julia_passes`. Set with +# `Preferences.set_preferences!(GPUCompiler, "julia_passes" => "ports")`. +const JULIA_PASSES = Symbol(@load_preference("julia_passes", "cpp")) +JULIA_PASSES in (:cpp, :ports) || + error("Invalid julia_passes preference $(repr(String(JULIA_PASSES))); expected \"cpp\" or \"ports\"") + +function check_julia_passes(@nospecialize(job::CompilerJob)) + job.config.julia_passes === :ports || return + supports_typed_pointers(context()) && + error("julia_passes=:ports requires a context with opaque pointers; create it with `JuliaContext(; opaque_pointers=true)`") + return +end + +# The `opaque_pointers` argument for contexts to compile in with `julia_passes`: the ports +# only support opaque pointers, which Julia 1.10 and 1.11 do not use by default. +context_opaque_pointers(julia_passes::Symbol) = julia_passes === :ports ? true : nothing + +# The port of each of Julia's passes in the pipeline, by the name of the `LLVM.Interop` +# constructor of the C++ pass (or the name it would have, see `CPP_PASS_NAMES`). +const PASS_PORTS = Dict{Symbol,String}( + :AllocOptPass => "jl-alloc-opt", + :CancellationLoweringPass => "jl-cancellation-lowering", + :CombineMulAddPass => "jl-combine-muladd", + :DemoteFloat16Pass => "jl-demote-float16", + :ExpandAtomicModifyPass => "jl-expand-atomic-modify", + :FinalLowerGCPass => "jl-final-lower-gc", + :GCInvariantVerifierPass => "jl-gc-invariant-verifier", + :JuliaLICMPass => "jl-licm", + :LateLowerGCPass => "jl-late-lower-gc-frame", + :LowerExcHandlersPass => "jl-lower-exc-handlers", + :LowerPTLSPass => "jl-lower-ptls", + :LowerSIMDLoopPass => "jl-lower-simdloop", + :PropagateJuliaAddrspacesPass => "jl-propagate-addrspaces", + :RemoveJuliaAddrspacesPass => "jl-remove-addrspaces", + :RemoveNIPass => "jl-remove-ni", +) + +""" + julia_pass(job, name::Symbol) + +The Julia pass `name` (the name of its constructor in `LLVM.Interop`, e.g. +`:AllocOptPass`) for use in the optimization pipeline of `job`: Julia's C++ pass, or its +port in JuliaIR.jl if `job.config.julia_passes == :ports`. Passes that `LLVM.Interop` has +no constructor for are named as if it had one, e.g. `:CancellationLoweringPass`. +""" +function julia_pass(@nospecialize(job::CompilerJob), name::Symbol) + if job.config.julia_passes === :ports + haskey(PASS_PORTS, name) || error("Unknown Julia pass $name") + return PASS_PORTS[name] + end + isdefined(LLVM.Interop, name) && return getfield(LLVM.Interop, name)() + haskey(CPP_PASS_NAMES, name) || error("Unknown Julia pass $name") + return CPP_PASS_NAMES[name] +end + +# Julia's passes that `LLVM.Interop` has no constructor for: their names in the pipelines +# of Julia's code generator, which registers them with every `PassBuilder`. +const CPP_PASS_NAMES = Dict{Symbol,String}( + :CancellationLoweringPass => "CancellationLowering", +) + +# make the ports usable by name in the pipeline of `pb` +function register_julia_passes!(pb::PassBuilder, @nospecialize(job::CompilerJob)) + job.config.julia_passes === :ports || return + JuliaIR.register!(pb) + return +end diff --git a/src/optim.jl b/src/optim.jl index e6390588..fab35623 100644 --- a/src/optim.jl +++ b/src/optim.jl @@ -57,6 +57,7 @@ function optimize!(@nospecialize(job::CompilerJob), mod::LLVM.Module, @dispose pb=PassBuilder() begin tti === nothing || LLVM.target_transform_info!(pb, tti) + register_julia_passes!(pb, job) register!(pb, GPULowerCPUFeaturesPass(job)) register!(pb, GPULowerPTLSPass(job)) register!(pb, GPULowerGCFramePass(job, relocs)) @@ -126,7 +127,7 @@ end function buildEarlySimplificationPipeline(mpm, @nospecialize(job::CompilerJob), opt_level) if should_verify() add!(mpm, FunctionPassManager()) do fpm - add!(fpm, GCInvariantVerifierPass()) + add!(fpm, julia_pass(job, :GCInvariantVerifierPass)) end add!(mpm, VerifierPass()) end @@ -139,7 +140,7 @@ function buildEarlySimplificationPipeline(mpm, @nospecialize(job::CompilerJob), add!(mpm, FunctionPassManager()) do fpm add!(fpm, LowerExpectIntrinsicPass()) if opt_level >= 2 - add!(fpm, PropagateJuliaAddrspacesPass()) + add!(fpm, julia_pass(job, :PropagateJuliaAddrspacesPass)) end # DCE must come before simplifycfg: codegen can generate unused # statements that would otherwise alter how simplifycfg optimizes the CFG. @@ -172,7 +173,7 @@ function buildEarlyOptimizerPipeline(mpm, @nospecialize(job::CompilerJob), opt_l end if opt_level >= 2 add!(cgpm, FunctionPassManager()) do fpm - add!(fpm, AllocOptPass()) + add!(fpm, julia_pass(job, :AllocOptPass)) end end end @@ -190,7 +191,7 @@ function buildEarlyOptimizerPipeline(mpm, @nospecialize(job::CompilerJob), opt_l add!(fpm, LibCallsShrinkWrapPass()) add!(fpm, ReassociatePass()) add!(fpm, ConstraintEliminationPass()) - add!(fpm, AllocOptPass()) + add!(fpm, julia_pass(job, :AllocOptPass)) else add!(fpm, EarlyCSEPass()) add!(fpm, instcombine_pass(job)) @@ -221,17 +222,17 @@ IRCEWithCleanupPass(job) = function buildLoopOptimizerPipeline(fpm, @nospecialize(job::CompilerJob), opt_level) add!(fpm, LoopPassManager(; use_memory_ssa=true)) do lpm - add!(lpm, LowerSIMDLoopPass()) + add!(lpm, julia_pass(job, :LowerSIMDLoopPass)) if opt_level >= 2 add!(lpm, LoopInstSimplifyPass()) add!(lpm, LoopSimplifyCFGPass()) # run LICM with AllowSpeculation=false before rotation to avoid # speculating loads that rotation can hoist more precisely. add!(lpm, LICMPass(; allowspeculation=false)) - add!(lpm, JuliaLICMPass()) + add!(lpm, julia_pass(job, :JuliaLICMPass)) add!(lpm, LoopRotatePass()) add!(lpm, LICMPass()) - add!(lpm, JuliaLICMPass()) + add!(lpm, julia_pass(job, :JuliaLICMPass)) add!(lpm, SimpleLoopUnswitchPass(nontrivial=true, trivial=true)) end if LLVM.version() >= v"17" @@ -278,7 +279,7 @@ end function buildScalarOptimizerPipeline(fpm, @nospecialize(job::CompilerJob), opt_level) if opt_level >= 2 - add!(fpm, AllocOptPass()) + add!(fpm, julia_pass(job, :AllocOptPass)) add!(fpm, SROAPass()) if can_vectorize(job) add!(fpm, VectorCombinePass()) @@ -296,7 +297,7 @@ function buildScalarOptimizerPipeline(fpm, @nospecialize(job::CompilerJob), opt_ add!(fpm, JumpThreadingPass()) add!(fpm, ConstraintEliminationPass()) elseif opt_level >= 1 - add!(fpm, AllocOptPass()) + add!(fpm, julia_pass(job, :AllocOptPass)) add!(fpm, SROAPass()) add!(fpm, MemCpyOptPass()) add!(fpm, SCCPPass()) @@ -309,10 +310,10 @@ function buildScalarOptimizerPipeline(fpm, @nospecialize(job::CompilerJob), opt_ add!(fpm, PeepholeCallbacks(; opt_level)) end add!(fpm, simplifycfg_pass(job, AggressiveSimplifyCFGOptions)) - add!(fpm, AllocOptPass()) + add!(fpm, julia_pass(job, :AllocOptPass)) add!(fpm, LoopPassManager(; use_memory_ssa=true)) do lpm add!(lpm, LICMPass()) - add!(lpm, JuliaLICMPass()) + add!(lpm, julia_pass(job, :JuliaLICMPass)) end add!(fpm, simplifycfg_pass(job, AggressiveSimplifyCFGOptions)) add!(fpm, instcombine_pass(job)) @@ -368,7 +369,7 @@ function buildVectorPipeline(fpm, @nospecialize(job::CompilerJob), opt_level) end function buildIntrinsicLoweringPipeline(mpm, @nospecialize(job::CompilerJob), opt_level) - add!(mpm, RemoveNIPass()) + add!(mpm, julia_pass(job, :RemoveNIPass)) # lower GC intrinsics if !uses_julia_runtime(job) @@ -406,24 +407,30 @@ function buildIntrinsicLoweringPipeline(mpm, @nospecialize(job::CompilerJob), op add!(mpm, FunctionPassManager()) do fpm # lower exception handling if uses_julia_runtime(job) && VERSION < v"1.13.0-DEV.36" - add!(fpm, LowerExcHandlersPass()) + add!(fpm, julia_pass(job, :LowerExcHandlersPass)) end - add!(fpm, GCInvariantVerifierPass()) - add!(fpm, LateLowerGCPass()) + # lower the cancellation points of `Base.@cancel_check` to `setjmp`s into the task, + # before GC lowering (JuliaLang/julia#62604, reverted on 1.14 by + # JuliaLang/julia#64651). Only Julia's runtime can deliver a cancellation. + if uses_julia_runtime(job) && VERSION >= v"1.15-" + add!(fpm, julia_pass(job, :CancellationLoweringPass)) + end + add!(fpm, julia_pass(job, :GCInvariantVerifierPass)) + add!(fpm, julia_pass(job, :LateLowerGCPass)) # FinalLowerGCPass moved from a module pass to a function pass in # JuliaLang/julia#51081 (1.11.0-DEV.208). if uses_julia_runtime(job) && VERSION >= v"1.11.0-DEV.208" - add!(fpm, FinalLowerGCPass()) + add!(fpm, julia_pass(job, :FinalLowerGCPass)) end # codegen emits atomic modifications (e.g. `@atomic x.f += 1`) as calls to the # `julia.atomicmodify` pseudo-intrinsic, which Julia expands after GC lowering # (JuliaLang/julia#57010) @static if VERSION >= v"1.13.0-DEV.321" - add!(fpm, ExpandAtomicModifyPass()) + add!(fpm, julia_pass(job, :ExpandAtomicModifyPass)) end end if uses_julia_runtime(job) && VERSION < v"1.11.0-DEV.208" - add!(mpm, FinalLowerGCPass()) + add!(mpm, julia_pass(job, :FinalLowerGCPass)) end if opt_level >= 2 @@ -437,7 +444,7 @@ function buildIntrinsicLoweringPipeline(mpm, @nospecialize(job::CompilerJob), op # lower PTLS intrinsics if uses_julia_runtime(job) - add!(mpm, LowerPTLSPass()) + add!(mpm, julia_pass(job, :LowerPTLSPass)) end if opt_level >= 1 @@ -455,7 +462,7 @@ function buildIntrinsicLoweringPipeline(mpm, @nospecialize(job::CompilerJob), op end # remove Julia address spaces - add!(mpm, RemoveJuliaAddrspacesPass()) + add!(mpm, julia_pass(job, :RemoveJuliaAddrspacesPass)) # Julia's operand bundles confuse the inliner, so repeat here now they are gone. # FIXME: we should fix the inliner so that inlined code gets optimized early-on @@ -469,7 +476,7 @@ function buildCleanupPipeline(mpm, @nospecialize(job::CompilerJob), opt_level) if opt_level >= 2 add!(mpm, FunctionPassManager()) do fpm if VERSION < v"1.12.0-DEV.1390" - add!(fpm, CombineMulAddPass()) + add!(fpm, julia_pass(job, :CombineMulAddPass)) end add!(fpm, DivRemPairsPass()) end @@ -481,7 +488,7 @@ function buildCleanupPipeline(mpm, @nospecialize(job::CompilerJob), opt_level) add!(fpm, AnnotationRemarksPass()) end add!(mpm, FunctionPassManager()) do fpm - add!(fpm, DemoteFloat16Pass()) + add!(fpm, julia_pass(job, :DemoteFloat16Pass)) end end diff --git a/src/reflection.jl b/src/reflection.jl index d86170ad..e8483481 100644 --- a/src/reflection.jl +++ b/src/reflection.jl @@ -256,7 +256,8 @@ function code_llvm(io::IO, @nospecialize(job::CompilerJob); optimize::Bool=job.c debuginfo::Symbol=:default, dump_module::Bool=false, kwargs...) # NOTE: jl_dump_function_ir supports stripping metadata, so don't do it in the driver config = CompilerConfig(job.config; validate=false, strip=false, optimize) - str = JuliaContext() do ctx + opaque_pointers = context_opaque_pointers(config.julia_passes) + str = JuliaContext(; opaque_pointers) do ctx ir, meta = compile(:llvm, CompilerJob(job; config)) ts_mod = ThreadSafeModule(ir) entry_fn = meta.entry @@ -289,7 +290,8 @@ See also: [`@device_code_native`](@ref), `InteractiveUtils.code_llvm` function code_native(io::IO, @nospecialize(job::CompilerJob); raw::Bool=false, dump_module::Bool=false) config = CompilerConfig(job.config; strip=!raw, only_entry=!dump_module, validate=false) - asm = JuliaContext() do ctx + opaque_pointers = context_opaque_pointers(config.julia_passes) + asm = JuliaContext(; opaque_pointers) do ctx local asm, meta = compile(:asm, CompilerJob(job; config)) dispose(meta.ir) asm diff --git a/src/rtlib.jl b/src/rtlib.jl index 4016277c..494d9642 100644 --- a/src/rtlib.jl +++ b/src/rtlib.jl @@ -85,7 +85,10 @@ end mutable struct RuntimeFunctionResults bitcode::Union{Nothing,Vector{UInt8}} relocations::Relocations - RuntimeFunctionResults() = new(nothing, Relocations()) + # whether `bitcode` uses opaque pointers: on Julia 1.10 and 1.11, contexts can use + # either, and bitcode with opaque pointers can't be read in a typed context + opaque_pointers::Bool + RuntimeFunctionResults() = new(nothing, Relocations(), false) end # Compile a single runtime function and link it into `mod`. The renamed bitcode is @@ -99,7 +102,8 @@ function emit_function!(mod, relocs::Relocations, config::CompilerConfig, # Don't run a standalone inference walk on a miss: `compile_unhooked` below drives # inference itself. ci, res = runtime_function_results(rt_job) - if res !== nothing && res.bitcode !== nothing + opaque_pointers = !supports_typed_pointers(context()) + if res !== nothing && res.bitcode !== nothing && res.opaque_pointers == opaque_pointers link_relocatable!(mod, relocs, parse(LLVM.Module, res.bitcode), res.relocations) @@ -139,6 +143,7 @@ function emit_function!(mod, relocs::Relocations, config::CompilerConfig, res === nothing && (res = runtime_results(RuntimeFunctionResults, ci, rt_job.config)) res.bitcode = take!(io) res.relocations = meta.relocations + res.opaque_pointers = opaque_pointers end link_relocatable!(mod, relocs, new_mod, meta.relocations) diff --git a/src/spirv.jl b/src/spirv.jl index ddf89b21..3e6c32f0 100644 --- a/src/spirv.jl +++ b/src/spirv.jl @@ -397,7 +397,8 @@ source_code(target::SPIRVCompilerTarget) = "spirv" # reimplementation that uses the SPIR-V disassembler, giving much more pleasant output function code_native(io::IO, job::CompilerJob{SPIRVCompilerTarget}; raw::Bool=false, dump_module::Bool=false) config = CompilerConfig(job.config; strip=!raw, only_entry=!dump_module, validate=false) - obj = JuliaContext() do ctx + opaque_pointers = context_opaque_pointers(config.julia_passes) + obj = JuliaContext(; opaque_pointers) do ctx local obj, meta = compile(:obj, CompilerJob(job; config)) dispose(meta.ir) obj diff --git a/test/Project.toml b/test/Project.toml index 8d2eba27..e72c8991 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -6,6 +6,7 @@ FileCheck = "4e644321-382b-4b05-b0b6-5d23c3d944fb" GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55" Highlights = "eafb193a-b7ab-5a9e-9068-77385905fa72" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" +JuliaIR = "3913dba9-7ba9-44aa-bf76-1cfa3b3073b1" LLVM = "929cbde3-209d-540e-8aea-75f648917ca0" LLVMDowngrader_jll = "f52de702-fb25-5922-94ba-81dd59b07444" Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" @@ -23,4 +24,5 @@ demumble_jll = "1e29f10c-031c-5a83-9565-69cddfc27673" [compat] Aqua = "0.8" +JuliaIR = "0.1.4" ParallelTestRunner = "2" diff --git a/test/julia_passes.jl b/test/julia_passes.jl new file mode 100644 index 00000000..eb4f74a7 --- /dev/null +++ b/test/julia_passes.jl @@ -0,0 +1,398 @@ +@testset "configuration" begin + job, _ = PTX.create_job(identity, (Int,)) + @test job.config.julia_passes === GPUCompiler.JULIA_PASSES + + cpp, _ = PTX.create_job(identity, (Int,); julia_passes=:cpp) + ports, _ = PTX.create_job(identity, (Int,); julia_passes=:ports) + @test cpp.config.julia_passes === :cpp + @test ports.config.julia_passes === :ports + # part of the identity of cached artifacts + @test cpp.config != ports.config + @test hash(cpp.config) != hash(ports.config) + # preserved by derived configs, e.g., of nested jobs + @test CompilerConfig(ports.config; toplevel=false).julia_passes === :ports + + @test_throws "Unknown julia_passes=llvm" PTX.create_job(identity, (Int,); julia_passes=:llvm) +end + +@testset "providers" begin + cpp, _ = PTX.create_job(identity, (Int,); julia_passes=:cpp) + ports, _ = PTX.create_job(identity, (Int,); julia_passes=:ports) + @test string(GPUCompiler.julia_pass(cpp, :AllocOptPass)) == "AllocOpt" + @test GPUCompiler.julia_pass(ports, :AllocOptPass) == "jl-alloc-opt" + @test_throws "Unknown Julia pass" GPUCompiler.julia_pass(ports, :NoSuchPass) + @test_throws "Unknown Julia pass" GPUCompiler.julia_pass(cpp, :NoSuchPass) + # passes that LLVM.jl has no constructor for, by their pipeline name + @test GPUCompiler.julia_pass(cpp, :CancellationLoweringPass) == "CancellationLowering" + + # every one of Julia's passes in the pipeline has a port + @test GPUCompiler.julia_pass(ports, :JuliaLICMPass) == "jl-licm" + @test GPUCompiler.julia_pass(ports, :FinalLowerGCPass) == "jl-final-lower-gc" + @test GPUCompiler.julia_pass(ports, :LowerPTLSPass) == "jl-lower-ptls" + @test GPUCompiler.julia_pass(ports, :RemoveJuliaAddrspacesPass) == "jl-remove-addrspaces" + @test GPUCompiler.julia_pass(ports, :CancellationLoweringPass) == "jl-cancellation-lowering" + @test length(GPUCompiler.PASS_PORTS) == 15 + @test all(in(JuliaIR.passes()), values(GPUCompiler.PASS_PORTS)) +end + +@testset "requirements" begin + kernel() = nothing + job, _ = PTX.create_job(kernel, (); julia_passes=:ports) + + if VERSION < v"1.12" + JuliaContext(; opaque_pointers=false) do ctx + @test_throws "requires a context with opaque pointers" GPUCompiler.compile(:llvm, job) + end + end + + # reflection picks a suitable context + @test occursin("define", sprint(io -> GPUCompiler.code_llvm(io, job))) +end + +# Workloads compile with the preference's provider, so that what they cache is used in a +# session with the same preference (test/native/precompile.jl checks that it is). +VERSION >= v"1.11" && precompile_test_harness("precompilation") do load_path + # not named like the package of test/native/precompile.jl, which a worker may have + # loaded already, so that the cache of the workload would not match it + create_standalone(load_path, "PortsCompiler", "native.jl") + write(joinpath(load_path, "PortsWorkload.jl"), :( + module PortsWorkload + import PortsCompiler + using PortsCompiler.GPUCompiler + + kernel(x) = x + 1 + const julia_passes = let + job, _ = PortsCompiler.Native.create_job(kernel, (Int,)) + JuliaContext() do ctx + GPUCompiler.compile(:llvm, job) + end + job.config.julia_passes + end + end) |> string) + Base.compilecache(Base.PkgId("PortsWorkload")) + + mod = Base.require(Base.PkgId("PortsWorkload")) + @test Base.invokelatest(getglobal, mod, :julia_passes) === GPUCompiler.JULIA_PASSES +end + +## differential: the C++ passes and the ports give the same IR + +# Two compilations differ in the counters codegen appends to the names of functions (and +# of the symbols derived from them in assembly), e.g. `julia_kernel_123`. +normalize_ir(ir::String) = + replace(ir, r"(? s"\1\2_\3_N") + +# The IR of a kernel. Without validation, which rejects some of the corpus on some targets +# (e.g. `Float64` on Metal) after optimization. +function compile_with(julia_passes, backend, f, tt; kwargs...) + JuliaContext(; opaque_pointers=true) do ctx + job, _ = backend.create_job(f, tt; julia_passes, validate=false, kwargs...) + ir, meta = GPUCompiler.compile(:llvm, job) + # in the context of the IR, which can live in another LLVM library than Julia's + LLVM.context!(LLVM.context(ir)) do + # Without the names of values and blocks: LLVM makes a name unique with a + # counter that every collision in the function advances, even for values a + # pass erases again, so the suffixes later passes pick (e.g. GVN's `%.pre12`) + # depend on how a pass named its temporaries. + for fn in ir.functions, bb in fn.blocks + for inst in bb.instructions + inst.name = "" + end + bb.name = "" + end + str = string(ir) + LLVM.dispose(ir) + str + end + end +end + +# Differences that don't come from the ports, undone on both sides before comparing again. +# +# FinalLowerGC builds the load of the previous frame and the address of the frame's slot +# for it as two arguments of one C++ call, which compilers evaluate in either order. The +# pair is independent and only feeds the store that follows it; put the address first. +# +# Julia 1.10 to 1.13 zero a GC frame with a volatile `memset`: their FinalLowerGC passes the +# frame's TBAA tag as the volatility flag. The ports zero it as Julia 1.14 does +# (JuliaLang/julia#62061), so later passes may drop the zeroing of slots that are +# overwritten before the frame is used. Such IR is compared after making the C++ frame +# `memset`s non-volatile and cleaning up both sides with the passes that shrink them. +const VOLATILE_FRAME_MEMSET = + r"call void @llvm\.memset\.p0\.i64\(ptr [^,]*%\d+, i8 0, i64 \d+, i1 true\)" + +# whether `load` and `gep` are the independent pair of a frame push that `store` links +function is_frame_push(load, gep, store) + load isa LLVM.LoadInst && !LLVM.volatile(load) && !LLVM.isatomic(load) || return false + gep isa LLVM.GetElementPtrInst && store isa LLVM.StoreInst || return false + LLVM.operands(store)[1] == load && LLVM.operands(store)[2] == gep || return false + return load ∉ LLVM.operands(gep) && gep ∉ LLVM.operands(load) +end + +# whether `inst` is the volatile `memset` zeroing all of a GC frame: an array of pointers +function is_volatile_frame_memset(dl, inst) + inst isa LLVM.CallInst || return false + callee = LLVM.called_operand(inst) + callee isa LLVM.Function && startswith(LLVM.name(callee), "llvm.memset.") || return false + frame, value, len, volatile = LLVM.operands(inst)[1:4] + volatile == LLVM.ConstantInt(LLVM.Int1Type(), true) || return false + value == LLVM.ConstantInt(LLVM.Int8Type(), 0) || return false + frame isa LLVM.AllocaInst && LLVM.alignment(frame) == 16 || return false + T = LLVM.allocated_type(frame) + T isa LLVM.PointerType || + (T isa LLVM.ArrayType && T.element_type isa LLVM.PointerType) || return false + count = LLVM.operands(frame)[1] + count isa LLVM.ConstantInt && len isa LLVM.ConstantInt || return false + return convert(Int, len) == LLVM.abi_size(dl, T) * convert(Int, count) +end + +# `cpp`: whether `ir` comes from the C++ passes; `frame_memsets`: whether to undo the +# zeroing difference (decided on the C++ side) +function canonicalize(ir::String; cpp::Bool, frame_memsets::Bool) + JuliaContext(; opaque_pointers=true) do ctx + mod = parse(LLVM.Module, ir) + dl = LLVM.datalayout(mod) + for fn in mod.functions, bb in fn.blocks + insts = collect(bb.instructions) + for i in 1:length(insts)-2 + load, gep, store = insts[i:i+2] + is_frame_push(load, gep, store) && LLVM.move!(gep, LLVM.before(load)) + end + end + if frame_memsets + if cpp + for fn in mod.functions, bb in fn.blocks, inst in bb.instructions + is_volatile_frame_memset(dl, inst) || continue + LLVM.operands(inst)[4] = LLVM.ConstantInt(LLVM.Int1Type(), false) + end + end + LLVM.run!("function(dse,instcombine)", mod) + for fn in collect(mod.functions) + startswith(LLVM.name(fn), "llvm.memset.") && isempty(LLVM.uses(fn)) && + LLVM.erase!(fn) + end + end + str = string(mod) + LLVM.dispose(mod) + str + end +end + +# the lines that differ between `a` and `b` (a longest-common-subsequence diff) +function line_diff(a::String, b::String) + x, y = split(a, '\n'), split(b, '\n') + lcs = zeros(Int, length(x) + 1, length(y) + 1) + for i in length(x):-1:1, j in length(y):-1:1 + lcs[i, j] = x[i] == y[j] ? lcs[i+1, j+1] + 1 : max(lcs[i+1, j], lcs[i, j+1]) + end + io = IOBuffer() + i = j = 1 + while i <= length(x) || j <= length(y) + if i <= length(x) && j <= length(y) && x[i] == y[j] + i += 1; j += 1 + elseif j > length(y) || (i <= length(x) && lcs[i+1, j] >= lcs[i, j+1]) + println(io, "- ", x[i]); i += 1 + else + println(io, "+ ", y[j]); j += 1 + end + end + String(take!(io)) +end + +function same_ir(backend, f, tt; kwargs...) + cpp = compile_with(:cpp, backend, f, tt; kwargs...) + ports = compile_with(:ports, backend, f, tt; kwargs...) + normalize_ir(cpp) == normalize_ir(ports) && return true + frame_memsets = VERSION < v"1.14-" && occursin(VOLATILE_FRAME_MEMSET, cpp) + cpp = canonicalize(cpp; cpp=true, frame_memsets) + ports = canonicalize(ports; cpp=false, frame_memsets) + normalize_ir(cpp) == normalize_ir(ports) && return true + @error "IR differs between the C++ passes (-) and the ports (+)" f tt kwargs + # printed, as the logger truncates long strings + print(stderr, line_diff(normalize_ir(cpp), normalize_ir(ports))) + return false +end + +module Corpus + mutable struct Box; x::Int; end + struct Pair2; a::Float32; b::Int128; end + + alloc(p, i) = (b = Box(i); unsafe_store!(p, b.x + 1); nothing) + ref(p, i) = (r = Ref(i); unsafe_store!(p, r[] * 2); nothing) + function simd(p, n) + s = 0.0f0 + @simd for i in 1:n + @inbounds s += unsafe_load(p, i) + end + unsafe_store!(p, s) + nothing + end + f16(p::Ptr{Float16}, x::Float16) = (unsafe_store!(p, x * x + Float16(1)); nothing) + throws(p, i) = (i > 10 && throw(BoundsError()); unsafe_store!(p, i); nothing) + i128(p::Ptr{Int128}, x::Int128) = (unsafe_store!(p, x * x); nothing) + tuple(p, t::NTuple{4,Float32}) = (unsafe_store!(p, sum(t)); nothing) + aggregate(p::Ptr{Pair2}, x::Pair2) = (unsafe_store!(p, Pair2(x.a + 1f0, x.b)); nothing) + function loop(p, n) + for i in 1:n, j in 1:n + unsafe_store!(p, unsafe_load(p, i) + j, i) + end + nothing + end + closure(p, x) = (f = y -> y + x; unsafe_store!(p, f(1)); nothing) + complex(p::Ptr{ComplexF32}, z::ComplexF32) = (unsafe_store!(p, z * z + abs(z)); nothing) + math(p::Ptr{Float64}, x::Float64) = + (unsafe_store!(p, sqrt(abs(x)) + muladd(x, x, 1.0) + copysign(x, -1.0)); nothing) + div(p::Ptr{Int}, a::Int, b::Int) = (unsafe_store!(p, Base.div(a, b) + rem(a, b)); nothing) + + # with device pointers, for targets that reject host pointers in kernels + device_alloc(p::Core.LLVMPtr{Int,1}, i) = (b = Box(i); unsafe_store!(p, b.x + 1); nothing) + device_tuple(p::Core.LLVMPtr{Float32,1}, t::NTuple{4,Float32}) = + (unsafe_store!(p, sum(t)); nothing) + function device_simd(p::Core.LLVMPtr{Float32,1}, n::Int32) + s = 0.0f0 + @simd for i in Int32(1):n + @inbounds s += unsafe_load(p, i) + end + unsafe_store!(p, s) + nothing + end + + const DEVICE_KERNELS = [ + (device_alloc, (Core.LLVMPtr{Int,1}, Int)), + (device_tuple, (Core.LLVMPtr{Float32,1}, NTuple{4,Float32})), + (device_simd, (Core.LLVMPtr{Float32,1}, Int32)), + ] + + const KERNELS = [ + (alloc, (Ptr{Int}, Int)), (ref, (Ptr{Int}, Int)), (simd, (Ptr{Float32}, Int)), + (f16, (Ptr{Float16}, Float16)), (throws, (Ptr{Int}, Int)), + (i128, (Ptr{Int128}, Int128)), (tuple, (Ptr{Float32}, NTuple{4,Float32})), + (aggregate, (Ptr{Pair2}, Pair2)), (loop, (Ptr{Int}, Int)), + (closure, (Ptr{Int}, Int)), (complex, (Ptr{ComplexF32}, ComplexF32)), + (math, (Ptr{Float64}, Float64)), (div, (Ptr{Int}, Int, Int)), + ] +end + +@testset "ports run" begin + # JuliaIR.jl records every pass it runs in its ledger + function passes_run(julia_passes, f, tt; backend=PTX, kernel=true) + ledger = tempname() + old = JuliaIR.LEDGER[] + try + JuliaIR.LEDGER[] = ledger + compile_with(julia_passes, backend, f, tt; kernel) + finally + JuliaIR.LEDGER[] = old + end + isfile(ledger) || return Set{String}() + Set(split(line, '\t')[3] for line in eachline(ledger)) + end + + @test isempty(passes_run(:cpp, Corpus.simd, (Ptr{Float32}, Int))) + ran = union(passes_run(:ports, Corpus.simd, (Ptr{Float32}, Int)), + passes_run(:ports, Corpus.alloc, (Ptr{Int}, Int))) + expected = ["jl-alloc-opt", "jl-demote-float16", "jl-gc-invariant-verifier", + "jl-late-lower-gc-frame", "jl-licm", "jl-lower-simdloop", + "jl-propagate-addrspaces", "jl-remove-addrspaces", "jl-remove-ni"] + VERSION < v"1.12.0-DEV.1390" && push!(expected, "jl-combine-muladd") + VERSION >= v"1.13.0-DEV.321" && push!(expected, "jl-expand-atomic-modify") + @test sort!(collect(ran)) == sort!(expected) + + # targets that use Julia's runtime also lower the GC frame and the GC stack getters + ran = passes_run(:ports, Corpus.alloc, (Ptr{Int}, Int); backend=Native, kernel=false) + @test "jl-final-lower-gc" in ran + @test "jl-lower-ptls" in ran + @test ("jl-cancellation-lowering" in ran) == (VERSION >= v"1.15-") +end + +# Compiling with both providers in opaque-pointer contexts, so that the comparison is +# independent of the typed pointers of Julia 1.10 and 1.11 (see below). +describe(kwargs) = join(("$k=$v" for (k, v) in pairs(kwargs)), " ") + +function differential_cases() + cases = Any[] + backends = Any[("PTX", PTX, (;)), ("SPIR-V", SPIRV, (; backend=:llvm)), + ("Metal", Metal, (;)), ("native", Native, (;)), ("BPF", BPF, (;))] + # see runtests.jl. the GCN datalayout comes from the in-process AMDGPU target, which + # Julia's LLVM lacks on macOS; without it, allocas stay in addrspace(0), which LLVM 23's + # verifier rejects for amdgcn. + if !LLVM.is_asserts() && :AMDGPU in LLVM.backends() + push!(backends, ("GCN", GCN, (;))) + end + for (name, backend, kwargs) in backends, + (f, tt) in [Corpus.KERNELS; Corpus.DEVICE_KERNELS], kernel in (false, true) + # Metal kernels can't take host pointers + name == "Metal" && kernel && (f, tt) ∉ Corpus.DEVICE_KERNELS && continue + push!(cases, (name, backend, f, tt, (; kernel, kwargs...))) + end + return cases +end + +@testset "differential" begin + @testset "$name $(nameof(f)) $(describe(kwargs))" for (name, backend, f, tt, kwargs) in differential_cases() + @test same_ir(backend, f, tt; kwargs...) + end + @testset "PTX opt_level=$opt_level" for opt_level in (0, 1, 3) + @test same_ir(PTX, Corpus.simd, (Ptr{Float32}, Int); kernel=true, opt_level) + @test same_ir(PTX, Corpus.alloc, (Ptr{Int}, Int); kernel=true, opt_level) + end +end + +# The ports require opaque pointers, so with them, Julia 1.10 and 1.11 generate code in +# opaque-pointer contexts instead of typed ones. Judge that switch separately, with the C++ +# passes and down to machine code. Not when Julia itself uses opaque pointers +# (`--opaque-pointers`), as its code generator then does not support typed contexts. +@static if VERSION < v"1.12" && Context(LLVM.supports_typed_pointers) +@testset "typed and opaque pointers" begin + function asm_with(opaque_pointers, backend, f, tt; kwargs...) + JuliaContext(; opaque_pointers) do ctx + job, _ = backend.create_job(f, tt; julia_passes=:cpp, validate=false, kwargs...) + asm, meta = GPUCompiler.compile(:asm, job) + LLVM.context!(() -> LLVM.dispose(meta.ir), LLVM.context(meta.ir)) + # without comments, which name basic blocks + replace(normalize_ir(String(asm)), r"//.*" => "", r"[ \t]+" => " ") + end + end + + # the back-ends that generate machine code outside of Julia's LLVM, see runtests.jl + have_ptx = NVPTX_LLVM_Backend_jll.is_available() + have_gcn = AMDGPU_LLVM_Backend_jll.is_available() && !LLVM.is_asserts() + have_spirv = SPIRV_LLVM_Backend_jll.is_available() && + SPIRV_LLVM_Translator_jll.is_available() && SPIRV_Tools_jll.is_available() + + have_ptx && @testset "PTX $(nameof(f)) kernel=$kernel" for (f, tt) in [Corpus.KERNELS; + Corpus.DEVICE_KERNELS], + kernel in (false, true) + if f === Corpus.aggregate && !kernel && Base.datatype_alignment(Int128) > 8 + # Where Julia aligns Int128 to 16 bytes before 1.12 (AArch64, not x86), the + # padding of the aggregate argument, passed by reference, is copied with + # opaque pointers but not with typed ones; only padding bytes differ. + @test asm_with(false, PTX, f, tt; kernel) isa String + @test asm_with(true, PTX, f, tt; kernel) isa String + else + @test asm_with(false, PTX, f, tt; kernel) == asm_with(true, PTX, f, tt; kernel) + end + end + + # elsewhere, the code differs in register allocation (native), debug locations (GCN) + # or the form of instructions (Metal's downgraded IR), so only require that it is + # generated. Metal and SPIR-V kernels can't take host pointers. + cases = Any[] + for (f, tt) in Corpus.KERNELS, kernel in (false, true) + push!(cases, ("native", Native, f, tt, (; kernel))) + have_gcn && push!(cases, ("GCN", GCN, f, tt, (; kernel))) + kernel || push!(cases, ("Metal", Metal, f, tt, (; kernel))) + end + for (f, tt) in Corpus.DEVICE_KERNELS, kernel in (false, true) + push!(cases, ("Metal", Metal, f, tt, (; kernel))) + if have_spirv + push!(cases, ("SPIR-V", SPIRV, f, tt, (; kernel, backend=:llvm))) + push!(cases, ("SPIR-V", SPIRV, f, tt, (; kernel, backend=:khronos))) + end + end + @testset "$name $(nameof(f)) $(describe(kwargs))" for (name, backend, f, tt, kwargs) in cases + @test asm_with(false, backend, f, tt; kwargs...) isa String + @test asm_with(true, backend, f, tt; kwargs...) isa String + end +end +end diff --git a/test/metal.jl b/test/metal.jl index 22b42e5c..98e17307 100644 --- a/test/metal.jl +++ b/test/metal.jl @@ -92,7 +92,7 @@ end Tuple{Core.LLVMPtr{Int32,1}, Core.LLVMPtr{Int32,1}, Int32, Int32, Int}; dump_module=true, kernel=true) end - @test Context() do ctx + @test JuliaContext() do ctx verify(parse(LLVM.Module, ir)) true end @@ -126,7 +126,7 @@ end # (by position, as older Julia versions do not name the parameters) function kernel_parameter_attributes(ir) - Context() do ctx + JuliaContext() do ctx m = parse(LLVM.Module, ir) f = only(filter(f -> startswith(f.name, "_Z6kernel"), collect(m.functions))) [Dict(LLVM.kind(attr) => LLVM.value(attr) @@ -176,7 +176,7 @@ end tt = Tuple{mod.Vec, Int, Int} ir = sprint(io -> Metal.code_llvm(io, mod.kernel, tt; kernel=true, dump_module=true)) - Context() do ctx + JuliaContext() do ctx m = parse(LLVM.Module, ir) f = only(filter(f -> startswith(f.name, "_Z6kernel"), collect(m.functions))) store = only(inst for bb in f.blocks for inst in bb.instructions @@ -657,7 +657,7 @@ end ret void } """ - @dispose ctx=Context() begin + JuliaContext() do ctx mod = parse(LLVM.Module, ir) GPUCompiler.merge_byte_gep_chains!(mod) f = mod.functions["k"] @@ -1417,7 +1417,7 @@ end 5 => [(0, 64, 8), (8, 8, 8)], ) - @dispose ctx=Context() begin + JuliaContext() do ctx mod = parse(LLVM.Module, ir) @test GPUCompiler.legalize_odd_width_integers!(mod) @test !GPUCompiler.legalize_odd_width_integers!(mod) @@ -1475,7 +1475,7 @@ end ret i32 %z } """ - @dispose ctx=Context() begin + JuliaContext() do ctx mod = parse(LLVM.Module, ir) @test GPUCompiler.legalize_odd_width_integers!(mod) @test LLVM.verify(mod) === nothing @@ -3689,7 +3689,10 @@ end source = methodinstance(typeof(mod.kernel), Tuple{Core.LLVMPtr{Float32,1}, Float32}, Base.get_world_counter()) target = MetalCompilerTarget(; macos=v"15", metal=v"3.2", air=v"2.7") - job = CompilerJob(source, CompilerConfig(target, Metal.CompilerParams(); kernel=true)) + # JuliaIR.jl's `AllocOpt` uses the escape analysis of 1.13 on every version, which + # removes the object altogether, so test the lowering with Julia's C++ passes + job = CompilerJob(source, CompilerConfig(target, Metal.CompilerParams(); kernel=true, + julia_passes=:cpp)) @test @filecheck begin @check "alloca" @check "cmpxchg" diff --git a/test/native.jl b/test/native.jl index 93da874b..dc2f5782 100644 --- a/test/native.jl +++ b/test/native.jl @@ -346,6 +346,19 @@ end @test Base.isdispatchtuple(mi.specTypes) end +@testset "argument classification checks the arity" begin + # e.g. codegen passing a `Type{T}` argument that the signature does not key as + # `Core.TypeEgal`, which `classify_arguments` treats as a ghost + job, _ = Native.create_job(identity, (Int,)) + JuliaContext() do ctx + ft = LLVM.FunctionType(LLVM.VoidType(), [LLVM.Int64Type(), LLVM.Int64Type()]) + @test_throws "the function generated for it has 2 parameters" GPUCompiler.classify_arguments(job, ft) + ft = LLVM.FunctionType(LLVM.VoidType(), [LLVM.Int64Type()]) + args = GPUCompiler.classify_arguments(job, ft) + @test [arg.idx for arg in args] == [nothing, 1] + end +end + # on 1.10, `methodinstance` is the generic, allocating lookup VERSION >= v"1.11.0-DEV.1552" && @testset "method instance lookups don't allocate" begin # back-ends look up the method instance on every kernel launch @@ -1206,6 +1219,23 @@ end end end +@static if VERSION >= v"1.15-" +@testset "cancellation points" begin + # from Julia 1.15, `@cancel_check` emits a `julia.cancellation_point()` call, which + # has to be lowered to a `setjmp` into the task (JuliaLang/julia#62604) + mod = @eval module $(gensym()) + f(x) = (Base.@cancel_check; x + 1) + end + + @testset "$julia_passes" for julia_passes in (:cpp, :ports) + @test @filecheck implicit_check_not=["julia.cancellation_point"] begin + @check "setjmp" + Native.code_llvm(mod.f, Tuple{Int}; julia_passes, dump_module=true) + end + end +end +end + @testset "tracked pointers" begin mod = @eval module $(gensym()) function kernel(a) @@ -2193,8 +2223,9 @@ end @testset "merged constant loads" begin # loads through a select or phi of baked relocation slots are folded to the selected - # values, as nothing else folds them after target finalization - fold(ir) = Context() do ctx + # values, as nothing else folds them after target finalization. (a Julia context, + # as `typed_ptrs` describes those: with the ports, they differ from plain ones on 1.11) + fold(ir) = JuliaContext() do ctx mod = parse(LLVM.Module, ir) changed = GPUCompiler.fold_merged_constant_loads!(mod) LLVM.verify(mod) diff --git a/test/runtests.jl b/test/runtests.jl index 44fd5629..1b58dcc5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -8,6 +8,7 @@ using AMDGPU_LLVM_Backend_jll const init_code = quote using GPUCompiler, LLVM, LLVM.IR, LLVM.Build, LLVM.Passes, LLVM.ORC + using JuliaIR # its ledger records the ports that run using SPIRV_LLVM_Backend_jll, SPIRV_LLVM_Translator_jll, SPIRV_Tools_jll using LLVMDowngrader_jll using NVPTX_LLVM_Backend_jll