A BPF arena is memory that a program shares with user space.  A pointer
into an arena holds the user-space address.  The addr_space_cast
instruction converts it to the kernel address, which the program can
dereference.  Model the arena as named address space 1, spelled
__arena, the number clang uses.

No instruction dereferences an arena pointer.  An access is a cast to
the generic address space, then an ordinary load or store.  So an arena
MEM has no RTL form: the cast is a separate instruction that must come
first.  Lower arena accesses in gimple instead, right after the CFG is
built.  No non-generic reference reaches RTL, and the casts get the
usual redundancy elimination and loop-invariant motion.

Objects declared in the arena go to section .addr_space.1, where libbpf
looks for the arena's initial contents.  The section is set on the
declaration so that the BTF DATASEC agrees.  Otherwise BTF says .bss
and libbpf fails to link the object.

Also add BPF versions of the x86 debug info tests for a typedef of an
address space qualified type.

__BPF_FEATURE_ARENA is defined so that source can test for the arena.
clang's __BPF_FEATURE_ADDR_SPACE_CAST is not defined, because it also
promises __attribute__((address_space(1))), which GCC does not accept.

        PR target/124974

gcc/ChangeLog:

        * config/bpf/bpf.h (enum bpf_addr_space): New.
        (REGISTER_TARGET_PRAGMAS): Define.
        * config/bpf/bpf-protos.h (bpf_register_pragmas): New prototype.
        (make_pass_lower_bpf_arena): Likewise.
        * config/bpf/bpf-c.cc (bpf_register_pragmas): New function.
        (bpf_target_macros): Define __BPF_FEATURE_ARENA.
        * doc/extend.texi (Named Address Spaces): Add BPF to the list of
        targets with named address spaces, and a BPF section documenting
        __arena.
        * config/bpf/bpf.cc (bpf_addr_space_subset_p): New function.
        (bpf_addr_space_convert): New function.
        (bpf_addr_space_section_name): New function.
        (bpf_encode_section_info): New function.
        (bpf_strip_addr_space): New function.
        (bpf_lower_arena_ref): New function.
        (execute_lower_bpf_arena): New function.
        (pass_lower_bpf_arena): New pass.
        (make_pass_lower_bpf_arena): New function.
        (TARGET_ADDR_SPACE_SUBSET_P): Define.
        (TARGET_ADDR_SPACE_CONVERT): Define.
        (TARGET_ENCODE_SECTION_INFO): Define.
        * config/bpf/bpf-passes.def: Add pass_lower_bpf_arena.
        * config/bpf/bpf.md (UNSPEC_ADDR_SPACE_CAST): New unspec.
        (addr_space_cast): New insn.

gcc/testsuite/ChangeLog:

        * gcc.target/bpf/arena-1.c: New test.
        * gcc.target/bpf/arena-2.c: New test.
        * gcc.target/bpf/arena-3.c: New test.
        * gcc.target/bpf/arena-btf-1.c: New test.
        * gcc.target/bpf/arena-arg-1.c: New test.
        * gcc.dg/debug/dwarf2/dwarf-addr-space-typedef-bpf.c: New test.

Signed-off-by: Vineet Gupta <[email protected]>
---
Changes in v3:
 - Assert in bpf_lower_arena_ref that an arena variable is static or
   external.  v2 tested that in the condition, so any other arena
   variable was quietly left alone (Jose E. Marchesi).

Changes in v2:
 - Lower the arguments of a call too.  An aggregate passed by value out
   of the arena was read straight through the arena pointer, with no
   addr_space_cast.  New test arena-arg-1.c (Jose E. Marchesi).
 - Say in bpf_lower_arena_ref that the front end rejects __arena on
   automatics and parameters, rather than that an automatic is left
   alone (Jose E. Marchesi).

 gcc/config/bpf/bpf-c.cc                       |  12 +
 gcc/config/bpf/bpf-passes.def                 |   1 +
 gcc/config/bpf/bpf-protos.h                   |   2 +
 gcc/config/bpf/bpf.cc                         | 264 ++++++++++++++++++
 gcc/config/bpf/bpf.h                          |  15 +
 gcc/config/bpf/bpf.md                         |  20 ++
 gcc/doc/extend.texi                           |  30 +-
 .../dwarf2/dwarf-addr-space-typedef-bpf.c     |  45 +++
 gcc/testsuite/gcc.target/bpf/arena-1.c        |  29 ++
 gcc/testsuite/gcc.target/bpf/arena-2.c        |  20 ++
 gcc/testsuite/gcc.target/bpf/arena-3.c        |  40 +++
 gcc/testsuite/gcc.target/bpf/arena-arg-1.c    |  25 ++
 gcc/testsuite/gcc.target/bpf/arena-btf-1.c    |  30 ++
 13 files changed, 532 insertions(+), 1 deletion(-)
 create mode 100644 
