Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,5 @@
does: an unordered load of the binding's value (with `UndefVarError` from the runtime)
and, for globals of type `Any`, a release store and the binding's write barrier; also
for `getglobal` and `setglobal!` with constant arguments.
- `ccall` in the `llvmcall` convention: calls of LLVM intrinsics (also ones LLVM doesn't
know, left to back ends and validation), as GPU code uses them.
1 change: 1 addition & 0 deletions docs/src/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ JuliaCodegen.specsig_abi
JuliaCodegen.emit_specsig_wrapper!
JuliaCodegen.emit_specsig_call!
JuliaCodegen.emit_cfunction
JuliaCodegen.emit_llvmcall_cc
JuliaCodegen.Entry
JuliaCodegen.literal_global
JuliaCodegen.literal_objects
Expand Down
50 changes: 50 additions & 0 deletions src/ccall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ function emit_foreigncall(fc::FnCtx, ex::Expr, @nospecialize(typ))
unsupported("a ccall with a cancellation handler")
reset_safe = cc isa Tuple && length(cc) >= 5 && cc[5] === true
cc = cc isa Tuple ? cc[1] : cc
cc === :llvmcall && return emit_llvmcall_cc(fc, ex, typ)
cc in (:ccall, :cdecl, :c) || unsupported("the calling convention ", cc)
argtypes = collect(Any, argtypes)
target = static_ccall_target(fc, name)
Expand Down Expand Up @@ -560,6 +561,55 @@ function emit_foreigncall(fc::FnCtx, ex::Expr, @nospecialize(typ))
return ccall_result(fc, r, rt)
end

"""
JuliaCodegen.emit_llvmcall_cc(fc, ex, typ) -> CGVal

`ccall("llvm.name", llvmcall, rt, argtypes, args...)`: a call of the LLVM function `name`
(an intrinsic, also one that LLVM doesn't know, which back ends and validation report),
declared with the LLVM types of `llvmcall` (tuples of `VecElement`s are vectors), as
native code calls it.
"""
function emit_llvmcall_cc(fc::FnCtx, ex::Expr, @nospecialize(typ))
b = fc.builder
name, rt, argtypes, nreq = ex.args[1:4]
target = static_ccall_target(fc, name)
target isa Tuple && length(target) == 1 && (target = target[1])
target isa Union{Symbol,String} || unsupported("an llvmcall of a name not known at compile time")
fname = String(target)
# (native code compiles anything but an intrinsic to an error: "llvmcall only supports
# intrinsic calls")
startswith(fname, "llvm.") || unsupported("an llvmcall of the non-intrinsic ", fname)
nargs = length(argtypes)
(nreq == 0 || nreq == nargs) || unsupported("a variadic llvmcall")
# (native code mangles the base name of an overloaded intrinsic, e.g. `llvm.powi`, from
# the signature)
intr = tryparse(LLVM.Intrinsic, fname)
intr !== nothing && LLVM.isoverloaded(intr) && LLVM.name(intr) == fname &&
unsupported("an llvmcall of the unmangled overloaded intrinsic ", fname)
mod = fc.cgm.mod
lrt = llvmcall_type(fc, rt)
lats = LLVM.LLVMType[llvmcall_type(fc, T) for T in argtypes]
ft = LLVM.FunctionType(lrt, lats)
f = if haskey(LLVM.functions(mod), fname)
g = LLVM.functions(mod)[fname]
LLVM.function_type(g) == ft || unsupported("an llvmcall of ", fname, " with another signature")
g
else
LLVM.Function(mod, fname, ft)
end
ops = LLVM.Value[]
for (i, T) in enumerate(argtypes)
v = convert_repr(fc, operand(fc, ex.args[5+i]), T)
v.kind === Bottom && return v
v.kind === Inline || unsupported("an llvmcall argument of type ", T)
push!(ops, LLVM.value_type(v.V) == lats[i] ? v.V : reinterpret_value(fc, v.V, lats[i]))
end
r = LLVM.call!(b, ft, f, ops)
lrt isa LLVM.VoidType && return ghost(Nothing, nothing)
rty = value_type(fc, rt)
return inline(rt, LLVM.value_type(r) == rty ? r : reinterpret_value(fc, r, rty))
end

# Runtime functions that native code emits inline (`emit_ccall`'s special functions):
# calling them would make their argument escape (e.g. the `Ref` of `pointer_from_objref`).
function emit_special_ccall(fc::FnCtx, name::Symbol, ex::Expr, @nospecialize(rt), argtypes)
Expand Down
26 changes: 26 additions & 0 deletions test/ccall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ if LIBCABI !== nothing
end
end

const CC_V4 = NTuple{4,VecElement{Float32}}
cc_sqrt(x) = ccall("llvm.sqrt.f64", llvmcall, Float64, (Float64,), x)
cc_ctpop(x) = ccall("llvm.ctpop.i64", llvmcall, Int64, (Int64,), x)
cc_fma(a, b, c) = ccall("llvm.fma.v4f32", llvmcall, CC_V4, (CC_V4, CC_V4, CC_V4), a, b, c)
cc_donothing(x) = (ccall("llvm.donothing", llvmcall, Cvoid, ()); x)
cc_unknown() = (ccall("llvm.jctest.nonexistent", llvmcall, Cvoid, ()); nothing)
cc_not_intrinsic(x) = ccall("jctest_function", llvmcall, Int, (Int,), x)

@testset "ccall" begin

@testset "calls" begin
Expand Down Expand Up @@ -109,4 +117,22 @@ end
end
end

@testset "llvmcall convention" begin
@cgtest cc_sqrt(2.25)
@cgtest cc_ctpop(0b101101)
v(x...) = map(VecElement, Float32.(x))
@cgtest cc_fma(v(1, 2, 3, 4), v(2, 2, 2, 2), v(1, 1, 1, 1))
@cgtest cc_donothing(3)
@test @filecheck begin
@check "call double @llvm.sqrt.f64(double"
test_ir(cc_sqrt, (Float64,))
end
# an intrinsic LLVM doesn't know is declared, and left to back ends and validation
@test @filecheck begin
@check "declare void @llvm.jctest.nonexistent()"
test_module_ir(cc_unknown, (); triple=JuliaCodegen.host_target().triple)
end
@test_throws JuliaCodegen.Unsupported test_ir(cc_not_intrinsic, (Int,))
end

end
Loading