Skip to content

Commit 26871f3

Browse files
authored
Switch native library compiler from GCC 14 to Clang 21 (elastic#145681)
This PR switches the compiler for the native simdvec library (libvec) from GCC 14 to Clang 21 for all targets (Darwin, Linux x64, Linux aarch64). Benchmarking showed Clang 21 produces 8-12% faster AVX-512 code than GCC 14 on x64, due to better register allocation (GCC generates unnecessary vmovdqa register-to-register copies in unrolled loops). On ARM, performance is identical between compilers.
1 parent eb6c470 commit 26871f3

File tree

5 files changed

+32
-32
lines changed

5 files changed

+32
-32
lines changed

libs/native/libraries/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ configurations {
1919
}
2020

2121
var zstdVersion = "1.5.7"
22-
var vecVersion = "1.0.79"
22+
var vecVersion = "1.0.83"
2323

2424
repositories {
2525
exclusiveContent {

libs/simdvec/native/Dockerfile.cross-toolchain

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,36 @@
88
#
99

1010
# Cross-compilation toolchain image for libvec.
11-
# Contains clang-18+lld-18 (Darwin target) and gcc-14 (Linux targets).
11+
# Contains clang-21 (all targets: Darwin, Linux x64, Linux aarch64).
1212
# No macOS SDK required — Darwin builds use a minimal C++ std library
1313
# (tinystd) bundled in the source tree, with clang's freestanding headers.
1414
#
1515
# To build and push (see also build_cross_toolchain_image.sh):
1616
# docker build \
1717
# -f Dockerfile.cross-toolchain \
18-
# -t docker.elastic.co/es-dev/es-native-cross-toolchain:1 .
19-
# docker push docker.elastic.co/es-dev/es-native-cross-toolchain:1
18+
# -t docker.elastic.co/es-dev/es-native-cross-toolchain:2 .
19+
# docker push docker.elastic.co/es-dev/es-native-cross-toolchain:2
2020

2121
FROM debian:trixie-slim
2222

23-
# clang-18 and lld-18 from the official LLVM apt repository.
23+
# clang-21 from the official LLVM apt repository.
24+
# libstdc++-14-dev-arm64-cross provides the aarch64 sysroot (C++ headers + libs)
25+
# for cross-compiling Linux aarch64 from x64.
2426
RUN apt-get update && apt-get install -y --no-install-recommends \
2527
ca-certificates \
2628
gnupg \
2729
wget \
2830
&& wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key \
2931
| gpg --dearmor > /usr/share/keyrings/llvm-archive-keyring.gpg \
3032
&& echo "deb [signed-by=/usr/share/keyrings/llvm-archive-keyring.gpg] \
31-
https://apt.llvm.org/trixie/ llvm-toolchain-trixie-18 main" \
32-
> /etc/apt/sources.list.d/llvm.list \
33+
https://apt.llvm.org/trixie/ llvm-toolchain-trixie-21 main" \
34+
> /etc/apt/sources.list.d/llvm-21.list \
3335
&& rm -rf /var/lib/apt/lists/*
3436

3537
RUN apt-get update && apt-get install -y --no-install-recommends \
3638
make \
37-
clang-18 \
38-
lld-18 \
39-
llvm-18 \
40-
gcc-14 \
41-
g++-14 \
42-
gcc-14-aarch64-linux-gnu \
43-
g++-14-aarch64-linux-gnu \
39+
clang-21 \
40+
lld-21 \
41+
llvm-21 \
42+
libstdc++-14-dev-arm64-cross \
4443
&& rm -rf /var/lib/apt/lists/*

libs/simdvec/native/Makefile

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ MARCH_AMD64_TIER2 = icelake-client
2929
# Compilers
3030
# Darwin cross-compilation: -nostdinc blocks host (glibc) headers, then we
3131
# add our minimal C++ std headers (tinystd) and clang builtins.
32-
CLANG_RESOURCE = $(shell clang++-18 -print-resource-dir)
33-
CLANG = clang++-18 --target=arm64-apple-macos14 -Winline -nostdinc \
32+
CLANG_RESOURCE = $(shell clang++-21 -print-resource-dir)
33+
CLANG = clang++-21 --target=arm64-apple-macos14 -Winline -nostdinc \
3434
-isystem src/vec/headers/darwin/tinystd \
3535
-isystem $(CLANG_RESOURCE)/include
36-
CLANG_LINK = clang++-18 --target=arm64-apple-macos14 -fuse-ld=lld -nostdlib -Wl,-undefined,dynamic_lookup
37-
CLANG_STRIP = llvm-strip-18 --strip-debug
38-
GCC_ARM = aarch64-linux-gnu-g++-14 -Winline
39-
GCC_X64 = g++-14 -Winline
36+
CLANG_LINK = clang++-21 --target=arm64-apple-macos14 -fuse-ld=lld -nostdlib -Wl,-undefined,dynamic_lookup
37+
CLANG_STRIP = llvm-strip-21 --strip-debug
38+
CXX_ARM = clang++-21 --target=aarch64-linux-gnu -Winline
39+
CXX_X64 = clang++-21 -Winline
4040

4141
# Sources split by tier
4242
SRC_AARCH64_TIER1 = $(filter-out %_2.cpp, $(wildcard src/vec/c/aarch64/*.cpp))
@@ -66,7 +66,7 @@ SO_X64 = $(OUT_DIR_AMD64)/libvec.so
6666

6767
# --- local: build for the current platform using the native compiler ---
6868
# On macOS, overrides CLANG/CLANG_LINK to use the system clang (SDK available natively).
69-
# On Linux, the GCC variables already use the native compiler.
69+
# On Linux aarch64, overrides CXX_ARM to drop the cross-compilation target.
7070

7171
.PHONY: all clean local install
7272

@@ -80,6 +80,7 @@ ifeq ($(shell uname -s),Darwin)
8080
else ifeq ($(shell uname -m),aarch64)
8181
LOCAL_PLATFORM = linux-aarch64
8282
LOCAL_OUTPUT = $(SO_ARM)
83+
local: CXX_ARM = clang++-21 -Winline
8384
local: $(SO_ARM)
8485
else
8586
LOCAL_PLATFORM = linux-x64
@@ -112,26 +113,26 @@ $(DYLIB): $(OBJ_DARWIN_TIER1) $(OBJ_DARWIN_TIER2) | $(OUT_DIR_AARCH64)
112113
# --- linux-aarch64 ---
113114

114115
$(OBJ_LIN_ARM_TIER1): $(OBJ_LIN_ARM)/%.o: src/vec/c/aarch64/%.cpp | $(OBJ_LIN_ARM)
115-
$(GCC_ARM) $(CXXFLAGS) -march=$(MARCH_AARCH64_TIER1) $(HEADERS) -c $< -o $@
116+
$(CXX_ARM) $(CXXFLAGS) -march=$(MARCH_AARCH64_TIER1) $(HEADERS) -c $< -o $@
116117

117118
$(OBJ_LIN_ARM_TIER2): $(OBJ_LIN_ARM)/%.o: src/vec/c/aarch64/%.cpp | $(OBJ_LIN_ARM)
118-
$(GCC_ARM) $(CXXFLAGS) -march=$(MARCH_AARCH64_TIER2) $(HEADERS) -c $< -o $@
119+
$(CXX_ARM) $(CXXFLAGS) -march=$(MARCH_AARCH64_TIER2) $(HEADERS) -c $< -o $@
119120

120121
$(SO_ARM): $(OBJ_LIN_ARM_TIER1) $(OBJ_LIN_ARM_TIER2) | $(OUT_DIR_AARCH64)
121-
$(GCC_ARM) -shared $^ -o $@
122-
aarch64-linux-gnu-strip --strip-unneeded $@
122+
$(CXX_ARM) -fuse-ld=lld -shared $^ -o $@
123+
llvm-strip-21 --strip-unneeded $@
123124

124125
# --- linux-x64 ---
125126

126127
$(OBJ_LIN_X64_TIER1): $(OBJ_LIN_X64)/%.o: src/vec/c/amd64/%.cpp | $(OBJ_LIN_X64)
127-
$(GCC_X64) $(CXXFLAGS) -march=$(MARCH_AMD64_TIER1) $(HEADERS) -c $< -o $@
128+
$(CXX_X64) $(CXXFLAGS) -march=$(MARCH_AMD64_TIER1) $(HEADERS) -c $< -o $@
128129

129130
$(OBJ_LIN_X64_TIER2): $(OBJ_LIN_X64)/%.o: src/vec/c/amd64/%.cpp | $(OBJ_LIN_X64)
130-
$(GCC_X64) $(CXXFLAGS) -march=$(MARCH_AMD64_TIER2) $(HEADERS) -c $< -o $@
131+
$(CXX_X64) $(CXXFLAGS) -march=$(MARCH_AMD64_TIER2) $(HEADERS) -c $< -o $@
131132

132133
$(SO_X64): $(OBJ_LIN_X64_TIER1) $(OBJ_LIN_X64_TIER2) | $(OUT_DIR_AMD64)
133-
$(GCC_X64) -shared $^ -o $@
134-
strip --strip-unneeded $@
134+
$(CXX_X64) -shared $^ -o $@
135+
llvm-strip-21 --strip-unneeded $@
135136

136137
# --- directories ---
137138

libs/simdvec/native/build_cross_toolchain_image.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ case "${1:-}" in
2727
*) echo "Usage: $0 [--local]" >&2; exit 1 ;;
2828
esac
2929

30-
VERSION=1
30+
VERSION=2
3131
HOST=docker.elastic.co
3232
REPOSITORY=elasticsearch-infra/es-native-cross-toolchain
3333
IMAGE=$HOST/$REPOSITORY:$VERSION

libs/simdvec/native/publish_vec_binaries.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
set -euo pipefail
1919

20-
VERSION="1.0.79"
20+
VERSION="1.0.83"
2121

2222
LOCAL=false
2323
FORCE_UPLOAD=false
@@ -39,7 +39,7 @@ if [ "$UPLOAD" = true ] && [ -z "${ARTIFACTORY_API_KEY:-}" ]; then
3939
exit 1;
4040
fi
4141

42-
TOOLCHAIN_IMAGE="docker.elastic.co/elasticsearch-infra/es-native-cross-toolchain:1"
42+
TOOLCHAIN_IMAGE="docker.elastic.co/elasticsearch-infra/es-native-cross-toolchain:2"
4343
if [ "$LOCAL" = true ]; then
4444
TOOLCHAIN_IMAGE="es-native-cross-toolchain:local"
4545
fi

0 commit comments

Comments
 (0)