Skip to content

Commit

Permalink
[nix] add standalone LLVM build for buddy-mlir
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 755f0f1 commit 3b7eba5
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 32 deletions.
76 changes: 76 additions & 0 deletions nix/pkgs/buddy-llvm.nix
Original file line number Diff line number Diff line change
@@ -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
'';
}
50 changes: 18 additions & 32 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 @@ -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;
}

0 comments on commit 3b7eba5

Please sign in to comment.