From c86f79be2e1679feb48ca2c5d2b86fadafb294ea Mon Sep 17 00:00:00 2001 From: Avimitin Date: Tue, 7 Nov 2023 20:02:12 +0800 Subject: [PATCH] [nix] add buddy-mlir derivation Signed-off-by: Avimitin --- README.md | 20 ++++++++++++++++++ nix/buddy-mlir.nix | 51 ++++++++++++++++++++++++++++++++++++++++++++++ nix/overlay.nix | 1 + 3 files changed, 72 insertions(+) create mode 100644 nix/buddy-mlir.nix diff --git a/README.md b/README.md index a2938a11f..4562c7c69 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,26 @@ $ ninja check-mlir check-clang $ ninja $ ninja check-buddy ``` + +### Use nix + +This repository have nix flake support. You can follow the [nix installation instruction](https://nixos.org/manual/nix/stable/installation/installation.html) and enable the [flake features](https://nixos.wiki/wiki/Flakes#Other_Distros.2C_without_Home-Manager) to have nix setup. + +- If you want to contribute to this project: + +```bash +nix develop . +``` + +This will setup a bash shell with `clang`, `clangd`, `cmake`, `ninja`, and other necessary dependencies to build buddy-mlir from source. + +- If you want to use the buddy-mlir bintools + +```bash +nix build .#buddy-mlir +./result/bin/buddy-opt --version +``` + ## Dialects ### Bud Dialect diff --git a/nix/buddy-mlir.nix b/nix/buddy-mlir.nix new file mode 100644 index 000000000..b59d82275 --- /dev/null +++ b/nix/buddy-mlir.nix @@ -0,0 +1,51 @@ +{ cmake, ninja, python3, llvmPackages_16, fetchFromGitHub, libjpeg, libpng, zlib-ng }: +let + # Using git submodule to obtain the llvm source is really slow. + # So here I use tarball to save time from git index. + llvmSrc = fetchFromGitHub { + owner = "llvm"; + repo = "llvm-project"; + rev = "6c59f0e1b0fb56c909ad7c9aad4bde37dc006ae0"; + sha256 = "sha256-bMJJ2q1hSh7m0ewclHOmIe7lOHv110rz/P7D3pw8Uiw"; + }; +in +# Use clang instead of gcc to build +llvmPackages_16.stdenv.mkDerivation { + pname = "buddy-mlir"; + version = "unstable-2023-11-07+rev=38bfd56"; + + srcs = [ + llvmSrc + ../. + ]; + sourceRoot = "llvm-project"; + unpackPhase = '' + sourceArray=($srcs) + cp -r ''${sourceArray[0]} llvm-project + cp -r ''${sourceArray[1]} buddy-mlir + + # Directories copied from nix store are read only + chmod -R u+w llvm-project buddy-mlir + ''; + + # Tablegen in latest commit have bug. See llvm-projects issue #68166 + prePatch = "pushd $NIX_BUILD_TOP/llvm-project"; + patches = [ ./tblgen.patch ]; + postPatch = "popd"; + + nativeBuildInputs = [ cmake ninja python3 llvmPackages_16.bintools libjpeg libpng zlib-ng ]; + + cmakeDir = "../llvm"; + cmakeFlags = [ + "-DCMAKE_BUILD_TYPE=Release" + "-DLLVM_ENABLE_PROJECTS=mlir" + "-DLLVM_TARGETS_TO_BUILD=host;RISCV" + "-DLLVM_ENABLE_ASSERTIONS=ON" + "-DLLVM_USE_LINKER=lld" + + "-DLLVM_EXTERNAL_PROJECTS=buddy-mlir" + "-DLLVM_EXTERNAL_BUDDY_MLIR_SOURCE_DIR=../../buddy-mlir" + ]; + + checkTarget = "check-mlir check-buddy"; +} diff --git a/nix/overlay.nix b/nix/overlay.nix index 2c3c5f08b..19c97fc33 100644 --- a/nix/overlay.nix +++ b/nix/overlay.nix @@ -2,4 +2,5 @@ final: prev: { # Add an alias here can help future migration llvmPkgs = final.llvmPackages_16; + buddy-mlir = final.callPackage ./buddy-mlir.nix { }; }