Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Add gcc offloading to GPUs #366903

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/systems/examples.nix
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ rec {
config = "avr";
};

amdgcn = {
config = "amdgcn-amdhsa";
libc = "newlib";
};

vc4 = {
config = "vc4-elf";
libc = "newlib";
Expand Down
3 changes: 3 additions & 0 deletions lib/systems/parse.nix
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ rec {
msp430 = { bits = 16; significantByte = littleEndian; family = "msp430"; };
avr = { bits = 8; family = "avr"; };

amdgcn = { bits = 64; significantByte = littleEndian; family = "amdgcn"; };

vc4 = { bits = 32; significantByte = littleEndian; family = "vc4"; };

or1k = { bits = 32; significantByte = bigEndian; family = "or1k"; };
Expand Down Expand Up @@ -338,6 +340,7 @@ rec {
ghcjs = { execFormat = unknown; families = { }; };
genode = { execFormat = elf; families = { }; };
mmixware = { execFormat = unknown; families = { }; };
amdhsa = { execFormat = unknown; families = { }; };
} // { # aliases
# 'darwin' is the kernel for all of them. We choose macOS by default.
darwin = kernels.macos;
Expand Down
8 changes: 7 additions & 1 deletion pkgs/build-support/cc-wrapper/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
, gnugrep ? null
, expand-response-params
, libcxx ? null
, extraCompilerB ? []

# Whether or not to add `-B` and `-L` to `nix-support/cc-{c,ld}flags`
, useCcForLibs ?
Expand Down Expand Up @@ -614,7 +615,12 @@ stdenvNoCC.mkDerivation {
ccLDFlags+=" -L${cc_solib}/lib"
ccCFlags+=" -B${cc_solib}/lib"

'' + optionalString (cc.langAda or false && !isArocc) ''
'' + (concatMapStrings (p: ''
ccCFlags+=" -B${p}/libexec/gcc/${stdenvNoCC.buildPlatform.config}/${p.version}"
ccCFlags+=" -B${p}/bin"
ccCFlags+=" -foffload-options=-Wl,-L${p}/${p.stdenv.targetPlatform.config}/lib"
'') extraCompilerB)
+ optionalString (cc.langAda or false && !isArocc) ''
touch "$out/nix-support/gnat-cflags"
touch "$out/nix-support/gnat-ldflags"
basePath=$(echo $cc/lib/*/*/*)
Expand Down
12 changes: 11 additions & 1 deletion pkgs/development/compilers/gcc/common/builder.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
stdenv,
enableMultilib,
targetConfig,
version,
}:

let
Expand Down Expand Up @@ -169,6 +170,10 @@ originalAttrs:
fi

eval "$oldOpts"
'' + lib.optionalString (targetConfig == "amdgcn-amdhsa") ''
makeFlagsArray=''${makeFlagsArray[@]/CFLAGS_FOR_TARGET=*}
makeFlagsArray=''${makeFlagsArray[@]/CXXFLAGS_FOR_TARGET=*}
makeFlagsArray=''${makeFlagsArray[@]/FLAGS_FOR_TARGET=*}
'';

preConfigure =
Expand Down Expand Up @@ -219,7 +224,7 @@ originalAttrs:
# symlink), this ensures that in every case we can assume that
# $lib/lib contains the .so files
lib.optionalString (with stdenv; targetPlatform.config != hostPlatform.config) ''
ln -Ts "''${!outputLib}/''${targetConfig}/lib" $lib/lib
ln -Ts "''${!outputLib}/''${targetConfig}/lib" "''${!outputLib}/lib"
''
+
# Make `lib64` symlinks to `lib`.
Expand Down Expand Up @@ -312,6 +317,11 @@ originalAttrs:
ln -sf "$man_prefix"gcc.1 "$i"
fi
done
'' + lib.optionalString (targetConfig == "amdgcn-amdhsa") ''
for a in ld as ar nm runlib; do
ln -s $(type -P amdgcn-amdhsa-$a) \
$out/lib/gcc/x86_64-unknown-linux-gnu/${version}/accel/amdgcn-amdhsa/$a
done
'';
}
))
31 changes: 21 additions & 10 deletions pkgs/development/compilers/gcc/common/configure-flags.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

, enableLTO
, enableMultilib
, enableOffload
, enablePlugin
, disableGdbPlugin ? !enablePlugin
, enableShared
Expand Down Expand Up @@ -53,10 +54,10 @@ let

