From 06f8f9a01770a53616c33139b69c33b38d977848 Mon Sep 17 00:00:00 2001 From: Yichen Yan Date: Wed, 12 Feb 2025 03:14:08 +0000 Subject: [PATCH] Update instructions about faster linker (#146750) This PR adds instructions to specify linker via cmake env `CMAKE_LINKER_TYPE` and also adds `mold` as a linker alternative. Since 3.29, cmake introduced [`CMAKE_LINKER_TYPE`](https://cmake.org/cmake/help/latest/variable/CMAKE_LINKER_TYPE.html) that can specify linker without overwriting `ld` file or changing build script. `mold` is already stable and **the fastest** (afaict) linker out there, and also easier to install compared with `lld`. So I added it here. After switching to `mold`, the time of linking `libtorch_cuda.so` has been reduced from ~7s to ~0.6s locally. Also note `gold` has been marked deprecated recently[1]. [1] https://lwn.net/Articles/1007541/ Pull Request resolved: https://github.com/pytorch/pytorch/pull/146750 Approved by: https://github.com/albanD --- CONTRIBUTING.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9e5b64270bef..e48eee1889eb 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -795,17 +795,15 @@ python setup.py develop #### Use a faster linker -If you are editing a single file and rebuilding in a tight loop, the time spent -linking will dominate. The system linker available in most Linux distributions -(GNU `ld`) is quite slow. Use a faster linker, like [lld](https://lld.llvm.org/). +If you are editing a single file and rebuilding in a tight loop, the time spent linking will dominate. The system linker available in most Linux distributions (GNU `ld`) is quite slow. To improve build times, consider using a faster linker such as [mold](https://github.com/rui314/mold) or [lld](https://lld.llvm.org/). -People on Mac, follow [this guide](https://stackoverflow.com/questions/42730345/how-to-install-llvm-for-mac) instead. +- **mold**: A modern, high-performance linker that significantly reduces linking time. It is typically available via package managers like `apt` or `yum`. Note that `mold` requires GCC version 12 or higher. +- **lld**: A fast linker from the LLVM project. The easiest way to get `lld` is from a [LLVM release](https://releases.llvm.org/download.html). -The easiest way to use `lld` this is download the -[latest LLVM binaries](http://releases.llvm.org/download.html#8.0.0) and run: +Starting with CMake 3.29, you can specify the linker type using the [`CMAKE_LINKER_TYPE`](https://cmake.org/cmake/help/latest/variable/CMAKE_LINKER_TYPE.html) variable. For example, with `mold` installed: -```bash -ln -s /path/to/downloaded/ld.lld /usr/local/bin/ld +```sh +CMAKE_LINKER_TYPE=MOLD python setup.py develop ``` #### Use pre-compiled headers