Skip to content

Commit

Permalink
Merge pull request #36 from JuliaLabs/vc/artifact
Browse files Browse the repository at this point in the history
CMake support for mlir-jl-tblgen
  • Loading branch information
vchuravy authored Jan 16, 2024
2 parents 2615761 + 847169e commit 0d6851b
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"
LLVM = "929cbde3-209d-540e-8aea-75f648917ca0"
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
MLIR_jll = "a70bccb4-a5c0-5e2e-a329-e731972457e8"
mlir_jl_tblgen_jll = "3dcf74f3-6bc3-597e-9d13-043388c3118a"

[compat]
CEnum = "0.4"
LLVM = "5"
MLIR_jll = "14,15"
julia = "1.9"
LLVM = "5"
9 changes: 5 additions & 4 deletions deps/Project.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[deps]
LLVM_full_jll = "a3ccf953-465e-511d-b87f-60a6490c289d"

[compat]
LLVM_full_jll = "15"
CMake_jll = "3f4e10e2-61f2-5801-8945-23b9d642d0e6"
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
Scratch = "6c6a2e73-6563-6170-7368-637461726353"
74 changes: 74 additions & 0 deletions deps/build_local.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# build a local version of mlir-jl-tblgen

using Pkg
Pkg.activate(@__DIR__)
Pkg.instantiate()

if haskey(ENV, "GITHUB_ACTIONS")
println("::warning ::Using a locally-built mlir-jl-tblgen; A bump of mlir_jl_tblgen_jll will be required before releasing MLIR.jl.")
end

using Pkg, Scratch, Preferences, Libdl, CMake_jll

MLIR = Base.UUID("bfde9dd4-8f40-4a1e-be09-1475335e1c92")

# get scratch directories
scratch_dir = get_scratch!(MLIR, "build")
isdir(scratch_dir) && rm(scratch_dir; recursive=true)
source_dir = joinpath(@__DIR__, "tblgen")

# get build directory
build_dir = if isempty(ARGS)
mktempdir()
else
ARGS[1]
end
mkpath(build_dir)

# download LLVM
Pkg.activate(; temp=true)
llvm_assertions = try
cglobal((:_ZN4llvm24DisableABIBreakingChecksE, Base.libllvm_path()), Cvoid)
false
catch
true
end
llvm_pkg_version = "$(Base.libllvm_version.major).$(Base.libllvm_version.minor)"
LLVM = if llvm_assertions
Pkg.add(name="LLVM_full_assert_jll", version=llvm_pkg_version)
using LLVM_full_assert_jll
LLVM_full_assert_jll
else
Pkg.add(name="LLVM_full_jll", version=llvm_pkg_version)
using LLVM_full_jll
LLVM_full_jll
end
LLVM_DIR = joinpath(LLVM.artifact_dir, "lib", "cmake", "llvm")
MLIR_DIR = joinpath(LLVM.artifact_dir, "lib", "cmake", "mlir")

# build and install
@info "Building" source_dir scratch_dir build_dir LLVM_DIR MLIR_DIR
cmake() do cmake_path
config_opts = `-DLLVM_ROOT=$(LLVM_DIR) -DMLIR_ROOT=$(MLIR_DIR) -DCMAKE_INSTALL_PREFIX=$(scratch_dir)`
if Sys.iswindows()
# prevent picking up MSVC
config_opts = `$config_opts -G "MSYS Makefiles"`
end
run(`$cmake_path $config_opts -B$(build_dir) -S$(source_dir)`)
run(`$cmake_path --build $(build_dir) --target install`)
end

# discover built binaries
built_libs = filter(readdir(joinpath(scratch_dir, "lib"))) do file
endswith(file, ".$(Libdl.dlext)")
end
lib_path = joinpath(scratch_dir, "lib", only(built_libs))
isfile(lib_path) || error("Could not find library $lib_path in build directory")

# tell LLVM.jl to load our library instead of the default artifact one
set_preferences!(
joinpath(dirname(@__DIR__), "LocalPreferences.toml"),
"mlir_jl_tblgen_jll",
"mlir_jl_tblgen" => lib_path;
force=true,
)
46 changes: 46 additions & 0 deletions deps/tblgen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

cmake_minimum_required(VERSION 3.3)

SET(CMAKE_CXX_FLAGS "-Wall -fPIC -fno-rtti")
cmake_policy(SET CMP0074 NEW)
cmake_policy(SET CMP0077 NEW)

project(mlir-jl-tblgen
LANGUAGES
CXX
C
)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


find_package(LLVM REQUIRED CONFIG)
find_package(MLIR REQUIRED CONFIG)
set(CMAKE_MODULE_PATH
${CMAKE_MODULE_PATH}
${LLVM_CMAKE_DIR}
${MLIR_CMAKE_DIR}
)
include(AddLLVM)
include(HandleLLVMOptions)
include(AddMLIR)

message(STATUS "Found LLVM: ${LLVM_VERSION}")
message(STATUS "Found MLIR: ${MLIR_VERSION}")

add_definitions(${LLVM_DEFINITIONS})
add_definitions(${MLIR_DEFINITIONS})
include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${MLIR_INCLUDE_DIRS})

add_executable(mlir-jl-tblgen
mlir-jl-tblgen.cc
jl-generators.cc)

target_link_libraries(mlir-jl-tblgen PUBLIC
MLIRTableGen
LLVMTableGen
)

install(TARGETS mlir-jl-tblgen DESTINATION bin)
4 changes: 4 additions & 0 deletions deps/tblgen/jl-generators.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ namespace
bool emitOpTableDefs(const llvm::RecordKeeper &recordKeeper,
llvm::raw_ostream &os)
{
#if LLVM_VERSION_MAJOR >= 16
std::vector<llvm::Record *> opdefs = recordKeeper.getAllDerivedDefinitionsIfDefined("Op");
#else
std::vector<llvm::Record *> opdefs = recordKeeper.getAllDerivedDefinitions("Op");
#endif

const char *moduleTemplate = R"(module {0}
Expand Down
1 change: 1 addition & 0 deletions src/MLIR.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module MLIR

import LLVM
import mlir_jl_tblgen_jll

module API
using CEnum
Expand Down

0 comments on commit 0d6851b

Please sign in to comment.