crossConfigureFlags =
# Ensure that -print-prog-name is able to find the correct programs.
[
(lib.optionals (targetPlatform.config != "amdgcn-amdhsa") [
"--with-as=${if targetPackages.stdenv.cc.bintools.isLLVM then binutils else targetPackages.stdenv.cc.bintools}/bin/${targetPlatform.config}-as"
"--with-ld=${targetPackages.stdenv.cc.bintools}/bin/${targetPlatform.config}-ld"
]
])
++ (if withoutTargetLibc then [
"--disable-libssp"
"--disable-nls"
Expand Down Expand Up @@ -84,9 +85,11 @@ let
# See Note [Windows Exception Handling]
"--enable-sjlj-exceptions"
"--with-dwarf2"
] else [
(if crossDarwin then "--with-sysroot=${lib.getLib libcCross}/share/sysroot"
else "--with-headers=${lib.getDev libcCross}${libcCross.incdir or "/include"}")
] else (
lib.optional crossDarwin "--with-sysroot=${lib.getLib libcCross}/share/sysroot"
++ lib.optional (!crossDarwin && targetPlatform.config != "amdgcn-amdhsa")
"--with-sysroot=${lib.getLib libcCross}/share/sysroot"
++ [
"--enable-__cxa_atexit"
"--enable-long-long"
"--enable-threads=${if targetPlatform.isUnix then "posix"
Expand All @@ -97,9 +100,10 @@ let
# libsanitizer requires netrom/netrom.h which is not
# available in uclibc.
"--disable-libsanitizer"
] ++ lib.optional (targetPlatform.libc == "newlib" || targetPlatform.libc == "newlib-nano") "--with-newlib"
] ++ lib.optional ((targetPlatform.libc == "newlib" || targetPlatform.libc == "newlib-nano") &&
targetPlatform.config != "amdgcn-amdhsa") "--with-newlib"
++ lib.optional (targetPlatform.libc == "avrlibc") "--with-avrlibc"
);
));

