LLVM学习笔记(1. 环境构建)

构建环境基于Manjaro20.2

系统配置

  1. 虚拟内存

本以为之前32G内存编译爆内存,扩展到64就不会了,结果证明我想多了。当初直接扩展到128好了,最近(2021.2)内存价格比去年双十一贵了不少。

所以还是需要虚拟内存,为了方便使用, 写了个简单脚本,保存在gist上面。

#!/usr/bin/env fish
set swap_file /home/feilong/Program-ext/swap/swapfile
fallocate -l 100G $swap_file
chmod 600 $swap_file
mkswap $swap_file
swapon $swap_file
# swapoff -v $swap_file
# rm $swap_file
view raw swap_make.fish hosted with ❤ by GitHub
#!/usr/bin/env fish
set swap_file /home/feilong/Program-ext/swap/swapfile
# fallocate -l 100G $swap_file
# chmod 600 $swap_file
# mkswap $swap_file
# swapon $swap_file
swapoff -v $swap_file
rm $swap_file
swap Create/remove

没有仔细测算,看SWAP的情况,内存大概少了20G左右。

2. 工具链

对于使用yay来说的arch系来说,工具链基本缺什么装什么就可以。

比较有用的是lld,可以让内存占用稍微小一点。

编译指令

  1. 贴一下官方描述(2021.2)
-G Ninja Setting this option will allow you to build with ninja instead of make. Building with ninja significantly improves your build time, especially with incremental builds, and improves your memory usage.

-DLLVM_USE_LINKER Setting this option to lld will significantly reduce linking time for LLVM executables on ELF-based platforms, such as Linux. If you are building LLVM for the first time and lld is not available to you as a binary package, then you may want to use the gold linker as a faster alternative to GNU ld.

-DCMAKE_BUILD_TYPE
 	
Debug — This is the default build type. This disables optimizations while compiling LLVM and enables debug info. On ELF-based platforms (e.g. Linux) linking with debug info may consume a large amount of memory.
Release — Turns on optimizations and disables debug info. Combining the Release build type with -DLLVM_ENABLE_ASSERTIONS=ON may be a good trade-off between speed and debugability during development, particularly for running the test suite.
-DLLVM_ENABLE_ASSERTIONS This option defaults to ON for Debug builds and defaults to OFF for Release builds. As mentioned in the previous option, using the Release build type and enabling assertions may be a good alternative to using the Debug build type.

-DLLVM_PARALLEL_LINK_JOBS Set this equal to number of jobs you wish to run simultaneously. This is similar to the -j option used with make, but only for link jobs. This option can only be used with ninja. You may wish to use a very low number of jobs, as this will greatly reduce the amount of memory used during the build process. If you have limited memory, you may wish to set this to 1.

-DLLVM_TARGETS_TO_BUILD Set this equal to the target you wish to build. You may wish to set this to X86; however, you will find a full list of targets within the llvm-project/llvm/lib/Target directory.

-DLLVM_OPTIMIZED_TABLEGEN Set this to ON to generate a fully optimized tablegen during your build. This will significantly improve your build time. This is only useful if you are using the Debug build type.

-DLLVM_ENABLE_PROJECTS Set this equal to the projects you wish to compile (e.g. clang, lld, etc.) If compiling more than one project, separate the items with a semicolon. Should you run into issues with the semicolon, try surrounding it with single quotes.

-DCLANG_ENABLE_STATIC_ANALYZER Set this option to OFF if you do not require the clang static analyzer. This should improve your build time slightly.

-DLLVM_USE_SPLIT_DWARF Consider setting this to ON if you require a debug build, as this will ease memory pressure on the linker. This will make linking much faster, as the binaries will not contain any of the debug information; however, this will generate the debug information in the form of a DWARF object file (with the extension .dwo). This only applies to host platforms using ELF, such as Linux.

2. 根据上面信息,提取出比较适合平时开发使用的命令:

cd llvm-project
mkdir build
cd build
# 上面都是废话 🙂
cmake -G Ninja -DLLVM_USE_LINKER=lld ../llvm
ninja

如果内存较小,不希望swap过大拖累编译速度,可以尝试设置 -DLLVM_PARALLEL_LINK_JOBS 或 -DLLVM_USE_SPLIT_DWARF

如果设置 -DLLVM_USE_SPLIT_DWARF 的话,内存占用会缩小至15G左右

参考链接

仓库位置:

https://github.com/llvm/llvm-project

参考文献

https://llvm.org/docs/GettingStarted.html