This is an archive of the discontinued LLVM Phabricator instance.

[libc][NOT FOR COMMIT] building LLVM-libc on 32 bit arm with gcc
AbandonedPublic

Authored by michaelrj on Apr 21 2022, 4:40 PM.

Details

Reviewers
None
Summary

This patch is only for demonstration purposes and is not intended to be
landed. It contains the changes needed to allow LLVM-libc to be built
with prebuilt GCC for ARM.

To build LLVM-libc with this configuration, use the following command:

cmake ../llvm -G Ninja -DLLVM_ENABLE_PROJECTS="libc"
-DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=arm-none-eabi-gcc
-DCMAKE_CXX_COMPILER=arm-none-eabi-g++ -DLLVM_LIBC_FULL_BUILD=OFF
-DLLVM_LIBC_ENABLE_LINTING=OFF -DLLVM_LIBC_INCLUDE_SCUDO=OFF
-DCMAKE_C_FLAGS="-specs=nosys.specs"
-DCMAKE_CXX_FLAGS="-specs=nosys.specs -Wno-psabi"
-DLLVM_DEFAULT_TARGET_TRIPLE=arm-unknown-linux-eabi
-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=arm
-DLLVM_INCLUDE_BENCHMARKS=OFF

Diff Detail

Event Timeline

michaelrj created this revision.Apr 21 2022, 4:40 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptApr 21 2022, 4:40 PM
michaelrj requested review of this revision.Apr 21 2022, 4:40 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 21 2022, 4:40 PM
gchatelet added inline comments.
libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
10

Which gcc version is this?

libc/src/__support/CPP/TypeTraits.h
53
#ifdef __SIZEOF_INT128__
     || IsSameV<__uint128_t, TypeNoCV> || IsSameV<__int128_t, TypeNoCV>
#endif // __uint128_t
libc/src/string/memory_utils/utils.h
21
libc/test/src/string/CMakeLists.txt
272

Could you copy/paste the error message?

michaelrj updated this revision to Diff 424595.Apr 22 2022, 1:38 PM
michaelrj marked 2 inline comments as done.

address comments

libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
10

arm-none-eabi-gcc (GNU Arm Embedded Toolchain 10-2020-q4-major) 10.2.1 20201103 (release)

libc/test/src/string/CMakeLists.txt
272

These were commented out from when I had the memory functions removed from the entrypoints list. The error was:

CMake Error at ~/llvm-project/libc/test/src/string/CMakeLists.txt:265 (target_link_libraries):
  Cannot specify link libraries for target
  "libc.test.src.string.libc.src.string.bcmp_opt_host_test" which is not
  built by this project.
Call Stack (most recent call first):
  ~/llvm-project/libc/test/src/string/CMakeLists.txt:272 (add_libc_multi_impl_test)

I've uncommented these lines since the memory functions are building now that there's a definition for CACHELINE_SIZE.

michaelrj updated this revision to Diff 425323.Apr 26 2022, 3:46 PM

add changes that allow building the tests

Thanks very much for posting the patch. Just curious to know whether you've tried building with Clang? Is using GCC due to it having a default target of Arm? If I get some time I'll try to see if I can get it work with the https://github.com/ARM-software/LLVM-embedded-toolchain-for-Arm this approximates some of the multilib and specs files with clang config files, although we're hoping to add better support for multilib.

libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
15

For Cortex-m4 strictly speaking you shouldn't need the -mabi=aapcs the GNU embedded toolchain will default to that, the same for -mthumb as Cortex-m4 only supports Thumb.

Although as you say, this is an experimental patch so no harm done at this point.

FWIW I was able to get at least the ninja llvmlibc working with the LLVM embedded toolchain with the following hacky bit of cmake

cmake ../../llvm -G Ninja -DLLVM_ENABLE_PROJECTS="libc" \
      -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY \
      -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_C_COMPILER=clang \
      -DCMAKE_CXX_COMPILER=clang++ \
      -DLLVM_LIBC_FULL_BUILD=OFF \
      -DCMAKE_C_COMPILER_TARGET=armv7-m-none-eabi \
      -DCMAKE_CXX_COMPILER_TARGET=armv7-m-none-eabi \
      -DCMAKE_C_FLAGS="--config armv7m_soft_nofp_rdimon_baremetal"\
      -DCMAKE_CXX_FLAGS="--config armv7m_soft_nofp_rdimon_baremetal"\
      -DCMAKE_AR=llvm-ar \
      -DLLVM_LIBC_ENABLE_LINTING=OFF -DLLVM_LIBC_INCLUDE_SCUDO=OFF \
      -DLLVM_DEFAULT_TARGET_TRIPLE="armv7-m-none-eabi" \
      -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=arm \
      -DLLVM_INCLUDE_BENCHMARKS=OFF

The reason I started with gcc is that it was suggested to me by an embedded developer as being more complete than the clang toolchain at the moment. That being said, the clang build does appear to work, I tried your cmake command and I can confirm that ninja llvmlibc works, although the tests appear to be having some trouble stemming from their inclusions from libc++. My guess is that I'll need to fiddle with the build options some more to get it to work, but the fact that it builds the core library is very promising. I'll look into it more tomorrow.

libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
15

The reason I picked cortex-m4 as the cpu was because the build wasn't working and I was trying a bunch of different build options to get it to work. For more long term solutions, the target CPU will be configurable.

From what I can tell, in their current state our unit tests can't be built with the libc++ you provide. The main code should be fine though, since it doesn't depend on libc++.

The problem is that there are certain parts of libc++ that the tests include and depend on that aren't available. Most noticeably is iostream, which has #include "locale.h", which doesn't work since locales are disabled. The other problem is the memory functions, which have references to posix_memalign, which doesn't appear to be provided by your environment.

If we can solve these problems then being able to build with clang instead of gcc would be very nice, since many of the cmake edits made in this patch are because the LLVM build system doesn't support gcc as well as it does clang.

gchatelet added a comment.EditedMay 2 2022, 1:26 AM

The problem is that there are certain parts of libc++ that the tests include and depend on that aren't available. Most noticeably is iostream, which has #include "locale.h", which doesn't work since locales are disabled. The other problem is the memory functions, which have references to posix_memalign, which doesn't appear to be provided by your environment.

I can only see a reference to aligned_alloc in benchmarks/LibcMemoryBenchmark.h:113. Is this the one you're referring to?

I can only see a reference to aligned_alloc in benchmarks/LibcMemoryBenchmark.h:113. Is this the one you're referring to?

The build error was from libc++. Specifically, libc++ calls posix_memalign when new is called. I noticed this in the functions included with #include <memory> (in utils/testutils/StreamWrapper.cpp, and possibly other test utils), but it probably effects most of libc++. As far as I'm aware your libc memory functions don't call malloc and work fine under the LLVM embedded toolchain, but I understand the confusion. I'll try to be more clear with my phrasing in future.

michaelrj abandoned this revision.Feb 2 2023, 11:36 AM