Skip to content

Commit

Permalink
[nix] split buddy-mlir build
Browse files Browse the repository at this point in the history
Signed-off-by: Avimitin <[email protected]>
  • Loading branch information
Avimitin committed Aug 7, 2024
1 parent b72e925 commit 79e413b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 28 deletions.
33 changes: 32 additions & 1 deletion nix/pkgs/buddy-llvm.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
'';
}
49 changes: 22 additions & 27 deletions nix/pkgs/buddy-mlir.nix
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;
Expand Down

0 comments on commit 79e413b

Please sign in to comment.