Skip to content
Merged
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
68 changes: 68 additions & 0 deletions pkgs/development/compilers/llvm/20/compiler-rt/libc-free.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
From 5eaae2daff7722ee01eee239e131ee1f62643abe Mon Sep 17 00:00:00 2001
From: Peter Waller <peter.waller@arm.com>
Date: Wed, 5 Feb 2025 19:46:15 +0000
Subject: [PATCH] [compiler-rt][AArch64] Enable libc-free builtins Linux build

Without this change, libc++abi.so.1.0 fails to link with:

```
error: undefined sybol: __aarch64_sme_accessible
```

This symbol is provided by baremetal libc's such as newlib.

In order to compile a libc with compiler-rt, it is necessary first to
build a compiler-rt with no dependency on libc to acquire the builtins.
The intended linux test requires getauxval which is provided by libc.

To that end, there are examples in the wild of building a compiler-rt
without libc by specifying -DCOMPILER_RT_BAREMETAL_BUILD=ON.
On Linux, this gives a builtins build with (almost) no libc dependencies.

See for example:

https://github.com/NixOS/nixpkgs/blob/d7fe3bcaca37e79d8b3cbde4dd69edeafbd35313/pkgs/development/compilers/llvm/common/compiler-rt/default.nix#L116-L118

```
] ++ lib.optionals (!haveLibc || bareMetal) [
"-DCMAKE_C_COMPILER_WORKS=ON"
"-DCOMPILER_RT_BAREMETAL_BUILD=ON"
```

The above specifies that a !haveLibc build sets
`-DCOMPILER_RT_BAREMETAL_BUILD`, which is done for example in a `pkgsLLVM` build.

AIUI, acquiring such a builtins build of compiler-rt is necessary to build a
pure LLVM toolchain, since builtins are required to build libc (and
libcxx is required to build a full compiler-rt).

The effect of falling back to unimplemented is that this early-stage
builtins build is incapable of doing function multiversioning tests and
falls back to behaving as-if the feature is unavailable.

This behaviour changed in
https://github.com/llvm/llvm-project/pull/119414, which introduced a
subtle change in semantics in the removal of
compiler-rt/lib/builtins/aarch64/sme-abi-init.c (definition of getauxval
macro, which was bracketed by `#if defined(__linux__)`) vs the new
definition which does not test for linux.

The proposed change is reinstating things as they were before #119414.
---
compiler-rt/lib/builtins/cpu_model/aarch64.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/builtins/cpu_model/aarch64.c b/lib/builtins/cpu_model/aarch64.c
index 4082fd62ea11a25..50438bcc7ddd885 100644
--- a/lib/builtins/cpu_model/aarch64.c
+++ b/lib/builtins/cpu_model/aarch64.c
@@ -80,7 +80,7 @@ struct {
#include "aarch64/fmv/getauxval.inc"
#elif defined(_WIN32)
#include "aarch64/fmv/windows.inc"
-#elif defined(ENABLE_BAREMETAL_AARCH64_FMV)
+#elif defined(ENABLE_BAREMETAL_AARCH64_FMV) && !defined(__linux__)
#include "aarch64/fmv/baremetal.inc"
#else
#include "aarch64/fmv/unimplemented.inc"

139 changes: 139 additions & 0 deletions pkgs/development/compilers/llvm/20/llvm/gnu-install-dirs.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c9ff3696e22d..bd96aab5e237 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1133,9 +1133,9 @@ if (NOT TENSORFLOW_AOT_PATH STREQUAL "")
add_subdirectory(${TENSORFLOW_AOT_PATH}/xla_aot_runtime_src
${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/tf_runtime)
install(TARGETS tf_xla_runtime EXPORT LLVMExports
- ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT tf_xla_runtime)
+ ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}${LLVM_LIBDIR_SUFFIX} COMPONENT tf_xla_runtime)
install(TARGETS tf_xla_runtime EXPORT LLVMDevelopmentExports
- ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT tf_xla_runtime)
+ ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}${LLVM_LIBDIR_SUFFIX} COMPONENT tf_xla_runtime)
set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS tf_xla_runtime)
# Once we add more modules, we should handle this more automatically.
if (DEFINED LLVM_OVERRIDE_MODEL_HEADER_INLINERSIZEMODEL)
diff --git a/cmake/modules/AddLLVM.cmake b/cmake/modules/AddLLVM.cmake
index baf47677b247..81954240a9bf 100644
--- a/cmake/modules/AddLLVM.cmake
+++ b/cmake/modules/AddLLVM.cmake
@@ -974,8 +974,8 @@ macro(add_llvm_library name)
endif()
install(TARGETS ${name}
${export_to_llvmexports}
- LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name}
- ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name}
+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}${LLVM_LIBDIR_SUFFIX} COMPONENT ${name}
+ ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}${LLVM_LIBDIR_SUFFIX} COMPONENT ${name}
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT ${name})

