Skip to content

Commit

Permalink
Support bitcode inputs. (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt authored Apr 29, 2024
1 parent bbd3b1b commit a0150d8
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 20 deletions.
1 change: 0 additions & 1 deletion res/wrap.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ output_file_path = "../src/libnvvm.jl"

[codegen]
use_ccall_macro = true
always_NUL_terminated_string = true
18 changes: 9 additions & 9 deletions src/libnvvm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using CEnum
end

function nvvmGetErrorString(result)
@ccall libnvvm.nvvmGetErrorString(result::nvvmResult)::Cstring
@ccall libnvvm.nvvmGetErrorString(result::nvvmResult)::Ptr{Cchar}
end

function nvvmVersion(major, minor)
Expand All @@ -39,23 +39,23 @@ function nvvmDestroyProgram(prog)
end

function nvvmAddModuleToProgram(prog, buffer, size, name)
@ccall libnvvm.nvvmAddModuleToProgram(prog::nvvmProgram, buffer::Cstring, size::Csize_t,
name::Cstring)::nvvmResult
@ccall libnvvm.nvvmAddModuleToProgram(prog::nvvmProgram, buffer::Ptr{Cchar},
size::Csize_t, name::Ptr{Cchar})::nvvmResult
end

function nvvmLazyAddModuleToProgram(prog, buffer, size, name)
@ccall libnvvm.nvvmLazyAddModuleToProgram(prog::nvvmProgram, buffer::Cstring,
size::Csize_t, name::Cstring)::nvvmResult
@ccall libnvvm.nvvmLazyAddModuleToProgram(prog::nvvmProgram, buffer::Ptr{Cchar},
size::Csize_t, name::Ptr{Cchar})::nvvmResult
end

function nvvmCompileProgram(prog, numOptions, options)
@ccall libnvvm.nvvmCompileProgram(prog::nvvmProgram, numOptions::Cint,
options::Ptr{Cstring})::nvvmResult
options::Ptr{Ptr{Cchar}})::nvvmResult
end

function nvvmVerifyProgram(prog, numOptions, options)
@ccall libnvvm.nvvmVerifyProgram(prog::nvvmProgram, numOptions::Cint,
options::Ptr{Cstring})::nvvmResult
options::Ptr{Ptr{Cchar}})::nvvmResult
end

function nvvmGetCompiledResultSize(prog, bufferSizeRet)
Expand All @@ -64,7 +64,7 @@ function nvvmGetCompiledResultSize(prog, bufferSizeRet)
end

function nvvmGetCompiledResult(prog, buffer)
@ccall libnvvm.nvvmGetCompiledResult(prog::nvvmProgram, buffer::Cstring)::nvvmResult
@ccall libnvvm.nvvmGetCompiledResult(prog::nvvmProgram, buffer::Ptr{Cchar})::nvvmResult
end

function nvvmGetProgramLogSize(prog, bufferSizeRet)
Expand All @@ -73,5 +73,5 @@ function nvvmGetProgramLogSize(prog, bufferSizeRet)
end

function nvvmGetProgramLog(prog, buffer)
@ccall libnvvm.nvvmGetProgramLog(prog::nvvmProgram, buffer::Cstring)::nvvmResult
@ccall libnvvm.nvvmGetProgramLog(prog::nvvmProgram, buffer::Ptr{Cchar})::nvvmResult
end
12 changes: 7 additions & 5 deletions src/wrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ function unsafe_destroy!(prog::Program)
end

"""
add!(prog::Program, mod::AbstractString, [name]; lazy=false)
add!(prog::Program, mod, [name]; lazy=false)
Add the NVVM IR module `mod` to the NVVM program `prog`. If `name` is specified,
it will be used as the name of the module. If `lazy` is set, only symbols that are
required by non-lazy modules will be included in the linked IR program.
Add the NVVM IR module `mod` to the NVVM program `prog`. The module can be provided as a
string containing textual IR, or a byte vector containing bigcode. If `name` is specified,
it will be used as the name of the module. If `lazy` is set, only symbols that are required
by non-lazy modules will be included in the linked IR program.
"""
function add!(prog::Program, mod::AbstractString, name=nothing; lazy::Bool=false)
function add!(prog::Program, mod::Union{AbstractString,Vector{UInt8},Vector{Int8}},
name=nothing; lazy::Bool=false)
if lazy
check(nvvmLazyAddModuleToProgram(prog.handle, mod, sizeof(mod), something(name, C_NULL)))
else
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[deps]
LLVM = "929cbde3-209d-540e-8aea-75f648917ca0"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
24 changes: 19 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NVVM, Test
import LLVM

@testset "NVVM" begin

Expand All @@ -21,12 +22,25 @@ dummy_ir = """
!nvvmir.version = !{!1}
!1 = !{i32 2, i32 0}"""

dummy_bitcode = LLVM.Context() do ctx
mod = parse(LLVM.Module, dummy_ir)
convert(Vector{UInt8}, mod)
end

@testset "smoke test" begin
prog = Program()
add!(prog, dummy_ir)
verify(prog)
ptx = compile(prog)
@test contains(ptx, ".visible .entry kernel")
let prog = Program()
add!(prog, dummy_ir)
verify(prog)
ptx = compile(prog)
@test contains(ptx, ".visible .entry kernel")
end

let prog = Program()
add!(prog, dummy_bitcode)
verify(prog)
ptx = compile(prog)
@test contains(ptx, ".visible .entry kernel")
end
end

@testset "errors" begin
Expand Down

0 comments on commit a0150d8

Please sign in to comment.