Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CPU execution #3

Closed
maleadt opened this issue Apr 13, 2020 · 5 comments
Closed

CPU execution #3

maleadt opened this issue Apr 13, 2020 · 5 comments
Labels
enhancement New feature or request native Stuff about the native back-end.

Comments

@maleadt
Copy link
Member

maleadt commented Apr 13, 2020

It would be useful if we could execute code on the CPU, both for testing and to extend the usability of this package. Regular execution should be pretty easy:

using GPUCompiler, LLVM


## runtime implementation

module NativeRuntime

# FIXME: actually implement these
signal_exception() = return
malloc(sz) = return
report_oom(sz) = return
report_exception(ex) = return
report_exception_name(ex) = return
report_exception_frame(idx, func, file, line) = return
end

## target

struct NativeCompilerTarget <: AbstractCompilerTarget
end

GPUCompiler.runtime_module(::NativeCompilerTarget) = NativeRuntime

GPUCompiler.llvm_triple(::NativeCompilerTarget) = Sys.MACHINE


## job

struct NativeCompilerJob <: AbstractCompilerJob
    target::NativeCompilerTarget
    source::FunctionSpec
end

Base.similar(job::NativeCompilerJob, source::FunctionSpec) =
    NativeCompilerJob(job.target, source)

GPUCompiler.target(job::NativeCompilerJob) = job.target
GPUCompiler.source(job::NativeCompilerJob) = job.source

GPUCompiler.runtime_slug(::AbstractCompilerJob) = "native"


## main

function kernel()
end

function run(mod::LLVM.Module, entry::LLVM.Function)
    res_jl = 0.0
    LLVM.JIT(mod) do engine
        f = LLVM.functions(engine)[LLVM.name(entry)]
        res = LLVM.run(engine, f)
        LLVM.dispose(res)
    end
    return
end

function main()
    target = NativeCompilerTarget()
    source = FunctionSpec(kernel)
    job = NativeCompilerJob(target,source)

    mod, entry = GPUCompiler.compile(:llvm, job)

    run(mod, entry)
end

Left to implement is the runtime, we could print by e.g. linking the C runtime and calling printf. However, it would be vastly more useful if we could actually reuse the full Julia runtime. This should be possible with the LLVM ORC JIT, which supports looking external functions and globals. https://www.doof.me.uk/2017/05/11/using-orc-with-llvms-c-api/

@maleadt maleadt added enhancement New feature or request native Stuff about the native back-end. labels May 27, 2020
@MasonProtter
Copy link

Does the merging of JuliaLang/julia#41936 make this any easier?

@maleadt
Copy link
Member Author

maleadt commented Oct 14, 2021

Not really. But we do actually have CPU execution nowadays with what @vchuravy implemented, right? See

@testset "LazyCodegen" begin
import .LazyCodegen: call_delayed
f(A) = (A[] += 42; nothing)
global flag = [0]
function caller()
call_delayed(f, flag::Vector{Int})
end
@test caller() === nothing
@test flag[] == 42
ir = sprint(io->native_code_llvm(io, caller, Tuple{}, dump_module=true))
@test occursin(r"add i64 %\d+, 42", ir)
# NOTE: can't just look for `jl_f` here, since it may be inlined and optimized away.
add(x, y) = x+y
function call_add(x, y)
call_delayed(add, x, y)
end
@test call_add(1, 3) == 4
incr(r) = r[] += 1
function call_incr(r)
call_delayed(incr, r)
end
r = Ref{Int}(0)
@test call_incr(r) == 1
@test r[] == 1
function call_real(c)
call_delayed(real, c)
end
@test call_real(1.0+im) == 1.0
# Test ABI removal
ir = sprint(io->native_code_llvm(io, call_real, Tuple{ComplexF64}))
@test !occursin("alloca", ir)
ghostly_identity(x, y) = y
@test call_delayed(ghostly_identity, nothing, 1) == 1
# tests struct return
@test call_delayed(complex, 1.0, 2.0) == 1.0+2.0im
end
end
. Those APIs could be made a little more user friendly though.

@vchuravy
Copy link
Member

Yeah we do have that, but it is not really meant for users, more as the CI definition necessary for Enzyme.jl

@MasonProtter
Copy link

Oh interesting, thanks! I played around a bit with this trying to write the result of the codegen to a .so file like what was attempted with StaticCompiler.jl but I didn’t have much luck.

@maleadt
Copy link
Member Author

maleadt commented Mar 15, 2023

We have this now.

@maleadt maleadt closed this as completed Mar 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request native Stuff about the native back-end.
Projects
None yet
Development

No branches or pull requests

3 participants