selftests/bpf: Fix pyperf180 compilation failure with llvm18
Subsystems (3)
Yonghong Song
reviewerPATCH 1/1 selftests/bpf: Fix pyperf180 compilation failure with llvm18
With latest llvm18 (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 llvm18.
To workaround the issue, previously all 180 loop iterations are fully unrolled. Now, the fully unrolling count is changed to 90 for llvm18 and later. This reduced some otherwise long branch target distance, and fixed the compilation failure.
[1] https://github.com/llvm/llvm-project/commit/1a2e77cf9e11dbf56b5720c607313a566eebb16e
--- tools/testing/selftests/bpf/progs/pyperf180.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
tools/testing/selftests/bpf/progs/pyperf180.c +13 -0
@@ -1,4 +1,17 @@// SPDX-License-Identifier: GPL-2.0 // Copyright (c) 2019 Facebook #define STACK_MAX_LEN 180 + +/* llvm upstream commit at llvm18 +* https://github.com/llvm/llvm-project/commit/1a2e77cf9e11dbf56b5720c607313a566eebb16e +* changed inlining behavior and caused compilation failure as some branch +* target distance exceeded 16bit representation which is the maximum for +* cpu v1/v2/v3. To workaround this, for llvm18 and later, let us set unroll_count +* to be 90, which reduced some branch target distances and resolved the +* compilation failure. +*/ +#if __clang_major__ >=18 +#define UNROLL_COUNT 90 +#endif +#include "pyperf.h"
Signature · 1 line
2.34.1
Quoted 24 lines · Yonghong Song
With latest llvm18 (main branch of llvm-project repo), when building bpf selftests,
[~/work/bpf-next (master)]$ make -Ctools/testing/selftests/bpfLLVM=1 -jThe 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 -cprogs/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 llvm18.
Quoted 7 lines
To workaround the issue, previously all 180 loop iterations are fully unrolled. Now, the fully unrolling count is changed to 90 for llvm18 and later. This reduced some otherwise long branch target distance, and fixed the compilation failure.
[1] https://github.com/llvm/llvm-project/commit/1a2e77cf9e11dbf56b5720c607313a566eebb16e
Signed-off-by Yonghong Song <[email protected]>
Can confirm, the issue is present on clang main w/o this patch and disappears after this patch.
Yonghong, is there a way to keep original UNROLL_COUNT if cpuv4 is used?
Yonghong Song
reviewerQuoted 29 lines · Eduard Zingerman
Quoted 28 lines · Yonghong Song
With latest llvm18 (main branch of llvm-project repo), when building bpf selftests,
[~/work/bpf-next (master)]$ make -Ctools/testing/selftests/bpfLLVM=1 -jThe 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 -cprogs/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 llvm18.
To workaround the issue, previously all 180 loop iterations are fully unrolled. Now, the fully unrolling count is changed to 90 for llvm18 and later. This reduced some otherwise long branch target distance, and fixed the compilation failure.
Quoted 7 lines
Quoted 3 lines
[1] https://github.com/llvm/llvm-project/commit/1a2e77cf9e11dbf56b5720c607313a566eebb16eSigned-off-by Yonghong Song <[email protected]>Can confirm, the issue is present on clang main w/o this patch and disappears after this patch.
Yonghong, is there a way to keep original
UNROLL_COUNTif cpuv4 is used?
I thought about this but a little bit lazy so not giving it enough throught. But since you mentioned this, I think adding a macro to indicate cpu version by llvm is a good idea. This will give bpf developers some flexibility to add new features (new cpu variant) or workaround bugs (for a particular cpu variant but not impacting others if they are fine), etc.
So here is the llvm patch: https://github.com/llvm/llvm-project/pull/71856
With the above llvm patch, the following code change should work:
tools/testing/selftests/bpf/progs/pyperf180.c +14 -0
@@ -1,4 +1,18 @@// SPDX-License-Identifier: GPL-2.0 // Copyright (c) 2019 Facebook #define STACK_MAX_LEN 180 + +/* llvm upstream commit at llvm18 +* https://github.com/llvm/llvm-project/commit/1a2e77cf9e11dbf56b5720c607313a566eebb16e +* changed inlining behavior and caused compilation failure as some branch +* target distance exceeded 16bit representation which is the maximum for +* cpu v1/v2/v3. Macro __bpf_cpu_version__ is implemented in llvm18 to specify +* which cpu version is used for compilation. So we can set a smaller +* unroll_count if __bpf_cpu_version__ is less than 4, which reduced +* some branch target distances and resolved the compilation failure. +*/ +#if defined(__bpf_cpu_version__) && __bpf_cpu_version__ <4 +#define UNROLL_COUNT 90 +#endif +#include "pyperf.h"
Quoted 2 lines
Tested-by Eduard Zingerman <[email protected]>
Eduard Zingerman
maintainerQuoted 38 lines · Yonghong Song
Quoted 29 lines · Eduard Zingerman
Quoted 28 lines · Yonghong Song
With latest llvm18 (main branch of llvm-project repo), when building bpf selftests,
[~/work/bpf-next (master)]$ make -Ctools/testing/selftests/bpfLLVM=1 -jThe 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 -cprogs/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 llvm18.
To workaround the issue, previously all 180 loop iterations are fully unrolled. Now, the fully unrolling count is changed to 90 for llvm18 and later. This reduced some otherwise long branch target distance, and fixed the compilation failure.
Quoted 7 lines
Quoted 3 lines
[1] https://github.com/llvm/llvm-project/commit/1a2e77cf9e11dbf56b5720c607313a566eebb16eSigned-off-by Yonghong Song <[email protected]>Can confirm, the issue is present on clang main w/o this patch and disappears after this patch.
Yonghong, is there a way to keep original
UNROLL_COUNTif cpuv4 is used?
Quoted 7 lines
I thought about this but a little bit lazy so not giving it enough throught. But since you mentioned this, I think adding a macro to indicate cpu version by llvm is a good idea. This will give bpf developers some flexibility to add new features (new cpu variant) or workaround bugs (for a particular cpu variant but not impacting others if they are fine), etc.
So here is the llvm patch: https://github.com/llvm/llvm-project/pull/71856
Thank you, tried it locally, works as expected.
Quoted 40 lines · Yonghong Song
Quoted 29 lines · Eduard Zingerman
Quoted 28 lines · Yonghong Song
With latest llvm18 (main branch of llvm-project repo), when building bpf selftests,
[~/work/bpf-next (master)]$ make -Ctools/testing/selftests/bpfLLVM=1 -jThe 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 -cprogs/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 llvm18.
To workaround the issue, previously all 180 loop iterations are fully unrolled. Now, the fully unrolling count is changed to 90 for llvm18 and later. This reduced some otherwise long branch target distance, and fixed the compilation failure.
Quoted 7 lines
Quoted 3 lines
[1] https://github.com/llvm/llvm-project/commit/1a2e77cf9e11dbf56b5720c607313a566eebb16eSigned-off-by Yonghong Song <[email protected]>Can confirm, the issue is present on clang main w/o this patch and disappears after this patch.
Yonghong, is there a way to keep original
UNROLL_COUNTif cpuv4 is used?
Quoted 7 lines
I thought about this but a little bit lazy so not giving it enough throught. But since you mentioned this, I think adding a macro to indicate cpu version by llvm is a good idea. This will give bpf developers some flexibility to add new features (new cpu variant) or workaround bugs (for a particular cpu variant but not impacting others if they are fine), etc.
So here is the llvm patch: https://github.com/llvm/llvm-project/pull/71856
Great idea. Commented on the diff.
Quoted 11 lines
With the above llvm patch, the following code change should work:
tools/testing/selftests/bpf/progs/pyperf180.c +1
-0@@ -1,4 +1,18 @@// SPDX-License-Identifier: GPL-2.0 // Copyright (c) 2019 Facebook #define STACK_MAX_LEN 180 +
tools/testing/selftests/bpf/progs/pyperf180.c line 14
Quoted 10 lines
tools/testing/selftests/bpf/progs/pyperf180.c +10
-0+/* llvm upstream commit at llvm18 +* https://github.com/llvm/llvm-project/commit/1a2e77cf9e11dbf56b5720c607313a566eebb16e +* changed inlining behavior and caused compilation failure as some branch +* target distance exceeded 16bit representation which is the maximum for +* cpu v1/v2/v3. Macro __bpf_cpu_version__ is implemented in llvm18 to specify +* which cpu version is used for compilation. So we can set a smaller +* unroll_count if __bpf_cpu_version__ is less than 4, which reduced +* some branch target distances and resolved the compilation failure. +*/ +#if defined(__bpf_cpu_version__) && __bpf_cpu_version__ <4
probably should be combined with __clang_major__ >= 18 check too.
Quoted 8 lines
tools/testing/selftests/bpf/progs/pyperf180.c +3
-0+#define UNROLL_COUNT 90 +#endif +#include "pyperf.h" Quoted 4 lines
Tested-by Eduard Zingerman <[email protected]>
Yonghong Song
reviewerQuoted 62 lines · Alexei Starovoitov
Quoted 37 lines · Yonghong Song
Quoted 35 lines · Eduard Zingerman
Quoted 24 lines · Yonghong Song
With latest llvm18 (main branch of llvm-project repo), when building bpf selftests,
[~/work/bpf-next (master)]$ make -Ctools/testing/selftests/bpfLLVM=1 -jThe 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 -cprogs/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 llvm18.
Quoted 7 lines
To workaround the issue, previously all 180 loop iterations are fully unrolled. Now, the fully unrolling count is changed to 90 for llvm18 and later. This reduced some otherwise long branch target distance, and fixed the compilation failure.
[1] https://github.com/llvm/llvm-project/commit/1a2e77cf9e11dbf56b5720c607313a566eebb16eSigned-off-by Yonghong Song <[email protected]>Can confirm, the issue is present on clang main w/o this patch and disappears after this patch.
Quoted 8 lines
Quoted 1 line
Yonghong, is there a way to keep original
UNROLL_COUNTif cpuv4 is used?I thought about this but a little bit lazy so not giving it enough throught. But since you mentioned this, I think adding a macro to indicate cpu version by llvm is a good idea. This will give bpf developers some flexibility to add new features (new cpu variant) or workaround bugs (for a particular cpu variant but not impacting others if they are fine), etc.
So here is the llvm patch: https://github.com/llvm/llvm-project/pull/71856
Great idea. Commented on the diff.
Quoted 14 lines
With the above llvm patch, the following code change should work:
tools/testing/selftests/bpf/progs/pyperf180.c +4
-0@@ -1,4 +1,18 @@// SPDX-License-Identifier: GPL-2.0 // Copyright (c) 2019 Facebook #define STACK_MAX_LEN 180 + +/* llvm upstream commit at llvm18 +* https://github.com/llvm/llvm-project/commit/1a2e77cf9e11dbf56b5720c607313a566eebb16e +* changed inlining behavior and caused compilation failure as some branch
Quoted 8 lines
tools/testing/selftests/bpf/progs/pyperf180.c line 14
Quoted 7 lines
tools/testing/selftests/bpf/progs/pyperf180.c +7
-0+* target distance exceeded 16bit representation which is the maximum for +* cpu v1/v2/v3. Macro __bpf_cpu_version__ is implemented in llvm18 to specify +* which cpu version is used for compilation. So we can set a smaller +* unroll_count if __bpf_cpu_version__ is less than 4, which reduced +* some branch target distances and resolved the compilation failure. +*/ +#if defined(__bpf_cpu_version__) && __bpf_cpu_version__ <4 probably should be combined with
__clang_major__>= 18 check too.
Okay, I could do this to catch the case where somebody uses development llvm18 which has this regression but __bpf_cpu_version__ is not introduced yet.
Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/bpf/progs/pyperf180.c +3
-0+#define UNROLL_COUNT 90 +#endif +#include "pyperf.h" Quoted 3 lines
Tested-by Eduard Zingerman <[email protected]>
Quoted 8 lines · Yonghong Song
Quoted 4 lines
Quoted 3 lines
+ */
+#ifdefined(__bpf_cpu_version__)&&__bpf_cpu_version__< 4probably should be combined with
__clang_major__>= 18 check too.Okay, I could do this to catch the case where somebody uses development llvm18 which has this regression but
__bpf_cpu_version__is not introduced yet.
Exactly. That's what I tried to say.