From 3f8e2bfc76d63b52a6e3f78b3b368813f60b70b9 Mon Sep 17 00:00:00 2001 From: WANG Rui Date: Tue, 8 Sep 2026 13:57:00 +0800 Subject: [PATCH] Enable LLVM Thin LTO for LoongArch64 This PR switches the LLVM build cross-compiler from GCC to Clang and enables LLVM Thin LTO for LoongArch64 distributions. There is no significant performance difference with this change. It also reduces the size of the build artifacts: | Artifacts | Before | After | |-------------------------------------------------------|--------|-------| | rust-nightly-loongarch64-unknown-linux-gnu.tar.xz | 287M | 177M | | rust-nightly-loongarch64-unknown-linux-gnu directory | 1.6G | 849M | | rust-nightly-loongarch64-unknown-linux-musl.tar.xz | 285M | 177M | | rust-nightly-loongarch64-unknown-linux-musl directory | 1.6G | 847M | --- .../dist-loongarch64-linux/Dockerfile | 35 ++++++++++++++---- .../dist-loongarch64-musl/Dockerfile | 37 +++++++++++++++---- 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/src/ci/docker/host-x86_64/dist-loongarch64-linux/Dockerfile b/src/ci/docker/host-x86_64/dist-loongarch64-linux/Dockerfile index 9b1684bbd2ace..b7d281cc2dbe7 100644 --- a/src/ci/docker/host-x86_64/dist-loongarch64-linux/Dockerfile +++ b/src/ci/docker/host-x86_64/dist-loongarch64-linux/Dockerfile @@ -1,8 +1,14 @@ -FROM ubuntu:22.04 +FROM ubuntu:26.04 COPY scripts/cross-apt-packages.sh /scripts/ RUN sh /scripts/cross-apt-packages.sh +RUN apt-get update && \ + DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ + clang-22 \ + llvm-22 \ + lld-22 + COPY scripts/crosstool-ng-git.sh /scripts/ COPY scripts/crosstool-ng-sha256-20260705.diff /scripts/ RUN sh /scripts/crosstool-ng-git.sh @@ -18,13 +24,26 @@ RUN /scripts/crosstool-ng-build.sh COPY scripts/sccache.sh /scripts/ RUN sh /scripts/sccache.sh -ENV PATH=$PATH:/x-tools/loongarch64-unknown-linux-gnu/bin +ENV PATH=/usr/lib/llvm-22/bin:$PATH:/x-tools/loongarch64-unknown-linux-gnu/bin + +# --no-rosegment keeps read-only code in the first LOAD segment, matching the +# segment layout produced by GNU ld. This allows Linux to make better use of +# file-backed PMD mappings and reduces iTLB misses. +ENV CLANG_FLAGS="--target=loongarch64-unknown-linux-gnu -fuse-ld=lld \ + --gcc-toolchain=/x-tools/loongarch64-unknown-linux-gnu \ + --sysroot=/x-tools/loongarch64-unknown-linux-gnu/loongarch64-unknown-linux-gnu/sysroot \ + -Wl,--no-rosegment" -ENV CC_loongarch64_unknown_linux_gnu=loongarch64-unknown-linux-gnu-gcc \ - AR_loongarch64_unknown_linux_gnu=loongarch64-unknown-linux-gnu-ar \ - CXX_loongarch64_unknown_linux_gnu=loongarch64-unknown-linux-gnu-g++ \ - CFLAGS_loongarch64_unknown_linux_gnu="-mcmodel=medium" \ - CXXFLAGS_loongarch64_unknown_linux_gnu="-mcmodel=medium" +ENV CC=clang \ + CXX=clang++ \ + CC_loongarch64_unknown_linux_gnu=clang \ + CXX_loongarch64_unknown_linux_gnu=clang++ \ + CFLAGS_loongarch64_unknown_linux_gnu="$CLANG_FLAGS -mcmodel=medium" \ + CXXFLAGS_loongarch64_unknown_linux_gnu="$CLANG_FLAGS -mcmodel=medium" \ + AR_loongarch64_unknown_linux_gnu=llvm-ar \ + RANLIB_loongarch64_unknown_linux_gnu=llvm-ranlib \ + CARGO_TARGET_LOONGARCH64_UNKNOWN_LINUX_GNU_LINKER=clang \ + CARGO_TARGET_LOONGARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS="-Clink-arg=${CLANG_FLAGS// / -Clink-arg=}" # We re-use the Linux toolchain for bare-metal, because upstream bare-metal # target support for LoongArch is only available from GCC 14+. @@ -64,6 +83,8 @@ ENV RUST_CONFIGURE_ARGS="--enable-extended \ --enable-sanitizers \ --disable-docs \ --set build.allocator=jemalloc \ + --set llvm.link-shared=true \ + --set llvm.thin-lto=true \ --set rust.lto=thin \ --set rust.codegen-units=1" diff --git a/src/ci/docker/host-x86_64/dist-loongarch64-musl/Dockerfile b/src/ci/docker/host-x86_64/dist-loongarch64-musl/Dockerfile index f9eac213e5060..582a22231b33c 100644 --- a/src/ci/docker/host-x86_64/dist-loongarch64-musl/Dockerfile +++ b/src/ci/docker/host-x86_64/dist-loongarch64-musl/Dockerfile @@ -1,8 +1,14 @@ -FROM ubuntu:22.04 +FROM ubuntu:26.04 COPY scripts/cross-apt-packages.sh /scripts/ RUN sh /scripts/cross-apt-packages.sh +RUN apt-get update && \ + DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ + clang-22 \ + llvm-22 \ + lld-22 + COPY scripts/crosstool-ng-git.sh /scripts/ COPY scripts/crosstool-ng-sha256-20260705.diff /scripts/ RUN sh /scripts/crosstool-ng-git.sh @@ -18,13 +24,26 @@ RUN /scripts/crosstool-ng-build.sh COPY scripts/sccache.sh /scripts/ RUN sh /scripts/sccache.sh -ENV PATH=$PATH:/x-tools/loongarch64-unknown-linux-musl/bin - -ENV CC_loongarch64_unknown_linux_musl=loongarch64-unknown-linux-musl-gcc \ - AR_loongarch64_unknown_linux_musl=loongarch64-unknown-linux-musl-ar \ - CXX_loongarch64_unknown_linux_musl=loongarch64-unknown-linux-musl-g++ \ - CFLAGS_loongarch64_unknown_linux_musl="-mcmodel=medium" \ - CXXFLAGS_loongarch64_unknown_linux_musl="-mcmodel=medium" +ENV PATH=/usr/lib/llvm-22/bin:$PATH:/x-tools/loongarch64-unknown-linux-musl/bin + +# --no-rosegment keeps read-only code in the first LOAD segment, matching the +# segment layout produced by GNU ld. This allows Linux to make better use of +# file-backed PMD mappings and reduces iTLB misses. +ENV CLANG_FLAGS="--target=loongarch64-unknown-linux-musl -fuse-ld=lld \ + --gcc-toolchain=/x-tools/loongarch64-unknown-linux-musl \ + --sysroot=/x-tools/loongarch64-unknown-linux-musl/loongarch64-unknown-linux-musl/sysroot \ + -Wl,--no-rosegment" + +ENV CC=clang \ + CXX=clang++ \ + CC_loongarch64_unknown_linux_musl=clang \ + CXX_loongarch64_unknown_linux_musl=clang++ \ + CFLAGS_loongarch64_unknown_linux_musl="$CLANG_FLAGS -mcmodel=medium" \ + CXXFLAGS_loongarch64_unknown_linux_musl="$CLANG_FLAGS -mcmodel=medium" \ + AR_loongarch64_unknown_linux_musl=llvm-ar \ + RANLIB_loongarch64_unknown_linux_musl=llvm-ranlib \ + CARGO_TARGET_LOONGARCH64_UNKNOWN_LINUX_MUSL_LINKER=clang \ + CARGO_TARGET_LOONGARCH64_UNKNOWN_LINUX_MUSL_RUSTFLAGS="-Clink-arg=${CLANG_FLAGS// / -Clink-arg=}" ENV HOSTS=loongarch64-unknown-linux-musl @@ -34,6 +53,8 @@ ENV RUST_CONFIGURE_ARGS="--enable-extended \ --enable-sanitizers \ --disable-docs \ --set build.allocator=jemalloc \ + --set llvm.link-shared=true \ + --set llvm.thin-lto=true \ --set rust.lto=thin \ --set rust.codegen-units=1 \ --set target.loongarch64-unknown-linux-musl.crt-static=false \