Re: [PATCH] bpf: create modifier for mem operand for xchg and cmpxchg

2024-07-19 Thread Cupertino Miranda


Fixed and pushed!

Thanks,
Cupertino

David Faust writes:

> On 7/15/24 08:33, Cupertino Miranda wrote:
>> Both xchg and cmpxchg instructions, in the pseudo-C dialect, do not
>> expect their memory address operand to be surrounded by parentheses.
>> For example, it should be output as "w0 =cmpxchg32_32(r8+8,w0,w2)"
>> instead of "w0 =cmpxchg32_32((r8+8),w0,w2)".
>
> After some brief searching, looks like these extra parens in [cmp]xchg*
> are an unintended consequence of:
>
>  bb5f619a938 bpf: fix printing of memory operands in pseudoc asm dialect
>
>>
>> This patch implements an operand modifier 'M' which marks the
>> instruction templates that do not expect the parentheses, and adds it do
>> xchg and cmpxchg templates.
>
> One very minor nit below, otherwise LGTM. Please apply.
> Thanks for the fix!
>
>>
>> gcc/ChangeLog:
>>  * config/bpf/atomic.md (atomic_compare_and_swap,
>>  atomic_exchange): Add operand modifier %M to the first
>>  operand.
>>  * config/bpf/bpf.cc (no_parentheses_mem_operand): Create
>>  variable.
>>  (bpf_print_operand): Set no_parentheses_mem_operand variable if
>>  %M operand is used.
>>  (bpf_print_operand_address): Conditionally output parentheses.
>>
>> gcc/testsuite/ChangeLog:
>>  * gcc.target/bpf/pseudoc-atomic-memaddr-op.c: Add test.
>> ---
>>  gcc/config/bpf/atomic.md  |  4 +--
>>  gcc/config/bpf/bpf.cc | 20 ---
>>  .../bpf/pseudoc-atomic-memaddr-op.c   | 36 +++
>>  3 files changed, 54 insertions(+), 6 deletions(-)
>>  create mode 100644 gcc/testsuite/gcc.target/bpf/pseudoc-atomic-memaddr-op.c
>>
>> diff --git a/gcc/config/bpf/atomic.md b/gcc/config/bpf/atomic.md
>> index 65bd5f266a1..be4511bb51b 100644
>> --- a/gcc/config/bpf/atomic.md
>> +++ b/gcc/config/bpf/atomic.md
>> @@ -129,7 +129,7 @@
>> (set (match_dup 1)
>>  (match_operand:AMO 2 "nonmemory_operand" "0"))]
>>"bpf_has_v3_atomics"
>> -  "{axchg\t%1,%0|%w0 = xchg(%1, %w0)}")
>> +  "{axchg\t%1,%0|%w0 = xchg(%M1, %w0)}")
>>
>>  ;; The eBPF atomic-compare-and-exchange instruction has the form
>>  ;;   acmp [%dst+offset], %src
>> @@ -182,4 +182,4 @@
>>(match_operand:AMO 3 "register_operand")]   ;; desired
>>   UNSPEC_ACMP))]
>>"bpf_has_v3_atomics"
>> -  "{acmp\t%1,%3|%w0 = cmpxchg(%1, %w0, %w3)}")
>> +  "{acmp\t%1,%3|%w0 = cmpxchg(%M1, %w0, %w3)}")
>> diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
>> index 687e7ba49c1..a6b6e20731c 100644
>> --- a/gcc/config/bpf/bpf.cc
>> +++ b/gcc/config/bpf/bpf.cc
>> @@ -835,6 +835,11 @@ bpf_print_register (FILE *file, rtx op, int code)
>>  }
>>  }
>>
>> +/* Variable defined to implement 'M' operand modifier for the special cases
>> +   where the parentheses should not be printed surrounding a memory address
>> +   operand. */
>> +static bool no_parentheses_mem_operand;
>> +
>>  /* Print an instruction operand.  This function is called in the macro
>> PRINT_OPERAND defined in bpf.h */
>>
>> @@ -847,6 +852,7 @@ bpf_print_operand (FILE *file, rtx op, int code)
>>bpf_print_register (file, op, code);
>>break;
>>  case MEM:
>> +  no_parentheses_mem_operand = (code == 'M');
>>output_address (GET_MODE (op), XEXP (op, 0));
>>break;
>>  case CONST_DOUBLE:
>> @@ -891,6 +897,9 @@ bpf_print_operand (FILE *file, rtx op, int code)
>>  }
>>  }
>>
>> +#define PAREN_OPEN  (asm_dialect == ASM_NORMAL ? "[" : 
>> no_parentheses_mem_operand ? "" : "(")
>> +#define PAREN_CLOSE (asm_dialect == ASM_NORMAL ? "]" : 
>> no_parentheses_mem_operand ? "" : ")")
>> +
>>  /* Print an operand which is an address.  This function should handle
>> any legit address, as accepted by bpf_legitimate_address_p, and
>> also addresses that are valid in CALL instructions.
>> @@ -904,9 +913,9 @@ bpf_print_operand_address (FILE *file, rtx addr)
>>switch (GET_CODE (addr))
>>  {
>>  case REG:
>> -  fprintf (file, asm_dialect == ASM_NORMAL ? "[" : "(");
>> +  fprintf (file, "%s", PAREN_OPEN);
>>bpf_print_register (file, addr, 0);
>> -  fprintf (file, asm_dialect == ASM_NORMAL ? "+0]" : "+0)&

[PATCH] bpf: create modifier for mem operand for xchg and cmpxchg

2024-07-15 Thread Cupertino Miranda
Both xchg and cmpxchg instructions, in the pseudo-C dialect, do not
expect their memory address operand to be surrounded by parentheses.
For example, it should be output as "w0 =cmpxchg32_32(r8+8,w0,w2)"
instead of "w0 =cmpxchg32_32((r8+8),w0,w2)".

This patch implements an operand modifier 'M' which marks the
instruction templates that do not expect the parentheses, and adds it do
xchg and cmpxchg templates.

gcc/ChangeLog:
* config/bpf/atomic.md (atomic_compare_and_swap,
atomic_exchange): Add operand modifier %M to the first
operand.
* config/bpf/bpf.cc (no_parentheses_mem_operand): Create
variable.
(bpf_print_operand): Set no_parentheses_mem_operand variable if
%M operand is used.
(bpf_print_operand_address): Conditionally output parentheses.

gcc/testsuite/ChangeLog:
* gcc.target/bpf/pseudoc-atomic-memaddr-op.c: Add test.
---
 gcc/config/bpf/atomic.md  |  4 +--
 gcc/config/bpf/bpf.cc | 20 ---
 .../bpf/pseudoc-atomic-memaddr-op.c   | 36 +++
 3 files changed, 54 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/bpf/pseudoc-atomic-memaddr-op.c

diff --git a/gcc/config/bpf/atomic.md b/gcc/config/bpf/atomic.md
index 65bd5f266a1..be4511bb51b 100644
--- a/gcc/config/bpf/atomic.md
+++ b/gcc/config/bpf/atomic.md
@@ -129,7 +129,7 @@
(set (match_dup 1)
 (match_operand:AMO 2 "nonmemory_operand" "0"))]
   "bpf_has_v3_atomics"
-  "{axchg\t%1,%0|%w0 = xchg(%1, %w0)}")
+  "{axchg\t%1,%0|%w0 = xchg(%M1, %w0)}")
 
 ;; The eBPF atomic-compare-and-exchange instruction has the form
 ;;   acmp [%dst+offset], %src
@@ -182,4 +182,4 @@
   (match_operand:AMO 3 "register_operand")]   ;; desired
  UNSPEC_ACMP))]
   "bpf_has_v3_atomics"
-  "{acmp\t%1,%3|%w0 = cmpxchg(%1, %w0, %w3)}")
+  "{acmp\t%1,%3|%w0 = cmpxchg(%M1, %w0, %w3)}")
diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
index 687e7ba49c1..a6b6e20731c 100644
--- a/gcc/config/bpf/bpf.cc
+++ b/gcc/config/bpf/bpf.cc
@@ -835,6 +835,11 @@ bpf_print_register (FILE *file, rtx op, int code)
 }
 }
 
+/* Variable defined to implement 'M' operand modifier for the special cases
+   where the parentheses should not be printed surrounding a memory address
+   operand. */
+static bool no_parentheses_mem_operand;
+
 /* Print an instruction operand.  This function is called in the macro
PRINT_OPERAND defined in bpf.h */
 
@@ -847,6 +852,7 @@ bpf_print_operand (FILE *file, rtx op, int code)
   bpf_print_register (file, op, code);
   break;
 case MEM:
+  no_parentheses_mem_operand = (code == 'M');
   output_address (GET_MODE (op), XEXP (op, 0));
   break;
 case CONST_DOUBLE:
@@ -891,6 +897,9 @@ bpf_print_operand (FILE *file, rtx op, int code)
 }
 }
 
+#define PAREN_OPEN  (asm_dialect == ASM_NORMAL ? "[" : 
no_parentheses_mem_operand ? "" : "(")
+#define PAREN_CLOSE (asm_dialect == ASM_NORMAL ? "]" : 
no_parentheses_mem_operand ? "" : ")")
+
 /* Print an operand which is an address.  This function should handle
any legit address, as accepted by bpf_legitimate_address_p, and
also addresses that are valid in CALL instructions.
@@ -904,9 +913,9 @@ bpf_print_operand_address (FILE *file, rtx addr)
   switch (GET_CODE (addr))
 {
 case REG:
-  fprintf (file, asm_dialect == ASM_NORMAL ? "[" : "(");
+  fprintf (file, "%s", PAREN_OPEN);
   bpf_print_register (file, addr, 0);
-  fprintf (file, asm_dialect == ASM_NORMAL ? "+0]" : "+0)");
+  fprintf (file, "+0%s", PAREN_CLOSE);
   break;
 case PLUS:
   {
@@ -923,14 +932,14 @@ bpf_print_operand_address (FILE *file, rtx addr)
|| (GET_CODE (op1) == UNSPEC
&& XINT (op1, 1) == UNSPEC_CORE_RELOC)))
  {
-fprintf (file, asm_dialect == ASM_NORMAL ? "[" : "(");
+   fprintf (file, "%s", PAREN_OPEN);
bpf_print_register (file, op0, 0);
fprintf (file, "+");
if (GET_CODE (op1) == UNSPEC)
  output_addr_const (file, XVECEXP (op1, 0, 0));
else
  output_addr_const (file, op1);
-fprintf (file, asm_dialect == ASM_NORMAL ? "]" : ")");
+   fprintf (file, "%s", PAREN_CLOSE);
  }
else
  fatal_insn ("invalid address in operand", addr);
@@ -948,6 +957,9 @@ bpf_print_operand_address (FILE *file, rtx addr)
 }
 }
 
+#undef PAREN_OPEN
+#undef PAREN_CLOSE
+
 /* Add a BPF builtin function with NAME, CODE and TYPE.  Return
the function decl or NULL_TREE if the builtin was not added.  */
 
diff --git a/gcc/testsuite/gcc.target/bpf/pseudoc-atomic-memaddr-op.c 
b/gcc/testsuite/gcc.target/bpf/pseudoc-atomic-memaddr-op.c
new file mode 100644
index 000..758faf04cc2
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/pseudoc-atomic-memaddr-op.c
@@ -0,0 +1,36 @@
+/* { dg-do compile } */
+/* { dg-options 

Re: [PATCH v3] bpf: remove huge memory waste with string allocation.

2024-04-19 Thread Cupertino Miranda


David Faust writes:

> Hi Cupertino,
>
> On 4/18/24 13:58, Cupertino Miranda wrote:
>> Hi David, everyone,
>>
>> Following Davids last review I decided to properly detect error cases,
>> as suggested.
>> The error however should be reported earlier in compilation in
>> pack_enum_valud function, where all the errors are reported.
>>
>> Thanks for the quick and detailed reviews.
>>
>> Regards,
>> Cupertino
>
> Thanks for taking the time on this.
> This version is nice, just one little comment:
>
>>
>> The BPF backend was allocating an unnecessarily large string when
>> constructing CO-RE relocations for enum types.
>> This patch further verifies if an enumerator is valid for CO-RE
>> representability and returns an error in those cases.
>
> The second sentence is a little awkward and seems to imply the error is
> returned when the enumerator is valid :)
> Perhaps "...verifies that an enumerator is valid for CO-RE, and returns
> an error if it is not" or similar would be more clear?
Thanks for all the suggestions.
>
> Otherwise, OK.
> Thanks!
Pushed!

>
>
>>
>> gcc/ChangeLog:
>>  * config/bpf/core-builtins.cc (get_index_for_enum_value): Create
>>  function.
>>  (pack_enum_value): Check for enumerator and error out.
>>  (process_enum_value): Correct string allocation.
>> ---
>>  gcc/config/bpf/core-builtins.cc | 57 ++---
>>  1 file changed, 38 insertions(+), 19 deletions(-)
>>
>> diff --git a/gcc/config/bpf/core-builtins.cc 
>> b/gcc/config/bpf/core-builtins.cc
>> index e03e986e2c1..829acea98f7 100644
>> --- a/gcc/config/bpf/core-builtins.cc
>> +++ b/gcc/config/bpf/core-builtins.cc
>> @@ -795,6 +795,23 @@ process_field_expr (struct cr_builtins *data)
>>  static GTY(()) hash_map *bpf_enum_mappings;
>>  tree enum_value_type = NULL_TREE;
>>
>> +static int
>> +get_index_for_enum_value (tree type, tree expr)
>> +{
>> +  gcc_assert (TREE_CODE (expr) == CONST_DECL
>> +  && TREE_CODE (type) == ENUMERAL_TYPE);
>> +
>> +  unsigned int index = 0;
>> +  for (tree l = TYPE_VALUES (type); l; l = TREE_CHAIN (l))
>> +{
>> +  gcc_assert (index < (1 << 16));
>> +  if (TREE_VALUE (l) == expr)
>> +return index;
>> +  index++;
>> +}
>> +  return -1;
>> +}
>> +
>>  /* Pack helper for the __builtin_preserve_enum_value.  */
>>
>>  static struct cr_local
>> @@ -846,6 +863,16 @@ pack_enum_value_fail:
>>  ret.reloc_data.default_value = integer_one_node;
>>  }
>>
>> +  if (ret.fail == false )
>> +{
>> +  int index = get_index_for_enum_value (type, tmp);
>> +  if (index == -1 || index >= (1 << 16))
>> +{
>> +  bpf_error ("enum value in CO-RE builtin cannot be represented");
>> +  ret.fail = true;
>> +}
>> +}
>> +
>>ret.reloc_data.type = type;
>>ret.reloc_data.kind = kind;
>>return ret;
>> @@ -864,25 +891,17 @@ process_enum_value (struct cr_builtins *data)
>>
>>struct cr_final ret = { NULL, type, data->kind };
>>
>> -  if (TREE_CODE (expr) == CONST_DECL
>> - && TREE_CODE (type) == ENUMERAL_TYPE)
>> -{
>> -  unsigned int index = 0;
>> -  for (tree l = TYPE_VALUES (type); l; l = TREE_CHAIN (l))
>> -{
>> -  if (TREE_VALUE (l) == expr)
>> -{
>> -  char *tmp = (char *) ggc_alloc_atomic ((index / 10) + 1);
>> -  sprintf (tmp, "%d", index);
>> -  ret.str = (const char *) tmp;
>> -
>> -  break;
>> -}
>> -  index++;
>> -}
>> -}
>> -  else
>> -gcc_unreachable ();
>> +  gcc_assert (TREE_CODE (expr) == CONST_DECL
>> +  && TREE_CODE (type) == ENUMERAL_TYPE);
>> +
>> +  int index = get_index_for_enum_value (type, expr);
>> +  gcc_assert (index != -1 && index < (1 << 16));
>> +
>> +  /* Index can only be a value up to 2^16.  Should always fit
>> + in 6 chars.  */
>> +  char tmp[6];
>> +  sprintf (tmp, "%u", index);
>> +  ret.str = CONST_CAST (char *, ggc_strdup(tmp));
>>
>>return ret;
>>  }


Re: [PATCH 1/3] bpf: support more instructions to match CO-RE relocations

2024-04-19 Thread Cupertino Miranda


Thanks! Pushed!

Jose E. Marchesi writes:

> Hi Cupertino.
> OK for master.
> Thanks!
>
>> BPF supports multiple instructions to be CO-RE relocatable regardless of
>> the position of the immediate field in the encoding.
>> In particular, not only the MOV instruction allows a CO-RE
>> relocation of its immediate operand, but the LD and ST instructions can
>> have a CO-RE relocation happening to their offset immediate operand,
>> even though those operands are encoded in different encoding bits.
>> This patch moves matching from a more traditional matching of the
>> UNSPEC_CORE_RELOC pattern within a define_insn to a match within the
>> constraints of both immediates and address operands from more generic
>> mov define_insn rule.
>>
>> gcc/Changelog:
>>  * config/bpf/bpf-protos.h (bpf_add_core_reloc): Renamed function
>>  to bpf_output_move.
>>  * config/bpf/bpf.cc (bpf_legitimate_address_p): Allow
>>  UNSPEC_CORE_RELOC to match an address.
>>  (bpf_insn_cost): Make UNSPEC_CORE_RELOC immediate moves
>>  expensive to prioritize loads and stores.
>>  (TARGET_INSN_COST): Add hook.
>>  (bpf_output_move): Wrapper to call bpf_output_core_reloc.
>>  (bpf_print_operand): Add support to print immediate operands
>>  specified with the UNSPEC_CORE_RELOC.
>>  (bpf_print_operand_address): Likewise, but to support
>>  UNSPEC_CORE_RELOC in addresses.
>>  (bpf_init_builtins): Flag BPF_BUILTIN_CORE_RELOC as NOTHROW.
>>  * config/bpf/bpf.md: Wrap patterns for MOV, LD and ST
>>  instruction with bpf_output_move call.
>>  (mov_reloc_core): Remove now spurious define_insn.
>>  * config/bpf/constraints.md: Added "c" and "C" constraints to
>>  match immediates represented with UNSPEC_CORE_RELOC.
>>  * config/bpf/core-builtins.cc (bpf_add_core_reloc): Remove
>>  (bpf_output_core_reloc): Add function to create the CO-RE
>>  relocations based on new matching rules.
>>  * config/bpf/core-builtins.h (bpf_output_core_reloc): Add
>>  prototype.
>>  * config/bpf/predicates.md (core_imm_operand) Add predicate.
>>  (mov_src_operand): Add match for core_imm_operand.
>>
>> gcc/testsuite/ChangeLog:
>>  * gcc.target/bpf/btfext-funcinfo.c: Updated to changes.
>>  * gcc.target/bpf/core-builtin-fieldinfo-const-elimination.c:
>>  Likewise.
>>  * gcc.target/bpf/core-builtin-fieldinfo-existence-1.c: Likewise.
>>  * gcc.target/bpf/core-builtin-fieldinfo-lshift-1-be.c: Likewise.
>>  * gcc.target/bpf/core-builtin-fieldinfo-lshift-1-le.c: Likewise.
>>  * gcc.target/bpf/core-builtin-fieldinfo-lshift-2.c: Likewise.
>>  * gcc.target/bpf/core-builtin-fieldinfo-offset-1.c: Likewise.
>>  * gcc.target/bpf/core-builtin-fieldinfo-rshift-1.c: Likewise.
>>  * gcc.target/bpf/core-builtin-fieldinfo-rshift-2.c: Likewise.
>>  * gcc.target/bpf/core-builtin-fieldinfo-sign-1.c: Likewise.
>>  * gcc.target/bpf/core-builtin-fieldinfo-sign-2.c: Likewise.
>>  * gcc.target/bpf/core-builtin-fieldinfo-size-1.c: Likewise.
>> ---
>>  gcc/config/bpf/bpf-protos.h   |  2 +-
>>  gcc/config/bpf/bpf.cc | 54 +-
>>  gcc/config/bpf/bpf.md | 56 ++-
>>  gcc/config/bpf/constraints.md | 20 ++
>>  gcc/config/bpf/core-builtins.cc   | 71 ++-
>>  gcc/config/bpf/core-builtins.h|  2 +
>>  gcc/config/bpf/predicates.md  |  7 +-
>>  .../gcc.target/bpf/btfext-funcinfo.c  |  2 -
>>  ...core-builtin-fieldinfo-const-elimination.c |  2 +-
>>  .../bpf/core-builtin-fieldinfo-existence-1.c  |  2 +-
>>  .../bpf/core-builtin-fieldinfo-lshift-1-be.c  |  8 +--
>>  .../bpf/core-builtin-fieldinfo-lshift-1-le.c  |  8 +--
>>  .../bpf/core-builtin-fieldinfo-lshift-2.c |  6 +-
>>  .../bpf/core-builtin-fieldinfo-offset-1.c | 12 ++--
>>  .../bpf/core-builtin-fieldinfo-rshift-1.c |  8 +--
>>  .../bpf/core-builtin-fieldinfo-rshift-2.c |  4 +-
>>  .../bpf/core-builtin-fieldinfo-sign-1.c   |  4 +-
>>  .../bpf/core-builtin-fieldinfo-sign-2.c   |  4 +-
>>  .../bpf/core-builtin-fieldinfo-size-1.c   |  8 +--
>>  19 files changed, 189 insertions(+), 91 deletions(-)
>>
>> diff --git a/gcc/config/bpf/bpf-protos.h b/gcc/config/bpf/bpf-protos.h
>> index ac0c2f4038f..b4866d34209 100644
>> --- a/gcc/config/bpf/bpf-protos.h
>> +++ b/gcc/config/bpf/bpf-protos.h
>> @@ -30,7 +30,7 @@ extern void bpf_print_operand_address (FILE *, rtx);
>>  extern void bpf_expand_prologue (void);
>>  extern void bpf_expand_epilogue (void);
>>  extern void bpf_expand_cbranch (machine_mode, rtx *);
>> -const char *bpf_add_core_reloc (rtx *operands, const char *templ);
>> +const char *bpf_output_move (rtx *operands, const char *templ);
>>
>>  class gimple_opt_pass;
>>  gimple_opt_pass *make_pass_lower_bpf_core (gcc::context *ctxt);
>> diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
>> index 

[PATCH v3] bpf: remove huge memory waste with string allocation.

2024-04-18 Thread Cupertino Miranda
Hi David, everyone,

Following Davids last review I decided to properly detect error cases,
as suggested.
The error however should be reported earlier in compilation in
pack_enum_valud function, where all the errors are reported.

Thanks for the quick and detailed reviews.

Regards,
Cupertino

The BPF backend was allocating an unnecessarily large string when
constructing CO-RE relocations for enum types.
This patch further verifies if an enumerator is valid for CO-RE
representability and returns an error in those cases.

gcc/ChangeLog:
* config/bpf/core-builtins.cc (get_index_for_enum_value): Create
function.
(pack_enum_value): Check for enumerator and error out.
(process_enum_value): Correct string allocation.
---
 gcc/config/bpf/core-builtins.cc | 57 ++---
 1 file changed, 38 insertions(+), 19 deletions(-)

diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc
index e03e986e2c1..829acea98f7 100644
--- a/gcc/config/bpf/core-builtins.cc
+++ b/gcc/config/bpf/core-builtins.cc
@@ -795,6 +795,23 @@ process_field_expr (struct cr_builtins *data)
 static GTY(()) hash_map *bpf_enum_mappings;
 tree enum_value_type = NULL_TREE;
 
+static int
+get_index_for_enum_value (tree type, tree expr)
+{
+  gcc_assert (TREE_CODE (expr) == CONST_DECL
+ && TREE_CODE (type) == ENUMERAL_TYPE);
+
+  unsigned int index = 0;
+  for (tree l = TYPE_VALUES (type); l; l = TREE_CHAIN (l))
+{
+  gcc_assert (index < (1 << 16));
+  if (TREE_VALUE (l) == expr)
+   return index;
+  index++;
+}
+  return -1;
+}
+
 /* Pack helper for the __builtin_preserve_enum_value.  */
 
 static struct cr_local
@@ -846,6 +863,16 @@ pack_enum_value_fail:
ret.reloc_data.default_value = integer_one_node;
 }
 
+  if (ret.fail == false )
+{
+  int index = get_index_for_enum_value (type, tmp);
+  if (index == -1 || index >= (1 << 16))
+   {
+ bpf_error ("enum value in CO-RE builtin cannot be represented");
+ ret.fail = true;
+   }
+}
+
   ret.reloc_data.type = type;
   ret.reloc_data.kind = kind;
   return ret;
@@ -864,25 +891,17 @@ process_enum_value (struct cr_builtins *data)
 
   struct cr_final ret = { NULL, type, data->kind };
 
-  if (TREE_CODE (expr) == CONST_DECL
- && TREE_CODE (type) == ENUMERAL_TYPE)
-{
-  unsigned int index = 0;
-  for (tree l = TYPE_VALUES (type); l; l = TREE_CHAIN (l))
-   {
- if (TREE_VALUE (l) == expr)
-   {
- char *tmp = (char *) ggc_alloc_atomic ((index / 10) + 1);
- sprintf (tmp, "%d", index);
- ret.str = (const char *) tmp;
-
- break;
-   }
- index++;
-   }
-}
-  else
-gcc_unreachable ();
+  gcc_assert (TREE_CODE (expr) == CONST_DECL
+ && TREE_CODE (type) == ENUMERAL_TYPE);
+
+  int index = get_index_for_enum_value (type, expr);
+  gcc_assert (index != -1 && index < (1 << 16));
+
+  /* Index can only be a value up to 2^16.  Should always fit
+ in 6 chars.  */
+  char tmp[6];
+  sprintf (tmp, "%u", index);
+  ret.str = CONST_CAST (char *, ggc_strdup(tmp));
 
   return ret;
 }
-- 
2.30.2



[PATCH v2] bpf: remove huge memory waste with string allocation.

2024-04-17 Thread Cupertino Miranda
The BPF backend was allocating an unnecessarily large string when
constructing CO-RE relocations for enum types.

gcc/ChangeLog:
* config/bpf/core-builtins.cc (process_enum_value): Correct
string allocation.
---
 gcc/config/bpf/core-builtins.cc | 10 ++
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc
index e03e986e2c1..beb039ea6e0 100644
--- a/gcc/config/bpf/core-builtins.cc
+++ b/gcc/config/bpf/core-builtins.cc
@@ -870,12 +870,14 @@ process_enum_value (struct cr_builtins *data)
   unsigned int index = 0;
   for (tree l = TYPE_VALUES (type); l; l = TREE_CHAIN (l))
{
+ gcc_assert (index < (1 << 16));
  if (TREE_VALUE (l) == expr)
{
- char *tmp = (char *) ggc_alloc_atomic ((index / 10) + 1);
- sprintf (tmp, "%d", index);
- ret.str = (const char *) tmp;
-
+ /* Index can only be a value up to 2^16.  Should always fit
+in 6 chars.  */
+ char tmp[6];
+ sprintf (tmp, "%u", index);
+ ret.str = CONST_CAST (char *, ggc_strdup(tmp));
  break;
}
  index++;
-- 
2.30.2



Re: [PATCH 3/3] bpf: add line_info support to BTF.ext section.

2024-04-17 Thread Cupertino Miranda


Jose E. Marchesi writes:

> Hi Cuper.
> Thanks for the patch.
>
>> This patch adds line_info debug information support to .BTF.ext
>> sections.
>> Line info information is used by the BPF verifier to improve error
>> reporting and give more precise source core referenced errors.
>>
>> gcc/Changelog:
>>  * config/bpf/bpf-protos.h (bpf_output_call): Change prototype.
>>  * config/bpf/bpf.cc (bpf_output_call): Change to adapt operands
>>  and return
>>  the instruction template instead of emmidiatelly emit asm and
>>  not allow proper final expected execution flow.
>>  (bpf_output_line_info): Add function to introduce line info
>>  entries in respective structures
>>  (bpf_asm_out_unwind_emit): Add function as hook to
>>  TARGET_ASM_UNWIND_EMIT. This hook is called before any
>>  instruction is emitted.
>
> Is it actually necessary to emit a label (plus .BTF.ext entry) for every
> instruction?
Maybe BPF would be Ok (not complaining of missing line_info) with just a
single entry per function pointing to the entry instruction. That
is not what clang does. Don't know if it emits any labels either.

It is probably possible to add some logic to compute the offset from the
function label and emit with an offset to the instruction location.
In case of inline assembly we could add a symbol after, and restart
offset computation.
It will need to add code to compute the instruction encoding size, and
increment function label offset each time we emit an instruction.

Still, my personal preference is to create those labels to properly
compute instruction location then some rather intricate solution that
would lead to future complications.
I know BPF is not like all the other targets, but I am thinking of
assembly/linker relaxation.

WDYT ?

>>  * config/bpf/bpf.md: Change calls to bpf_output_call.
>>  * config/bpf/btfext-out.cc (struct btf_ext_lineinfo): Add fields
>>  to struct.
>>  (bpf_create_lineinfo, btf_add_line_info_for): Add support
>>  function to insert line_info data in respective structures.
>>  (output_btfext_line_info): Function to emit line_info data in
>>  .BTF.ext section.
>>  (btf_ext_output): Call output_btfext_line_info.
>>  * config/bpf/btfext-out.h: Add prototype for
>>  btf_add_line_info_for.
>> ---
>>  gcc/config/bpf/bpf-protos.h   |   2 +-
>>  gcc/config/bpf/bpf.cc | 103 ++---
>>  gcc/config/bpf/bpf.md |   4 +-
>>  gcc/config/bpf/btfext-out.cc  | 108 +-
>>  gcc/config/bpf/btfext-out.h   |   4 +
>>  .../gcc.target/bpf/btfext-funcinfo.c  |   3 +-
>>  6 files changed, 203 insertions(+), 21 deletions(-)
>>
>> diff --git a/gcc/config/bpf/bpf-protos.h b/gcc/config/bpf/bpf-protos.h
>> index b4866d34209..ddaca50af69 100644
>> --- a/gcc/config/bpf/bpf-protos.h
>> +++ b/gcc/config/bpf/bpf-protos.h
>> @@ -23,7 +23,7 @@ along with GCC; see the file COPYING3.  If not see
>>  /* Routines implemented in bpf.cc.  */
>>
>>  extern HOST_WIDE_INT bpf_initial_elimination_offset (int, int);
>> -extern const char *bpf_output_call (rtx);
>> +extern const char *bpf_output_call (const char *templ, rtx *, int 
>> target_index);
>>  extern void bpf_target_macros (cpp_reader *);
>>  extern void bpf_print_operand (FILE *, rtx, int);
>>  extern void bpf_print_operand_address (FILE *, rtx);
>> diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
>> index d9141dd625a..f1a8eb8d62c 100644
>> --- a/gcc/config/bpf/bpf.cc
>> +++ b/gcc/config/bpf/bpf.cc
>> @@ -754,14 +754,12 @@ bpf_output_destructor (rtx symbol, int priority 
>> ATTRIBUTE_UNUSED)
>> bpf.md.  */
>>
>>  const char *
>> -bpf_output_call (rtx target)
>> +bpf_output_call (const char *templ, rtx *operands, int target_index)
>>  {
>> -  rtx xops[1];
>> -
>> +  rtx target = operands[target_index];
>>switch (GET_CODE (target))
>>  {
>>  case CONST_INT:
>> -  output_asm_insn ("call\t%0", );
>>break;
>>  case SYMBOL_REF:
>>{
>> @@ -774,26 +772,20 @@ bpf_output_call (rtx target)
>>{
>>  tree attr_args = TREE_VALUE (attr);
>>
>> -xops[0] = GEN_INT (TREE_INT_CST_LOW (TREE_VALUE (attr_args)));
>> -output_asm_insn ("call\t%0", xops);
>> -  }
>> -else
>> -  output_asm_insn ("call\t%0", );
>> +operands[target_index] = GEN_INT (TREE_INT_CST_LOW (TREE_VALUE 
>> (attr_args)));
>>
>> +  }
>>  break;
>>}
>>  default:
>> -  if (TARGET_XBPF)
>> -output_asm_insn ("call\t%0", );
>> -  else
>> +  if (!TARGET_XBPF)
>>  {
>>error ("indirect call in function, which are not supported by eBPF");
>> -  output_asm_insn ("call 0", NULL);
>> +  operands[target_index] = GEN_INT (0);
>>  }
>>break;
>>  }
>> -
>> -  return "";
>> +  return templ;
>>  }
>>
>>  const char *
>> @@ -1144,6 +1136,87 @@ bpf_debug_unwind_info ()
>>  

Re: [PATCH 2/3] bpf: remove huge memory waste with string allocation.

2024-04-11 Thread Cupertino Miranda


Hi David,

Thanks for the review.
Will apply the changes.
Nice catch and optimization with the vlen size.

Cupertino

David Faust writes:

> Hi Cupertino,
>
> On 4/11/24 04:11, Cupertino Miranda wrote:
>> Code was allocating way too much space for the string.
>
> A little bit more description would not hurt. Perhaps you could say
> something like:
> "The BPF backend was allocating an unnecessarily large string when
>  constructing CO-RE relocations for enum types."
>
>>
>> gcc/ChangeLog:
>>  * config/bpf/core-builtins.cc (process_enum_value): Corrected
>>  string allocation.
>
> nit: present tense, i.e. "Correct" rather than "Corrected"
>
>> ---
>>  gcc/config/bpf/core-builtins.cc | 7 ---
>>  1 file changed, 4 insertions(+), 3 deletions(-)
>>
>> diff --git a/gcc/config/bpf/core-builtins.cc 
>> b/gcc/config/bpf/core-builtins.cc
>> index e03e986e2c1..ead1777d465 100644
>> --- a/gcc/config/bpf/core-builtins.cc
>> +++ b/gcc/config/bpf/core-builtins.cc
>> @@ -872,10 +872,11 @@ process_enum_value (struct cr_builtins *data)
>>  {
>>if (TREE_VALUE (l) == expr)
>>  {
>> -  char *tmp = (char *) ggc_alloc_atomic ((index / 10) + 1);
>> +  /* Array size is 21 = ceil(log_10(2^64)) + 1 to hold string
>> + representations of 64 bit integers.  */
>> +  char tmp[21];
>>sprintf (tmp, "%d", index);
>
> It looks like `index' is an `unsigned int', so this sprintf should use
> %u rather %d, no?
>
> Also, it occurs to me that the `vlen' of BTF types is only 16 bits, so
> BTF has no way currently to represent enums with more than 65535
> enumerators. It may be worth adding a sanity check to bail out (error)
> here if we're going to claim an index higher than that. And if that is
> validated before the printf, the buffer can be 6 bytes ("65535\0").
>
>> -  ret.str = (const char *) tmp;
>> -
>> +  ret.str = CONST_CAST (char *, ggc_strdup(tmp));
>>break;
>>  }
>>index++;


[PATCH 3/3] bpf: add line_info support to BTF.ext section.

2024-04-11 Thread Cupertino Miranda
This patch adds line_info debug information support to .BTF.ext
sections.
Line info information is used by the BPF verifier to improve error
reporting and give more precise source core referenced errors.

gcc/Changelog:
* config/bpf/bpf-protos.h (bpf_output_call): Change prototype.
* config/bpf/bpf.cc (bpf_output_call): Change to adapt operands
and return
the instruction template instead of emmidiatelly emit asm and
not allow proper final expected execution flow.
(bpf_output_line_info): Add function to introduce line info
entries in respective structures
(bpf_asm_out_unwind_emit): Add function as hook to
TARGET_ASM_UNWIND_EMIT. This hook is called before any
instruction is emitted.
* config/bpf/bpf.md: Change calls to bpf_output_call.
* config/bpf/btfext-out.cc (struct btf_ext_lineinfo): Add fields
to struct.
(bpf_create_lineinfo, btf_add_line_info_for): Add support
function to insert line_info data in respective structures.
(output_btfext_line_info): Function to emit line_info data in
.BTF.ext section.
(btf_ext_output): Call output_btfext_line_info.
* config/bpf/btfext-out.h: Add prototype for
btf_add_line_info_for.
---
 gcc/config/bpf/bpf-protos.h   |   2 +-
 gcc/config/bpf/bpf.cc | 103 ++---
 gcc/config/bpf/bpf.md |   4 +-
 gcc/config/bpf/btfext-out.cc  | 108 +-
 gcc/config/bpf/btfext-out.h   |   4 +
 .../gcc.target/bpf/btfext-funcinfo.c  |   3 +-
 6 files changed, 203 insertions(+), 21 deletions(-)

diff --git a/gcc/config/bpf/bpf-protos.h b/gcc/config/bpf/bpf-protos.h
index b4866d34209..ddaca50af69 100644
--- a/gcc/config/bpf/bpf-protos.h
+++ b/gcc/config/bpf/bpf-protos.h
@@ -23,7 +23,7 @@ along with GCC; see the file COPYING3.  If not see
 /* Routines implemented in bpf.cc.  */
 
 extern HOST_WIDE_INT bpf_initial_elimination_offset (int, int);
-extern const char *bpf_output_call (rtx);
+extern const char *bpf_output_call (const char *templ, rtx *, int 
target_index);
 extern void bpf_target_macros (cpp_reader *);
 extern void bpf_print_operand (FILE *, rtx, int);
 extern void bpf_print_operand_address (FILE *, rtx);
diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
index d9141dd625a..f1a8eb8d62c 100644
--- a/gcc/config/bpf/bpf.cc
+++ b/gcc/config/bpf/bpf.cc
@@ -754,14 +754,12 @@ bpf_output_destructor (rtx symbol, int priority 
ATTRIBUTE_UNUSED)
bpf.md.  */
 
 const char *
-bpf_output_call (rtx target)
+bpf_output_call (const char *templ, rtx *operands, int target_index)
 {
-  rtx xops[1];
-
+  rtx target = operands[target_index];
   switch (GET_CODE (target))
 {
 case CONST_INT:
-  output_asm_insn ("call\t%0", );
   break;
 case SYMBOL_REF:
   {
@@ -774,26 +772,20 @@ bpf_output_call (rtx target)
  {
tree attr_args = TREE_VALUE (attr);
 
-   xops[0] = GEN_INT (TREE_INT_CST_LOW (TREE_VALUE (attr_args)));
-   output_asm_insn ("call\t%0", xops);
- }
-   else
- output_asm_insn ("call\t%0", );
+   operands[target_index] = GEN_INT (TREE_INT_CST_LOW (TREE_VALUE 
(attr_args)));
 
+ }
break;
   }
 default:
-  if (TARGET_XBPF)
-   output_asm_insn ("call\t%0", );
-  else
+  if (!TARGET_XBPF)
{
  error ("indirect call in function, which are not supported by eBPF");
- output_asm_insn ("call 0", NULL);
+ operands[target_index] = GEN_INT (0);
}
   break;
 }
-
-  return "";
+  return templ;
 }
 
 const char *
@@ -1144,6 +1136,87 @@ bpf_debug_unwind_info ()
 #undef TARGET_DEBUG_UNWIND_INFO
 #define TARGET_DEBUG_UNWIND_INFO bpf_debug_unwind_info
 
+/* Create a BTF.ext line_info entry.  */
+
+static void
+bpf_output_line_info (FILE *asm_out_file, rtx_insn *insn)
+{
+  static unsigned int line_info_label = 1;
+  static tree cfun_decl = NULL_TREE;
+  static bool func_start_added = false;
+  const char *label = NULL;
+  unsigned int loc = 0;
+  const char *filename = NULL;
+  unsigned int line = 0;
+  unsigned int column = 0;
+
+  if(!btf_debuginfo_p ())
+return;
+
+  gcc_assert (insn != NULL_RTX);
+
+  if (current_function_decl != cfun_decl
+  && GET_CODE (insn) == NOTE)
+{
+  label = current_function_func_begin_label;
+  loc = DECL_SOURCE_LOCATION (current_function_decl);
+  filename = LOCATION_FILE (loc);
+  line = LOCATION_LINE (loc);
+  column = LOCATION_COLUMN (loc);
+  func_start_added = true;
+}
+  else
+{
+  if (GET_CODE (insn) == NOTE)
+   return;
+
+  /* Already added a label for this location. This might not be fully
+acurate but it is better then adding 2 entries on the same location,
+which is imcompatible with the verifier expectations.  */
+  if 

[PATCH 1/3] bpf: support more instructions to match CO-RE relocations

2024-04-11 Thread Cupertino Miranda
BPF supports multiple instructions to be CO-RE relocatable regardless of
the position of the immediate field in the encoding.
In particular, not only the MOV instruction allows a CO-RE
relocation of its immediate operand, but the LD and ST instructions can
have a CO-RE relocation happening to their offset immediate operand,
even though those operands are encoded in different encoding bits.
This patch moves matching from a more traditional matching of the
UNSPEC_CORE_RELOC pattern within a define_insn to a match within the
constraints of both immediates and address operands from more generic
mov define_insn rule.

gcc/Changelog:
* config/bpf/bpf-protos.h (bpf_add_core_reloc): Renamed function
to bpf_output_move.
* config/bpf/bpf.cc (bpf_legitimate_address_p): Allow
UNSPEC_CORE_RELOC to match an address.
(bpf_insn_cost): Make UNSPEC_CORE_RELOC immediate moves
expensive to prioritize loads and stores.
(TARGET_INSN_COST): Add hook.
(bpf_output_move): Wrapper to call bpf_output_core_reloc.
(bpf_print_operand): Add support to print immediate operands
specified with the UNSPEC_CORE_RELOC.
(bpf_print_operand_address): Likewise, but to support
UNSPEC_CORE_RELOC in addresses.
(bpf_init_builtins): Flag BPF_BUILTIN_CORE_RELOC as NOTHROW.
* config/bpf/bpf.md: Wrap patterns for MOV, LD and ST
instruction with bpf_output_move call.
(mov_reloc_core): Remove now spurious define_insn.
* config/bpf/constraints.md: Added "c" and "C" constraints to
match immediates represented with UNSPEC_CORE_RELOC.
* config/bpf/core-builtins.cc (bpf_add_core_reloc): Remove
(bpf_output_core_reloc): Add function to create the CO-RE
relocations based on new matching rules.
* config/bpf/core-builtins.h (bpf_output_core_reloc): Add
prototype.
* config/bpf/predicates.md (core_imm_operand) Add predicate.
(mov_src_operand): Add match for core_imm_operand.

gcc/testsuite/ChangeLog:
* gcc.target/bpf/btfext-funcinfo.c: Updated to changes.
* gcc.target/bpf/core-builtin-fieldinfo-const-elimination.c:
Likewise.
* gcc.target/bpf/core-builtin-fieldinfo-existence-1.c: Likewise.
* gcc.target/bpf/core-builtin-fieldinfo-lshift-1-be.c: Likewise.
* gcc.target/bpf/core-builtin-fieldinfo-lshift-1-le.c: Likewise.
* gcc.target/bpf/core-builtin-fieldinfo-lshift-2.c: Likewise.
* gcc.target/bpf/core-builtin-fieldinfo-offset-1.c: Likewise.
* gcc.target/bpf/core-builtin-fieldinfo-rshift-1.c: Likewise.
* gcc.target/bpf/core-builtin-fieldinfo-rshift-2.c: Likewise.
* gcc.target/bpf/core-builtin-fieldinfo-sign-1.c: Likewise.
* gcc.target/bpf/core-builtin-fieldinfo-sign-2.c: Likewise.
* gcc.target/bpf/core-builtin-fieldinfo-size-1.c: Likewise.
---
 gcc/config/bpf/bpf-protos.h   |  2 +-
 gcc/config/bpf/bpf.cc | 54 +-
 gcc/config/bpf/bpf.md | 56 ++-
 gcc/config/bpf/constraints.md | 20 ++
 gcc/config/bpf/core-builtins.cc   | 71 ++-
 gcc/config/bpf/core-builtins.h|  2 +
 gcc/config/bpf/predicates.md  |  7 +-
 .../gcc.target/bpf/btfext-funcinfo.c  |  2 -
 ...core-builtin-fieldinfo-const-elimination.c |  2 +-
 .../bpf/core-builtin-fieldinfo-existence-1.c  |  2 +-
 .../bpf/core-builtin-fieldinfo-lshift-1-be.c  |  8 +--
 .../bpf/core-builtin-fieldinfo-lshift-1-le.c  |  8 +--
 .../bpf/core-builtin-fieldinfo-lshift-2.c |  6 +-
 .../bpf/core-builtin-fieldinfo-offset-1.c | 12 ++--
 .../bpf/core-builtin-fieldinfo-rshift-1.c |  8 +--
 .../bpf/core-builtin-fieldinfo-rshift-2.c |  4 +-
 .../bpf/core-builtin-fieldinfo-sign-1.c   |  4 +-
 .../bpf/core-builtin-fieldinfo-sign-2.c   |  4 +-
 .../bpf/core-builtin-fieldinfo-size-1.c   |  8 +--
 19 files changed, 189 insertions(+), 91 deletions(-)

diff --git a/gcc/config/bpf/bpf-protos.h b/gcc/config/bpf/bpf-protos.h
index ac0c2f4038f..b4866d34209 100644
--- a/gcc/config/bpf/bpf-protos.h
+++ b/gcc/config/bpf/bpf-protos.h
@@ -30,7 +30,7 @@ extern void bpf_print_operand_address (FILE *, rtx);
 extern void bpf_expand_prologue (void);
 extern void bpf_expand_epilogue (void);
 extern void bpf_expand_cbranch (machine_mode, rtx *);
-const char *bpf_add_core_reloc (rtx *operands, const char *templ);
+const char *bpf_output_move (rtx *operands, const char *templ);
 
 class gimple_opt_pass;
 gimple_opt_pass *make_pass_lower_bpf_core (gcc::context *ctxt);
diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
index fb60770c170..d9141dd625a 100644
--- a/gcc/config/bpf/bpf.cc
+++ b/gcc/config/bpf/bpf.cc
@@ -584,6 +584,16 @@ bpf_legitimate_address_p (machine_mode mode,
if (bpf_address_base_p (x0, strict) && GET_CODE (x1) == CONST_INT)
  return 

[PATCH 2/3] bpf: remove huge memory waste with string allocation.

2024-04-11 Thread Cupertino Miranda
Code was allocating way too much space for the string.

gcc/ChangeLog:
* config/bpf/core-builtins.cc (process_enum_value): Corrected
string allocation.
---
 gcc/config/bpf/core-builtins.cc | 7 ---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc
index e03e986e2c1..ead1777d465 100644
--- a/gcc/config/bpf/core-builtins.cc
+++ b/gcc/config/bpf/core-builtins.cc
@@ -872,10 +872,11 @@ process_enum_value (struct cr_builtins *data)
{
  if (TREE_VALUE (l) == expr)
{
- char *tmp = (char *) ggc_alloc_atomic ((index / 10) + 1);
+ /* Array size is 21 = ceil(log_10(2^64)) + 1 to hold string
+representations of 64 bit integers.  */
+ char tmp[21];
  sprintf (tmp, "%d", index);
- ret.str = (const char *) tmp;
-
+ ret.str = CONST_CAST (char *, ggc_strdup(tmp));
  break;
}
  index++;
-- 
2.30.2



Re: [PATCH] btf: Emit labels in DATASEC bts_offset entries.

2024-03-27 Thread Cupertino Miranda


Hi Jakub,

Thanks for the patch and appologies for the results regression.

Cupertino

Jakub Jelinek writes:

> On Tue, Mar 26, 2024 at 02:28:52PM +0000, Cupertino Miranda wrote:
>> GCC was defining bts_offset entry to always contain 0.
>> When comparing with clang, the same entry is instead a label to the
>> respective variable or function. The assembler emits relocations for
>> those labels.
>>
>> gcc/ChangeLog:
>>  PR target/114431
>>  * btfout.cc (get_name_for_datasec_entry): Add function.
>>  (btf_asm_datasec_entry): Print label when possible.
>>
>> gcc/testsuite/ChangeLog:
>>  gcc.dg/debug/btf/btf-datasec-1.c: Correct for new
>>  implementation.
>>  gcc.dg/debug/btf/btf-datasec-2.c: Likewise
>>  gcc.dg/debug/btf/btf-pr106773.c: Likewise
>
> For next time, missing dot after Likewise
>
>> --- a/gcc/testsuite/gcc.dg/debug/btf/btf-datasec-1.c
>> +++ b/gcc/testsuite/gcc.dg/debug/btf/btf-datasec-1.c
>> @@ -19,8 +19,10 @@
>>  /* { dg-final { scan-assembler-times "0xf03\[\t \]+\[^\n\]*btt_info" 2 
>> } } */
>>  /* { dg-final { scan-assembler-times "0xf01\[\t \]+\[^\n\]*btt_info" 1 
>> } } */
>>
>> -/* The offset entry for each variable in a DATSEC should be 0 at compile 
>> time.  */
>> -/* { dg-final { scan-assembler-times "0\[\t \]+\[^\n\]*bts_offset" 7 } } */
>> +/* The offset entry for each variable in a DATSEC should contain a label.  
>> */
>> +/* { dg-final { scan-assembler-times ".4byte\[\t \]\[a-e\]\[\t 
>> \]+\[^\n\]*bts_offset" 5 } } */
>
> Where has this been tested?
> 4byte is used only on some targets, what exact assembler directive is used
> for 4byte unaligned data is heavily target dependent.
> git grep define.*TARGET_ASM_UNALIGNED_SI_OP
> gcc/config/alpha/alpha.cc:#define TARGET_ASM_UNALIGNED_SI_OP "\t.align 
> 0\n\t.long\t"
> gcc/config/avr/avr.cc:#define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
> gcc/config/cris/cris.cc:#define TARGET_ASM_UNALIGNED_SI_OP 
> TARGET_ASM_ALIGNED_SI_OP
> gcc/config/csky/csky.cc:#define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
> gcc/config/i386/i386.cc:#define TARGET_ASM_UNALIGNED_SI_OP 
> TARGET_ASM_ALIGNED_SI_OP
> gcc/config/ia64/ia64.cc:#define TARGET_ASM_UNALIGNED_SI_OP "\tdata4.ua\t"
> gcc/config/m68k/m68k.cc:#define TARGET_ASM_UNALIGNED_SI_OP 
> TARGET_ASM_ALIGNED_SI_OP
> gcc/config/mcore/mcore.cc:#define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
> gcc/config/pa/pa.cc:#define TARGET_ASM_UNALIGNED_SI_OP 
> TARGET_ASM_ALIGNED_SI_OP
> gcc/config/rs6000/rs6000.cc:#define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
> gcc/config/rs6000/rs6000.cc:#define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
> gcc/config/sh/sh.cc:#define TARGET_ASM_UNALIGNED_SI_OP "\t.ualong\t"
> gcc/config/sparc/sparc.cc:#define TARGET_ASM_UNALIGNED_SI_OP "\t.uaword\t"
> gcc/target-def.h:#define TARGET_ASM_UNALIGNED_SI_OP "\t.4byte\t"
> gcc/target-def.h:#define TARGET_ASM_UNALIGNED_SI_OP NULL
> Now
> git grep 'define[[:blank:]]*TARGET_ASM_ALIGNED_SI_OP' | grep 
> '/\(cris\|i386\|m68k\|pa\)/'
> gcc/config/cris/cris.cc:#define TARGET_ASM_ALIGNED_SI_OP "\t.dword\t"
> gcc/config/i386/i386.cc:#define TARGET_ASM_ALIGNED_SI_OP ASM_LONG
> gcc/config/m68k/m68k.cc:#define TARGET_ASM_ALIGNED_SI_OP "\tlong\t"
> gcc/config/m68k/m68k.cc:#define TARGET_ASM_ALIGNED_SI_OP "\tdc.l\t"
> gcc/config/pa/pa.cc:#define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
> git grep 'define[[:blank:]]*ASM_LONG'
> gcc/config/i386/att.h:#define ASM_LONG "\t.long\t"
> gcc/config/i386/bsd.h:#define ASM_LONG "\t.long\t"
> gcc/config/i386/darwin.h:#define ASM_LONG "\t.long\t"
>
> So, if you want to handle it for all arches, instead of .4byte\[\t \] you'd 
> need to
> use
> (?:(?:.4byte|.long|data4.ua|.ualong|.uaword|.dword|long|dc.l|.word)\[\t 
> \]|.vbyte\t4,\[\t \]?)
> or so.
>
> BTW,
> /* { dg-options "-O0 -gbtf -dA" } */
> /* { dg-options "-O0 -gbtf -dA -msdata=none" { target { { powerpc*-*-* } && 
> ilp32 } } } */
> /* { dg-options "-O0 -gbtf -dA -msmall-data-limit=0" { target { riscv*-*-* } 
> } } */
> /* { dg-options "-O0 -gbtf -dA -G0" { target { nios2-*-* } } } */
> would be perhaps right for test written 2 decades ago, but for a test
> written 3 years ago is something that shouldn't have passed patch review.
> This should be done with
> /* { dg-options "-O0 -gbtf -dA" } */
> /* { dg-additional-options "-msdata=none" { target { { powerpc*-*-* } && 
> ilp32 } } } */
> /* { dg-additional-options "-msmall-data-limit=0" { target { riscv*-*-* } } } 
> */
> /* { dg-additional-options "-G0" { target { nios2-*-* } } } */
>
> btf-cvr-quals-1.c test suffers from that too.
>
>   Jakub


Re: [PATCH] btf: Emit labels in DATASEC bts_offset entries.

2024-03-26 Thread Cupertino Miranda


David Faust writes:

> On 3/26/24 07:28, Cupertino Miranda wrote:
>> Hi everyone,
>>
>> This patch is an expected fix for the issue reported by systemd in:
>> https://github.com/systemd/systemd/issues/31888
>> Also, Jose Marchesi opened the following bugzilla to report it:
>> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114431
>>
>> Please notice that the function created was inspired by existing code
>> in the function btf_asm_type_ref.
>>
>> Looking forward to your comments.
>
> OK, LGTM.
> Thanks for the fix!

Pushed! Thanks!

>>
>> Cupertino
>>
>>
>> GCC was defining bts_offset entry to always contain 0.
>> When comparing with clang, the same entry is instead a label to the
>> respective variable or function. The assembler emits relocations for
>> those labels.
>>
>> gcc/ChangeLog:
>>  PR target/114431
>>  * btfout.cc (get_name_for_datasec_entry): Add function.
>>  (btf_asm_datasec_entry): Print label when possible.
>>
>> gcc/testsuite/ChangeLog:
>>  gcc.dg/debug/btf/btf-datasec-1.c: Correct for new
>>  implementation.
>>  gcc.dg/debug/btf/btf-datasec-2.c: Likewise
>>  gcc.dg/debug/btf/btf-pr106773.c: Likewise
>> ---
>>  gcc/btfout.cc | 30 ++-
>>  .../gcc.dg/debug/btf/btf-datasec-1.c  |  6 ++--
>>  .../gcc.dg/debug/btf/btf-datasec-2.c  |  7 +++--
>>  gcc/testsuite/gcc.dg/debug/btf/btf-pr106773.c |  2 +-
>>  4 files changed, 39 insertions(+), 6 deletions(-)
>>
>> diff --git a/gcc/btfout.cc b/gcc/btfout.cc
>> index e63289bc554..5f708630e04 100644
>> --- a/gcc/btfout.cc
>> +++ b/gcc/btfout.cc
>> @@ -1014,13 +1014,41 @@ btf_asm_func_type (ctf_container_ref ctfc, 
>> ctf_dtdef_ref dtd, ctf_id_t id)
>>btf_asm_type_ref ("btt_type", ctfc, get_btf_id (ref_id));
>>  }
>>
>> +/* Collect the name for the DATASEC reference required to be output as a
>> +   symbol. */
>> +
>> +static const char *
>> +get_name_for_datasec_entry (ctf_container_ref ctfc, ctf_id_t ref_id)
>> +{
>> +  if (ref_id >= num_types_added + 1
>> +  && ref_id < num_types_added + num_vars_added + 1)
>> +{
>> +  /* Ref to a variable.  Should only appear in DATASEC entries.  */
>> +  ctf_id_t var_id = btf_relative_var_id (ref_id);
>> +  ctf_dvdef_ref dvd = ctfc->ctfc_vars_list[var_id];
>> +  return dvd->dvd_name;
>> +}
>> +  else if (ref_id >= num_types_added + num_vars_added + 1)
>> +{
>> +  /* Ref to a FUNC record.  */
>> +  size_t func_id = btf_relative_func_id (ref_id);
>> +  ctf_dtdef_ref ref_type = (*funcs)[func_id];
>> +  return get_btf_type_name (ref_type);
>> +}
>> +  return NULL;
>> +}
>> +
>>  /* Asm'out a variable entry following a BTF_KIND_DATASEC.  */
>>
>>  static void
>>  btf_asm_datasec_entry (ctf_container_ref ctfc, struct btf_var_secinfo info)
>>  {
>> +  const char *symbol_name = get_name_for_datasec_entry (ctfc, info.type);
>>btf_asm_type_ref ("bts_type", ctfc, info.type);
>> -  dw2_asm_output_data (4, info.offset, "bts_offset");
>> +  if (symbol_name == NULL)
>> +dw2_asm_output_data (4, info.offset, "bts_offset");
>> +  else
>> +dw2_asm_output_offset (4, symbol_name, NULL, "bts_offset");
>>dw2_asm_output_data (4, info.size, "bts_size");
>>  }
>>
>> diff --git a/gcc/testsuite/gcc.dg/debug/btf/btf-datasec-1.c 
>> b/gcc/testsuite/gcc.dg/debug/btf/btf-datasec-1.c
>> index 77df88648c5..8557c38c20d 100644
>> --- a/gcc/testsuite/gcc.dg/debug/btf/btf-datasec-1.c
>> +++ b/gcc/testsuite/gcc.dg/debug/btf/btf-datasec-1.c
>> @@ -19,8 +19,10 @@
>>  /* { dg-final { scan-assembler-times "0xf03\[\t \]+\[^\n\]*btt_info" 2 
>> } } */
>>  /* { dg-final { scan-assembler-times "0xf01\[\t \]+\[^\n\]*btt_info" 1 
>> } } */
>>
>> -/* The offset entry for each variable in a DATSEC should be 0 at compile 
>> time.  */
>> -/* { dg-final { scan-assembler-times "0\[\t \]+\[^\n\]*bts_offset" 7 } } */
>> +/* The offset entry for each variable in a DATSEC should contain a label.  
>> */
>> +/* { dg-final { scan-assembler-times ".4byte\[\t \]\[a-e\]\[\t 
>> \]+\[^\n\]*bts_offset" 5 } } */
>> +/* { dg-final { scan-assembler-times "my_cstruct\[\t \]+\[^\n\]*bts_offset" 
>> 1 } } */
>> +/* { dg-final { scan-assembler-times &q

[PATCH] btf: Emit labels in DATASEC bts_offset entries.

2024-03-26 Thread Cupertino Miranda
Hi everyone,

This patch is an expected fix for the issue reported by systemd in:
https://github.com/systemd/systemd/issues/31888
Also, Jose Marchesi opened the following bugzilla to report it:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114431

Please notice that the function created was inspired by existing code
in the function btf_asm_type_ref.

Looking forward to your comments.

Cupertino


GCC was defining bts_offset entry to always contain 0.
When comparing with clang, the same entry is instead a label to the
respective variable or function. The assembler emits relocations for
those labels.

gcc/ChangeLog:
PR target/114431
* btfout.cc (get_name_for_datasec_entry): Add function.
(btf_asm_datasec_entry): Print label when possible.

gcc/testsuite/ChangeLog:
gcc.dg/debug/btf/btf-datasec-1.c: Correct for new
implementation.
gcc.dg/debug/btf/btf-datasec-2.c: Likewise
gcc.dg/debug/btf/btf-pr106773.c: Likewise
---
 gcc/btfout.cc | 30 ++-
 .../gcc.dg/debug/btf/btf-datasec-1.c  |  6 ++--
 .../gcc.dg/debug/btf/btf-datasec-2.c  |  7 +++--
 gcc/testsuite/gcc.dg/debug/btf/btf-pr106773.c |  2 +-
 4 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/gcc/btfout.cc b/gcc/btfout.cc
index e63289bc554..5f708630e04 100644
--- a/gcc/btfout.cc
+++ b/gcc/btfout.cc
@@ -1014,13 +1014,41 @@ btf_asm_func_type (ctf_container_ref ctfc, 
ctf_dtdef_ref dtd, ctf_id_t id)
   btf_asm_type_ref ("btt_type", ctfc, get_btf_id (ref_id));
 }
 
+/* Collect the name for the DATASEC reference required to be output as a
+   symbol. */
+
+static const char *
+get_name_for_datasec_entry (ctf_container_ref ctfc, ctf_id_t ref_id)
+{
+  if (ref_id >= num_types_added + 1
+  && ref_id < num_types_added + num_vars_added + 1)
+{
+  /* Ref to a variable.  Should only appear in DATASEC entries.  */
+  ctf_id_t var_id = btf_relative_var_id (ref_id);
+  ctf_dvdef_ref dvd = ctfc->ctfc_vars_list[var_id];
+  return dvd->dvd_name;
+}
+  else if (ref_id >= num_types_added + num_vars_added + 1)
+{
+  /* Ref to a FUNC record.  */
+  size_t func_id = btf_relative_func_id (ref_id);
+  ctf_dtdef_ref ref_type = (*funcs)[func_id];
+  return get_btf_type_name (ref_type);
+}
+  return NULL;
+}
+
 /* Asm'out a variable entry following a BTF_KIND_DATASEC.  */
 
 static void
 btf_asm_datasec_entry (ctf_container_ref ctfc, struct btf_var_secinfo info)
 {
+  const char *symbol_name = get_name_for_datasec_entry (ctfc, info.type);
   btf_asm_type_ref ("bts_type", ctfc, info.type);
-  dw2_asm_output_data (4, info.offset, "bts_offset");
+  if (symbol_name == NULL)
+dw2_asm_output_data (4, info.offset, "bts_offset");
+  else
+dw2_asm_output_offset (4, symbol_name, NULL, "bts_offset");
   dw2_asm_output_data (4, info.size, "bts_size");
 }
 
diff --git a/gcc/testsuite/gcc.dg/debug/btf/btf-datasec-1.c 
b/gcc/testsuite/gcc.dg/debug/btf/btf-datasec-1.c
index 77df88648c5..8557c38c20d 100644
--- a/gcc/testsuite/gcc.dg/debug/btf/btf-datasec-1.c
+++ b/gcc/testsuite/gcc.dg/debug/btf/btf-datasec-1.c
@@ -19,8 +19,10 @@
 /* { dg-final { scan-assembler-times "0xf03\[\t \]+\[^\n\]*btt_info" 2 } } 
*/
 /* { dg-final { scan-assembler-times "0xf01\[\t \]+\[^\n\]*btt_info" 1 } } 
*/
 
-/* The offset entry for each variable in a DATSEC should be 0 at compile time. 
 */
-/* { dg-final { scan-assembler-times "0\[\t \]+\[^\n\]*bts_offset" 7 } } */
+/* The offset entry for each variable in a DATSEC should contain a label.  */
+/* { dg-final { scan-assembler-times ".4byte\[\t \]\[a-e\]\[\t 
\]+\[^\n\]*bts_offset" 5 } } */
+/* { dg-final { scan-assembler-times "my_cstruct\[\t \]+\[^\n\]*bts_offset" 1 
} } */
+/* { dg-final { scan-assembler-times "bigarr\[\t \]+\[^\n\]*bts_offset" 1 } } 
*/
 
 /* Check that strings for each DATASEC have been added to the BTF string 
table.  */
 /* { dg-final { scan-assembler-times "ascii \".data.0\"\[\t 
\]+\[^\n\]*btf_aux_string" 1 } } */
diff --git a/gcc/testsuite/gcc.dg/debug/btf/btf-datasec-2.c 
b/gcc/testsuite/gcc.dg/debug/btf/btf-datasec-2.c
index d6f3358d6fb..857d830e446 100644
--- a/gcc/testsuite/gcc.dg/debug/btf/btf-datasec-2.c
+++ b/gcc/testsuite/gcc.dg/debug/btf/btf-datasec-2.c
@@ -9,8 +9,11 @@
 /* { dg-final { scan-assembler-times " BTF_KIND_DATASEC 
'.foo_sec'\[\\r\\n\]+\[^\\r\\n\]*0xf01\[\t \]+\[^\n\]*btt_info" 1 } } */
 /* { dg-final { scan-assembler-times " BTF_KIND_DATASEC 
'.bar_sec'\[\\r\\n\]+\[^\\r\\n\]*0xf02\[\t \]+\[^\n\]*btt_info" 1 } } */
 
-/* Function entries should have offset and size of 0 at compile time.  */
-/* { dg-final { scan-assembler-times "0\[\t \]+\[^\n\]*bts_offset" 3 } } */
+/* Function entries should have offset with a label and size of 0 at compile 
time.  */
+/* { dg-final { scan-assembler-times "chacha\[\t \]+\[^\n\]*bts_offset" 1 } } 
*/
+/* { dg-final { scan-assembler-times "bar\[\t \]+\[^\n\]*bts_offset" 1 } } */
+/* { 

Re: [PATCH 3/3] bpf: Corrected index computation when present with unnamed struct fields

2024-03-20 Thread Cupertino Miranda


David Faust writes:

> On 3/13/24 07:24, Cupertino Miranda wrote:
>> Any unnamed structure field if not a member of the BTF_KIND_STRUCT.
> typo: if -> is
>
> I'd suggest to clarify that "any unnamed structure field" is really
> any unnamed non-struct-or-union field, since anonymous inner structs
> and unions certainly are present in BTF (and you handle them here).
>
>> For that reason, CO-RE access strings indexes should take that in
>> consideration. This patch adds a condition to the incrementer that
>> computes the index for the field access.
>
> Otherwise, OK.
> Thanks.

Corrected and Pushed. Thanks
>
>>
>> gcc/ChangeLog:
>>  * config/bpf/core-builtins.cc (bpf_core_get_index): Check if
>>  field contains a DECL_NAME.
>>
>> gcc/testsuite/ChangeLog:
>>  * gcc.target/bpf/core-builtin-fieldinfo-offset-1.c: Add
>>  testcase for unnamed fields.
>> ---
>>  gcc/config/bpf/core-builtins.cc|  6 +-
>>  .../gcc.target/bpf/core-builtin-fieldinfo-offset-1.c   | 10 --
>>  2 files changed, 13 insertions(+), 3 deletions(-)
>>
>> diff --git a/gcc/config/bpf/core-builtins.cc 
>> b/gcc/config/bpf/core-builtins.cc
>> index 70b14e48e6e5..8333ad81d0e0 100644
>> --- a/gcc/config/bpf/core-builtins.cc
>> +++ b/gcc/config/bpf/core-builtins.cc
>> @@ -553,7 +553,11 @@ bpf_core_get_index (const tree node, bool *valid)
>>  {
>>if (l == node)
>>  return i;
>> -  i++;
>> +  /* Skip unnamed padding, not represented by BTF.  */
>> +  if (DECL_NAME(l) != NULL_TREE
>> +  || TREE_CODE (TREE_TYPE (l)) == UNION_TYPE
>> +  || TREE_CODE (TREE_TYPE (l)) == RECORD_TYPE)
>> +i++;
>>  }
>>  }
>>else if (code == ARRAY_REF || code == ARRAY_RANGE_REF || code == MEM_REF)
>> diff --git a/gcc/testsuite/gcc.target/bpf/core-builtin-fieldinfo-offset-1.c 
>> b/gcc/testsuite/gcc.target/bpf/core-builtin-fieldinfo-offset-1.c
>> index 27654205287d..8b1d8b012a2a 100644
>> --- a/gcc/testsuite/gcc.target/bpf/core-builtin-fieldinfo-offset-1.c
>> +++ b/gcc/testsuite/gcc.target/bpf/core-builtin-fieldinfo-offset-1.c
>> @@ -14,6 +14,9 @@ struct T {
>>struct S s[2];
>>char c;
>>char d;
>> +  int a: 1;
>> +  int:31;
>> +  int f;
>>  };
>>
>>  enum {
>> @@ -38,7 +41,9 @@ unsigned int foo (struct T *t)
>>unsigned e1 = __builtin_preserve_field_info (bar()->d, FIELD_BYTE_OFFSET);
>>unsigned e2 = __builtin_preserve_field_info (bar()->s[1].a4, 
>> FIELD_BYTE_OFFSET);
>>
>> -  return s0a1 + s0a4 + s0x + s1a1 + s1a4 + s1x + c + d + e1 + e2;
>> +  unsigned f1 = __builtin_preserve_field_info (t->f, FIELD_BYTE_OFFSET);
>> +
>> +  return s0a1 + s0a4 + s0x + s1a1 + s1a4 + s1x + c + d + e1 + e2 + f1;
>>  }
>>
>>  /* { dg-final { scan-assembler-times "\[\t \]mov\[\t \]%r\[0-9\],4" 2 } } */
>> @@ -65,5 +70,6 @@ unsigned int foo (struct T *t)
>>  /* { dg-final { scan-assembler-times "bpfcr_astr_off \\(\"0:1:1:4\"\\)" 1 } 
>> } */
>>  /* { dg-final { scan-assembler-times "bpfcr_astr_off \\(\"0:2\"\\)" 1 } } */
>>  /* { dg-final { scan-assembler-times "bpfcr_astr_off \\(\"0:3\"\\)" 2 } } */
>> +/* { dg-final { scan-assembler-times "bpfcr_astr_off \\(\"0:5\"\\)" 1 } } */
>>
>> -/* { dg-final { scan-assembler-times "0\[\t \]+\[^\n\]*bpfcr_kind" 10 } } */
>> +/* { dg-final { scan-assembler-times "0\[\t \]+\[^\n\]*bpfcr_kind" 11 } } */


Re: [PATCH 2/3] bpf: Fix access string default for CO-RE type based relocations

2024-03-20 Thread Cupertino Miranda


David Faust writes:

> On 3/13/24 07:24, Cupertino Miranda wrote:
>> Although part of all CO-RE relocation data, type based relocations do
>> not require an access string.
>> Initial implementation defined it as an empty string.
>> On the other hand, libbpf when parsing the CO-RE relocations verifies
>> that those strings would contain "0", otherwise reports an error.
>> This patch makes GCC compliant with libbpf expectations.
>
> OK, thanks.
>
Pushed! Thanks
>>
>> gcc/Changelog:
>>  * config/bpf/btfext-out.cc (cpf_core_reloc_add): Correct for new code.
>>  Add assert to validate the string is set.
>>  * config/bpf/core-builtins.cc (cr_final): Make string struct
>>  field as const.
>>  (process_enum_value): Correct for field type change.
>>  (process_type): Set access string to "0".
>>
>> gcc/testsuite/ChangeLog:
>>  * gcc.target/bpf/core-builtin-type-based.c: Correct.
>>  * gcc.target/bpf/core-builtin-type-id.c: Correct.
>> ---
>>  gcc/config/bpf/btfext-out.cc   |  5 +++--
>>  gcc/config/bpf/core-builtins.cc| 10 ++
>>  gcc/testsuite/gcc.target/bpf/core-builtin-type-based.c |  1 +
>>  gcc/testsuite/gcc.target/bpf/core-builtin-type-id.c|  1 +
>>  4 files changed, 11 insertions(+), 6 deletions(-)
>>
>> diff --git a/gcc/config/bpf/btfext-out.cc b/gcc/config/bpf/btfext-out.cc
>> index 57c0dc323812..ff1fd0739f1e 100644
>> --- a/gcc/config/bpf/btfext-out.cc
>> +++ b/gcc/config/bpf/btfext-out.cc
>> @@ -299,8 +299,9 @@ bpf_core_reloc_add (const tree type, const char * 
>> section_name,
>>
>>/* Buffer the access string in the auxiliary strtab.  */
>>bpfcr->bpfcr_astr_off = 0;
>> -  if (accessor != NULL)
>> -bpfcr->bpfcr_astr_off = btf_ext_add_string (accessor);
>> +  gcc_assert (accessor != NULL);
>> +  bpfcr->bpfcr_astr_off = btf_ext_add_string (accessor);
>> +
>>bpfcr->bpfcr_type = get_btf_id (ctf_lookup_tree_type (ctfc, type));
>>bpfcr->bpfcr_insn_label = label;
>>bpfcr->bpfcr_kind = kind;
>> diff --git a/gcc/config/bpf/core-builtins.cc 
>> b/gcc/config/bpf/core-builtins.cc
>> index 4256fea15e49..70b14e48e6e5 100644
>> --- a/gcc/config/bpf/core-builtins.cc
>> +++ b/gcc/config/bpf/core-builtins.cc
>> @@ -205,7 +205,7 @@ struct cr_local
>>  /* Core Relocation Final data */
>>  struct cr_final
>>  {
>> -  char *str;
>> +  const char *str;
>>tree type;
>>enum btf_core_reloc_kind kind;
>>  };
>> @@ -868,8 +868,10 @@ process_enum_value (struct cr_builtins *data)
>>  {
>>if (TREE_VALUE (l) == expr)
>>  {
>> -  ret.str = (char *) ggc_alloc_atomic ((index / 10) + 1);
>> -  sprintf (ret.str, "%d", index);
>> +  char *tmp = (char *) ggc_alloc_atomic ((index / 10) + 1);
>> +  sprintf (tmp, "%d", index);
>> +  ret.str = (const char *) tmp;
>> +
>>break;
>>  }
>>index++;
>> @@ -987,7 +989,7 @@ process_type (struct cr_builtins *data)
>>|| data->kind == BPF_RELO_TYPE_MATCHES);
>>
>>struct cr_final ret;
>> -  ret.str = NULL;
>> +  ret.str = ggc_strdup ("0");
>>ret.type = data->type;
>>ret.kind = data->kind;
>>
>> diff --git a/gcc/testsuite/gcc.target/bpf/core-builtin-type-based.c 
>> b/gcc/testsuite/gcc.target/bpf/core-builtin-type-based.c
>> index 74a8d5a14d9d..9d818133c084 100644
>> --- a/gcc/testsuite/gcc.target/bpf/core-builtin-type-based.c
>> +++ b/gcc/testsuite/gcc.target/bpf/core-builtin-type-based.c
>> @@ -56,3 +56,4 @@ int foo(void *data)
>>  /* { dg-final { scan-assembler-times "0x8\[\t \]+\[^\n\]*bpfcr_kind" 13 } } 
>> BPF_TYPE_EXISTS */
>>  /* { dg-final { scan-assembler-times "0x9\[\t \]+\[^\n\]*bpfcr_kind" 11 } } 
>> BPF_TYPE_SIZE */
>>  /* { dg-final { scan-assembler-times "0xc\[\t \]+\[^\n\]*bpfcr_kind" 13 } } 
>> BPF_TYPE_MATCHES */
>> +/* { dg-final { scan-assembler-times "bpfcr_astr_off \[(\"\]+0\[(\"\]+" 37 
>> } } */
>> diff --git a/gcc/testsuite/gcc.target/bpf/core-builtin-type-id.c 
>> b/gcc/testsuite/gcc.target/bpf/core-builtin-type-id.c
>> index 4b23288eac08..9576b91bc940 100644
>> --- a/gcc/testsuite/gcc.target/bpf/core-builtin-type-id.c
>> +++ b/gcc/testsuite/gcc.target/bpf/core-builtin-type-id.c
>> @@ -38,3 +38,4 @@ int foo(void *data)
>>  /* { dg-final { scan-assembler-times "0\[\t \]+\[^\n\]*bpfcr_type" 0  { 
>> xfail *-*-* } } } */
>>  /* { dg-final { scan-assembler-times "0x6\[\t \]+\[^\n\]*bpfcr_kind" 13 } } 
>> BPF_TYPE_ID_LOCAL */
>>  /* { dg-final { scan-assembler-times "0x7\[\t \]+\[^\n\]*bpfcr_kind" 7 } } 
>> BPF_TYPE_ID_TARGET */
>> +/* { dg-final { scan-assembler-times "bpfcr_astr_off \[(\"\]+0\[(\"\]+" 20 
>> } } */


Re: [PATCH 1/3] bpf: Fix CO-RE field expression builtins

2024-03-20 Thread Cupertino Miranda


> This patch corrects bugs within the CO-RE builtin field expression
> related builtins.
> The following bugs were identified and corrected based on the expected
> results of bpf-next selftests testsuite.
> It addresses the following problems:
>  - Expressions with pointer dereferencing now point to the BTF structure
>type, instead of the structure pointer type.
>  - Pointer addition to structure root is now identified and constructed
>in CO-RE relocations as if it is an array access. For example,
>   "&(s+2)->b" generates "2:1" as an access string where "2" is
>   refering to the access for "s+2".
>
> gcc/ChangeLog:
>   * config/bpf/core-builtins.cc (core_field_info): Add
>   support for POINTER_PLUS_EXPR in the root of the field expression.
>   (bpf_core_get_index): Likewise.
>   (pack_field_expr): Make the BTF type to point to the structure
>   related node, instead of its pointer type.
>   (make_core_safe_access_index): Correct to new code.
>
> gcc/testsuite/ChangeLog:
>   * gcc.target/bpf/core-attr-5.c: Correct.
>   * gcc.target/bpf/core-attr-6.c: Likewise.
>   * gcc.target/bpf/core-attr-struct-as-array.c: Add test case for
>   pointer arithmetics as array access use case.
> ---
>  gcc/config/bpf/core-builtins.cc   | 54 +++
>  gcc/testsuite/gcc.target/bpf/core-attr-5.c|  4 +-
>  gcc/testsuite/gcc.target/bpf/core-attr-6.c|  4 +-
>  .../bpf/core-attr-struct-as-array.c   | 35 
>  4 files changed, 82 insertions(+), 15 deletions(-)
>  create mode 100644 
> gcc/testsuite/gcc.target/bpf/core-attr-struct-as-array.c
>
> diff --git a/gcc/config/bpf/core-builtins.cc 
> b/gcc/config/bpf/core-builtins.cc
> index 8d8c54c1fb3d..4256fea15e49 100644
> --- a/gcc/config/bpf/core-builtins.cc
> +++ b/gcc/config/bpf/core-builtins.cc
> @@ -388,8 +388,8 @@ core_field_info (tree src, enum btf_core_reloc_kind 
> kind)
>
>src = root_for_core_field_info (src);
>
> -  get_inner_reference (src, , , _off, , 
> ,
> -, );
> +  tree root = get_inner_reference (src, , , _off, 
> ,
> +, , );
>
>/* Note: Use DECL_BIT_FIELD_TYPE rather than DECL_BIT_FIELD here, 
> because it
>   remembers whether the field in question was originally declared as a
> @@ -414,6 +414,23 @@ core_field_info (tree src, enum btf_core_reloc_kind 
> kind)
>  {
>  case BPF_RELO_FIELD_BYTE_OFFSET:
>{
> + result = 0;
> + if (var_off == NULL_TREE
> + && TREE_CODE (root) == INDIRECT_REF
> + && TREE_CODE (TREE_OPERAND (root, 0)) == POINTER_PLUS_EXPR)
> +   {
> + tree node = TREE_OPERAND (root, 0);
> + tree offset = TREE_OPERAND (node, 1);
> + tree type = TREE_TYPE (TREE_OPERAND (node, 0));
> + type = TREE_TYPE (type);
> +
> + gcc_assert (TREE_CODE (offset) == INTEGER_CST && tree_fits_shwi_p 
> (offset)
> + && COMPLETE_TYPE_P (type) && tree_fits_shwi_p (TYPE_SIZE 
> (type)));

 What if an expression with a non-constant offset (something like s+foo)
 is passed to the builtin?  Wouldn't it be better to error there instead
 of ICEing?

>>> In that case, var_off == NULL_TREE, and it did not reach the assert.
>>> In any case, please notice that this code was copied from some different
>>> code in the same file which in that case would actually produce the
>>> error earlier.  The assert is there as a safe guard just in case the
>>> other function stops detecting this case.
>>>
>>> In core-builtins.cc:572
>>>
>>> else if (code == POINTER_PLUS_EXPR)
>>>   {
>>> tree offset = TREE_OPERAND (node, 1);
>>> tree type = TREE_TYPE (TREE_OPERAND (node, 0));
>>> type = TREE_TYPE (type);
>>>
>>> if (TREE_CODE (offset) == INTEGER_CST && tree_fits_shwi_p (offset)
>>> && COMPLETE_TYPE_P (type) && tree_fits_shwi_p (TYPE_SIZE 
>>> (type)))
>>>   {
>>> HOST_WIDE_INT offset_i = tree_to_shwi (offset);
>>> HOST_WIDE_INT type_size_i = tree_to_shwi (TYPE_SIZE_UNIT 
>>> (type));
>>> if ((offset_i % type_size_i) == 0)
>>>   return offset_i / type_size_i;
>>>   }
>>>   }
>>>
>>> if (valid != NULL)
>>>   *valid = false;
>>> return -1;
>>>   }
>>>
>>> Because the code, although similar, is actually having different
>>> purposes, I decided not to abstract this in an independent function. My
>>> perception was that it would be more confusing.
>>>
>>> Without wanting to paste too much code, please notice that the function
>>> with the assert is only called if the above function, does not return
>>> with error (i.e. valid != false).
>>
>> Ok understood.
>> Please submit upstream.
>> Thanks!
>
> Heh 

Re: [PATCH 1/3] bpf: Fix CO-RE field expression builtins

2024-03-14 Thread Cupertino Miranda


Jose E. Marchesi writes:

>> This patch corrects bugs within the CO-RE builtin field expression
>> related builtins.
>> The following bugs were identified and corrected based on the expected
>> results of bpf-next selftests testsuite.
>> It addresses the following problems:
>>  - Expressions with pointer dereferencing now point to the BTF structure
>>type, instead of the structure pointer type.
>>  - Pointer addition to structure root is now identified and constructed
>>in CO-RE relocations as if it is an array access. For example,
>>   "&(s+2)->b" generates "2:1" as an access string where "2" is
>>   refering to the access for "s+2".
>>
>> gcc/ChangeLog:
>>  * config/bpf/core-builtins.cc (core_field_info): Add
>>  support for POINTER_PLUS_EXPR in the root of the field expression.
>>  (bpf_core_get_index): Likewise.
>>  (pack_field_expr): Make the BTF type to point to the structure
>>  related node, instead of its pointer type.
>>  (make_core_safe_access_index): Correct to new code.
>>
>> gcc/testsuite/ChangeLog:
>>  * gcc.target/bpf/core-attr-5.c: Correct.
>>  * gcc.target/bpf/core-attr-6.c: Likewise.
>>  * gcc.target/bpf/core-attr-struct-as-array.c: Add test case for
>>  pointer arithmetics as array access use case.
>> ---
>>  gcc/config/bpf/core-builtins.cc   | 54 +++
>>  gcc/testsuite/gcc.target/bpf/core-attr-5.c|  4 +-
>>  gcc/testsuite/gcc.target/bpf/core-attr-6.c|  4 +-
>>  .../bpf/core-attr-struct-as-array.c   | 35 
>>  4 files changed, 82 insertions(+), 15 deletions(-)
>>  create mode 100644 gcc/testsuite/gcc.target/bpf/core-attr-struct-as-array.c
>>
>> diff --git a/gcc/config/bpf/core-builtins.cc 
>> b/gcc/config/bpf/core-builtins.cc
>> index 8d8c54c1fb3d..4256fea15e49 100644
>> --- a/gcc/config/bpf/core-builtins.cc
>> +++ b/gcc/config/bpf/core-builtins.cc
>> @@ -388,8 +388,8 @@ core_field_info (tree src, enum btf_core_reloc_kind kind)
>>
>>src = root_for_core_field_info (src);
>>
>> -  get_inner_reference (src, , , _off, , ,
>> -   , );
>> +  tree root = get_inner_reference (src, , , _off, ,
>> +   , , );
>>
>>/* Note: Use DECL_BIT_FIELD_TYPE rather than DECL_BIT_FIELD here, because 
>> it
>>   remembers whether the field in question was originally declared as a
>> @@ -414,6 +414,23 @@ core_field_info (tree src, enum btf_core_reloc_kind 
>> kind)
>>  {
>>  case BPF_RELO_FIELD_BYTE_OFFSET:
>>{
>> +result = 0;
>> +if (var_off == NULL_TREE
>> +&& TREE_CODE (root) == INDIRECT_REF
>> +&& TREE_CODE (TREE_OPERAND (root, 0)) == POINTER_PLUS_EXPR)
>> +  {
>> +tree node = TREE_OPERAND (root, 0);
>> +tree offset = TREE_OPERAND (node, 1);
>> +tree type = TREE_TYPE (TREE_OPERAND (node, 0));
>> +type = TREE_TYPE (type);
>> +
>> +gcc_assert (TREE_CODE (offset) == INTEGER_CST && tree_fits_shwi_p 
>> (offset)
>> +&& COMPLETE_TYPE_P (type) && tree_fits_shwi_p (TYPE_SIZE 
>> (type)));
>
> What if an expression with a non-constant offset (something like s+foo)
> is passed to the builtin?  Wouldn't it be better to error there instead
> of ICEing?
>
In that case, var_off == NULL_TREE, and it did not reach the assert.
In any case, please notice that this code was copied from some different
code in the same file which in that case would actually produce the
error earlier.  The assert is there as a safe guard just in case the
other function stops detecting this case.

In core-builtins.cc:572

else if (code == POINTER_PLUS_EXPR)
  {
tree offset = TREE_OPERAND (node, 1);
tree type = TREE_TYPE (TREE_OPERAND (node, 0));
type = TREE_TYPE (type);

if (TREE_CODE (offset) == INTEGER_CST && tree_fits_shwi_p (offset)
&& COMPLETE_TYPE_P (type) && tree_fits_shwi_p (TYPE_SIZE (type)))
  {
HOST_WIDE_INT offset_i = tree_to_shwi (offset);
HOST_WIDE_INT type_size_i = tree_to_shwi (TYPE_SIZE_UNIT (type));
if ((offset_i % type_size_i) == 0)
  return offset_i / type_size_i;
  }
  }

if (valid != NULL)
  *valid = false;
return -1;
  }

Because the code, although similar, is actually having different
purposes, I decided not to abstract this in an independent function. My
perception was that it would be more confusing.

Without wanting to paste too much code, please notice that the function
with the assert is only called if the above function, does not return
with error (i.e. valid != false).

>
>> +
>> +HOST_WIDE_INT offset_i = tree_to_shwi (offset);
>> +result += offset_i;
>> +  }
>> +
>>  type = unsigned_type_node;
>>  if (var_off != NULL_TREE)
>>{
>> @@ -422,9 +439,9 @@ core_field_info (tree src, enum btf_core_reloc_kind kind)
>>}
>>
>>  if (bitfieldp)
>> -  result = start_bitpos / 8;
>> +  result += 

[PATCH 3/3] bpf: Corrected index computation when present with unnamed struct fields

2024-03-13 Thread Cupertino Miranda
Any unnamed structure field if not a member of the BTF_KIND_STRUCT.
For that reason, CO-RE access strings indexes should take that in
consideration. This patch adds a condition to the incrementer that
computes the index for the field access.

gcc/ChangeLog:
* config/bpf/core-builtins.cc (bpf_core_get_index): Check if
field contains a DECL_NAME.

gcc/testsuite/ChangeLog:
* gcc.target/bpf/core-builtin-fieldinfo-offset-1.c: Add
testcase for unnamed fields.
---
 gcc/config/bpf/core-builtins.cc|  6 +-
 .../gcc.target/bpf/core-builtin-fieldinfo-offset-1.c   | 10 --
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc
index 70b14e48e6e5..8333ad81d0e0 100644
--- a/gcc/config/bpf/core-builtins.cc
+++ b/gcc/config/bpf/core-builtins.cc
@@ -553,7 +553,11 @@ bpf_core_get_index (const tree node, bool *valid)
{
  if (l == node)
return i;
- i++;
+ /* Skip unnamed padding, not represented by BTF.  */
+ if (DECL_NAME(l) != NULL_TREE
+ || TREE_CODE (TREE_TYPE (l)) == UNION_TYPE
+ || TREE_CODE (TREE_TYPE (l)) == RECORD_TYPE)
+   i++;
}
 }
   else if (code == ARRAY_REF || code == ARRAY_RANGE_REF || code == MEM_REF)
diff --git a/gcc/testsuite/gcc.target/bpf/core-builtin-fieldinfo-offset-1.c 
b/gcc/testsuite/gcc.target/bpf/core-builtin-fieldinfo-offset-1.c
index 27654205287d..8b1d8b012a2a 100644
--- a/gcc/testsuite/gcc.target/bpf/core-builtin-fieldinfo-offset-1.c
+++ b/gcc/testsuite/gcc.target/bpf/core-builtin-fieldinfo-offset-1.c
@@ -14,6 +14,9 @@ struct T {
   struct S s[2];
   char c;
   char d;
+  int a: 1;
+  int:31;
+  int f;
 };
 
 enum {
@@ -38,7 +41,9 @@ unsigned int foo (struct T *t)
   unsigned e1 = __builtin_preserve_field_info (bar()->d, FIELD_BYTE_OFFSET);
   unsigned e2 = __builtin_preserve_field_info (bar()->s[1].a4, 
FIELD_BYTE_OFFSET);
 
-  return s0a1 + s0a4 + s0x + s1a1 + s1a4 + s1x + c + d + e1 + e2;
+  unsigned f1 = __builtin_preserve_field_info (t->f, FIELD_BYTE_OFFSET);
+
+  return s0a1 + s0a4 + s0x + s1a1 + s1a4 + s1x + c + d + e1 + e2 + f1;
 }
 
 /* { dg-final { scan-assembler-times "\[\t \]mov\[\t \]%r\[0-9\],4" 2 } } */
@@ -65,5 +70,6 @@ unsigned int foo (struct T *t)
 /* { dg-final { scan-assembler-times "bpfcr_astr_off \\(\"0:1:1:4\"\\)" 1 } } 
*/
 /* { dg-final { scan-assembler-times "bpfcr_astr_off \\(\"0:2\"\\)" 1 } } */
 /* { dg-final { scan-assembler-times "bpfcr_astr_off \\(\"0:3\"\\)" 2 } } */
+/* { dg-final { scan-assembler-times "bpfcr_astr_off \\(\"0:5\"\\)" 1 } } */
 
-/* { dg-final { scan-assembler-times "0\[\t \]+\[^\n\]*bpfcr_kind" 10 } } */
+/* { dg-final { scan-assembler-times "0\[\t \]+\[^\n\]*bpfcr_kind" 11 } } */
-- 
2.30.2



[PATCH 2/3] bpf: Fix access string default for CO-RE type based relocations

2024-03-13 Thread Cupertino Miranda
Although part of all CO-RE relocation data, type based relocations do
not require an access string.
Initial implementation defined it as an empty string.
On the other hand, libbpf when parsing the CO-RE relocations verifies
that those strings would contain "0", otherwise reports an error.
This patch makes GCC compliant with libbpf expectations.

gcc/Changelog:
* config/bpf/btfext-out.cc (cpf_core_reloc_add): Correct for new code.
Add assert to validate the string is set.
* config/bpf/core-builtins.cc (cr_final): Make string struct
field as const.
(process_enum_value): Correct for field type change.
(process_type): Set access string to "0".

gcc/testsuite/ChangeLog:
* gcc.target/bpf/core-builtin-type-based.c: Correct.
* gcc.target/bpf/core-builtin-type-id.c: Correct.
---
 gcc/config/bpf/btfext-out.cc   |  5 +++--
 gcc/config/bpf/core-builtins.cc| 10 ++
 gcc/testsuite/gcc.target/bpf/core-builtin-type-based.c |  1 +
 gcc/testsuite/gcc.target/bpf/core-builtin-type-id.c|  1 +
 4 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/gcc/config/bpf/btfext-out.cc b/gcc/config/bpf/btfext-out.cc
index 57c0dc323812..ff1fd0739f1e 100644
--- a/gcc/config/bpf/btfext-out.cc
+++ b/gcc/config/bpf/btfext-out.cc
@@ -299,8 +299,9 @@ bpf_core_reloc_add (const tree type, const char * 
section_name,
 
   /* Buffer the access string in the auxiliary strtab.  */
   bpfcr->bpfcr_astr_off = 0;
-  if (accessor != NULL)
-bpfcr->bpfcr_astr_off = btf_ext_add_string (accessor);
+  gcc_assert (accessor != NULL);
+  bpfcr->bpfcr_astr_off = btf_ext_add_string (accessor);
+
   bpfcr->bpfcr_type = get_btf_id (ctf_lookup_tree_type (ctfc, type));
   bpfcr->bpfcr_insn_label = label;
   bpfcr->bpfcr_kind = kind;
diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc
index 4256fea15e49..70b14e48e6e5 100644
--- a/gcc/config/bpf/core-builtins.cc
+++ b/gcc/config/bpf/core-builtins.cc
@@ -205,7 +205,7 @@ struct cr_local
 /* Core Relocation Final data */
 struct cr_final
 {
-  char *str;
+  const char *str;
   tree type;
   enum btf_core_reloc_kind kind;
 };
@@ -868,8 +868,10 @@ process_enum_value (struct cr_builtins *data)
{
  if (TREE_VALUE (l) == expr)
{
- ret.str = (char *) ggc_alloc_atomic ((index / 10) + 1);
- sprintf (ret.str, "%d", index);
+ char *tmp = (char *) ggc_alloc_atomic ((index / 10) + 1);
+ sprintf (tmp, "%d", index);
+ ret.str = (const char *) tmp;
+
  break;
}
  index++;
@@ -987,7 +989,7 @@ process_type (struct cr_builtins *data)
  || data->kind == BPF_RELO_TYPE_MATCHES);
 
   struct cr_final ret;
-  ret.str = NULL;
+  ret.str = ggc_strdup ("0");
   ret.type = data->type;
   ret.kind = data->kind;
 
diff --git a/gcc/testsuite/gcc.target/bpf/core-builtin-type-based.c 
b/gcc/testsuite/gcc.target/bpf/core-builtin-type-based.c
index 74a8d5a14d9d..9d818133c084 100644
--- a/gcc/testsuite/gcc.target/bpf/core-builtin-type-based.c
+++ b/gcc/testsuite/gcc.target/bpf/core-builtin-type-based.c
@@ -56,3 +56,4 @@ int foo(void *data)
 /* { dg-final { scan-assembler-times "0x8\[\t \]+\[^\n\]*bpfcr_kind" 13 } } 
BPF_TYPE_EXISTS */
 /* { dg-final { scan-assembler-times "0x9\[\t \]+\[^\n\]*bpfcr_kind" 11 } } 
BPF_TYPE_SIZE */
 /* { dg-final { scan-assembler-times "0xc\[\t \]+\[^\n\]*bpfcr_kind" 13 } } 
BPF_TYPE_MATCHES */
+/* { dg-final { scan-assembler-times "bpfcr_astr_off \[(\"\]+0\[(\"\]+" 37 } } 
*/
diff --git a/gcc/testsuite/gcc.target/bpf/core-builtin-type-id.c 
b/gcc/testsuite/gcc.target/bpf/core-builtin-type-id.c
index 4b23288eac08..9576b91bc940 100644
--- a/gcc/testsuite/gcc.target/bpf/core-builtin-type-id.c
+++ b/gcc/testsuite/gcc.target/bpf/core-builtin-type-id.c
@@ -38,3 +38,4 @@ int foo(void *data)
 /* { dg-final { scan-assembler-times "0\[\t \]+\[^\n\]*bpfcr_type" 0  { xfail 
*-*-* } } } */
 /* { dg-final { scan-assembler-times "0x6\[\t \]+\[^\n\]*bpfcr_kind" 13 } } 
BPF_TYPE_ID_LOCAL */
 /* { dg-final { scan-assembler-times "0x7\[\t \]+\[^\n\]*bpfcr_kind" 7 } } 
BPF_TYPE_ID_TARGET */
+/* { dg-final { scan-assembler-times "bpfcr_astr_off \[(\"\]+0\[(\"\]+" 20 } } 
*/
-- 
2.30.2



[PATCH 1/3] bpf: Fix CO-RE field expression builtins

2024-03-13 Thread Cupertino Miranda
This patch corrects bugs within the CO-RE builtin field expression
related builtins.
The following bugs were identified and corrected based on the expected
results of bpf-next selftests testsuite.
It addresses the following problems:
 - Expressions with pointer dereferencing now point to the BTF structure
   type, instead of the structure pointer type.
 - Pointer addition to structure root is now identified and constructed
   in CO-RE relocations as if it is an array access. For example,
  "&(s+2)->b" generates "2:1" as an access string where "2" is
  refering to the access for "s+2".

gcc/ChangeLog:
* config/bpf/core-builtins.cc (core_field_info): Add
support for POINTER_PLUS_EXPR in the root of the field expression.
(bpf_core_get_index): Likewise.
(pack_field_expr): Make the BTF type to point to the structure
related node, instead of its pointer type.
(make_core_safe_access_index): Correct to new code.

gcc/testsuite/ChangeLog:
* gcc.target/bpf/core-attr-5.c: Correct.
* gcc.target/bpf/core-attr-6.c: Likewise.
* gcc.target/bpf/core-attr-struct-as-array.c: Add test case for
pointer arithmetics as array access use case.
---
 gcc/config/bpf/core-builtins.cc   | 54 +++
 gcc/testsuite/gcc.target/bpf/core-attr-5.c|  4 +-
 gcc/testsuite/gcc.target/bpf/core-attr-6.c|  4 +-
 .../bpf/core-attr-struct-as-array.c   | 35 
 4 files changed, 82 insertions(+), 15 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/bpf/core-attr-struct-as-array.c

diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc
index 8d8c54c1fb3d..4256fea15e49 100644
--- a/gcc/config/bpf/core-builtins.cc
+++ b/gcc/config/bpf/core-builtins.cc
@@ -388,8 +388,8 @@ core_field_info (tree src, enum btf_core_reloc_kind kind)
 
   src = root_for_core_field_info (src);
 
-  get_inner_reference (src, , , _off, , ,
-  , );
+  tree root = get_inner_reference (src, , , _off, ,
+  , , );
 
   /* Note: Use DECL_BIT_FIELD_TYPE rather than DECL_BIT_FIELD here, because it
  remembers whether the field in question was originally declared as a
@@ -414,6 +414,23 @@ core_field_info (tree src, enum btf_core_reloc_kind kind)
 {
 case BPF_RELO_FIELD_BYTE_OFFSET:
   {
+   result = 0;
+   if (var_off == NULL_TREE
+   && TREE_CODE (root) == INDIRECT_REF
+   && TREE_CODE (TREE_OPERAND (root, 0)) == POINTER_PLUS_EXPR)
+ {
+   tree node = TREE_OPERAND (root, 0);
+   tree offset = TREE_OPERAND (node, 1);
+   tree type = TREE_TYPE (TREE_OPERAND (node, 0));
+   type = TREE_TYPE (type);
+
+   gcc_assert (TREE_CODE (offset) == INTEGER_CST && tree_fits_shwi_p 
(offset)
+   && COMPLETE_TYPE_P (type) && tree_fits_shwi_p (TYPE_SIZE 
(type)));
+
+   HOST_WIDE_INT offset_i = tree_to_shwi (offset);
+   result += offset_i;
+ }
+
type = unsigned_type_node;
if (var_off != NULL_TREE)
  {
@@ -422,9 +439,9 @@ core_field_info (tree src, enum btf_core_reloc_kind kind)
  }
 
if (bitfieldp)
- result = start_bitpos / 8;
+ result += start_bitpos / 8;
else
- result = bitpos / 8;
+ result += bitpos / 8;
   }
   break;
 
@@ -552,6 +569,7 @@ bpf_core_get_index (const tree node, bool *valid)
 {
   tree offset = TREE_OPERAND (node, 1);
   tree type = TREE_TYPE (TREE_OPERAND (node, 0));
+  type = TREE_TYPE (type);
 
   if (TREE_CODE (offset) == INTEGER_CST && tree_fits_shwi_p (offset)
  && COMPLETE_TYPE_P (type) && tree_fits_shwi_p (TYPE_SIZE (type)))
@@ -627,14 +645,18 @@ compute_field_expr (tree node, unsigned int *accessors,
 
   switch (TREE_CODE (node))
 {
-case ADDR_EXPR:
-  return 0;
 case INDIRECT_REF:
-  accessors[0] = 0;
-  return 1;
-case POINTER_PLUS_EXPR:
-  accessors[0] = bpf_core_get_index (node, valid);
-  return 1;
+  if (TREE_CODE (node = TREE_OPERAND (node, 0)) == POINTER_PLUS_EXPR)
+   {
+ accessors[0] = bpf_core_get_index (node, valid);
+ *access_node = TREE_OPERAND (node, 0);
+ return 1;
+   }
+  else
+   {
+ accessors[0] = 0;
+ return 1;
+   }
 case COMPONENT_REF:
   n = compute_field_expr (TREE_OPERAND (node, 0), accessors,
  valid,
@@ -660,6 +682,7 @@ compute_field_expr (tree node, unsigned int *accessors,
  access_node, false);
   return n;
 
+case ADDR_EXPR:
 case CALL_EXPR:
 case SSA_NAME:
 case VAR_DECL:
@@ -688,6 +711,9 @@ pack_field_expr (tree *args,
   tree access_node = NULL_TREE;
   tree type = NULL_TREE;
 
+  if (TREE_CODE (root) == ADDR_EXPR)
+root = TREE_OPERAND (root, 0);
+
   ret.reloc_decision = REPLACE_CREATE_RELOCATION;
 
   unsigned 

Re: [PATCH v2 5/5] bpf: renamed coreout.* files to btfext-out.*.

2024-02-28 Thread Cupertino Miranda


Corrected and Pushed.

Thanks,
Cupertino

David Faust writes:

> On 2/27/24 11:04, Cupertino Miranda wrote:
>> gcc/ChangeLog:
>>
>>  * config.gcc (target_gtfiles): Changes coreout to btfext-out.
>>  (extra_objs): Changes coreout to btfext-out.
>>  * config/bpf/coreout.cc: Renamed to btfext-out.cc.
>>  * config/bpf/btfext-out.cc: Added.
>>  * config/bpf/coreout.h: Renamed to btfext-out.h.
>>  * config/bpf/btfext-out.h: Added.
>>  * config/bpf/core-builtins.cc: Changes include.
>>  * config/bpf/core-builtins.h: Changes include.
>>  * config/bpf/t-bpf: Renamed file.
> This last entry is confusing, sounds like t-bpf is renamed, which it
> isn't. I'd suggest to just say "accomodate renamed files" or so.
>
> Similar to prior patches, there is a mix of present and past tenses here.
> Please stick with the present.
>
> Changes -> Change
> Added -> Add  (or just "New.")
> Renamed -> Rename.
>
> OK with those changes.
> Thanks.
>
>> ---
>>  gcc/config.gcc   | 4 ++--
>>  gcc/config/bpf/{coreout.cc => btfext-out.cc} | 4 ++--
>>  gcc/config/bpf/{coreout.h => btfext-out.h}   | 2 +-
>>  gcc/config/bpf/core-builtins.cc  | 2 +-
>>  gcc/config/bpf/core-builtins.h   | 2 +-
>>  gcc/config/bpf/t-bpf | 4 ++--
>>  6 files changed, 9 insertions(+), 9 deletions(-)
>>  rename gcc/config/bpf/{coreout.cc => btfext-out.cc} (99%)
>>  rename gcc/config/bpf/{coreout.h => btfext-out.h} (98%)
>>
>> diff --git a/gcc/config.gcc b/gcc/config.gcc
>> index a0f9c6723083..1ca033d75b66 100644
>> --- a/gcc/config.gcc
>> +++ b/gcc/config.gcc
>> @@ -1653,8 +1653,8 @@ bpf-*-*)
>>  tmake_file="${tmake_file} bpf/t-bpf"
>>  use_collect2=no
>>  use_gcc_stdint=provide
>> -extra_objs="coreout.o core-builtins.o"
>> -target_gtfiles="$target_gtfiles \$(srcdir)/config/bpf/coreout.cc 
>> \$(srcdir)/config/bpf/core-builtins.cc"
>> +extra_objs="btfext-out.o core-builtins.o"
>> +target_gtfiles="$target_gtfiles \$(srcdir)/config/bpf/btfext-out.cc 
>> \$(srcdir)/config/bpf/core-builtins.cc"
>>  ;;
>>  cris-*-elf | cris-*-none)
>>  tm_file="elfos.h newlib-stdint.h ${tm_file}"
>> diff --git a/gcc/config/bpf/coreout.cc b/gcc/config/bpf/btfext-out.cc
>> similarity index 99%
>> rename from gcc/config/bpf/coreout.cc
>> rename to gcc/config/bpf/btfext-out.cc
>> index 31b2abc3151b..4281cca83e13 100644
>> --- a/gcc/config/bpf/coreout.cc
>> +++ b/gcc/config/bpf/btfext-out.cc
>> @@ -33,7 +33,7 @@
>>  #include "tree-pretty-print.h"
>>  #include "cgraph.h"
>>
>> -#include "coreout.h"
>> +#include "btfext-out.h"
>>
>>  /* This file contains data structures and routines for construction and 
>> output
>> of BPF Compile Once - Run Everywhere (BPF CO-RE) information.
>> @@ -618,4 +618,4 @@ btf_ext_output (void)
>>dw2_asm_output_data (4, 0, "Required padding by libbpf structs");
>>  }
>>
>> -#include "gt-coreout.h"
>> +#include "gt-btfext-out.h"
>> diff --git a/gcc/config/bpf/coreout.h b/gcc/config/bpf/btfext-out.h
>> similarity index 98%
>> rename from gcc/config/bpf/coreout.h
>> rename to gcc/config/bpf/btfext-out.h
>> index 1c26b9274739..b36309475c97 100644
>> --- a/gcc/config/bpf/coreout.h
>> +++ b/gcc/config/bpf/btfext-out.h
>> @@ -1,4 +1,4 @@
>> -/* coreout.h - Declarations and definitions related to
>> +/* btfext-out.h - Declarations and definitions related to
>> BPF Compile Once - Run Everywhere (CO-RE) support.
>> Copyright (C) 2021-2024 Free Software Foundation, Inc.
>>
>> diff --git a/gcc/config/bpf/core-builtins.cc 
>> b/gcc/config/bpf/core-builtins.cc
>> index aa75fd68cae6..8d8c54c1fb3d 100644
>> --- a/gcc/config/bpf/core-builtins.cc
>> +++ b/gcc/config/bpf/core-builtins.cc
>> @@ -45,7 +45,7 @@ along with GCC; see the file COPYING3.  If not see
>>
>>  #include "ctfc.h"
>>  #include "btf.h"
>> -#include "coreout.h"
>> +#include "btfext-out.h"
>>  #include "core-builtins.h"
>>
>>  /* BPF CO-RE builtins definition.
>> diff --git a/gcc/config/bpf/core-builtins.h b/gcc/config/bpf/core-builtins.h
>> index c54f6ddac812..e56b55b94e0c 100644
>> --- a/gcc/config/bpf/core-builtins.h
>> +++ b/gcc/config/bpf/core-builtins.h
>> @@ -1,7 +1,7 @@
>>  #ifndef BPF_CORE_BUILTINS_H
>>  #define BPF_CORE_BUILTINS_H
>>
>> -#include "coreout.h"
>> +#include "btfext-out.h"
>>
>>  enum bpf_builtins
>>  {
>> diff --git a/gcc/config/bpf/t-bpf b/gcc/config/bpf/t-bpf
>> index 18f1fa67794d..dc50332350c4 100644
>> --- a/gcc/config/bpf/t-bpf
>> +++ b/gcc/config/bpf/t-bpf
>> @@ -1,7 +1,7 @@
>>
>> -TM_H += $(srcdir)/config/bpf/coreout.h $(srcdir)/config/bpf/core-builtins.h
>> +TM_H += $(srcdir)/config/bpf/btfext-out.h 
>> $(srcdir)/config/bpf/core-builtins.h
>>
>> -coreout.o: $(srcdir)/config/bpf/coreout.cc
>> +btfext-out.o: $(srcdir)/config/bpf/btfext-out.cc
>>  $(COMPILE) $<
>>  $(POSTCOMPILE)
>>


Re: [PATCH v2 4/5] bpf: implementation of func_info in .BTF.ext.

2024-02-28 Thread Cupertino Miranda


Corrected and Pushed, with the following little change to resolve a
warning I missed before, the patch introduced was:


diff --git a/gcc/config/bpf/btfext-out.cc b/gcc/config/bpf/btfext-out.cc
index 6ebbb54ef73e..00d2501a976b 100644
--- a/gcc/config/bpf/btfext-out.cc
+++ b/gcc/config/bpf/btfext-out.cc
@@ -200,7 +200,7 @@ btfext_info_sec_find_or_add (const char *sec_name, bool add)
   return ret;
 }

-#define SEARCH_NODE_AND_RETURN(TYPE, FIELD, CONDITION) ({ \
+#define SEARCH_NODE_AND_RETURN(TYPE, FIELD, CONDITION) __extension__ ({ \
   TYPE **head = &(FIELD); \
   while (*head != NULL) \
 { \

Thanks,
Cupertino


David Faust writes:

> Hi Cupertino,
>
> On 2/27/24 11:04, Cupertino Miranda wrote:
>> Kernel verifier complains in some particular cases for missing func_info
>> implementation in .BTF.ext. This patch implements it.
>>
>> Strings are cached locally in coreout.cc to avoid adding duplicated
>> strings in the string list. This string deduplication should eventually
>> be moved to the CTFC functions such that this happens widely.
>>
>> With this implementation, the CO-RE relocations information was also
>> simplified and integrated with the FuncInfo structures.
>>
>
> I have just a couple small comments inline in the patch below, but they
> are very minor and only suggestions/nits.
>
> The ChangeLog has the same past/present tense issue as the other patches
> in the series, but apart from that I see no issues. Great work! Thanks
> for implementing this.
>
> Patch is OK with the ChangeLog fixed up, and the inline nits - if
> you agree.
> Thanks!
>
>> gcc/Changelog:
>>
>>  PR target/113453
>>  * config/bpf/bpf.cc (bpf_function_prologue): Defined target
>>  hook.
>>  * config/bpf/coreout.cc (brf_ext_info_section)
>>  (btf_ext_info): Moved from coreout.h
>>  (btf_ext_funcinfo, btf_ext_lineinfo): Added struct.
>>  (bpf_core_reloc): Renamed to btf_ext_core_reloc.
>>  (btf_ext): Added static variable.
>>  (btfext_info_sec_find_or_add, SEARCH_NODE_AND_RETURN)
>>  (bpf_create_or_find_funcinfo, bpt_create_core_reloc)
>>  (btf_ext_add_string, btf_funcinfo_type_callback)
>>  (btf_add_func_info_for, btf_validate_funcinfo)
>>  (btf_ext_info_len, output_btfext_func_info): Added function.
>>  (output_btfext_header, bpf_core_reloc_add)
>>  (output_btfext_core_relocs, btf_ext_init, btf_ext_output):
>>  Changed to support new structs.
>>  * config/bpf/coreout.h (btf_ext_funcinfo, btf_ext_lineinfo):
>>  Moved and changed in coreout.cc.
>>  (btf_add_func_info_for, btf_ext_add_string): Added prototypes.
>>
>> gcc/testsuite/ChangeLog:
>>  PR target/113453
>>  * gcc.target/bpf/btfext-funcinfo-nocore.c: Added.
>>  * gcc.target/bpf/btfext-funcinfo.c: Added.
>>  * gcc.target/bpf/core-attr-5.c: Fixed regexp.
>>  * gcc.target/bpf/core-attr-6.c: Fixed regexp.
>>  * gcc.target/bpf/core-builtin-fieldinfo-offset-1.c: Fixed regexp.
>>  * gcc.target/bpf/core-section-1.c: Fixed regexp
>> ---
>>  gcc/config/bpf/bpf.cc |  12 +
>>  gcc/config/bpf/coreout.cc | 518 +-
>>  gcc/config/bpf/coreout.h  |  20 +-
>>  .../gcc.target/bpf/btfext-funcinfo-nocore.c   |  42 ++
>>  .../gcc.target/bpf/btfext-funcinfo.c  |  46 ++
>>  gcc/testsuite/gcc.target/bpf/core-attr-5.c|   9 +-
>>  gcc/testsuite/gcc.target/bpf/core-attr-6.c|   6 +-
>>  .../bpf/core-builtin-fieldinfo-offset-1.c |  13 +-
>>  gcc/testsuite/gcc.target/bpf/core-section-1.c |   2 +-
>>  9 files changed, 506 insertions(+), 162 deletions(-)
>>  create mode 100644 gcc/testsuite/gcc.target/bpf/btfext-funcinfo-nocore.c
>>  create mode 100644 gcc/testsuite/gcc.target/bpf/btfext-funcinfo.c
>>
>> diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
>> index 4318b26b9cda..ea47e3a8dbfb 100644
>> --- a/gcc/config/bpf/bpf.cc
>> +++ b/gcc/config/bpf/bpf.cc
>> @@ -385,6 +385,18 @@ bpf_compute_frame_layout (void)
>>  #undef TARGET_COMPUTE_FRAME_LAYOUT
>>  #define TARGET_COMPUTE_FRAME_LAYOUT bpf_compute_frame_layout
>>
>> +/* Defined to initialize data for func_info region in .BTF.ext section.  */
>> +
>> +static void
>> +bpf_function_prologue (FILE *f ATTRIBUTE_UNUSED)
>> +{
>> +  if (btf_debuginfo_p ())
>> +btf_add_func_info_for (cfun->decl, current_function_func_begin_label);
>> +}
>> +
>> +#undef TARGET_ASM_FUNCTION_PROLOGUE
>> +#define TARGET_ASM_FUNCTION_PROLOGUE bpf_function_prologue
>> 

Re: [PATCH v2 3/5] bpf: Always emit .BTF.ext section if generating BTF

2024-02-28 Thread Cupertino Miranda


Corrected and Pushed.

Thanks,
Cupertino

David Faust writes:

> On 2/27/24 11:04, Cupertino Miranda wrote:
>> BPF applications, when generating BTF information should always create a
>> .BTF.ext section.
>> Current implementation was only creating it when -mco-re option was used.
>> This patch makes .BTF.ext always be generated for BPF target objects.
>> The patch also adds conditions around btf_finalize function call
>> such that BTF deallocation happens later for BPF target.
>> For BPF, btf_finalize is only called after .BTF.ext is generated.
>
> Thank you, this version makes it much more clear what the patch does.
>
>>
>> gcc/ChangeLog:
>>
>>  * config/bpf/bpf.cc (bpf_option_override): Make .BTF.ext
>>  enabled by default for BPF.
>>  (bpf_file_end): Call BTF deallocation.
>>  * dwarf2ctf.cc (ctf_debug_finalize): Conditionally execute BTF
>>  deallocation.
>
> You are missing ChangeLog entries for bpf_asm_init_sections and
> ctf_debug_finish.
>
> The script contrib/gcc-changelog/git_check_commit.py may help
> to catch those.
>
> The code changes LGTM, so OK with the ChangeLog fixed.
> Thanks.
>
>> ---
>>  gcc/config/bpf/bpf.cc | 20 +---
>>  gcc/dwarf2ctf.cc  | 12 ++--
>>  2 files changed, 15 insertions(+), 17 deletions(-)
>>
>> diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
>> index d6ca47eeecbe..4318b26b9cda 100644
>> --- a/gcc/config/bpf/bpf.cc
>> +++ b/gcc/config/bpf/bpf.cc
>> @@ -195,10 +195,8 @@ bpf_option_override (void)
>>if (TARGET_BPF_CORE && !btf_debuginfo_p ())
>>  error ("BPF CO-RE requires BTF debugging information, use %<-gbtf%>");
>>
>> -  /* To support the portability needs of BPF CO-RE approach, BTF debug
>> - information includes the BPF CO-RE relocations.  */
>> -  if (TARGET_BPF_CORE)
>> -write_symbols |= BTF_WITH_CORE_DEBUG;
>> +  /* BPF applications always generate .BTF.ext.  */
>> +  write_symbols |= BTF_WITH_CORE_DEBUG;
>>
>>/* Unlike much of the other BTF debug information, the information 
>> necessary
>>   for CO-RE relocations is added to the CTF container by the BPF backend.
>> @@ -218,10 +216,7 @@ bpf_option_override (void)
>>/* -gbtf implies -mcore when using the BPF backend, unless -mno-co-re
>>   is specified.  */
>>if (btf_debuginfo_p () && !(target_flags_explicit & MASK_BPF_CORE))
>> -{
>> -  target_flags |= MASK_BPF_CORE;
>> -  write_symbols |= BTF_WITH_CORE_DEBUG;
>> -}
>> +target_flags |= MASK_BPF_CORE;
>>
>>/* Determine available features from ISA setting (-mcpu=).  */
>>if (bpf_has_jmpext == -1)
>> @@ -267,7 +262,7 @@ bpf_option_override (void)
>>  static void
>>  bpf_asm_init_sections (void)
>>  {
>> -  if (TARGET_BPF_CORE)
>> +  if (btf_debuginfo_p () && btf_with_core_debuginfo_p ())
>>  btf_ext_init ();
>>  }
>>
>> @@ -279,8 +274,11 @@ bpf_asm_init_sections (void)
>>  static void
>>  bpf_file_end (void)
>>  {
>> -  if (TARGET_BPF_CORE)
>> -btf_ext_output ();
>> +  if (btf_debuginfo_p () && btf_with_core_debuginfo_p ())
>> +{
>> +  btf_ext_output ();
>> +  btf_finalize ();
>> +}
>>  }
>>
>>  #undef TARGET_ASM_FILE_END
>> diff --git a/gcc/dwarf2ctf.cc b/gcc/dwarf2ctf.cc
>> index 93e5619933fa..dca86edfffa9 100644
>> --- a/gcc/dwarf2ctf.cc
>> +++ b/gcc/dwarf2ctf.cc
>> @@ -944,7 +944,10 @@ ctf_debug_finalize (const char *filename, bool btf)
>>if (btf)
>>  {
>>btf_output (filename);
>> -  btf_finalize ();
>> +  /* btf_finalize when compiling BPF applciations gets deallocated by 
>> the
>> + BPF target in bpf_file_end.  */
>> +  if (btf_debuginfo_p () && !btf_with_core_debuginfo_p ())
>> +btf_finalize ();
>>  }
>>
>>else
>> @@ -1027,11 +1030,8 @@ ctf_debug_finish (const char * filename)
>>/* Emit BTF debug info here when CO-RE relocations need to be generated.
>>   BTF with CO-RE relocations needs to be generated when CO-RE is in 
>> effect
>>   for the BPF target.  */
>> -  if (btf_with_core_debuginfo_p ())
>> -{
>> -  gcc_assert (btf_debuginfo_p ());
>> -  ctf_debug_finalize (filename, btf_debuginfo_p ());
>> -}
>> +  if (btf_debuginfo_p () && btf_with_core_debuginfo_p ())
>> +ctf_debug_finalize (filename, btf_debuginfo_p ());
>>  }
>>
>>  #include "gt-dwarf2ctf.h"


Re: [PATCH v2 2/5] btf: added KIND_FUNC traversal function.

2024-02-28 Thread Cupertino Miranda


Corrected and Pushed.

Thanks,
Cupertino

David Faust writes:

> Hi Cupertino,
>
> Similar to patch 1, please use present tense to match the style of
> existing commits, in commit message and in ChangeLog.
>
> On 2/27/24 11:04, Cupertino Miranda wrote:
>> Added a traversal function to traverse all BTF_KIND_FUNC nodes with a
>> callback function. Used for .BTF.ext section content creation.
>
> Added -> Add
>
>>
>> gcc/ChangeLog:
>>
>>  * btfout.cc (output_btf_func_types): Use FOR_EACH_VEC_ELT.
>>  (traverse_btf_func_types): Defined function.
>>  * ctfc.h (funcs_traverse_callback): Typedef for function
>>  prototype.
>>  (traverse_btf_func_types): Added prototype.
>
> Mix of present and past tenses here, please stick to the present:
> Defined -> Define
> Added -> Add
>
> The code changes LGTM, so OK with those nits fixed.
> Thanks.
>
>> ---
>>  gcc/btfout.cc | 22 --
>>  gcc/ctfc.h|  3 +++
>>  2 files changed, 23 insertions(+), 2 deletions(-)
>>
>> diff --git a/gcc/btfout.cc b/gcc/btfout.cc
>> index 7e114e224449..7aabd99f3e7c 100644
>> --- a/gcc/btfout.cc
>> +++ b/gcc/btfout.cc
>> @@ -1276,8 +1276,10 @@ output_btf_types (ctf_container_ref ctfc)
>>  static void
>>  output_btf_func_types (ctf_container_ref ctfc)
>>  {
>> -  for (size_t i = 0; i < vec_safe_length (funcs); i++)
>> -btf_asm_func_type (ctfc, (*funcs)[i], i);
>> +  ctf_dtdef_ref ref;
>> +  unsigned i;
>> +  FOR_EACH_VEC_ELT (*funcs, i, ref)
>> +btf_asm_func_type (ctfc, ref, i);
>>  }
>>
>>  /* Output all BTF_KIND_DATASEC records.  */
>> @@ -1452,4 +1454,20 @@ btf_finalize (void)
>>tu_ctfc = NULL;
>>  }
>>
>> +/* Traversal function for all BTF_KIND_FUNC type records.  */
>> +
>> +bool
>> +traverse_btf_func_types (funcs_traverse_callback callback, void *data)
>> +{
>> +  ctf_dtdef_ref ref;
>> +  unsigned i;
>> +  FOR_EACH_VEC_ELT (*funcs, i, ref)
>> +{
>> +  bool stop = callback (ref, data);
>> +  if (stop == true)
>> +return true;
>> +}
>> +  return false;
>> +}
>> +
>>  #include "gt-btfout.h"
>> diff --git a/gcc/ctfc.h b/gcc/ctfc.h
>> index 7aac57edac55..fa188bf2f5a4 100644
>> --- a/gcc/ctfc.h
>> +++ b/gcc/ctfc.h
>> @@ -441,6 +441,9 @@ extern int ctf_add_variable (ctf_container_ref, const 
>> char *, ctf_id_t,
>>  extern ctf_id_t ctf_lookup_tree_type (ctf_container_ref, const tree);
>>  extern ctf_id_t get_btf_id (ctf_id_t);
>>
>> +typedef bool (*funcs_traverse_callback) (ctf_dtdef_ref, void *);
>> +bool traverse_btf_func_types (funcs_traverse_callback, void *);
>> +
>>  /* CTF section does not emit location information; at this time, location
>> information is needed for BTF CO-RE use-cases.  */
>>


Re: [PATCH v2 1/5] btf: fixed type id in BTF_KIND_FUNC struct data.

2024-02-28 Thread Cupertino Miranda


Corrected and Pushed.

Thanks,
Cupertino

David Faust writes:

> Hi Cupertino,
>
> Just some nits below. Apologies for incoming pedantry.
>
> On 2/27/24 11:04, Cupertino Miranda wrote:
>> This patch correct the aditition of +1 on the type id, which originally
>> was done in the wrong location and leaded to func_sts->dtd_type for
>> BTF_KIND_FUNCS struct data to contain the type id of the previous entry.
>
> Multiple typos here:
>   correct -> corrects
>   aditition -> addition
>   ...leaded to.. -> ..led to..
>   func_sts -> func_dtd
>   BTF_KIND_FUNCS -> BTF_KIND_FUNC
>
>>
>> gcc/ChangeLog:
>>
>>  * btfout.cc (btf_collect_dataset): Corrected BTF type id.
>
> Please use present tense in the ChangeLog entries, to match GNU style
> guidelines and existing entries,
> i.e. "Correct..." instead of "Corrected..."
>
> The same goes for the commit header, please use present tense to match
> the style of existing commits,
> i.e. "btf: fix type id..." instead of "fixed".
>
> The patch itself LGTM, so OK with above changes.
> Thanks!
>
>> ---
>>  gcc/btfout.cc | 6 +++---
>>  1 file changed, 3 insertions(+), 3 deletions(-)
>>
>> diff --git a/gcc/btfout.cc b/gcc/btfout.cc
>> index dcf751f8fe0d..7e114e224449 100644
>> --- a/gcc/btfout.cc
>> +++ b/gcc/btfout.cc
>> @@ -457,7 +457,8 @@ btf_collect_datasec (ctf_container_ref ctfc)
>>func_dtd->dtd_data.ctti_type = dtd->dtd_type;
>>func_dtd->linkage = dtd->linkage;
>>func_dtd->dtd_name = dtd->dtd_name;
>> -  func_dtd->dtd_type = num_types_added + num_types_created;
>> +  /* +1 for the sentinel type not in the types map.  */
>> +  func_dtd->dtd_type = num_types_added + num_types_created + 1;
>>
>>/* Only the BTF_KIND_FUNC type actually references the name. The
>>   BTF_KIND_FUNC_PROTO is always anonymous.  */
>> @@ -480,8 +481,7 @@ btf_collect_datasec (ctf_container_ref ctfc)
>>
>>struct btf_var_secinfo info;
>>
>> -  /* +1 for the sentinel type not in the types map.  */
>> -  info.type = func_dtd->dtd_type + 1;
>> +  info.type = func_dtd->dtd_type;
>>
>>/* Both zero at compile time.  */
>>info.size = 0;


[PATCH] ctf: Fix multi-dimentional array types ordering in CTF

2024-02-28 Thread Cupertino Miranda
Hi everyone,

In order to facilitate reviewing, I include a copy of the function in
this email, since the code structure changes are too hard to analyse
in the patch itself.

Looking forward to your comments.

Regards,
Cupertino

=== Function changes ===

/* Generate CTF for an ARRAY_TYPE.
   C argument is used as the iterator for the recursive calls to
   gen_ctf_array_type. C is the current die within the recursion.
   When C is NULL, it means it is the first call to gen_ctf_array_type.
   C should always be NULL when called from other functions.  */

static ctf_id_t
gen_ctf_array_type (ctf_container_ref ctfc,
dw_die_ref array_type,
dw_die_ref c = NULL)
{
  int vector_type_p = get_AT_flag (array_type, DW_AT_GNU_vector);
  if (vector_type_p)
return CTF_NULL_TYPEID;

  if (c == dw_get_die_child (array_type))
{
  dw_die_ref array_elems_type = ctf_get_AT_type (array_type);
  return gen_ctf_type (ctfc, array_elems_type);
}
  else
{
  ctf_arinfo_t arinfo;
  ctf_id_t array_node_type_id = CTF_NULL_TYPEID;
  if (c == NULL)
c = dw_get_die_child (array_type);

  c = dw_get_die_sib (c);

  ctf_id_t child_id = gen_ctf_array_type (ctfc, array_type, c);

  dw_die_ref array_index_type;
  uint32_t array_num_elements;

  if (dw_get_die_tag (c) == DW_TAG_subrange_type)
{
  dw_attr_node *upper_bound_at;

  array_index_type = ctf_get_AT_type (c);

  /* When DW_AT_upper_bound is used to specify the size of an
 array in DWARF, it is usually an unsigned constant
 specifying the upper bound index of the array.  However,
 for unsized arrays, such as foo[] or bar[0],
 DW_AT_upper_bound is a signed integer constant
 instead.  */

  upper_bound_at = get_AT (c, DW_AT_upper_bound);
  if (upper_bound_at
  && AT_class (upper_bound_at) == dw_val_class_unsigned_const)
/* This is the upper bound index.  */
array_num_elements = get_AT_unsigned (c, DW_AT_upper_bound) + 1;
  else if (get_AT (c, DW_AT_count))
array_num_elements = get_AT_unsigned (c, DW_AT_count);
  else
{
  /* This is a VLA of some kind.  */
  array_num_elements = 0;
}
}
  else if (dw_get_die_tag (c) == DW_TAG_enumeration_type)
{
  array_index_type = 0;
  array_num_elements = 0;
  /* XXX writeme. */
  gcc_assert (1);
}
  else
gcc_unreachable ();

  /* Ok, mount and register the array type.  Note how the array
 type we register here is the type of the elements in
 subsequent "dimensions", if there are any.  */

  arinfo.ctr_nelems = array_num_elements;
  if (array_index_type)
arinfo.ctr_index = gen_ctf_type (ctfc, array_index_type);
  else
arinfo.ctr_index = gen_ctf_type (ctfc, ctf_array_index_die);

  arinfo.ctr_contents = child_id;
  if (!ctf_type_exists (ctfc, c, _node_type_id))
array_node_type_id = ctf_add_array (ctfc, CTF_ADD_ROOT, ,
c);
  return array_node_type_id;
}
}

=== The patch ===

Multi-dimentional array types would be linked in reverse order to the
expected C standard ordering. In other words, CTF would define the type
of "char [a][b][c]" as if it was an array of "char [c][b][a]"
dimentions.

gcc/ChangeLog:

* dwarf2ctf.cc (gen_ctf_array_type): Correct order in which CTF
multi-dimentional array types are linked.

gcc/testsuite/ChangeLog:
* gcc.dg/debug/ctf/ctf-array-6.c: Add test.
---
 gcc/dwarf2ctf.cc | 67 
 gcc/testsuite/gcc.dg/debug/ctf/ctf-array-6.c | 14 
 2 files changed, 41 insertions(+), 40 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/debug/ctf/ctf-array-6.c

diff --git a/gcc/dwarf2ctf.cc b/gcc/dwarf2ctf.cc
index 93e5619933fa..95ceca196217 100644
--- a/gcc/dwarf2ctf.cc
+++ b/gcc/dwarf2ctf.cc
@@ -349,42 +349,40 @@ gen_ctf_pointer_type (ctf_container_ref ctfc, dw_die_ref 
ptr_type)
   return ptr_type_id;
 }
 
-/* Generate CTF for an array type.  */
+/* Generate CTF for an ARRAY_TYPE.
+   C argument is used as the iterator for the recursive calls to
+   gen_ctf_array_type. C is the current die within the recursion.
+   When C is NULL, it means it is the first call to gen_ctf_array_type.
+   C should always be NULL when called from other functions.  */
 
 static ctf_id_t
-gen_ctf_array_type (ctf_container_ref ctfc, dw_die_ref array_type)
+gen_ctf_array_type (ctf_container_ref ctfc,
+   dw_die_ref array_type,
+   dw_die_ref c = NULL)
 {
-  dw_die_ref c;
-  ctf_id_t array_elems_type_id = CTF_NULL_TYPEID;
-
   int vector_type_p = get_AT_flag (array_type, DW_AT_GNU_vector);
   if (vector_type_p)
-return array_elems_type_id;
-
-  dw_die_ref 

[PATCH v2 5/5] bpf: renamed coreout.* files to btfext-out.*.

2024-02-27 Thread Cupertino Miranda
gcc/ChangeLog:

* config.gcc (target_gtfiles): Changes coreout to btfext-out.
(extra_objs): Changes coreout to btfext-out.
* config/bpf/coreout.cc: Renamed to btfext-out.cc.
* config/bpf/btfext-out.cc: Added.
* config/bpf/coreout.h: Renamed to btfext-out.h.
* config/bpf/btfext-out.h: Added.
* config/bpf/core-builtins.cc: Changes include.
* config/bpf/core-builtins.h: Changes include.
* config/bpf/t-bpf: Renamed file.
---
 gcc/config.gcc   | 4 ++--
 gcc/config/bpf/{coreout.cc => btfext-out.cc} | 4 ++--
 gcc/config/bpf/{coreout.h => btfext-out.h}   | 2 +-
 gcc/config/bpf/core-builtins.cc  | 2 +-
 gcc/config/bpf/core-builtins.h   | 2 +-
 gcc/config/bpf/t-bpf | 4 ++--
 6 files changed, 9 insertions(+), 9 deletions(-)
 rename gcc/config/bpf/{coreout.cc => btfext-out.cc} (99%)
 rename gcc/config/bpf/{coreout.h => btfext-out.h} (98%)

diff --git a/gcc/config.gcc b/gcc/config.gcc
index a0f9c6723083..1ca033d75b66 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -1653,8 +1653,8 @@ bpf-*-*)
 tmake_file="${tmake_file} bpf/t-bpf"
 use_collect2=no
 use_gcc_stdint=provide
-extra_objs="coreout.o core-builtins.o"
-target_gtfiles="$target_gtfiles \$(srcdir)/config/bpf/coreout.cc 
\$(srcdir)/config/bpf/core-builtins.cc"
+extra_objs="btfext-out.o core-builtins.o"
+target_gtfiles="$target_gtfiles \$(srcdir)/config/bpf/btfext-out.cc 
\$(srcdir)/config/bpf/core-builtins.cc"
 ;;
 cris-*-elf | cris-*-none)
tm_file="elfos.h newlib-stdint.h ${tm_file}"
diff --git a/gcc/config/bpf/coreout.cc b/gcc/config/bpf/btfext-out.cc
similarity index 99%
rename from gcc/config/bpf/coreout.cc
rename to gcc/config/bpf/btfext-out.cc
index 31b2abc3151b..4281cca83e13 100644
--- a/gcc/config/bpf/coreout.cc
+++ b/gcc/config/bpf/btfext-out.cc
@@ -33,7 +33,7 @@
 #include "tree-pretty-print.h"
 #include "cgraph.h"
 
-#include "coreout.h"
+#include "btfext-out.h"
 
 /* This file contains data structures and routines for construction and output
of BPF Compile Once - Run Everywhere (BPF CO-RE) information.
@@ -618,4 +618,4 @@ btf_ext_output (void)
   dw2_asm_output_data (4, 0, "Required padding by libbpf structs");
 }
 
-#include "gt-coreout.h"
+#include "gt-btfext-out.h"
diff --git a/gcc/config/bpf/coreout.h b/gcc/config/bpf/btfext-out.h
similarity index 98%
rename from gcc/config/bpf/coreout.h
rename to gcc/config/bpf/btfext-out.h
index 1c26b9274739..b36309475c97 100644
--- a/gcc/config/bpf/coreout.h
+++ b/gcc/config/bpf/btfext-out.h
@@ -1,4 +1,4 @@
-/* coreout.h - Declarations and definitions related to
+/* btfext-out.h - Declarations and definitions related to
BPF Compile Once - Run Everywhere (CO-RE) support.
Copyright (C) 2021-2024 Free Software Foundation, Inc.
 
diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc
index aa75fd68cae6..8d8c54c1fb3d 100644
--- a/gcc/config/bpf/core-builtins.cc
+++ b/gcc/config/bpf/core-builtins.cc
@@ -45,7 +45,7 @@ along with GCC; see the file COPYING3.  If not see
 
 #include "ctfc.h"
 #include "btf.h"
-#include "coreout.h"
+#include "btfext-out.h"
 #include "core-builtins.h"
 
 /* BPF CO-RE builtins definition.
diff --git a/gcc/config/bpf/core-builtins.h b/gcc/config/bpf/core-builtins.h
index c54f6ddac812..e56b55b94e0c 100644
--- a/gcc/config/bpf/core-builtins.h
+++ b/gcc/config/bpf/core-builtins.h
@@ -1,7 +1,7 @@
 #ifndef BPF_CORE_BUILTINS_H
 #define BPF_CORE_BUILTINS_H
 
-#include "coreout.h"
+#include "btfext-out.h"
 
 enum bpf_builtins
 {
diff --git a/gcc/config/bpf/t-bpf b/gcc/config/bpf/t-bpf
index 18f1fa67794d..dc50332350c4 100644
--- a/gcc/config/bpf/t-bpf
+++ b/gcc/config/bpf/t-bpf
@@ -1,7 +1,7 @@
 
-TM_H += $(srcdir)/config/bpf/coreout.h $(srcdir)/config/bpf/core-builtins.h
+TM_H += $(srcdir)/config/bpf/btfext-out.h $(srcdir)/config/bpf/core-builtins.h
 
-coreout.o: $(srcdir)/config/bpf/coreout.cc
+btfext-out.o: $(srcdir)/config/bpf/btfext-out.cc
$(COMPILE) $<
$(POSTCOMPILE)
 
-- 
2.39.2



[PATCH v2 4/5] bpf: implementation of func_info in .BTF.ext.

2024-02-27 Thread Cupertino Miranda
Kernel verifier complains in some particular cases for missing func_info
implementation in .BTF.ext. This patch implements it.

Strings are cached locally in coreout.cc to avoid adding duplicated
strings in the string list. This string deduplication should eventually
be moved to the CTFC functions such that this happens widely.

With this implementation, the CO-RE relocations information was also
simplified and integrated with the FuncInfo structures.

gcc/Changelog:

PR target/113453
* config/bpf/bpf.cc (bpf_function_prologue): Defined target
hook.
* config/bpf/coreout.cc (brf_ext_info_section)
(btf_ext_info): Moved from coreout.h
(btf_ext_funcinfo, btf_ext_lineinfo): Added struct.
(bpf_core_reloc): Renamed to btf_ext_core_reloc.
(btf_ext): Added static variable.
(btfext_info_sec_find_or_add, SEARCH_NODE_AND_RETURN)
(bpf_create_or_find_funcinfo, bpt_create_core_reloc)
(btf_ext_add_string, btf_funcinfo_type_callback)
(btf_add_func_info_for, btf_validate_funcinfo)
(btf_ext_info_len, output_btfext_func_info): Added function.
(output_btfext_header, bpf_core_reloc_add)
(output_btfext_core_relocs, btf_ext_init, btf_ext_output):
Changed to support new structs.
* config/bpf/coreout.h (btf_ext_funcinfo, btf_ext_lineinfo):
Moved and changed in coreout.cc.
(btf_add_func_info_for, btf_ext_add_string): Added prototypes.

gcc/testsuite/ChangeLog:
PR target/113453
* gcc.target/bpf/btfext-funcinfo-nocore.c: Added.
* gcc.target/bpf/btfext-funcinfo.c: Added.
* gcc.target/bpf/core-attr-5.c: Fixed regexp.
* gcc.target/bpf/core-attr-6.c: Fixed regexp.
* gcc.target/bpf/core-builtin-fieldinfo-offset-1.c: Fixed regexp.
* gcc.target/bpf/core-section-1.c: Fixed regexp
---
 gcc/config/bpf/bpf.cc |  12 +
 gcc/config/bpf/coreout.cc | 518 +-
 gcc/config/bpf/coreout.h  |  20 +-
 .../gcc.target/bpf/btfext-funcinfo-nocore.c   |  42 ++
 .../gcc.target/bpf/btfext-funcinfo.c  |  46 ++
 gcc/testsuite/gcc.target/bpf/core-attr-5.c|   9 +-
 gcc/testsuite/gcc.target/bpf/core-attr-6.c|   6 +-
 .../bpf/core-builtin-fieldinfo-offset-1.c |  13 +-
 gcc/testsuite/gcc.target/bpf/core-section-1.c |   2 +-
 9 files changed, 506 insertions(+), 162 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/bpf/btfext-funcinfo-nocore.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/btfext-funcinfo.c

diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
index 4318b26b9cda..ea47e3a8dbfb 100644
--- a/gcc/config/bpf/bpf.cc
+++ b/gcc/config/bpf/bpf.cc
@@ -385,6 +385,18 @@ bpf_compute_frame_layout (void)
 #undef TARGET_COMPUTE_FRAME_LAYOUT
 #define TARGET_COMPUTE_FRAME_LAYOUT bpf_compute_frame_layout
 
+/* Defined to initialize data for func_info region in .BTF.ext section.  */
+
+static void
+bpf_function_prologue (FILE *f ATTRIBUTE_UNUSED)
+{
+  if (btf_debuginfo_p ())
+btf_add_func_info_for (cfun->decl, current_function_func_begin_label);
+}
+
+#undef TARGET_ASM_FUNCTION_PROLOGUE
+#define TARGET_ASM_FUNCTION_PROLOGUE bpf_function_prologue
+
 /* Expand to the instructions in a function prologue.  This function
is called when expanding the 'prologue' pattern in bpf.md.  */
 
diff --git a/gcc/config/bpf/coreout.cc b/gcc/config/bpf/coreout.cc
index 2f06ec2a0f29..31b2abc3151b 100644
--- a/gcc/config/bpf/coreout.cc
+++ b/gcc/config/bpf/coreout.cc
@@ -31,6 +31,7 @@
 #include "btf.h"
 #include "rtl.h"
 #include "tree-pretty-print.h"
+#include "cgraph.h"
 
 #include "coreout.h"
 
@@ -95,64 +96,193 @@
result, a single .BTF.ext section can contain CO-RE relocations for multiple
programs in distinct sections.  */
 
-/* Internal representation of a BPF CO-RE relocation record.  */
+/* BTF.ext debug info section.  */
+static GTY (()) section * btf_ext_info_section;
+
+#ifndef BTF_EXT_INFO_SECTION_NAME
+#define BTF_EXT_INFO_SECTION_NAME ".BTF.ext"
+#endif
+#define BTF_EXT_INFO_SECTION_FLAGS (SECTION_DEBUG)
+
+#ifndef BTF_EXT_INFO_SECTION_LABEL
+#define BTF_EXT_INFO_SECTION_LABEL "Lbtfext"
+#endif
+
+#define MAX_BTF_EXT_LABEL_BYTES 40
+static char btf_ext_info_section_label[MAX_BTF_EXT_LABEL_BYTES];
+
+/* A funcinfo record, in the .BTF.ext funcinfo section.  */
+struct GTY ((chain_next ("%h.next"))) btf_ext_funcinfo
+{
+  uint32_t type; /* Type ID of a BTF_KIND_FUNC type.  */
+  const char *fnname;
+  const char *label;
+
+  struct btf_ext_funcinfo *next; /* Linked list to collect func_info elems.  */
+};
+
+/* A lineinfo record, in the .BTF.ext lineinfo section.  */
+struct GTY ((chain_next ("%h.next"))) btf_ext_lineinfo
+{
+  uint32_t insn_off;  /* Offset of the instruction.  */
+  uint32_t file_name_off; /* Offset of file name in BTF string table.  */
+  uint32_t line_off;  /* Offset of source line in BTF string table.  */
+  uint32_t 

[PATCH v2 3/5] bpf: Always emit .BTF.ext section if generating BTF

2024-02-27 Thread Cupertino Miranda
BPF applications, when generating BTF information should always create a
.BTF.ext section.
Current implementation was only creating it when -mco-re option was used.
This patch makes .BTF.ext always be generated for BPF target objects.
The patch also adds conditions around btf_finalize function call
such that BTF deallocation happens later for BPF target.
For BPF, btf_finalize is only called after .BTF.ext is generated.

gcc/ChangeLog:

* config/bpf/bpf.cc (bpf_option_override): Make .BTF.ext
enabled by default for BPF.
(bpf_file_end): Call BTF deallocation.
* dwarf2ctf.cc (ctf_debug_finalize): Conditionally execute BTF
deallocation.
---
 gcc/config/bpf/bpf.cc | 20 +---
 gcc/dwarf2ctf.cc  | 12 ++--
 2 files changed, 15 insertions(+), 17 deletions(-)

diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
index d6ca47eeecbe..4318b26b9cda 100644
--- a/gcc/config/bpf/bpf.cc
+++ b/gcc/config/bpf/bpf.cc
@@ -195,10 +195,8 @@ bpf_option_override (void)
   if (TARGET_BPF_CORE && !btf_debuginfo_p ())
 error ("BPF CO-RE requires BTF debugging information, use %<-gbtf%>");
 
-  /* To support the portability needs of BPF CO-RE approach, BTF debug
- information includes the BPF CO-RE relocations.  */
-  if (TARGET_BPF_CORE)
-write_symbols |= BTF_WITH_CORE_DEBUG;
+  /* BPF applications always generate .BTF.ext.  */
+  write_symbols |= BTF_WITH_CORE_DEBUG;
 
   /* Unlike much of the other BTF debug information, the information necessary
  for CO-RE relocations is added to the CTF container by the BPF backend.
@@ -218,10 +216,7 @@ bpf_option_override (void)
   /* -gbtf implies -mcore when using the BPF backend, unless -mno-co-re
  is specified.  */
   if (btf_debuginfo_p () && !(target_flags_explicit & MASK_BPF_CORE))
-{
-  target_flags |= MASK_BPF_CORE;
-  write_symbols |= BTF_WITH_CORE_DEBUG;
-}
+target_flags |= MASK_BPF_CORE;
 
   /* Determine available features from ISA setting (-mcpu=).  */
   if (bpf_has_jmpext == -1)
@@ -267,7 +262,7 @@ bpf_option_override (void)
 static void
 bpf_asm_init_sections (void)
 {
-  if (TARGET_BPF_CORE)
+  if (btf_debuginfo_p () && btf_with_core_debuginfo_p ())
 btf_ext_init ();
 }
 
@@ -279,8 +274,11 @@ bpf_asm_init_sections (void)
 static void
 bpf_file_end (void)
 {
-  if (TARGET_BPF_CORE)
-btf_ext_output ();
+  if (btf_debuginfo_p () && btf_with_core_debuginfo_p ())
+{
+  btf_ext_output ();
+  btf_finalize ();
+}
 }
 
 #undef TARGET_ASM_FILE_END
diff --git a/gcc/dwarf2ctf.cc b/gcc/dwarf2ctf.cc
index 93e5619933fa..dca86edfffa9 100644
--- a/gcc/dwarf2ctf.cc
+++ b/gcc/dwarf2ctf.cc
@@ -944,7 +944,10 @@ ctf_debug_finalize (const char *filename, bool btf)
   if (btf)
 {
   btf_output (filename);
-  btf_finalize ();
+  /* btf_finalize when compiling BPF applciations gets deallocated by the
+BPF target in bpf_file_end.  */
+  if (btf_debuginfo_p () && !btf_with_core_debuginfo_p ())
+   btf_finalize ();
 }
 
   else
@@ -1027,11 +1030,8 @@ ctf_debug_finish (const char * filename)
   /* Emit BTF debug info here when CO-RE relocations need to be generated.
  BTF with CO-RE relocations needs to be generated when CO-RE is in effect
  for the BPF target.  */
-  if (btf_with_core_debuginfo_p ())
-{
-  gcc_assert (btf_debuginfo_p ());
-  ctf_debug_finalize (filename, btf_debuginfo_p ());
-}
+  if (btf_debuginfo_p () && btf_with_core_debuginfo_p ())
+ctf_debug_finalize (filename, btf_debuginfo_p ());
 }
 
 #include "gt-dwarf2ctf.h"
-- 
2.39.2



[PATCH v2 2/5] btf: added KIND_FUNC traversal function.

2024-02-27 Thread Cupertino Miranda
Added a traversal function to traverse all BTF_KIND_FUNC nodes with a
callback function. Used for .BTF.ext section content creation.

gcc/ChangeLog:

* btfout.cc (output_btf_func_types): Use FOR_EACH_VEC_ELT.
(traverse_btf_func_types): Defined function.
* ctfc.h (funcs_traverse_callback): Typedef for function
prototype.
(traverse_btf_func_types): Added prototype.
---
 gcc/btfout.cc | 22 --
 gcc/ctfc.h|  3 +++
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/gcc/btfout.cc b/gcc/btfout.cc
index 7e114e224449..7aabd99f3e7c 100644
--- a/gcc/btfout.cc
+++ b/gcc/btfout.cc
@@ -1276,8 +1276,10 @@ output_btf_types (ctf_container_ref ctfc)
 static void
 output_btf_func_types (ctf_container_ref ctfc)
 {
-  for (size_t i = 0; i < vec_safe_length (funcs); i++)
-btf_asm_func_type (ctfc, (*funcs)[i], i);
+  ctf_dtdef_ref ref;
+  unsigned i;
+  FOR_EACH_VEC_ELT (*funcs, i, ref)
+btf_asm_func_type (ctfc, ref, i);
 }
 
 /* Output all BTF_KIND_DATASEC records.  */
@@ -1452,4 +1454,20 @@ btf_finalize (void)
   tu_ctfc = NULL;
 }
 
+/* Traversal function for all BTF_KIND_FUNC type records.  */
+
+bool
+traverse_btf_func_types (funcs_traverse_callback callback, void *data)
+{
+  ctf_dtdef_ref ref;
+  unsigned i;
+  FOR_EACH_VEC_ELT (*funcs, i, ref)
+{
+  bool stop = callback (ref, data);
+  if (stop == true)
+   return true;
+}
+  return false;
+}
+
 #include "gt-btfout.h"
diff --git a/gcc/ctfc.h b/gcc/ctfc.h
index 7aac57edac55..fa188bf2f5a4 100644
--- a/gcc/ctfc.h
+++ b/gcc/ctfc.h
@@ -441,6 +441,9 @@ extern int ctf_add_variable (ctf_container_ref, const char 
*, ctf_id_t,
 extern ctf_id_t ctf_lookup_tree_type (ctf_container_ref, const tree);
 extern ctf_id_t get_btf_id (ctf_id_t);
 
+typedef bool (*funcs_traverse_callback) (ctf_dtdef_ref, void *);
+bool traverse_btf_func_types (funcs_traverse_callback, void *);
+
 /* CTF section does not emit location information; at this time, location
information is needed for BTF CO-RE use-cases.  */
 
-- 
2.39.2



bpf: PR target/113453 func_info .BTF.ext implementation

2024-02-27 Thread Cupertino Miranda
Hi everyone,

Just an updated version of the patches based on recent reviews from
David Faust.
Thanks for the feedback.

Regards,
Cupertino




[PATCH v2 1/5] btf: fixed type id in BTF_KIND_FUNC struct data.

2024-02-27 Thread Cupertino Miranda
This patch correct the aditition of +1 on the type id, which originally
was done in the wrong location and leaded to func_sts->dtd_type for
BTF_KIND_FUNCS struct data to contain the type id of the previous entry.

gcc/ChangeLog:

* btfout.cc (btf_collect_dataset): Corrected BTF type id.
---
 gcc/btfout.cc | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/gcc/btfout.cc b/gcc/btfout.cc
index dcf751f8fe0d..7e114e224449 100644
--- a/gcc/btfout.cc
+++ b/gcc/btfout.cc
@@ -457,7 +457,8 @@ btf_collect_datasec (ctf_container_ref ctfc)
   func_dtd->dtd_data.ctti_type = dtd->dtd_type;
   func_dtd->linkage = dtd->linkage;
   func_dtd->dtd_name = dtd->dtd_name;
-  func_dtd->dtd_type = num_types_added + num_types_created;
+  /* +1 for the sentinel type not in the types map.  */
+  func_dtd->dtd_type = num_types_added + num_types_created + 1;
 
   /* Only the BTF_KIND_FUNC type actually references the name. The
 BTF_KIND_FUNC_PROTO is always anonymous.  */
@@ -480,8 +481,7 @@ btf_collect_datasec (ctf_container_ref ctfc)
 
  struct btf_var_secinfo info;
 
- /* +1 for the sentinel type not in the types map.  */
- info.type = func_dtd->dtd_type + 1;
+ info.type = func_dtd->dtd_type;
 
  /* Both zero at compile time.  */
  info.size = 0;
-- 
2.39.2



[PATCH 4/5] bpf: implementation of func_info in .BTF.ext.

2024-02-20 Thread Cupertino Miranda
Kernel verifier complains in some particular cases for missing func_info
implementation in .BTF.ext. This patch implements it.

Strings are cached locally in coreout.cc to avoid adding duplicated
strings in the string list. This string deduplication should eventually
be moved to the CTFC functions such that this happens widely.

With this implementation, the CO-RE relocations information was also
simplified and integrated with the FuncInfo structures.

gcc/Changelog:
PR target/113453
* config/bpf/bpf.cc (bpf_function_prologue): Defined target
hook.
* config/bpf/coreout.cc (brf_ext_info_section)
(btf_ext_info): Moved from coreout.h
(btf_ext_funcinfo, btf_ext_lineinfo): Added struct.
(bpf_core_reloc): Renamed to btf_ext_core_reloc.
(btf_ext): Added static variable.
(btfext_info_sec_find_or_add, SEARCH_NODE_AND_RETURN)
(bpf_create_or_find_funcinfo, bpt_create_core_reloc)
(btf_ext_add_string, btf_funcinfo_type_callback)
(btf_add_func_info_for, btf_validate_funcinfo)
(btf_ext_info_len, output_btfext_func_info): Added function.
(output_btfext_header, bpf_core_reloc_add)
(output_btfext_core_relocs, btf_ext_init, btf_ext_output):
Changed to support new structs.
* config/bpf/coreout.h (btf_ext_funcinfo, btf_ext_lineinfo):
Moved and changed in coreout.cc.
(btf_add_func_info_for, btf_ext_add_string): Added prototypes.

gcc/testsuite/ChangeLog:
PR target/113453
* gcc.target/bpf/btfext-funcinfo-nocore.c: Added.
* gcc.target/bpf/btfext-funcinfo.c: Added.
* gcc.target/bpf/core-attr-5.c: Fixed regexp.
* gcc.target/bpf/core-attr-6.c: Fixed regexp.
* gcc.target/bpf/core-builtin-fieldinfo-offset-1.c: Fixed regexp.
* gcc.target/bpf/core-section-1.c: Fixed regexp
---
 gcc/config/bpf/bpf.cc |  12 +
 gcc/config/bpf/coreout.cc | 518 +-
 gcc/config/bpf/coreout.h  |  20 +-
 .../gcc.target/bpf/btfext-funcinfo-nocore.c   |  42 ++
 .../gcc.target/bpf/btfext-funcinfo.c  |  46 ++
 gcc/testsuite/gcc.target/bpf/core-attr-5.c|   9 +-
 gcc/testsuite/gcc.target/bpf/core-attr-6.c|   6 +-
 .../bpf/core-builtin-fieldinfo-offset-1.c |  13 +-
 gcc/testsuite/gcc.target/bpf/core-section-1.c |   2 +-
 9 files changed, 506 insertions(+), 162 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/bpf/btfext-funcinfo-nocore.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/btfext-funcinfo.c

diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
index 4318b26b9cda..ea47e3a8dbfb 100644
--- a/gcc/config/bpf/bpf.cc
+++ b/gcc/config/bpf/bpf.cc
@@ -385,6 +385,18 @@ bpf_compute_frame_layout (void)
 #undef TARGET_COMPUTE_FRAME_LAYOUT
 #define TARGET_COMPUTE_FRAME_LAYOUT bpf_compute_frame_layout
 
+/* Defined to initialize data for func_info region in .BTF.ext section.  */
+
+static void
+bpf_function_prologue (FILE *f ATTRIBUTE_UNUSED)
+{
+  if (btf_debuginfo_p ())
+btf_add_func_info_for (cfun->decl, current_function_func_begin_label);
+}
+
+#undef TARGET_ASM_FUNCTION_PROLOGUE
+#define TARGET_ASM_FUNCTION_PROLOGUE bpf_function_prologue
+
 /* Expand to the instructions in a function prologue.  This function
is called when expanding the 'prologue' pattern in bpf.md.  */
 
diff --git a/gcc/config/bpf/coreout.cc b/gcc/config/bpf/coreout.cc
index 2f06ec2a0f29..31b2abc3151b 100644
--- a/gcc/config/bpf/coreout.cc
+++ b/gcc/config/bpf/coreout.cc
@@ -31,6 +31,7 @@
 #include "btf.h"
 #include "rtl.h"
 #include "tree-pretty-print.h"
+#include "cgraph.h"
 
 #include "coreout.h"
 
@@ -95,64 +96,193 @@
result, a single .BTF.ext section can contain CO-RE relocations for multiple
programs in distinct sections.  */
 
-/* Internal representation of a BPF CO-RE relocation record.  */
+/* BTF.ext debug info section.  */
+static GTY (()) section * btf_ext_info_section;
+
+#ifndef BTF_EXT_INFO_SECTION_NAME
+#define BTF_EXT_INFO_SECTION_NAME ".BTF.ext"
+#endif
+#define BTF_EXT_INFO_SECTION_FLAGS (SECTION_DEBUG)
+
+#ifndef BTF_EXT_INFO_SECTION_LABEL
+#define BTF_EXT_INFO_SECTION_LABEL "Lbtfext"
+#endif
+
+#define MAX_BTF_EXT_LABEL_BYTES 40
+static char btf_ext_info_section_label[MAX_BTF_EXT_LABEL_BYTES];
+
+/* A funcinfo record, in the .BTF.ext funcinfo section.  */
+struct GTY ((chain_next ("%h.next"))) btf_ext_funcinfo
+{
+  uint32_t type; /* Type ID of a BTF_KIND_FUNC type.  */
+  const char *fnname;
+  const char *label;
+
+  struct btf_ext_funcinfo *next; /* Linked list to collect func_info elems.  */
+};
+
+/* A lineinfo record, in the .BTF.ext lineinfo section.  */
+struct GTY ((chain_next ("%h.next"))) btf_ext_lineinfo
+{
+  uint32_t insn_off;  /* Offset of the instruction.  */
+  uint32_t file_name_off; /* Offset of file name in BTF string table.  */
+  uint32_t line_off;  /* Offset of source line in BTF string table.  */
+  uint32_t 

[PATCH 5/5] bpf: renamed coreout.* files to btfext-out.*.

2024-02-20 Thread Cupertino Miranda
gcc/ChangeLog:
* config.gcc (target_gtfiles): changed coreout to btfext-out.
(extra_objs): changed coreout to btfext-out.
* config/bpf/coreout.cc: Renamed to btfext-out.cc
* config/bpf/btfext-out.cc: Added
* config/bpf/coreout.h: Renamed to btfext-out.h
* config/bpf/btfext-out.h: Added
* config/bpf/core-builtins.cc: Changed include
* config/bpf/core-builtins.h: Changed include
* config/bpf/t-bpf: Renamed file.
---
 gcc/config.gcc   | 4 ++--
 gcc/config/bpf/{coreout.cc => btfext-out.cc} | 4 ++--
 gcc/config/bpf/{coreout.h => btfext-out.h}   | 2 +-
 gcc/config/bpf/core-builtins.cc  | 2 +-
 gcc/config/bpf/core-builtins.h   | 2 +-
 gcc/config/bpf/t-bpf | 4 ++--
 6 files changed, 9 insertions(+), 9 deletions(-)
 rename gcc/config/bpf/{coreout.cc => btfext-out.cc} (99%)
 rename gcc/config/bpf/{coreout.h => btfext-out.h} (98%)

diff --git a/gcc/config.gcc b/gcc/config.gcc
index a0f9c6723083..1ca033d75b66 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -1653,8 +1653,8 @@ bpf-*-*)
 tmake_file="${tmake_file} bpf/t-bpf"
 use_collect2=no
 use_gcc_stdint=provide
-extra_objs="coreout.o core-builtins.o"
-target_gtfiles="$target_gtfiles \$(srcdir)/config/bpf/coreout.cc 
\$(srcdir)/config/bpf/core-builtins.cc"
+extra_objs="btfext-out.o core-builtins.o"
+target_gtfiles="$target_gtfiles \$(srcdir)/config/bpf/btfext-out.cc 
\$(srcdir)/config/bpf/core-builtins.cc"
 ;;
 cris-*-elf | cris-*-none)
tm_file="elfos.h newlib-stdint.h ${tm_file}"
diff --git a/gcc/config/bpf/coreout.cc b/gcc/config/bpf/btfext-out.cc
similarity index 99%
rename from gcc/config/bpf/coreout.cc
rename to gcc/config/bpf/btfext-out.cc
index 31b2abc3151b..4281cca83e13 100644
--- a/gcc/config/bpf/coreout.cc
+++ b/gcc/config/bpf/btfext-out.cc
@@ -33,7 +33,7 @@
 #include "tree-pretty-print.h"
 #include "cgraph.h"
 
-#include "coreout.h"
+#include "btfext-out.h"
 
 /* This file contains data structures and routines for construction and output
of BPF Compile Once - Run Everywhere (BPF CO-RE) information.
@@ -618,4 +618,4 @@ btf_ext_output (void)
   dw2_asm_output_data (4, 0, "Required padding by libbpf structs");
 }
 
-#include "gt-coreout.h"
+#include "gt-btfext-out.h"
diff --git a/gcc/config/bpf/coreout.h b/gcc/config/bpf/btfext-out.h
similarity index 98%
rename from gcc/config/bpf/coreout.h
rename to gcc/config/bpf/btfext-out.h
index 1c26b9274739..b36309475c97 100644
--- a/gcc/config/bpf/coreout.h
+++ b/gcc/config/bpf/btfext-out.h
@@ -1,4 +1,4 @@
-/* coreout.h - Declarations and definitions related to
+/* btfext-out.h - Declarations and definitions related to
BPF Compile Once - Run Everywhere (CO-RE) support.
Copyright (C) 2021-2024 Free Software Foundation, Inc.
 
diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc
index aa75fd68cae6..8d8c54c1fb3d 100644
--- a/gcc/config/bpf/core-builtins.cc
+++ b/gcc/config/bpf/core-builtins.cc
@@ -45,7 +45,7 @@ along with GCC; see the file COPYING3.  If not see
 
 #include "ctfc.h"
 #include "btf.h"
-#include "coreout.h"
+#include "btfext-out.h"
 #include "core-builtins.h"
 
 /* BPF CO-RE builtins definition.
diff --git a/gcc/config/bpf/core-builtins.h b/gcc/config/bpf/core-builtins.h
index c54f6ddac812..e56b55b94e0c 100644
--- a/gcc/config/bpf/core-builtins.h
+++ b/gcc/config/bpf/core-builtins.h
@@ -1,7 +1,7 @@
 #ifndef BPF_CORE_BUILTINS_H
 #define BPF_CORE_BUILTINS_H
 
-#include "coreout.h"
+#include "btfext-out.h"
 
 enum bpf_builtins
 {
diff --git a/gcc/config/bpf/t-bpf b/gcc/config/bpf/t-bpf
index 18f1fa67794d..dc50332350c4 100644
--- a/gcc/config/bpf/t-bpf
+++ b/gcc/config/bpf/t-bpf
@@ -1,7 +1,7 @@
 
-TM_H += $(srcdir)/config/bpf/coreout.h $(srcdir)/config/bpf/core-builtins.h
+TM_H += $(srcdir)/config/bpf/btfext-out.h $(srcdir)/config/bpf/core-builtins.h
 
-coreout.o: $(srcdir)/config/bpf/coreout.cc
+btfext-out.o: $(srcdir)/config/bpf/btfext-out.cc
$(COMPILE) $<
$(POSTCOMPILE)
 
-- 
2.39.2



[PATCH 3/5] btf: moved btf deallocation to final.

2024-02-20 Thread Cupertino Miranda
Dissociated .BTF.ext from the CO-RE relocations creation. Improvement of
allocation/deallocation of BTF structures. Moving deallocation to final
when needed.

gcc/ChangeLog:

* config/bpf/bpf.cc (bpf_option_override): Make BTF.ext enabled
by default for BPF.
(btf_asm_init_sections): Add btf deallocation.
* dwarf2ctf.cc (ctf_debug_finalize): Fixed btf deallocation.
---
 gcc/config/bpf/bpf.cc | 20 +---
 gcc/dwarf2ctf.cc  |  5 -
 2 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
index d6ca47eeecbe..4318b26b9cda 100644
--- a/gcc/config/bpf/bpf.cc
+++ b/gcc/config/bpf/bpf.cc
@@ -195,10 +195,8 @@ bpf_option_override (void)
   if (TARGET_BPF_CORE && !btf_debuginfo_p ())
 error ("BPF CO-RE requires BTF debugging information, use %<-gbtf%>");
 
-  /* To support the portability needs of BPF CO-RE approach, BTF debug
- information includes the BPF CO-RE relocations.  */
-  if (TARGET_BPF_CORE)
-write_symbols |= BTF_WITH_CORE_DEBUG;
+  /* BPF applications always generate .BTF.ext.  */
+  write_symbols |= BTF_WITH_CORE_DEBUG;
 
   /* Unlike much of the other BTF debug information, the information necessary
  for CO-RE relocations is added to the CTF container by the BPF backend.
@@ -218,10 +216,7 @@ bpf_option_override (void)
   /* -gbtf implies -mcore when using the BPF backend, unless -mno-co-re
  is specified.  */
   if (btf_debuginfo_p () && !(target_flags_explicit & MASK_BPF_CORE))
-{
-  target_flags |= MASK_BPF_CORE;
-  write_symbols |= BTF_WITH_CORE_DEBUG;
-}
+target_flags |= MASK_BPF_CORE;
 
   /* Determine available features from ISA setting (-mcpu=).  */
   if (bpf_has_jmpext == -1)
@@ -267,7 +262,7 @@ bpf_option_override (void)
 static void
 bpf_asm_init_sections (void)
 {
-  if (TARGET_BPF_CORE)
+  if (btf_debuginfo_p () && btf_with_core_debuginfo_p ())
 btf_ext_init ();
 }
 
@@ -279,8 +274,11 @@ bpf_asm_init_sections (void)
 static void
 bpf_file_end (void)
 {
-  if (TARGET_BPF_CORE)
-btf_ext_output ();
+  if (btf_debuginfo_p () && btf_with_core_debuginfo_p ())
+{
+  btf_ext_output ();
+  btf_finalize ();
+}
 }
 
 #undef TARGET_ASM_FILE_END
diff --git a/gcc/dwarf2ctf.cc b/gcc/dwarf2ctf.cc
index 93e5619933fa..b9dfecf2c1c4 100644
--- a/gcc/dwarf2ctf.cc
+++ b/gcc/dwarf2ctf.cc
@@ -944,7 +944,10 @@ ctf_debug_finalize (const char *filename, bool btf)
   if (btf)
 {
   btf_output (filename);
-  btf_finalize ();
+  /* btf_finalize when compiling BPF applciations gets deallocated by the
+BPF target in bpf_file_end.  */
+  if (btf_debuginfo_p () && !btf_with_core_debuginfo_p ())
+   btf_finalize ();
 }
 
   else
-- 
2.39.2



[PATCH 2/5] btf: added KIND_FUNC traversal function.

2024-02-20 Thread Cupertino Miranda
Added a traversal function to traverse all BTF_KIND_FUNC nodes with a
callback function. Used for .BTF.ext section content creation.

gcc/ChangeLog

* btfout.cc (output_btf_func_types): use FOR_EACH_VEC_ELT.
(traverse_btf_func_types): Defined function.
* ctfc.h (funcs_traverse_callback): typedef for function
prototype.
(traverse_btf_func_types): Added prototype.
---
 gcc/btfout.cc | 22 --
 gcc/ctfc.h|  3 +++
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/gcc/btfout.cc b/gcc/btfout.cc
index 7e114e224449..7aabd99f3e7c 100644
--- a/gcc/btfout.cc
+++ b/gcc/btfout.cc
@@ -1276,8 +1276,10 @@ output_btf_types (ctf_container_ref ctfc)
 static void
 output_btf_func_types (ctf_container_ref ctfc)
 {
-  for (size_t i = 0; i < vec_safe_length (funcs); i++)
-btf_asm_func_type (ctfc, (*funcs)[i], i);
+  ctf_dtdef_ref ref;
+  unsigned i;
+  FOR_EACH_VEC_ELT (*funcs, i, ref)
+btf_asm_func_type (ctfc, ref, i);
 }
 
 /* Output all BTF_KIND_DATASEC records.  */
@@ -1452,4 +1454,20 @@ btf_finalize (void)
   tu_ctfc = NULL;
 }
 
+/* Traversal function for all BTF_KIND_FUNC type records.  */
+
+bool
+traverse_btf_func_types (funcs_traverse_callback callback, void *data)
+{
+  ctf_dtdef_ref ref;
+  unsigned i;
+  FOR_EACH_VEC_ELT (*funcs, i, ref)
+{
+  bool stop = callback (ref, data);
+  if (stop == true)
+   return true;
+}
+  return false;
+}
+
 #include "gt-btfout.h"
diff --git a/gcc/ctfc.h b/gcc/ctfc.h
index 7aac57edac55..fa188bf2f5a4 100644
--- a/gcc/ctfc.h
+++ b/gcc/ctfc.h
@@ -441,6 +441,9 @@ extern int ctf_add_variable (ctf_container_ref, const char 
*, ctf_id_t,
 extern ctf_id_t ctf_lookup_tree_type (ctf_container_ref, const tree);
 extern ctf_id_t get_btf_id (ctf_id_t);
 
+typedef bool (*funcs_traverse_callback) (ctf_dtdef_ref, void *);
+bool traverse_btf_func_types (funcs_traverse_callback, void *);
+
 /* CTF section does not emit location information; at this time, location
information is needed for BTF CO-RE use-cases.  */
 
-- 
2.39.2



[PATCH 1/5] btf: fixed type id in BTF_KIND_FUNC struct data.

2024-02-20 Thread Cupertino Miranda
This patch correct the aditition of +1 on the type id, which originally
was done in the wrong location and leaded to func_sts->dtd_type for
BTF_KIND_FUNCS struct data to contain the type id of the previous entry.

gcc/ChangeLog:
* btfout.cc (btf_collect_dataset): Corrected BTF type id.
---
 gcc/btfout.cc | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/gcc/btfout.cc b/gcc/btfout.cc
index dcf751f8fe0d..7e114e224449 100644
--- a/gcc/btfout.cc
+++ b/gcc/btfout.cc
@@ -457,7 +457,8 @@ btf_collect_datasec (ctf_container_ref ctfc)
   func_dtd->dtd_data.ctti_type = dtd->dtd_type;
   func_dtd->linkage = dtd->linkage;
   func_dtd->dtd_name = dtd->dtd_name;
-  func_dtd->dtd_type = num_types_added + num_types_created;
+  /* +1 for the sentinel type not in the types map.  */
+  func_dtd->dtd_type = num_types_added + num_types_created + 1;
 
   /* Only the BTF_KIND_FUNC type actually references the name. The
 BTF_KIND_FUNC_PROTO is always anonymous.  */
@@ -480,8 +481,7 @@ btf_collect_datasec (ctf_container_ref ctfc)
 
  struct btf_var_secinfo info;
 
- /* +1 for the sentinel type not in the types map.  */
- info.type = func_dtd->dtd_type + 1;
+ info.type = func_dtd->dtd_type;
 
  /* Both zero at compile time.  */
  info.size = 0;
-- 
2.39.2



bpf: PR target/113453 func_info .BTF.ext implementation

2024-02-20 Thread Cupertino Miranda
Good morning,

This is a patch series with the implementation of func_info region
within bpf target .BTF.ext section.
Considering the required changes it also implied some changes in BTF and
in the original CO-RE implementation, more specifically the structure
used and how the relocations were created.

Looking forward to your review.

Best regards,
Cupertino




Re: [PATCH] btf: print string position as comment for validation and testing purposes.

2024-01-08 Thread Cupertino Miranda


Thanks! Committed.

David Faust writes:

> Hi Cupertino,
>
> On 1/8/24 02:55, Cupertino Miranda wrote:
>> Hi everyone,
>>
>> This patch adds a comment to the BTF strings regarding their position
>> within the section. This is useful for assembly inspection purposes.
>>
>> Regards,
>> Cupertino
>>
>> When using -dA, this function was only printing as comment btf_string or
>> btf_aux_string.
>> This patch changes the comment to also include the position of the
>> string within the section in hexadecimal format.
>>
>> gcc/ChangeLog:
>>  * btfout.cc (output_btf_strs): Changed.
>
> Please be a little bit more expressive in the ChangeLog.
> Something along the lines of "print string offset in comment" will be
> much more useful.
>
> LGTM with that change, please apply.
> Thanks!
>
>> ---
>>  gcc/btfout.cc | 7 +--
>>  1 file changed, 5 insertions(+), 2 deletions(-)
>>
>> diff --git a/gcc/btfout.cc b/gcc/btfout.cc
>> index db4f1084f85c..04218adc9e66 100644
>> --- a/gcc/btfout.cc
>> +++ b/gcc/btfout.cc
>> @@ -1081,17 +1081,20 @@ static void
>>  output_btf_strs (ctf_container_ref ctfc)
>>  {
>>ctf_string_t * ctf_string = ctfc->ctfc_strtable.ctstab_head;
>> +  static int str_pos = 0;
>>
>>while (ctf_string)
>>  {
>> -  dw2_asm_output_nstring (ctf_string->cts_str, -1, "btf_string");
>> +  dw2_asm_output_nstring (ctf_string->cts_str, -1, "btf_string, str_pos 
>> = 0x%x", str_pos);
>> +  str_pos += strlen(ctf_string->cts_str) + 1;
>>ctf_string = ctf_string->cts_next;
>>  }
>>
>>ctf_string = ctfc->ctfc_aux_strtable.ctstab_head;
>>while (ctf_string)
>>  {
>> -  dw2_asm_output_nstring (ctf_string->cts_str, -1, "btf_aux_string");
>> +  dw2_asm_output_nstring (ctf_string->cts_str, -1, "btf_aux_string, 
>> str_pos = 0x%x", str_pos);
>> +  str_pos += strlen(ctf_string->cts_str) + 1;
>>ctf_string = ctf_string->cts_next;
>>  }
>>  }


Re: [PATCH] bpf: Correct BTF for kernel_helper attributed decls.

2024-01-08 Thread Cupertino Miranda


Thanks! Committed.

David Faust writes:

> Hi Cupetino,
>
> On 1/8/24 03:05, Cupertino Miranda wrote:
>> Hi everyone,
>>
>> This patch address the problem reported in:
>> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113225
>>
>> Looking forward to your review.
>
> LGTM, thanks. Please apply.
>
>>
>> Cheers,
>> Cupertino
>>
>>
>> This patch fix a problem with kernel_helper attribute BTF information,
>> which incorrectly generates BTF_KIND_FUNC entry.
>> This BTF entry although accurate with traditional extern function
>> declarations, once the function is attributed with kernel_helper, it is
>> semantically incompatible of the kernel helpers in BPF infrastructure.
>>
>> gcc/ChangeLog:
>>  PR target/113225
>>  * btfout.cc (btf_collect_datasec): Skip creating BTF info for
>>  extern and kernel_helper attributed function decls.
>> gcc/testsuite/ChangeLog:
>>  * gcc.target/bpf/attr-kernel-helper.c: New test.
>> ---
>>  gcc/btfout.cc |  7 +++
>>  gcc/testsuite/gcc.target/bpf/attr-kernel-helper.c | 15 +++
>>  2 files changed, 22 insertions(+)
>>  create mode 100644 gcc/testsuite/gcc.target/bpf/attr-kernel-helper.c
>>
>> diff --git a/gcc/btfout.cc b/gcc/btfout.cc
>> index 04218adc9e66..39e7bec43bfb 100644
>> --- a/gcc/btfout.cc
>> +++ b/gcc/btfout.cc
>> @@ -35,6 +35,8 @@ along with GCC; see the file COPYING3.  If not see
>>  #include "diagnostic-core.h"
>>  #include "cgraph.h"
>>  #include "varasm.h"
>> +#include "stringpool.h"
>> +#include "attribs.h"
>>  #include "dwarf2out.h" /* For lookup_decl_die.  */
>>
>>  static int btf_label_num;
>> @@ -429,6 +431,11 @@ btf_collect_datasec (ctf_container_ref ctfc)
>>if (dtd == NULL)
>>  continue;
>>
>> +  if (DECL_EXTERNAL (func->decl)
>> +  && (lookup_attribute ("kernel_helper",
>> +DECL_ATTRIBUTES (func->decl))) != NULL_TREE)
>> +continue;
>> +
>>/* Functions actually get two types: a BTF_KIND_FUNC_PROTO, and
>>   also a BTF_KIND_FUNC.  But the CTF container only allocates one
>>   type per function, which matches closely with BTF_KIND_FUNC_PROTO.
>> diff --git a/gcc/testsuite/gcc.target/bpf/attr-kernel-helper.c 
>> b/gcc/testsuite/gcc.target/bpf/attr-kernel-helper.c
>> new file mode 100644
>> index ..7c5a0007c979
>> --- /dev/null
>> +++ b/gcc/testsuite/gcc.target/bpf/attr-kernel-helper.c
>> @@ -0,0 +1,15 @@
>> +/* Basic test for kernel_helper attribute BTF information.  */
>> +
>> +/* { dg-do compile } */
>> +/* { dg-options "-O0 -dA -gbtf" } */
>> +
>> +extern int foo_helper(int) __attribute((kernel_helper(42)));
>> +extern int foo_nohelper(int);
>> +
>> +int bar (int arg)
>> +{
>> +  return foo_helper (arg) + foo_nohelper (arg);
>> +}
>> +
>> +/* { dg-final { scan-assembler-times "BTF_KIND_FUNC 'foo_nohelper'" 1 } } */
>> +/* { dg-final { scan-assembler-times "BTF_KIND_FUNC 'foo_helper'" 0 } } */


[PATCH] btf: print string position as comment for validation and testing purposes.

2024-01-08 Thread Cupertino Miranda
Hi everyone,

This patch adds a comment to the BTF strings regarding their position
within the section. This is useful for assembly inspection purposes.

Regards,
Cupertino

When using -dA, this function was only printing as comment btf_string or
btf_aux_string.
This patch changes the comment to also include the position of the
string within the section in hexadecimal format.

gcc/ChangeLog:
* btfout.cc (output_btf_strs): Changed.
---
 gcc/btfout.cc | 7 +--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/gcc/btfout.cc b/gcc/btfout.cc
index db4f1084f85c..04218adc9e66 100644
--- a/gcc/btfout.cc
+++ b/gcc/btfout.cc
@@ -1081,17 +1081,20 @@ static void
 output_btf_strs (ctf_container_ref ctfc)
 {
   ctf_string_t * ctf_string = ctfc->ctfc_strtable.ctstab_head;
+  static int str_pos = 0;
 
   while (ctf_string)
 {
-  dw2_asm_output_nstring (ctf_string->cts_str, -1, "btf_string");
+  dw2_asm_output_nstring (ctf_string->cts_str, -1, "btf_string, str_pos = 
0x%x", str_pos);
+  str_pos += strlen(ctf_string->cts_str) + 1;
   ctf_string = ctf_string->cts_next;
 }
 
   ctf_string = ctfc->ctfc_aux_strtable.ctstab_head;
   while (ctf_string)
 {
-  dw2_asm_output_nstring (ctf_string->cts_str, -1, "btf_aux_string");
+  dw2_asm_output_nstring (ctf_string->cts_str, -1, "btf_aux_string, 
str_pos = 0x%x", str_pos);
+  str_pos += strlen(ctf_string->cts_str) + 1;
   ctf_string = ctf_string->cts_next;
 }
 }
-- 
2.30.2



[PATCH] bpf: Correct BTF for kernel_helper attributed decls.

2024-01-08 Thread Cupertino Miranda
Hi everyone,

This patch address the problem reported in:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113225

Looking forward to your review.

Cheers,
Cupertino


This patch fix a problem with kernel_helper attribute BTF information,
which incorrectly generates BTF_KIND_FUNC entry.
This BTF entry although accurate with traditional extern function
declarations, once the function is attributed with kernel_helper, it is
semantically incompatible of the kernel helpers in BPF infrastructure.

gcc/ChangeLog:
PR target/113225
* btfout.cc (btf_collect_datasec): Skip creating BTF info for
extern and kernel_helper attributed function decls.
gcc/testsuite/ChangeLog:
* gcc.target/bpf/attr-kernel-helper.c: New test.
---
 gcc/btfout.cc |  7 +++
 gcc/testsuite/gcc.target/bpf/attr-kernel-helper.c | 15 +++
 2 files changed, 22 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/bpf/attr-kernel-helper.c

diff --git a/gcc/btfout.cc b/gcc/btfout.cc
index 04218adc9e66..39e7bec43bfb 100644
--- a/gcc/btfout.cc
+++ b/gcc/btfout.cc
@@ -35,6 +35,8 @@ along with GCC; see the file COPYING3.  If not see
 #include "diagnostic-core.h"
 #include "cgraph.h"
 #include "varasm.h"
+#include "stringpool.h"
+#include "attribs.h"
 #include "dwarf2out.h" /* For lookup_decl_die.  */
 
 static int btf_label_num;
@@ -429,6 +431,11 @@ btf_collect_datasec (ctf_container_ref ctfc)
   if (dtd == NULL)
continue;
 
+  if (DECL_EXTERNAL (func->decl)
+ && (lookup_attribute ("kernel_helper",
+   DECL_ATTRIBUTES (func->decl))) != NULL_TREE)
+   continue;
+
   /* Functions actually get two types: a BTF_KIND_FUNC_PROTO, and
 also a BTF_KIND_FUNC.  But the CTF container only allocates one
 type per function, which matches closely with BTF_KIND_FUNC_PROTO.
diff --git a/gcc/testsuite/gcc.target/bpf/attr-kernel-helper.c 
b/gcc/testsuite/gcc.target/bpf/attr-kernel-helper.c
new file mode 100644
index ..7c5a0007c979
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/attr-kernel-helper.c
@@ -0,0 +1,15 @@
+/* Basic test for kernel_helper attribute BTF information.  */
+
+/* { dg-do compile } */
+/* { dg-options "-O0 -dA -gbtf" } */
+
+extern int foo_helper(int) __attribute((kernel_helper(42)));
+extern int foo_nohelper(int);
+
+int bar (int arg)
+{
+  return foo_helper (arg) + foo_nohelper (arg);
+}
+
+/* { dg-final { scan-assembler-times "BTF_KIND_FUNC 'foo_nohelper'" 1 } } */
+/* { dg-final { scan-assembler-times "BTF_KIND_FUNC 'foo_helper'" 0 } } */
-- 
2.30.2



Re: bpf: Throw error when external libcalls are generated.

2023-11-28 Thread Cupertino Miranda


Reverted! Apologies for the mistake.

Jose E. Marchesi writes:

>> Hi Cuper.
>> OK. Thanks for the patch.
>
> This commit is breaking the BPF build, because libgcc emits libcalls to
> __builtin_abort.  We need to rethink this.
>
> Please revert:
>
>   commit faf5b148588bd7fbb60ec669aefa704044037cdc
>   Author: Cupertino Miranda 
>   Date:   Thu Nov 23 22:28:01 2023 +
>
> Thanks!
>
>>
>>> Hi everyone,
>>>
>>> The attached patch is a temporary solution for the lack of proper linker
>>> and external library linking of the eBPF platform.
>>> Any calls created by the compiler, that would usually be defined within
>>> libgcc, will endup being undefined in bpftool, when GCC the compiled
>>> code is passed.
>>>
>>> This patch anticipates that error to the compiler, by verifiying if
>>> any of those calls are being generated, and reporting as an error.
>>>
>>> Looking forward to your comments.
>>>
>>> Cheers,
>>> Cupertino
>>>
>>> commit c2110ae497c7ff83c309f172bc265973652b760d
>>> Author: Cupertino Miranda 
>>> Date:   Thu Nov 23 22:28:01 2023 +
>>>
>>> This patch enables errors when external calls are created.
>>>
>>> When architectural limitations or usage of builtins implies the compiler
>>> to create function calls to external libraries that implement the
>>> functionality, GCC will now report an error claiming that this function
>>> calls are not compatible with eBPF target.
>>> Examples of those are the usage of __builtin_memmove and a sign division
>>> in BPF ISA v3 or below that will require to call __divdi3.
>>> This is currently an eBPF limitation which does not support linking of
>>> object files but rather "raw" non linked ones. Those object files are
>>> loaded and relocated by libbpf and the kernel.
>>>
>>> gcc/ChangeLog:
>>> * config/bpf/bpf.cc (bpf_output_call): Report error in case the
>>> function call is for a builtin.
>>> (bpf_external_libcall): Added target hook to detect and report
>>> error when other external calls that are not builtins.
>>>
>>> diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
>>> index 0c9d5257c384..1c84113055b1 100644
>>> --- a/gcc/config/bpf/bpf.cc
>>> +++ b/gcc/config/bpf/bpf.cc
>>> @@ -744,6 +744,15 @@ bpf_output_call (rtx target)
>>> xops[0] = GEN_INT (TREE_INT_CST_LOW (TREE_VALUE (attr_args)));
>>> output_asm_insn ("call\t%0", xops);
>>>   }
>>> +   else if (fndecl_built_in_p (decl))
>>> + {
>>> +   /* For now lets report this as an error while we are not able to
>>> +  link eBPF object files.  In particular with libgcc.  */
>>> +   tree name = DECL_NAME (decl);
>>> +   error ("call to external builtin %s in function, which is not 
>>> supported by "
>>> +  "eBPF", name != NULL_TREE ? IDENTIFIER_POINTER (name) : 
>>> "(anon)");
>>> +   output_asm_insn ("call 0", NULL);
>>> + }
>>> else
>>>   output_asm_insn ("call\t%0", );
>>>
>>> @@ -763,6 +772,18 @@ bpf_output_call (rtx target)
>>>return "";
>>>  }
>>>
>>> +static void
>>> +bpf_external_libcall (rtx fun)
>>> +{
>>> +  tree decl = SYMBOL_REF_DECL (fun);
>>> +  tree name = DECL_NAME (decl);
>>> +  error ("call to external libcall %s in function, which is not supported 
>>> by "
>>> +"eBPF", name != NULL_TREE ? IDENTIFIER_POINTER (name) : "(anon)");
>>> +}
>>> +
>>> +#undef  TARGET_ASM_EXTERNAL_LIBCALL
>>> +#define TARGET_ASM_EXTERNAL_LIBCALL bpf_external_libcall
>>> +
>>>  /* Print register name according to assembly dialect.  In normal
>>> syntax registers are printed like %rN where N is the register
>>> number.
>>> diff --git a/gcc/testsuite/gcc.target/bpf/atomic-cmpxchg-2.c 
>>> b/gcc/testsuite/gcc.target/bpf/atomic-cmpxchg-2.c
>>> index 4036570ac601..fec720584e48 100644
>>> --- a/gcc/testsuite/gcc.target/bpf/atomic-cmpxchg-2.c
>>> +++ b/gcc/testsuite/gcc.target/bpf/atomic-cmpxchg-2.c
>>> @@ -6,7 +6,7 @@ foo (int *p, int *expected, int desired)
>>>  {
>>>return __atomic_com

Re: [PATCH v2] Fixed problem with BTF defining smaller enums.

2023-11-28 Thread Cupertino Miranda


Thanks! Committed!

David Faust writes:

> Hi Cupertino,
>
> On 11/27/23 09:21, Cupertino Miranda wrote:
>> Hi everyone,
>>
>> David: Thanks for the v1 review.
>>
>> This version adds the following;
>>  - test case,
>>  - improves condition logic,
>>  - fixes mask typo.
>>
>> Looking forward to your review.
>
> v2 LGTM, please apply.
> Thanks!
>
>>
>> v1 at: https://gcc.gnu.org/pipermail/gcc-patches/2023-November/636391.html
>>
>> Cheers,
>> Cupertino
>>
>>
>> 0004-Fixed-problem-with-BTF-defining-smaller-enums.patch
>>
>> commit 3f89d352a4ee90882089142d743f8a748013b5fe
>> Author: Cupertino Miranda 
>> Date:   Fri Nov 10 14:02:30 2023 +
>>
>> Fixed problem with BTF defining smaller enums.
>>
>> This patch fixes a BTF, which would become invalid when having
>> smaller then 4 byte definitions of enums.
>> For example, when using the __attribute__((mode(byte))) in the enum
>> definition.
>>
>> Two problems were identified:
>>  - it would incorrectly create an entry for enum64 when the size of the
>>enum was different then 4.
>>  - it would allocate less then 4 bytes for the value entry in BTF, in
>>case the type was smaller.
>>
>> BTF generated was validated against clang.
>>
>> gcc/ChangeLog:
>> * bpfout.cc (btf_calc_num_vbytes): Fixed logic for enum64.
>> (btf_asm_enum_const): Corrected logic for enum64 and smaller
>> than 4 bytes values.
>>
>> gcc/testsuite/ChangeLog:
>> gcc.dg/debug/btf/btf-enum-small.c: Added test.
>>
>> diff --git a/gcc/btfout.cc b/gcc/btfout.cc
>> index e07fed302c24..5f2e99ce4725 100644
>> --- a/gcc/btfout.cc
>> +++ b/gcc/btfout.cc
>> @@ -299,7 +299,7 @@ btf_calc_num_vbytes (ctf_dtdef_ref dtd)
>>break;
>>
>>  case BTF_KIND_ENUM:
>> -  vlen_bytes += (dtd->dtd_data.ctti_size == 0x8)
>> +  vlen_bytes += (dtd->dtd_data.ctti_size > 4)
>>  ? vlen * sizeof (struct btf_enum64)
>>  : vlen * sizeof (struct btf_enum);
>>break;
>> @@ -914,8 +914,8 @@ btf_asm_enum_const (unsigned int size, ctf_dmdef_t * 
>> dmd, unsigned int idx)
>>  {
>>dw2_asm_output_data (4, dmd->dmd_name_offset, "ENUM_CONST '%s' idx=%u",
>> dmd->dmd_name, idx);
>> -  if (size == 4)
>> -dw2_asm_output_data (size, dmd->dmd_value, "bte_value");
>> +  if (size <= 4)
>> +dw2_asm_output_data (size < 4 ? 4 : size, dmd->dmd_value, "bte_value");
>>else
>>  {
>>dw2_asm_output_data (4, dmd->dmd_value & 0x, 
>> "bte_value_lo32");
>> diff --git a/gcc/testsuite/gcc.dg/debug/btf/btf-enum-small.c 
>> b/gcc/testsuite/gcc.dg/debug/btf/btf-enum-small.c
>> new file mode 100644
>> index ..eb8a1bd2c438
>> --- /dev/null
>> +++ b/gcc/testsuite/gcc.dg/debug/btf/btf-enum-small.c
>> @@ -0,0 +1,28 @@
>> +/* Test BTF generation for small enums.  */
>> +
>> +/* { dg-do compile } */
>> +/* { dg-options "-O2 -gbtf -dA" } */
>> +
>> +/* { dg-final { scan-assembler-not "bte_value_lo32" } } */
>> +/* { dg-final { scan-assembler-not "bte_value_hi32" } } */
>> +/* { dg-final { scan-assembler-times "\[\t \]0x602\[\t 
>> \]+\[^\n\]*btt_info" 1 } } */
>> +/* { dg-final { scan-assembler-times " ENUM_CONST 'eSMALL' idx=0" 1 } } */
>> +/* { dg-final { scan-assembler-times " ENUM_CONST 'eSMALLY' idx=1" 1 } } */
>> +/* { dg-final { scan-assembler-times "ascii \"eSMALL.0\"\[\t 
>> \]+\[^\n\]*btf_string" 1 } } */
>> +/* { dg-final { scan-assembler-times "ascii \"eSMALLY.0\"\[\t 
>> \]+\[^\n\]*btf_string" 1 } } */
>> +/* { dg-final { scan-assembler-times "bte_value" 2 } } */
>> +
>> +enum smalled_enum
>> +{
>> +  eSMALL,
>> +  eSMALLY,
>> +} __attribute__((mode(byte)));
>> +
>> +struct root_struct {
>> +  enum smalled_enum esmall;
>> +};
>> +
>> +enum smalled_enum
>> +foo(struct root_struct *root) {
>> +  return root->esmall;
>> +}
>>


Re: [PATCH] bpf: Forces __buildin_memcmp not to generate a call upto 1024 bytes.

2023-11-28 Thread Cupertino Miranda


Thanks! Committed!

Jose E. Marchesi writes:

> Hi Cuper.
>
> Sorry, I missed this patch last week.
> This is OK.
>
> Thanks!
>
>> This patch forces __builtin_memcmp calls upto data sizes of 1024 to
>> become inline in caller.
>> This is a requirement by BPF and it mimics the default behaviour of the
>> clang BPF implementation.
>>
>> gcc/ChangeLog:
>>  * config/bpf/bpf.cc (bpf_use_by_pieces_infrastructure_p): Added
>>  function to bypass default behaviour.
>>  * config/bpf/bpf.h (COMPARE_MAX_PIECES): Defined to 1024 bytes.
>> ---
>>  gcc/config/bpf/bpf.cc | 16 
>>  gcc/config/bpf/bpf.h  |  5 +
>>  2 files changed, 21 insertions(+)
>>
>> diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
>> index a0956a069729..764a3e487cb6 100644
>> --- a/gcc/config/bpf/bpf.cc
>> +++ b/gcc/config/bpf/bpf.cc
>> @@ -1115,6 +1115,22 @@ bpf_small_register_classes_for_mode_p (machine_mode 
>> mode)
>>  #define TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P \
>>bpf_small_register_classes_for_mode_p
>>
>> +static bool
>> +bpf_use_by_pieces_infrastructure_p (unsigned HOST_WIDE_INT size,
>> +unsigned int align ATTRIBUTE_UNUSED,
>> +enum by_pieces_operation op,
>> +bool speed_p)
>> +{
>> +  if (op != COMPARE_BY_PIECES)
>> +return default_use_by_pieces_infrastructure_p (size, align, op, 
>> speed_p);
>> +
>> +  return size <= COMPARE_MAX_PIECES;
>> +}
>> +
>> +#undef TARGET_USE_BY_PIECES_INFRASTRUCTURE_P
>> +#define TARGET_USE_BY_PIECES_INFRASTRUCTURE_P \
>> +  bpf_use_by_pieces_infrastructure_p
>> +
>>  /* Finally, build the GCC target.  */
>>
>>  struct gcc_target targetm = TARGET_INITIALIZER;
>> diff --git a/gcc/config/bpf/bpf.h b/gcc/config/bpf/bpf.h
>> index 82702aa7b6ba..1f177ec4c4ef 100644
>> --- a/gcc/config/bpf/bpf.h
>> +++ b/gcc/config/bpf/bpf.h
>> @@ -489,6 +489,11 @@ enum reg_class
>> locations.  */
>>  #define MOVE_MAX 8
>>
>> +/* Allow upto 1024 bytes moves to occur using by_pieces
>> +   infrastructure.  This mimics clang behaviour when using
>> +   __builtin_memcmp.  */
>> +#define COMPARE_MAX_PIECES 1024
>> +
>>  /* An alias for the machine mode for pointers.  */
>>  #define Pmode DImode


Re: [PATCH] bpf: Forces __buildin_memcmp not to generate a call upto 1024 bytes.

2023-11-28 Thread Cupertino Miranda


Thanks! Committed !

Jose E. Marchesi writes:

> Hi Cuper.
>
> Sorry, I missed this patch last week.
> This is OK.
>
> Thanks!
>
>> This patch forces __builtin_memcmp calls upto data sizes of 1024 to
>> become inline in caller.
>> This is a requirement by BPF and it mimics the default behaviour of the
>> clang BPF implementation.
>>
>> gcc/ChangeLog:
>>  * config/bpf/bpf.cc (bpf_use_by_pieces_infrastructure_p): Added
>>  function to bypass default behaviour.
>>  * config/bpf/bpf.h (COMPARE_MAX_PIECES): Defined to 1024 bytes.
>> ---
>>  gcc/config/bpf/bpf.cc | 16 
>>  gcc/config/bpf/bpf.h  |  5 +
>>  2 files changed, 21 insertions(+)
>>
>> diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
>> index a0956a069729..764a3e487cb6 100644
>> --- a/gcc/config/bpf/bpf.cc
>> +++ b/gcc/config/bpf/bpf.cc
>> @@ -1115,6 +1115,22 @@ bpf_small_register_classes_for_mode_p (machine_mode 
>> mode)
>>  #define TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P \
>>bpf_small_register_classes_for_mode_p
>>
>> +static bool
>> +bpf_use_by_pieces_infrastructure_p (unsigned HOST_WIDE_INT size,
>> +unsigned int align ATTRIBUTE_UNUSED,
>> +enum by_pieces_operation op,
>> +bool speed_p)
>> +{
>> +  if (op != COMPARE_BY_PIECES)
>> +return default_use_by_pieces_infrastructure_p (size, align, op, 
>> speed_p);
>> +
>> +  return size <= COMPARE_MAX_PIECES;
>> +}
>> +
>> +#undef TARGET_USE_BY_PIECES_INFRASTRUCTURE_P
>> +#define TARGET_USE_BY_PIECES_INFRASTRUCTURE_P \
>> +  bpf_use_by_pieces_infrastructure_p
>> +
>>  /* Finally, build the GCC target.  */
>>
>>  struct gcc_target targetm = TARGET_INITIALIZER;
>> diff --git a/gcc/config/bpf/bpf.h b/gcc/config/bpf/bpf.h
>> index 82702aa7b6ba..1f177ec4c4ef 100644
>> --- a/gcc/config/bpf/bpf.h
>> +++ b/gcc/config/bpf/bpf.h
>> @@ -489,6 +489,11 @@ enum reg_class
>> locations.  */
>>  #define MOVE_MAX 8
>>
>> +/* Allow upto 1024 bytes moves to occur using by_pieces
>> +   infrastructure.  This mimics clang behaviour when using
>> +   __builtin_memcmp.  */
>> +#define COMPARE_MAX_PIECES 1024
>> +
>>  /* An alias for the machine mode for pointers.  */
>>  #define Pmode DImode


Re: [PATCH] bpf: Corrected condition in core_mark_as_access_index.

2023-11-28 Thread Cupertino Miranda


Thanks! Committed !

David Faust writes:

> On 11/13/23 14:36, Cupertino Miranda wrote:
>> gcc/ChangeLog:
>>  * config/bpf/core-builtins.cc (core_mark_as_access_index):
>>  Corrected check.
>
> OK, thanks.
>
>> ---
>>  gcc/config/bpf/core-builtins.cc | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/gcc/config/bpf/core-builtins.cc 
>> b/gcc/config/bpf/core-builtins.cc
>> index 2ba78d7aed2e..1376c9309035 100644
>> --- a/gcc/config/bpf/core-builtins.cc
>> +++ b/gcc/config/bpf/core-builtins.cc
>> @@ -1611,7 +1611,7 @@ core_mark_as_access_index (tree expr)
>>|| TREE_CODE (expr) == INDIRECT_REF)
>>  expr = TREE_OPERAND (expr, 0);
>>
>> -  if (bpf_enum_mappings->get (expr) == NULL)
>> +  if (core_access_index_map->get (expr) == NULL)
>>  core_access_index_map->put (expr, NULL_TREE);
>>  }
>>


Re: [PATCH] bpf: Delayed the removal of the parser enum plugin handler.

2023-11-28 Thread Cupertino Miranda


Thanks!! Commited !

David Faust writes:

> On 11/13/23 14:35, Cupertino Miranda wrote:
>> The parser plugin handler that is responsible for collecting enum values
>> information was being removed way too early.
>> bpf_resolve_overloaded_core_builtin is called by the parser.
>> It was moved to the function execute_lower_bpf_core.
>>
>
> OK, thanks.
>
>> gcc/ChangeLog:
>>  * config/bpf/core-builtins.cc
>>  (bpf_resolve_overloaded_core_builtin): Removed call.
>>  (execute_lower_bpf_core): Added all to remove_parser_plugin.
>> ---
>>  gcc/config/bpf/core-builtins.cc | 3 +--
>>  1 file changed, 1 insertion(+), 2 deletions(-)
>>
>> diff --git a/gcc/config/bpf/core-builtins.cc 
>> b/gcc/config/bpf/core-builtins.cc
>> index a224847d5d91..2ba78d7aed2e 100644
>> --- a/gcc/config/bpf/core-builtins.cc
>> +++ b/gcc/config/bpf/core-builtins.cc
>> @@ -1473,8 +1473,6 @@ tree
>>  bpf_resolve_overloaded_core_builtin (location_t loc, tree fndecl,
>>   void *arglist)
>>  {
>> -  remove_parser_plugin ();
>> -
>>if (!bpf_require_core_support ())
>>  return error_mark_node;
>>
>> @@ -1688,6 +1686,7 @@ make_gimple_core_safe_access_index (tree *tp,
>>  static unsigned int
>>  execute_lower_bpf_core (void)
>>  {
>> +  remove_parser_plugin ();
>>if (!TARGET_BPF_CORE)
>>  return 0;
>>


[PATCH v2] Fixed problem with BTF defining smaller enums.

2023-11-27 Thread Cupertino Miranda

Hi everyone,

David: Thanks for the v1 review.

This version adds the following;
 - test case,
 - improves condition logic,
 - fixes mask typo.

Looking forward to your review.

v1 at: https://gcc.gnu.org/pipermail/gcc-patches/2023-November/636391.html

Cheers,
Cupertino

commit 3f89d352a4ee90882089142d743f8a748013b5fe
Author: Cupertino Miranda 
Date:   Fri Nov 10 14:02:30 2023 +

Fixed problem with BTF defining smaller enums.

This patch fixes a BTF, which would become invalid when having
smaller then 4 byte definitions of enums.
For example, when using the __attribute__((mode(byte))) in the enum
definition.

Two problems were identified:
 - it would incorrectly create an entry for enum64 when the size of the
   enum was different then 4.
 - it would allocate less then 4 bytes for the value entry in BTF, in
   case the type was smaller.

BTF generated was validated against clang.

gcc/ChangeLog:
* bpfout.cc (btf_calc_num_vbytes): Fixed logic for enum64.
(btf_asm_enum_const): Corrected logic for enum64 and smaller
than 4 bytes values.

gcc/testsuite/ChangeLog:
gcc.dg/debug/btf/btf-enum-small.c: Added test.

diff --git a/gcc/btfout.cc b/gcc/btfout.cc
index e07fed302c24..5f2e99ce4725 100644
--- a/gcc/btfout.cc
+++ b/gcc/btfout.cc
@@ -299,7 +299,7 @@ btf_calc_num_vbytes (ctf_dtdef_ref dtd)
   break;
 
 case BTF_KIND_ENUM:
-  vlen_bytes += (dtd->dtd_data.ctti_size == 0x8)
+  vlen_bytes += (dtd->dtd_data.ctti_size > 4)
 			? vlen * sizeof (struct btf_enum64)
 			: vlen * sizeof (struct btf_enum);
   break;
@@ -914,8 +914,8 @@ btf_asm_enum_const (unsigned int size, ctf_dmdef_t * dmd, unsigned int idx)
 {
   dw2_asm_output_data (4, dmd->dmd_name_offset, "ENUM_CONST '%s' idx=%u",
 		   dmd->dmd_name, idx);
-  if (size == 4)
-dw2_asm_output_data (size, dmd->dmd_value, "bte_value");
+  if (size <= 4)
+dw2_asm_output_data (size < 4 ? 4 : size, dmd->dmd_value, "bte_value");
   else
 {
   dw2_asm_output_data (4, dmd->dmd_value & 0x, "bte_value_lo32");
diff --git a/gcc/testsuite/gcc.dg/debug/btf/btf-enum-small.c b/gcc/testsuite/gcc.dg/debug/btf/btf-enum-small.c
new file mode 100644
index ..eb8a1bd2c438
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/debug/btf/btf-enum-small.c
@@ -0,0 +1,28 @@
+/* Test BTF generation for small enums.  */
+
+/* { dg-do compile } */
+/* { dg-options "-O2 -gbtf -dA" } */
+
+/* { dg-final { scan-assembler-not "bte_value_lo32" } } */
+/* { dg-final { scan-assembler-not "bte_value_hi32" } } */
+/* { dg-final { scan-assembler-times "\[\t \]0x602\[\t \]+\[^\n\]*btt_info" 1 } } */
+/* { dg-final { scan-assembler-times " ENUM_CONST 'eSMALL' idx=0" 1 } } */
+/* { dg-final { scan-assembler-times " ENUM_CONST 'eSMALLY' idx=1" 1 } } */
+/* { dg-final { scan-assembler-times "ascii \"eSMALL.0\"\[\t \]+\[^\n\]*btf_string" 1 } } */
+/* { dg-final { scan-assembler-times "ascii \"eSMALLY.0\"\[\t \]+\[^\n\]*btf_string" 1 } } */
+/* { dg-final { scan-assembler-times "bte_value" 2 } } */
+
+enum smalled_enum
+{
+  eSMALL,
+  eSMALLY,
+} __attribute__((mode(byte)));
+
+struct root_struct {
+  enum smalled_enum esmall;
+};
+
+enum smalled_enum
+foo(struct root_struct *root) {
+  return root->esmall;
+}


bpf: Throw error when external libcalls are generated.

2023-11-27 Thread Cupertino Miranda


User-agent: mu4e 1.4.15; emacs 28.1
Author: Cupertino Miranda 
Hi everyone,

The attached patch is a temporary solution for the lack of proper linker
and external library linking of the eBPF platform.
Any calls created by the compiler, that would usually be defined within
libgcc, will endup being undefined in bpftool, when GCC the compiled
code is passed.

This patch anticipates that error to the compiler, by verifiying if
any of those calls are being generated, and reporting as an error.

Looking forward to your comments.

Cheers,
Cupertino

commit c2110ae497c7ff83c309f172bc265973652b760d
This patch enables errors when external calls are created.

When architectural limitations or usage of builtins implies the compiler
to create function calls to external libraries that implement the
functionality, GCC will now report an error claiming that this function
calls are not compatible with eBPF target.
Examples of those are the usage of __builtin_memmove and a sign division
in BPF ISA v3 or below that will require to call __divdi3.
This is currently an eBPF limitation which does not support linking of
object files but rather "raw" non linked ones. Those object files are
loaded and relocated by libbpf and the kernel.

gcc/ChangeLog:
* config/bpf/bpf.cc (bpf_output_call): Report error in case the
function call is for a builtin.
(bpf_external_libcall): Added target hook to detect and report
error when other external calls that are not builtins.

diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
index 0c9d5257c384..1c84113055b1 100644
--- a/gcc/config/bpf/bpf.cc
+++ b/gcc/config/bpf/bpf.cc
@@ -744,6 +744,15 @@ bpf_output_call (rtx target)
xops[0] = GEN_INT (TREE_INT_CST_LOW (TREE_VALUE (attr_args)));
output_asm_insn ("call\t%0", xops);
  }
+   else if (fndecl_built_in_p (decl))
+ {
+   /* For now lets report this as an error while we are not able to
+  link eBPF object files.  In particular with libgcc.  */
+   tree name = DECL_NAME (decl);
+   error ("call to external builtin %s in function, which is not 
supported by "
+  "eBPF", name != NULL_TREE ? IDENTIFIER_POINTER (name) : 
"(anon)");
+   output_asm_insn ("call 0", NULL);
+ }
else
  output_asm_insn ("call\t%0", );

@@ -763,6 +772,18 @@ bpf_output_call (rtx target)
   return "";
 }

+static void
+bpf_external_libcall (rtx fun)
+{
+  tree decl = SYMBOL_REF_DECL (fun);
+  tree name = DECL_NAME (decl);
+  error ("call to external libcall %s in function, which is not supported by "
+"eBPF", name != NULL_TREE ? IDENTIFIER_POINTER (name) : "(anon)");
+}
+
+#undef  TARGET_ASM_EXTERNAL_LIBCALL
+#define TARGET_ASM_EXTERNAL_LIBCALL bpf_external_libcall
+
 /* Print register name according to assembly dialect.  In normal
syntax registers are printed like %rN where N is the register
number.
diff --git a/gcc/testsuite/gcc.target/bpf/atomic-cmpxchg-2.c 
b/gcc/testsuite/gcc.target/bpf/atomic-cmpxchg-2.c
index 4036570ac601..fec720584e48 100644
--- a/gcc/testsuite/gcc.target/bpf/atomic-cmpxchg-2.c
+++ b/gcc/testsuite/gcc.target/bpf/atomic-cmpxchg-2.c
@@ -6,7 +6,7 @@ foo (int *p, int *expected, int desired)
 {
   return __atomic_compare_exchange (p, expected, , 0,
__ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
-}
+} /* { dg-error "call to external builtin" } */

 int
 foo64 (long *p, long *expected, long desired)
diff --git a/gcc/testsuite/gcc.target/bpf/atomic-fetch-op-3.c 
b/gcc/testsuite/gcc.target/bpf/atomic-fetch-op-3.c
index 044a2f76474b..ea1b8e48928a 100644
--- a/gcc/testsuite/gcc.target/bpf/atomic-fetch-op-3.c
+++ b/gcc/testsuite/gcc.target/bpf/atomic-fetch-op-3.c
@@ -9,7 +9,7 @@ long
 test_atomic_fetch_add (long x)
 {
   return __atomic_fetch_add (, x, __ATOMIC_ACQUIRE);
-}
+} /* { dg-error "call to external builtin" } */

 long
 test_atomic_fetch_sub (long x)
diff --git a/gcc/testsuite/gcc.target/bpf/atomic-op-3.c 
b/gcc/testsuite/gcc.target/bpf/atomic-op-3.c
index b2ce28926347..fefafd6b748f 100644
--- a/gcc/testsuite/gcc.target/bpf/atomic-op-3.c
+++ b/gcc/testsuite/gcc.target/bpf/atomic-op-3.c
@@ -20,7 +20,7 @@ void
 test_atomic_and (int x)
 {
   __atomic_and_fetch (, x, __ATOMIC_ACQUIRE);
-}
+} /* { dg-error "call to external builtin" } */

 void
 test_atomic_nand (int x)
diff --git a/gcc/testsuite/gcc.target/bpf/atomic-xchg-2.c 
b/gcc/testsuite/gcc.target/bpf/atomic-xchg-2.c
index 3b6324e966b8..eab695bf388c 100644
--- a/gcc/testsuite/gcc.target/bpf/atomic-xchg-2.c
+++ b/gcc/testsuite/gcc.target/bpf/atomic-xchg-2.c
@@ -7,7 +7,7 @@ int foo (int *p, int *new)
   int old;
   __atomic_exchange (p, new, , __ATOMIC_RELAXED);
   return old;
-}
+} /* { dg-erro

bpf: Throw error when external libcalls are generated.

2023-11-24 Thread Cupertino Miranda

Hi everyone,

The attached patch is a temporary solution for the lack of proper linker
and external library linking of the eBPF platform.
Any calls created by the compiler, that would usually be defined within
libgcc, will endup being undefined in bpftool, when GCC the compiled
code is passed.

This patch anticipates that error to the compiler, by verifiying if
any of those calls are being generated, and reporting as an error.

Looking forward to your comments.

Cheers,
Cupertino

commit c2110ae497c7ff83c309f172bc265973652b760d
Author: Cupertino Miranda 
Date:   Thu Nov 23 22:28:01 2023 +

This patch enables errors when external calls are created.

When architectural limitations or usage of builtins implies the compiler
to create function calls to external libraries that implement the
functionality, GCC will now report an error claiming that this function
calls are not compatible with eBPF target.
Examples of those are the usage of __builtin_memmove and a sign division
in BPF ISA v3 or below that will require to call __divdi3.
This is currently an eBPF limitation which does not support linking of
object files but rather "raw" non linked ones. Those object files are
loaded and relocated by libbpf and the kernel.

gcc/ChangeLog:
* config/bpf/bpf.cc (bpf_output_call): Report error in case the
function call is for a builtin.
(bpf_external_libcall): Added target hook to detect and report
error when other external calls that are not builtins.

diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
index 0c9d5257c384..1c84113055b1 100644
--- a/gcc/config/bpf/bpf.cc
+++ b/gcc/config/bpf/bpf.cc
@@ -744,6 +744,15 @@ bpf_output_call (rtx target)
 	xops[0] = GEN_INT (TREE_INT_CST_LOW (TREE_VALUE (attr_args)));
 	output_asm_insn ("call\t%0", xops);
 	  }
+	else if (fndecl_built_in_p (decl))
+	  {
+	/* For now lets report this as an error while we are not able to
+	   link eBPF object files.  In particular with libgcc.  */
+	tree name = DECL_NAME (decl);
+	error ("call to external builtin %s in function, which is not supported by "
+		   "eBPF", name != NULL_TREE ? IDENTIFIER_POINTER (name) : "(anon)");
+	output_asm_insn ("call 0", NULL);
+	  }
 	else
 	  output_asm_insn ("call\t%0", );
 
@@ -763,6 +772,18 @@ bpf_output_call (rtx target)
   return "";
 }
 
+static void
+bpf_external_libcall (rtx fun)
+{
+  tree decl = SYMBOL_REF_DECL (fun);
+  tree name = DECL_NAME (decl);
+  error ("call to external libcall %s in function, which is not supported by "
+	 "eBPF", name != NULL_TREE ? IDENTIFIER_POINTER (name) : "(anon)");
+}
+
+#undef  TARGET_ASM_EXTERNAL_LIBCALL
+#define TARGET_ASM_EXTERNAL_LIBCALL bpf_external_libcall
+
 /* Print register name according to assembly dialect.  In normal
syntax registers are printed like %rN where N is the register
number.
diff --git a/gcc/testsuite/gcc.target/bpf/atomic-cmpxchg-2.c b/gcc/testsuite/gcc.target/bpf/atomic-cmpxchg-2.c
index 4036570ac601..fec720584e48 100644
--- a/gcc/testsuite/gcc.target/bpf/atomic-cmpxchg-2.c
+++ b/gcc/testsuite/gcc.target/bpf/atomic-cmpxchg-2.c
@@ -6,7 +6,7 @@ foo (int *p, int *expected, int desired)
 {
   return __atomic_compare_exchange (p, expected, , 0,
 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
-}
+} /* { dg-error "call to external builtin" } */
 
 int
 foo64 (long *p, long *expected, long desired)
diff --git a/gcc/testsuite/gcc.target/bpf/atomic-fetch-op-3.c b/gcc/testsuite/gcc.target/bpf/atomic-fetch-op-3.c
index 044a2f76474b..ea1b8e48928a 100644
--- a/gcc/testsuite/gcc.target/bpf/atomic-fetch-op-3.c
+++ b/gcc/testsuite/gcc.target/bpf/atomic-fetch-op-3.c
@@ -9,7 +9,7 @@ long
 test_atomic_fetch_add (long x)
 {
   return __atomic_fetch_add (, x, __ATOMIC_ACQUIRE);
-}
+} /* { dg-error "call to external builtin" } */
 
 long
 test_atomic_fetch_sub (long x)
diff --git a/gcc/testsuite/gcc.target/bpf/atomic-op-3.c b/gcc/testsuite/gcc.target/bpf/atomic-op-3.c
index b2ce28926347..fefafd6b748f 100644
--- a/gcc/testsuite/gcc.target/bpf/atomic-op-3.c
+++ b/gcc/testsuite/gcc.target/bpf/atomic-op-3.c
@@ -20,7 +20,7 @@ void
 test_atomic_and (int x)
 {
   __atomic_and_fetch (, x, __ATOMIC_ACQUIRE);
-}
+} /* { dg-error "call to external builtin" } */
 
 void
 test_atomic_nand (int x)
diff --git a/gcc/testsuite/gcc.target/bpf/atomic-xchg-2.c b/gcc/testsuite/gcc.target/bpf/atomic-xchg-2.c
index 3b6324e966b8..eab695bf388c 100644
--- a/gcc/testsuite/gcc.target/bpf/atomic-xchg-2.c
+++ b/gcc/testsuite/gcc.target/bpf/atomic-xchg-2.c
@@ -7,7 +7,7 @@ int foo (int *p, int *new)
   int old;
   __atomic_exchange (p, new, , __ATOMIC_RELAXED);
   return old;
-}
+} /* { dg-error "call to external builtin" } */
 
 int foo64 (long *p, long *new)
 {
diff --git a/gcc/testsuite/gcc.target/b

[PATCH] Fixed problem with BTF defining smaller enums.

2023-11-13 Thread Cupertino Miranda
This patch fixes a BTF, which would become invalid when having
smaller then 4 byte definitions of enums.
For example, when using the __attribute__((mode(byte))) in the enum
definition.

Two problems were identified:
 - it would incorrectly create an entry for enum64 when the size of the
   enum was different then 4.
 - it would allocate less then 4 bytes for the value entry in BTF, in
   case the type was smaller.

BTF generated was validated against clang.

gcc/ChangeLog:
* bpfout.cc (btf_calc_num_vbytes): Fixed logic for enum64.
(btf_asm_enum_const): Corrected logic for enum64 and smaller
than 4 bytes values.
---
 gcc/btfout.cc | 10 +-
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/gcc/btfout.cc b/gcc/btfout.cc
index e07fed302c24..d2263ec6eec3 100644
--- a/gcc/btfout.cc
+++ b/gcc/btfout.cc
@@ -299,7 +299,7 @@ btf_calc_num_vbytes (ctf_dtdef_ref dtd)
   break;
 
 case BTF_KIND_ENUM:
-  vlen_bytes += (dtd->dtd_data.ctti_size == 0x8)
+  vlen_bytes += (dtd->dtd_data.ctti_size > 4)
? vlen * sizeof (struct btf_enum64)
: vlen * sizeof (struct btf_enum);
   break;
@@ -914,13 +914,13 @@ btf_asm_enum_const (unsigned int size, ctf_dmdef_t * dmd, 
unsigned int idx)
 {
   dw2_asm_output_data (4, dmd->dmd_name_offset, "ENUM_CONST '%s' idx=%u",
   dmd->dmd_name, idx);
-  if (size == 4)
-dw2_asm_output_data (size, dmd->dmd_value, "bte_value");
-  else
+  if (size > 4)
 {
-  dw2_asm_output_data (4, dmd->dmd_value & 0x, "bte_value_lo32");
+  dw2_asm_output_data (4, dmd->dmd_value & 0xfffe, "bte_value_lo32");
   dw2_asm_output_data (4, (dmd->dmd_value >> 32) & 0x, 
"bte_value_hi32");
 }
+  else
+dw2_asm_output_data (size < 4 ? 4 : size, dmd->dmd_value, "bte_value");
 }
 
 /* Asm'out a function parameter description following a BTF_KIND_FUNC_PROTO.  
*/
-- 
2.30.2



[PATCH] bpf: Forces __buildin_memcmp not to generate a call upto 1024 bytes.

2023-11-13 Thread Cupertino Miranda
This patch forces __builtin_memcmp calls upto data sizes of 1024 to
become inline in caller.
This is a requirement by BPF and it mimics the default behaviour of the
clang BPF implementation.

gcc/ChangeLog:
* config/bpf/bpf.cc (bpf_use_by_pieces_infrastructure_p): Added
function to bypass default behaviour.
* config/bpf/bpf.h (COMPARE_MAX_PIECES): Defined to 1024 bytes.
---
 gcc/config/bpf/bpf.cc | 16 
 gcc/config/bpf/bpf.h  |  5 +
 2 files changed, 21 insertions(+)

diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
index a0956a069729..764a3e487cb6 100644
--- a/gcc/config/bpf/bpf.cc
+++ b/gcc/config/bpf/bpf.cc
@@ -1115,6 +1115,22 @@ bpf_small_register_classes_for_mode_p (machine_mode mode)
 #define TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P \
   bpf_small_register_classes_for_mode_p
 
+static bool
+bpf_use_by_pieces_infrastructure_p (unsigned HOST_WIDE_INT size,
+   unsigned int align ATTRIBUTE_UNUSED,
+   enum by_pieces_operation op,
+   bool speed_p)
+{
+  if (op != COMPARE_BY_PIECES)
+return default_use_by_pieces_infrastructure_p (size, align, op, speed_p);
+
+  return size <= COMPARE_MAX_PIECES;
+}
+
+#undef TARGET_USE_BY_PIECES_INFRASTRUCTURE_P
+#define TARGET_USE_BY_PIECES_INFRASTRUCTURE_P \
+  bpf_use_by_pieces_infrastructure_p
+
 /* Finally, build the GCC target.  */
 
 struct gcc_target targetm = TARGET_INITIALIZER;
diff --git a/gcc/config/bpf/bpf.h b/gcc/config/bpf/bpf.h
index 82702aa7b6ba..1f177ec4c4ef 100644
--- a/gcc/config/bpf/bpf.h
+++ b/gcc/config/bpf/bpf.h
@@ -489,6 +489,11 @@ enum reg_class
locations.  */
 #define MOVE_MAX 8
 
+/* Allow upto 1024 bytes moves to occur using by_pieces
+   infrastructure.  This mimics clang behaviour when using
+   __builtin_memcmp.  */
+#define COMPARE_MAX_PIECES 1024
+
 /* An alias for the machine mode for pointers.  */
 #define Pmode DImode
 
-- 
2.30.2



[PATCH] bpf: Corrected condition in core_mark_as_access_index.

2023-11-13 Thread Cupertino Miranda
gcc/ChangeLog:
* config/bpf/core-builtins.cc (core_mark_as_access_index):
Corrected check.
---
 gcc/config/bpf/core-builtins.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc
index 2ba78d7aed2e..1376c9309035 100644
--- a/gcc/config/bpf/core-builtins.cc
+++ b/gcc/config/bpf/core-builtins.cc
@@ -1611,7 +1611,7 @@ core_mark_as_access_index (tree expr)
   || TREE_CODE (expr) == INDIRECT_REF)
 expr = TREE_OPERAND (expr, 0);
 
-  if (bpf_enum_mappings->get (expr) == NULL)
+  if (core_access_index_map->get (expr) == NULL)
 core_access_index_map->put (expr, NULL_TREE);
 }
 
-- 
2.30.2



[PATCH] bpf: Delayed the removal of the parser enum plugin handler.

2023-11-13 Thread Cupertino Miranda
The parser plugin handler that is responsible for collecting enum values
information was being removed way too early.
bpf_resolve_overloaded_core_builtin is called by the parser.
It was moved to the function execute_lower_bpf_core.

gcc/ChangeLog:
* config/bpf/core-builtins.cc
(bpf_resolve_overloaded_core_builtin): Removed call.
(execute_lower_bpf_core): Added all to remove_parser_plugin.
---
 gcc/config/bpf/core-builtins.cc | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc
index a224847d5d91..2ba78d7aed2e 100644
--- a/gcc/config/bpf/core-builtins.cc
+++ b/gcc/config/bpf/core-builtins.cc
@@ -1473,8 +1473,6 @@ tree
 bpf_resolve_overloaded_core_builtin (location_t loc, tree fndecl,
 void *arglist)
 {
-  remove_parser_plugin ();
-
   if (!bpf_require_core_support ())
 return error_mark_node;
 
@@ -1688,6 +1686,7 @@ make_gimple_core_safe_access_index (tree *tp,
 static unsigned int
 execute_lower_bpf_core (void)
 {
+  remove_parser_plugin ();
   if (!TARGET_BPF_CORE)
 return 0;
 
-- 
2.30.2



Re: [PATCH v5] bpf: Improvements in CO-RE builtins implementation.

2023-10-31 Thread Cupertino Miranda



> On 10/31/23 09:58, David Faust wrote:
>> Hi Cupertino,
>>
>> On 10/30/23 12:39, Cupertino Miranda wrote:
>>>
>>> Hi everyone,
>>>
>>> Please find a new version for the review as inline attachment.
>>>
>>> Best regards,
>>> Cupertino
>>>
>>
>> This version LGTM.
>> Thanks!
>
> OK for trunk.
Pushed !

> Thanks.
Thanks,
Cupertino

>
>>
>>>
>>> Changes from v4:
>>>  - Implemented TARGET_DELEGITIMIZE_ADDRESS target hook as the proper
>>>  solution to the the warning for UNSPEC_CORE_RELOC being
>>>  non-delegitimize.
>>>
>>
>>


[PATCH v5] bpf: Improvements in CO-RE builtins implementation.

2023-10-30 Thread Cupertino Miranda

Hi everyone,

Please find a new version for the review as inline attachment.

Best regards,
Cupertino


Changes from v4:
 - Implemented TARGET_DELEGITIMIZE_ADDRESS target hook as the proper
 solution to the the warning for UNSPEC_CORE_RELOC being
 non-delegitimize.

commit 5b45d225c473827b5ef7001e5b24df74d27953ff
Author: Cupertino Miranda 
Date:   Tue Aug 8 09:22:41 2023 +0100

bpf: Improvements in CO-RE builtins implementation.

This patch moved the processing of attribute preserve_access_index to
its own independent pass in a gimple lowering pass.
This approach is more consistent with the implementation of the CO-RE
builtins when used explicitly in the code.  The attributed type accesses
are now early converted to __builtin_core_reloc builtin instead of being
kept as an expression in code through out all of the middle-end.
This disables the compiler to optimize out or manipulate the expression
using the local defined type, instead of assuming nothing is known about
this expression, as it should be the case in all of the CO-RE
relocations.

In the process, also the __builtin_preserve_access_index has been
improved to generate code for more complex expressions that would
require more then one CO-RE relocation.
This turned out to be a requirement, since bpf-next selftests would rely on
loop unrolling in order to convert an undefined index array access into a
defined one. This seemed extreme to expect for the unroll to happen, and for
that reason GCC still generates correct code in such scenarios, even when index
access is never predictable or unrolling does not occur.

gcc/ChangeLog:
* config/bpf/bpf-passes.def (pass_lower_bpf_core): Added pass.
* config/bpf/bpf-protos.h: Added prototype for new pass.
* config/bpf/bpf.cc (bpf_delegitimize_address): New function.
* config/bpf/bpf.md (mov_reloc_core): Prefixed
name with '*'.
* config/bpf/core-builtins.cc (cr_builtins) Added access_node to
struct.
(is_attr_preserve_access): Improved check.
(core_field_info): Make use of root_for_core_field_info
function.
(process_field_expr): Adapted to new functions.
(pack_type): Small improvement.
(bpf_handle_plugin_finish_type): Adapted to GTY(()).
(bpf_init_core_builtins): Changed to new function names.
(construct_builtin_core_reloc): Improved implementation.
(bpf_resolve_overloaded_core_builtin): Changed how
__builtin_preserve_access_index is converted.
(compute_field_expr): Corrected implementation. Added
access_node argument.
(bpf_core_get_index): Added valid argument.
(root_for_core_field_info, pack_field_expr)
(core_expr_with_field_expr_plus_base, make_core_safe_access_index)
(replace_core_access_index_comp_expr, maybe_get_base_for_field_expr)
(core_access_clean, core_is_access_index, core_mark_as_access_index)
(make_gimple_core_safe_access_index, execute_lower_bpf_core)
(make_pass_lower_bpf_core): Added functions.
(pass_data_lower_bpf_core): New pass struct.
(pass_lower_bpf_core): New gimple_opt_pass class.
(pack_field_expr_for_preserve_field)
(bpf_replace_core_move_operands): Removed function.
(bpf_enum_value_kind): Added GTY(()).
* config/bpf/core-builtins.h (bpf_field_info_kind, bpf_type_id_kind)
(bpf_type_info_kind, bpf_enum_value_kind): New enum.
* config/bpf/t-bpf: Added pass bpf-passes.def to PASSES_EXTRA.

gcc/testsuite/ChangeLog:
* gcc.target/bpf/core-attr-5.c: New test.
* gcc.target/bpf/core-attr-6.c: New test.
* gcc.target/bpf/core-builtin-1.c: Corrected
* gcc.target/bpf/core-builtin-enumvalue-opt.c: Corrected regular
expression.
* gcc.target/bpf/core-builtin-enumvalue.c: Corrected regular
expression.
* gcc.target/bpf/core-builtin-exprlist-1.c: New test.
* gcc.target/bpf/core-builtin-exprlist-2.c: New test.
* gcc.target/bpf/core-builtin-exprlist-3.c: New test.
* gcc.target/bpf/core-builtin-exprlist-4.c: New test.
* gcc.target/bpf/core-builtin-fieldinfo-offset-1.c: Extra tests

diff --git a/gcc/config/bpf/bpf-passes.def b/gcc/config/bpf/bpf-passes.def
new file mode 100644
index ..0ec20eac965d
--- /dev/null
+++ b/gcc/config/bpf/bpf-passes.def
@@ -0,0 +1,20 @@
+/* Declaration of target-specific passes for eBPF.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+
+   This file is part of GCC.
+
+   GCC is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as published by
+   the Free Software

Re: [PATCH v4] bpf: Improvements in CO-RE builtins implementation.

2023-10-27 Thread Cupertino Miranda


Hi David,

David Faust writes:

> On 10/26/23 08:08, Cupertino Miranda wrote:
>>
>> Changes from v1:
>>  - Fixed Davids remarks on initial patch.
>>  - Fixed mistake with deleted '*'.
>>
>> Changes from v2:
>>  - Reversed return value for bpf_const_not_ok_for_debug_p function.
>
> Hmm..
>
>> +static bool
>> +bpf_const_not_ok_for_debug_p (rtx p)
>> +{
>> +  if (GET_CODE (p) == UNSPEC
>> +  && XINT (p, 1) == UNSPEC_CORE_RELOC)
>> +return false;
>> +
>> +  return true;
>> +}
>> +
>> +#undef TARGET_CONST_NOT_OK_FOR_DEBUG_P
>> +#define TARGET_CONST_NOT_OK_FOR_DEBUG_P bpf_const_not_ok_for_debug_p
>
>  -- Target Hook: bool TARGET_CONST_NOT_OK_FOR_DEBUG_P (rtx X)
>  This hook should return true if X should not be emitted into debug
>  sections.
>
> As written now, won't this cause all ordinary (non-UNSPEC_CORE_RELOC)
> consts to get rejected for debug? ("regular" debug i.e. DWARF, not to
> be confused with the BTF.ext holding CO-RE relocs).
>
> I see other targets implementing the hook returning true only in
> specific cases and false otherwise.  The implementation in v1 makes
> more sense to me.  Could you explain why flip the return value?
It turns out that defining this hook is not the proper solution.
I am trying a different approach which I believe is better.

Thanks,
Cupertino

>>
>> Changes from v3:
>>  - Fixed ICE in two bpf-next tests:
>>  -  if (!wi->is_lhs)
>>  -   core_mark_as_access_index (gimple_get_lhs (wi->stmt));
>>  +  tree lhs;
>>  +  if (!wi->is_lhs
>>  + && (lhs = gimple_get_lhs (wi->stmt)) != NULL_TREE)
>>  +   core_mark_as_access_index (lhs);
>>


[PATCH v4] bpf: Improvements in CO-RE builtins implementation.

2023-10-26 Thread Cupertino Miranda

Changes from v1:
 - Fixed Davids remarks on initial patch.
 - Fixed mistake with deleted '*'.

Changes from v2:
 - Reversed return value for bpf_const_not_ok_for_debug_p function.

Changes from v3:
 - Fixed ICE in two bpf-next tests:
 -  if (!wi->is_lhs)
 -   core_mark_as_access_index (gimple_get_lhs (wi->stmt));
 +  tree lhs;
 +  if (!wi->is_lhs
 + && (lhs = gimple_get_lhs (wi->stmt)) != NULL_TREE)
 +   core_mark_as_access_index (lhs);

commit b525feaeb159f55c2a6db1cb4246bd027351f2c5
Author: Cupertino Miranda 
Date:   Tue Aug 8 09:22:41 2023 +0100

bpf: Improvements in CO-RE builtins implementation.

This patch moved the processing of attribute preserve_access_index to
its own independent pass in a gimple lowering pass.
This approach is more consistent with the implementation of the CO-RE
builtins when used explicitly in the code.  The attributed type accesses
are now early converted to __builtin_core_reloc builtin instead of being
kept as an expression in code through out all of the middle-end.
This disables the compiler to optimize out or manipulate the expression
using the local defined type, instead of assuming nothing is known about
this expression, as it should be the case in all of the CO-RE
relocations.

In the process, also the __builtin_preserve_access_index has been
improved to generate code for more complex expressions that would
require more then one CO-RE relocation.
This turned out to be a requirement, since bpf-next selftests would rely on
loop unrolling in order to convert an undefined index array access into a
defined one. This seemed extreme to expect for the unroll to happen, and for
that reason GCC still generates correct code in such scenarios, even when index
access is never predictable or unrolling does not occur.

gcc/ChangeLog:
* config/bpf/bpf-passes.def (pass_lower_bpf_core): Added pass.
* config/bpf/bpf-protos.h: Added prototype for new pass.
* config/bpf/bpf.cc (bpf_const_not_ok_for_debug_p): New function.
* config/bpf/bpf.md (mov_reloc_core): Prefixed
name with '*'.
* config/bpf/core-builtins.cc (cr_builtins) Added access_node to
struct.
(is_attr_preserve_access): Improved check.
(core_field_info): Make use of root_for_core_field_info
function.
(process_field_expr): Adapted to new functions.
(pack_type): Small improvement.
(bpf_handle_plugin_finish_type): Adapted to GTY(()).
(bpf_init_core_builtins): Changed to new function names.
(construct_builtin_core_reloc): Improved implementation.
(bpf_resolve_overloaded_core_builtin): Changed how
__builtin_preserve_access_index is converted.
(compute_field_expr): Corrected implementation. Added
access_node argument.
(bpf_core_get_index): Added valid argument.
(root_for_core_field_info, pack_field_expr)
(core_expr_with_field_expr_plus_base, make_core_safe_access_index)
(replace_core_access_index_comp_expr, maybe_get_base_for_field_expr)
(core_access_clean, core_is_access_index, core_mark_as_access_index)
(make_gimple_core_safe_access_index, execute_lower_bpf_core)
(make_pass_lower_bpf_core): Added functions.
(pass_data_lower_bpf_core): New pass struct.
(pass_lower_bpf_core): New gimple_opt_pass class.
(pack_field_expr_for_preserve_field)
(bpf_replace_core_move_operands): Removed function.
(bpf_enum_value_kind): Added GTY(()).
* config/bpf/core-builtins.h (bpf_field_info_kind, bpf_type_id_kind)
(bpf_type_info_kind, bpf_enum_value_kind): New enum.
* config/bpf/t-bpf: Added pass bpf-passes.def to PASSES_EXTRA.

gcc/testsuite/ChangeLog:
* gcc.target/bpf/core-attr-5.c: New test.
* gcc.target/bpf/core-attr-6.c: New test.
* gcc.target/bpf/core-builtin-1.c: Corrected
* gcc.target/bpf/core-builtin-enumvalue-opt.c: Corrected regular
expression.
* gcc.target/bpf/core-builtin-enumvalue.c: Corrected regular
expression.
* gcc.target/bpf/core-builtin-exprlist-1.c: New test.
* gcc.target/bpf/core-builtin-exprlist-2.c: New test.
* gcc.target/bpf/core-builtin-exprlist-3.c: New test.
* gcc.target/bpf/core-builtin-exprlist-4.c: New test.
* gcc.target/bpf/core-builtin-fieldinfo-offset-1.c: Extra tests

diff --git a/gcc/config/bpf/bpf-passes.def b/gcc/config/bpf/bpf-passes.def
new file mode 100644
index ..0ec20eac965d
--- /dev/null
+++ b/gcc/config/bpf/bpf-passes.def
@@ -0,0 +1,20 @@
+/* Declaration of target-specific passes for eB

Re: [PATCH v3] bpf: Improvements in CO-RE builtins implementation.

2023-10-26 Thread Cupertino Miranda

Changes from v2:
 - Reversed return value for bpf_const_not_ok_for_debug_p function.


commit 3a0b09273727a49fab7461d059d504899bb6556d
Author: Cupertino Miranda 
Date:   Tue Aug 8 09:22:41 2023 +0100

bpf: Improvements in CO-RE builtins implementation.

This patch moved the processing of attribute preserve_access_index to
its own independent pass in a gimple lowering pass.
This approach is more consistent with the implementation of the CO-RE
builtins when used explicitly in the code.  The attributed type accesses
are now early converted to __builtin_core_reloc builtin instead of being
kept as an expression in code through out all of the middle-end.
This disables the compiler to optimize out or manipulate the expression
using the local defined type, instead of assuming nothing is known about
this expression, as it should be the case in all of the CO-RE
relocations.

In the process, also the __builtin_preserve_access_index has been
improved to generate code for more complex expressions that would
require more then one CO-RE relocation.
This turned out to be a requirement, since bpf-next selftests would rely on
loop unrolling in order to convert an undefined index array access into a
defined one. This seemed extreme to expect for the unroll to happen, and for
that reason GCC still generates correct code in such scenarios, even when index
access is never predictable or unrolling does not occur.

gcc/ChangeLog:
* config/bpf/bpf-passes.def (pass_lower_bpf_core): Added pass.
* config/bpf/bpf-protos.h: Added prototype for new pass.
* config/bpf/bpf.cc (bpf_const_not_ok_for_debug_p): New function.
* config/bpf/bpf.md (mov_reloc_core): Prefixed
name with '*'.
* config/bpf/core-builtins.cc (cr_builtins) Added access_node to
struct.
(is_attr_preserve_access): Improved check.
(core_field_info): Make use of root_for_core_field_info
function.
(process_field_expr): Adapted to new functions.
(pack_type): Small improvement.
(bpf_handle_plugin_finish_type): Adapted to GTY(()).
(bpf_init_core_builtins): Changed to new function names.
(construct_builtin_core_reloc): Improved implementation.
(bpf_resolve_overloaded_core_builtin): Changed how
__builtin_preserve_access_index is converted.
(compute_field_expr): Corrected implementation. Added
access_node argument.
(bpf_core_get_index): Added valid argument.
(root_for_core_field_info, pack_field_expr)
(core_expr_with_field_expr_plus_base, make_core_safe_access_index)
(replace_core_access_index_comp_expr, maybe_get_base_for_field_expr)
(core_access_clean, core_is_access_index, core_mark_as_access_index)
(make_gimple_core_safe_access_index, execute_lower_bpf_core)
(make_pass_lower_bpf_core): Added functions.
(pass_data_lower_bpf_core): New pass struct.
(pass_lower_bpf_core): New gimple_opt_pass class.
(pack_field_expr_for_preserve_field)
(bpf_replace_core_move_operands): Removed function.
(bpf_enum_value_kind): Added GTY(()).
* config/bpf/core-builtins.h (bpf_field_info_kind, bpf_type_id_kind)
(bpf_type_info_kind, bpf_enum_value_kind): New enum.
* config/bpf/t-bpf: Added pass bpf-passes.def to PASSES_EXTRA.

gcc/testsuite/ChangeLog:
* gcc.target/bpf/core-attr-5.c: New test.
* gcc.target/bpf/core-attr-6.c: New test.
* gcc.target/bpf/core-builtin-1.c: Corrected
* gcc.target/bpf/core-builtin-enumvalue-opt.c: Corrected regular
expression.
* gcc.target/bpf/core-builtin-enumvalue.c: Corrected regular
expression.
* gcc.target/bpf/core-builtin-exprlist-1.c: New test.
* gcc.target/bpf/core-builtin-exprlist-2.c: New test.
* gcc.target/bpf/core-builtin-exprlist-3.c: New test.
* gcc.target/bpf/core-builtin-exprlist-4.c: New test.
* gcc.target/bpf/core-builtin-fieldinfo-offset-1.c: Extra tests

diff --git a/gcc/config/bpf/bpf-passes.def b/gcc/config/bpf/bpf-passes.def
new file mode 100644
index ..0ec20eac965d
--- /dev/null
+++ b/gcc/config/bpf/bpf-passes.def
@@ -0,0 +1,20 @@
+/* Declaration of target-specific passes for eBPF.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+
+   This file is part of GCC.
+
+   GCC is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   GCC is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even

Re: [PATCH v2] bpf: Improvements in CO-RE builtins implementation.

2023-10-26 Thread Cupertino Miranda

Hi David,

Please find the new version inline right after the inline reply.

>> gcc/ChangeLog:
>> * config/bpf/bpf-passes.def (pass_lower_bpf_core): Added pass.
>
> It may only be due to how the patch is formatted in the attachment
> (everything above the first diff seems to be indented?), but each entry
> here should start with a tab rather than spaces.
>
> Please double-check with contrib/gcc-changelog/git_check_commit.py, it
> will complain if the indentation is wrong.
It did pass the check. I think it might be because of inline attachment.

>   * config/bpf/core-builtins.cc (cr_builtins, is_attr_preserve_access)
>   (core_field_info, bpf_core_get_index): Changed to do blah.
>
> Same thing about the multi-line entry within a single () pair.
>
Corrected!

>> +/* Declaration of target-specific passes for eBPF.
>> +   Copyright (C) 2021-2023 Free Software Foundation, Inc.
>
> This file is new, so just 2023 no?
Yes !

>> -  const char * section_name;
>> +  const char   section_name;
>
> Why this change from char* to char?
>
> This change doesn't make sense given the expressions assigned to
> the variable.  In fact, this does not compile.
>
It was an editor mistake ... caused by the editor sitting in a chair. ;-)
Apologies for this mistake and thanks for catching it.

Thanks,
Cupertino

commit 687b67d82c7d8c6cf5b0e3a9dc61fd4f1e1a1fbb
Author: Cupertino Miranda 
Date:   Tue Aug 8 09:22:41 2023 +0100

bpf: Improvements in CO-RE builtins implementation.

This patch moved the processing of attribute preserve_access_index to
its own independent pass in a gimple lowering pass.
This approach is more consistent with the implementation of the CO-RE
builtins when used explicitly in the code.  The attributed type accesses
are now early converted to __builtin_core_reloc builtin instead of being
kept as an expression in code through out all of the middle-end.
This disables the compiler to optimize out or manipulate the expression
using the local defined type, instead of assuming nothing is known about
this expression, as it should be the case in all of the CO-RE
relocations.

In the process, also the __builtin_preserve_access_index has been
improved to generate code for more complex expressions that would
require more then one CO-RE relocation.
This turned out to be a requirement, since bpf-next selftests would rely on
loop unrolling in order to convert an undefined index array access into a
defined one. This seemed extreme to expect for the unroll to happen, and for
that reason GCC still generates correct code in such scenarios, even when index
access is never predictable or unrolling does not occur.

gcc/ChangeLog:
* config/bpf/bpf-passes.def (pass_lower_bpf_core): Added pass.
* config/bpf/bpf-protos.h: Added prototype for new pass.
* config/bpf/bpf.cc (bpf_const_not_ok_for_debug_p): New function.
* config/bpf/bpf.md (mov_reloc_core): Prefixed
name with '*'.
* config/bpf/core-builtins.cc (cr_builtins) Added access_node to
struct.
(is_attr_preserve_access): Improved check.
(core_field_info): Make use of root_for_core_field_info
function.
(process_field_expr): Adapted to new functions.
(pack_type): Small improvement.
(bpf_handle_plugin_finish_type): Adapted to GTY(()).
(bpf_init_core_builtins): Changed to new function names.
(construct_builtin_core_reloc): Improved implementation.
(bpf_resolve_overloaded_core_builtin): Changed how
__builtin_preserve_access_index is converted.
(compute_field_expr): Corrected implementation. Added
access_node argument.
(bpf_core_get_index): Added valid argument.
(root_for_core_field_info, pack_field_expr)
(core_expr_with_field_expr_plus_base, make_core_safe_access_index)
(replace_core_access_index_comp_expr, maybe_get_base_for_field_expr)
(core_access_clean, core_is_access_index, core_mark_as_access_index)
(make_gimple_core_safe_access_index, execute_lower_bpf_core)
(make_pass_lower_bpf_core): Added functions.
(pass_data_lower_bpf_core): New pass struct.
(pass_lower_bpf_core): New gimple_opt_pass class.
(pack_field_expr_for_preserve_field)
(bpf_replace_core_move_operands): Removed function.
(bpf_enum_value_kind): Added GTY(()).
* config/bpf/core-builtins.h (bpf_field_info_kind, bpf_type_id_kind)
(bpf_type_info_kind, bpf_enum_value_kind): New enum.
* config/bpf/t-bpf: Added pass bpf-passes.def to PASSES_EXTRA.

gcc/testsuite/ChangeLog:
* gcc.target/bpf/core-at

Re: [PATCH] bpf: Improvements in CO-RE builtins implementation.

2023-10-25 Thread Cupertino Miranda


Hi David,

Thanks for the comments.
I have just realized that I introduced several errors in the patch by
mistake right before I sent it.
Like the missing "*".

Apologies, I will revise it and send a new patch in a moment.

Thanks,
Cupertino

David Faust writes:

> On 10/25/23 11:51, Cupertino Miranda wrote:
>> Hi everyone,
>>
>> This patch contains some more recent improvements to BPF CO-RE builtins.
>> Please find further details of the changes on the patch header.
>>
>> Looking forward for your review and comments.
>>
>> Best regards,
>> Cupertino Miranda
>
> Hi Cupertino,
>
> Thanks for continuing to work on these BPF builtins.
>
> Comments inline below.
>
>>
>>
>> bpf_builtins_upstream.patch
>>
>> commit 6054209c0a8af9c3e6363550bf2ba4f4f2172eba
>> Author: Cupertino Miranda 
>> Date:   Tue Aug 8 09:22:41 2023 +0100
>>
>> bpf: Improvements in CO-RE builtins implementation.
>>
>> This patch moved the processing of attribute preserve_access_index to
>> its own independent pass in a gimple lowering pass.
>> This approach is more consistent with the implementation of the CO-RE
>> builtins when used explicitly in the code.  The attributed type accesses
>> are now early converted to __builtin_core_reloc builtin instead of being
>> kept as an expression in code through out all of the middle-end.
>> This disables the compiler to optimize out or manipulate the expression
>> using the local defined type, instead of assuming nothing is known about
>> this expression, as it should be the case in all of the CO-RE
>> relocations.
>>
>> In the process, also the __builtin_preserve_access_index has been
>> improved to generate code for more complex expressions that would
>> require more then one CO-RE relocation.
>> This turned out to be a requirement, since bpf-next selftests would rely 
>> on
>> loop unrolling in order to convert an undefined index array access into a
>> defined one. This seemed extreme to expect for the unroll to happen, and 
>> for
>> that reason GCC still generates correct code in such scenarios, even 
>> when index
>> access is never predictable or unrolling does not occur.
>>
>> gcc/ChangeLog:
>> * config/bpf/bpf-passes.def (pass_lower_bpf_core): Added pass.
>
> It may only be due to how the patch is formatted in the attachment
> (everything above the first diff seems to be indented?), but each entry
> here should start with a tab rather than spaces.
>
> Please double-check with contrib/gcc-changelog/git_check_commit.py, it
> will complain if the indentation is wrong.
>
>> * config/bpf/bpf-protos.h: Added prototype for new pass.
>> * config/bpf/bpf.cc (bpf_const_not_ok_for_debug_p): New function.
>> * config/bpf/bpf.md (mov_reloc_core): Changed
>
> Looks like an entry along the lines of "Renamed to..." would be
> more fitting.
>
>> * config/bpf/core-builtins.cc (cr_builtins, 
>> is_attr_preserve_access,
>> core_field_info, bpf_core_get_index, compute_field_expr,
>> process_field_expr, pack_type, make_core_relo,
>> bpf_handle_plugin_finish_type, core_buintin_helpers,
>> construct_builtin_core_reloc, 
>> bpf_resolve_overloaded_core_builtin,
>> bpf_add_core_reloc): Changed.
>
> "Changed." as a ChangeLog entry is not useful. Please be at least a
> little bit more descriptive here so someone only reading the ChangeLog
> has an idea of what they're looking at.
>
> Also, I'm not sure this is a proper way to note multiple functions
> with the same entry, usually this is done with a new pair of () for
> each new line, like:
>
>   * config/bpf/core-builtins.cc (cr_builtins, is_attr_preserve_access)
>   (core_field_info, bpf_core_get_index): Changed to do blah.
>
> contrib/gcc-changelog/git_check_commit.py might complain about this;
> please check.
>
>> (root_for_core_field_info, pack_field_expr,
>> core_expr_with_field_expr_plus_base, make_core_safe_access_index,
>> replace_core_access_index_comp_expr, 
>> maybe_get_base_for_field_expr,
>> core_access_clean, core_is_access_index, 
>> core_mark_as_access_index,
>> make_gimple_core_safe_access_index, execute_lower_bpf_core,
>> make_pass_lower_bpf_core): Added functions.
>
> Same thing about the multi-line entry within a single () pair.
>
>> (pass_da

[PATCH] bpf: Improvements in CO-RE builtins implementation.

2023-10-25 Thread Cupertino Miranda

Hi everyone,

This patch contains some more recent improvements to BPF CO-RE builtins.
Please find further details of the changes on the patch header.

Looking forward for your review and comments.

Best regards,
Cupertino Miranda

commit 6054209c0a8af9c3e6363550bf2ba4f4f2172eba
Author: Cupertino Miranda 
Date:   Tue Aug 8 09:22:41 2023 +0100

bpf: Improvements in CO-RE builtins implementation.

This patch moved the processing of attribute preserve_access_index to
its own independent pass in a gimple lowering pass.
This approach is more consistent with the implementation of the CO-RE
builtins when used explicitly in the code.  The attributed type accesses
are now early converted to __builtin_core_reloc builtin instead of being
kept as an expression in code through out all of the middle-end.
This disables the compiler to optimize out or manipulate the expression
using the local defined type, instead of assuming nothing is known about
this expression, as it should be the case in all of the CO-RE
relocations.

In the process, also the __builtin_preserve_access_index has been
improved to generate code for more complex expressions that would
require more then one CO-RE relocation.
This turned out to be a requirement, since bpf-next selftests would rely on
loop unrolling in order to convert an undefined index array access into a
defined one. This seemed extreme to expect for the unroll to happen, and for
that reason GCC still generates correct code in such scenarios, even when index
access is never predictable or unrolling does not occur.

gcc/ChangeLog:
* config/bpf/bpf-passes.def (pass_lower_bpf_core): Added pass.
* config/bpf/bpf-protos.h: Added prototype for new pass.
* config/bpf/bpf.cc (bpf_const_not_ok_for_debug_p): New function.
* config/bpf/bpf.md (mov_reloc_core): Changed
* config/bpf/core-builtins.cc (cr_builtins, is_attr_preserve_access,
core_field_info, bpf_core_get_index, compute_field_expr,
process_field_expr, pack_type, make_core_relo,
bpf_handle_plugin_finish_type, core_buintin_helpers,
construct_builtin_core_reloc, bpf_resolve_overloaded_core_builtin,
bpf_add_core_reloc): Changed.
(root_for_core_field_info, pack_field_expr,
core_expr_with_field_expr_plus_base, make_core_safe_access_index,
replace_core_access_index_comp_expr, maybe_get_base_for_field_expr,
core_access_clean, core_is_access_index, core_mark_as_access_index,
make_gimple_core_safe_access_index, execute_lower_bpf_core,
make_pass_lower_bpf_core): Added functions.
(pass_data_lower_bpf_core): New pass struct.
(pass_lower_bpf_core): New gimple_opt_pass class.
(pack_field_expr_for_preserve_field, bpf_replace_core_move_operands): Removed function.
(bpf_enum_value_kind): Added GTY(()).
* config/bpf/core-builtins.h (bpf_field_info_kind, bpf_type_id_kind,
bpf_type_info_kind, bpf_enum_value_kind): New enum.
* config/bpf/t-bpf: Added pass bpf-passes.def to PASSES_EXTRA.

gcc/testsuite/ChangeLog:
* gcc.target/bpf/core-attr-5.c: New test.
* gcc.target/bpf/core-attr-6.c: New test.
* gcc.target/bpf/core-builtin-1.c: Corrected
* gcc.target/bpf/core-builtin-enumvalue-opt.c: Corrected regular
expression.
* gcc.target/bpf/core-builtin-enumvalue.c: Corrected regular
expression.
* gcc.target/bpf/core-builtin-exprlist-1.c: New test.
* gcc.target/bpf/core-builtin-exprlist-2.c: New test.
* gcc.target/bpf/core-builtin-exprlist-3.c: New test.
* gcc.target/bpf/core-builtin-exprlist-4.c: New test.
* gcc.target/bpf/core-builtin-fieldinfo-offset-1.c: Extra tests

diff --git a/gcc/config/bpf/bpf-passes.def b/gcc/config/bpf/bpf-passes.def
new file mode 100644
index ..249c58e22067
--- /dev/null
+++ b/gcc/config/bpf/bpf-passes.def
@@ -0,0 +1,20 @@
+/* Declaration of target-specific passes for eBPF.
+   Copyright (C) 2021-2023 Free Software Foundation, Inc.
+
+   This file is part of GCC.
+
+   GCC is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   GCC is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GCC; see the file COPYING3.  If not see
+   <http://www.gnu.org/licenses/>.  */
+
+INSERT_PASS_

Re: [v2 PATCH 1/2] bpf: Implementation of BPF CO-RE builtins

2023-08-11 Thread Cupertino Miranda via Gcc-patches


Hi,

Thanks for the finding.
I will fix it in next upcoming patches.

Thanks,
Cupertino

Shung-Hsi Yu writes:

> Hi,
>
> Thanks for working on the BPF backend!
>
> I noticed a tiny typo while test compiling libbpf-tools[1]. (Have yet look
> into the cause of failure in detail though)
>
> On Thu, Aug 03, 2023 at 10:54:31AM +0100, Cupertino Miranda wrote:
>> [snip]
>> +
>> +pack_type_fail:
>> +  bpf_error_at (EXPR_LOC_OR_LOC (args[0], UNKNOWN_LOCATION),
>> +"invelid first argument format for enum value builtin");
>  ^^^
>
>> +  ret.fail = true;
>> +  return ret;
>> +}
>> +
>> [snip]
>
> Thanks,
> Shung-Hsi
>
> 1: https://github.com/iovisor/bcc/tree/master/libbpf-tools


Re: [PATCH] bpf: Fixed GC mistakes in BPF builtins code.

2023-08-08 Thread Cupertino Miranda via Gcc-patches


Thanks! Pushed to master.

Jose E. Marchesi writes:

> Hi Cuper.
>
> OK.  Hopefully all the roots are marked now to avoid these nodes being
> collected.
>
> Thanks.
>
>> Hi everyone,
>>
>> This patch fixes BPF CO-RE builtins support that missed information for
>> garbage collector (GC).
>>
>> The BPF CO-RE implementation defines several data structures that keep
>> builtin information throught all of the compilation flow aside from
>> code.  This intentionally avoids having the builtin calls arguments
>> expressions/enum/types tree nodes within the compiling code in order to
>> avoid the compiler to optimize those away, based on information in
>> current compilation unit.
>> CO-RE builtins are target kernel specific and very little can be infered
>> from type inforamtion within the compilation unit.
>>
>> Fault was triggered when attempting to compile some BPF kernel big
>> examples that revealed the lack of GC information.
>>
>> Patch also removes some spurious includes of header files.
>>
>> Best regards,
>> Cupertino
>>
>>
>>
>> commit c71b5c604189d04664c5b5ee155326fa4b79808b
>> Author: Cupertino Miranda 
>> Date:   Tue Aug 8 11:12:00 2023 +0100
>>
>> bpf: Fixed GC mistakes in BPF builtins code.
>>
>> This patches fixes problems with GC within the CO-RE builtins
>> implementation.
>> List of included headers was also reviseD.
>>
>> gcc/ChangeLog:
>>
>> * config/bpf/core-builtins.cc: Cleaned include headers.
>> (struct cr_builtins): Added GTY.
>> (cr_builtins_ref): Created.
>> (builtins_data) Changed to GC root.
>> (allocate_builtin_data): Changed.
>> Included gt-core-builtins.h.
>> * config/bpf/coreout.cc: (bpf_core_extra) Added GTY.
>> (bpf_core_extra_ref): Created.
>> (bpf_comment_info): Changed to GC root.
>> (bpf_core_reloc_add, output_btfext_header, btf_ext_init): 
>> Changed.
>>
>> diff --git a/gcc/config/bpf/core-builtins.cc 
>> b/gcc/config/bpf/core-builtins.cc
>> index 575e63d8ea77..c3222b4c7804 100644
>> --- a/gcc/config/bpf/core-builtins.cc
>> +++ b/gcc/config/bpf/core-builtins.cc
>> @@ -22,52 +22,23 @@ along with GCC; see the file COPYING3.  If not see
>>  #include "config.h"
>>  #include "system.h"
>>  #include "coretypes.h"
>> -#include "tm.h"
>> +#include "target.h"
>>  #include "rtl.h"
>> -#include "regs.h"
>> -#include "insn-config.h"
>> -#include "insn-attr.h"
>> -#include "recog.h"
>>  #include "output.h"
>> -#include "alias.h"
>>  #include "tree.h"
>>  #include "stringpool.h"
>>  #include "attribs.h"
>> -#include "varasm.h"
>> -#include "stor-layout.h"
>> -#include "calls.h"
>>  #include "function.h"
>> -#include "explow.h"
>>  #include "memmodel.h"
>>  #include "emit-rtl.h"
>> -#include "reload.h"
>> -#include "tm_p.h"
>> -#include "target.h"
>> -#include "basic-block.h"
>>  #include "expr.h"
>> -#include "optabs.h"
>> -#include "bitmap.h"
>> -#include "df.h"
>> -#include "c-family/c-common.h"
>>  #include "diagnostic.h"
>> -#include "builtins.h"
>> -#include "predict.h"
>>  #include "langhooks.h"
>> -#include "flags.h"
>> -
>> -#include "cfg.h"
>> +#include "basic-block.h"
>>  #include "gimple.h"
>>  #include "gimple-iterator.h"
>>  #include "gimple-walk.h"
>>  #include "tree-pass.h"
>> -#include "tree-iterator.h"
>> -
>> -#include "context.h"
>> -#include "pass_manager.h"
>> -
>> -#include "gimplify.h"
>> -#include "gimplify-me.h"
>> -
>>  #include "plugin.h"
>>
>>  #include "ctfc.h"
>> @@ -159,37 +130,41 @@ along with GCC; see the file COPYING3.  If not see
>>  as a builtin.  */
>>
>>
>> -struct cr_builtins
>> +struct GTY(()) cr_builtins
>>  {
>>tree type;
>>tree expr;
>>tree default_value;
>>rtx

[PATCH] bpf: Fixed GC mistakes in BPF builtins code.

2023-08-08 Thread Cupertino Miranda via Gcc-patches

Hi everyone,

This patch fixes BPF CO-RE builtins support that missed information for
garbage collector (GC).

The BPF CO-RE implementation defines several data structures that keep
builtin information throught all of the compilation flow aside from
code.  This intentionally avoids having the builtin calls arguments
expressions/enum/types tree nodes within the compiling code in order to
avoid the compiler to optimize those away, based on information in
current compilation unit.
CO-RE builtins are target kernel specific and very little can be infered
from type inforamtion within the compilation unit.

Fault was triggered when attempting to compile some BPF kernel big
examples that revealed the lack of GC information.

Patch also removes some spurious includes of header files.

Best regards,
Cupertino



commit c71b5c604189d04664c5b5ee155326fa4b79808b
Author: Cupertino Miranda 
Date:   Tue Aug 8 11:12:00 2023 +0100

bpf: Fixed GC mistakes in BPF builtins code.

This patches fixes problems with GC within the CO-RE builtins
implementation.
List of included headers was also reviseD.

gcc/ChangeLog:

* config/bpf/core-builtins.cc: Cleaned include headers.
(struct cr_builtins): Added GTY.
(cr_builtins_ref): Created.
(builtins_data) Changed to GC root.
(allocate_builtin_data): Changed.
Included gt-core-builtins.h.
* config/bpf/coreout.cc: (bpf_core_extra) Added GTY.
(bpf_core_extra_ref): Created.
(bpf_comment_info): Changed to GC root.
(bpf_core_reloc_add, output_btfext_header, btf_ext_init): Changed.

diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc
index 575e63d8ea77..c3222b4c7804 100644
--- a/gcc/config/bpf/core-builtins.cc
+++ b/gcc/config/bpf/core-builtins.cc
@@ -22,52 +22,23 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
-#include "tm.h"
+#include "target.h"
 #include "rtl.h"
-#include "regs.h"
-#include "insn-config.h"
-#include "insn-attr.h"
-#include "recog.h"
 #include "output.h"
-#include "alias.h"
 #include "tree.h"
 #include "stringpool.h"
 #include "attribs.h"
-#include "varasm.h"
-#include "stor-layout.h"
-#include "calls.h"
 #include "function.h"
-#include "explow.h"
 #include "memmodel.h"
 #include "emit-rtl.h"
-#include "reload.h"
-#include "tm_p.h"
-#include "target.h"
-#include "basic-block.h"
 #include "expr.h"
-#include "optabs.h"
-#include "bitmap.h"
-#include "df.h"
-#include "c-family/c-common.h"
 #include "diagnostic.h"
-#include "builtins.h"
-#include "predict.h"
 #include "langhooks.h"
-#include "flags.h"
-
-#include "cfg.h"
+#include "basic-block.h"
 #include "gimple.h"
 #include "gimple-iterator.h"
 #include "gimple-walk.h"
 #include "tree-pass.h"
-#include "tree-iterator.h"
-
-#include "context.h"
-#include "pass_manager.h"
-
-#include "gimplify.h"
-#include "gimplify-me.h"
-
 #include "plugin.h"
 
 #include "ctfc.h"
@@ -159,37 +130,41 @@ along with GCC; see the file COPYING3.  If not see
 as a builtin.  */
 
 
-struct cr_builtins
+struct GTY(()) cr_builtins
 {
   tree type;
   tree expr;
   tree default_value;
   rtx rtx_default_value;
-  enum btf_core_reloc_kind kind; /* Recovered from proper argument.  */
+  enum btf_core_reloc_kind kind;
   enum bpf_builtins orig_builtin_code;
   tree orig_arg_expr;
 };
+typedef struct cr_builtins *cr_builtins_ref;
 
 #define CORE_BUILTINS_DATA_EMPTY \
   { NULL_TREE, NULL_TREE, NULL_TREE, NULL_RTX, BPF_RELO_INVALID, \
 BPF_BUILTIN_UNUSED, NULL }
 
 /* Vector definition and its access function.  */
-vec builtins_data;
+static GTY(()) vec *builtins_data = NULL;
 
 static inline int
 allocate_builtin_data ()
 {
-  struct cr_builtins data = CORE_BUILTINS_DATA_EMPTY;
-  int ret = builtins_data.length ();
-  builtins_data.safe_push (data);
+  if (builtins_data == NULL)
+vec_alloc (builtins_data, 1);
+
+  cr_builtins_ref data = ggc_cleared_alloc ();
+  int ret = builtins_data->length ();
+  vec_safe_push (builtins_data, data);
   return ret;
 }
 
 static inline struct cr_builtins *
 get_builtin_data (int index)
 {
-  return _data[index];
+  return (*builtins_data)[index];
 }
 
 typedef bool
@@ -200,11 +175,12 @@ search_builtin_data (builtin_local_data_compare_fn callback,
 		 struct cr_builtins *elem)
 {
   unsigned int i;
-  for (i = 0; i < builtins_data.length (); i++)
-if ((callback != NULL

Re: [v2 PATCH 2/2] bpf: CO-RE builtins support tests.

2023-08-03 Thread Cupertino Miranda via Gcc-patches


Pushed to upstream master.

Thanks !
Jose E. Marchesi writes:

> OK.
> Thanks.
>
>> Hi,
>>
>> Resending this patch since I have noticed I had a testcase added in
>> previous patch. Makes more sense here.
>>
>> Thanks,
>> Cupertino
>>
>> From 334e9ae0f428f6573f2a5e8a3067a4d181b8b9c5 Mon Sep 17 00:00:00 2001
>> From: Cupertino Miranda 
>> Date: Thu, 27 Jul 2023 18:05:22 +0100
>> Subject: [PATCH v2 2/2] bpf: CO-RE builtins support tests.
>>
>> This patch adds tests for the following builtins:
>>   __builtin_preserve_enum_value
>>   __builtin_btf_type_id
>>   __builtin_preserve_type_info
>> ---
>>  .../gcc.target/bpf/core-builtin-enumvalue.c   |  52 +
>>  .../bpf/core-builtin-enumvalue_errors.c   |  22 
>>  .../bpf/core-builtin-enumvalue_opt.c  |  35 ++
>>  ...core-builtin-fieldinfo-const-elimination.c |  29 +
>>  .../bpf/core-builtin-fieldinfo-errors-1.c |   2 +-
>>  .../bpf/core-builtin-fieldinfo-errors-2.c |   2 +-
>>  .../gcc.target/bpf/core-builtin-type-based.c  |  58 ++
>>  .../gcc.target/bpf/core-builtin-type-id.c |  40 +++
>>  gcc/testsuite/gcc.target/bpf/core-support.h   | 109 ++
>>  9 files changed, 347 insertions(+), 2 deletions(-)
>>  create mode 100644 gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue.c
>>  create mode 100644 
>> gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue_errors.c
>>  create mode 100644 gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue_opt.c
>>  create mode 100644 
>> gcc/testsuite/gcc.target/bpf/core-builtin-fieldinfo-const-elimination.c
>>  create mode 100644 gcc/testsuite/gcc.target/bpf/core-builtin-type-based.c
>>  create mode 100644 gcc/testsuite/gcc.target/bpf/core-builtin-type-id.c
>>  create mode 100644 gcc/testsuite/gcc.target/bpf/core-support.h
>>
>> diff --git a/gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue.c 
>> b/gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue.c
>> new file mode 100644
>> index ..3e3334dc089a
>> --- /dev/null
>> +++ b/gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue.c
>> @@ -0,0 +1,52 @@
>> +/* { dg-do compile } */
>> +/* { dg-options "-O0 -dA -gbtf -mco-re" } */
>> +
>> +#include "core-support.h"
>> +
>> +extern int *v;
>> +
>> +int foo(void *data)
>> +{
>> + int i = 0;
>> + enum named_ue64 named_unsigned64 = 0;
>> + enum named_se64 named_signed64 = 0;
>> + enum named_ue named_unsigned = 0;
>> + enum named_se named_signed = 0;
>> +
>> + v[i++] = bpf_core_enum_value_exists (named_unsigned64, UE64_VAL1);
>> + v[i++] = bpf_core_enum_value_exists (enum named_ue64, UE64_VAL2);
>> + v[i++] = bpf_core_enum_value_exists (enum named_ue64, UE64_VAL3);
>> + v[i++] = bpf_core_enum_value_exists (named_signed64, SE64_VAL1);
>> + v[i++] = bpf_core_enum_value_exists (enum named_se64, SE64_VAL2);
>> + v[i++] = bpf_core_enum_value_exists (enum named_se64, SE64_VAL3);
>> +
>> + v[i++] = bpf_core_enum_value (named_unsigned64, UE64_VAL1);
>> + v[i++] = bpf_core_enum_value (named_unsigned64, UE64_VAL2);
>> + v[i++] = bpf_core_enum_value (named_signed64, SE64_VAL1);
>> + v[i++] = bpf_core_enum_value (named_signed64, SE64_VAL2);
>> +
>> + v[i++] = bpf_core_enum_value_exists (named_unsigned, UE_VAL1);
>> + v[i++] = bpf_core_enum_value_exists (enum named_ue, UE_VAL2);
>> + v[i++] = bpf_core_enum_value_exists (enum named_ue, UE_VAL3);
>> + v[i++] = bpf_core_enum_value_exists (named_signed, SE_VAL1);
>> + v[i++] = bpf_core_enum_value_exists (enum named_se, SE_VAL2);
>> + v[i++] = bpf_core_enum_value_exists (enum named_se, SE_VAL3);
>> +
>> + v[i++] = bpf_core_enum_value (named_unsigned, UE_VAL1);
>> + v[i++] = bpf_core_enum_value (named_unsigned, UE_VAL2);
>> + v[i++] = bpf_core_enum_value (named_signed, SE_VAL1);
>> + v[i++] = bpf_core_enum_value (named_signed, SE_VAL2);
>> +
>> + return 0;
>> +}
>> +
>> +/* { dg-final { scan-assembler-times "\t.4byte\t0x8\t; bpfcr_type 
>> \\(named_ue64\\)" 5 } } */
>> +/* { dg-final { scan-assembler-times "\t.4byte\t0x9\t; bpfcr_type 
>> \\(named_se64\\)" 5} } */
>> +/* { dg-final { scan-assembler-times "\t.4byte\t0xb\t; bpfcr_type 
>> \\(named_ue\\)" 5 } } */
>> +/* { dg-final { scan-assembler-times "\t.4byte\t0xc\t; bpfcr_type 
>> \\(named_se\\)" 5} } */
>> +/* { dg-final { scan-assembler-times "\t.4byte\t0xa\t; bpfcr_kind" 12 } } 
>> BPF_ENUMVAL_EXISTS */
>> +/* { d

Re: [v2 PATCH 1/2] bpf: Implementation of BPF CO-RE builtins

2023-08-03 Thread Cupertino Miranda via Gcc-patches


Pushed to upstream master.

Thanks !

Jose E. Marchesi writes:

> Ok.
> Thanks!
>
>> From fda9603ded735205b6e20fc5b65a04f8d15685e6 Mon Sep 17 00:00:00 2001
>> From: Cupertino Miranda 
>> Date: Thu, 6 Apr 2023 15:22:48 +0100
>> Subject: [PATCH v2 1/2] bpf: Implementation of BPF CO-RE builtins
>>
>> This patch updates the support for the BPF CO-RE builtins
>> __builtin_preserve_access_index and __builtin_preserve_field_info,
>> and adds support for the CO-RE builtins __builtin_btf_type_id,
>> __builtin_preserve_type_info and __builtin_preserve_enum_value.
>>
>> These CO-RE relocations are now converted to __builtin_core_reloc which
>> abstracts all of the original builtins in a polymorphic relocation
>> specific builtin.
>>
>> The builtin processing is now split in 2 stages, the first (pack) is
>> executed right after the front-end and the second (process) right before
>> the asm output.
>>
>> In expand pass the __builtin_core_reloc is converted to a
>> unspec:UNSPEC_CORE_RELOC rtx entry.
>>
>> The data required to process the builtin is now collected in the packing
>> stage (after front-end), not allowing the compiler to optimize any of
>> the relevant information required to compose the relocation when
>> necessary.
>> At expansion, that information is recovered and CTF/BTF is queried to
>> construct the information that will be used in the relocation.
>> At this point the relocation is added to specific section and the
>> builtin is expanded to the expected default value for the builtin.
>>
>> In order to process __builtin_preserve_enum_value, it was necessary to
>> hook the front-end to collect the original enum value reference.
>> This is needed since the parser folds all the enum values to its
>> integer_cst representation.
>>
>> More details can be found within the core-builtins.cc.
>>
>> Regtested in host x86_64-linux-gnu and target bpf-unknown-none.
>> ---
>>  gcc/config.gcc  |4 +-
>>  gcc/config/bpf/bpf-passes.def   |   20 -
>>  gcc/config/bpf/bpf-protos.h |4 +-
>>  gcc/config/bpf/bpf.cc   |  806 ++
>>  gcc/config/bpf/bpf.md   |   17 +
>>  gcc/config/bpf/core-builtins.cc | 1394 +++
>>  gcc/config/bpf/core-builtins.h  |   35 +
>>  gcc/config/bpf/coreout.cc   |   50 +-
>>  gcc/config/bpf/coreout.h|   13 +-
>>  gcc/config/bpf/t-bpf|6 +-
>>  gcc/doc/extend.texi |   51 ++
>>  11 files changed, 1595 insertions(+), 805 deletions(-)
>>  delete mode 100644 gcc/config/bpf/bpf-passes.def
>>  create mode 100644 gcc/config/bpf/core-builtins.cc
>>  create mode 100644 gcc/config/bpf/core-builtins.h
>>
>> diff --git a/gcc/config.gcc b/gcc/config.gcc
>> index eba69a463be0..c521669e78b1 100644
>> --- a/gcc/config.gcc
>> +++ b/gcc/config.gcc
>> @@ -1597,8 +1597,8 @@ bpf-*-*)
>>  use_collect2=no
>>  extra_headers="bpf-helpers.h"
>>  use_gcc_stdint=provide
>> -extra_objs="coreout.o"
>> -target_gtfiles="$target_gtfiles \$(srcdir)/config/bpf/coreout.cc"
>> +extra_objs="coreout.o core-builtins.o"
>> +target_gtfiles="$target_gtfiles \$(srcdir)/config/bpf/coreout.cc 
>> \$(srcdir)/config/bpf/core-builtins.cc"
>>  ;;
>>  cris-*-elf | cris-*-none)
>>  tm_file="elfos.h newlib-stdint.h ${tm_file}"
>> diff --git a/gcc/config/bpf/bpf-passes.def b/gcc/config/bpf/bpf-passes.def
>> deleted file mode 100644
>> index deeaee988a01..
>> --- a/gcc/config/bpf/bpf-passes.def
>> +++ /dev/null
>> @@ -1,20 +0,0 @@
>> -/* Declaration of target-specific passes for eBPF.
>> -   Copyright (C) 2021-2023 Free Software Foundation, Inc.
>> -
>> -   This file is part of GCC.
>> -
>> -   GCC is free software; you can redistribute it and/or modify it
>> -   under the terms of the GNU General Public License as published by
>> -   the Free Software Foundation; either version 3, or (at your option)
>> -   any later version.
>> -
>> -   GCC is distributed in the hope that it will be useful, but
>> -   WITHOUT ANY WARRANTY; without even the implied warranty of
>> -   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> -   General Public License for more details.
>> -
>> -   You should have received a copy of the GNU General Public License
>> -   along with GCC; see the file COPYING3.  If not see
>> -  

Re: [v2 PATCH 2/2] bpf: CO-RE builtins support tests.

2023-08-03 Thread Cupertino Miranda via Gcc-patches

Hi,

Resending this patch since I have noticed I had a testcase added in
previous patch. Makes more sense here.

Thanks,
Cupertino

>From 334e9ae0f428f6573f2a5e8a3067a4d181b8b9c5 Mon Sep 17 00:00:00 2001
From: Cupertino Miranda 
Date: Thu, 27 Jul 2023 18:05:22 +0100
Subject: [PATCH v2 2/2] bpf: CO-RE builtins support tests.

This patch adds tests for the following builtins:
  __builtin_preserve_enum_value
  __builtin_btf_type_id
  __builtin_preserve_type_info
---
 .../gcc.target/bpf/core-builtin-enumvalue.c   |  52 +
 .../bpf/core-builtin-enumvalue_errors.c   |  22 
 .../bpf/core-builtin-enumvalue_opt.c  |  35 ++
 ...core-builtin-fieldinfo-const-elimination.c |  29 +
 .../bpf/core-builtin-fieldinfo-errors-1.c |   2 +-
 .../bpf/core-builtin-fieldinfo-errors-2.c |   2 +-
 .../gcc.target/bpf/core-builtin-type-based.c  |  58 ++
 .../gcc.target/bpf/core-builtin-type-id.c |  40 +++
 gcc/testsuite/gcc.target/bpf/core-support.h   | 109 ++
 9 files changed, 347 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue_errors.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue_opt.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/core-builtin-fieldinfo-const-elimination.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/core-builtin-type-based.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/core-builtin-type-id.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/core-support.h

diff --git a/gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue.c b/gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue.c
new file mode 100644
index ..3e3334dc089a
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue.c
@@ -0,0 +1,52 @@
+/* { dg-do compile } */
+/* { dg-options "-O0 -dA -gbtf -mco-re" } */
+
+#include "core-support.h"
+
+extern int *v;
+
+int foo(void *data)
+{
+ int i = 0;
+ enum named_ue64 named_unsigned64 = 0;
+ enum named_se64 named_signed64 = 0;
+ enum named_ue named_unsigned = 0;
+ enum named_se named_signed = 0;
+
+ v[i++] = bpf_core_enum_value_exists (named_unsigned64, UE64_VAL1);
+ v[i++] = bpf_core_enum_value_exists (enum named_ue64, UE64_VAL2);
+ v[i++] = bpf_core_enum_value_exists (enum named_ue64, UE64_VAL3);
+ v[i++] = bpf_core_enum_value_exists (named_signed64, SE64_VAL1);
+ v[i++] = bpf_core_enum_value_exists (enum named_se64, SE64_VAL2);
+ v[i++] = bpf_core_enum_value_exists (enum named_se64, SE64_VAL3);
+
+ v[i++] = bpf_core_enum_value (named_unsigned64, UE64_VAL1);
+ v[i++] = bpf_core_enum_value (named_unsigned64, UE64_VAL2);
+ v[i++] = bpf_core_enum_value (named_signed64, SE64_VAL1);
+ v[i++] = bpf_core_enum_value (named_signed64, SE64_VAL2);
+
+ v[i++] = bpf_core_enum_value_exists (named_unsigned, UE_VAL1);
+ v[i++] = bpf_core_enum_value_exists (enum named_ue, UE_VAL2);
+ v[i++] = bpf_core_enum_value_exists (enum named_ue, UE_VAL3);
+ v[i++] = bpf_core_enum_value_exists (named_signed, SE_VAL1);
+ v[i++] = bpf_core_enum_value_exists (enum named_se, SE_VAL2);
+ v[i++] = bpf_core_enum_value_exists (enum named_se, SE_VAL3);
+
+ v[i++] = bpf_core_enum_value (named_unsigned, UE_VAL1);
+ v[i++] = bpf_core_enum_value (named_unsigned, UE_VAL2);
+ v[i++] = bpf_core_enum_value (named_signed, SE_VAL1);
+ v[i++] = bpf_core_enum_value (named_signed, SE_VAL2);
+
+ return 0;
+}
+
+/* { dg-final { scan-assembler-times "\t.4byte\t0x8\t; bpfcr_type \\(named_ue64\\)" 5 } } */
+/* { dg-final { scan-assembler-times "\t.4byte\t0x9\t; bpfcr_type \\(named_se64\\)" 5} } */
+/* { dg-final { scan-assembler-times "\t.4byte\t0xb\t; bpfcr_type \\(named_ue\\)" 5 } } */
+/* { dg-final { scan-assembler-times "\t.4byte\t0xc\t; bpfcr_type \\(named_se\\)" 5} } */
+/* { dg-final { scan-assembler-times "\t.4byte\t0xa\t; bpfcr_kind" 12 } } BPF_ENUMVAL_EXISTS */
+/* { dg-final { scan-assembler-times "\t.4byte\t0xb\t; bpfcr_kind" 8 } } BPF_ENUMVAL_VALUE */
+
+/* { dg-final { scan-assembler-times "bpfcr_astr_off \\(\"0\"\\)" 8 } } */
+/* { dg-final { scan-assembler-times "bpfcr_astr_off \\(\"1\"\\)" 8 } } */
+/* { dg-final { scan-assembler-times "bpfcr_astr_off \\(\"2\"\\)" 4 } } */
diff --git a/gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue_errors.c b/gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue_errors.c
new file mode 100644
index ..138e99895160
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue_errors.c
@@ -0,0 +1,22 @@
+/* { dg-do compile } */
+/* { dg-options "-O0 -dA -gbtf -mco-re" } */
+
+#include "core-support.h"
+
+extern int *v;
+
+unsigned long foo(void *data)
+{
+  int i = 0;
+  enum named_ue64 named_unsigned = 0;
+  enum named_se64 named_signed = 0;
+  typeof(enum named_ue64) a = 0;
+
+

Re: [v2 PATCH 1/2] bpf: Implementation of BPF CO-RE builtins

2023-08-03 Thread Cupertino Miranda via Gcc-patches
>From fda9603ded735205b6e20fc5b65a04f8d15685e6 Mon Sep 17 00:00:00 2001
From: Cupertino Miranda 
Date: Thu, 6 Apr 2023 15:22:48 +0100
Subject: [PATCH v2 1/2] bpf: Implementation of BPF CO-RE builtins

This patch updates the support for the BPF CO-RE builtins
__builtin_preserve_access_index and __builtin_preserve_field_info,
and adds support for the CO-RE builtins __builtin_btf_type_id,
__builtin_preserve_type_info and __builtin_preserve_enum_value.

These CO-RE relocations are now converted to __builtin_core_reloc which
abstracts all of the original builtins in a polymorphic relocation
specific builtin.

The builtin processing is now split in 2 stages, the first (pack) is
executed right after the front-end and the second (process) right before
the asm output.

In expand pass the __builtin_core_reloc is converted to a
unspec:UNSPEC_CORE_RELOC rtx entry.

The data required to process the builtin is now collected in the packing
stage (after front-end), not allowing the compiler to optimize any of
the relevant information required to compose the relocation when
necessary.
At expansion, that information is recovered and CTF/BTF is queried to
construct the information that will be used in the relocation.
At this point the relocation is added to specific section and the
builtin is expanded to the expected default value for the builtin.

In order to process __builtin_preserve_enum_value, it was necessary to
hook the front-end to collect the original enum value reference.
This is needed since the parser folds all the enum values to its
integer_cst representation.

More details can be found within the core-builtins.cc.

Regtested in host x86_64-linux-gnu and target bpf-unknown-none.
---
 gcc/config.gcc  |4 +-
 gcc/config/bpf/bpf-passes.def   |   20 -
 gcc/config/bpf/bpf-protos.h |4 +-
 gcc/config/bpf/bpf.cc   |  806 ++
 gcc/config/bpf/bpf.md   |   17 +
 gcc/config/bpf/core-builtins.cc | 1394 +++
 gcc/config/bpf/core-builtins.h  |   35 +
 gcc/config/bpf/coreout.cc   |   50 +-
 gcc/config/bpf/coreout.h|   13 +-
 gcc/config/bpf/t-bpf|6 +-
 gcc/doc/extend.texi |   51 ++
 11 files changed, 1595 insertions(+), 805 deletions(-)
 delete mode 100644 gcc/config/bpf/bpf-passes.def
 create mode 100644 gcc/config/bpf/core-builtins.cc
 create mode 100644 gcc/config/bpf/core-builtins.h

diff --git a/gcc/config.gcc b/gcc/config.gcc
index eba69a463be0..c521669e78b1 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -1597,8 +1597,8 @@ bpf-*-*)
 use_collect2=no
 extra_headers="bpf-helpers.h"
 use_gcc_stdint=provide
-extra_objs="coreout.o"
-target_gtfiles="$target_gtfiles \$(srcdir)/config/bpf/coreout.cc"
+extra_objs="coreout.o core-builtins.o"
+target_gtfiles="$target_gtfiles \$(srcdir)/config/bpf/coreout.cc \$(srcdir)/config/bpf/core-builtins.cc"
 ;;
 cris-*-elf | cris-*-none)
 	tm_file="elfos.h newlib-stdint.h ${tm_file}"
diff --git a/gcc/config/bpf/bpf-passes.def b/gcc/config/bpf/bpf-passes.def
deleted file mode 100644
index deeaee988a01..
--- a/gcc/config/bpf/bpf-passes.def
+++ /dev/null
@@ -1,20 +0,0 @@
-/* Declaration of target-specific passes for eBPF.
-   Copyright (C) 2021-2023 Free Software Foundation, Inc.
-
-   This file is part of GCC.
-
-   GCC is free software; you can redistribute it and/or modify it
-   under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 3, or (at your option)
-   any later version.
-
-   GCC is distributed in the hope that it will be useful, but
-   WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with GCC; see the file COPYING3.  If not see
-   <http://www.gnu.org/licenses/>.  */
-
-INSERT_PASS_AFTER (pass_df_initialize_opt, 1, pass_bpf_core_attr);
diff --git a/gcc/config/bpf/bpf-protos.h b/gcc/config/bpf/bpf-protos.h
index b484310e8cbf..fbe0d8a0213f 100644
--- a/gcc/config/bpf/bpf-protos.h
+++ b/gcc/config/bpf/bpf-protos.h
@@ -30,7 +30,7 @@ extern void bpf_print_operand_address (FILE *, rtx);
 extern void bpf_expand_prologue (void);
 extern void bpf_expand_epilogue (void);
 extern void bpf_expand_cbranch (machine_mode, rtx *);
-
-rtl_opt_pass * make_pass_bpf_core_attr (gcc::context *);
+const char *bpf_add_core_reloc (rtx *operands, const char *templ);
+void bpf_replace_core_move_operands (rtx *operands);
 
 #endif /* ! GCC_BPF_PROTOS_H */
diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
index 57817cdf2f86..4873475e73bd 100644
--- a/gcc/config/bpf/bpf.cc
+++ b/gcc/config/bpf/bpf.cc
@@ -69,10 +69,7 @@ along with GCC; see the file COPYING3.  If not see

Re: [PATCH 1/2] bpf: Implementation of BPF CO-RE builtins

2023-08-03 Thread Cupertino Miranda via Gcc-patches


>> +  /* FIXED: This wat not Ok.
>
> Hm?  If that is fixed, do we still need that comment? :)
Touche! ;)

>
>> +emit_insn ( \
>> +  gen_mov_reloc_coredi (reg, \
>> +gen_rtx_CONST_INT (Pmode, 0), \
>> +gen_rtx_CONST_INT (Pmode, index))); \
>
> These backslahes... was that in a macro originally?
Forgot to reply to this.
Yes, but my own defined macro. I initially assumed it needed
different emits depending on the original mode, so I made a macro and
used it in a switch case.
>
>> +return true;
>> +  }
>> +  }
>> +  return false;
>> +}


Re: [PATCH 1/2] bpf: Implementation of BPF CO-RE builtins

2023-08-03 Thread Cupertino Miranda via Gcc-patches


Jose E. Marchesi writes:

>> This patch updates the support for the BPF CO-RE builtins
>> __builtin_preserve_access_index and __builtin_preserve_field_info,
>> and adds support for the CO-RE builtins __builtin_btf_type_id,
>> __builtin_preserve_type_info and __builtin_preserve_enum_value.
>>
>> These CO-RE relocations are now converted to __builtin_core_reloc which
>> abstracts all of the original builtins in a polymorphic relocation
>> specific builtin.
>>
>> The builtin processing is now split in 2 stages, the first (pack) is
>> executed right after the front-end and the second (process) right before
>> the asm output.
>>
>> In expand pass the __builtin_core_reloc is converted to a
>> unspec:UNSPEC_CORE_RELOC rtx entry.
>>
>> The data required to process the builtin is now collected in the packing
>> stage (after front-end), not allowing the compiler to optimize any of
>> the relevant information required to compose the relocation when
>> necessary.
>> At expansion, that information is recovered and CTF/BTF is queried to
>> construct the information that will be used in the relocation.
>> At this point the relocation is added to specific section and the
>> builtin is expanded to the expected default value for the builtin.
>>
>> In order to process __builtin_preserve_enum_value, it was necessary to
>> hook the front-end to collect the original enum value reference.
>> This is needed since the parser folds all the enum values to its
>> integer_cst representation.
>>
>> More details can be found within the core-builtins.cc.
>>
>> Regtested in host x86_64-linux-gnu and target bpf-unknown-none.
>> ---
>>  gcc/config.gcc|4 +-
>>  gcc/config/bpf/bpf-passes.def |   20 -
>>  gcc/config/bpf/bpf-protos.h   |4 +-
>>  gcc/config/bpf/bpf.cc |  817 +-
>>  gcc/config/bpf/bpf.md |   17 +
>>  gcc/config/bpf/core-builtins.cc   | 1397 +
>>  gcc/config/bpf/core-builtins.h|   36 +
>>  gcc/config/bpf/coreout.cc |   50 +-
>>  gcc/config/bpf/coreout.h  |   13 +-
>>  gcc/config/bpf/t-bpf  |6 +-
>>  gcc/doc/extend.texi   |   51 +
>>  ...core-builtin-fieldinfo-const-elimination.c |   29 +
>>  12 files changed, 1639 insertions(+), 805 deletions(-)
>>  delete mode 100644 gcc/config/bpf/bpf-passes.def
>>  create mode 100644 gcc/config/bpf/core-builtins.cc
>>  create mode 100644 gcc/config/bpf/core-builtins.h
>>  create mode 100644 
>> gcc/testsuite/gcc.target/bpf/core-builtin-fieldinfo-const-elimination.c
>>
>> diff --git a/gcc/config.gcc b/gcc/config.gcc
>> index eba69a463be0..c521669e78b1 100644
>> --- a/gcc/config.gcc
>> +++ b/gcc/config.gcc
>> @@ -1597,8 +1597,8 @@ bpf-*-*)
>>  use_collect2=no
>>  extra_headers="bpf-helpers.h"
>>  use_gcc_stdint=provide
>> -extra_objs="coreout.o"
>> -target_gtfiles="$target_gtfiles \$(srcdir)/config/bpf/coreout.cc"
>> +extra_objs="coreout.o core-builtins.o"
>> +target_gtfiles="$target_gtfiles \$(srcdir)/config/bpf/coreout.cc 
>> \$(srcdir)/config/bpf/core-builtins.cc"
>>  ;;
>>  cris-*-elf | cris-*-none)
>>  tm_file="elfos.h newlib-stdint.h ${tm_file}"
>> diff --git a/gcc/config/bpf/bpf-passes.def b/gcc/config/bpf/bpf-passes.def
>> deleted file mode 100644
>> index deeaee988a01..
>> --- a/gcc/config/bpf/bpf-passes.def
>> +++ /dev/null
>> @@ -1,20 +0,0 @@
>> -/* Declaration of target-specific passes for eBPF.
>> -   Copyright (C) 2021-2023 Free Software Foundation, Inc.
>> -
>> -   This file is part of GCC.
>> -
>> -   GCC is free software; you can redistribute it and/or modify it
>> -   under the terms of the GNU General Public License as published by
>> -   the Free Software Foundation; either version 3, or (at your option)
>> -   any later version.
>> -
>> -   GCC is distributed in the hope that it will be useful, but
>> -   WITHOUT ANY WARRANTY; without even the implied warranty of
>> -   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> -   General Public License for more details.
>> -
>> -   You should have received a copy of the GNU General Public License
>> -   along with GCC; see the file COPYING3.  If not see
>> -   .  */
>> -
>> -INSERT_PASS_AFTER (pass_df_initialize_opt, 1, pass_bpf_core_attr);
>> diff --git a/gcc/config/bpf/bpf-protos.h b/gcc/config/bpf/bpf-protos.h
>> index b484310e8cbf..fbcf5111eb21 100644
>> --- a/gcc/config/bpf/bpf-protos.h
>> +++ b/gcc/config/bpf/bpf-protos.h
>> @@ -30,7 +30,7 @@ extern void bpf_print_operand_address (FILE *, rtx);
>>  extern void bpf_expand_prologue (void);
>>  extern void bpf_expand_epilogue (void);
>>  extern void bpf_expand_cbranch (machine_mode, rtx *);
>> -
>> -rtl_opt_pass * make_pass_bpf_core_attr (gcc::context *);
>> +const char *bpf_add_core_reloc 

[PATCH 1/2] bpf: Implementation of BPF CO-RE builtins

2023-08-01 Thread Cupertino Miranda via Gcc-patches
This patch updates the support for the BPF CO-RE builtins
__builtin_preserve_access_index and __builtin_preserve_field_info,
and adds support for the CO-RE builtins __builtin_btf_type_id,
__builtin_preserve_type_info and __builtin_preserve_enum_value.

These CO-RE relocations are now converted to __builtin_core_reloc which
abstracts all of the original builtins in a polymorphic relocation
specific builtin.

The builtin processing is now split in 2 stages, the first (pack) is
executed right after the front-end and the second (process) right before
the asm output.

In expand pass the __builtin_core_reloc is converted to a
unspec:UNSPEC_CORE_RELOC rtx entry.

The data required to process the builtin is now collected in the packing
stage (after front-end), not allowing the compiler to optimize any of
the relevant information required to compose the relocation when
necessary.
At expansion, that information is recovered and CTF/BTF is queried to
construct the information that will be used in the relocation.
At this point the relocation is added to specific section and the
builtin is expanded to the expected default value for the builtin.

In order to process __builtin_preserve_enum_value, it was necessary to
hook the front-end to collect the original enum value reference.
This is needed since the parser folds all the enum values to its
integer_cst representation.

More details can be found within the core-builtins.cc.

Regtested in host x86_64-linux-gnu and target bpf-unknown-none.
---
 gcc/config.gcc|4 +-
 gcc/config/bpf/bpf-passes.def |   20 -
 gcc/config/bpf/bpf-protos.h   |4 +-
 gcc/config/bpf/bpf.cc |  817 +-
 gcc/config/bpf/bpf.md |   17 +
 gcc/config/bpf/core-builtins.cc   | 1397 +
 gcc/config/bpf/core-builtins.h|   36 +
 gcc/config/bpf/coreout.cc |   50 +-
 gcc/config/bpf/coreout.h  |   13 +-
 gcc/config/bpf/t-bpf  |6 +-
 gcc/doc/extend.texi   |   51 +
 ...core-builtin-fieldinfo-const-elimination.c |   29 +
 12 files changed, 1639 insertions(+), 805 deletions(-)
 delete mode 100644 gcc/config/bpf/bpf-passes.def
 create mode 100644 gcc/config/bpf/core-builtins.cc
 create mode 100644 gcc/config/bpf/core-builtins.h
 create mode 100644 
gcc/testsuite/gcc.target/bpf/core-builtin-fieldinfo-const-elimination.c

diff --git a/gcc/config.gcc b/gcc/config.gcc
index eba69a463be0..c521669e78b1 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -1597,8 +1597,8 @@ bpf-*-*)
 use_collect2=no
 extra_headers="bpf-helpers.h"
 use_gcc_stdint=provide
-extra_objs="coreout.o"
-target_gtfiles="$target_gtfiles \$(srcdir)/config/bpf/coreout.cc"
+extra_objs="coreout.o core-builtins.o"
+target_gtfiles="$target_gtfiles \$(srcdir)/config/bpf/coreout.cc 
\$(srcdir)/config/bpf/core-builtins.cc"
 ;;
 cris-*-elf | cris-*-none)
tm_file="elfos.h newlib-stdint.h ${tm_file}"
diff --git a/gcc/config/bpf/bpf-passes.def b/gcc/config/bpf/bpf-passes.def
deleted file mode 100644
index deeaee988a01..
--- a/gcc/config/bpf/bpf-passes.def
+++ /dev/null
@@ -1,20 +0,0 @@
-/* Declaration of target-specific passes for eBPF.
-   Copyright (C) 2021-2023 Free Software Foundation, Inc.
-
-   This file is part of GCC.
-
-   GCC is free software; you can redistribute it and/or modify it
-   under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 3, or (at your option)
-   any later version.
-
-   GCC is distributed in the hope that it will be useful, but
-   WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with GCC; see the file COPYING3.  If not see
-   .  */
-
-INSERT_PASS_AFTER (pass_df_initialize_opt, 1, pass_bpf_core_attr);
diff --git a/gcc/config/bpf/bpf-protos.h b/gcc/config/bpf/bpf-protos.h
index b484310e8cbf..fbcf5111eb21 100644
--- a/gcc/config/bpf/bpf-protos.h
+++ b/gcc/config/bpf/bpf-protos.h
@@ -30,7 +30,7 @@ extern void bpf_print_operand_address (FILE *, rtx);
 extern void bpf_expand_prologue (void);
 extern void bpf_expand_epilogue (void);
 extern void bpf_expand_cbranch (machine_mode, rtx *);
-
-rtl_opt_pass * make_pass_bpf_core_attr (gcc::context *);
+const char *bpf_add_core_reloc (rtx *operands, const char *templ);
+void bpf_process_move_operands (rtx *operands);
 
 #endif /* ! GCC_BPF_PROTOS_H */
diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
index b5b5674edbb5..101e994905d2 100644
--- a/gcc/config/bpf/bpf.cc
+++ b/gcc/config/bpf/bpf.cc
@@ -69,10 +69,7 @@ along with GCC; see the file COPYING3.  If 

[PATCH 2/2] bpf: CO-RE builtins support tests.

2023-08-01 Thread Cupertino Miranda via Gcc-patches
This patch adds tests for the following builtins:
  __builtin_preserve_enum_value
  __builtin_btf_type_id
  __builtin_preserve_type_info
---
 .../gcc.target/bpf/core-builtin-enumvalue.c   |  52 +
 .../bpf/core-builtin-enumvalue_errors.c   |  22 
 .../bpf/core-builtin-enumvalue_opt.c  |  35 ++
 .../bpf/core-builtin-fieldinfo-errors-1.c |   2 +-
 .../bpf/core-builtin-fieldinfo-errors-2.c |   2 +-
 .../gcc.target/bpf/core-builtin-type-based.c  |  58 ++
 .../gcc.target/bpf/core-builtin-type-id.c |  40 +++
 gcc/testsuite/gcc.target/bpf/core-support.h   | 109 ++
 8 files changed, 318 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue_errors.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue_opt.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/core-builtin-type-based.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/core-builtin-type-id.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/core-support.h

diff --git a/gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue.c 
b/gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue.c
new file mode 100644
index ..3e3334dc089a
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue.c
@@ -0,0 +1,52 @@
+/* { dg-do compile } */
+/* { dg-options "-O0 -dA -gbtf -mco-re" } */
+
+#include "core-support.h"
+
+extern int *v;
+
+int foo(void *data)
+{
+ int i = 0;
+ enum named_ue64 named_unsigned64 = 0;
+ enum named_se64 named_signed64 = 0;
+ enum named_ue named_unsigned = 0;
+ enum named_se named_signed = 0;
+
+ v[i++] = bpf_core_enum_value_exists (named_unsigned64, UE64_VAL1);
+ v[i++] = bpf_core_enum_value_exists (enum named_ue64, UE64_VAL2);
+ v[i++] = bpf_core_enum_value_exists (enum named_ue64, UE64_VAL3);
+ v[i++] = bpf_core_enum_value_exists (named_signed64, SE64_VAL1);
+ v[i++] = bpf_core_enum_value_exists (enum named_se64, SE64_VAL2);
+ v[i++] = bpf_core_enum_value_exists (enum named_se64, SE64_VAL3);
+
+ v[i++] = bpf_core_enum_value (named_unsigned64, UE64_VAL1);
+ v[i++] = bpf_core_enum_value (named_unsigned64, UE64_VAL2);
+ v[i++] = bpf_core_enum_value (named_signed64, SE64_VAL1);
+ v[i++] = bpf_core_enum_value (named_signed64, SE64_VAL2);
+
+ v[i++] = bpf_core_enum_value_exists (named_unsigned, UE_VAL1);
+ v[i++] = bpf_core_enum_value_exists (enum named_ue, UE_VAL2);
+ v[i++] = bpf_core_enum_value_exists (enum named_ue, UE_VAL3);
+ v[i++] = bpf_core_enum_value_exists (named_signed, SE_VAL1);
+ v[i++] = bpf_core_enum_value_exists (enum named_se, SE_VAL2);
+ v[i++] = bpf_core_enum_value_exists (enum named_se, SE_VAL3);
+
+ v[i++] = bpf_core_enum_value (named_unsigned, UE_VAL1);
+ v[i++] = bpf_core_enum_value (named_unsigned, UE_VAL2);
+ v[i++] = bpf_core_enum_value (named_signed, SE_VAL1);
+ v[i++] = bpf_core_enum_value (named_signed, SE_VAL2);
+
+ return 0;
+}
+
+/* { dg-final { scan-assembler-times "\t.4byte\t0x8\t; bpfcr_type 
\\(named_ue64\\)" 5 } } */
+/* { dg-final { scan-assembler-times "\t.4byte\t0x9\t; bpfcr_type 
\\(named_se64\\)" 5} } */
+/* { dg-final { scan-assembler-times "\t.4byte\t0xb\t; bpfcr_type 
\\(named_ue\\)" 5 } } */
+/* { dg-final { scan-assembler-times "\t.4byte\t0xc\t; bpfcr_type 
\\(named_se\\)" 5} } */
+/* { dg-final { scan-assembler-times "\t.4byte\t0xa\t; bpfcr_kind" 12 } } 
BPF_ENUMVAL_EXISTS */
+/* { dg-final { scan-assembler-times "\t.4byte\t0xb\t; bpfcr_kind" 8 } } 
BPF_ENUMVAL_VALUE */
+
+/* { dg-final { scan-assembler-times "bpfcr_astr_off \\(\"0\"\\)" 8 } } */
+/* { dg-final { scan-assembler-times "bpfcr_astr_off \\(\"1\"\\)" 8 } } */
+/* { dg-final { scan-assembler-times "bpfcr_astr_off \\(\"2\"\\)" 4 } } */
diff --git a/gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue_errors.c 
b/gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue_errors.c
new file mode 100644
index ..138e99895160
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/core-builtin-enumvalue_errors.c
@@ -0,0 +1,22 @@
+/* { dg-do compile } */
+/* { dg-options "-O0 -dA -gbtf -mco-re" } */
+
+#include "core-support.h"
+
+extern int *v;
+
+unsigned long foo(void *data)
+{
+  int i = 0;
+  enum named_ue64 named_unsigned = 0;
+  enum named_se64 named_signed = 0;
+  typeof(enum named_ue64) a = 0;
+
+  v[i++] = __builtin_preserve_enum_value (({ extern typeof(named_unsigned) 
*_type0; _type0; }), 0, BPF_ENUMVAL_EXISTS); /* { dg-error "invalid 
enum value argument for enum value builtin" } */
+  v[i++] = __builtin_preserve_enum_value (({ extern typeof(enum named_ue64) 
*_type0; _type0; }), v,BPF_ENUMVAL_EXISTS); /* { dg-error "invalid enum 
value argument for enum value builtin" } */
+  v[i++] = __builtin_preserve_enum_value (a,   
 UE64_VAL3, BPF_ENUMVAL_EXISTS); /* { dg-error "invalid 
type argument format for enum value builtin" } */
+  v[i++] = 

[PATCH] CO-RE BPF builtins support

2023-08-01 Thread Cupertino Miranda via Gcc-patches
Hi everyone,

This patch series implements all the BPF CO-RE builtins.
It improves the support for __builtin_preserve_access_index and
__builtin_preserve_field_info, but also introduces the support for
__builtin_btf_type_id, __builtin_btf_preserve_type_info and
__builtin_preserve_enum_value.

Regtested in host x86_64-linux-gnu and target bpf-unknown-none.

Looking forward to your comments.

Best regards,
Cupertino




Re: [PATCH v3] bpf: pseudo-c assembly dialect support

2023-07-21 Thread Cupertino Miranda via Gcc-patches



>> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
>> index 3063e71c8906..b3be65d3efae 100644
>> --- a/gcc/doc/invoke.texi
>> +++ b/gcc/doc/invoke.texi
>> @@ -946,8 +946,8 @@ Objective-C and Objective-C++ Dialects}.
>>
>>  @emph{eBPF Options}
>>  @gccoptlist{-mbig-endian -mlittle-endian -mkernel=@var{version}
>> --mframe-limit=@var{bytes} -mxbpf -mco-re -mno-co-re
>> --mjmpext -mjmp32 -malu32 -mcpu=@var{version}}
>> +-mframe-limit=@var{bytes} -mxbpf -mco-re -mno-co-re -mjmpext
>> +-mjmp32 -malu32 -mcpu=@var{version} -masm=@var{dialect>}}
>
> There is a spurious > character there.
>
> Other than that, the patch is OK.
> Thanks!

Fixed the extra character and committed.
Thanks !


Re: [COMMITTED] bpf: fixed template for neg (added second operand)

2023-07-21 Thread Cupertino Miranda via Gcc-patches


This patch fixes define_insn for "neg" to support 2 operands.
Initial implementation assumed the format "neg %0" while the instruction
allows both a destination and source operands. The second operand can
either be a register or an immediate value.

gcc/ChangeLog:

* config/bpf/bpf.md: fixed template for neg instruction.
---
 gcc/config/bpf/bpf.md | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/gcc/config/bpf/bpf.md b/gcc/config/bpf/bpf.md
index 329f62f55c33..adf11e151df1 100644
--- a/gcc/config/bpf/bpf.md
+++ b/gcc/config/bpf/bpf.md
@@ -139,10 +139,10 @@

 ;;; Negation
 (define_insn "neg2"
-  [(set (match_operand:AM 0 "register_operand" "=r")
-(neg:AM (match_operand:AM 1 "register_operand" " 0")))]
+  [(set (match_operand:AM 0 "register_operand" "=r,r")
+(neg:AM (match_operand:AM 1 "register_operand" " r,I")))]
   ""
-  "neg\t%0"
+  "neg\t%0,%1"
   [(set_attr "type" "")])

 ;;; Multiplication
--
2.38.1


[PATCH v4] bpf: fixed template for neg (added second operand)

2023-07-21 Thread Cupertino Miranda via Gcc-patches
This patch fixes define_insn for "neg" to support 2 operands.
Initial implementation assumed the format "neg %0" while the instruction
allows both a destination and source operands. The second operand can
either be a register or an immediate value.

gcc/ChangeLog:

* config/bpf/bpf.md: fixed template for neg instruction.
---
 gcc/config/bpf/bpf.md | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/gcc/config/bpf/bpf.md b/gcc/config/bpf/bpf.md
index 329f62f55c33..adf11e151df1 100644
--- a/gcc/config/bpf/bpf.md
+++ b/gcc/config/bpf/bpf.md
@@ -139,10 +139,10 @@
 
 ;;; Negation
 (define_insn "neg2"
-  [(set (match_operand:AM 0 "register_operand" "=r")
-(neg:AM (match_operand:AM 1 "register_operand" " 0")))]
+  [(set (match_operand:AM 0 "register_operand" "=r,r")
+(neg:AM (match_operand:AM 1 "register_operand" " r,I")))]
   ""
-  "neg\t%0"
+  "neg\t%0,%1"
   [(set_attr "type" "")])
 
 ;;; Multiplication
-- 
2.38.1



[COMMITTED] MAINTAINERS: Add myself to write after approval

2023-07-21 Thread Cupertino Miranda via Gcc-patches


Hi everyone,

Just to confirm that I pushed the change in MAINTAINERS file, adding
myself to the write after approval list.

Thanks,
Cupertino


Re: [PATCH v3] bpf: fixed template for neg (added second operand)

2023-07-21 Thread Cupertino Miranda via Gcc-patches
>From 7756a4becd1934e55d6d14ac4a9fd6d408a4797b Mon Sep 17 00:00:00 2001
From: Cupertino Miranda 
Date: Fri, 21 Jul 2023 17:40:07 +0100
Subject: [PATCH v3] bpf: fixed template for neg (added second operand)

gcc/ChangeLog:

	* config/bpf/bpf.md: fixed template for neg instruction.
---
 gcc/config/bpf/bpf.md | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/gcc/config/bpf/bpf.md b/gcc/config/bpf/bpf.md
index 329f62f55c33..adf11e151df1 100644
--- a/gcc/config/bpf/bpf.md
+++ b/gcc/config/bpf/bpf.md
@@ -139,10 +139,10 @@
 
 ;;; Negation
 (define_insn "neg2"
-  [(set (match_operand:AM 0 "register_operand" "=r")
-(neg:AM (match_operand:AM 1 "register_operand" " 0")))]
+  [(set (match_operand:AM 0 "register_operand" "=r,r")
+(neg:AM (match_operand:AM 1 "register_operand" " r,I")))]
   ""
-  "neg\t%0"
+  "neg\t%0,%1"
   [(set_attr "type" "")])
 
 ;;; Multiplication
-- 
2.30.2



Re: [PATCH v2] bpf: fixed template for neg (added second operand)

2023-07-21 Thread Cupertino Miranda via Gcc-patches
>From 9db2044c1d20bd9f05acf3c910ad0ffc9d5fda8f Mon Sep 17 00:00:00 2001
From: Cupertino Miranda 
Date: Fri, 21 Jul 2023 17:40:07 +0100
Subject: [PATCH v2] bpf: fixed template for neg (added second operand)

gcc/ChangeLog:

	* config/bpf/bpf.md: fixed template for neg instruction.
---
 gcc/config/bpf/bpf.md | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/gcc/config/bpf/bpf.md b/gcc/config/bpf/bpf.md
index 329f62f55c33..2ba862f3935a 100644
--- a/gcc/config/bpf/bpf.md
+++ b/gcc/config/bpf/bpf.md
@@ -139,10 +139,10 @@
 
 ;;; Negation
 (define_insn "neg2"
-  [(set (match_operand:AM 0 "register_operand" "=r")
-(neg:AM (match_operand:AM 1 "register_operand" " 0")))]
+  [(set (match_operand:AM 0 "register_operand" "=r")
+(neg:AM (match_operand:AM 1 "register_operand" " r")))]
   ""
-  "neg\t%0"
+  "neg\t%0,%1"
   [(set_attr "type" "")])
 
 ;;; Multiplication
-- 
2.30.2



[PATCH] bpf: fixed template for neg (added second operand)

2023-07-21 Thread Cupertino Miranda via Gcc-patches
gcc/ChangeLog:

* config/bpf/bpf.md: fixed template for neg instruction.
---
 gcc/config/bpf/bpf.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gcc/config/bpf/bpf.md b/gcc/config/bpf/bpf.md
index 329f62f55c33..bb414d8a4428 100644
--- a/gcc/config/bpf/bpf.md
+++ b/gcc/config/bpf/bpf.md
@@ -142,7 +142,7 @@
   [(set (match_operand:AM 0 "register_operand" "=r")
 (neg:AM (match_operand:AM 1 "register_operand" " 0")))]
   ""
-  "neg\t%0"
+  "neg\t%0,%1"
   [(set_attr "type" "")])
 
 ;;; Multiplication
-- 
2.30.2



Re: [PATCH v3] bpf: pseudo-c assembly dialect support

2023-07-21 Thread Cupertino Miranda via Gcc-patches

Thanks for the suggestions/fixes in changelog.
Inlined new patch.

Cupertino

>> gcc/ChangeLog:
>>
>>  * config/bpf/bpf.opt: Added option -masm=.
>>  * config/bpf/bpf-opts.h: Likewize.
>>  * config/bpf/bpf.cc: Changed it to conform with new pseudoc
>>dialect support.
>>  * config/bpf/bpf.h: Likewise.
>>  * config/bpf/bpf.md: Added pseudo-c templates.
>>  * doc/invoke.texi: (-masm=DIALECT) New eBPF option item.
>
> I think the ChangeLog could be made more useful, and the syntax of the
> last entry is not entirely right.  I suggest something like:
>
>   * config/bpf/bpf.opt: Added option -masm=.
>   * config/bpf/bpf-opts.h (enum bpf_asm_dialect): New type.
>   * config/bpf/bpf.cc (bpf_print_register): New function.
>   (bpf_print_register): Support pseudo-c syntax for registers.
>   (bpf_print_operand_address): Likewise.
>   * config/bpf/bpf.h (ASM_SPEC): handle -msasm.
>   (ASSEMBLER_DIALECT): Define.
>   * config/bpf/bpf.md: Added pseudo-c templates.
>   * doc/invoke.texi (-masm=DIALECT): New eBPF option item.
>
> Please make sure to run the contrib/gcc-changelog/git_check-commit.py
> script.
>

>From 6ebe3229a59b32ffb2ed24b3a2cf8c360a807c31 Mon Sep 17 00:00:00 2001
From: Cupertino Miranda 
Date: Mon, 17 Jul 2023 17:42:42 +0100
Subject: [PATCH v3] bpf: pseudo-c assembly dialect support

New pseudo-c BPF assembly dialect already supported by clang and widely
used in the linux kernel.

gcc/ChangeLog:

	* config/bpf/bpf.opt: Added option -masm=.
	* config/bpf/bpf-opts.h (enum bpf_asm_dialect): New type.
	* config/bpf/bpf.cc (bpf_print_register): New function.
	(bpf_print_register): Support pseudo-c syntax for registers.
	(bpf_print_operand_address): Likewise.
	* config/bpf/bpf.h (ASM_SPEC): handle -msasm.
	(ASSEMBLER_DIALECT): Define.
	* config/bpf/bpf.md: Added pseudo-c templates.
	* doc/invoke.texi (-masm=): New eBPF option item.
---
 gcc/config/bpf/bpf-opts.h |  6 +++
 gcc/config/bpf/bpf.cc | 46 ---
 gcc/config/bpf/bpf.h  |  5 +-
 gcc/config/bpf/bpf.md | 97 ---
 gcc/config/bpf/bpf.opt| 14 ++
 gcc/doc/invoke.texi   | 21 -
 6 files changed, 133 insertions(+), 56 deletions(-)

diff --git a/gcc/config/bpf/bpf-opts.h b/gcc/config/bpf/bpf-opts.h
index 8282351cf045..92db01ec4d54 100644
--- a/gcc/config/bpf/bpf-opts.h
+++ b/gcc/config/bpf/bpf-opts.h
@@ -60,4 +60,10 @@ enum bpf_isa_version
   ISA_V3,
 };
 
+enum bpf_asm_dialect
+{
+  ASM_NORMAL,
+  ASM_PSEUDOC
+};
+
 #endif /* ! BPF_OPTS_H */
diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
index e0324e1e0e08..1d3936871d60 100644
--- a/gcc/config/bpf/bpf.cc
+++ b/gcc/config/bpf/bpf.cc
@@ -873,16 +873,47 @@ bpf_output_call (rtx target)
   return "";
 }
 
+/* Print register name according to assembly dialect.
+   In normal syntax registers are printed like %rN where N is the
+   register number.
+   In pseudoc syntax, the register names do not feature a '%' prefix.
+   Additionally, the code 'w' denotes that the register should be printed
+   as wN instead of rN, where N is the register number, but only when the
+   value stored in the operand OP is 32-bit wide.  */
+static void
+bpf_print_register (FILE *file, rtx op, int code)
+{
+  if(asm_dialect == ASM_NORMAL)
+fprintf (file, "%s", reg_names[REGNO (op)]);
+  else
+{
+  if (code == 'w' && GET_MODE (op) == SImode)
+	{
+	  if (REGNO (op) == BPF_FP)
+	fprintf (file, "w10");
+	  else
+	fprintf (file, "w%s", reg_names[REGNO (op)]+2);
+	}
+  else
+	{
+	  if (REGNO (op) == BPF_FP)
+	fprintf (file, "r10");
+	  else
+	fprintf (file, "%s", reg_names[REGNO (op)]+1);
+	}
+}
+}
+
 /* Print an instruction operand.  This function is called in the macro
PRINT_OPERAND defined in bpf.h */
 
 void
-bpf_print_operand (FILE *file, rtx op, int code ATTRIBUTE_UNUSED)
+bpf_print_operand (FILE *file, rtx op, int code)
 {
   switch (GET_CODE (op))
 {
 case REG:
-  fprintf (file, "%s", reg_names[REGNO (op)]);
+  bpf_print_register (file, op, code);
   break;
 case MEM:
   output_address (GET_MODE (op), XEXP (op, 0));
@@ -936,7 +967,9 @@ bpf_print_operand_address (FILE *file, rtx addr)
   switch (GET_CODE (addr))
 {
 case REG:
-  fprintf (file, "[%s+0]", reg_names[REGNO (addr)]);
+  fprintf (file, asm_dialect == ASM_NORMAL ? "[" : "(");
+  bpf_print_register (file, addr, 0);
+  fprintf (file, asm_dialect == ASM_NORMAL ? "+0]" : "+0)");
   break;
 case PLUS:
   {
@@ -945,9 +978,11 @@ bpf_print_operand_address (FILE *file, rtx addr)
 
 	if (GET_CODE (op0) == REG && GET_CODE (op1) == CONST_INT)
 	  {
-	fprintf (file, "[%s+&q

Re: [PATCH v2] bpf: pseudo-c assembly dialect support

2023-07-21 Thread Cupertino Miranda via Gcc-patches

Hi Jose,

Thanks for the review.
New patch is inline attached.

Regards,
Cupertino

Jose E. Marchesi writes:

> Hello Cuper.
>
> Thanks for the patch.
>
> We will need an update for the "eBPF Options" section in the GCC manual,
> documenting -masm=@var{dialect} and the supported values.  Can you
> please add it and re-submit?
>
>
>> Hi everyone,
>>
>> Looking forward to all your reviews.
>>
>> Best regards,
>> Cupertino


>From fa227fefd84e6eaaf8edafed698e9960d7b115e6 Mon Sep 17 00:00:00 2001
From: Cupertino Miranda 
Date: Mon, 17 Jul 2023 17:42:42 +0100
Subject: [PATCH v2] bpf: pseudo-c assembly dialect support

New pseudo-c BPF assembly dialect already supported by clang and widely
used in the linux kernel.

gcc/ChangeLog:

	* config/bpf/bpf.opt: Added option -masm=.
	* config/bpf/bpf-opts.h: Likewize.
	* config/bpf/bpf.cc: Changed it to conform with new pseudoc
	  dialect support.
	* config/bpf/bpf.h: Likewise.
	* config/bpf/bpf.md: Added pseudo-c templates.
	* doc/invoke.texi: (-masm=DIALECT) New eBPF option item.
---
 gcc/config/bpf/bpf-opts.h |  6 +++
 gcc/config/bpf/bpf.cc | 46 ---
 gcc/config/bpf/bpf.h  |  5 +-
 gcc/config/bpf/bpf.md | 97 ---
 gcc/config/bpf/bpf.opt| 14 ++
 gcc/doc/invoke.texi   | 21 -
 6 files changed, 133 insertions(+), 56 deletions(-)

diff --git a/gcc/config/bpf/bpf-opts.h b/gcc/config/bpf/bpf-opts.h
index 8282351cf045..92db01ec4d54 100644
--- a/gcc/config/bpf/bpf-opts.h
+++ b/gcc/config/bpf/bpf-opts.h
@@ -60,4 +60,10 @@ enum bpf_isa_version
   ISA_V3,
 };
 
+enum bpf_asm_dialect
+{
+  ASM_NORMAL,
+  ASM_PSEUDOC
+};
+
 #endif /* ! BPF_OPTS_H */
diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
index e0324e1e0e08..1d3936871d60 100644
--- a/gcc/config/bpf/bpf.cc
+++ b/gcc/config/bpf/bpf.cc
@@ -873,16 +873,47 @@ bpf_output_call (rtx target)
   return "";
 }
 
+/* Print register name according to assembly dialect.
+   In normal syntax registers are printed like %rN where N is the
+   register number.
+   In pseudoc syntax, the register names do not feature a '%' prefix.
+   Additionally, the code 'w' denotes that the register should be printed
+   as wN instead of rN, where N is the register number, but only when the
+   value stored in the operand OP is 32-bit wide.  */
+static void
+bpf_print_register (FILE *file, rtx op, int code)
+{
+  if(asm_dialect == ASM_NORMAL)
+fprintf (file, "%s", reg_names[REGNO (op)]);
+  else
+{
+  if (code == 'w' && GET_MODE (op) == SImode)
+	{
+	  if (REGNO (op) == BPF_FP)
+	fprintf (file, "w10");
+	  else
+	fprintf (file, "w%s", reg_names[REGNO (op)]+2);
+	}
+  else
+	{
+	  if (REGNO (op) == BPF_FP)
+	fprintf (file, "r10");
+	  else
+	fprintf (file, "%s", reg_names[REGNO (op)]+1);
+	}
+}
+}
+
 /* Print an instruction operand.  This function is called in the macro
PRINT_OPERAND defined in bpf.h */
 
 void
-bpf_print_operand (FILE *file, rtx op, int code ATTRIBUTE_UNUSED)
+bpf_print_operand (FILE *file, rtx op, int code)
 {
   switch (GET_CODE (op))
 {
 case REG:
-  fprintf (file, "%s", reg_names[REGNO (op)]);
+  bpf_print_register (file, op, code);
   break;
 case MEM:
   output_address (GET_MODE (op), XEXP (op, 0));
@@ -936,7 +967,9 @@ bpf_print_operand_address (FILE *file, rtx addr)
   switch (GET_CODE (addr))
 {
 case REG:
-  fprintf (file, "[%s+0]", reg_names[REGNO (addr)]);
+  fprintf (file, asm_dialect == ASM_NORMAL ? "[" : "(");
+  bpf_print_register (file, addr, 0);
+  fprintf (file, asm_dialect == ASM_NORMAL ? "+0]" : "+0)");
   break;
 case PLUS:
   {
@@ -945,9 +978,11 @@ bpf_print_operand_address (FILE *file, rtx addr)
 
 	if (GET_CODE (op0) == REG && GET_CODE (op1) == CONST_INT)
 	  {
-	fprintf (file, "[%s+", reg_names[REGNO (op0)]);
+	fprintf (file, asm_dialect == ASM_NORMAL ? "[" : "(");
+	bpf_print_register (file, op0, 0);
+	fprintf (file, "+");
 	output_addr_const (file, op1);
-	fputs ("]", file);
+	fprintf (file, asm_dialect == ASM_NORMAL ? "]" : ")");
 	  }
 	else
 	  fatal_insn ("invalid address in operand", addr);
@@ -1816,7 +1851,6 @@ handle_attr_preserve (function *fn)
 }
 }
 
-
 /* This pass finds accesses to structures marked with the BPF target attribute
__attribute__((preserve_access_index)). For every such access, a CO-RE
relocation record is generated, to be output in the .BTF.ext section.  */
diff --git a/gcc/config/bpf/bpf.h b/gcc/config/bpf/bpf.h
index 344aca02d1bb..9561bf59b800 100644
--- a/gcc/config/bpf/bpf.h
+++ b/gcc/config/bpf/bpf.h
@@ -22,7 +22,8 @@
 
 / Controlling the Compilation Dri

[PATCH] bpf: pseudo-c assembly dialect support

2023-07-21 Thread Cupertino Miranda via Gcc-patches
Hi everyone,

Looking forward to all your reviews.

Best regards,
Cupertino

New pseudo-c BPF assembly dialect already supported by clang and widely
used in the linux kernel.

gcc/ChangeLog:

* config/bpf/bpf.opt: Added option -masm=.
* config/bpf/bpf-opts.h: Likewize.
* config/bpf/bpf.cc: Changed it to conform with new pseudoc
  dialect support.
* config/bpf/bpf.h: Likewise.
* config/bpf/bpf.md: Added pseudo-c templates.
---
 gcc/config/bpf/bpf-opts.h |  6 +++
 gcc/config/bpf/bpf.cc | 46 ---
 gcc/config/bpf/bpf.h  |  5 +-
 gcc/config/bpf/bpf.md | 97 ---
 gcc/config/bpf/bpf.opt| 14 ++
 5 files changed, 114 insertions(+), 54 deletions(-)

diff --git a/gcc/config/bpf/bpf-opts.h b/gcc/config/bpf/bpf-opts.h
index 8282351cf045..92db01ec4d54 100644
--- a/gcc/config/bpf/bpf-opts.h
+++ b/gcc/config/bpf/bpf-opts.h
@@ -60,4 +60,10 @@ enum bpf_isa_version
   ISA_V3,
 };
 
+enum bpf_asm_dialect
+{
+  ASM_NORMAL,
+  ASM_PSEUDOC
+};
+
 #endif /* ! BPF_OPTS_H */
diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
index e0324e1e0e08..1d3936871d60 100644
--- a/gcc/config/bpf/bpf.cc
+++ b/gcc/config/bpf/bpf.cc
@@ -873,16 +873,47 @@ bpf_output_call (rtx target)
   return "";
 }
 
+/* Print register name according to assembly dialect.
+   In normal syntax registers are printed like %rN where N is the
+   register number.
+   In pseudoc syntax, the register names do not feature a '%' prefix.
+   Additionally, the code 'w' denotes that the register should be printed
+   as wN instead of rN, where N is the register number, but only when the
+   value stored in the operand OP is 32-bit wide.  */
+static void
+bpf_print_register (FILE *file, rtx op, int code)
+{
+  if(asm_dialect == ASM_NORMAL)
+fprintf (file, "%s", reg_names[REGNO (op)]);
+  else
+{
+  if (code == 'w' && GET_MODE (op) == SImode)
+   {
+ if (REGNO (op) == BPF_FP)
+   fprintf (file, "w10");
+ else
+   fprintf (file, "w%s", reg_names[REGNO (op)]+2);
+   }
+  else
+   {
+ if (REGNO (op) == BPF_FP)
+   fprintf (file, "r10");
+ else
+   fprintf (file, "%s", reg_names[REGNO (op)]+1);
+   }
+}
+}
+
 /* Print an instruction operand.  This function is called in the macro
PRINT_OPERAND defined in bpf.h */
 
 void
-bpf_print_operand (FILE *file, rtx op, int code ATTRIBUTE_UNUSED)
+bpf_print_operand (FILE *file, rtx op, int code)
 {
   switch (GET_CODE (op))
 {
 case REG:
-  fprintf (file, "%s", reg_names[REGNO (op)]);
+  bpf_print_register (file, op, code);
   break;
 case MEM:
   output_address (GET_MODE (op), XEXP (op, 0));
@@ -936,7 +967,9 @@ bpf_print_operand_address (FILE *file, rtx addr)
   switch (GET_CODE (addr))
 {
 case REG:
-  fprintf (file, "[%s+0]", reg_names[REGNO (addr)]);
+  fprintf (file, asm_dialect == ASM_NORMAL ? "[" : "(");
+  bpf_print_register (file, addr, 0);
+  fprintf (file, asm_dialect == ASM_NORMAL ? "+0]" : "+0)");
   break;
 case PLUS:
   {
@@ -945,9 +978,11 @@ bpf_print_operand_address (FILE *file, rtx addr)
 
if (GET_CODE (op0) == REG && GET_CODE (op1) == CONST_INT)
  {
-   fprintf (file, "[%s+", reg_names[REGNO (op0)]);
+   fprintf (file, asm_dialect == ASM_NORMAL ? "[" : "(");
+   bpf_print_register (file, op0, 0);
+   fprintf (file, "+");
output_addr_const (file, op1);
-   fputs ("]", file);
+   fprintf (file, asm_dialect == ASM_NORMAL ? "]" : ")");
  }
else
  fatal_insn ("invalid address in operand", addr);
@@ -1816,7 +1851,6 @@ handle_attr_preserve (function *fn)
 }
 }
 
-
 /* This pass finds accesses to structures marked with the BPF target attribute
__attribute__((preserve_access_index)). For every such access, a CO-RE
relocation record is generated, to be output in the .BTF.ext section.  */
diff --git a/gcc/config/bpf/bpf.h b/gcc/config/bpf/bpf.h
index 344aca02d1bb..9561bf59b800 100644
--- a/gcc/config/bpf/bpf.h
+++ b/gcc/config/bpf/bpf.h
@@ -22,7 +22,8 @@
 
 / Controlling the Compilation Driver.  */
 
-#define ASM_SPEC "%{mbig-endian:-EB} %{!mbig-endian:-EL} %{mxbpf:-mxbpf}"
+#define ASM_SPEC "%{mbig-endian:-EB} %{!mbig-endian:-EL} %{mxbpf:-mxbpf} " \
+  "%{masm=pseudoc:-mdialect=pseudoc}"
 #define LINK_SPEC "%{mbig-endian:-EB} %{!mbig-endian:-EL}"
 #define LIB_SPEC ""
 #define STARTFILE_SPEC ""
@@ -503,4 +504,6 @@ enum reg_class
 #define DO_GLOBAL_DTORS_BODY   \
   do { } while (0)
 
+#define ASSEMBLER_DIALECT ((int) asm_dialect)
+
 #endif /* ! GCC_BPF_H */
diff --git a/gcc/config/bpf/bpf.md b/gcc/config/bpf/bpf.md
index f6be0a212345..0b8f409db687 100644
--- a/gcc/config/bpf/bpf.md
+++ b/gcc/config/bpf/bpf.md
@@ -77,6 +77,8 @@
 
 (define_mode_attr mop [(QI "b") (HI "h") (SI "w") (DI "dw")

Re: [PATCH 2/2] Corrected pr25521.c target matching.

2023-03-13 Thread Cupertino Miranda via Gcc-patches

Cupertino Miranda via Gcc-patches writes:

>> On 1/24/23 05:24, Cupertino Miranda wrote:
>>> Thank you for the comments and suggestions.
>>> I have changed the patch.
>>> Unfortunately in case of rx target I could not make
>>> scan-assembler-symbol-section to match. I believe it is because the
>>> .section and .global entries order is reversed in this target.
>>> Patch in inlined below. looking forward to your comments.
>> Sorry for the long delays.   I've installed this version.
>>
>> As a follow-up, can you update the documentation in doc/sourcebuild.texi to
>> include the new check-effective-target test?
>
> Hi Jeff,
>
> Thank you for installing the patch.
> I have prepared the doc change you requested.
> Hopefully this is what you were expecting.
>
> Regards,
> Cupertino

Just realized previous patch was in incorrect placement alphabetically.
Please consider this one instead.

Cupertino

>From 2a4f87294ad0af037b80e13d67678e52bc382c95 Mon Sep 17 00:00:00 2001
From: Cupertino Miranda 
Date: Mon, 13 Mar 2023 17:45:14 +
Subject: [PATCH] Added item entry in docs for the new check_effective_target.

More precisely for:
 - check_effective_target_const_volatile_readonly_section
---
 gcc/doc/sourcebuild.texi | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/gcc/doc/sourcebuild.texi b/gcc/doc/sourcebuild.texi
index be4318221cc..f08893aeb02 100644
--- a/gcc/doc/sourcebuild.texi
+++ b/gcc/doc/sourcebuild.texi
@@ -2774,6 +2774,9 @@ Target supports automatic stack alignment.
 @item branch_cost
 Target supports @option{-branch-cost=N}.
 
+@item const_volatile_readonly_section
+Target places const volatile variables in readonly sections.
+
 @item cxa_atexit
 Target uses @code{__cxa_atexit}.
 
-- 
2.38.1



Re: [PATCH 2/2] Corrected pr25521.c target matching.

2023-03-13 Thread Cupertino Miranda via Gcc-patches

> On 1/24/23 05:24, Cupertino Miranda wrote:
>> Thank you for the comments and suggestions.
>> I have changed the patch.
>> Unfortunately in case of rx target I could not make
>> scan-assembler-symbol-section to match. I believe it is because the
>> .section and .global entries order is reversed in this target.
>> Patch in inlined below. looking forward to your comments.
> Sorry for the long delays.   I've installed this version.
>
> As a follow-up, can you update the documentation in doc/sourcebuild.texi to
> include the new check-effective-target test?

Hi Jeff,

Thank you for installing the patch.
I have prepared the doc change you requested.
Hopefully this is what you were expecting.

Regards,
Cupertino

>From ea2a0434014516c7f680d24f0e702b407be1b83e Mon Sep 17 00:00:00 2001
From: Cupertino Miranda 
Date: Mon, 13 Mar 2023 17:45:14 +
Subject: [PATCH] Added item entry in docs for the new check_effective_target.

More precisely for:
 - check_effective_target_const_volatile_readonly_section
---
 gcc/doc/sourcebuild.texi | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/gcc/doc/sourcebuild.texi b/gcc/doc/sourcebuild.texi
index be4318221cc..c614292f864 100644
--- a/gcc/doc/sourcebuild.texi
+++ b/gcc/doc/sourcebuild.texi
@@ -2777,6 +2777,9 @@ Target supports @option{-branch-cost=N}.
 @item cxa_atexit
 Target uses @code{__cxa_atexit}.
 
+@item const_volatile_readonly_section
+Target places const volatile variables in readonly sections.
+
 @item default_packed
 @anchor{default_packed}
 Target has packed layout of structure members by default.
-- 
2.38.1



Re: [PING, PING] Re: [PATCH 2/2] Corrected pr25521.c target matching.

2023-03-09 Thread Cupertino Miranda via Gcc-patches


[PING]

Cupertino Miranda writes:

> Hi Jeff,
>
> Please, please, give me some feedback on this one.
> I just don't want to have to keep asking you for time on this small
> pending patches that I also have to keep track on.
>
> I realized your committed the other one. Thank you !
>
> Best regards,
> Cupertino
>
>
> Cupertino Miranda writes:
>
>> PING !
>>
>> Cupertino Miranda via Gcc-patches writes:
>>
>>> Hi Jeff,
>>>
>>> Can you please confirm if the patch is Ok?
>>>
>>> Thanks,
>>> Cupertino
>>>
>>>> Cupertino Miranda via Gcc-patches writes:
>>>>
>>>>> Thank you for the comments and suggestions.
>>>>> I have changed the patch.
>>>>>
>>>>> Unfortunately in case of rx target I could not make
>>>>> scan-assembler-symbol-section to match. I believe it is because the
>>>>> .section and .global entries order is reversed in this target.
>>>>>
>>>>> Patch in inlined below. looking forward to your comments.
>>>>>
>>>>> Cupertino
>>>>>
>>>>> diff --git a/gcc/testsuite/gcc.dg/pr25521.c 
>>>>> b/gcc/testsuite/gcc.dg/pr25521.c
>>>>> index 63363a03b9f..82b4cd88ec0 100644
>>>>> --- a/gcc/testsuite/gcc.dg/pr25521.c
>>>>> +++ b/gcc/testsuite/gcc.dg/pr25521.c
>>>>> @@ -2,9 +2,10 @@
>>>>> sections.
>>>>>
>>>>> { dg-require-effective-target elf }
>>>>> -   { dg-do compile } */
>>>>> +   { dg-do compile }
>>>>> +   { dg-skip-if "" { ! const_volatile_readonly_section } } */
>>>>>
>>>>>  const volatile int foo = 30;
>>>>>
>>>>> -
>>>>> -/* { dg-final { scan-assembler "\\.s\?rodata" } } */
>>>>> +/* { dg-final { scan-assembler {.section C,} { target { rx-*-* } } } } */
>>>>> +/* { dg-final { scan-assembler-symbol-section {^_?foo$} 
>>>>> {^\.(const|s?rodata)} { target { ! "rx-*-*" } } } } */
>>>>> diff --git a/gcc/testsuite/lib/target-supports.exp 
>>>>> b/gcc/testsuite/lib/target-supports.exp
>>>>> index c0694af2338..91aafd89909 100644
>>>>> --- a/gcc/testsuite/lib/target-supports.exp
>>>>> +++ b/gcc/testsuite/lib/target-supports.exp
>>>>> @@ -12295,3 +12295,13 @@ proc check_is_prog_name_available { prog } {
>>>>>
>>>>>  return 1
>>>>>  }
>>>>> +
>>>>> +# returns 1 if target does selects a readonly section for const volatile 
>>>>> variables.
>>>>> +proc check_effective_target_const_volatile_readonly_section { } {
>>>>> +
>>>>> +if { [istarget powerpc-*-*]
>>>>> +   || [check-flags { "" { powerpc64-*-* } { -m32 } }] } {
>>>>> + return 0
>>>>> +}
>>>>> +  return 1
>>>>> +}
>>>>>
>>>>>
>>>>> Jeff Law writes:
>>>>>
>>>>>> On 12/7/22 08:45, Cupertino Miranda wrote:
>>>>>>>
>>>>>>>> On 12/2/22 10:52, Cupertino Miranda via Gcc-patches wrote:
>>>>>>>>> This commit is a follow up of bugzilla #107181.
>>>>>>>>> The commit /a0aafbc/ changed the default implementation of the
>>>>>>>>> SELECT_SECTION hook in order to match clang/llvm behaviour w.r.t the
>>>>>>>>> placement of `const volatile' objects.
>>>>>>>>> However, the following targets use target-specific selection functions
>>>>>>>>> and they choke on the testcase pr25521.c:
>>>>>>>>>*rx - target sets its const variables as '.section 
>>>>>>>>> C,"a",@progbits'.
>>>>>>>> That's presumably a constant section.  We should instead twiddle the 
>>>>>>>> test to
>>>>>>>> recognize that section.
>>>>>>> Although @progbits is indeed a constant section, I believe it is
>>>>>>> more interesting to detect if the `rx' starts selecting more
>>>>>>> standard sections instead of the current @progbits.
>>>>>>> That was the reason why I opted to XFAIL instead of PASSing it.
>>>>>>> Can I keep it as such ?
>>>>>> I'm not aware of any ongoing development for that port, so I would not 
>>>>>> let
>>>>>> concerns about the rx port changing behavior dominate how we approach 
>>>>>> this
>>>>>> problem.
>>>>>>
>>>>>> The rx port is using a different name for the section.  That's  valid 
>>>>>> thing to
>>>>>> do and to the extent we can, we should support that in the test rather 
>>>>>> than
>>>>>> (incorrectly IMHO) xfailing the test just becuase the name isn't what we
>>>>>> expected.
>>>>>>
>>>>>> To avoid over-eagerly matching, I would probably search for "C,"  I 
>>>>>> wouldn't do
>>>>>> that for the const or rodata sections as they often have a suffix like 
>>>>>> 1, 2, 4,
>>>>>> 8 for different sized rodata sections.
>>>>>>
>>>>>> PPC32 is explicitly doing something different and placing those objects 
>>>>>> into an
>>>>>> RW section.  So for PPC32 it makes more sense to skip the test rather 
>>>>>> than xfail
>>>>>> it.
>>>>>>
>>>>>> Jeff


Re: [PING] Re: [PATCH 2/2] Corrected pr25521.c target matching.

2023-02-27 Thread Cupertino Miranda via Gcc-patches


Hi Jeff,

Please, please, give me some feedback on this one.
I just don't want to have to keep asking you for time on this small
pending patches that I also have to keep track on.

I realized your committed the other one. Thank you !

Best regards,
Cupertino


Cupertino Miranda writes:

> PING !
>
> Cupertino Miranda via Gcc-patches writes:
>
>> Hi Jeff,
>>
>> Can you please confirm if the patch is Ok?
>>
>> Thanks,
>> Cupertino
>>
>>> Cupertino Miranda via Gcc-patches writes:
>>>
>>>> Thank you for the comments and suggestions.
>>>> I have changed the patch.
>>>>
>>>> Unfortunately in case of rx target I could not make
>>>> scan-assembler-symbol-section to match. I believe it is because the
>>>> .section and .global entries order is reversed in this target.
>>>>
>>>> Patch in inlined below. looking forward to your comments.
>>>>
>>>> Cupertino
>>>>
>>>> diff --git a/gcc/testsuite/gcc.dg/pr25521.c 
>>>> b/gcc/testsuite/gcc.dg/pr25521.c
>>>> index 63363a03b9f..82b4cd88ec0 100644
>>>> --- a/gcc/testsuite/gcc.dg/pr25521.c
>>>> +++ b/gcc/testsuite/gcc.dg/pr25521.c
>>>> @@ -2,9 +2,10 @@
>>>> sections.
>>>>
>>>> { dg-require-effective-target elf }
>>>> -   { dg-do compile } */
>>>> +   { dg-do compile }
>>>> +   { dg-skip-if "" { ! const_volatile_readonly_section } } */
>>>>
>>>>  const volatile int foo = 30;
>>>>
>>>> -
>>>> -/* { dg-final { scan-assembler "\\.s\?rodata" } } */
>>>> +/* { dg-final { scan-assembler {.section C,} { target { rx-*-* } } } } */
>>>> +/* { dg-final { scan-assembler-symbol-section {^_?foo$} 
>>>> {^\.(const|s?rodata)} { target { ! "rx-*-*" } } } } */
>>>> diff --git a/gcc/testsuite/lib/target-supports.exp 
>>>> b/gcc/testsuite/lib/target-supports.exp
>>>> index c0694af2338..91aafd89909 100644
>>>> --- a/gcc/testsuite/lib/target-supports.exp
>>>> +++ b/gcc/testsuite/lib/target-supports.exp
>>>> @@ -12295,3 +12295,13 @@ proc check_is_prog_name_available { prog } {
>>>>
>>>>  return 1
>>>>  }
>>>> +
>>>> +# returns 1 if target does selects a readonly section for const volatile 
>>>> variables.
>>>> +proc check_effective_target_const_volatile_readonly_section { } {
>>>> +
>>>> +if { [istarget powerpc-*-*]
>>>> +|| [check-flags { "" { powerpc64-*-* } { -m32 } }] } {
>>>> +  return 0
>>>> +}
>>>> +  return 1
>>>> +}
>>>>
>>>>
>>>> Jeff Law writes:
>>>>
>>>>> On 12/7/22 08:45, Cupertino Miranda wrote:
>>>>>>
>>>>>>> On 12/2/22 10:52, Cupertino Miranda via Gcc-patches wrote:
>>>>>>>> This commit is a follow up of bugzilla #107181.
>>>>>>>> The commit /a0aafbc/ changed the default implementation of the
>>>>>>>> SELECT_SECTION hook in order to match clang/llvm behaviour w.r.t the
>>>>>>>> placement of `const volatile' objects.
>>>>>>>> However, the following targets use target-specific selection functions
>>>>>>>> and they choke on the testcase pr25521.c:
>>>>>>>>*rx - target sets its const variables as '.section C,"a",@progbits'.
>>>>>>> That's presumably a constant section.  We should instead twiddle the 
>>>>>>> test to
>>>>>>> recognize that section.
>>>>>> Although @progbits is indeed a constant section, I believe it is
>>>>>> more interesting to detect if the `rx' starts selecting more
>>>>>> standard sections instead of the current @progbits.
>>>>>> That was the reason why I opted to XFAIL instead of PASSing it.
>>>>>> Can I keep it as such ?
>>>>> I'm not aware of any ongoing development for that port, so I would not let
>>>>> concerns about the rx port changing behavior dominate how we approach this
>>>>> problem.
>>>>>
>>>>> The rx port is using a different name for the section.  That's  valid 
>>>>> thing to
>>>>> do and to the extent we can, we should support that in the test rather 
>>>>> than
>>>>> (incorrectly IMHO) xfailing the test just becuase the name isn't what we
>>>>> expected.
>>>>>
>>>>> To avoid over-eagerly matching, I would probably search for "C,"  I 
>>>>> wouldn't do
>>>>> that for the const or rodata sections as they often have a suffix like 1, 
>>>>> 2, 4,
>>>>> 8 for different sized rodata sections.
>>>>>
>>>>> PPC32 is explicitly doing something different and placing those objects 
>>>>> into an
>>>>> RW section.  So for PPC32 it makes more sense to skip the test rather 
>>>>> than xfail
>>>>> it.
>>>>>
>>>>> Jeff


Re: [PING] Re: [PATCH 2/2] Corrected pr25521.c target matching.

2023-02-17 Thread Cupertino Miranda via Gcc-patches


PING !

Cupertino Miranda via Gcc-patches writes:

> Hi Jeff,
>
> Can you please confirm if the patch is Ok?
>
> Thanks,
> Cupertino
>
>> Cupertino Miranda via Gcc-patches writes:
>>
>>> Thank you for the comments and suggestions.
>>> I have changed the patch.
>>>
>>> Unfortunately in case of rx target I could not make
>>> scan-assembler-symbol-section to match. I believe it is because the
>>> .section and .global entries order is reversed in this target.
>>>
>>> Patch in inlined below. looking forward to your comments.
>>>
>>> Cupertino
>>>
>>> diff --git a/gcc/testsuite/gcc.dg/pr25521.c b/gcc/testsuite/gcc.dg/pr25521.c
>>> index 63363a03b9f..82b4cd88ec0 100644
>>> --- a/gcc/testsuite/gcc.dg/pr25521.c
>>> +++ b/gcc/testsuite/gcc.dg/pr25521.c
>>> @@ -2,9 +2,10 @@
>>> sections.
>>>
>>> { dg-require-effective-target elf }
>>> -   { dg-do compile } */
>>> +   { dg-do compile }
>>> +   { dg-skip-if "" { ! const_volatile_readonly_section } } */
>>>
>>>  const volatile int foo = 30;
>>>
>>> -
>>> -/* { dg-final { scan-assembler "\\.s\?rodata" } } */
>>> +/* { dg-final { scan-assembler {.section C,} { target { rx-*-* } } } } */
>>> +/* { dg-final { scan-assembler-symbol-section {^_?foo$} 
>>> {^\.(const|s?rodata)} { target { ! "rx-*-*" } } } } */
>>> diff --git a/gcc/testsuite/lib/target-supports.exp 
>>> b/gcc/testsuite/lib/target-supports.exp
>>> index c0694af2338..91aafd89909 100644
>>> --- a/gcc/testsuite/lib/target-supports.exp
>>> +++ b/gcc/testsuite/lib/target-supports.exp
>>> @@ -12295,3 +12295,13 @@ proc check_is_prog_name_available { prog } {
>>>
>>>  return 1
>>>  }
>>> +
>>> +# returns 1 if target does selects a readonly section for const volatile 
>>> variables.
>>> +proc check_effective_target_const_volatile_readonly_section { } {
>>> +
>>> +if { [istarget powerpc-*-*]
>>> + || [check-flags { "" { powerpc64-*-* } { -m32 } }] } {
>>> +   return 0
>>> +}
>>> +  return 1
>>> +}
>>>
>>>
>>> Jeff Law writes:
>>>
>>>> On 12/7/22 08:45, Cupertino Miranda wrote:
>>>>>
>>>>>> On 12/2/22 10:52, Cupertino Miranda via Gcc-patches wrote:
>>>>>>> This commit is a follow up of bugzilla #107181.
>>>>>>> The commit /a0aafbc/ changed the default implementation of the
>>>>>>> SELECT_SECTION hook in order to match clang/llvm behaviour w.r.t the
>>>>>>> placement of `const volatile' objects.
>>>>>>> However, the following targets use target-specific selection functions
>>>>>>> and they choke on the testcase pr25521.c:
>>>>>>>*rx - target sets its const variables as '.section C,"a",@progbits'.
>>>>>> That's presumably a constant section.  We should instead twiddle the 
>>>>>> test to
>>>>>> recognize that section.
>>>>> Although @progbits is indeed a constant section, I believe it is
>>>>> more interesting to detect if the `rx' starts selecting more
>>>>> standard sections instead of the current @progbits.
>>>>> That was the reason why I opted to XFAIL instead of PASSing it.
>>>>> Can I keep it as such ?
>>>> I'm not aware of any ongoing development for that port, so I would not let
>>>> concerns about the rx port changing behavior dominate how we approach this
>>>> problem.
>>>>
>>>> The rx port is using a different name for the section.  That's  valid 
>>>> thing to
>>>> do and to the extent we can, we should support that in the test rather than
>>>> (incorrectly IMHO) xfailing the test just becuase the name isn't what we
>>>> expected.
>>>>
>>>> To avoid over-eagerly matching, I would probably search for "C,"  I 
>>>> wouldn't do
>>>> that for the const or rodata sections as they often have a suffix like 1, 
>>>> 2, 4,
>>>> 8 for different sized rodata sections.
>>>>
>>>> PPC32 is explicitly doing something different and placing those objects 
>>>> into an
>>>> RW section.  So for PPC32 it makes more sense to skip the test rather than 
>>>> xfail
>>>> it.
>>>>
>>>> Jeff


Re: [PING] Re: [PATCH 2/2] Corrected pr25521.c target matching.

2023-02-07 Thread Cupertino Miranda via Gcc-patches


Hi Jeff,

Can you please confirm if the patch is Ok?

Thanks,
Cupertino

> Cupertino Miranda via Gcc-patches writes:
>
>> Thank you for the comments and suggestions.
>> I have changed the patch.
>>
>> Unfortunately in case of rx target I could not make
>> scan-assembler-symbol-section to match. I believe it is because the
>> .section and .global entries order is reversed in this target.
>>
>> Patch in inlined below. looking forward to your comments.
>>
>> Cupertino
>>
>> diff --git a/gcc/testsuite/gcc.dg/pr25521.c b/gcc/testsuite/gcc.dg/pr25521.c
>> index 63363a03b9f..82b4cd88ec0 100644
>> --- a/gcc/testsuite/gcc.dg/pr25521.c
>> +++ b/gcc/testsuite/gcc.dg/pr25521.c
>> @@ -2,9 +2,10 @@
>> sections.
>>
>> { dg-require-effective-target elf }
>> -   { dg-do compile } */
>> +   { dg-do compile }
>> +   { dg-skip-if "" { ! const_volatile_readonly_section } } */
>>
>>  const volatile int foo = 30;
>>
>> -
>> -/* { dg-final { scan-assembler "\\.s\?rodata" } } */
>> +/* { dg-final { scan-assembler {.section C,} { target { rx-*-* } } } } */
>> +/* { dg-final { scan-assembler-symbol-section {^_?foo$} 
>> {^\.(const|s?rodata)} { target { ! "rx-*-*" } } } } */
>> diff --git a/gcc/testsuite/lib/target-supports.exp 
>> b/gcc/testsuite/lib/target-supports.exp
>> index c0694af2338..91aafd89909 100644
>> --- a/gcc/testsuite/lib/target-supports.exp
>> +++ b/gcc/testsuite/lib/target-supports.exp
>> @@ -12295,3 +12295,13 @@ proc check_is_prog_name_available { prog } {
>>
>>  return 1
>>  }
>> +
>> +# returns 1 if target does selects a readonly section for const volatile 
>> variables.
>> +proc check_effective_target_const_volatile_readonly_section { } {
>> +
>> +if { [istarget powerpc-*-*]
>> +  || [check-flags { "" { powerpc64-*-* } { -m32 } }] } {
>> +return 0
>> +}
>> +  return 1
>> +}
>>
>>
>> Jeff Law writes:
>>
>>> On 12/7/22 08:45, Cupertino Miranda wrote:
>>>>
>>>>> On 12/2/22 10:52, Cupertino Miranda via Gcc-patches wrote:
>>>>>> This commit is a follow up of bugzilla #107181.
>>>>>> The commit /a0aafbc/ changed the default implementation of the
>>>>>> SELECT_SECTION hook in order to match clang/llvm behaviour w.r.t the
>>>>>> placement of `const volatile' objects.
>>>>>> However, the following targets use target-specific selection functions
>>>>>> and they choke on the testcase pr25521.c:
>>>>>>*rx - target sets its const variables as '.section C,"a",@progbits'.
>>>>> That's presumably a constant section.  We should instead twiddle the test 
>>>>> to
>>>>> recognize that section.
>>>> Although @progbits is indeed a constant section, I believe it is
>>>> more interesting to detect if the `rx' starts selecting more
>>>> standard sections instead of the current @progbits.
>>>> That was the reason why I opted to XFAIL instead of PASSing it.
>>>> Can I keep it as such ?
>>> I'm not aware of any ongoing development for that port, so I would not let
>>> concerns about the rx port changing behavior dominate how we approach this
>>> problem.
>>>
>>> The rx port is using a different name for the section.  That's  valid thing 
>>> to
>>> do and to the extent we can, we should support that in the test rather than
>>> (incorrectly IMHO) xfailing the test just becuase the name isn't what we
>>> expected.
>>>
>>> To avoid over-eagerly matching, I would probably search for "C,"  I 
>>> wouldn't do
>>> that for the const or rodata sections as they often have a suffix like 1, 
>>> 2, 4,
>>> 8 for different sized rodata sections.
>>>
>>> PPC32 is explicitly doing something different and placing those objects 
>>> into an
>>> RW section.  So for PPC32 it makes more sense to skip the test rather than 
>>> xfail
>>> it.
>>>
>>> Jeff


[PING] Re: [PATCH 2/2] Corrected pr25521.c target matching.

2023-01-31 Thread Cupertino Miranda via Gcc-patches


Cupertino Miranda via Gcc-patches writes:

> Thank you for the comments and suggestions.
> I have changed the patch.
>
> Unfortunately in case of rx target I could not make
> scan-assembler-symbol-section to match. I believe it is because the
> .section and .global entries order is reversed in this target.
>
> Patch in inlined below. looking forward to your comments.
>
> Cupertino
>
> diff --git a/gcc/testsuite/gcc.dg/pr25521.c b/gcc/testsuite/gcc.dg/pr25521.c
> index 63363a03b9f..82b4cd88ec0 100644
> --- a/gcc/testsuite/gcc.dg/pr25521.c
> +++ b/gcc/testsuite/gcc.dg/pr25521.c
> @@ -2,9 +2,10 @@
> sections.
>
> { dg-require-effective-target elf }
> -   { dg-do compile } */
> +   { dg-do compile }
> +   { dg-skip-if "" { ! const_volatile_readonly_section } } */
>
>  const volatile int foo = 30;
>
> -
> -/* { dg-final { scan-assembler "\\.s\?rodata" } } */
> +/* { dg-final { scan-assembler {.section C,} { target { rx-*-* } } } } */
> +/* { dg-final { scan-assembler-symbol-section {^_?foo$} 
> {^\.(const|s?rodata)} { target { ! "rx-*-*" } } } } */
> diff --git a/gcc/testsuite/lib/target-supports.exp 
> b/gcc/testsuite/lib/target-supports.exp
> index c0694af2338..91aafd89909 100644
> --- a/gcc/testsuite/lib/target-supports.exp
> +++ b/gcc/testsuite/lib/target-supports.exp
> @@ -12295,3 +12295,13 @@ proc check_is_prog_name_available { prog } {
>
>  return 1
>  }
> +
> +# returns 1 if target does selects a readonly section for const volatile 
> variables.
> +proc check_effective_target_const_volatile_readonly_section { } {
> +
> +if { [istarget powerpc-*-*]
> +   || [check-flags { "" { powerpc64-*-* } { -m32 } }] } {
> + return 0
> +}
> +  return 1
> +}
>
>
> Jeff Law writes:
>
>> On 12/7/22 08:45, Cupertino Miranda wrote:
>>>
>>>> On 12/2/22 10:52, Cupertino Miranda via Gcc-patches wrote:
>>>>> This commit is a follow up of bugzilla #107181.
>>>>> The commit /a0aafbc/ changed the default implementation of the
>>>>> SELECT_SECTION hook in order to match clang/llvm behaviour w.r.t the
>>>>> placement of `const volatile' objects.
>>>>> However, the following targets use target-specific selection functions
>>>>> and they choke on the testcase pr25521.c:
>>>>>*rx - target sets its const variables as '.section C,"a",@progbits'.
>>>> That's presumably a constant section.  We should instead twiddle the test 
>>>> to
>>>> recognize that section.
>>> Although @progbits is indeed a constant section, I believe it is
>>> more interesting to detect if the `rx' starts selecting more
>>> standard sections instead of the current @progbits.
>>> That was the reason why I opted to XFAIL instead of PASSing it.
>>> Can I keep it as such ?
>> I'm not aware of any ongoing development for that port, so I would not let
>> concerns about the rx port changing behavior dominate how we approach this
>> problem.
>>
>> The rx port is using a different name for the section.  That's  valid thing 
>> to
>> do and to the extent we can, we should support that in the test rather than
>> (incorrectly IMHO) xfailing the test just becuase the name isn't what we
>> expected.
>>
>> To avoid over-eagerly matching, I would probably search for "C,"  I wouldn't 
>> do
>> that for the const or rodata sections as they often have a suffix like 1, 2, 
>> 4,
>> 8 for different sized rodata sections.
>>
>> PPC32 is explicitly doing something different and placing those objects into 
>> an
>> RW section.  So for PPC32 it makes more sense to skip the test rather than 
>> xfail
>> it.
>>
>> Jeff


Re: [PATCH 2/2] Corrected pr25521.c target matching.

2023-01-24 Thread Cupertino Miranda via Gcc-patches

Thank you for the comments and suggestions.
I have changed the patch.

Unfortunately in case of rx target I could not make
scan-assembler-symbol-section to match. I believe it is because the
.section and .global entries order is reversed in this target.

Patch in inlined below. looking forward to your comments.

Cupertino

diff --git a/gcc/testsuite/gcc.dg/pr25521.c b/gcc/testsuite/gcc.dg/pr25521.c
index 63363a03b9f..82b4cd88ec0 100644
--- a/gcc/testsuite/gcc.dg/pr25521.c
+++ b/gcc/testsuite/gcc.dg/pr25521.c
@@ -2,9 +2,10 @@
sections.
 
{ dg-require-effective-target elf }
-   { dg-do compile } */
+   { dg-do compile }
+   { dg-skip-if "" { ! const_volatile_readonly_section } } */
 
 const volatile int foo = 30;
 
-
-/* { dg-final { scan-assembler "\\.s\?rodata" } } */
+/* { dg-final { scan-assembler {.section C,} { target { rx-*-* } } } } */
+/* { dg-final { scan-assembler-symbol-section {^_?foo$} {^\.(const|s?rodata)} { target { ! "rx-*-*" } } } } */
diff --git a/gcc/testsuite/lib/target-supports.exp b/gcc/testsuite/lib/target-supports.exp
index c0694af2338..91aafd89909 100644
--- a/gcc/testsuite/lib/target-supports.exp
+++ b/gcc/testsuite/lib/target-supports.exp
@@ -12295,3 +12295,13 @@ proc check_is_prog_name_available { prog } {
 
 return 1
 }
+
+# returns 1 if target does selects a readonly section for const volatile variables.
+proc check_effective_target_const_volatile_readonly_section { } {
+
+if { [istarget powerpc-*-*]
+	  || [check-flags { "" { powerpc64-*-* } { -m32 } }] } {
+	return 0
+}
+  return 1
+}


Jeff Law writes:

> On 12/7/22 08:45, Cupertino Miranda wrote:
>>
>>> On 12/2/22 10:52, Cupertino Miranda via Gcc-patches wrote:
>>>> This commit is a follow up of bugzilla #107181.
>>>> The commit /a0aafbc/ changed the default implementation of the
>>>> SELECT_SECTION hook in order to match clang/llvm behaviour w.r.t the
>>>> placement of `const volatile' objects.
>>>> However, the following targets use target-specific selection functions
>>>> and they choke on the testcase pr25521.c:
>>>>*rx - target sets its const variables as '.section C,"a",@progbits'.
>>> That's presumably a constant section.  We should instead twiddle the test to
>>> recognize that section.
>> Although @progbits is indeed a constant section, I believe it is
>> more interesting to detect if the `rx' starts selecting more
>> standard sections instead of the current @progbits.
>> That was the reason why I opted to XFAIL instead of PASSing it.
>> Can I keep it as such ?
> I'm not aware of any ongoing development for that port, so I would not let
> concerns about the rx port changing behavior dominate how we approach this
> problem.
>
> The rx port is using a different name for the section.  That's  valid thing to
> do and to the extent we can, we should support that in the test rather than
> (incorrectly IMHO) xfailing the test just becuase the name isn't what we
> expected.
>
> To avoid over-eagerly matching, I would probably search for "C,"  I wouldn't 
> do
> that for the const or rodata sections as they often have a suffix like 1, 2, 
> 4,
> 8 for different sized rodata sections.
>
> PPC32 is explicitly doing something different and placing those objects into 
> an
> RW section.  So for PPC32 it makes more sense to skip the test rather than 
> xfail
> it.
>
> Jeff


Re: [PATCH 1/2] select .rodata for const volatile variables.

2023-01-19 Thread Cupertino Miranda via Gcc-patches


Hi Jeff,

Kindly calling your attention to this thread.

Regards,
Cupertino

Cupertino Miranda via Gcc-patches writes:

> Richard Biener writes:
>
>> On Mon, Dec 5, 2022 at 7:07 PM Jeff Law via Gcc-patches
>>  wrote:
>>>
>>>
>>>
>>> On 12/2/22 10:52, Cupertino Miranda via Gcc-patches wrote:
>>> > Changed target code to select .rodata section for 'const volatile'
>>> > defined variables.
>>> > This change is in the context of the bugzilla #170181.
>>> >
>>> > gcc/ChangeLog:
>>> >
>>> >   v850.c(v850_select_section): Changed function.
>>> I'm not sure this is safe/correct.  ISTM that you need to look at the
>>> underlying TREE_TYPE to check for const-volatile rather than
>>> TREE_SIDE_EFFECTS.
>>
>> Just to quote tree.h:
>>
>> /* In any expression, decl, or constant, nonzero means it has side effects or
>>reevaluation of the whole expression could produce a different value.
>>This is set if any subexpression is a function call, a side effect or a
>>reference to a volatile variable.  In a ..._DECL, this is set only if the
>>declaration said `volatile'.  This will never be set for a constant.  */
>> #define TREE_SIDE_EFFECTS(NODE) \
>>   (NON_TYPE_CHECK (NODE)->base.side_effects_flag)
>>
>> so if exp is a decl then that's the volatile check.
>>
>
> Thank you Richard for the review.
> Jeff: Can you please let me know if Richard comments reply to your
> concerns?
>
> Cupertino
>
>>> Of secondary importance is the ChangeLog.  Just saying "Changed
>>> function" provides no real information.  Something like this would be
>>> better:
>>>
>>> * config/v850/v850.c (v850_select_section): Put const volatile
>>> objects into read-only sections.
>>>
>>>
>>> Jeff
>>>
>>>
>>>
>>>
>>> > ---
>>> >   gcc/config/v850/v850.cc | 1 -
>>> >   1 file changed, 1 deletion(-)
>>> >
>>> > diff --git a/gcc/config/v850/v850.cc b/gcc/config/v850/v850.cc
>>> > index c7d432990ab..e66893fede4 100644
>>> > --- a/gcc/config/v850/v850.cc
>>> > +++ b/gcc/config/v850/v850.cc
>>> > @@ -2865,7 +2865,6 @@ v850_select_section (tree exp,
>>> >   {
>>> > int is_const;
>>> > if (!TREE_READONLY (exp)
>>> > -   || TREE_SIDE_EFFECTS (exp)
>>> > || !DECL_INITIAL (exp)
>>> > || (DECL_INITIAL (exp) != error_mark_node
>>> > && !TREE_CONSTANT (DECL_INITIAL (exp


Re: [PATCH 2/2] Corrected pr25521.c target matching.

2023-01-13 Thread Cupertino Miranda via Gcc-patches


Cupertino Miranda writes:

>> On 12/2/22 10:52, Cupertino Miranda via Gcc-patches wrote:
>>> This commit is a follow up of bugzilla #107181.
>>> The commit /a0aafbc/ changed the default implementation of the
>>> SELECT_SECTION hook in order to match clang/llvm behaviour w.r.t the
>>> placement of `const volatile' objects.
>>> However, the following targets use target-specific selection functions
>>> and they choke on the testcase pr25521.c:
>>>   *rx - target sets its const variables as '.section C,"a",@progbits'.
>> That's presumably a constant section.  We should instead twiddle the test to
>> recognize that section.
>
> Although @progbits is indeed a constant section, I believe it is
> more interesting to detect if the `rx' starts selecting more
> standard sections instead of the current @progbits.
> That was the reason why I opted to XFAIL instead of PASSing it.
> Can I keep it as such ?
>
Jeff: Can you please give me an answer on this ?

Cupertino

>>
>>>   *powerpc - its 32bit version is eager to allocate globals in .sdata
>>>  sections.
>>> Normally, one can expect for the variable to be allocated in .srodata,
>>> however, in case of powerpc-*-* or powerpc64-*-* (with -m32)
>>> 'targetm.have_srodata_section == false' and the code in
>>> categorize_decl_for_section(varasm.cc), forces it to allocate in .sdata.
>>>/* If the target uses small data sections, select it.  */
>>>else if (targetm.in_small_data_p (decl))
>>>  {
>>>if (ret == SECCAT_BSS)
>>> ret = SECCAT_SBSS;
>>>else if targetm.have_srodata_section && ret == SECCAT_RODATA)
>>> ret = SECCAT_SRODATA;
>>>else
>>> ret = SECCAT_SDATA;
>>>  }
>> I'd just skip the test for 32bit ppc.  There should be suitable 
>> effective-target
>> tests you can use.
>>
>> jeff


Re: [PATCH 1/2] select .rodata for const volatile variables.

2023-01-13 Thread Cupertino Miranda via Gcc-patches


Richard Biener writes:

> On Mon, Dec 5, 2022 at 7:07 PM Jeff Law via Gcc-patches
>  wrote:
>>
>>
>>
>> On 12/2/22 10:52, Cupertino Miranda via Gcc-patches wrote:
>> > Changed target code to select .rodata section for 'const volatile'
>> > defined variables.
>> > This change is in the context of the bugzilla #170181.
>> >
>> > gcc/ChangeLog:
>> >
>> >   v850.c(v850_select_section): Changed function.
>> I'm not sure this is safe/correct.  ISTM that you need to look at the
>> underlying TREE_TYPE to check for const-volatile rather than
>> TREE_SIDE_EFFECTS.
>
> Just to quote tree.h:
>
> /* In any expression, decl, or constant, nonzero means it has side effects or
>reevaluation of the whole expression could produce a different value.
>This is set if any subexpression is a function call, a side effect or a
>reference to a volatile variable.  In a ..._DECL, this is set only if the
>declaration said `volatile'.  This will never be set for a constant.  */
> #define TREE_SIDE_EFFECTS(NODE) \
>   (NON_TYPE_CHECK (NODE)->base.side_effects_flag)
>
> so if exp is a decl then that's the volatile check.
>

Thank you Richard for the review.
Jeff: Can you please let me know if Richard comments reply to your
concerns?

Cupertino

>> Of secondary importance is the ChangeLog.  Just saying "Changed
>> function" provides no real information.  Something like this would be
>> better:
>>
>> * config/v850/v850.c (v850_select_section): Put const volatile
>> objects into read-only sections.
>>
>>
>> Jeff
>>
>>
>>
>>
>> > ---
>> >   gcc/config/v850/v850.cc | 1 -
>> >   1 file changed, 1 deletion(-)
>> >
>> > diff --git a/gcc/config/v850/v850.cc b/gcc/config/v850/v850.cc
>> > index c7d432990ab..e66893fede4 100644
>> > --- a/gcc/config/v850/v850.cc
>> > +++ b/gcc/config/v850/v850.cc
>> > @@ -2865,7 +2865,6 @@ v850_select_section (tree exp,
>> >   {
>> > int is_const;
>> > if (!TREE_READONLY (exp)
>> > -   || TREE_SIDE_EFFECTS (exp)
>> > || !DECL_INITIAL (exp)
>> > || (DECL_INITIAL (exp) != error_mark_node
>> > && !TREE_CONSTANT (DECL_INITIAL (exp


Re: [PING] Re: [PATCH 2/2] Corrected pr25521.c target matching.

2023-01-02 Thread Cupertino Miranda via Gcc-patches


PING PING

Cupertino Miranda writes:

> Cupertino Miranda via Gcc-patches writes:
>
>> gentle ping
>>
>> Cupertino Miranda writes:
>>
>>>> On 12/2/22 10:52, Cupertino Miranda via Gcc-patches wrote:
>>>>> This commit is a follow up of bugzilla #107181.
>>>>> The commit /a0aafbc/ changed the default implementation of the
>>>>> SELECT_SECTION hook in order to match clang/llvm behaviour w.r.t the
>>>>> placement of `const volatile' objects.
>>>>> However, the following targets use target-specific selection functions
>>>>> and they choke on the testcase pr25521.c:
>>>>>   *rx - target sets its const variables as '.section C,"a",@progbits'.
>>>> That's presumably a constant section.  We should instead twiddle the test 
>>>> to
>>>> recognize that section.
>>>
>>> Although @progbits is indeed a constant section, I believe it is
>>> more interesting to detect if the `rx' starts selecting more
>>> standard sections instead of the current @progbits.
>>> That was the reason why I opted to XFAIL instead of PASSing it.
>>> Can I keep it as such ?
>>>
>>>>
>>>>>   *powerpc - its 32bit version is eager to allocate globals in .sdata
>>>>>  sections.
>>>>> Normally, one can expect for the variable to be allocated in .srodata,
>>>>> however, in case of powerpc-*-* or powerpc64-*-* (with -m32)
>>>>> 'targetm.have_srodata_section == false' and the code in
>>>>> categorize_decl_for_section(varasm.cc), forces it to allocate in .sdata.
>>>>>/* If the target uses small data sections, select it.  */
>>>>>else if (targetm.in_small_data_p (decl))
>>>>>  {
>>>>>if (ret == SECCAT_BSS)
>>>>>   ret = SECCAT_SBSS;
>>>>>else if targetm.have_srodata_section && ret == SECCAT_RODATA)
>>>>>   ret = SECCAT_SRODATA;
>>>>>else
>>>>>   ret = SECCAT_SDATA;
>>>>>  }
>>>> I'd just skip the test for 32bit ppc.  There should be suitable 
>>>> effective-target
>>>> tests you can use.
>>>>
>>>> jeff


Re: [PING] Re: [PATCH 1/2] select .rodata for const volatile variables.

2023-01-02 Thread Cupertino Miranda via Gcc-patches


PING PING

Cupertino Miranda writes:

> Cupertino Miranda via Gcc-patches writes:
>
>> gentle ping
>>
>> Cupertino Miranda writes:
>>
>>> Hi Jeff,
>>>
>>> First of all thanks for your quick review.
>>> Apologies for the delay replying, the message got lost in my inbox.
>>>
>>>> On 12/2/22 10:52, Cupertino Miranda via Gcc-patches wrote:
>>>>> Changed target code to select .rodata section for 'const volatile'
>>>>> defined variables.
>>>>> This change is in the context of the bugzilla #170181.
>>>>> gcc/ChangeLog:
>>>>>   v850.c(v850_select_section): Changed function.
>>>> I'm not sure this is safe/correct.  ISTM that you need to look at the 
>>>> underlying
>>>> TREE_TYPE to check for const-volatile rather than TREE_SIDE_EFFECTS.
>>>
>>> I believe this was asked by Jose when he first sent the generic patches.
>>> Please notice my change is influenced by his original patch that does
>>> the same and was approved.
>>>
>>> https://gcc.gnu.org/pipermail/gcc-patches/2022-August/599348.html
>>> https://gcc.gnu.org/pipermail/gcc-patches/2022-September/602374.html
>>>
>>>>
>>>> Of secondary importance is the ChangeLog.  Just saying "Changed function"
>>>> provides no real information.  Something like this would be better:
>>>>
>>>>* config/v850/v850.c (v850_select_section): Put const volatile
>>>>objects into read-only sections.
>>>>
>>>>
>>>> Jeff
>>>>
>>>>
>>>>
>>>>
>>>>> ---
>>>>>   gcc/config/v850/v850.cc | 1 -
>>>>>   1 file changed, 1 deletion(-)
>>>>> diff --git a/gcc/config/v850/v850.cc b/gcc/config/v850/v850.cc
>>>>> index c7d432990ab..e66893fede4 100644
>>>>> --- a/gcc/config/v850/v850.cc
>>>>> +++ b/gcc/config/v850/v850.cc
>>>>> @@ -2865,7 +2865,6 @@ v850_select_section (tree exp,
>>>>>   {
>>>>> int is_const;
>>>>> if (!TREE_READONLY (exp)
>>>>> -   || TREE_SIDE_EFFECTS (exp)
>>>>> || !DECL_INITIAL (exp)
>>>>> || (DECL_INITIAL (exp) != error_mark_node
>>>>> && !TREE_CONSTANT (DECL_INITIAL (exp


[PING] Re: [PATCH 2/2] Corrected pr25521.c target matching.

2022-12-22 Thread Cupertino Miranda via Gcc-patches


Cupertino Miranda via Gcc-patches writes:

> gentle ping
>
> Cupertino Miranda writes:
>
>>> On 12/2/22 10:52, Cupertino Miranda via Gcc-patches wrote:
>>>> This commit is a follow up of bugzilla #107181.
>>>> The commit /a0aafbc/ changed the default implementation of the
>>>> SELECT_SECTION hook in order to match clang/llvm behaviour w.r.t the
>>>> placement of `const volatile' objects.
>>>> However, the following targets use target-specific selection functions
>>>> and they choke on the testcase pr25521.c:
>>>>   *rx - target sets its const variables as '.section C,"a",@progbits'.
>>> That's presumably a constant section.  We should instead twiddle the test to
>>> recognize that section.
>>
>> Although @progbits is indeed a constant section, I believe it is
>> more interesting to detect if the `rx' starts selecting more
>> standard sections instead of the current @progbits.
>> That was the reason why I opted to XFAIL instead of PASSing it.
>> Can I keep it as such ?
>>
>>>
>>>>   *powerpc - its 32bit version is eager to allocate globals in .sdata
>>>>  sections.
>>>> Normally, one can expect for the variable to be allocated in .srodata,
>>>> however, in case of powerpc-*-* or powerpc64-*-* (with -m32)
>>>> 'targetm.have_srodata_section == false' and the code in
>>>> categorize_decl_for_section(varasm.cc), forces it to allocate in .sdata.
>>>>/* If the target uses small data sections, select it.  */
>>>>else if (targetm.in_small_data_p (decl))
>>>>  {
>>>>if (ret == SECCAT_BSS)
>>>>ret = SECCAT_SBSS;
>>>>else if targetm.have_srodata_section && ret == SECCAT_RODATA)
>>>>ret = SECCAT_SRODATA;
>>>>else
>>>>ret = SECCAT_SDATA;
>>>>  }
>>> I'd just skip the test for 32bit ppc.  There should be suitable 
>>> effective-target
>>> tests you can use.
>>>
>>> jeff


  1   2   >