Skip to content

Commit

Permalink
Improve compile times (PLC-lang#679) (PLC-lang#688)
Browse files Browse the repository at this point in the history
Added a script which automates the process of creating a `.cargo/config.toml` file to use `lld` as the default linker. Improves linking times by 2x to 4x times.
  • Loading branch information
volsa authored Dec 13, 2022
1 parent eac5ac1 commit 6aa56a0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ RUN chmod -R a+rw $CARGO_HOME \

# Switch back to dialog for any ad-hoc use of apt-get
ENV DEBIAN_FRONTEND=dialog

# Required if we want to use `lld` as the default linker for RuSTy
RUN ln -sf /usr/bin/ld.lld-$LLVM_VER /usr/bin/ld.lld
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@
*.bc
*.a
*.elf

# Introduced with issue #679:
# We're assuming config.toml to be a local config file for now.
# This entry has to be removed if a global config file is needed in the near future.
.cargo/config.toml
4 changes: 4 additions & 0 deletions book/src/build_and_install.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,9 @@ cargo build --release

You can find the binary at `./target/release/rustyc`.

## Improving Compile Times
By default Rust uses the GNU Linker on Linux which compared to [lld](https://lld.llvm.org/) is slower by a margin of [~2x - 4x](https://llvm.org/devmtg/2016-10/slides/Ueyama-lld.pdf). To improve compile times we can therefore use `lld`. To do so you will need to run the `rusty/scripts/lld.sh` script inside the `rusty` root folder, i.e. by executing `./scripts/lld.sh`. **Note** that the script was only tested on Ubuntu based distributions thus far.


## Installing
_TODO_
41 changes: 41 additions & 0 deletions scripts/lld.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env sh

LD_LLD_WITH_VERSION=$(command -v ld.lld-$1)
LD_LLD_WITHOUT_VERSION=$(command -v ld.lld)

if [ -z $1 ]; then
echo "No version specified!"
echo "Usage: $0 <LLD version>"
echo "Example: $0 13"
exit 1
fi

# Check if the specified `lld` version is present on the system
if [ -z $LD_LLD_WITH_VERSION ]; then
echo "Could not find ld.lld-$1, make sure lld is installed"
exit 1
fi

# Check if `ld.lld` is present on the system
if [ -z $LD_LLD_WITHOUT_VERSION ]; then
# Not present, create a symlink into ~/.local/bin
mkdir -p $HOME/.local/bin
ln -svf $LD_LLD_WITH_VERSION $HOME/.local/bin/ld.lld

# Append new $PATH to `.bashrc`
echo 'export PATH=$HOME/.local/bin:$PATH' >> $HOME/.bashrc
echo 'A new $PATH entry has been added to your .bashrc'
echo 'For changes to take effect close and reopen your current shell.'

else
# Present, do nothing because we don't want to modifiy system-wide configurations
echo "Note: Using already present $LD_LLD_WITHOUT_VERSION binary"
fi

# Create `config.toml` to make `lld` the default linker for RuSTy
mkdir -p .cargo
echo -n '[build]
rustflags = ["-C", "link-arg=-fuse-ld=lld"]' > .cargo/config.toml

# Makes sure new builds with lld succeed
cargo clean

0 comments on commit 6aa56a0

Please sign in to comment.