diff options
author | David Faust <david.faust@oracle.com> | 2024-06-10 10:54:53 -0700 |
---|---|---|
committer | David Faust <david.faust@oracle.com> | 2024-07-02 10:16:12 -0700 |
commit | b8977d928a7a261913dd89db55d4123b6a54ba46 (patch) | |
tree | e375ac2add88242b6b533a568c4290151cfa7190 /gcc/doc | |
parent | 616c44f02b574b2b6c9dc24b30bb75de8e8b9640 (diff) | |
download | gcc-b8977d928a7a261913dd89db55d4123b6a54ba46.zip gcc-b8977d928a7a261913dd89db55d4123b6a54ba46.tar.gz gcc-b8977d928a7a261913dd89db55d4123b6a54ba46.tar.bz2 |
btf: add -gprune-btf option
This patch adds a new option, -gprune-btf, to control BTF debug info
generation.
As the name implies, this option enables a kind of "pruning" of the BTF
information before it is emitted. When enabled, rather than emitting
all type information translated from DWARF, only information for types
directly used in the source program is emitted.
The primary purpose of this pruning is to reduce the amount of
unnecessary BTF information emitted, especially for BPF programs. It is
very common for BPF programs to include Linux kernel internal headers in
order to have access to kernel data structures. However, doing so often
has the side effect of also adding type definitions for a large number
of types which are not actually used by nor relevant to the program.
In these cases, -gprune-btf commonly reduces the size of the resulting
BTF information by 10x or more, as seen on average when compiling Linux
kernel BPF selftests. This both slims down the size of the resulting
object and reduces the time required by the BPF loader to verify the
program and its BTF information.
Note that the pruning implemented in this patch follows the same rules
as the BTF pruning performed unconditionally by LLVM's BPF backend when
generating BTF. In particular, the main sources of pruning are:
1) Only generate BTF for types used by variables and functions at the
file scope.
Note that which variables are known to be "used" may differ
slightly between LTO and non-LTO builds due to optimizations. For
non-LTO builds (and always for the BPF target), variables which are
optimized away during compilation are considered to be unused, and
they (along with their types) are pruned. For LTO builds, such
variables are not known to be optimized away by the time pruning
occurs, so VAR records for them and information for their types may
be present in the emitted BTF information. This is a missed
optimization that may be fixed in the future.
2) Avoid emitting full BTF for struct and union types which are only
pointed-to by members of other struct/union types. In these cases,
the full BTF_KIND_STRUCT or BTF_KIND_UNION which would normally
be emitted is replaced with a BTF_KIND_FWD, as though the
underlying type was a forward-declared struct or union type.
gcc/
* btfout.cc (btf_used_types): New hash set.
(struct btf_fixup): New.
(fixups, forwards): New vecs.
(btf_output): Calculate num_types depending on debug_prune_btf.
(btf_early_finsih): New initialization for debug_prune_btf.
(btf_add_used_type): New function.
(btf_used_type_list_cb): Likewise.
(btf_collect_pruned_types): Likewise.
(btf_add_vars): Handle special case for variables in ".maps" section
when generating BTF for BPF CO-RE target.
(btf_late_finish): Use btf_collect_pruned_types when debug_prune_btf
is in effect. Move some initialization to btf_early_finish.
(btf_finalize): Additional deallocation for debug_prune_btf.
* common.opt (gprune-btf): New flag.
* ctfc.cc (init_ctf_strtable): Make non-static.
* ctfc.h (init_ctf_strtable, ctfc_delete_strtab): Make extern.
* doc/invoke.texi (Debugging Options): Document -gprune-btf.
gcc/testsuite/
* gcc.dg/debug/btf/btf-prune-1.c: New test.
* gcc.dg/debug/btf/btf-prune-2.c: Likewise.
* gcc.dg/debug/btf/btf-prune-3.c: Likewise.
* gcc.dg/debug/btf/btf-prune-maps.c: Likewise.
Diffstat (limited to 'gcc/doc')
-rw-r--r-- | gcc/doc/invoke.texi | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 30c4b00..f4eb4ae 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -522,6 +522,7 @@ Objective-C and Objective-C++ Dialects}. @xref{Debugging Options,,Options for Debugging Your Program}. @gccoptlist{-g -g@var{level} -gdwarf -gdwarf-@var{version} -gbtf -gctf -gctf@var{level} +-gprune-btf -gno-prune-btf -ggdb -grecord-gcc-switches -gno-record-gcc-switches -gstrict-dwarf -gno-strict-dwarf -gas-loc-support -gno-as-loc-support @@ -12024,6 +12025,25 @@ eBPF target. On other targets, like x86, BTF debug information can be generated along with DWARF debug information when both of the debug formats are enabled explicitly via their respective command line options. +@opindex gprune-btf +@opindex gno-prune-btf +@item -gprune-btf +@itemx -gno-prune-btf +Prune BTF information before emission. When pruning, only type +information for types used by global variables and file-scope functions +will be emitted. If compiling for the BPF target with BPF CO-RE +enabled, type information will also be emitted for types used in BPF +CO-RE relocations. In addition, struct and union types which are only +referred to via pointers from members of other struct or union types +shall be pruned and replaced with BTF_KIND_FWD, as though those types +were only present in the input as forward declarations. + +This option substantially reduces the size of produced BTF information, +but at significant loss in the amount of detailed type information. +It is primarily useful when compiling for the BPF target, to minimize +the size of the resulting object, and to eliminate BTF information +which is not immediately relevant to the BPF program loading process. + @opindex gctf @item -gctf @itemx -gctf@var{level} |