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

Clang link time optimization not working on OSX #19098

Closed
dipinhora opened this issue Sep 30, 2016 · 21 comments
Closed

Clang link time optimization not working on OSX #19098

dipinhora opened this issue Sep 30, 2016 · 21 comments
Labels
6.topic: darwin Running or building packages on Darwin 6.topic: llvm/clang Issues related to llvmPackages, clangStdenv and related

Comments

@dipinhora
Copy link
Contributor

Issue description

The example for Clang's Link Time Optimization (http://llvm.org/docs/LinkTimeOptimization.html#example-of-link-time-optimization) doesn't work in nix on OSX. I tested with clang versions 3.8.0, 3.7.1, and 3.6.2. I wasn't able to test with clang 3.9.0 because of an error compiling llvm-3.9.0.

It fails with the following on OSX with Clang 3.7.1:

[nix-shell:~/tmp]$ clang --version
clang version 3.7.1 (tags/RELEASE_371/final)
Target: x86_64-apple-darwin14.5.0
Thread model: posix
[nix-shell:~/tmp]$ clang -flto a.o main.o -o main
ld: warning: ignoring file a.o, file was built for unsupported file format ( 0xDE 0xC0 0x17 0x0B 0x00 0x00 0x00 0x00 0x14 0x00 0x00 0x00 0xFC 0x06 0x00 0x00 ) which is not the architecture being linked (x86_64): a.o
Undefined symbols for architecture x86_64:
  "_foo1", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang-3.7: error: linker command failed with exit code 1 (use -v to see invocation)

It fails with the following on OSX with Clang 3.8.0:

[nix-shell:~/tmp]$ clang --version
clang version 3.8.0 (tags/RELEASE_380/final)
Target: x86_64-apple-darwin14.5.0
Thread model: posix
InstalledDir: /nix/store/w440mfm2abkxyaw7j86pmhbkj680118d-clang-3.8.0/bin
[nix-shell:~/tmp]$ clang -flto a.o main.o -o main
clang-3.8: warning: libLTO.dylib relative to clang installed dir not found; using 'ld' default search path instead
ld: warning: ignoring file a.o, file was built for unsupported file format ( 0xDE 0xC0 0x17 0x0B 0x00 0x00 0x00 0x00 0x14 0x00 0x00 0x00 0x94 0x09 0x00 0x00 ) which is not the architecture being linked (x86_64): a.o
Undefined symbols for architecture x86_64:
  "_foo1", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang-3.8: error: linker command failed with exit code 1 (use -v to see invocation)

Steps to reproduce

On an OSX machine:
Install nix: curl https://nixos.org/nix/install | sh
Run (for clang 3.8.0): nix-shell --pure -p llvmPackages_38.clang
Run (for clang 3.7.1): nix-shell --pure -p llvmPackages_37.clang
In shell: Follow instructions at http://llvm.org/docs/LinkTimeOptimization.html#example-of-link-time-optimization to test link time optimization with the example code.

Technical details

  • System: Mac OSX 10.10.5; Output of uname -a:
Darwin Dipins-MBP.home 14.5.0 Darwin Kernel Version 14.5.0: Wed Jul 29 02:26:53 PDT 2015; root:xnu-2782.40.9~1/RELEASE_X86_64 x86_64
  • Nix version: nix-env (Nix) 1.11.4
  • Nixpkgs version: "17.03pre91937.5cda2be"
@LnL7 LnL7 added the 6.topic: darwin Running or building packages on Darwin label Sep 30, 2016
@copumpkin
Copy link
Member

I seem to remember that the cctools we're using has the LTO option turned off. I tried turning it on a while back and ran into issues. Perhaps it'll work nowadays but I can't look for a while.

@dipinhora
Copy link
Contributor Author

@copumpkin thanks for the quick response.

I'm more than happy to try and get cctools working with LTO if you can point me in the right direction.

@copumpkin
Copy link
Member

I think you'll want to take the cctools-port expression and make sure to pass --enable-lto-support in its configureFlags. Unfortunately modifying that expression is painful because it'll cause you to rebootstrap your entire stdenv, so I'd probably duplicate the file, import the new file under a different name, then tinker with the build flags until you get it working. Then comes the fun of trying to get it integrated with the bootstrap, which may or may not be straightforward.

