Skip to content

Commit ddb7541

Browse files
yonghong-songkernel-patches-bot
authored andcommitted
samples/bpf: change Makefile to cope with latest llvm
With latest llvm trunk, bpf programs under samples/bpf directory, if using CORE, may experience the following errors: LLVM ERROR: Cannot select: intrinsic %llvm.preserve.struct.access.index PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace. Stack dump: 0. Program arguments: llc -march=bpf -filetype=obj -o samples/bpf/test_probe_write_user_kern.o 1. Running pass 'Function Pass Manager' on module '<stdin>'. 2. Running pass 'BPF DAG->DAG Pattern Instruction Selection' on function '@bpf_prog1' #0 0x000000000183c26c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/data/users/yhs/work/llvm-project/llvm/build.cur/install/bin/llc+0x183c26c) ... #7 0x00000000017c375e (/data/users/yhs/work/llvm-project/llvm/build.cur/install/bin/llc+0x17c375e) #8 0x00000000016a75c5 llvm::SelectionDAGISel::CannotYetSelect(llvm::SDNode*) (/data/users/yhs/work/llvm-project/llvm/build.cur/install/bin/llc+0x16a75c5) #9 0x00000000016ab4f8 llvm::SelectionDAGISel::SelectCodeCommon(llvm::SDNode*, unsigned char const*, unsigned int) (/data/users/yhs/work/llvm-project/llvm/build.cur/install/bin/llc+0x16ab4f8) ... Aborted (core dumped) | llc -march=bpf -filetype=obj -o samples/bpf/test_probe_write_user_kern.o The reason is due to llvm change https://reviews.llvm.org/D87153 where the CORE relocation global generation is moved from the beginning of target dependent optimization (llc) to the beginning of target independent optimization (opt). Since samples/bpf programs did not use vmlinux.h and its clang compilation uses native architecture, we need to adjust arch triple at opt level to do CORE relocation global generation properly. Otherwise, the above error will appear. This patch fixed the issue by introduce opt and llvm-dis to compilation chain, which will do proper CORE relocation global generation as well as O2 level optimization. Tested with llvm10, llvm11 and trunk/llvm12. Acked-by: Andrii Nakryiko <[email protected]> Signed-off-by: Yonghong Song <[email protected]>
1 parent 77b0b16 commit ddb7541

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

samples/bpf/Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ TPROGLDLIBS_xsk_fwd += -pthread
211211
# make M=samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang
212212
LLC ?= llc
213213
CLANG ?= clang
214+
OPT ?= opt
215+
LLVM_DIS ?= llvm-dis
214216
LLVM_OBJCOPY ?= llvm-objcopy
215217
BTF_PAHOLE ?= pahole
216218

@@ -303,6 +305,11 @@ $(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h
303305
# asm/sysreg.h - inline assembly used by it is incompatible with llvm.
304306
# But, there is no easy way to fix it, so just exclude it since it is
305307
# useless for BPF samples.
308+
# below we use long chain of commands, clang | opt | llvm-dis | llc,
309+
# to generate final object file. 'clang' compiles the source into IR
310+
# with native target, e.g., x64, arm64, etc. 'opt' does bpf CORE IR builtin
311+
# processing (llvm12) and IR optimizations. 'llvm-dis' converts
312+
# 'opt' output to IR, and finally 'llc' generates bpf byte code.
306313
$(obj)/%.o: $(src)/%.c
307314
@echo " CLANG-bpf " $@
308315
$(Q)$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(BPF_EXTRA_CFLAGS) \
@@ -314,7 +321,9 @@ $(obj)/%.o: $(src)/%.c
314321
-Wno-address-of-packed-member -Wno-tautological-compare \
315322
-Wno-unknown-warning-option $(CLANG_ARCH_ARGS) \
316323
-I$(srctree)/samples/bpf/ -include asm_goto_workaround.h \
317-
-O2 -emit-llvm -c $< -o -| $(LLC) -march=bpf $(LLC_FLAGS) -filetype=obj -o $@
324+
-O2 -emit-llvm -Xclang -disable-llvm-passes -c $< -o - | \
325+
$(OPT) -O2 -mtriple=bpf-pc-linux | $(LLVM_DIS) | \
326+
$(LLC) -march=bpf $(LLC_FLAGS) -filetype=obj -o $@
318327
ifeq ($(DWARF2BTF),y)
319328
$(BTF_PAHOLE) -J $@
320329
endif

0 commit comments

Comments
 (0)