Skip to content

Commit a7137b2

Browse files
committed
[BPF] Add support for floats and doubles
Some BPF programs compiled on s390 fail to load, because s390 arch-specific linux headers contain float and double types. At the moment there is no BTF_KIND for floats and doubles, so the release version of LLVM ends up emitting type id 0 for them, which the in-kernel verifier does not accept. Introduce support for such types to libbpf by representing them using the new BTF_KIND_FLOAT. Reviewed By: yonghong-song Differential Revision: https://reviews.llvm.org/D83289
1 parent e67d91f commit a7137b2

File tree

5 files changed

+160
-9
lines changed

5 files changed

+160
-9
lines changed

llvm/lib/Target/BPF/BTF.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ HANDLE_BTF_KIND(12, FUNC)
3030
HANDLE_BTF_KIND(13, FUNC_PROTO)
3131
HANDLE_BTF_KIND(14, VAR)
3232
HANDLE_BTF_KIND(15, DATASEC)
33+
HANDLE_BTF_KIND(16, FLOAT)
3334

3435
#undef HANDLE_BTF_KIND

llvm/lib/Target/BPF/BTFDebug.cpp

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,21 @@ void BTFKindDataSec::emitType(MCStreamer &OS) {
371371
}
372372
}
373373

374+
BTFTypeFloat::BTFTypeFloat(uint32_t SizeInBits, StringRef TypeName)
375+
: Name(TypeName) {
376+
Kind = BTF::BTF_KIND_FLOAT;
377+
BTFType.Info = Kind << 24;
378+
BTFType.Size = roundupToBytes(SizeInBits);
379+
}
380+
381+
void BTFTypeFloat::completeType(BTFDebug &BDebug) {
382+
if (IsCompleted)
383+
return;
384+
IsCompleted = true;
385+
386+
BTFType.NameOff = BDebug.addString(Name);
387+
}
388+
374389
uint32_t BTFStringTable::addString(StringRef S) {
375390
// Check whether the string already exists.
376391
for (auto &OffsetM : OffsetToIdMap) {
@@ -409,18 +424,28 @@ uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry) {
409424
}
410425

411426
void BTFDebug::visitBasicType(const DIBasicType *BTy, uint32_t &TypeId) {
412-
// Only int types are supported in BTF.
427+
// Only int and binary floating point types are supported in BTF.
413428
uint32_t Encoding = BTy->getEncoding();
414-
if (Encoding != dwarf::DW_ATE_boolean && Encoding != dwarf::DW_ATE_signed &&
415-
Encoding != dwarf::DW_ATE_signed_char &&
416-
Encoding != dwarf::DW_ATE_unsigned &&
417-
Encoding != dwarf::DW_ATE_unsigned_char)
429+
std::unique_ptr<BTFTypeBase> TypeEntry;
430+
switch (Encoding) {
431+
case dwarf::DW_ATE_boolean:
432+
case dwarf::DW_ATE_signed:
433+
case dwarf::DW_ATE_signed_char:
434+
case dwarf::DW_ATE_unsigned:
435+
case dwarf::DW_ATE_unsigned_char:
436+
// Create a BTF type instance for this DIBasicType and put it into
437+
// DIToIdMap for cross-type reference check.
438+
TypeEntry = std::make_unique<BTFTypeInt>(
439+
Encoding, BTy->getSizeInBits(), BTy->getOffsetInBits(), BTy->getName());
440+
break;
441+
case dwarf::DW_ATE_float:
442+
TypeEntry =
443+
std::make_unique<BTFTypeFloat>(BTy->getSizeInBits(), BTy->getName());
444+
break;
445+
default:
418446
return;
447+
}
419448

420-
// Create a BTF type instance for this DIBasicType and put it into
421-
// DIToIdMap for cross-type reference check.
422-
auto TypeEntry = std::make_unique<BTFTypeInt>(
423-
Encoding, BTy->getSizeInBits(), BTy->getOffsetInBits(), BTy->getName());
424449
TypeId = addType(std::move(TypeEntry), BTy);
425450
}
426451

llvm/lib/Target/BPF/BTFDebug.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,15 @@ class BTFKindDataSec : public BTFTypeBase {
195195
void emitType(MCStreamer &OS) override;
196196
};
197197

198+
/// Handle binary floating point type.
199+
class BTFTypeFloat : public BTFTypeBase {
200+
StringRef Name;
201+
202+
public:
203+
BTFTypeFloat(uint32_t SizeInBits, StringRef TypeName);
204+
void completeType(BTFDebug &BDebug) override;
205+
};
206+
198207
/// String table.
199208
class BTFStringTable {
200209
/// String table size in bytes.

llvm/test/CodeGen/BPF/BTF/double.ll

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
; RUN: llc -march=bpfel -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK %s
2+
; RUN: llc -march=bpfeb -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK %s
3+
4+
; Source code:
5+
; double a;
6+
; Compilation flag:
7+
; clang -target bpf -O2 -g -S -emit-llvm t.c
8+
9+
@a = dso_local local_unnamed_addr global double 0.000000e+00, align 8, !dbg !0
10+
11+
!llvm.dbg.cu = !{!2}
12+
!llvm.module.flags = !{!7, !8, !9}
13+
!llvm.ident = !{!10}
14+
15+
; CHECK: .section .BTF,"",@progbits
16+
; CHECK-NEXT: .short 60319 # 0xeb9f
17+
; CHECK-NEXT: .byte 1
18+
; CHECK-NEXT: .byte 0
19+
; CHECK-NEXT: .long 24
20+
; CHECK-NEXT: .long 0
21+
; CHECK-NEXT: .long 52
22+
; CHECK-NEXT: .long 52
23+
; CHECK-NEXT: .long 15
24+
; [1] double, size=8 bytes (64 bits)
25+
; CHECK-NEXT: .long 1 # BTF_KIND_FLOAT(id = 1)
26+
; CHECK-NEXT: .long 268435456 # 0x10000000
27+
; CHECK-NEXT: .long 8
28+
; [2] a, type=double (1), global
29+
; CHECK-NEXT: .long 8 # BTF_KIND_VAR(id = 2)
30+
; CHECK-NEXT: .long 234881024 # 0xe000000
31+
; CHECK-NEXT: .long 1
32+
; CHECK-NEXT: .long 1
33+
; [3] .bss, 1 var, {a, offset=&a, size=8 bytes}
34+
; CHECK-NEXT: .long 10 # BTF_KIND_DATASEC(id = 3)
35+
; CHECK-NEXT: .long 251658241 # 0xf000001
36+
; CHECK-NEXT: .long 0
37+
; CHECK-NEXT: .long 2
38+
; CHECK-NEXT: .long a
39+
; CHECK-NEXT: .long 8
40+
; CHECK-NEXT: .byte 0 # string offset=0
41+
; CHECK-NEXT: .ascii "double" # string offset=1
42+
; CHECK-NEXT: .byte 0
43+
; CHECK-NEXT: .byte 97 # string offset=8
44+
; CHECK-NEXT: .byte 0
45+
; CHECK-NEXT: .ascii ".bss" # string offset=10
46+
; CHECK-NEXT: .byte 0
47+
48+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
49+
!1 = distinct !DIGlobalVariable(name: "a", scope: !2, file: !3, line: 1, type: !6, isLocal: false, isDefinition: true)
50+
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 11.0.0 ", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, splitDebugInlining: false, nameTableKind: None)
51+
!3 = !DIFile(filename: "t.c", directory: "/home/yhs/tmp")
52+
!4 = !{}
53+
!5 = !{!0}
54+
!6 = !DIBasicType(name: "double", size: 64, encoding: DW_ATE_float)
55+
!7 = !{i32 7, !"Dwarf Version", i32 4}
56+
!8 = !{i32 2, !"Debug Info Version", i32 3}
57+
!9 = !{i32 1, !"wchar_size", i32 4}
58+
!10 = !{!"clang version 11.0.0 "}

llvm/test/CodeGen/BPF/BTF/float.ll

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
; RUN: llc -march=bpfel -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK %s
2+
; RUN: llc -march=bpfeb -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK %s
3+
4+
; Source code:
5+
; float a;
6+
; Compilation flag:
7+
; clang -target bpf -O2 -g -S -emit-llvm t.c
8+
9+
@a = dso_local local_unnamed_addr global float 0.000000e+00, align 4, !dbg !0
10+
11+
!llvm.dbg.cu = !{!2}
12+
!llvm.module.flags = !{!7, !8, !9}
13+
!llvm.ident = !{!10}
14+
15+
; CHECK: .section .BTF,"",@progbits
16+
; CHECK-NEXT: .short 60319 # 0xeb9f
17+
; CHECK-NEXT: .byte 1
18+
; CHECK-NEXT: .byte 0
19+
; CHECK-NEXT: .long 24
20+
; CHECK-NEXT: .long 0
21+
; CHECK-NEXT: .long 52
22+
; CHECK-NEXT: .long 52
23+
; CHECK-NEXT: .long 14
24+
; [1] float, size=4 bytes (32 bits)
25+
; CHECK-NEXT: .long 1 # BTF_KIND_FLOAT(id = 1)
26+
; CHECK-NEXT: .long 268435456 # 0x10000000
27+
; CHECK-NEXT: .long 4
28+
; [2] a, type=float (1), global
29+
; CHECK-NEXT: .long 7 # BTF_KIND_VAR(id = 2)
30+
; CHECK-NEXT: .long 234881024 # 0xe000000
31+
; CHECK-NEXT: .long 1
32+
; CHECK-NEXT: .long 1
33+
; [3] .bss, 1 var, {a, offset=&a, size=4 bytes}
34+
; CHECK-NEXT: .long 9 # BTF_KIND_DATASEC(id = 3)
35+
; CHECK-NEXT: .long 251658241 # 0xf000001
36+
; CHECK-NEXT: .long 0
37+
; CHECK-NEXT: .long 2
38+
; CHECK-NEXT: .long a
39+
; CHECK-NEXT: .long 4
40+
; CHECK-NEXT: .byte 0 # string offset=0
41+
; CHECK-NEXT: .ascii "float" # string offset=1
42+
; CHECK-NEXT: .byte 0
43+
; CHECK-NEXT: .byte 97 # string offset=7
44+
; CHECK-NEXT: .byte 0
45+
; CHECK-NEXT: .ascii ".bss" # string offset=9
46+
; CHECK-NEXT: .byte 0
47+
48+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
49+
!1 = distinct !DIGlobalVariable(name: "a", scope: !2, file: !3, line: 1, type: !6, isLocal: false, isDefinition: true)
50+
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 11.0.0 ", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, splitDebugInlining: false, nameTableKind: None)
51+
!3 = !DIFile(filename: "t.c", directory: "/home/yhs/tmp")
52+
!4 = !{}
53+
!5 = !{!0}
54+
!6 = !DIBasicType(name: "float", size: 32, encoding: DW_ATE_float)
55+
!7 = !{i32 7, !"Dwarf Version", i32 4}
56+
!8 = !{i32 2, !"Debug Info Version", i32 3}
57+
!9 = !{i32 1, !"wchar_size", i32 4}
58+
!10 = !{!"clang version 11.0.0 "}

0 commit comments

Comments
 (0)