if (NOT LLVM_ENABLE_IDE)
@@ -2243,7 +2243,7 @@ function(llvm_install_library_symlink name dest type)
set(LLVM_LINK_OR_COPY copy)
endif()

- set(output_dir lib${LLVM_LIBDIR_SUFFIX})
+ set(output_dir ${CMAKE_INSTALL_FULL_LIBDIR}${LLVM_LIBDIR_SUFFIX})
if(WIN32 AND "${type}" STREQUAL "SHARED")
set(output_dir "${CMAKE_INSTALL_BINDIR}")
endif()
@@ -2519,16 +2519,37 @@ function(llvm_setup_rpath name)

if (APPLE)
set(_install_name_dir INSTALL_NAME_DIR "@rpath")
- set(_install_rpath "@loader_path/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
+ set(_install_rpath ${extra_libdir})
elseif(${CMAKE_SYSTEM_NAME} MATCHES "AIX" AND BUILD_SHARED_LIBS)
# $ORIGIN is not interpreted at link time by aix ld.
# Since BUILD_SHARED_LIBS is only recommended for use by developers,
# hardcode the rpath to build/install lib dir first in this mode.
# FIXME: update this when there is better solution.
- set(_install_rpath "${LLVM_LIBRARY_OUTPUT_INTDIR}" "${CMAKE_INSTALL_PREFIX}/lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
+ set(_install_rpath "${LLVM_LIBRARY_OUTPUT_INTDIR}" "${CMAKE_INSTALL_FULL_LIBDIR}${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
elseif(UNIX)
+ # Note that we add `extra_libdir` (aka `LLVM_LIBRARY_DIR` in our case) back
+ # to `_install_rpath` here.
+ #
+ # In nixpkgs we do not build and install LLVM alongside rdeps of LLVM (i.e.
+ # clang); instead LLVM is its own package and thus lands at its own nix
+ # store path. This makes it so that the default relative rpath (`../lib/`)
+ # does not point at the LLVM shared objects.
+ #
+ # More discussion here:
+ # - https://github.com/NixOS/nixpkgs/pull/235624#discussion_r1220150329
+ # - https://reviews.llvm.org/D146918 (16.0.5+)
+ #
+ # Note that we leave `extra_libdir` in `_build_rpath`: without FHS there is
+ # no potential that this will result in us pulling in the "wrong" LLVM.
+ # Adding this to the build rpath means we aren't forced to use
+ # `installCheckPhase` instead of `checkPhase` (i.e. binaries in the build
+ # dir, pre-install, will have the right rpath for LLVM).
+ #
+ # As noted in the differential above, an alternative solution is to have
+ # all rdeps of nixpkgs' LLVM (that use the AddLLVM.cmake machinery) set
+ # `CMAKE_INSTALL_RPATH`.
set(_build_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir})
- set(_install_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}")
+ set(_install_rpath ${extra_libdir})
if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)")
set_property(TARGET ${name} APPEND_STRING PROPERTY
LINK_FLAGS " -Wl,-z,origin ")
diff --git a/cmake/modules/AddOCaml.cmake b/cmake/modules/AddOCaml.cmake
index 2d9116b08a52..2dd7cad4ec66 100644
--- a/cmake/modules/AddOCaml.cmake
+++ b/cmake/modules/AddOCaml.cmake
@@ -147,9 +147,9 @@ function(add_ocaml_library name)
endforeach()

if( APPLE )
- set(ocaml_rpath "@executable_path/../../../lib${LLVM_LIBDIR_SUFFIX}")
+ set(ocaml_rpath ${LLVM_LIBRARY_DIR})
elseif( UNIX )
- set(ocaml_rpath "\\$ORIGIN/../../../lib${LLVM_LIBDIR_SUFFIX}")
+ set(ocaml_rpath ${LLVM_LIBRARY_DIR})
endif()
list(APPEND ocaml_flags "-ldopt" "-Wl,-rpath,${ocaml_rpath}")

diff --git a/cmake/modules/CMakeLists.txt b/cmake/modules/CMakeLists.txt
index ef4cfa3acdb5..7478e157a7c2 100644
--- a/cmake/modules/CMakeLists.txt
+++ b/cmake/modules/CMakeLists.txt
@@ -130,7 +130,7 @@ set(LLVM_CONFIG_INCLUDE_DIRS
)
list(REMOVE_DUPLICATES LLVM_CONFIG_INCLUDE_DIRS)

-extend_path(LLVM_CONFIG_LIBRARY_DIR "\${LLVM_INSTALL_PREFIX}" "lib\${LLVM_LIBDIR_SUFFIX}")
+extend_path(LLVM_CONFIG_LIBRARY_DIR "\${LLVM_INSTALL_PREFIX}" "${CMAKE_INSTALL_LIBDIR}\${LLVM_LIBDIR_SUFFIX}")
set(LLVM_CONFIG_LIBRARY_DIRS
"${LLVM_CONFIG_LIBRARY_DIR}"
# FIXME: Should there be other entries here?
diff --git a/tools/llvm-config/BuildVariables.inc.in b/tools/llvm-config/BuildVariables.inc.in
index 370005cd8d7d..7e790bc52111 100644
--- a/tools/llvm-config/BuildVariables.inc.in
+++ b/tools/llvm-config/BuildVariables.inc.in
@@ -23,6 +23,7 @@
#define LLVM_CXXFLAGS "@LLVM_CXXFLAGS@"
#define LLVM_BUILDMODE "@LLVM_BUILDMODE@"
#define LLVM_LIBDIR_SUFFIX "@LLVM_LIBDIR_SUFFIX@"
+#define LLVM_INSTALL_LIBDIR "@CMAKE_INSTALL_LIBDIR@"
#define LLVM_INSTALL_INCLUDEDIR "@CMAKE_INSTALL_INCLUDEDIR@"
#define LLVM_INSTALL_PACKAGE_DIR "@LLVM_INSTALL_PACKAGE_DIR@"
#define LLVM_TARGETS_BUILT "@LLVM_TARGETS_BUILT@"
diff --git a/tools/llvm-config/llvm-config.cpp b/tools/llvm-config/llvm-config.cpp
index d5b76b1bb6c1..1dbdb2a8f10d 100644
--- a/tools/llvm-config/llvm-config.cpp
+++ b/tools/llvm-config/llvm-config.cpp
@@ -366,7 +366,11 @@ int main(int argc, char **argv) {
sys::fs::make_absolute(ActivePrefix, Path);
ActiveBinDir = std::string(Path);
}
- ActiveLibDir = ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX;
+ {
+ SmallString<256> Path(LLVM_INSTALL_LIBDIR LLVM_LIBDIR_SUFFIX);
+ sys::fs::make_absolute(ActivePrefix, Path);
+ ActiveLibDir = std::string(Path);
+ }
{
SmallString<256> Path(LLVM_INSTALL_PACKAGE_DIR);
sys::fs::make_absolute(ActivePrefix, Path);
53 changes: 53 additions & 0 deletions pkgs/development/compilers/llvm/20/llvm/orcjit.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
From 03d6f704d07aa3650a2f59be6f7802a8735460c3 Mon Sep 17 00:00:00 2001
From: Lang Hames <lhames@gmail.com>
Date: Wed, 29 Jan 2025 03:58:29 +0000
Subject: [PATCH] [ORC][LLI] Remove redundant eh-frame registration plugin
construction from lli.

As of d0052ebbe2e the setUpGenericLLVMIRPlatform function will automatically
add an instance of the EHFrameRegistrationPlugin (for LLJIT instances whose
object linking layers are ObjectLinkingLayers, not RTDyldObjectLinkingLayers).

This commit removes the redundant plugin creation in the object linking
layer constructor function in lli.cpp to prevent duplicate registration of
eh-frames, which is likely the cause of recent bot failures, e.g.
https://lab.llvm.org/buildbot/#/builders/108/builds/8685.

(cherry picked from commit 9052b37ab1aa67a039b34356f37236fecc42bac2)
---
llvm/tools/lli/lli.cpp | 14 ++++----------
1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/llvm/tools/lli/lli.cpp b/tools/lli/lli.cpp
index 448660a539a0b0..19246f03941673 100644
--- a/llvm/tools/lli/lli.cpp
+++ b/tools/lli/lli.cpp
@@ -27,9 +27,7 @@
#include "llvm/ExecutionEngine/Orc/AbsoluteSymbols.h"
#include "llvm/ExecutionEngine/Orc/DebugUtils.h"
#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupport.h"
-#include "llvm/ExecutionEngine/Orc/EHFrameRegistrationPlugin.h"
#include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
-#include "llvm/ExecutionEngine/Orc/EPCEHFrameRegistrar.h"
#include "llvm/ExecutionEngine/Orc/EPCGenericRTDyldMemoryManager.h"
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
#include "llvm/ExecutionEngine/Orc/IRPartitionLayer.h"
@@ -1033,14 +1031,10 @@ int runOrcJIT(const char *ProgName) {
Builder.getJITTargetMachineBuilder()
->setRelocationModel(Reloc::PIC_)
.setCodeModel(CodeModel::Small);
- Builder.setObjectLinkingLayerCreator([&P](orc::ExecutionSession &ES,
- const Triple &TT) {
- auto L = std::make_unique<orc::ObjectLinkingLayer>(ES);
- if (P != LLJITPlatform::ExecutorNative)
- L->addPlugin(std::make_unique<orc::EHFrameRegistrationPlugin>(
- ES, ExitOnErr(orc::EPCEHFrameRegistrar::Create(ES))));
- return L;
- });
+ Builder.setObjectLinkingLayerCreator(
+ [&](orc::ExecutionSession &ES, const Triple &TT) {
+ return std::make_unique<orc::ObjectLinkingLayer>(ES);
+ });
}

auto J = ExitOnErr(Builder.create());
42 changes: 30 additions & 12 deletions pkgs/development/compilers/llvm/common/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,13 @@ let
}
];
"llvm/gnu-install-dirs.patch" = [
{
after = "20";
path = ../20;
}
{
after = "18";
before = "20";
path = ../18;
}
];
Expand Down Expand Up @@ -494,7 +499,12 @@ let
hash = "sha256-EX+PYGicK73lsL/J0kSZ4S5y1/NHIclBddhsnV6NPPI=";
stripLen = 1;
})
];
]
++
lib.optional (lib.versions.major metadata.release_version == "20")
# Fix OrcJIT
# PR: https://github.com/llvm/llvm-project/pull/125431
(metadata.getVersionFile "llvm/orcjit.patch");
pollyPatches =
[ (metadata.getVersionFile "llvm/gnu-install-dirs-polly.patch") ]
++ lib.optional (lib.versionAtLeast metadata.release_version "15")
Expand All @@ -507,9 +517,11 @@ let
llvm = tools.libllvm;