gcc/testsuite/gcc.dg/debug/dwarf2/dwarf-addr-space-typedef-bpf.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/arena-1.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/arena-2.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/arena-3.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/arena-arg-1.c
 create mode 100644 gcc/testsuite/gcc.target/bpf/arena-btf-1.c

diff --git a/gcc/config/bpf/bpf-c.cc b/gcc/config/bpf/bpf-c.cc
index 33b15f44daaf..b7e18aff9739 100644
--- a/gcc/config/bpf/bpf-c.cc
+++ b/gcc/config/bpf/bpf-c.cc
@@ -27,6 +27,16 @@ along with GCC; see the file COPYING3.  If not see
 #include "c-family/c-common.h"
 #include "cpplib.h"
 
+/* Register the BPF named address spaces with the C family front ends.
+   This function is used in the definition of REGISTER_TARGET_PRAGMAS
+   in bpf.h.  */
+
+void
+bpf_register_pragmas (void)
+{
+  c_register_addr_space ("__arena", ADDR_SPACE_ARENA);
+}
+
 /* Define target-specific CPP macros.  This function in used in the
    definition of TARGET_CPU_CPP_BUILTINS in bpf.h */
 
@@ -79,6 +89,8 @@ bpf_target_macros (cpp_reader *pfile)
   if (bpf_has_callx)
     builtin_define ("__BPF_FEATURE_CALLX");
 
+  builtin_define ("__BPF_FEATURE_ARENA");
+
   /* Other CPU features can only be enabled/disabled generically by
      selecting the corresponding CPU version.  */
   if (bpf_isa >= ISA_V4)
diff --git a/gcc/config/bpf/bpf-passes.def b/gcc/config/bpf/bpf-passes.def
index eb0e9837d3a9..9480efc6b601 100644
--- a/gcc/config/bpf/bpf-passes.def
+++ b/gcc/config/bpf/bpf-passes.def
@@ -18,3 +18,4 @@
    <http://www.gnu.org/licenses/>.  */
 
 INSERT_PASS_BEFORE (pass_lower_omp, 1, pass_lower_bpf_core);
+INSERT_PASS_AFTER (pass_build_cfg, 1, pass_lower_bpf_arena);
diff --git a/gcc/config/bpf/bpf-protos.h b/gcc/config/bpf/bpf-protos.h
index eee260b799bf..2108406a74c7 100644
--- a/gcc/config/bpf/bpf-protos.h
+++ b/gcc/config/bpf/bpf-protos.h
@@ -26,6 +26,7 @@ extern HOST_WIDE_INT bpf_initial_elimination_offset (int, 
int);
 extern rtx bpf_check_call_target (rtx);
 extern const char *bpf_output_call (const char *templ, rtx *, int 
target_index);
 extern void bpf_target_macros (cpp_reader *);
+extern void bpf_register_pragmas (void);
 extern void bpf_print_operand (FILE *, rtx, int);
 extern void bpf_print_operand_address (FILE *, rtx);
 extern void bpf_expand_prologue (void);
@@ -35,6 +36,7 @@ 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);
+gimple_opt_pass *make_pass_lower_bpf_arena (gcc::context *ctxt);
 
 bool bpf_expand_cpymem (rtx *, bool);
 bool bpf_expand_setmem (rtx *);
diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
index d852e2fbd70d..c611c36bd508 100644
--- a/gcc/config/bpf/bpf.cc
+++ b/gcc/config/bpf/bpf.cc
@@ -58,6 +58,7 @@ along with GCC; see the file COPYING3.  If not see
 
 #include "cfg.h" /* needed for struct control_flow_graph used in BB macros */
 #include "gimple.h"