configureFlags =
# Basic dependencies
Expand All @@ -110,7 +114,7 @@ let
"--with-mpfr-lib=${mpfr.out}/lib"
"--with-mpc=${libmpc}"
]
++ lib.optionals (!withoutTargetLibc) [
++ lib.optionals (!withoutTargetLibc && targetPlatform.config != "amdgcn-amdhsa") [
(if libcCross == null
then (
# GCC will search for the headers relative to SDKROOT on Darwin, so it will find them in the store.
Expand Down Expand Up @@ -160,7 +164,12 @@ let
"--disable-libstdcxx-pch"
"--without-included-gettext"
"--with-system-zlib"
] ++ lib.optionals (targetPlatform.config != "amdgcn-amdhsa") [
"--enable-static"
] ++ lib.optionals (targetPlatform.config == "amdgcn-amdhsa") [
"--disable-sjlj-exceptions"
"--disable-libquadmath"
] ++ [
"--enable-languages=${
lib.concatStringsSep ","
( lib.optional langC "c"
Expand All @@ -177,7 +186,7 @@ let
)
}"
]

++ lib.optional enableOffload "--enable-offload-targets=amdgcn-amdhsa"
++ (if (enableMultilib || targetPlatform.isAvr)
then ["--enable-multilib" "--disable-libquadmath"]
else ["--disable-multilib"])
Expand Down Expand Up @@ -208,7 +217,9 @@ let
++ import ../common/platform-flags.nix { inherit (stdenv) targetPlatform; inherit lib; }
++ lib.optionals (targetPlatform != hostPlatform) crossConfigureFlags
++ lib.optional disableBootstrap' "--disable-bootstrap"

++ lib.optionals (targetPlatform.config == "amdgcn-amdhsa") [
"--target=amdgcn-amdhsa" "--enable-as-accelerator-for=x86_64-unknown-linux-gnu"
]
# Platform-specific flags
++ lib.optional (targetPlatform == hostPlatform && targetPlatform.isx86_32) "--with-arch=${stdenv.hostPlatform.parsed.cpu.name}"
++ lib.optional targetPlatform.isNetBSD "--disable-libssp" # Provided by libc.
Expand Down
23 changes: 20 additions & 3 deletions pkgs/development/compilers/gcc/common/dependencies.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,26 @@
let
inherit (lib) optionals;
inherit (stdenv) buildPlatform hostPlatform targetPlatform;

## llvm18 fails with gcc <14
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114419
llvmAmd =
let
llvm = buildPackages.llvmPackages_19;
in buildPackages.runCommand "${llvm.llvm.name}-wrapper" {} ''
mkdir -p $out/bin
for a in ar nm ranlib; do
ln -s ${llvm.llvm}/bin/llvm-$a $out/bin/amdgcn-amdhsa-$a
done
ln -s ${llvm.llvm}/bin/llvm-mc $out/bin/amdgcn-amdhsa-as
ln -s ${llvm.lld}/bin/lld $out/bin/amdgcn-amdhsa-ld
'';
in

{
# same for all gcc's
depsBuildBuild = [ buildPackages.stdenv.cc ];
depsBuildBuild = [ buildPackages.stdenv.cc ] ++
lib.optional (targetPlatform.config == "amdgcn-amdhsa") llvmAmd;

nativeBuildInputs =
[
Expand All @@ -55,7 +70,9 @@ in
# same for all gcc's
depsBuildTarget =
(
if hostPlatform == buildPlatform then
if (targetPlatform.config == "amdgcn-amdhsa") then
[ ]
else if hostPlatform == buildPlatform then
[
targetPackages.stdenv.cc.bintools # newly-built gcc will be used
]
Expand All @@ -75,7 +92,7 @@ in
libmpc
]
++ optionals (lib.versionAtLeast version "10") [ libxcrypt ]
++ [
++ optionals (targetPlatform.config != "amdgcn-amdhsa") [
targetPackages.stdenv.cc.bintools # For linking code at run-time
]
++ optionals (isl != null) [ isl ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ in
[
"-O2 -idirafter ${lib.getDev dep}${dep.incdir or "/include"}"
]
++ lib.optionals (!withoutTargetLibc) [
++ lib.optionals (!withoutTargetLibc && targetPlatform.config != "amdgcn-amdhsa") [
"-B${lib.getLib dep}${dep.libdir or "/lib"}"
]
);
Expand Down
2 changes: 1 addition & 1 deletion pkgs/development/compilers/gcc/common/strip-attributes.nix
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
)
popd
''
+ lib.optionalString (!langJit) ''
+ lib.optionalString (!langJit && stdenv.targetPlatform.config != "amdgcn-amdhsa") ''
${
# keep indentation
""
Expand Down
9 changes: 7 additions & 2 deletions pkgs/development/compilers/gcc/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
, staticCompiler ? false
, enableShared ? stdenv.targetPlatform.hasSharedLibraries
, enableLTO ? stdenv.hostPlatform.hasSharedLibraries
, enableOffload ? false
, texinfo ? null
, perl ? null # optional, for texi2pod (then pod2man)
, gmp, mpfr, libmpc, gettext, which, patchelf, binutils
, gmp, mpfr, libmpc, gettext, which, patchelf, binutils, newlib
, isl ? null # optional, for the Graphite optimization framework.
, zlib ? null
, libucontext ? null
Expand Down Expand Up @@ -111,6 +112,7 @@ let
disableBootstrap
disableGdbPlugin
enableLTO
enableOffload
enableMultilib
enablePlugin
enableShared
Expand Down Expand Up @@ -152,6 +154,7 @@ let
threadsCross
which
zlib
newlib
;
};

Expand Down Expand Up @@ -187,7 +190,7 @@ pipe ((callFile ./common/builder.nix {}) ({

inherit patches;

outputs = [ "out" "man" "info" ] ++ optional (!langJit) "lib";
outputs = [ "out" "man" "info" ] ++ optional (!langJit && targetPlatform.config != "amdgcn-amdhsa") "lib";

setOutputFlags = false;

Expand Down Expand Up @@ -356,6 +359,8 @@ pipe ((callFile ./common/builder.nix {}) ({
'';
} // optionalAttrs enableMultilib {
dontMoveLib64 = true;
} // optionalAttrs (targetPlatform.config == "amdgcn-amdhsa") {
newlibSrc = newlib.src;
}
))
([
Expand Down
1 change: 1 addition & 0 deletions pkgs/development/compilers/llvm/common/common-let.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rec {

# See llvm/cmake/config-ix.cmake.
platforms =
["amdgcn-amdhsa" ]++
lib.platforms.aarch64 ++
lib.platforms.arm ++
lib.platforms.mips ++
Expand Down
4 changes: 2 additions & 2 deletions pkgs/development/misc/newlib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

stdenv.mkDerivation (finalAttrs: {
pname = "newlib";
version = "4.3.0.20230120";
version = "4.4.0.20231231";

src = fetchurl {
url = "ftp://sourceware.org/pub/newlib/newlib-${finalAttrs.version}.tar.gz";
sha256 = "sha256-g6Yqma9Z4465sMWO0JLuJNcA//Q6IsA+QzlVET7zUVA=";
sha256 = "sha256-DBZqOeG/CVHfr81olJ/g5LbTZYCB1igvOa7vxjEPLxM=";
};

patches = lib.optionals nanoizeNewlib [
Expand Down
18 changes: 18 additions & 0 deletions pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6080,6 +6080,17 @@ with pkgs;

gccCrossLibcStdenv = overrideCC stdenvNoCC buildPackages.gccWithoutTargetLibc;

amdgcc = pkgsCross.amdgcn.buildPackages.gcc_latest.cc.override {
langCC = true;
# enableLTO = false;
# enableMultilib = true;
withoutTargetLibc = false;
enableShared = false;
noSysDirs = false;
libcCross = binutilsNoLibc.libc;
targetPackages.stdenv.cc.bintools = binutilsNoLibc;
};

# The GCC used to build libc for the target platform. Normal gccs will be
# built with, and use, that cross-compiled libc.
gccWithoutTargetLibc = assert stdenv.targetPlatform != stdenv.hostPlatform; let
Expand Down Expand Up @@ -6118,6 +6129,13 @@ with pkgs;

gcc_latest = gcc14;

gcc_offload = wrapCCWith {
cc = gcc_latest.cc.override {
enableOffload = true;
};
extraCompilerB = [ amdgcc ];
};

libgccjit = gcc.cc.override {
name = "libgccjit";
langFortran = false;
Expand Down
Loading