[PATCH v2 1/3] bpf: spell xBPF indirect calls `callx' in the pseudo-C dialect

David Faust david.faust@oss.qualcomm.com
Fri Oct 9 08:02:17 GMT 2026


Hi Vineet,

On 10/9/26 12:43 AM, Vineet Gupta wrote:
> An indirect call is spelled `callx rN' in the pseudo-C dialect however
> the call patterns emitted a single `call\t%N' template for both dialects,
> so a function pointer call built with -mxbpf could not be assembled
> because Pseudo-C is the default:
> 
>   $ bpf-unknown-none-gcc -mxbpf -O2 -c callx.c
>   Error: unexpected register name `r0' in instruction `call r0'
> 
> The only existing test for this, xbpf-indirect-call-1.c, passes
> -masm=normal and so did not catch it.
> 
> A direct call is spelled `call' in both dialects, so the template cannot
> simply be turned into an alternation.  Instead split the call target
> constraint, which already distinguishes a constant call address (S) from
> a register, into two alternatives and give each its own template.

Thanks for the patch.

Just one little comment below, otherwise LGTM and OK to push
with that fixed.

(Also a tiny typo nit).

> 
> gcc/ChangeLog:
> 
> 	* config/bpf/bpf.md (call_internal): Split the call target
> 	constraint into separate alternatives for a constant address and
> 	for a register, and use the pseudo-C spelling `callx' for the
> 	latter.
> 	(call_value_internal): Likewise.
> 	* config/bpf/bpf.cc (bpf_output_call): Update comment.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* gcc.target/bpf/xbpf-indirect-call-2.c: New test.
> 
> Signed-off-by: Vineet Gupta <vineet.gupta@linux.dev>
> ---
>  gcc/config/bpf/bpf.cc                         |  2 +-
>  gcc/config/bpf/bpf.md                         | 15 ++++++++----
>  .../gcc.target/bpf/xbpf-indirect-call-2.c     | 23 +++++++++++++++++++
>  3 files changed, 35 insertions(+), 5 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.target/bpf/xbpf-indirect-call-2.c
> 
> diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
> index 6bd714d50534..0e63f2c10ea0 100644
> --- a/gcc/config/bpf/bpf.cc
> +++ b/gcc/config/bpf/bpf.cc
> @@ -760,7 +760,7 @@ bpf_output_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
>  
>     The main purposes of this function are:
>     - To reject indirect CALL instructions, which are not supported by
> -     eBPF.
> +     eBPF, unless targetting xBPF.

tiny nit but typo: "targeting", here and below

>     - To recognize calls to kernel helper functions and emit the
>       corresponding CALL N instruction.
>  
> diff --git a/gcc/config/bpf/bpf.md b/gcc/config/bpf/bpf.md
> index a2bceb8998d7..541016b6f9e3 100644
> --- a/gcc/config/bpf/bpf.md
> +++ b/gcc/config/bpf/bpf.md
> @@ -539,13 +539,18 @@
>    DONE;
>  })
>  
> +;; Indirect calls, generated when targetting xBPF.
> +;; The register based are spelled differently for Pseudo-C `callx rN'
> +;; vs. `call %rN' in the normal assembly dialect.

I find the new comment a little bit misleading since call_internal is
used both for direct (S) and indirect calls (r).  Would be better to
clarify that it emits either depending on the operand.

>  (define_insn "call_internal"
> -  [(call (mem:DI (match_operand:DI 0 "call_operand" "Sr"))
> +  [(call (mem:DI (match_operand:DI 0 "call_operand" "S,r"))
>           (match_operand:SI 1 "general_operand" ""))]
>    ;; operands[2] is next_arg_register
>    ;; operands[3] is struct_value_size_rtx.
>    ""
> -  { return bpf_output_call ("call\t%0", operands, 0); }
> +  "@
> +   *return bpf_output_call (\"call\t%0\", operands, 0);
> +   *return bpf_output_call (\"{call\t%0|callx\t%0}\", operands, 0);"
>    [(set_attr "type" "jmp")])
>  
>  (define_expand "call_value"
> @@ -563,12 +568,14 @@
>  
>  (define_insn "call_value_internal"
>    [(set (match_operand 0 "register_operand" "")
> -	(call (mem:DI (match_operand:DI 1 "call_operand" "Sr"))
> +	(call (mem:DI (match_operand:DI 1 "call_operand" "S,r"))
>  	      (match_operand:SI 2 "general_operand" "")))]
>    ;; operands[3] is next_arg_register
>    ;; operands[4] is struct_value_size_rtx.
>    ""
> -  { return bpf_output_call ("call\t%1", operands, 1); }
> +  "@
> +   *return bpf_output_call (\"call\t%1\", operands, 1);
> +   *return bpf_output_call (\"{call\t%1|callx\t%1}\", operands, 1);"
>    [(set_attr "type" "jmp")])
>  
>  (define_insn "sibcall"
> diff --git a/gcc/testsuite/gcc.target/bpf/xbpf-indirect-call-2.c b/gcc/testsuite/gcc.target/bpf/xbpf-indirect-call-2.c
> new file mode 100644
> index 000000000000..1a7206fbf3f8
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/bpf/xbpf-indirect-call-2.c
> @@ -0,0 +1,23 @@
> +/* { dg-do compile } */
> +/* { dg-options "-mxbpf -masm=pseudoc" } */
> +
> +/* GCC should generate an indirect call instruction (callx REG) when
> +   targetting xBPF in the pseudo-C assembly dialect.  The normal dialect
> +   spelling (call %REG) is not accepted by the assembler in pseudo-C.  */
> +
> +void
> +foo ()
> +{
> +  ;
> +}
> +
> +void
> +bar()
> +{
> +  void (*funp) () = &foo;
> +
> +  (*funp) ();
> +}
> +
> +/* { dg-final { scan-assembler "callx\tr" } } */
> +/* { dg-final { scan-assembler-not "call\tr" } } */



More information about the Gcc-patches mailing list