[llvm] [BPF] Support Jump Table (PR #149715)

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 3 11:08:17 PDT 2025


================
@@ -0,0 +1,64 @@
+; RUN: llc -march=bpf -mcpu=v4 < %s | FileCheck %s
+;
+; Source code:
+;    int bar(int a) {
+;       __label__ l1, l2;
+;       void * volatile tgt;
+;       int ret = 0;
+;       if (a)
+;         tgt = &&l1; // synthetic jump table generated here
+;       else
+;         tgt = &&l2; // another synthetic jump table
+;       goto *tgt;
+;   l1: ret += 1;
+;   l2: ret += 2;
+;       return ret;
+;     }
+;
+; Compilation Flags:
----------------
yonghong-song wrote:

I tried the following:
```
$ cat test/CodeGen/BPF/jump_table_blockaddr.ll
; RUN: rm -rf %t && split-file %s %t && cd %t
; RUN: llc -march=bpf -mcpu=v4 < %s | FileCheck %s

; CHECK:     bar

;--- test.c
int bar(int a) {
    __label__ l1, l2;
    void * volatile tgt;
    int ret = 0;
    if (a)
      tgt = &&l1; // synthetic jump table generated here
    else
      tgt = &&l2; // another synthetic jump table
    goto *tgt;
l1: ret += 1;
l2: ret += 2;
    return ret;
}
;--- gen
clang --target=bpf -mcpu=v4 -O2 -emit-llvm -S test.c -o -

;--- test.ll
```

And run `utils/update_test_body.py test/CodeGen/BPF/jump_table_blockaddr.ll`. It does generate .ll file properly.
```
...
;--- test.ll
; ModuleID = 'test.c'
source_filename = "test.c"
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
target triple = "bpf"

; Function Attrs: nofree norecurse nounwind memory(inaccessiblemem: readwrite)
define dso_local range(i32 2, 4) i32 @bar(i32 noundef %0) local_unnamed_addr #0 {
  %2 = alloca ptr, align 8
  %3 = icmp eq i32 %0, 0
  %4 = select i1 %3, ptr blockaddress(@bar, %7), ptr blockaddress(@bar, %6)
  store volatile ptr %4, ptr %2, align 8, !tbaa !2
  %5 = load volatile ptr, ptr %2, align 8, !tbaa !2
  indirectbr ptr %5, [label %6, label %7]
  ...
  ```
  But with the above generated .ll file, update_llc_test_checks.py not working any more.
  
  ```
  $ utils/update_llc_test_checks.py test/CodeGen/BPF/jump_table_blockaddr.ll 
WARNING: Skipping unparsable RUN line: rm -rf %t && split-file %s %t && cd %t
llc: error: llc: <stdin>:7:1: error: expected top-level entity
int bar(int a) {
...
```

But we do want to have IR in the test and also want to have update_llc_test_checks.py, so for now, I will stick to update_llc_test_checks.py.
  


https://github.com/llvm/llvm-project/pull/149715


More information about the llvm-commits mailing list