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 introduces the generic deferred constant mechanism into the C
frontend.  It adds DEFERRED_CST_EXPR to represent expressions with
otherwise constant semantics while postponing their final evaluation.
For explicitly marked record types, offsetof expressions preserve the
identities of the referenced fields.  For explicitly marked enum types,
enumerator references preserve the identities of their declarations.

Importantly, the mechanism is entirely opt-in and compiler-internal.
The generic implementation does not introduce any user-facing attribute
or other language construct to enable deferred constants.  Instead,
consumers explicitly mark the types for which they need deferral.
Unmarked types retain the existing folding behavior.

I'm also CCing the GCC Rust frontend maintainers, following a suggestion
at the BPF team meeting.  Although this series currently implements
deferred constant support in the C frontend, the underlying
representation and lowering infrastructure are language-independent.  The
design allows other frontends to create deferred constant expressions as
well, so Rust could potentially use the same mechanism to preserve field
or enum identities for BPF CO-RE relocations.  I'd welcome any feedback
on whether that's sensible/possible.

Patch 2 implements the BPF consumer, using deferred constants to
generate automatic field-offset relocations and to recover enumerator
identities for the existing __builtin_preserve_enum_value arguments.

Patch 3 introduces the BPF-specific "preserve_enum_value" attribute,
enabling automatic enum-value relocations.

Changes since v1:

  * Extended deferred constant folding to handle more expressions,
    including cases where deferred constants cancel out and the
    resulting expression no longer depend on deferred components.

  * Added support for deferred constants in switch case labels.

  * Introduced resolver callbacks, allowing consumers to resolve
    deferred constants when required by the frontend. In particular, the
    BPF consumer now resolves enumerators of unattributed enum types to
    ordinary constants, avoiding frontend errors in contexts requiring
    compile-time constants.

  * Added test cases to illustrate the expected behavior of deferred
    constants in different contexts.

I'm open to suggestions or change requests.

Thanks,
Jasper

[1] https://lore.kernel.org/lkml/[email protected]/

York Jasper Niebuhr (3):
  c: deferred constant expressions
  bpf: CO-RE consumer of deferred constants
  bpf: preserve_enum_value attribute

 gcc/Makefile.in                               |   4 +-
 gcc/c-family/c-common.cc                      | 109 ++-
 gcc/c/c-decl.cc                               |  24 +
 gcc/c/c-fold.cc                               | 183 ++++
 gcc/c/c-lang.cc                               |   5 +
 gcc/c/c-parser.cc                             |   6 +
 gcc/c/c-typeck.cc                             | 230 ++++-
 gcc/config/bpf/bpf-c.cc                       |   5 +
 gcc/config/bpf/bpf-passes.def                 |   1 +
 gcc/config/bpf/bpf-protos.h                   |   3 +
 gcc/config/bpf/bpf.cc                         |  30 +
 gcc/config/bpf/core-builtins.cc               | 765 +++++++++++++++-
 gcc/deferred-cst.cc                           | 451 ++++++++++
 gcc/deferred-cst.h                            |  98 ++
 gcc/gimple-expr.cc                            |   5 +
 gcc/gimple.cc                                 |   3 +-
 gcc/gimplify.cc                               |   5 +
 gcc/internal-fn.cc                            |  18 +
 gcc/internal-fn.def                           |   6 +
 gcc/langhooks-def.h                           |   4 +-
 gcc/langhooks.h                               |   3 +
 gcc/passes.def                                |   1 +
 .../gcc.dg/plugin/deferred_cst-basic.c        |  59 ++
 .../gcc.dg/plugin/deferred_cst-canceled.c     |  37 +
 .../gcc.dg/plugin/deferred_cst-invalid.c      |  77 ++
 .../gcc.dg/plugin/deferred_cst-resolved.c     |  77 ++
 .../gcc.dg/plugin/deferred_cst-run.c          | 235 +++++
 .../gcc.dg/plugin/deferred_cst-static-init.c  |  51 ++
 .../gcc.dg/plugin/deferred_cst-switch-case.c  |  67 ++
 .../gcc.dg/plugin/deferred_cst_plugin.cc      | 836 ++++++++++++++++++
 gcc/testsuite/gcc.dg/plugin/plugin.exp        |   8 +
 .../gcc.target/bpf/core-auto-drain-1.c        |  41 +
 .../gcc.target/bpf/core-auto-enum-value-1.c   |  47 +
 .../gcc.target/bpf/core-auto-field-offset-1.c |  40 +
 gcc/tree-pass.h                               |   1 +
 gcc/tree-pretty-print.cc                      |   7 +
 gcc/tree.def                                  |  10 +
 gcc/varasm.cc                                 |   5 +-
 38 files changed, 3491 insertions(+), 66 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-basic.c
 create mode 100644 gcc/testsuite/gcc.dg/plugin/deferred_cst-canceled.c
 create mode 100644 gcc/testsuite/gcc.dg/plugin/deferred_cst-invalid.c
 create mode 100644 gcc/testsuite/gcc.dg/plugin/deferred_cst-resolved.c
 create mode 100644 gcc/testsuite/gcc.dg/plugin/deferred_cst-run.c
 create mode 100644 gcc/testsuite/gcc.dg/plugin/deferred_cst-static-init.c
 create mode 100644 gcc/testsuite/gcc.dg/plugin/deferred_cst-switch-case.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

Reply via email to