+#include "gimple-ssa.h"
 #include "gimple-iterator.h"
 #include "gimple-walk.h"
 #include "tree-pass.h"
@@ -600,6 +601,41 @@ bpf_legitimate_address_p (machine_mode mode,
 #undef TARGET_LEGITIMATE_ADDRESS_P
 #define TARGET_LEGITIMATE_ADDRESS_P bpf_legitimate_address_p
 
+/* Return true if the named address space SUBSET is contained within
+   SUPERSET.  Every arena address denotes a location that also has a
+   generic address, the one the BPF program actually dereferences, so
+   the arena address space is a subset of the generic one.  */
+
+static bool
+bpf_addr_space_subset_p (addr_space_t subset, addr_space_t superset)
+{
+  return (subset == superset
+         || (subset == ADDR_SPACE_ARENA && superset == ADDR_SPACE_GENERIC));
+}
+
+#undef TARGET_ADDR_SPACE_SUBSET_P
+#define TARGET_ADDR_SPACE_SUBSET_P bpf_addr_space_subset_p
+
+/* Convert the pointer expression OP, of type FROM_TYPE, into a pointer
+   of TO_TYPE, which points into a different named address space.  */
+
+static rtx
+bpf_addr_space_convert (rtx op, tree from_type, tree to_type)
+{
+  addr_space_t from_as = TYPE_ADDR_SPACE (TREE_TYPE (from_type));
+  addr_space_t to_as = TYPE_ADDR_SPACE (TREE_TYPE (to_type));
+  rtx result = gen_reg_rtx (DImode);
+
+  gcc_assert (from_as != to_as);
+
+  emit_insn (gen_addr_space_cast (result, force_reg (DImode, op),
+                                 GEN_INT (to_as), GEN_INT (from_as)));
+  return result;
+}
+
+#undef TARGET_ADDR_SPACE_CONVERT
+#define TARGET_ADDR_SPACE_CONVERT bpf_addr_space_convert
+
 /* Describe the relative costs of RTL expressions.  Return true when
    all subexpressions of X have been processed, and false when
    `rtx_cost' should recurse.  */
@@ -1342,6 +1378,44 @@ bpf_asm_named_section (const char *name, unsigned int 
flags,
 #undef TARGET_ASM_NAMED_SECTION
 #define TARGET_ASM_NAMED_SECTION bpf_asm_named_section
 
+/* Name of the section an object in address space AS lives in, which is
+   where libbpf looks for the initial contents of the arena map.  */
+
+static const char *
+bpf_addr_space_section_name (addr_space_t as)
+{
+  static char name[sizeof (".addr_space.") + 3 * sizeof (as)];
+
+  sprintf (name, ".addr_space.%u", (unsigned) as);
+  return name;
+}
+
+/* Implement target hook TARGET_ENCODE_SECTION_INFO.
+
+   Record the section on the declaration rather than only choosing it
+   when the object is written out.  Everything downstream then agrees on
+   where the object lives: in particular BTF, whose DATASEC comes from
+   DECL_SECTION_NAME and otherwise falls back to categorizing the decl,
+   which knows nothing of address spaces and says ".bss".  A BTF that
+   disagrees with the symbol table is not a cosmetic problem -- libbpf
+   looks the variable up in the section BTF names and fails to link.  */
+
+static void
+bpf_encode_section_info (tree decl, rtx rtl, int first)
+{
+  default_encode_section_info (decl, rtl, first);
+
+  if (!VAR_P (decl) || DECL_SECTION_NAME (decl) != NULL)
+    return;
+
+  addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
+  if (!ADDR_SPACE_GENERIC_P (as))
+    set_decl_section_name (decl, bpf_addr_space_section_name (as));
+}
+
+#undef TARGET_ENCODE_SECTION_INFO
+#define TARGET_ENCODE_SECTION_INFO bpf_encode_section_info
+
 /* Implement target hook small_register_classes_for_mode_p.  */
 
 static bool
@@ -1635,6 +1709,196 @@ bpf_expand_setmem (rtx *operands)
   return true;
 }
 
+/* Lowering of accesses to the arena address space.
+
+   There is no BPF instruction that dereferences an arena pointer.  An
+   arena access is written as a conversion of the pointer to the
+   generic address space followed by an ordinary load or store through
+   the result; the kernel verifier gives the destination of the
+   conversion the PTR_TO_ARENA type and rewrites the accesses made
+   through it into their arena forms.
+
+   So no reference to the arena address space may survive into RTL.
+   This pass rewrites each of them into the conversion plus a generic
+   reference.  It runs right after the CFG is built, before the SSA
+   optimizers, so that the conversions it introduces are subject to
+   the usual redundancy elimination and loop-invariant motion.  */
+
+/* Return TYPE without its address space qualifier.  */
+
+static tree
+bpf_strip_addr_space (tree type)
+{
+  return build_qualified_type (type, TYPE_QUALS_NO_ADDR_SPACE (type));
+}
+
+/* If the memory reference *REF_P is based on an arena pointer, rewrite
+   it into a generic reference through a converted pointer, inserting
+   the conversion before the statement GSI points at.  Return true if
+   *REF_P was rewritten.  */
+
+static bool
+bpf_lower_arena_ref (gimple_stmt_iterator *gsi, tree *ref_p)
+{
+  tree *p = ref_p;
+
+  while (handled_component_p (*p))
+    p = &TREE_OPERAND (*p, 0);
+
+  /* An object that lives in an arena is reached through its address,
+     just like anything else in there.  */
+  if (VAR_P (*p) && TYPE_ADDR_SPACE (TREE_TYPE (*p)) == ADDR_SPACE_ARENA)
+    {
+      /* The front end rejects __arena on automatics and parameters.  */
+      gcc_assert (is_global_var (*p));
+
+      tree decl = *p;
+      tree decl_type = TREE_TYPE (decl);
+      tree generic_type = build_pointer_type (bpf_strip_addr_space 
(decl_type));
+      tree address = build_fold_addr_expr (decl);
+      tree converted
+       = force_gimple_operand_gsi (gsi,
+                                   build1 (ADDR_SPACE_CONVERT_EXPR,
+                                           generic_type, address),
+                                   true, NULL_TREE, true, GSI_SAME_STMT);
+
+      *p = build2 (MEM_REF, bpf_strip_addr_space (decl_type), converted,
+                  build_int_cst (generic_type, 0));
+      return true;
+    }
+
+  if (TREE_CODE (*p) != MEM_REF)
+    return false;
+
+  tree mem = *p;
+  tree base = TREE_OPERAND (mem, 0);
+  tree offset = TREE_OPERAND (mem, 1);
+
+  if (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (base))) != ADDR_SPACE_ARENA)
+    return false;
+
+  tree generic_type
+    = build_pointer_type (bpf_strip_addr_space (TREE_TYPE (TREE_TYPE (base))));
+  tree converted
+    = force_gimple_operand_gsi (gsi,
+                               build1 (ADDR_SPACE_CONVERT_EXPR, generic_type,
+                                       base),
+                               true, NULL_TREE, true, GSI_SAME_STMT);
+
+  /* The second operand of a MEM_REF is an offset whose type supplies
+     the alias information, and hence the address space as well.  */
+  tree offset_type = TREE_TYPE (TREE_TYPE (offset));
+  tree generic_alias_type
+    = build_pointer_type (bpf_strip_addr_space (offset_type));
+  tree new_mem = build2 (MEM_REF, bpf_strip_addr_space (TREE_TYPE (mem)),
+                        converted,
+                        wide_int_to_tree (generic_alias_type,
+                                          wi::to_wide (offset)));
+
+  TREE_THIS_VOLATILE (new_mem) = TREE_THIS_VOLATILE (mem);
+  TREE_SIDE_EFFECTS (new_mem) = TREE_SIDE_EFFECTS (mem);
+  TREE_THIS_NOTRAP (new_mem) = TREE_THIS_NOTRAP (mem);
+  REF_REVERSE_STORAGE_ORDER (new_mem) = REF_REVERSE_STORAGE_ORDER (mem);
+  protected_set_expr_location (new_mem, EXPR_LOCATION (mem));
+
+  *p = new_mem;
+  return true;
+}
+
+/* Rewrite every arena memory reference in the current function.  */
+
+static unsigned int
+execute_lower_bpf_arena (void)
+{
+  basic_block bb;
+
+  FOR_EACH_BB_FN (bb, cfun)
+    {
+      for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
+          !gsi_end_p (gsi);
+          gsi_next (&gsi))
+       {
+         gimple *stmt = gsi_stmt (gsi);
+         bool changed = false;
+
+         if (gimple_clobber_p (stmt) || is_gimple_debug (stmt))
+           continue;
+
+         if (gassign *assign = dyn_cast <gassign *> (stmt))
+           {
+             tree *lhs = gimple_assign_lhs_ptr (assign);
+             changed |= bpf_lower_arena_ref (&gsi, lhs);
+             if (gimple_assign_single_p (assign))
+               {
+                 tree *rhs = gimple_assign_rhs1_ptr (assign);
+                 changed |= bpf_lower_arena_ref (&gsi, rhs);
+               }
+           }
+         else if (gcall *call = dyn_cast <gcall *> (stmt))
+           {
+             tree *lhs = gimple_call_lhs_ptr (call);
+             if (*lhs)
+               changed |= bpf_lower_arena_ref (&gsi, lhs);
+             /* An aggregate passed by value is a memory reference.  */
+             for (unsigned i = 0; i < gimple_call_num_args (call); i++)
+               changed |= bpf_lower_arena_ref (&gsi,
+                                               gimple_call_arg_ptr (call, i));
+           }
+         else if (gasm *a = dyn_cast <gasm *> (stmt))
+           {
+             for (unsigned i = 0; i < gimple_asm_noutputs (a); i++)
+               changed |= bpf_lower_arena_ref
+                 (&gsi, &TREE_VALUE (gimple_asm_output_op (a, i)));
+             for (unsigned i = 0; i < gimple_asm_ninputs (a); i++)
+               changed |= bpf_lower_arena_ref
+                 (&gsi, &TREE_VALUE (gimple_asm_input_op (a, i)));
+           }
+
+         if (changed)
+           update_stmt (stmt);
+       }
+    }
+
+  return 0;
+}
+
+namespace {
+
+const pass_data pass_data_lower_bpf_arena =
+{
+  GIMPLE_PASS, /* type */
+  "bpf_arena_lower", /* name */
+  OPTGROUP_NONE, /* optinfo_flags */
+  TV_NONE, /* tv_id */
+  PROP_gimple_any, /* properties_required */
+  0, /* properties_provided */
+  0, /* properties_destroyed */
+  0, /* todo_flags_start */
+  0, /* todo_flags_finish */
+};
+
+class pass_lower_bpf_arena : public gimple_opt_pass
+{
+public:
+  pass_lower_bpf_arena (gcc::context *ctxt)
+    : gimple_opt_pass (pass_data_lower_bpf_arena, ctxt)
+  {}
+
+  unsigned int execute (function *) final override
+  {
+    return execute_lower_bpf_arena ();
+  }
+
+}; /* class pass_lower_bpf_arena */
+
+} /* anon namespace */
+
+gimple_opt_pass *
+make_pass_lower_bpf_arena (gcc::context *ctxt)
+{
+  return new pass_lower_bpf_arena (ctxt);
+}
+
 #undef TARGET_DOCUMENTATION_NAME
 #define TARGET_DOCUMENTATION_NAME "BPF"
 
