diff --git a/NEWS.md b/NEWS.md index 23f5ec9..faaa081 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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. diff --git a/docs/src/internals.md b/docs/src/internals.md index 0ff894f..8b241e8 100644 --- a/docs/src/internals.md +++ b/docs/src/internals.md @@ -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 diff --git a/src/ccall.jl b/src/ccall.jl index 2b1f107..ca1d2b0 100644 --- a/src/ccall.jl +++ b/src/ccall.jl @@ -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) @@ -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) diff --git a/test/ccall.jl b/test/ccall.jl index bb4a887..d421c6d 100644 --- a/test/ccall.jl +++ b/test/ccall.jl @@ -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 @@ -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