Also keep in mind that both darwin and linux use cctools-port, so please make sure the linux build still works (or call out that you can't and see if you can get someone else to test it)

@dipinhora
Copy link
Contributor Author

Thanks for the pointers. I'll see if I can make any progress. I'll test on linux also to prevent breaking it.

@Profpatsch
Copy link
Member

(triage) fixed?

@stale
Copy link

stale bot commented Jun 4, 2020

Thank you for your contributions.

This has been automatically marked as stale because it has had no activity for 180 days.

If this is still important to you, we ask that you leave a comment below. Your comment can be as simple as "still important to me". This lets people see that at least one person still cares about this. Someone will have to do this at most twice a year if there is no other activity.

Here are suggestions that might help resolve this more quickly:

  1. Search for maintainers and people that previously touched the related code and @ mention them in a comment.
  2. Ask on the NixOS Discourse.
  3. Ask on the #nixos channel on irc.freenode.net.

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 4, 2020
@takagiy takagiy mentioned this issue Oct 28, 2020
10 tasks
@nixos-discourse
Copy link

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/darwin-system-and-stdenv-hostplatform-features/9745/1

@ttuegel
Copy link
Member

ttuegel commented Nov 26, 2020

This is still a problem.

@ttuegel ttuegel reopened this Nov 26, 2020
@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Nov 26, 2020
@stale
Copy link

stale bot commented Jun 3, 2021

I marked this as stale due to inactivity. → More info

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 3, 2021
@ttuegel
Copy link
Member

ttuegel commented Jun 3, 2021

This is still an issue.

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 3, 2021
@nixos-discourse
Copy link

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/fails-to-link-on-arm64-only-when-built-through-nix/23592/2

@rkitover
Copy link
Contributor

rkitover commented Oct 4, 2023

This is still broken, in clang15Stdenv as well.

@tobim
Copy link
Contributor

tobim commented Oct 7, 2023

There are 2 tools that need to be adjusted to make things work:

  • It seems that the default linker on darwin does not understand lto objects, this can be worked around by using the compiler driver with -fuse-ld=lld.
  • The ar tool from llvm changes behavior when it is called as just ar, as it is done in nixpkgs. In this case the lto objects aren't archived correctly and won't have any symbols. We actually need to invoke it as llvm-ar to get it to work correctly.

Regarding lld, I'm not sure why it is not default anyways, maybe someone here know more?
Regarding ar, the real binary is actually named <targetprefix>-llvm-ar, but it is called through a chain of symlinks, and none of those have the llvm component in their name.

@reckenrode
Copy link
Contributor

Regarding lld, I'm not sure why it is not default anyways, maybe someone here know more?

lld isn’t a drop-in replacement for ld64. It doesn’t work with xcbuild or Qt’s build system (and probably others). I tried linking the Darwin stdenv with it, but it wasn’t even able to do that. I plan to try again with the clang 18 update next year, but it doesn’t look like much has changed so far upstream.

michaelhthomas added a commit to michaelhthomas/nixpkgs that referenced this issue Nov 16, 2023
firebird-emu supports darwin, but the derivation has not been tested on macOS.

This commit adds darwin support to the derivation, with the necessary tweaks to support building for a darwin target.
Namely, adds a workaround for NixOS#19098, and adds a missing dependency, qtquickcontrols.
michaelhthomas added a commit to michaelhthomas/nixpkgs that referenced this issue Nov 16, 2023
firebird-emu supports darwin, but the derivation has not been tested on macOS.

This commit adds darwin support to the derivation, with the necessary tweaks to support building for a darwin target.
Namely, adds a workaround for NixOS#19098, and adds a missing dependency, qtquickcontrols.
michaelhthomas added a commit to michaelhthomas/nixpkgs that referenced this issue Nov 16, 2023
firebird-emu supports darwin, but the derivation has not been tested on macOS.

This commit adds darwin support to the derivation, with the necessary tweaks to support building for a darwin target.
Namely, adds a workaround for NixOS#19098, and adds a missing dependency, qtquickcontrols.
wegank pushed a commit to michaelhthomas/nixpkgs that referenced this issue Nov 17, 2023
firebird-emu supports darwin, but the derivation has not been tested on macOS.

This commit adds darwin support to the derivation, with the necessary tweaks to support building for a darwin target.
Namely, adds a workaround for NixOS#19098, and adds a missing dependency, qtquickcontrols.

Also, adds a postInstall hook which moves the MacOS .app file to the Applications/ folder in the
store, enabling proper linking to /Applications or ~/Applications and appearance in Spotlight
and Launchpad.
@multivac61
Copy link
Contributor

multivac61 commented Jan 4, 2024

Affecting our project too. What if anything can be done to fix this issue? I am not technically apt enough in this area but would be willing to fund the effort to get this fixed (if on the order of $100).

@reckenrode
Copy link
Contributor

reckenrode commented Apr 8, 2024

I’m working on updates to libtapi, cctools, and ld64 to the latest versions from Apple’s OSS releases. I currently have libtapi and ld64 building with working LTO in ld64.

$ nix run .#ld64 -- -v
@(#)PROGRAM:ld PROJECT:ld64-951.9
BUILD 00:00:00 Jan  1 1980
configured to support archs: i386 x86_64 x86_64h armv6 armv7 armv7s armv7m armv7k arm64 arm64e arm64_32
LTO support using: LLVM version 16.0.6 (static support for 29, runtime is 29)
TAPI support using: TAPI version 15.0.0 (tapi-1500.0.12.3)
$ nix run .#clang -- -flto -fuse-ld=$(nix path-info .#ld64)/bin/ld test.c -o test
# exit status: 0

Note that Apple’s upstream sources do not build on Linux. I am building ld64 (and plan to build cctools the same way) with Meson instead of the Xcode projects. I’ve also been replacing private API usage with portable APIs. I’m doing that with an eye towards restoring support for Linux (and I have limited patience for dealing with xcbuild failures).

I should have a PR open against later this week. There is a clang piece needed for LTO. It’s passing the wrong path to ld64 (via -lto_library), which doesn’t fall back to its default LTO if libLTO.dylib does not exist at that path.

@nixos-discourse
Copy link

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/darwin-updates-news/42249/10

@nixos-discourse
Copy link

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/darwin-updates-news/42249/16

@emilazy
Copy link
Member

emilazy commented Jul 20, 2024

Shall we close this now that it’s in staging?

@reckenrode
Copy link
Contributor

Closed via #307880.

emilazy added a commit to emilazy/nixpkgs that referenced this issue Jul 28, 2024
This is unnecessary now that NixOS#19098 is fixed.
gador pushed a commit to gador/nixpkgs that referenced this issue Aug 4, 2024
This is unnecessary now that NixOS#19098 is fixed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: darwin Running or building packages on Darwin 6.topic: llvm/clang Issues related to llvmPackages, clangStdenv and related
Projects
None yet
Development

Successfully merging a pull request may close this issue.