Skip to content

compiler-rt: enable build of libatomic #398520

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mildsunrise
Copy link
Contributor

LLVM stdenvs lack a set of __atomic_* routines that compilers sometimes rely on, making it impossible to build certain C programs in them. The reason we lack these routines is that we're using neither compiler-rt's implementation of them (which was disabled by default a long time ago) nor gcc's implementation (libatomic). See #391740 for a more detailed explanation and an example of program that cannot be built.

Since no particular preference was expressed as to which approach should be used to solve this, this PR goes with LLVM's implementation and recommended setup, which seems to be used also in AIX, Fuchsia and Apple platforms. This consists of enabling a CMake flag, COMPILER_RT_BUILD_STANDALONE_LIBATOMIC, which causes the routines to be built and shipped in a separate DSO (placing them in a DSO instead of builtins.a is needed for correctness, as it ensures the lock section is unique in memory).

As with other builtins, I'm symlinking this DSO to libatomic.so so that downstream packages don't need specific/complicated logic for LLVM.

Other details:

  • For static platforms, since no dynamic linking is expected at all, it should be correct to ship the symbols in builtins.a. So, that's what I'm doing in those cases.
  • Since v19, compiler-rt allows using pthread locks rather than ad-hoc ones for the atomic routines. Since this plays better with instrumentation, I'm enabling this whenever libc is available.
  • It would be nice to put the DSO in a separate output / derivation, so that the rest of compiler-rt isn't pulled into the runtime closure, but it isn't high prio since compiler-rt doesn't pull in dependencies other than libc, libc++ and unwinder.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 25.05 Release Notes (or backporting 24.11 and 25.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

Copy link
Member

@alyssais alyssais left a comment

Choose a reason for hiding this comment

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

Seems fine.

@alyssais
Copy link
Member

Looks like this could go straight to master?

@wegank wegank added 12.approvals: 1 This PR was reviewed and approved by one reputable person 12.approved-by: package-maintainer This PR was reviewed and approved by a maintainer listed in the package labels Apr 15, 2025
@wegank wegank added 12.approvals: 2 This PR was reviewed and approved by two reputable people and removed 12.approvals: 1 This PR was reviewed and approved by one reputable person labels Apr 17, 2025
@mildsunrise mildsunrise force-pushed the compiler-rt-build-libatomic branch from ea89c19 to 0d7159d Compare April 17, 2025 12:08
@mildsunrise
Copy link
Contributor Author

ah, you're right... I'm so used to my changes causing mass rebuilds :_ I'll change that and also rewrite the cmake options a bit so that we don't pass them in case of older versions that don't have them

@mildsunrise mildsunrise changed the base branch from staging to master April 17, 2025 12:09
LLVM stdenvs lack a set of `__atomic_*` routines that compilers
sometimes rely on, making it impossible to build certain C programs in
them. The reason we lack these routines is that we're using neither
compiler-rt's implementation of them (which was disabled by default a
long time ago) nor gcc's implementation (libatomic). See NixOS#391740 for a
more detailed explanation and an example of program that cannot be built.

Since no particular preference was expressed as to which approach
should be used to solve this, I'm going with LLVM's implementation
and recommended setup, which seems to be used also in AIX, Fuchsia
and Apple platforms. This consists of enabling a CMake flag,
`COMPILER_RT_BUILD_STANDALONE_LIBATOMIC`, which causes the routines to
be built and shipped in a separate DSO (placing them in a DSO instead
of `builtins.a` is needed for correctness, as it ensures the lock
section is unique in memory).

As with the other builtins, I'm symlinking this DSO to `libatomic.so` so
that downstream packages don't need specific/complicated logic for LLVM.

Other details:
- For static platforms, since no dynamic linking is expected at all, it
  should be correct to ship the symbols in `builtins.a`.
  So, that's what I'm doing in those cases.
- Since v19, compiler-rt allows using pthread locks rather than ad-hoc
  ones for the atomic routines. Since this plays better with
  instrumentation, I'm enabling this whenever libc is available.
- It would be nice to put the DSO in a separate output / derivation, so
  that the rest of compiler-rt isn't pulled into the runtime closure,
  but it isn't high prio since compiler-rt doesn't pull in dependencies
  other than libc, libc++ and unwinder.

Fixes: NixOS#311930
@mildsunrise mildsunrise force-pushed the compiler-rt-build-libatomic branch from 0d7159d to 7582576 Compare April 17, 2025 12:21
Comment on lines +283 to +285
[ (lib.cmakeBool "COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN" (!withAtomicsLib)) ]
++ lib.optional withAtomicsLib (lib.cmakeBool "COMPILER_RT_BUILD_STANDALONE_LIBATOMIC" true)
++ lib.optional withAtomicsPthread (lib.cmakeBool "COMPILER_RT_LIBATOMIC_USE_PTHREAD" true)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
[ (lib.cmakeBool "COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN" (!withAtomicsLib)) ]
++ lib.optional withAtomicsLib (lib.cmakeBool "COMPILER_RT_BUILD_STANDALONE_LIBATOMIC" true)
++ lib.optional withAtomicsPthread (lib.cmakeBool "COMPILER_RT_LIBATOMIC_USE_PTHREAD" true)
[
(lib.cmakeBool "COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN" (!withAtomicsLib))
(lib.cmakeBool "COMPILER_RT_BUILD_STANDALONE_LIBATOMIC" withAtomicsLib)
(lib.cmakeBool "COMPILER_RT_LIBATOMIC_USE_PTHREAD" withAtomicsPthread)
]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that's how it was in the previous version (0d7159d). I changed it to avoid passing unknown CMake options to old LLVM versions that don't support them

Copy link
Member

Choose a reason for hiding this comment

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

We always could use lib.versionAtLeast. With the new refactoring, we tried to get less optionals here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

versionAtLeast is being used already, see the default values of the fields. I can move it here if desired (which I agree is more explicit, but less true to the user's intent IMO... I'd prefer things to fail if user requests standalone atomics in a version that doesn't have them)

regardless of whether versionAtLeast is used here or in the defaults, we still need the optionals right? COMPILER_RT_BUILD_STANDALONE_LIBATOMIC was introduced in v13 and COMPILER_RT_LIBATOMIC_USE_PTHREAD was introduced in v19

Copy link
Member

Choose a reason for hiding this comment

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

Should be fine for at least COMPILER_RT_BUILD_STANDALONE_LIBATOMIC since only 1 thing uses LLVM 12

# since that's when the standalone build became available; GCC's
# libatomic should be used in other cases.
withAtomics ?
(stdenv.hostPlatform.useLLVM or false)
Copy link
Member

Choose a reason for hiding this comment

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

We don't need the or false since lib/systems/default.nix adds it in.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah, good to know. we do stdenv.hostPlatform.useLLVM or false everywhere in this file so I assumed I was missing something

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: llvm/clang Issues related to llvmPackages, clangStdenv and related 10.rebuild-darwin: 11-100 10.rebuild-linux: 11-100 12.approvals: 2 This PR was reviewed and approved by two reputable people 12.approved-by: package-maintainer This PR was reviewed and approved by a maintainer listed in the package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants