[llvm] [BPF] Use ".L" local prefix label for basic blocks (PR #95103)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 26 21:09:34 PDT 2024


yonghong-song wrote:

@aengelke This patch does not really work. I downloaded [aengelke:bpf-private-label-prefix](https://github.com/aengelke/llvm-project/tree/bpf-private-label-prefix) and build the clang/llvm tools.  I hit the following errors:

```
$ cat test.c
int test(int a, int b, int *p1, int *p2) {
   if (a < b) *p1 = a * b;
   else if (a > b) *p2 = a * b * a;
   return a * a;
}
$ ~/tmp3/llvm-project/llvm/build/install/bin/clang --target=bpf -O2 -c -g test.c
error: backend data layout 'e-m:b-p:64:64-i64:64-i128:128-n32:64-S128' does not match expected target description 'e-m:e-p:64:64-i64:64-i128:128-n32:64-S128'
1 error generated.
$
```

I then applied the following change,
```
diff --git a/clang/lib/Basic/Targets/BPF.h b/clang/lib/Basic/Targets/BPF.h
index d19b37dd4df7..00909afb8093 100644
--- a/clang/lib/Basic/Targets/BPF.h
+++ b/clang/lib/Basic/Targets/BPF.h
@@ -35,9 +35,9 @@ public:
     Int64Type = SignedLong;
     RegParmMax = 5;
     if (Triple.getArch() == llvm::Triple::bpfeb) {
-      resetDataLayout("E-m:e-p:64:64-i64:64-i128:128-n32:64-S128");
+      resetDataLayout("E-m:b-p:64:64-i64:64-i128:128-n32:64-S128");
     } else {
-      resetDataLayout("e-m:e-p:64:64-i64:64-i128:128-n32:64-S128");
+      resetDataLayout("e-m:b-p:64:64-i64:64-i128:128-n32:64-S128");
     }
     MaxAtomicPromoteWidth = 64;
     MaxAtomicInlineWidth = 64;
```
and compilation can now pass. However, I didn't see L* lables in the symbol table:
```
$ ~/tmp3/llvm-project/llvm/build/install/bin/clang --target=bpf -O2 -c -g test.c
$ ~/tmp3/llvm-project/llvm/build/install/bin/llvm-readelf -s test.o

Symbol table '.symtab' contains 12 entries:
   Num:    Value          Size Type    Bind   Vis       Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT   UND 
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT   ABS test.c
     2: 0000000000000000     0 SECTION LOCAL  DEFAULT     2 .text
     3: 0000000000000000     0 SECTION LOCAL  DEFAULT     3 .debug_loclists
     4: 0000000000000000     0 SECTION LOCAL  DEFAULT     4 .debug_abbrev
     5: 0000000000000000     0 SECTION LOCAL  DEFAULT     7 .debug_str_offsets
     6: 0000000000000000     0 SECTION LOCAL  DEFAULT     9 .debug_str
     7: 0000000000000000     0 SECTION LOCAL  DEFAULT    10 .debug_addr
     8: 0000000000000000     0 SECTION LOCAL  DEFAULT    15 .debug_frame
     9: 0000000000000000     0 SECTION LOCAL  DEFAULT    17 .debug_line
    10: 0000000000000000     0 SECTION LOCAL  DEFAULT    19 .debug_line_str
    11: 0000000000000000   136 FUNC    GLOBAL DEFAULT     2 test
$ ~/tmp3/llvm-project/llvm/build/install/bin/llvm-objdump -d test.o

test.o: file format elf64-bpf

Disassembly of section .text:

0000000000000000 <test>:
       0:       bf 25 00 00 00 00 00 00 r5 = r2
       1:       67 05 00 00 20 00 00 00 r5 <<= 0x20
       2:       c7 05 00 00 20 00 00 00 r5 s>>= 0x20
       3:       bf 16 00 00 00 00 00 00 r6 = r1
       4:       67 06 00 00 20 00 00 00 r6 <<= 0x20
       5:       c7 06 00 00 20 00 00 00 r6 s>>= 0x20
       6:       bf 10 00 00 00 00 00 00 r0 = r1
       7:       2f 00 00 00 00 00 00 00 r0 *= r0
       8:       7d 56 03 00 00 00 00 00 if r6 s>= r5 goto +0x3 <test+0x60>
       9:       2f 12 00 00 00 00 00 00 r2 *= r1
      10:       63 23 00 00 00 00 00 00 *(u32 *)(r3 + 0x0) = r2
      11:       05 00 04 00 00 00 00 00 goto +0x4 <test+0x80>
      12:       7d 65 03 00 00 00 00 00 if r5 s>= r6 goto +0x3 <test+0x80>
      13:       bf 01 00 00 00 00 00 00 r1 = r0
      14:       2f 21 00 00 00 00 00 00 r1 *= r2
      15:       63 14 00 00 00 00 00 00 *(u32 *)(r4 + 0x0) = r1
      16:       95 00 00 00 00 00 00 00 exit
```

If I add '-Wa,-L' in the command line below, the L* labels will be in symbol
and those labels are in llvm-objdump as well.
```
$ ~/tmp3/llvm-project/llvm/build/install/bin/clang --target=bpf -O2 -c -g test.c -Wa,-L                                     
$ ~/tmp3/llvm-project/llvm/build/install/bin/llvm-readelf -s test.o                                                         
                                                                                                                                                       
Symbol table '.symtab' contains 15 entries:                                                                                                            
   Num:    Value          Size Type    Bind   Vis       Ndx Name                                                                                       
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT   UND                                                                                            
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT   ABS test.c                                                                                     
     2: 0000000000000000     0 SECTION LOCAL  DEFAULT     2 .text                                                                                      
     3: 0000000000000060     0 NOTYPE  LOCAL  DEFAULT     2 LBB0_2                                                                                     
     4: 0000000000000080     0 NOTYPE  LOCAL  DEFAULT     2 LBB0_4                                                                                     
     5: 0000000000000000     0 SECTION LOCAL  DEFAULT     3 .debug_loclists                                                                            
     6: 0000000000000000     0 SECTION LOCAL  DEFAULT     4 .debug_abbrev
     7: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT    17 Lline_table_start0
     8: 0000000000000000     0 SECTION LOCAL  DEFAULT     7 .debug_str_offsets
     9: 0000000000000000     0 SECTION LOCAL  DEFAULT     9 .debug_str
    10: 0000000000000000     0 SECTION LOCAL  DEFAULT    10 .debug_addr
    11: 0000000000000000     0 SECTION LOCAL  DEFAULT    15 .debug_frame
    12: 0000000000000000     0 SECTION LOCAL  DEFAULT    17 .debug_line
    13: 0000000000000000     0 SECTION LOCAL  DEFAULT    19 .debug_line_str 
    14: 0000000000000000   136 FUNC    GLOBAL DEFAULT     2 test
$ ~/tmp3/llvm-project/llvm/build/install/bin/llvm-objdump -d test.o

test.o: file format elf64-bpf

Disassembly of section .text:

0000000000000000 <test>:
       0:       bf 25 00 00 00 00 00 00 r5 = r2
       1:       67 05 00 00 20 00 00 00 r5 <<= 0x20
       2:       c7 05 00 00 20 00 00 00 r5 s>>= 0x20
       3:       bf 16 00 00 00 00 00 00 r6 = r1
       4:       67 06 00 00 20 00 00 00 r6 <<= 0x20
       5:       c7 06 00 00 20 00 00 00 r6 s>>= 0x20
       6:       bf 10 00 00 00 00 00 00 r0 = r1
       7:       2f 00 00 00 00 00 00 00 r0 *= r0
       8:       7d 56 03 00 00 00 00 00 if r6 s>= r5 goto +0x3 <LBB0_2>
       9:       2f 12 00 00 00 00 00 00 r2 *= r1
      10:       63 23 00 00 00 00 00 00 *(u32 *)(r3 + 0x0) = r2
      11:       05 00 04 00 00 00 00 00 goto +0x4 <LBB0_4>

0000000000000060 <LBB0_2>:
      12:       7d 65 03 00 00 00 00 00 if r5 s>= r6 goto +0x3 <LBB0_4>
      13:       bf 01 00 00 00 00 00 00 r1 = r0
      14:       2f 21 00 00 00 00 00 00 r1 *= r2
      15:       63 14 00 00 00 00 00 00 *(u32 *)(r4 + 0x0) = r1

0000000000000080 <LBB0_4>:
      16:       95 00 00 00 00 00 00 00 exit
```

So to ensure L* labels in symbol table, one way is to add -Wa,-L in compilation flags. But that is a little bit tedious, so maybe we can find some llvm internal setting which allows target to overwrite in order to enable L* labels in symbol table. I have not poked into llvm internal yet.

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


More information about the llvm-commits mailing list