Skip to content

Commit

Permalink
llvmPackages.bolt: add experimental bolt hook
Browse files Browse the repository at this point in the history
  • Loading branch information
RossComputerGuy committed Jan 22, 2025
1 parent 1371a55 commit ee19a77
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/systems/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ let
# Derived meta-data
useLLVM = final.isFreeBSD || final.isOpenBSD;

useBolt = false;

libc =
/**/ if final.isDarwin then "libSystem"
else if final.isMinGW then "msvcrt"
Expand Down
8 changes: 8 additions & 0 deletions pkgs/build-support/cc-wrapper/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,14 @@ stdenvNoCC.mkDerivation {
substituteAll ${./add-clang-cc-cflags-before.sh} $out/nix-support/add-local-cc-cflags-before.sh
''

###
### Bolt support
### Binaries need to be linked with relocations.
###
+ optionalString targetPlatform.useBolt ''
echo " --emit-relocs" >> $out/nix-support/cc-ldflags
''

##
## Extra custom steps
##
Expand Down
5 changes: 5 additions & 0 deletions pkgs/development/compilers/llvm/common/bolt/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,18 @@ stdenv.mkDerivation (finalAttrs: {
postInstall = ''
mkdir -p $dev/lib
mv $out/lib/libLLVMBOLT*.a $dev/lib
mkdir -p $out/nix-support
cp $setupHook $out/nix-support/setup-hook
'';

outputs = [
"out"
"dev"
];

setupHook = ./setup-hook.sh;

meta = llvm_meta // {
homepage = "https://github.com/llvm/llvm-project/tree/main/bolt";
description = "LLVM post-link optimizer.";
Expand Down
16 changes: 16 additions & 0 deletions pkgs/development/compilers/llvm/common/bolt/setup-hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# shellcheck shell=bash

function boltFixupPhase {
if [ -d "$prefix/bin" ]; then
for e in $(find "$prefix/bin" -executable -type f); do
if $(file "$e" | cut -f2 -d ':' | grep -q ELF); then
llvm-bolt "$e" -o $(dirname "$e")/.bolt-$(basename "$e") "${boltFlags[@]-}"
mv $(dirname "$e")/.bolt-$(basename "$e") "$e"
fi
done
fi
}

if [ -z "${dontUseBolt-}" ]; then
appendToVar preFixupPhases boltFixupPhase
fi
3 changes: 3 additions & 0 deletions pkgs/stdenv/cross/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ lib.init bootStages
extraNativeBuildInputs =
old.extraNativeBuildInputs
++ lib.optionals (hostPlatform.isLinux && !buildPlatform.isLinux) [ buildPackages.patchelf ]
++ lib.optionals hostPlatform.useBolt [
buildPackages.llvmPackages.bolt
]
++ lib.optional (
let
f =
Expand Down
1 change: 1 addition & 0 deletions pkgs/top-level/release-attrpaths-superset.nix
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ let
# cross packagesets
pkgsLLVM = true;
pkgsLLVMLibc = true;
pkgsBolt = true;
pkgsMusl = true;
pkgsStatic = true;
pkgsCross = true;
Expand Down
1 change: 1 addition & 0 deletions pkgs/top-level/release.nix
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ let
agdaPackages = packagePlatforms pkgs.agdaPackages;

pkgsLLVM.stdenv = [ "x86_64-linux" "aarch64-linux" ];
pkgsBolt.stdenv = [ "x86_64-linux" "aarch64-linux" ];
pkgsLLVMLibc.stdenv = [ "x86_64-linux" "aarch64-linux" ];
pkgsArocc.stdenv = [ "x86_64-linux" "aarch64-linux" ];
pkgsZig.stdenv = [ "x86_64-linux" "aarch64-linux" ];
Expand Down
14 changes: 14 additions & 0 deletions pkgs/top-level/stage.nix
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,20 @@ let
};
};

pkgsBolt = nixpkgsFun {
overlays = [
(self': super': {
pkgsBolt = super';
})
] ++ overlays;
# Bootstrap a cross stdenv and apply the Bolt post-link optimizer.
# This is currently not possible when compiling natively,
# so we don't need to check hostPlatform != buildPlatform.
crossSystem = stdenv.hostPlatform // {
useBolt = true;
};
};

pkgsLLVMLibc = nixpkgsFun {
overlays = [ (self': super': {
pkgsLLVMLibc = super';
Expand Down

0 comments on commit ee19a77

Please sign in to comment.