diff --git a/gcc/config/bpf/bpf.h b/gcc/config/bpf/bpf.h
index 57084ac3fa15..d90e5f57df4d 100644
--- a/gcc/config/bpf/bpf.h
+++ b/gcc/config/bpf/bpf.h
@@ -34,6 +34,21 @@
 
 #define TARGET_CPU_CPP_BUILTINS() bpf_target_macros (pfile)
 
+#define REGISTER_TARGET_PRAGMAS() bpf_register_pragmas ()
+
+/**** Named address spaces.  */
+
+/* Objects in ADDR_SPACE_ARENA live in a BPF arena map.  A pointer into
+   an arena holds the user-space view of the address; dereferencing it
+   from a BPF program requires first converting it to the kernel view,
+   which is what the generic address space holds.  That conversion is
+   the `addr_space_cast' instruction.  */
+
+enum bpf_addr_space
+{
+  ADDR_SPACE_ARENA = 1
+};
+
 /**** Storage Layout.  */
 
 /* Endianness and word size.  */
diff --git a/gcc/config/bpf/bpf.md b/gcc/config/bpf/bpf.md
index a34e685df127..e71a0b036211 100644
--- a/gcc/config/bpf/bpf.md
+++ b/gcc/config/bpf/bpf.md
@@ -46,6 +46,7 @@
   UNSPEC_AXCHG
   UNSPEC_ACMP
   UNSPEC_CORE_RELOC
