• compiling vanilla kernel with Debian clang-13

    From Ryutaroh Matsumoto@21:1/5 to All on Sun Nov 14 07:40:02 2021
    Hi, this is a clang related notes on compiling vanilla Linux kernel 5.15
    by Debian clang-13 (from sid/experimental).

    In short, the combination of Linux 5.15 + clang 13 seems better than
    previous ones, e.g. Linux 5.13 + clang12, on arm64.

    (1) Clang-related Kconfig items seem to work fine with my use case,
    in particular, I have not seen any problem with the following config items: CONFIG_RELR=y
    CONFIG_SHADOW_CALL_STACK=y
    CONFIG_LTO_CLANG_FULL=y
    CONFIG_CFI_CLANG=y
    CONFIG_CFI_CLANG_SHADOW=y
    CONFIG_TRIM_UNUSED_KSYMS=y
    CONFIG_INIT_STACK_ALL_ZERO=y (or CONFIG_INIT_STACK_ALL_PATTERN=y)

    (2) "-mcpu=native" seems to work. With 5.13, it did not build a bootable kernel.

    (3) Polyhedral optimization seems to work, as proposed at https://groups.google.com/g/clang-built-linux/c/Pg7CXssRuYo/m/eHJkNgjTDgAJ

    (4) ld.lld-13 uses less memory than ld.lld-12. ld.lld-12 used 7GiB with CONFIG_LTO_CLANG_FULL=y, but ld.lld-13 uses "only" 4GiB.

    I used the following script to compile a kernel.
    Clang on Debian can be unstalled by
    apt-get install clang/exerimental llvm/experimental lld/experimental.
    clang in sid depends on clang-11 for now.

    (5) CONFIG_XFS_FS=y or CONFIG_OVERLAY_FS=y causes runtime error in
    clang.


    cd /var/tmp

    arch=`dpkg --print-architecture`
    if [ $arch = amd64 ]; then
    variant=amd64_none_amd64
    march='-march=native'
    elif [ $arch = arm64 ]; then
    variant=arm64_none_arm64
    march='-mcpu=native'
    elif [ $arch = armhf ]; then
    variant=armhf_none_armmp-lpae
    march='-mcpu=native'
    else
    echo "Please find a suitable config of $arch."
    exit 1
    fi

    KVER=5.15.2
    exec </dev/null >build-log-${KVER}.txt 2>&1
    set -xe
    wget -T 10 -c https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-${KVER}.tar.xz tar Jxf linux-${KVER}.tar.xz
    cd linux-${KVER}
    # install linux-config-5.15 from sid or experimental
    xzcat /usr/src/linux-config-5.15/config.${variant}.xz >.config
    cat >>.config <<EOF
    CONFIG_LTO_CLANG_FULL=y
    CONFIG_TRIM_UNUSED_KSYMS=y
    CONFIG_CFI_CLANG=y
    CONFIG_CFI_CLANG_SHADOW=y
    CONFIG_SHADOW_CALL_STACK=y
    CONFIG_INIT_STACK_ALL_ZERO=y
    CONFIG_IRQ_TIME_ACCOUNTING=y
    CONFIG_LD_DEAD_CODE_DATA_ELIMINATION=y

    CONFIG_XFS_FS=n
    CONFIG_DEBUG_INFO=n
    CONFIG_OVERLAY_FS=n
    EOF

    yes '' |
    chrt --batch 0 nice -19 /usr/bin/time -v make -j `nproc` V=1 LLVM=1 LLVM_IAS=1 \
    KCFLAGS="-pipe ${march} -fintegrated-cc1 -mllvm -polly -mllvm -polly-ast-use-context -mllvm -polly-invariant-load-hoisting -mllvm -polly-opt-fusion=max -mllvm -polly-run-inliner -mllvm -polly-vectorizer=stripmine -mllvm -polly-run-dce" \
    LOCALVERSION=-clang13ltopolly \
    bindeb-pkg

    Best regards, Ryutaroh Matsumoto

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)