Re: [PATCH v2 6/6] bpf,btf: enable BTF pruning by default for BPF
On 5/2/24 10:11, David Faust wrote: This patch enables -fprune-btf by default in the BPF backend when generating BTF information, and fixes BPF CO-RE generation when using -fprune-btf. When generating BPF CO-RE information, we must ensure that types used in CO-RE relocations always have sufficient BTF information emited so that the CO-RE relocations can be processed by a BPF loader. The BTF pruning algorithm on its own does not have sufficient information to determine which types are used in a BPF CO-RE relocation, so this information must be supplied by the BPF backend, using a new btf_mark_type_used function. Co-authored-by: Cupertino Miranda gcc/ * btfout.cc (btf_mark_type_used): New. * ctfc.h (btf_mark_type_used): Declare it here. * config/bpf/bpf.cc (bpf_option_override): Enable -fprune-btf by default if -gbtf is enabled. * config/bpf/bcore-builtins.cc (extra_fn): New typedef. (compute_field_expr): Add callback parameter, and call it if supplied. Fix computation for MEM_REF. (mark_component_type_as_used): New. (bpf_mark_types_as_used): Likewise. (bpf_expand_core_builtin): Call here. * doc/invoke.texi (Debugging Options): Note that -fprune-btf is enabled by default for BPF target when generating BTF. gcc/testsuite/ * gcc.dg/debug/btf/btf-variables-5.c: Add -fno-prune-btf to dg-options. --- gcc/btfout.cc | 22 ++ gcc/config/bpf/bpf.cc | 5 ++ gcc/config/bpf/core-builtins.cc | 70 +-- gcc/ctfc.h| 1 + gcc/doc/invoke.texi | 3 + .../gcc.dg/debug/btf/btf-variables-5.c| 2 +- 6 files changed, 96 insertions(+), 7 deletions(-) diff --git a/gcc/btfout.cc b/gcc/btfout.cc index 93d56492bbe..da2c9d35be9 100644 --- a/gcc/btfout.cc +++ b/gcc/btfout.cc @@ -1539,6 +1539,28 @@ btf_late_assign_datasec_ids (ctf_container_ref ctfc) } } + +/* Manually mark that type T is used to ensure it will not be pruned. + Used by the BPF backend when generating BPF CO-RE to mark types used + in CO-RE relocations. */ + +void +btf_mark_type_used (tree t) +{ + /* If we are not going to prune anyway, this is a no-op. */ + if (!flag_prune_btf) +return; + + gcc_assert (TYPE_P (t)); + ctf_container_ref ctfc = ctf_get_tu_ctfc (); + ctf_dtdef_ref dtd = ctf_lookup_tree_type (ctfc, t); + + if (!dtd) +return; + + btf_minimal_add_type (ctfc, dtd, false, false); +} + /* Callback used for assembling the only-used-types list. Note that this is the same as btf_type_list_cb above, but the hash_set traverse requires a different function signature. */ diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc index e6ea211a2c6..75303ce8f46 100644 --- a/gcc/config/bpf/bpf.cc +++ b/gcc/config/bpf/bpf.cc @@ -221,6 +221,11 @@ bpf_option_override (void) && !(target_flags_explicit & MASK_BPF_CORE)) target_flags |= MASK_BPF_CORE; + /* -gbtf implies -fprune-btf for BPF target. */ + if (btf_debuginfo_p ()) +SET_OPTION_IF_UNSET (_options, _options_set, +flag_prune_btf, true); + /* Determine available features from ISA setting (-mcpu=). */ if (bpf_has_jmpext == -1) bpf_has_jmpext = (bpf_isa >= ISA_V2); diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc index d5a7de825ad..1b91b1c0d25 100644 --- a/gcc/config/bpf/core-builtins.cc +++ b/gcc/config/bpf/core-builtins.cc @@ -624,13 +624,20 @@ bpf_core_get_index (const tree node, bool *valid) ALLOW_ENTRY_CAST is an input arguments and specifies if the function should consider as valid expressions in which NODE entry is a cast expression (or - tree code nop_expr). */ + tree code nop_expr). + + EXTRA_FN is a callback function to allow extra functionality with this + function traversal. Currently used for marking used type during expand + pass. */ + +typedef void (*extra_fn) (tree); static unsigned char compute_field_expr (tree node, unsigned int *accessors, bool *valid, tree *access_node, - bool allow_entry_cast = true) + bool allow_entry_cast = true, + extra_fn callback = NULL) { unsigned char n = 0; unsigned int fake_accessors[MAX_NR_ACCESSORS]; @@ -647,6 +654,9 @@ compute_field_expr (tree node, unsigned int *accessors, *access_node = node; + if (callback != NULL) +callback (node); + switch (TREE_CODE (node)) { case INDIRECT_REF: @@ -664,17 +674,19 @@ compute_field_expr (tree node, unsigned int *accessors, case COMPONENT_REF: n = compute_field_expr (TREE_OPERAND (node, 0), accessors, valid, - access_node, false); + access_node, false,
[PATCH v2 6/6] bpf,btf: enable BTF pruning by default for BPF
This patch enables -fprune-btf by default in the BPF backend when generating BTF information, and fixes BPF CO-RE generation when using -fprune-btf. When generating BPF CO-RE information, we must ensure that types used in CO-RE relocations always have sufficient BTF information emited so that the CO-RE relocations can be processed by a BPF loader. The BTF pruning algorithm on its own does not have sufficient information to determine which types are used in a BPF CO-RE relocation, so this information must be supplied by the BPF backend, using a new btf_mark_type_used function. Co-authored-by: Cupertino Miranda gcc/ * btfout.cc (btf_mark_type_used): New. * ctfc.h (btf_mark_type_used): Declare it here. * config/bpf/bpf.cc (bpf_option_override): Enable -fprune-btf by default if -gbtf is enabled. * config/bpf/bcore-builtins.cc (extra_fn): New typedef. (compute_field_expr): Add callback parameter, and call it if supplied. Fix computation for MEM_REF. (mark_component_type_as_used): New. (bpf_mark_types_as_used): Likewise. (bpf_expand_core_builtin): Call here. * doc/invoke.texi (Debugging Options): Note that -fprune-btf is enabled by default for BPF target when generating BTF. gcc/testsuite/ * gcc.dg/debug/btf/btf-variables-5.c: Add -fno-prune-btf to dg-options. --- gcc/btfout.cc | 22 ++ gcc/config/bpf/bpf.cc | 5 ++ gcc/config/bpf/core-builtins.cc | 70 +-- gcc/ctfc.h| 1 + gcc/doc/invoke.texi | 3 + .../gcc.dg/debug/btf/btf-variables-5.c| 2 +- 6 files changed, 96 insertions(+), 7 deletions(-) diff --git a/gcc/btfout.cc b/gcc/btfout.cc index 93d56492bbe..da2c9d35be9 100644 --- a/gcc/btfout.cc +++ b/gcc/btfout.cc @@ -1539,6 +1539,28 @@ btf_late_assign_datasec_ids (ctf_container_ref ctfc) } } + +/* Manually mark that type T is used to ensure it will not be pruned. + Used by the BPF backend when generating BPF CO-RE to mark types used + in CO-RE relocations. */ + +void +btf_mark_type_used (tree t) +{ + /* If we are not going to prune anyway, this is a no-op. */ + if (!flag_prune_btf) +return; + + gcc_assert (TYPE_P (t)); + ctf_container_ref ctfc = ctf_get_tu_ctfc (); + ctf_dtdef_ref dtd = ctf_lookup_tree_type (ctfc, t); + + if (!dtd) +return; + + btf_minimal_add_type (ctfc, dtd, false, false); +} + /* Callback used for assembling the only-used-types list. Note that this is the same as btf_type_list_cb above, but the hash_set traverse requires a different function signature. */ diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc index e6ea211a2c6..75303ce8f46 100644 --- a/gcc/config/bpf/bpf.cc +++ b/gcc/config/bpf/bpf.cc @@ -221,6 +221,11 @@ bpf_option_override (void) && !(target_flags_explicit & MASK_BPF_CORE)) target_flags |= MASK_BPF_CORE; + /* -gbtf implies -fprune-btf for BPF target. */ + if (btf_debuginfo_p ()) +SET_OPTION_IF_UNSET (_options, _options_set, +flag_prune_btf, true); + /* Determine available features from ISA setting (-mcpu=). */ if (bpf_has_jmpext == -1) bpf_has_jmpext = (bpf_isa >= ISA_V2); diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc index d5a7de825ad..1b91b1c0d25 100644 --- a/gcc/config/bpf/core-builtins.cc +++ b/gcc/config/bpf/core-builtins.cc @@ -624,13 +624,20 @@ bpf_core_get_index (const tree node, bool *valid) ALLOW_ENTRY_CAST is an input arguments and specifies if the function should consider as valid expressions in which NODE entry is a cast expression (or - tree code nop_expr). */ + tree code nop_expr). + + EXTRA_FN is a callback function to allow extra functionality with this + function traversal. Currently used for marking used type during expand + pass. */ + +typedef void (*extra_fn) (tree); static unsigned char compute_field_expr (tree node, unsigned int *accessors, bool *valid, tree *access_node, - bool allow_entry_cast = true) + bool allow_entry_cast = true, + extra_fn callback = NULL) { unsigned char n = 0; unsigned int fake_accessors[MAX_NR_ACCESSORS]; @@ -647,6 +654,9 @@ compute_field_expr (tree node, unsigned int *accessors, *access_node = node; + if (callback != NULL) +callback (node); + switch (TREE_CODE (node)) { case INDIRECT_REF: @@ -664,17 +674,19 @@ compute_field_expr (tree node, unsigned int *accessors, case COMPONENT_REF: n = compute_field_expr (TREE_OPERAND (node, 0), accessors, valid, - access_node, false); + access_node, false, callback); accessors[n] = bpf_core_get_index (TREE_OPERAND (node, 1), valid);