Skip to content

Commit

Permalink
Add Julia pointer intrinsics.
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt committed Jun 4, 2020
1 parent f957e07 commit 7a6173f
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/interop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ const jlctx = Ref{LLVM.Context}()
include("interop/base.jl")
include("interop/asmcall.jl")
include("interop/passes.jl")
if VERSION >= v"1.5-"
include("interop/pointer.jl")
end

end
96 changes: 96 additions & 0 deletions src/interop/pointer.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# pointer intrinsics

# TODO: can we use constant propagation instead of passing the alignment as a Val?

using Core: LLVMPtr

function tbaa_make_child(name::String, constant::Bool=false; ctx::LLVM.Context=JuliaContext())
tbaa_root = MDNode([MDString("custom_tbaa", ctx)], ctx)
tbaa_struct_type =
MDNode([MDString("custom_tbaa_$name", ctx),
tbaa_root,
LLVM.ConstantInt(0, ctx)], ctx)
tbaa_access_tag =
MDNode([tbaa_struct_type,
tbaa_struct_type,
LLVM.ConstantInt(0, ctx),
LLVM.ConstantInt(constant ? 1 : 0, ctx)], ctx)

return tbaa_access_tag
end

tbaa_addrspace(as) = tbaa_make_child("addrspace($(as))")

@generated function pointerref(ptr::LLVMPtr{T,A}, i::Int, ::Val{align}) where {T,A,align}
sizeof(T) == 0 && return T.instance
eltyp = convert(LLVMType, T)

T_int = convert(LLVMType, Int)
T_ptr = convert(LLVMType, ptr)

T_typed_ptr = LLVM.PointerType(eltyp, A)

# create a function
param_types = [T_ptr, T_int]
llvm_f, _ = create_function(eltyp, param_types)

# generate IR
Builder(JuliaContext()) do builder
entry = BasicBlock(llvm_f, "entry", JuliaContext())
position!(builder, entry)

typed_ptr = bitcast!(builder, parameters(llvm_f)[1], T_typed_ptr)
typed_ptr = gep!(builder, typed_ptr, [parameters(llvm_f)[2]])
ld = load!(builder, typed_ptr)

if A != 0
metadata(ld)[LLVM.MD_tbaa] = tbaa_addrspace(A)
end
alignment!(ld, align)

ret!(builder, ld)
end

call_function(llvm_f, T, Tuple{LLVMPtr{T,A}, Int}, :((ptr, Int(i-one(i)))))
end

@generated function pointerset(ptr::LLVMPtr{T,A}, x::T, i::Int, ::Val{align}) where {T,A,align}
sizeof(T) == 0 && return
eltyp = convert(LLVMType, T)

T_int = convert(LLVMType, Int)
T_ptr = convert(LLVMType, ptr)

T_typed_ptr = LLVM.PointerType(eltyp, A)

# create a function
param_types = [T_ptr, eltyp, T_int]
llvm_f, _ = create_function(LLVM.VoidType(JuliaContext()), param_types)

# generate IR
Builder(JuliaContext()) do builder
entry = BasicBlock(llvm_f, "entry", JuliaContext())
position!(builder, entry)

typed_ptr = bitcast!(builder, parameters(llvm_f)[1], T_typed_ptr)
typed_ptr = gep!(builder, typed_ptr, [parameters(llvm_f)[3]])
val = parameters(llvm_f)[2]
st = store!(builder, val, typed_ptr)

if A != 0
metadata(st)[LLVM.MD_tbaa] = tbaa_addrspace(A)
end
alignment!(st, align)

ret!(builder)
end

call_function(llvm_f, Cvoid, Tuple{LLVMPtr{T,A}, T, Int},
:((ptr, convert(T,x), Int(i-one(i)))))
end

Base.unsafe_load(ptr::Core.LLVMPtr, i::Integer=1, align::Val=Val(1)) =
pointerref(ptr, Int(i), align)

Base.unsafe_store!(ptr::Core.LLVMPtr{T}, x, i::Integer=1, align::Val=Val(1)) where {T} =
pointerset(ptr, convert(T, x), Int(i), align)
55 changes: 55 additions & 0 deletions test/interop.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LLVM.Interop
using InteractiveUtils

@testset "interop" begin

Expand Down Expand Up @@ -126,4 +127,58 @@ end
end
end

VERSION >= v"1.5-" && @testset "pointer" begin

a = Int64[1]
ptr = reinterpret(Core.LLVMPtr{Int64,0}, pointer(a))
@test unsafe_load(ptr) == 1
unsafe_store!(ptr, 2)
@test unsafe_load(ptr) == 2

ir = sprint(io->code_llvm(io, unsafe_load, Tuple{typeof(ptr)}))
@test contains(ir, r"@julia_unsafe_load_\d+\(i8\*\)")
@test contains(ir, r"load i64, i64\* %\d+, align 1")

ir = sprint(io->code_llvm(io, unsafe_load, Tuple{typeof(ptr), Int, Val{4}}))
@test contains(ir, r"load i64, i64\* %\d+, align 4")

@testset "reinterpret(Nothing, nothing)" begin
ptr = reinterpret(Core.LLVMPtr{Nothing,0}, C_NULL)
@test unsafe_load(ptr) === nothing
end

@testset "TBAA" begin
load(ptr) = unsafe_load(ptr)
store(ptr) = unsafe_store!(ptr, 0)

for f in (load, store)
ir = sprint(io->code_llvm(io, f,
Tuple{Core.LLVMPtr{Float32,1}};
dump_module=true, raw=true))
@test occursin("custom_tbaa_addrspace(1)", ir)

# no TBAA on generic pointers
ir = sprint(io->code_llvm(io, f,
Tuple{Core.LLVMPtr{Float32,0}};
dump_module=true, raw=true))
@test !occursin("custom_tbaa", ir)
end
end

@testset "ghost values" begin
@eval struct Singleton end

ir = sprint(io->code_llvm(io, unsafe_load,
Tuple{Core.LLVMPtr{Singleton,0}}))
@test occursin("ret void", ir)
@test unsafe_load(reinterpret(Core.LLVMPtr{Singleton,0}, C_NULL)) === Singleton()

ir = sprint(io->code_llvm(io, unsafe_store!,
Tuple{Core.LLVMPtr{Singleton,0},
Singleton}))
@test !occursin("\bstore\b", ir)
end

end

end

0 comments on commit 7a6173f

Please sign in to comment.