diff --git a/nix/pkgs/buddy-llvm.nix b/nix/pkgs/buddy-llvm.nix new file mode 100644 index 000000000..af5bc1c86 --- /dev/null +++ b/nix/pkgs/buddy-llvm.nix @@ -0,0 +1,76 @@ +{ stdenv +, cmake +, ninja +, python3 +, fetchFromGitHub +}: + +let + pythonEnv = python3.withPackages (ps: [ + ps.numpy + ps.pybind11 + ps.pyyaml + ps.ml-dtypes + ]); +in +stdenv.mkDerivation rec { + name = "llvm-for-buddy-mlir"; + version = "6c59f0e1b0fb56c909ad7c9aad4bde37dc006ae0"; + src = fetchFromGitHub { + owner = "llvm"; + repo = "llvm-project"; + rev = version; + hash = "sha256-bMJJ2q1hSh7m0ewclHOmIe7lOHv110rz/P7D3pw8Uiw="; + }; + + requiredSystemFeatures = [ "big-parallel" ]; + + propagatedBuildInputs = [ + pythonEnv + ]; + + nativeBuildInputs = [ + cmake + ninja + ]; + + cmakeDir = "../llvm"; + cmakeFlags = [ + "-DLLVM_ENABLE_PROJECTS=mlir" + "-DLLVM_TARGETS_TO_BUILD=host;RISCV" + "-DLLVM_ENABLE_ASSERTIONS=ON" + "-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 abdae657a..f2f899cea 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"; @@ -17,38 +18,23 @@ stdenv.mkDerivation { src = fetchFromGitHub { owner = "buddy-compiler"; repo = "buddy-mlir"; - rev = "be2811cde9158faa0c08ad90801edf5ebfcf8e0e"; - hash = "sha256-5ZFqDZZjMbVoqbEZ1mt1RXY2oR+VSQ6wJ1dQJCGrRC4="; + rev = "d7d90a488ac0d6fc1e700e932f842c7b2bcad816"; + hash = "sha256-MhykCa6Z7Z8PpAlNh+vMuWYEOZZDyWhtMzMnFlNbGIk="; }; - 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" ]; - passthru.llvm = buddy-llvm; - # No need to do check, and it also takes too much time to finish. doCheck = false; }