tblgen = callPackage ./tblgen.nix {
patches = builtins.filter
# Crude method to drop polly patches if present, they're not needed for tblgen.
(p: (!lib.hasInfix "-polly" p)) tools.libllvm.patches;
patches =
builtins.filter
# Crude method to drop polly patches if present, they're not needed for tblgen.
(p: (!lib.hasInfix "-polly" p))
tools.libllvm.patches;
clangPatches = [
# Would take tools.libclang.patches, but this introduces a cycle due
# to replacements depending on the llvm outpath (e.g. the LLVMgold patch).
Expand All @@ -532,12 +544,13 @@ let
# libraries. eg: `clang -munsupported hello.c -lc`
./clang/clang-unsupported-option.patch
]
++ lib.optional (lib.versions.major metadata.release_version == "13")
# Revert of https://reviews.llvm.org/D100879
# The malloc alignment assumption is incorrect for jemalloc and causes
# mis-compilation in firefox.
# See: https://bugzilla.mozilla.org/show_bug.cgi?id=1741454
(metadata.getVersionFile "clang/revert-malloc-alignment-assumption.patch")
++
lib.optional (lib.versions.major metadata.release_version == "13")
# Revert of https://reviews.llvm.org/D100879
# The malloc alignment assumption is incorrect for jemalloc and causes
# mis-compilation in firefox.
# See: https://bugzilla.mozilla.org/show_bug.cgi?id=1741454
(metadata.getVersionFile "clang/revert-malloc-alignment-assumption.patch")
++ lib.optional (lib.versionOlder metadata.release_version "17") (
if lib.versionAtLeast metadata.release_version "14" then
fetchpatch {
Expand Down Expand Up @@ -666,7 +679,7 @@ let
lldbPlugins = lib.makeExtensible (
lldbPlugins:
let
callPackage = newScope ( lldbPlugins // tools // args // metadata );
callPackage = newScope (lldbPlugins // tools // args // metadata);
in
lib.recurseIntoAttrs { llef = callPackage ./lldb-plugins/llef.nix { }; }
);
Expand Down Expand Up @@ -1073,7 +1086,12 @@ let
url = "https://github.com/llvm/llvm-project/pull/99837/commits/14ae0a660a38e1feb151928a14f35ff0f4487351.patch";
hash = "sha256-JykABCaNNhYhZQxCvKiBn54DZ5ZguksgCHnpdwWF2no=";
relative = "compiler-rt";
});
})
# Fixes baremetal
Copy link
Contributor

@peterwaller-arm peterwaller-arm Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment could do with refinement; it's not fixing baremetal (baremetal is fine), it's fixing the linux libc-free build.

Upstream appears to indicate a more appropriate fix would be to provide libc headers to the compiler-rt builtins build. I don't know if that's something tractable or not in the various possible useLLVM scenarios. Could use some help from others interested in determining whether/how this might impact other platforms such as Darwin. cc @emilazy when you're back and @Ericson2314.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another issue here:

nix eval nixpkgs/master#llvmPackages_git.compiler-rt
«error: path '/nix/store/54xn52z7jbib8ryvz6bw244ia34g5hw3-source/pkgs/development/compilers/llvm/git/compiler-rt/libc-free.patch' does not exist»

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've opened up #380213 to take care of things

# PR: https://github.com/llvm/llvm-project/pull/125922
++ lib.optional (lib.versionAtLeast metadata.release_version "20") (
metadata.getVersionFile "compiler-rt/libc-free.patch"
);
in
(
{
Expand Down
6 changes: 3 additions & 3 deletions pkgs/development/compilers/llvm/common/libc/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let
stdenv.override {
cc = stdenv.cc.override {
nixSupport = stdenv.cc.nixSupport // {
cc-cflags = lib.remove "-lunwind" stdenv.cc.nixSupport.cc-cflags;
cc-cflags = lib.remove "-lunwind" (stdenv.cc.nixSupport.cc-cflags or [ ]);
};
};
}
Expand All @@ -58,8 +58,8 @@ stdenv'.mkDerivation (finalAttrs: {
outputs = [ "out" ] ++ (lib.optional isFullBuild "dev");

postUnpack = lib.optionalString isFullBuild ''
patchShebangs $sourceRoot/../$pname/hdrgen/yaml_to_classes.py
chmod +x $sourceRoot/../$pname/hdrgen/yaml_to_classes.py
patchShebangs $sourceRoot/../$pname/utils/hdrgen/main.py
chmod +x $sourceRoot/../$pname/utils/hdrgen/main.py
'';

prePatch = ''
Expand Down
9 changes: 5 additions & 4 deletions pkgs/development/compilers/llvm/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ let
"17.0.6".officialRelease.sha256 = "sha256-8MEDLLhocshmxoEBRSKlJ/GzJ8nfuzQ8qn0X/vLA+ag=";
"18.1.8".officialRelease.sha256 = "sha256-iiZKMRo/WxJaBXct9GdAcAT3cz9d9pnAcO1mmR6oPNE=";
"19.1.7".officialRelease.sha256 = "sha256-cZAB5vZjeTsXt9QHbP5xluWNQnAHByHtHnAhVDV0E6I=";
"20.0.0-git".gitRelease = {
rev = "6383a12e3b4339fa4743bb97da4d51dea6d2e2ea";
rev-version = "20.0.0-unstable-2025-01-25";
sha256 = "sha256-LMew+lFm+HrR5iwFDnoXA6B2LiU/pVqsy1YrP9xr5GU=";
"20.1.0-rc1".officialRelease.sha256 = "sha256-yOczbperlR20+iLoao9g0CR+Ml2mjTCx1cqP/9WOhME=";
"21.0.0-git".gitRelease = {
rev = "312055d1da169f043e65b35fcd62d6d685700114";
rev-version = "21.0.0-unstable-2025-02-01";
sha256 = "sha256-V14ypUGn7GxzchmKjoSH2oiHDUQa07b4bQWjjRpQKCo=";
};
} // llvmVersions;

Expand Down
Loading