This series introduces a mechanism for preserving the semantic origins
of C constant expressions which would usually be lost to frontend
folding, and new BPF CO-RE capabilities that consume them.
The C language frontend folds expressions such as
__builtin_offsetof (struct S, field)
or enum constants to raw integer constants, with no remaining
association to field or enum identities. The BPF backend must retrieve
such information to generate CO-RE relocations.
Until now, BPF had to rely on custom builtins and copied integer
constant keys in side-maps to funnel semantic intent through the C
frontend.
With deferred constants...
* The BPF backend can automatically generate relocations for offsetof
expressions that refer to offsets of structs marked with the
"preserve_access_index" attribute, without the need to use a dedicated
builtin function.
* The BPF backend can recover the identity of the enum value handed to
__builtin_preserve_enum_value without a side-map.
* BPF supports a new "preserve_enum_values" attribute for enum types.
When set, all occurrences of enumerators of that type automatically
get corresponding relocations during BPF lowering, without the need
to use a builtin everywhere. This is basically the enum equivalent
to "preserve_access_index".
* Other consumers, such as Selfpatch-SLR (in development) [1] can get
the information they need to instrument all dependencies on field
offsets or enum values.
Patch 1 of this series introduces the entirely opt-in deferred constant
mechanism into the C frontend. It adds a DEFERRED_CST_EXPR to represent
expressions with otherwise constant semantics while postponing their final
evaluation. If a struct type is explicitly marked for constant deferral
(e.g. in a PLUGIN_FINISH_TYPE callback), fold_offsetof returns
DEFERRED_CST_EXPRs instead of INTEGER_CST nodes for matching offset
expressions. If an enum type is marked for constant deferral, its
enumerators' occurrences are also resolved to DEFERRED_CST_EXPRs instead
of their INTEGER_CST initial values.
Patch 2 implements a BPF consumer of deferred constants that provides
the improvements/features mentioned above.
I'm open to any suggestions or change requests, though I am no BPF
wizard, so it would be lovely to get some BPF people involved with that
part of this series.
Thanks,
Jasper
[1] https://lore.kernel.org/lkml/[email protected]/
York Jasper Niebuhr (2):
c: deferred constant expressions
bpf: CO-RE consumer of deferred constants
gcc/Makefile.in | 4 +-
gcc/c-family/c-common.cc | 109 ++-
gcc/c/c-fold.cc | 173 +++++
gcc/c/c-typeck.cc | 55 +-
gcc/config/bpf/bpf-passes.def | 1 +
gcc/config/bpf/bpf-protos.h | 3 +
gcc/config/bpf/bpf.cc | 24 +
gcc/config/bpf/core-builtins.cc | 637 ++++++++++++++++--
gcc/deferred-cst.cc | 403 +++++++++++
gcc/deferred-cst.h | 54 ++
gcc/gimple-expr.cc | 5 +
gcc/gimple.cc | 3 +-
gcc/gimplify.cc | 5 +
gcc/internal-fn.cc | 18 +
gcc/internal-fn.def | 6 +
gcc/passes.def | 1 +
.../gcc.dg/plugin/deferred_cst-test.c | 80 +++
.../gcc.dg/plugin/deferred_cst_plugin.cc | 619 +++++++++++++++++
gcc/testsuite/gcc.dg/plugin/plugin.exp | 1 +
.../gcc.target/bpf/core-auto-drain-1.c | 29 +
.../gcc.target/bpf/core-auto-enum-value-1.c | 33 +
.../gcc.target/bpf/core-auto-field-offset-1.c | 36 +
gcc/tree-pass.h | 1 +
gcc/tree-pretty-print.cc | 7 +
gcc/tree.def | 10 +
gcc/varasm.cc | 5 +-
26 files changed, 2253 insertions(+), 69 deletions(-)
create mode 100644 gcc/deferred-cst.cc
create mode 100644 gcc/deferred-cst.h
create mode 100644 gcc/testsuite/gcc.dg/plugin/deferred_cst-test.c
create mode 100644 gcc/testsuite/gcc.dg/plugin/deferred_cst_plugin.cc
create mode 100644 gcc/testsuite/gcc.target/bpf/core-auto-drain-1.c
create mode 100644 gcc/testsuite/gcc.target/bpf/core-auto-enum-value-1.c
create mode 100644 gcc/testsuite/gcc.target/bpf/core-auto-field-offset-1.c
--
2.43.0