Files
clang-p2996/llvm/test/CodeGen/BPF/objdump_imm_hex.ll
yonghong-song 03958680b2 [BPF] Make llvm-objdump disasm default cpu v4 (#102166)
Currently, with the following example,
  $ cat t.c
  void foo(int a, _Atomic int *b)
  {
   *b &= a;
  }
  $ clang --target=bpf -O2 -c -mcpu=v3 t.c
  $ llvm-objdump -d t.o
  t.o:    file format elf64-bpf

  Disassembly of section .text:

  0000000000000000 <foo>:
       0:       c3 12 00 00 51 00 00 00 <unknown>
       1:       95 00 00 00 00 00 00 00 exit

Basically, the default cpu for llvm-objdump is v1 and it won't be able
to decode insn properly.

If we add --mcpu=v3 to llvm-objdump command line, we will have
  $ llvm-objdump -d --mcpu=v3 t.o

  t.o:    file format elf64-bpf

  Disassembly of section .text:

  0000000000000000 <foo>:
0: c3 12 00 00 51 00 00 00 w1 = atomic_fetch_and((u32 *)(r2 + 0x0), w1)
       1:       95 00 00 00 00 00 00 00 exit

The atomic_fetch_and insn can be decoded properly. Using latest cpu
version --mcpu=v4 can also decode properly like the above --mcpu=v3.

To avoid the above '<unknown>' decoding with common 'llvm-objdump -d
t.o', this patch marked the default cpu for llvm-objdump with the
current highest cpu number v4 in ELFObjectFileBase::tryGetCPUName(). The
cpu number in ELFObjectFileBase::tryGetCPUName() will be adjusted in the
future if cpu number is increased e.g. v5 etc. Such an approach also
aligns with gcc-bpf as discussed in [1].

Six bpf unit tests are affected with this change. I changed test output
for three unit tests and added --mcpu=v1 for the other three unit tests,
to demonstrate the default (cpu v4) behavior and explicit --mcpu=v1
behavior.

[1]
https://lore.kernel.org/bpf/6f32c0a1-9de2-4145-92ea-be025362182f@linux.dev/T/#m0f7e63c390bc8f5a5523e7f2f0537becd4205200

Co-authored-by: Yonghong Song <yonghong.song@linux.dev>
2024-08-06 18:23:46 -07:00

66 lines
2.3 KiB
LLVM

; RUN: llc -mtriple=bpfel -filetype=obj -o - %s | llvm-objdump --no-print-imm-hex -d - | FileCheck --check-prefix=CHECK-DEC %s
; RUN: llc -mtriple=bpfel -filetype=obj -o - %s | llvm-objdump -d --print-imm-hex - | FileCheck --check-prefix=CHECK-HEX %s
; Source Code:
; int gbl;
; int test(unsigned long long a, unsigned long long b) {
; int ret = 0;
; if (a == 0xABCDABCDabcdabcdULL) {
; gbl = gbl * gbl * 2;
; ret = 1;
; goto out;
; }
; if (b == 0xABCDabcdabcdULL) {
; gbl = gbl * 4;
; ret = 2;
; }
; out:
; return ret;
; }
@gbl = common local_unnamed_addr global i32 0, align 4
; Function Attrs: norecurse nounwind
define i32 @test(i64, i64) local_unnamed_addr #0 {
; CHECK-LABEL: test
%3 = icmp eq i64 %0, -6067004223159161907
br i1 %3, label %4, label %8
; CHECK-DEC: 18 03 00 00 cd ab cd ab 00 00 00 00 cd ab cd ab r3 = -6067004223159161907 ll
; CHECK-DEC: 5d 31 07 00 00 00 00 00 if r1 != r3 goto +7
; CHECK-HEX: 18 03 00 00 cd ab cd ab 00 00 00 00 cd ab cd ab r3 = -0x5432543254325433 ll
; CHECK-HEX: 5d 31 07 00 00 00 00 00 if r1 != r3 goto +0x7
; <label>:4: ; preds = %2
%5 = load i32, ptr @gbl, align 4
%6 = shl i32 %5, 1
; CHECK-DEC: 67 01 00 00 01 00 00 00 r1 <<= 1
; CHECK-HEX: 67 01 00 00 01 00 00 00 r1 <<= 0x1
%7 = mul i32 %6, %5
br label %13
; <label>:8: ; preds = %2
%9 = icmp eq i64 %1, 188899839028173
; CHECK-DEC: 18 01 00 00 cd ab cd ab 00 00 00 00 cd ab 00 00 r1 = 188899839028173 ll
; CHECK-HEX: 18 01 00 00 cd ab cd ab 00 00 00 00 cd ab 00 00 r1 = 0xabcdabcdabcd ll
br i1 %9, label %10, label %16
; <label>:10: ; preds = %8
%11 = load i32, ptr @gbl, align 4
%12 = shl nsw i32 %11, 2
br label %13
; <label>:13: ; preds = %4, %10
%14 = phi i32 [ %12, %10 ], [ %7, %4 ]
%15 = phi i32 [ 2, %10 ], [ 1, %4 ]
store i32 %14, ptr @gbl, align 4
; CHECK-DEC: 63 12 00 00 00 00 00 00 *(u32 *)(r2 + 0) = w1
; CHECK-HEX: 63 12 00 00 00 00 00 00 *(u32 *)(r2 + 0x0) = w1
br label %16
; <label>:16: ; preds = %13, %8
%17 = phi i32 [ 0, %8 ], [ %15, %13 ]
ret i32 %17
}
attributes #0 = { norecurse nounwind }