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

Add ExecutionEngine test #10

Merged
merged 7 commits into from
May 16, 2023
Merged
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
479 changes: 252 additions & 227 deletions lib/14/libMLIR_h.jl

Large diffs are not rendered by default.

485 changes: 240 additions & 245 deletions lib/15/libMLIR_h.jl

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion res/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
BinaryBuilderBase = "7f725544-6523-48cd-82d1-3fa08ff4056e"
Clang = "40e3b903-d033-50b4-a0cc-940c62c95e31"
Clang_jll = "0ee61d77-7f21-5576-8119-9fcc46b10100"
LLVM_full_jll = "a3ccf953-465e-511d-b87f-60a6490c289d"
15 changes: 9 additions & 6 deletions res/wrap.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# generate LLVM wrappers

using LLVM_full_jll

using Clang.Generators

cd(@__DIR__)
options = load_options(joinpath(@__DIR__, "wrap.toml"))

@add_def off_t
@add_def MlirTypesCallback

import Pkg
import BinaryBuilderBase: PkgSpec, Prefix, temp_prefix, setup_dependencies, cleanup_dependencies, destdir
Expand All @@ -16,6 +15,9 @@ const dependencies = PkgSpec[PkgSpec(; name = "LLVM_full_jll")]

const libdir = joinpath(@__DIR__, "..", "lib")

function rewrite!(dag::ExprDAG)
end

for (llvm_version, julia_version) in ((v"14.0.5", v"1.9"),
(v"15.0.6", v"1.10"))
@info "Generating..." llvm_version julia_version
Expand All @@ -32,18 +34,19 @@ for (llvm_version, julia_version) in ((v"14.0.5", v"1.9"),
options["general"]["output_file_path"] = output_file_path

include_dir = joinpath(destdir(prefix, platform), "include")
libclang_header_dir = joinpath(include_dir, "mlir-c")
libmlir_header_dir = joinpath(include_dir, "mlir-c")
args = Generators.get_default_args()
push!(args, "-I$include_dir")
push!(args, "-x")
push!(args, "c++")

headers = detect_headers(libclang_header_dir, args)
filter!(h->!endswith(h, "Python/Interop.h"), headers)
headers = detect_headers(libmlir_header_dir, args, Dict(), endswith("Python/Interop.h"))
ctx = create_context(headers, args, options)

# build without printing so we can do custom rewriting
build!(ctx, BUILDSTAGE_NO_PRINTING)

# rewrite!(ctx.dag)
rewrite!(ctx.dag)

# print
build!(ctx, BUILDSTAGE_PRINTING_ONLY)
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"
99 changes: 99 additions & 0 deletions test/executionengine.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using MLIR
using Test

import LLVM

function registerAllUpstreamDialects!(ctx)
if LLVM.version() >= v"15"
registry = MLIR.API.mlirDialectRegistryCreate()
MLIR.API.mlirRegisterAllDialects(registry)
MLIR.API.mlirContextAppendDialectRegistry(ctx, registry)
MLIR.API.mlirDialectRegistryDestroy(registry)
else
MLIR.API.mlirRegisterAllDialects(ctx)
end

return nothing
end

# TODO: Fix for LLVM 15
function lowerModuleToLLVM(ctx, mod)
pm = MLIR.API.mlirPassManagerCreate(ctx)
if LLVM.version() >= v"15"
op = MLIR.API.mlirStringRefCreateFromCString("func.func")
else
op = MLIR.API.mlirStringRefCreateFromCString("builtin.func")
end
opm = MLIR.API.mlirPassManagerGetNestedUnder(pm, op)
if LLVM.version() >= v"15"
MLIR.API.mlirPassManagerAddOwnedPass(pm,
MLIR.API.mlirCreateConversionConvertFuncToLLVM()
)
else
MLIR.API.mlirPassManagerAddOwnedPass(pm,
MLIR.API.mlirCreateConversionConvertStandardToLLVM()
)
end
MLIR.API.mlirOpPassManagerAddOwnedPass(opm,
MLIR.API.mlirCreateConversionConvertArithmeticToLLVM()
)
status = MLIR.API.mlirPassManagerRun(pm, mod)
# undefined symbol: mlirLogicalResultIsFailure
if status.value == 0
error("Unexpected failure running pass failure")
end
MLIR.API.mlirPassManagerDestroy(pm)
end

ctx = MLIR.API.mlirContextCreate()
registerAllUpstreamDialects!(ctx)

if LLVM.version() >= v"15"
ir = MLIR.API.mlirStringRefCreateFromCString(
"""
module {
func.func @add(%arg0 : i32) -> i32 attributes { llvm.emit_c_interface } {
%res = arith.addi %arg0, %arg0 : i32
return %res : i32
}
}
"""
)
else
ir = MLIR.API.mlirStringRefCreateFromCString(
"""
module {
func @add(%arg0 : i32) -> i32 attributes { llvm.emit_c_interface } {
%res = arith.addi %arg0, %arg0 : i32
return %res : i32
}
}
"""
)
end
mod = MLIR.API.mlirModuleCreateParse(ctx, ir)
lowerModuleToLLVM(ctx, mod)

MLIR.API.mlirRegisterAllLLVMTranslations(ctx)

# TODO add C-API for translateModuleToLLVMIR

jit = MLIR.API.mlirExecutionEngineCreate(
mod, #=optLevel=# 2, #=numPaths=# 0, #=sharedLibPaths=# C_NULL)

if jit == C_NULL
error("Execution engine creation failed")
end

addr = MLIR.API.mlirExecutionEngineLookup(jit,
MLIR.API.mlirStringRefCreateFromCString("add"))

if addr == C_NULL
error("Lookup failed")
end

@test ccall(addr, Cint, (Cint,), 42) == 84

MLIR.API.mlirExecutionEngineDestroy(jit)
MLIR.API.mlirModuleDestroy(mod)
MLIR.API.mlirContextDestroy(ctx)
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MLIR
using Test

include("examples.jl")
include("examples.jl")
include("executionengine.jl")