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

Support compiling with LLVM #510

Merged
merged 6 commits into from
Jun 15, 2023
Merged

Support compiling with LLVM #510

merged 6 commits into from
Jun 15, 2023

Conversation

imwints
Copy link
Contributor

@imwints imwints commented Mar 22, 2023

Support LLVM as toolchain. Set CXX=clang++ to compile with LLVM instead of GCC.
Only works with Clang/LLVM 16.0.0 and higher.

This may not work on all supported variants, feedback appreciated.

Closes: #490
Closes: #508

@imwints
Copy link
Contributor Author

imwints commented May 23, 2023

I don't know why the CI for FreeBSD is failing.

@aristocratos
Copy link
Owner

@nobounce
Seems to be a Makefile issue
Makefile:51: *** ERROR: c++: Version minimum is 16.0.0. Stop.

It stops because of wrong clang version detected ignoring that a compatible gcc version is available.

A solution would be to instead increment a error counter variable and if the version checks for both clang and gcc fails, then throw an error.

@imwints imwints force-pushed the LLVM branch 3 times, most recently from 3ae6d65 to 37bc27e Compare May 23, 2023 13:49
@aristocratos
Copy link
Owner

@nobounce
I'm getting 8000 lines of undefined reference linker errors trying to compile with clang 16 on Ubuntu 22.04 with everything directly pulled from https://apt.llvm.org/

Installed packages:
clang-16 clang-tools-16 clang-16-doc libclang-common-16-dev libclang-16-dev libclang1-16 clang-format-16 python3-clang-16 clangd-16 clang-tidy-16 libc++-16-dev libc++abi-16-dev libllvm-16-ocaml-dev libllvm16 llvm-16 llvm-16-dev llvm-16-doc llvm-16-examples llvm-16-runtime

Am I missing something?

@imwints
Copy link
Contributor Author

imwints commented May 23, 2023

@aristocratos

Installed packages: clang-16 clang-tools-16 clang-16-doc libclang-common-16-dev libclang-16-dev libclang1-16 clang-format-16 python3-clang-16 clangd-16 clang-tidy-16 libc++-16-dev libc++abi-16-dev libllvm-16-ocaml-dev libllvm16 llvm-16 llvm-16-dev llvm-16-doc llvm-16-examples llvm-16-runtime

Am I missing something?

Seems like lld and libunwind are missing. If both are installed you can try to force Clang to use it's own tools by passing

-stdlib=libc++ -fuse-ld=lld -unwindlib=libunwind -rtlib=compiler-rt

@aristocratos
Copy link
Owner

@nobounce
Turns out the problem was that I was passing CXX=clang-16 instead of CXX=clang++-16 :)
lld and libunwind got pulled in as dependencies by the earlier packages.

Not sure why, but the binary compiled by clang is 2.0MiB compared to 1.5MiB by gcc, with the same optimization flags?

Will take a closer look at the changes this week, but since it's passing compile for all platforms now it's looking good. Nice work! 👍🏼

@imwints
Copy link
Contributor Author

imwints commented May 23, 2023

For what I know, Clang does more aggressive loop unrolling with -O2 than GCC with -O3. I'll have a look aswell.

@imwints
Copy link
Contributor Author

imwints commented May 23, 2023

@aristocratos

Compiler Bytes Opt Libc Static
Clang 1,690,008 -O2 Glibc false
Clang 1,826,016 -O3 Glibc false
GCC 1,411,432 -O2 Glibc false
GCC 1,493,384 -O3 Glibc false
Clang 1,691,552 -O2 musl false
Clang 1,826,152 -O3 musl false
Clang 2,515,832 -O2 musl true
Clang 2,651,528 -O3 musl true

That seems reasonable to me.
My only concern is that Clang takes 1.5x the time for compilation (08s / 12s).

check.

Clang cannot handle <semaphore> being included in a namespace, which is
also unadvised see:
https://softwareengineering.stackexchange.com/a/335261.

Using the fallback <semaphore.h> is only meant for GCC 10, but Clang
defines `__GNUC__ = 4` so exclude Clang.
On my musl system statvfs64 is not exposed by default. The musl FAQ
recommends against using type64_t types, see:
https://wiki.musl-libc.org/faq.html#Q:-Do-I-need-to-define-%3Ccode%3E_LARGEFILE64_SOURCE%3C/code%3E-to-get-64bit-%3Ccode%3Eoff_t%3C/code%3E?.

Defining `_FILE_OFFSET_BITS=64` and using type_t lets type_t use the 64
bit interface, see:
https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html#index-_005fFILE_005fOFFSET_005fBITS.
Returning from the thread this way prevents local variables to be
destructed correctly since pthread_exit is marked noreturn.

This fixes a segmentation fault with glibc and llvm-libunwind on exit.
Clang 16.0.0 or later can now be used to compile btop. Simply call
`CXX=clang++` make.

If the CXX variable contains an incompatible Clang version try to
fallback to GCC.
@imwints
Copy link
Contributor Author

imwints commented Jun 14, 2023

@aristocratos
Did you have the time to look at this?
It would be great if that can land in main. Maybe coupled with a new release so (source-based-)distros can take advantage of this and remove gcc as a dependency :)

Copy link
Owner

@aristocratos aristocratos left a comment

Choose a reason for hiding this comment

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

Maybe coupled with a new release so (source-based-)distros can take advantage of this and remove gcc as a dependency :)

I can't imagine that many distros have clang16 included by default yet?

Gpu support should be done in a little while, and are planning to do a release with a minor version bump then.

@aristocratos aristocratos merged commit 7e50b03 into aristocratos:main Jun 15, 2023
@imwints
Copy link
Contributor Author

imwints commented Jun 15, 2023

@aristocratos

I can't imagine that many distros have clang16 included by default yet?

Yeah, probably not many :)
I'm running Gentoo on my machine and a server, and the server only has Clang as a compiler. Bothers be that the current version has a hard dependency on GCC (Compiling GCC takes a couple of hours). That's why I made the PR in the first place. A small version bump might not hurt but in the mean time I can at least run the "official" git version.

@imwints imwints deleted the LLVM branch June 15, 2023 16:10
@vimproved
Copy link

FYI, Gentoo has clang 16, but only on the testing branch. LLVM 16 is not yet stabilized because of those various issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

utilise _FILE_OFFSET_BITS=64 [REQUEST] Add LLVM support
3 participants