Skip to content

Commit 65cf069

Browse files
committed
selftests/bpf: Fix pyperf180 compilation failure with clang18
jira LE-2125 commit-author Yonghong Song <[email protected]> commit 100888f With latest clang18 (main branch of llvm-project repo), when building bpf selftests, [~/work/bpf-next (master)]$ make -C tools/testing/selftests/bpf LLVM=1 -j The following compilation error happens: fatal error: error in backend: Branch target out of insn range ... Stack dump: 0. Program arguments: clang -g -Wall -Werror -D__TARGET_ARCH_x86 -mlittle-endian -I/home/yhs/work/bpf-next/tools/testing/selftests/bpf/tools/include -I/home/yhs/work/bpf-next/tools/testing/selftests/bpf -I/home/yhs/work/bpf-next/tools/include/uapi -I/home/yhs/work/bpf-next/tools/testing/selftests/usr/include -idirafter /home/yhs/work/llvm-project/llvm/build.18/install/lib/clang/18/include -idirafter /usr/local/include -idirafter /usr/include -Wno-compare-distinct-pointer-types -DENABLE_ATOMICS_TESTS -O2 --target=bpf -c progs/pyperf180.c -mcpu=v3 -o /home/yhs/work/bpf-next/tools/testing/selftests/bpf/pyperf180.bpf.o 1. <eof> parser at end of file 2. Code generation ... The compilation failure only happens to cpu=v2 and cpu=v3. cpu=v4 is okay since cpu=v4 supports 32-bit branch target offset. The above failure is due to upstream llvm patch [1] where some inlining behavior are changed in clang18. To workaround the issue, previously all 180 loop iterations are fully unrolled. The bpf macro __BPF_CPU_VERSION__ (implemented in clang18 recently) is used to avoid unrolling changes if cpu=v4. If __BPF_CPU_VERSION__ is not available and the compiler is clang18, the unrollng amount is unconditionally reduced. [1] llvm/llvm-project@1a2e77c Signed-off-by: Yonghong Song <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Tested-by: Alan Maguire <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] (cherry picked from commit 100888f) Signed-off-by: Brett Mastbergen <[email protected]>
1 parent 1e87a74 commit 65cf069

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
11
// SPDX-License-Identifier: GPL-2.0
22
// Copyright (c) 2019 Facebook
33
#define STACK_MAX_LEN 180
4+
5+
/* llvm upstream commit at clang18
6+
* https://github.com/llvm/llvm-project/commit/1a2e77cf9e11dbf56b5720c607313a566eebb16e
7+
* changed inlining behavior and caused compilation failure as some branch
8+
* target distance exceeded 16bit representation which is the maximum for
9+
* cpu v1/v2/v3. Macro __BPF_CPU_VERSION__ is later implemented in clang18
10+
* to specify which cpu version is used for compilation. So a smaller
11+
* unroll_count can be set if __BPF_CPU_VERSION__ is less than 4, which
12+
* reduced some branch target distances and resolved the compilation failure.
13+
*
14+
* To capture the case where a developer/ci uses clang18 but the corresponding
15+
* repo checkpoint does not have __BPF_CPU_VERSION__, a smaller unroll_count
16+
* will be set as well to prevent potential compilation failures.
17+
*/
18+
#ifdef __BPF_CPU_VERSION__
19+
#if __BPF_CPU_VERSION__ < 4
20+
#define UNROLL_COUNT 90
21+
#endif
22+
#elif __clang_major__ == 18
23+
#define UNROLL_COUNT 90
24+
#endif
25+
426
#include "pyperf.h"

0 commit comments

Comments
 (0)