mbox series

[v2,0/6] btf: refactor and add pruning option

Message ID 20240502171132.95601-1-david.faust@oracle.com
Headers show
Series btf: refactor and add pruning option | expand

Message

David Faust May 2, 2024, 5:11 p.m. UTC
[Changes from v1: Fix missing btfout.cc changes in patch 3 which
 prevent the patch from building as reported by Linaro CI.  Those
 changes are then removed/overwritten in patch 4, so the resulting
 code before patch 3 and after patch 4 is identical to v1. ]

This patch series signficantly refactors the BTF generation in gcc,
making it simpler and easier to understand, extend and maintain.

It also introduces an optional algorithm to "prune" BTF information
before emission.  This pruning is meant to be used for BPF programs,
to alleviate the massive bloating of BPF progams caused by including
Linux kernel internal headers.  The pruning is designed to be almost
equivalent to that performed unconditionally by the LLVM BPF backend
when generating BTF information, for compatibility purposes.

While the changes are fairly significant, there is very little actual
change in emitted BTF information (unless pruning is enabled), other
than bug fixes and additions to the assembler debug comments.

Patch 1 restructures the emission of CTF and BTF information, with the
result that CTF is always completely generated and emitted before any
BTF-related procedures are run.

Patch 2 takes advantage of that change, and removes the restriction on
generating both CTF and BTF in the same compiler run, allowing for any
combinaion of -gdwarf, -gctf and -gbtf.

Patch 3 changes the data structures shared by CTF and BTF to use
pointers rather than type IDs for all inter-type references.  This
change is completely transparent to both CTF and BTF.

Patch 4 heavily refactors btfout.cc to take advantage of the prior
changes and significantly simplify the BTF implementation.  The changes
are nearly transparent, however some small but important improvements
are also made possible by the refactor, such as fixing PR113566.

Patch 5 adds a new option to perform pruning of the BTF information
before emission.  This is intended to be used for BPF programs which
often include kernel headers, and in many cases reduces the size of
the resulting BTF information by a factor of 10.

Patch 6 makes BTF pruning work with BPF CO-RE, and enables the pruning
by default in the BPF backend.

Tested on x86_64-linux-gnu, and on x86_64-linux-gnu host for
bpf-unknown-none target.

Also heavily tested with Linux kernel BPF selftests.
No known regressions.

David Faust (6):
  ctf, btf: restructure CTF/BTF emission
  opts: allow any combination of DWARF, CTF, BTF
  ctf: use pointers instead of IDs internally
  btf: refactor and simplify implementation
  btf: add -fprune-btf option
  bpf,btf: enable BTF pruning by default for BPF

 gcc/btfout.cc                                 | 1645 ++++++++++-------
 gcc/common.opt                                |    4 +
 gcc/config/bpf/bpf.cc                         |    5 +
 gcc/config/bpf/btfext-out.cc                  |   12 +-
 gcc/config/bpf/core-builtins.cc               |   73 +-
 gcc/ctfc.cc                                   |  139 +-
 gcc/ctfc.h                                    |   86 +-
 gcc/ctfout.cc                                 |   19 +-
 gcc/doc/invoke.texi                           |   23 +
 gcc/dwarf2ctf.cc                              |  300 ++-
 gcc/dwarf2ctf.h                               |    2 +-
 gcc/dwarf2out.cc                              |    4 +-
 gcc/opts.cc                                   |   19 +-
 gcc/testsuite/gcc.dg/debug/btf/btf-prune-1.c  |   25 +
 gcc/testsuite/gcc.dg/debug/btf/btf-prune-2.c  |   33 +
 gcc/testsuite/gcc.dg/debug/btf/btf-prune-3.c  |   35 +
 .../gcc.dg/debug/btf/btf-variables-5.c        |    2 +-
 17 files changed, 1399 insertions(+), 1027 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/debug/btf/btf-prune-1.c
 create mode 100644 gcc/testsuite/gcc.dg/debug/btf/btf-prune-2.c
 create mode 100644 gcc/testsuite/gcc.dg/debug/btf/btf-prune-3.c