llvm-project/llvm/test/CodeGen/BPF/addr-space-phi.ll
4ast 2aacb56e83
BPF address space insn (#84410)
This commit aims to support BPF arena kernel side
[feature](https://lore.kernel.org/bpf/20240209040608.98927-1-alexei.starovoitov@gmail.com/):
- arena is a memory region accessible from both BPF program and
userspace;
- base pointers for this memory region differ between kernel and user
spaces;
- `dst_reg = addr_space_cast(src_reg, dst_addr_space, src_addr_space)`
translates src_reg, a pointer in src_addr_space to dst_reg, equivalent
pointer in dst_addr_space, {src,dst}_addr_space are immediate constants;
- number 0 is assigned to kernel address space;
- number 1 is assigned to user address space.

On the LLVM side, the goal is to make load and store operations on arena
pointers "transparent" for BPF programs:
- assume that pointers with non-zero address space are pointers to
  arena memory;
- assume that arena is identified by address space number;
- assume that address space zero corresponds to kernel address space;
- assume that every BPF-side load or store from arena is done via
pointer in user address space, thus convert base pointers using
`addr_space_cast(src_reg, 0, 1)`;

Only load, store, cmpxchg and atomicrmw IR instructions are handled by
this transformation.

For example, the following C code:

```c
   #define __as __attribute__((address_space(1)))
   void copy(int __as *from, int __as *to) { *to = *from; }
```

Compiled to the following IR:

```llvm
    define void @copy(ptr addrspace(1) %from, ptr addrspace(1) %to) {
    entry:
      %0 = load i32, ptr addrspace(1) %from, align 4
      store i32 %0, ptr addrspace(1) %to, align 4
      ret void
    }
```

Is transformed to:

```llvm
    %to2 = addrspacecast ptr addrspace(1) %to to ptr     ;; !
    %from1 = addrspacecast ptr addrspace(1) %from to ptr ;; !
    %0 = load i32, ptr %from1, align 4, !tbaa !3
    store i32 %0, ptr %to2, align 4, !tbaa !3
    ret void
```

And compiled as:

```asm
    r2 = addr_space_cast(r2, 0, 1)
    r1 = addr_space_cast(r1, 0, 1)
    r1 = *(u32 *)(r1 + 0)
    *(u32 *)(r2 + 0) = r1
    exit
```

Co-authored-by: Eduard Zingerman <eddyz87@gmail.com>
2024-03-13 02:27:25 +02:00

54 lines
1.6 KiB
LLVM

; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
; RUN: opt --bpf-check-and-opt-ir -S -mtriple=bpf-pc-linux < %s | FileCheck %s
; Generated from the following C code:
;
; #define __uptr __attribute__((address_space(1)))
;
; extern int __uptr *magic1();
; extern int __uptr *magic2();
;
; void test(long i) {
; int __uptr *a;
;
; if (i > 42)
; a = magic1();
; else
; a = magic2();
; a[5] = 7;
; }
;
; Using the following command:
;
; clang --target=bpf -O2 -S -emit-llvm -o t.ll t.c
define void @test(i64 noundef %i) {
; CHECK: if.end:
; CHECK-NEXT: [[A_0:%.*]] = phi ptr addrspace(1)
; CHECK-NEXT: [[A_01:%.*]] = addrspacecast ptr addrspace(1) [[A_0]] to ptr
; CHECK-NEXT: [[ARRAYIDX2:%.*]] = getelementptr inbounds i32, ptr [[A_01]], i64 5
; CHECK-NEXT: store i32 7, ptr [[ARRAYIDX2]], align 4
; CHECK-NEXT: ret void
;
entry:
%cmp = icmp sgt i64 %i, 42
br i1 %cmp, label %if.then, label %if.else
if.then: ; preds = %entry
%call = tail call ptr addrspace(1) @magic1()
br label %if.end
if.else: ; preds = %entry
%call1 = tail call ptr addrspace(1) @magic2()
br label %if.end
if.end: ; preds = %if.else, %if.then
%a.0 = phi ptr addrspace(1) [ %call, %if.then ], [ %call1, %if.else ]
%arrayidx = getelementptr inbounds i32, ptr addrspace(1) %a.0, i64 5
store i32 7, ptr addrspace(1) %arrayidx, align 4
ret void
}
declare ptr addrspace(1) @magic1(...)
declare ptr addrspace(1) @magic2(...)