Skip to content

Commit

Permalink
selftests/bpf: Fix pyperf180 compilation failure with llvm18
Browse files Browse the repository at this point in the history
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] llvm/llvm-project@1a2e77c

Signed-off-by: Yonghong Song <[email protected]>
Tested-by: Eduard Zingerman <[email protected]>
  • Loading branch information
Yonghong Song authored and d-e-s-o committed Nov 9, 2023
1 parent d955544 commit 74ae083
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tools/testing/selftests/bpf/progs/pyperf180.c
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 74ae083

Please sign in to comment.