From 272f4e8c8a9d77b547862fe557c85bdfce7864f5 Mon Sep 17 00:00:00 2001 From: Avimitin Date: Wed, 7 Aug 2024 03:41:07 +0800 Subject: [PATCH] [nix] split buddy-mlir build Signed-off-by: Avimitin --- nix/pkgs/buddy-llvm.nix | 33 ++++++++++++++++++++++++++- nix/pkgs/buddy-mlir.nix | 49 ++++++++++++++++++----------------------- 2 files changed, 54 insertions(+), 28 deletions(-) diff --git a/nix/pkgs/buddy-llvm.nix b/nix/pkgs/buddy-llvm.nix index 4cbf66db77..af5bc1c867 100644 --- a/nix/pkgs/buddy-llvm.nix +++ b/nix/pkgs/buddy-llvm.nix @@ -39,7 +39,38 @@ stdenv.mkDerivation rec { "-DLLVM_ENABLE_PROJECTS=mlir" "-DLLVM_TARGETS_TO_BUILD=host;RISCV" "-DLLVM_ENABLE_ASSERTIONS=ON" - "-DCMAKE_BUILD_TYPE=RELEASE" + "-DCMAKE_BUILD_TYPE=Release" + # required for MLIR python binding "-DMLIR_ENABLE_BINDINGS_PYTHON=ON" + # required for not, FileCheck... + "-DLLVM_INSTALL_UTILS=ON" ]; + + outputs = [ "out" "lib" "dev" ]; + + postInstall = '' + # buddy-mlir have custom RVV backend that required LLVM backend, + # and those LLVM backend headers require this config.h header file. + # However for LLVM, this config.h is meant to be used on build phase only, + # so it will not be installed for cmake install. + # We have to do some hack + cp -v "include/llvm/Config/config.h" "$dev/include/llvm/Config/config.h" + + # move llvm-config to $dev to resolve a circular dependency + moveToOutput "bin/llvm-config*" "$dev" + + # move all lib files to $lib except lib/cmake + moveToOutput "lib" "$lib" + moveToOutput "lib/cmake" "$dev" + + # patch configuration files so each path points to the new $lib or $dev paths + substituteInPlace "$dev/lib/cmake/llvm/LLVMConfig.cmake" \ + --replace 'set(LLVM_BINARY_DIR "''${LLVM_INSTALL_PREFIX}")' 'set(LLVM_BINARY_DIR "'"$lib"'")' + substituteInPlace \ + "$dev/lib/cmake/llvm/LLVMExports-release.cmake" \ + "$dev/lib/cmake/mlir/MLIRTargets-release.cmake" \ + --replace "\''${_IMPORT_PREFIX}/lib/lib" "$lib/lib/lib" \ + --replace "\''${_IMPORT_PREFIX}/lib/objects-Release" "$lib/lib/objects-Release" \ + --replace "$out/bin/llvm-config" "$dev/bin/llvm-config" # patch path for llvm-config + ''; } diff --git a/nix/pkgs/buddy-mlir.nix b/nix/pkgs/buddy-mlir.nix index abdae657a8..7141f678b4 100644 --- a/nix/pkgs/buddy-mlir.nix +++ b/nix/pkgs/buddy-mlir.nix @@ -1,14 +1,15 @@ -{ cmake, ninja, python3, llvmPackages_17, fetchFromGitHub, fetchpatch }: +{ cmake +, ninja +, llvmPackages_17 +, fetchFromGitHub +, fetchpatch +, callPackage +}: let stdenv = llvmPackages_17.stdenv; bintools = llvmPackages_17.bintools; - buddy-llvm = fetchFromGitHub { - owner = "llvm"; - repo = "llvm-project"; - rev = "6c59f0e1b0fb56c909ad7c9aad4bde37dc006ae0"; - hash = "sha256-bMJJ2q1hSh7m0ewclHOmIe7lOHv110rz/P7D3pw8Uiw="; - }; + buddy-llvm = callPackage ./buddy-llvm.nix { inherit stdenv; }; in stdenv.mkDerivation { pname = "buddy-mlir"; @@ -20,31 +21,25 @@ stdenv.mkDerivation { rev = "be2811cde9158faa0c08ad90801edf5ebfcf8e0e"; hash = "sha256-5ZFqDZZjMbVoqbEZ1mt1RXY2oR+VSQ6wJ1dQJCGrRC4="; }; - unpackPhase = '' - # We can only use one-step build now...buddy-mlir have bad build system that always - # assume the build artifacts are inside of the LLVM sources. And it also relies on - # some LLVM Cpp source that are configured to be installed by default. - cp -r ${buddy-llvm} llvm-project - cp -r $src buddy-mlir - - # Directories copied from nix store are read only - chmod -R u+w llvm-project buddy-mlir - ''; - sourceRoot = "llvm-project"; - nativeBuildInputs = [ cmake ninja python3 bintools ]; + nativeBuildInputs = [ cmake ninja bintools ]; + buildInputs = [ + buddy-llvm + ]; - cmakeDir = "../llvm"; cmakeFlags = [ + "-DMLIR_DIR=${buddy-llvm.dev}/lib/cmake/mlir" + "-DLLVM_DIR=${buddy-llvm.dev}/lib/cmake/llvm" + "-DLLVM_MAIN_SRC_DIR=${buddy-llvm.src}/llvm" + "-DBUDDY_MLIR_ENABLE_PYTHON_PACKAGES=ON" "-DCMAKE_BUILD_TYPE=Release" - "-DLLVM_INSTALL_UTILS=ON" - "-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" + patches = [ + (fetchpatch { + url = "https://github.com/buddy-compiler/buddy-mlir/pull/359.patch"; + hash = "sha256-j75npHWrCVUcKFfm+JGbs9/CkS51Uxi3AfI5x8o8jkc="; + }) ]; passthru.llvm = buddy-llvm;