public inbox for
 help / color / mirror / Atom feed
* [COMMITTED] bpf: facilitate constant propagation of function addresses
@ 2022-08-24 13:32 Jose E. Marchesi
  0 siblings, 0 replies; only message in thread
From: Jose E. Marchesi @ 2022-08-24 13:32 UTC (permalink / raw)
  To: gcc-patches

eBPF effectively supports two kind of call instructions:

- The so called pseudo-calls ("bpf to bpf").
- External calls ("bpf to kernel").

The BPF call instruction always gets an immediate argument, whose
interpretation varies depending on the purpose of the instruction:

- For pseudo-calls, the immediate argument is interpreted as a
  32-bit PC-relative displacement measured in number of 64-bit words
  minus one.

- For external calls, the immediate argument is interpreted as the
  identification of a kernel helper.

In order to differenciate both flavors of CALL instructions the SRC
field of the instruction (otherwise unused) is abused as an opcode;
if the field holds 0 the instruction is an external call, if it holds
BPF_PSEUDO_CALL the instruction is a pseudo-call.

C-to-BPF toolchains, including the GNU toolchain, use the following
practical heuristic at assembly time in order to determine what kind
of CALL instruction to generate: call instructions requiring a fixup
at assembly time are interpreted as pseudo-calls.  This means that in
practice a call instruction involving symbols at assembly time (such
as `call foo') is assembled into a pseudo-call instruction, whereas
something like `call 12' is assembled into an external call

In both cases, the argument of CALL is an immediate: at the time of
writing eBPF lacks support for indirect calls, i.e. there is no
call-to-register instruction.

This is the reason why BPF programs, in practice, rely on certain
optimizations to happen in order to generate calls to immediates.
This is a typical example involving a kernel helper:

  static void * (*bpf_map_lookup_elem)(void *map, const void *key)
    = (void *) 1;

  int foo (...)
    char *ret;

    ret = bpf_map_lookup_elem (args...);
    if (ret)
      return 1;
    return 0;

Note how the code above relies on the compiler to do constant
propagation so the call to bpf_map_lookup_elem can be compiled to a
`call 1' instruction.

While GCC provides a kernel_helper function declaration attribute that
can be used in a robust way to tell GCC to generate an external call
despite of optimization level and any other consideration, the Linux
kernel bpf_helpers.h file relies on tricks like the above.

This patch modifies the BPF backend to avoid SSA sparse constant
propagation to be "undone" by the expander loading the function
address into a register.  A new test is also added.

Tested in bpf-unknown-linux-gnu.
No regressions.


	PR target/106733
	* config/bpf/ (bpf_legitimate_address_p): Recognize integer
	constants as legitimate addresses for functions.
	(bpf_small_register_classes_for_mode_p): Define target hook.


	PR target/106733
	* Rename to ...
	* and modify to not expect
	failure anymore.
	* New test.
 gcc/config/bpf/                         | 21 ++++++++++++++++++-
 .../{constant-calls.c => constant-calls-1.c}  |  1 -
 .../         | 16 ++++++++++++++
 3 files changed, 36 insertions(+), 2 deletions(-)
 rename gcc/testsuite/{constant-calls.c => constant-calls-1.c} (88%)
 create mode 100644 gcc/testsuite/

diff --git a/gcc/config/bpf/ b/gcc/config/bpf/
index 6a0e3bbca9e..7e37e080808 100644
--- a/gcc/config/bpf/
+++ b/gcc/config/bpf/
@@ -659,12 +659,15 @@ bpf_address_base_p (rtx x, bool strict)
    target machine for a memory operand of mode MODE.  */
 static bool
-bpf_legitimate_address_p (machine_mode mode ATTRIBUTE_UNUSED,
+bpf_legitimate_address_p (machine_mode mode,
 			  rtx x,
 			  bool strict)
   switch (GET_CODE (x))
+    case CONST_INT:
+      return (mode == FUNCTION_MODE);
     case REG:
       return bpf_address_base_p (x, strict);
@@ -1311,6 +1314,22 @@ bpf_core_walk (tree *tp, int *walk_subtrees, void *data)
   return NULL_TREE;
+/* Implement target hook small_register_classes_for_mode_p.  */
+static bool
+bpf_small_register_classes_for_mode_p (machine_mode mode)
+    return 1;
+  else
+    /* Avoid putting function addresses in registers, as calling these
+       is not supported in eBPF.  */
+    return (mode != FUNCTION_MODE);
+  bpf_small_register_classes_for_mode_p
 /* Implement TARGET_RESOLVE_OVERLOADED_BUILTIN (see gccint manual section
    Target Macros::Misc.).
diff --git a/gcc/testsuite/ b/gcc/testsuite/
similarity index 88%
rename from gcc/testsuite/
rename to gcc/testsuite/
index 84612a92ae9..6effc7dfdd4 100644
--- a/gcc/testsuite/
+++ b/gcc/testsuite/
@@ -1,5 +1,4 @@
 /* { dg-do compile } */
-/* { dg-xfail-if "" { bpf-*-* } } */
 typedef void  *(*T)(void);
 f1 ()
diff --git a/gcc/testsuite/ b/gcc/testsuite/
new file mode 100644
index 00000000000..836ab67a1fd
--- /dev/null
+++ b/gcc/testsuite/
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-std=c89 -O2" } */
+static void * (*bpf_map_lookup_elem)(void *map, const void *key) = (void *) 666;
+int foo ()
+  char *ret;
+  ret = bpf_map_lookup_elem (ret, ret);
+  if (ret)
+    return 0;
+  return 1;
+/* { dg-final { scan-assembler "call\t666" } } */

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-08-24 13:32 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-24 13:32 [COMMITTED] bpf: facilitate constant propagation of function addresses Jose E. Marchesi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).