rocmPackages.llvm: reduce closure size by moving clang static libs to dev

and then dealing with the consequences
This commit is contained in:
Luna Nova
2025-10-02 08:12:36 -07:00
parent bd0292cf6b
commit 6fb5f7ac2e
2 changed files with 113 additions and 77 deletions
@@ -417,6 +417,16 @@ let
};
}
// lib.optionalAttrs config.allowAliases {
rocm-merged-llvm = throw ''
'rocm-merged-llvm' has been removed.
For 'libllvm' or 'libclang' use 'rocmPackages.llvm.libllvm/clang'.
For a ROCm compiler toolchain use 'rocmPackages.rocm-toolchain'.
If a package uses '$<TARGET_FILE:clang>' in CMake from 'libclang'
it may be necessary to convince it to use 'rocm-toolchain' instead.
'rocm-merged-llvm' avoided this at the cost of significantly bloating closure
size.
''; # Added 2025-09-30
hsa-amd-aqlprofile-bin = lib.warn ''
'hsa-amd-aqlprofile-bin' has been replaced by 'aqlprofile'.
'' self.aqlprofile; # Added 2025-08-27
+103 -77
View File
@@ -4,6 +4,7 @@
# LLVM version closest to ROCm fork to override
llvmPackages_19,
overrideCC,
lndir,
rocm-device-libs,
fetchFromGitHub,
runCommand,
@@ -112,14 +113,6 @@ let
ln -s $out $out/x86_64-unknown-linux-gnu
'';
};
usefulOutputs =
drv:
builtins.filter (x: x != null) [
drv
(drv.lib or null)
(drv.dev or null)
];
listUsefulOutputs = builtins.concatMap usefulOutputs;
llvmSrc = fetchFromGitHub {
owner = "ROCm";
repo = "llvm-project";
@@ -141,8 +134,21 @@ let
monorepoSrc = llvmSrc;
doCheck = false;
});
refsToRemove = builtins.concatStringsSep " -t " [
stdenvToBuildRocmLlvm
stdenvToBuildRocmLlvm.cc
stdenvToBuildRocmLlvm.cc.cc
stdenv.cc
stdenv.cc.cc
stdenv.cc.bintools
];
sysrootCompiler =
cc: name: paths:
{
cc,
name,
paths,
linkPaths,
}:
let
linked = symlinkJoin { inherit name paths; };
in
@@ -152,29 +158,38 @@ let
# nix why-depends --precise .#rocmPackages.llvm.rocm-toolchain.linked /store/path/its/not/allowed
disallowedRequisites = disallowedRefsForToolchain;
passthru.linked = linked;
linkPaths = linkPaths;
passAsFile = [ "linkPaths" ];
# TODO(@LunNova): Try to use --sysroot with clang in its original location instead of
# relying on copying the binary?
# $clang/bin/clang++ --sysroot=$rocm-toolchain is not equivalent
# to a clang copied to $rocm-toolchain/bin here, have not yet figured out why
}
''
set -x
mkdir -p $out/
cp --reflink=auto -rL ${linked}/* $out/
chmod -R +rw $out
mkdir -p $out/usr
ln -s $out/ $out/usr/local
mkdir -p $out/nix-support/
# we don't need mixed 32 bit, the presence of lib64 is used by LLVM to decide it's a multilib sysroot
rm -rf $out/lib64
echo 'export CC=clang' >> $out/nix-support/setup-hook
echo 'export CXX=clang++' >> $out/nix-support/setup-hook
rm -rf $out/lib/cmake $out/lib/lib*.a
mkdir -p $out/lib/clang/${llvmMajorVersion}/lib/linux/
ln -s $out/lib/linux/libclang_rt.* $out/lib/clang/${llvmMajorVersion}/lib/linux/
find $out -type f -exec sed -i "s|${cc.out}|$out|g" {} +
find $out -type f -exec sed -i "s|${cc.dev}|$out|g" {} +
# our /include now has more than clang expects, so this specific dir still needs to point to cc.dev
# FIXME: could copy into a different subdir?
sed -i 's|set(CLANG_INCLUDE_DIRS.*$|set(CLANG_INCLUDE_DIRS "${cc.dev}/include")|g' $out/lib/cmake/clang/ClangConfig.cmake
${lib.getExe rdfind} -makesymlinks true $out/ # create links *within* the sysroot to save space
${lib.getExe rdfind} -makesymlinks true ${
builtins.concatStringsSep " " (map (x: "${x}/lib") paths)
} $out/ # create links *within* the sysroot to save space
for i in $(cat $linkPathsPath); do
${lib.getExe lndir} -silent $i $out
done
echo 'export CC=clang' >> $out/nix-support/setup-hook
echo 'export CXX=clang++' >> $out/nix-support/setup-hook
'';
# Removes patches which either aren't desired, or don't apply against ROCm LLVM
removeInapplicablePatches =
@@ -277,6 +292,11 @@ rec {
zstd
zlib
];
preFixup = ''
moveToOutput "lib/lib*.a" "$dev"
moveToOutput "lib/cmake" "$dev"
sed -Ei "s|$lib/lib/(lib[^/]*)\.a|$dev/lib/\1.a|g" $dev/lib/cmake/llvm/*.cmake
'';
env = (old.env or { }) // {
NIX_CFLAGS_COMPILE = "${(old.env or { }).NIX_CFLAGS_COMPILE or ""} ${llvmExtraCflags}";
};
@@ -285,10 +305,7 @@ rec {
disallowedReferences = (old.disallowedReferences or [ ]) ++ disallowedRefsForToolchain;
postFixup = ''
${old.postFixup or ""}
remove-references-to -t "${stdenv.cc}" "$lib/lib/libLLVMSupport.a"
find $lib -type f -exec remove-references-to -t ${stdenv.cc.cc} {} +
find $lib -type f -exec remove-references-to -t ${stdenvToBuildRocmLlvm.cc} {} +
find $lib -type f -exec remove-references-to -t ${stdenv.cc.bintools} {} +
find $lib -type f -exec remove-references-to -t ${refsToRemove} {} +
'';
meta = old.meta // llvmMeta;
});
@@ -314,16 +331,15 @@ rec {
disallowedReferences = (old.disallowedReferences or [ ]) ++ disallowedRefsForToolchain;
postFixup = ''
${old.postFixup or ""}
find $lib -type f -exec remove-references-to -t ${stdenv.cc.cc} {} +
find $lib -type f -exec remove-references-to -t ${stdenv.cc.bintools} {} +
find $lib -type f -exec remove-references-to -t ${refsToRemove} {} +
'';
meta = old.meta // llvmMeta;
});
clang-unwrapped =
(
(llvmPackagesRocm.clang-unwrapped.override {
libllvm = llvm;
}).overrideAttrs
clang-unwrapped = (
(llvmPackagesRocm.clang-unwrapped.override {
libllvm = llvm;
enableClangToolsExtra = false;
}).overrideAttrs
(
old:
let
@@ -389,43 +405,52 @@ rec {
"-DUSE_DEPRECATED_GCC_INSTALL_PREFIX=ON"
"-DGCC_INSTALL_PREFIX=${gcc-prefix}"
];
postFixup = (old.postFixup or "") + ''
find $lib -type f -exec remove-references-to -t ${stdenvToBuildRocmLlvm} {} +
find $lib -type f -exec remove-references-to -t ${stdenvToBuildRocmLlvm.cc} {} +
find $lib -type f -exec remove-references-to -t ${stdenvToBuildRocmLlvm.cc.cc} {} +
find $lib -type f -exec remove-references-to -t ${stdenv.cc} {} +
find $lib -type f -exec remove-references-to -t ${stdenv.cc.cc} {} +
find $lib -type f -exec remove-references-to -t ${stdenv.cc.bintools} {} +
preFixup = ''
${toString old.preFixup or ""}
moveToOutput "lib/lib*.a" "$dev"
moveToOutput "lib/cmake" "$dev"
mkdir -p $dev/lib/clang/
ln -s $lib/lib/clang/${llvmMajorVersion} $dev/lib/clang/
sed -Ei "s|$lib/lib/(lib[^/]*)\.a|$dev/lib/\1.a|g" $dev/lib/cmake/clang/*.cmake
'';
postFixup = ''
${toString old.postFixup or ""}
find $lib -type f -exec remove-references-to -t ${refsToRemove} {} +
find $dev -type f -exec remove-references-to -t ${refsToRemove} {} +
'';
meta = old.meta // llvmMeta;
}
)
)
// {
libllvm = llvm;
};
);
# A clang that understands standard include searching in a GNU sysroot and will put GPU libs in include path
# in the right order
# and expects its libc to be in the sysroot
rocm-toolchain =
(sysrootCompiler clang-unwrapped "rocmcxx" (
listUsefulOutputs (
[
clang-unwrapped
bintools
compiler-rt
openmp
]
++ (lib.optionals withLibcxx [
libcxx
])
++ (lib.optionals (!withLibcxx) [
gcc-include
glibc
glibc.dev
])
)
))
(sysrootCompiler {
cc = clang-unwrapped;
name = "rocm-toolchain";
paths = [
clang-unwrapped.out
clang-unwrapped.lib
bintools.out
compiler-rt.out
openmp.out
openmp.dev
]
++ lib.optionals withLibcxx [
libcxx
]
++ lib.optionals (!withLibcxx) [
glibc
glibc.dev
];
linkPaths = [
bintools.bintools.out
]
++ lib.optionals (!withLibcxx) [
gcc-include.out
];
})
// {
version = llvmMajorVersion;
cc = rocm-toolchain;
@@ -433,26 +458,27 @@ rec {
isClang = true;
isGNU = false;
};
clang-tools = llvmPackagesRocm.clang-tools.override {
inherit clang-unwrapped clang;
};
compiler-rt-libc = llvmPackagesRocm.compiler-rt-libc.overrideAttrs (old: {
patches = old.patches ++ [
(fetchpatch {
name = "Fix-missing-main-function-in-float16-bfloat16-support-checks.patch";
url = "https://github.com/ROCm/llvm-project/commit/68d8b3846ab1e6550910f2a9a685690eee558af2.patch";
hash = "sha256-Db+L1HFMWVj4CrofsGbn5lnMoCzEcU+7q12KKFb17/g=";
relative = "compiler-rt";
})
(fetchpatch {
# Fixes fortify hardening compile error related to openat usage
hash = "sha256-pgpN1q1vIQrPXHPxNSZ6zfgV2EflHO5Amzl+2BDjXbs=";
url = "https://github.com/llvm/llvm-project/commit/155b7a12820ec45095988b6aa6e057afaf2bc892.patch";
relative = "compiler-rt";
})
];
meta = old.meta // llvmMeta;
});
compiler-rt-libc =
(llvmPackagesRocm.compiler-rt-libc.override {
libllvm = llvm;
}).overrideAttrs
(old: {
patches = old.patches ++ [
(fetchpatch {
name = "Fix-missing-main-function-in-float16-bfloat16-support-checks.patch";
url = "https://github.com/ROCm/llvm-project/commit/68d8b3846ab1e6550910f2a9a685690eee558af2.patch";
hash = "sha256-Db+L1HFMWVj4CrofsGbn5lnMoCzEcU+7q12KKFb17/g=";
relative = "compiler-rt";
})
(fetchpatch {
# Fixes fortify hardening compile error related to openat usage
hash = "sha256-pgpN1q1vIQrPXHPxNSZ6zfgV2EflHO5Amzl+2BDjXbs=";
url = "https://github.com/llvm/llvm-project/commit/155b7a12820ec45095988b6aa6e057afaf2bc892.patch";
relative = "compiler-rt";
})
];
meta = old.meta // llvmMeta;
});
compiler-rt = compiler-rt-libc;
bintools = wrapBintoolsWith {
bintools = llvmPackagesRocm.bintools-unwrapped.override {