+  UNSPEC_ADDR_SPACE_CAST
 ])
 
 ;;;; Constants
@@ -390,6 +391,25 @@
    *return bpf_output_move (operands, \"{st<mop>\t%0,%1|*(<smop> *) %0 = 
%1}\");"
 [(set_attr "type" "ldx,alu,alu,stx,st")])
 
+;;;; Address space casts
+
+;; Convert a pointer between the generic address space and the arena
+;; address space.  Operand 2 is the destination address space, operand
+;; 3 the source one, matching the operand order the instruction is
+;; written in.  The instruction is a `mov' with a distinguished offset
+;; field, and the kernel verifier relies on it to give the destination
+;; register the PTR_TO_ARENA type.
+
+(define_insn "addr_space_cast"
+  [(set (match_operand:DI 0 "register_operand"               "=r")
+        (unspec:DI [(match_operand:DI 1 "register_operand"   " r")
+                    (match_operand 2 "const_int_operand"      "n")
+                    (match_operand 3 "const_int_operand"      "n")]
+                   UNSPEC_ADDR_SPACE_CAST))]
+  ""
+  "{addr_space_cast\t%0,%1,%2,%3|%0 = addr_space_cast(%1, %2, %3)}"
+  [(set_attr "type" "alu")])
+
 ;;;; Shifts
 
 (define_mode_iterator SIM [(SI "bpf_has_alu32") DI])
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index b70c1b4135d8..d24600926500 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -1379,13 +1379,41 @@ As an extension, GNU C supports named address spaces as
 defined in the N1275 draft of ISO/IEC DTR 18037.  Support for named
 address spaces in GCC will evolve as the draft technical report
 changes.  Calling conventions for any target might also change.  At
-present, only the AVR, PRU, RL78, x86 and Xtensa targets support
+present, only the AVR, BPF, PRU, RL78, x86 and Xtensa targets support
 address spaces other than the generic address space.
 
 Address space identifiers may be used exactly like any other C type
 qualifier (e.g., @code{const} or @code{volatile}).  See the N1275
 document for more details.
 
+@anchor{BPF Named Address Spaces}
+@subsection BPF Named Address Spaces
+@cindex BPF named address spaces
+
+On the BPF target, an object may be declared as living in a BPF arena,
+a region of memory shared between a BPF program and user space.
+
+@table @code
+@cindex @code{__arena} BPF named address space
+@item __arena
+The object lives in an arena.  A pointer qualified with @code{__arena}
+holds the user-space view of an arena address; the kernel view, which
+is what a BPF program can dereference, is a different value, and the
+compiler converts between the two with the @code{addr_space_cast}
+instruction wherever such a pointer is dereferenced or converted.
+
+The arena address space is a subset of the generic address space, so a
+pointer to it converts to a generic pointer without a cast, while the
+other direction requires an explicit cast.
+
+Objects defined in this address space are emitted in a section named
+@code{.addr_space.1}, which is where libbpf looks for the initial
+contents of the arena.
+
+The preprocessor symbol @code{__BPF_FEATURE_ARENA} is
+defined when the arena address space is supported.
+@end table
+
 @anchor{AVR Named Address Spaces}
 @subsection AVR Named Address Spaces
 
diff --git a/gcc/testsuite/gcc.dg/debug/dwarf2/dwarf-addr-space-typedef-bpf.c 
b/gcc/testsuite/gcc.dg/debug/dwarf2/dwarf-addr-space-typedef-bpf.c
new file mode 100644
index 000000000000..0b38aa4f3253
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/debug/dwarf2/dwarf-addr-space-typedef-bpf.c
@@ -0,0 +1,45 @@
+/* The BPF side of dwarf-addr-space-typedef.c: a typedef of a type in the
+   arena address space keeps its DW_TAG_typedef.
+
+   The address space is recorded on the pointer as DW_AT_address_class
+   and so is not one of the qualifiers a type variant is looked up by.
+   Looking the variant up without it must not land on a variant the
+   typedef does not name, or the typedef is lost: the aggregate ones
+   below then point straight at "struct node", and the base type one
+   comes out as a second DW_TAG_base_type carrying the typedef's name.
+
+   No -fno-eliminate-unused-debug-types here: an unreferenced typedef
+   DIE would be pruned, so a DIE surviving is what shows it is still
+   referenced.  */
+
+/* { dg-do compile { target { bpf-*-* } } } */
+/* { dg-options "-gdwarf-4 -dA" } */
+
+struct node { int v; };
+
+typedef struct node plain_t;
+typedef struct node __arena arena_t;
+typedef unsigned int __arena arena_uint_t;
+
+plain_t *p;
+arena_t *a;
+arena_t  d;
+arena_uint_t *u;
+
+/* Without the fix only "plain_t" is left.
+
+   The name patterns accept either the strp form
+   (DW_AT_name: "arena_t") or the inline-string form
+   ("arena_t\0" ... DW_AT_name).  Which one is emitted depends on
+   whether the build detected an assembler that pools debug strings,
+   so a pattern fixed to one of them passes or fails by build
+   configuration rather than by what the compiler got right.  */
+/* { dg-final { scan-assembler-times "DIE \\(0x\[0-9a-f\]+\\) DW_TAG_typedef" 
3 } } */
+/* { dg-final { scan-assembler {(?n)( DW_AT_name: "plain_t"|"plain_t..".* 
DW_AT_name)} } } */
+/* { dg-final { scan-assembler {(?n)( DW_AT_name: "arena_t"|"arena_t..".* 
DW_AT_name)} } } */
+/* { dg-final { scan-assembler {(?n)( DW_AT_name: 
"arena_uint_t"|"arena_uint_t..".* DW_AT_name)} } } */
+/* { dg-final { scan-assembler "DW_AT_address_class" } } */
+
+/* An address space qualified type that no typedef names is unaffected,
+   and still shares the DIE of its unqualified variant.  */
+/* { dg-final { scan-assembler-times "DIE \\(0x\[0-9a-f\]+\\) 
DW_TAG_base_type" 2 } } */
diff --git a/gcc/testsuite/gcc.target/bpf/arena-1.c 
b/gcc/testsuite/gcc.target/bpf/arena-1.c
new file mode 100644
index 000000000000..1ceeffa186cf
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/arena-1.c
@@ -0,0 +1,29 @@
+/* Check that accesses through an arena pointer are lowered to a cast to
+   the generic address space followed by an ordinary load or store.  */
+
+/* { dg-do compile } */
+/* { dg-options "-O2 -masm=pseudoc" } */
+
+int
+load (int __arena *p)
+{
+  return *p;
+}
+
+void
+store (int __arena *p, int v)
+{
+  *p = v;
+}
+
+struct s { int a; int b; };
+
+int
+field (struct s __arena *p)
+{
+  return p->b;
+}
+
+/* { dg-final { scan-assembler-times "addr_space_cast\\(r\[0-9\]+, 0, 1\\)" 3 
} } */
+/* { dg-final { scan-assembler "= \\*\\(u32 \\*\\) \\(r\[0-9\]+\\+0\\)" } } */
+/* { dg-final { scan-assembler "= \\*\\(u32 \\*\\) \\(r\[0-9\]+\\+4\\)" } } */
diff --git a/gcc/testsuite/gcc.target/bpf/arena-2.c 
b/gcc/testsuite/gcc.target/bpf/arena-2.c
new file mode 100644
index 000000000000..5b997a23ecb7
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/arena-2.c
@@ -0,0 +1,20 @@
+/* Check the direction of explicit conversions between the arena and the
+   generic address spaces.  */
+
+/* { dg-do compile } */
+/* { dg-options "-O2 -masm=pseudoc" } */
+
+int __arena *
+to_arena (int *p)
+{
+  return (int __arena *) p;
+}
+
+int *
+from_arena (int __arena *p)
+{
+  return (int *) p;
+}
+
+/* { dg-final { scan-assembler "addr_space_cast\\(r\[0-9\]+, 1, 0\\)" } } */
+/* { dg-final { scan-assembler "addr_space_cast\\(r\[0-9\]+, 0, 1\\)" } } */
diff --git a/gcc/testsuite/gcc.target/bpf/arena-3.c 
b/gcc/testsuite/gcc.target/bpf/arena-3.c
new file mode 100644
index 000000000000..f91698700b2c
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/arena-3.c
@@ -0,0 +1,40 @@
+/* Check that objects declared in the arena address space are placed in
+   the section named after it, and are reached through a cast like any
+   other arena object.
+
+   The BTF has to agree with the symbol table about where the object
+   lives: libbpf looks a variable up in the section BTF names it in, so
+   a DATASEC of ".bss" for something emitted in ".addr_space.1" makes
+   the object fail to link with "failed to find symbol for variable".  */
+
+/* { dg-do compile } */
+/* { dg-options "-O2 -masm=pseudoc -gbtf -dA" } */
+
+int __arena g;
+extern int __arena e;
+
+/* A tentative definition, which is how the kernel's arena headers
+   declare these, and the shape that used to be categorized as .bss.  */
+int __arena tentative;
+
+int plain;
+
+int
+read (void)
+{
+  return g + e + tentative + plain;
+}
+
+void
+write (int v)
+{
+  g = v;
+}
+
+/* { dg-final { scan-assembler "\\.addr_space\\.1" } } */
+/* { dg-final { scan-assembler-times "addr_space_cast\\(r\[0-9\]+, 0, 1\\)" 4 
} } */
+
+/* The arena objects go in a DATASEC of their own, and the ordinary one
+   stays in .bss.  */
+/* { dg-final { scan-assembler "BTF_KIND_DATASEC '\\.addr_space\\.1'" } } */
+/* { dg-final { scan-assembler "BTF_KIND_DATASEC '\\.bss'" } } */
diff --git a/gcc/testsuite/gcc.target/bpf/arena-arg-1.c 
b/gcc/testsuite/gcc.target/bpf/arena-arg-1.c
new file mode 100644
index 000000000000..c379e718a5e9
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/arena-arg-1.c
@@ -0,0 +1,25 @@
+/* Check that an aggregate passed by value out of the arena is read
+   through a cast to the generic address space.  */
+
+/* { dg-do compile } */
+/* { dg-options "-O2 -masm=pseudoc" } */
+
+struct s { int a; int b; };
+
+void f (struct s);
+
+void
+by_pointer (struct s __arena *p)
+{
+  f (*p);
+}
+
+struct s __arena g;
+
+void
+by_object (void)
+{
+  f (g);
+}
+
+/* { dg-final { scan-assembler-times "addr_space_cast\\(r\[0-9\]+, 0, 1\\)" 2 
} } */
diff --git a/gcc/testsuite/gcc.target/bpf/arena-btf-1.c 
b/gcc/testsuite/gcc.target/bpf/arena-btf-1.c
new file mode 100644
index 000000000000..588879a456e5
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/arena-btf-1.c
@@ -0,0 +1,30 @@
+/* The BTF for a typedef of an arena type keeps the typedef, and with it
+   any btf_type_tag on it, which is how the kernel's arena headers carry
+   the address space into BTF: BTF has no way to record an address space
+   itself, so the tag is what a consumer sees.  */
+
+/* { dg-do compile } */
+/* { dg-options "-O2 -gbtf -dA" } */
+
+/* The macro expands to the keyword followed by the tag; the self
+   reference is not rescanned, so __arena reaches the parser as the
+   address space keyword.  This is the shape the kernel headers use.  */
+#define __arena __arena __attribute__((btf_type_tag("arena")))
+
+struct node { int v; };
+
+typedef struct node __arena node_t;
+
+node_t *p;
+
+int
+read_v (node_t *n)
+{
+  return n->v;
+}
+
+/* Without the typedef both of these are absent: the uses of node_t
+   resolve straight to "struct node".  */
+
+/* { dg-final { scan-assembler "BTF_KIND_TYPEDEF 'node_t'" } } */
+/* { dg-final { scan-assembler "BTF_KIND_TYPE_TAG 'arena'" } } */
-- 
2.53.1-Meta

Reply via email to