The arena needs the addr_space_cast instruction, which no released
binutils supports.  With an older assembler, the compiler accepted
__arena and then emitted assembly that the assembler rejected.

Check for the instruction at configure time.  If it is missing,
__BPF_FEATURE_ARENA is not defined, and a use of __arena is an error,
through TARGET_ADDR_SPACE_DIAGNOSE_USAGE.  The keyword stays
registered, so the error names the address space and not an unknown
type name.  Configure does not fail, because no binutils release has
the instruction yet.

Gate the arena tests on a new effective target, bpf_arena.

        PR target/124974

gcc/ChangeLog:

        * configure.ac: Check whether the assembler supports the
        addr_space_cast instruction.
        * configure: Regenerate.
        * config.in: Regenerate.
        * config/bpf/bpf.cc (bpf_has_arena_p): New function.
        (bpf_addr_space_diagnose_usage): New function.
        (TARGET_ADDR_SPACE_DIAGNOSE_USAGE): Define.
        * config/bpf/bpf-c.cc (bpf_target_macros): Only define
        __BPF_FEATURE_ARENA when the assembler supports the
        instruction.
        * config/bpf/bpf-protos.h (bpf_has_arena_p): Declare.

gcc/testsuite/ChangeLog:

        * lib/target-supports.exp
        (check_effective_target_bpf_arena): New.
        * gcc.target/bpf/arena-1.c: Require it.
        * gcc.target/bpf/arena-2.c: Likewise.
        * gcc.target/bpf/arena-3.c: Likewise.
        * gcc.target/bpf/arena-btf-1.c: Likewise.
        * gcc.target/bpf/arena-arg-1.c: Likewise.
        * gcc.dg/debug/dwarf2/dwarf-addr-space-typedef-bpf.c: Likewise.

Signed-off-by: Vineet Gupta <[email protected]>
---
Changes in v3:
 - None.

Changes in v2:
 - Make the bpf_arena effective target an assembly probe, like tls: it
   asks whether the compiler accepts __arena, and does not run the
   assembler (Uros Bizjak).
 - Require bpf_arena in the new arena-arg-1.c too.

 gcc/config.in                                 |  7 ++++
 gcc/config/bpf/bpf-c.cc                       |  5 ++-
 gcc/config/bpf/bpf-protos.h                   |  1 +
 gcc/config/bpf/bpf.cc                         | 33 +++++++++++++++++
 gcc/configure                                 | 36 +++++++++++++++++++
 gcc/configure.ac                              | 11 ++++++
 .../dwarf2/dwarf-addr-space-typedef-bpf.c     |  1 +
 gcc/testsuite/gcc.target/bpf/arena-1.c        |  1 +
 gcc/testsuite/gcc.target/bpf/arena-2.c        |  1 +
 gcc/testsuite/gcc.target/bpf/arena-3.c        |  1 +
 gcc/testsuite/gcc.target/bpf/arena-arg-1.c    |  1 +
 gcc/testsuite/gcc.target/bpf/arena-btf-1.c    |  1 +
 gcc/testsuite/lib/target-supports.exp         | 13 +++++++
 13 files changed, 111 insertions(+), 1 deletion(-)

diff --git a/gcc/config.in b/gcc/config.in
index 241c299682d0..05af45b94868 100644
--- a/gcc/config.in
+++ b/gcc/config.in
@@ -415,6 +415,13 @@
 #endif
 
 
+/* Define if your bpf assembler supports the addr_space_cast instruction, which
+   the arena address space needs. */
+#ifndef USED_FOR_TARGET
+#undef HAVE_AS_BPF_ADDR_SPACE_CAST
+#endif
+
+
 /* Define if your avr assembler supports -mrmw option. */
 #ifndef USED_FOR_TARGET
 #undef HAVE_AS_AVR_MRMW_OPTION
diff --git a/gcc/config/bpf/bpf-c.cc b/gcc/config/bpf/bpf-c.cc
index b7e18aff9739..2cc473081523 100644
--- a/gcc/config/bpf/bpf-c.cc
+++ b/gcc/config/bpf/bpf-c.cc
@@ -24,6 +24,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "system.h"
 #include "coretypes.h"
 #include "tm.h"
+#include "tm_p.h"
 #include "c-family/c-common.h"
 #include "cpplib.h"
 
@@ -89,7 +90,9 @@ bpf_target_macros (cpp_reader *pfile)
   if (bpf_has_callx)
     builtin_define ("__BPF_FEATURE_CALLX");
 
-  builtin_define ("__BPF_FEATURE_ARENA");
+  /* Only if the assembler can encode addr_space_cast.  */
+  if (bpf_has_arena_p ())
+    builtin_define ("__BPF_FEATURE_ARENA");
 
   /* Other CPU features can only be enabled/disabled generically by
      selecting the corresponding CPU version.  */
diff --git a/gcc/config/bpf/bpf-protos.h b/gcc/config/bpf/bpf-protos.h
index 2108406a74c7..9790303b27d1 100644
--- a/gcc/config/bpf/bpf-protos.h
+++ b/gcc/config/bpf/bpf-protos.h
@@ -27,6 +27,7 @@ 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 bool bpf_has_arena_p (void);
 extern void bpf_print_operand (FILE *, rtx, int);
 extern void bpf_print_operand_address (FILE *, rtx);
 extern void bpf_expand_prologue (void);
diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
index c611c36bd508..d2b20f33369b 100644
--- a/gcc/config/bpf/bpf.cc
+++ b/gcc/config/bpf/bpf.cc
@@ -616,6 +616,39 @@ bpf_addr_space_subset_p (addr_space_t subset, addr_space_t 
superset)
 #undef TARGET_ADDR_SPACE_SUBSET_P
 #define TARGET_ADDR_SPACE_SUBSET_P bpf_addr_space_subset_p
 
+/* Whether the assembler can assemble an addr_space_cast instruction.
+   The arena address space is implemented entirely in terms of that
+   instruction, so without it the compiler could only emit assembly its
+   own assembler would reject.  Detected at configure time; see
+   HAVE_AS_BPF_ADDR_SPACE_CAST.  */
+
+bool
+bpf_has_arena_p (void)
+{
+#ifdef HAVE_AS_BPF_ADDR_SPACE_CAST
+  return true;
+#else
+  return false;
+#endif
+}
+
+/* Implement TARGET_ADDR_SPACE_DIAGNOSE_USAGE.
+
+   The keyword is registered unconditionally, so that naming the arena
+   with an assembler that cannot encode a cast gives this rather than a
+   syntax error about an unknown type name.  */
+
+static void
+bpf_addr_space_diagnose_usage (addr_space_t as, location_t loc)
+{
+  if (as == ADDR_SPACE_ARENA && !bpf_has_arena_p ())
+    error_at (loc, "the %<__arena%> address space requires an assembler "
+             "that supports the %<addr_space_cast%> instruction");
+}
+
+#undef TARGET_ADDR_SPACE_DIAGNOSE_USAGE
+#define TARGET_ADDR_SPACE_DIAGNOSE_USAGE bpf_addr_space_diagnose_usage
+
 /* Convert the pointer expression OP, of type FROM_TYPE, into a pointer
    of TO_TYPE, which points into a different named address space.  */
 
diff --git a/gcc/configure b/gcc/configure
index f4bafc11c46c..fed077904deb 100755
--- a/gcc/configure
+++ b/gcc/configure
@@ -29179,6 +29179,42 @@ if test $gcc_cv_as_alpha_jsrdirect_relocs = yes; then
 
 $as_echo "#define HAVE_AS_JSRDIRECT_RELOCS 1" >>confdefs.h
 
+fi
+
+    ;;
+
+  bpf-*-*)
+    # The arena address space is implemented with the addr_space_cast
+    # instruction, so it can only be offered if gas can assemble one.
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking assembler for 
addr_space_cast instruction" >&5
+$as_echo_n "checking assembler for addr_space_cast instruction... " >&6; }
+if ${gcc_cv_as_bpf_addr_space_cast+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  gcc_cv_as_bpf_addr_space_cast=no
+  if test x"$gcc_cv_as" != x; then
+    $as_echo 'r1 = addr_space_cast(r1, 0, 1)' > conftest.s
+    if { ac_try='$gcc_cv_as $gcc_cv_as_flags -mdialect=pseudoc -o conftest.o 
conftest.s >&5'
+  { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; }
+    then
+       gcc_cv_as_bpf_addr_space_cast=yes
+    else
+      echo "configure: failed program was" >&5
+      cat conftest.s >&5
+    fi
+    rm -f conftest.o conftest.s
+  fi
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: 
$gcc_cv_as_bpf_addr_space_cast" >&5
+$as_echo "$gcc_cv_as_bpf_addr_space_cast" >&6; }
+if test $gcc_cv_as_bpf_addr_space_cast = yes; then
+
+$as_echo "#define HAVE_AS_BPF_ADDR_SPACE_CAST 1" >>confdefs.h
+
 fi
 
     ;;
diff --git a/gcc/configure.ac b/gcc/configure.ac
index 0165f8268a4d..61a679294959 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -4688,6 +4688,17 @@ AS_HELP_STRING([--disable-fix-cortex-a53-843419],
   [Define if your assembler supports the lituse_jsrdirect relocation.])])
     ;;
 
+  bpf-*-*)
+    # The arena address space is implemented with the addr_space_cast
+    # instruction, so it can only be offered if gas can assemble one.
+    gcc_GAS_CHECK_FEATURE([addr_space_cast instruction],
+      gcc_cv_as_bpf_addr_space_cast,
+      [-mdialect=pseudoc], [r1 = addr_space_cast(r1, 0, 1)],,
+      [AC_DEFINE(HAVE_AS_BPF_ADDR_SPACE_CAST, 1,
+               [Define if your bpf assembler supports the addr_space_cast
+                instruction, which the arena address space needs.])])
+    ;;
+
   avr-*-*)
     gcc_GAS_CHECK_FEATURE([--mlink-relax option], gcc_cv_as_avr_mlink_relax,
       [--mlink-relax], [.text],,
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
index 0b38aa4f3253..9c53d1717ef9 100644
--- 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
@@ -13,6 +13,7 @@
    referenced.  */
 
 /* { dg-do compile { target { bpf-*-* } } } */
+/* { dg-require-effective-target bpf_arena } */
 /* { dg-options "-gdwarf-4 -dA" } */
 
 struct node { int v; };
diff --git a/gcc/testsuite/gcc.target/bpf/arena-1.c 
b/gcc/testsuite/gcc.target/bpf/arena-1.c
index 1ceeffa186cf..b3b8d4752202 100644
--- a/gcc/testsuite/gcc.target/bpf/arena-1.c
+++ b/gcc/testsuite/gcc.target/bpf/arena-1.c
@@ -2,6 +2,7 @@
    the generic address space followed by an ordinary load or store.  */
 
 /* { dg-do compile } */
+/* { dg-require-effective-target bpf_arena } */
 /* { dg-options "-O2 -masm=pseudoc" } */
 
 int
diff --git a/gcc/testsuite/gcc.target/bpf/arena-2.c 
b/gcc/testsuite/gcc.target/bpf/arena-2.c
index 5b997a23ecb7..f7649a927c32 100644
--- a/gcc/testsuite/gcc.target/bpf/arena-2.c
+++ b/gcc/testsuite/gcc.target/bpf/arena-2.c
@@ -2,6 +2,7 @@
    generic address spaces.  */
 
 /* { dg-do compile } */
+/* { dg-require-effective-target bpf_arena } */
 /* { dg-options "-O2 -masm=pseudoc" } */
 
 int __arena *
diff --git a/gcc/testsuite/gcc.target/bpf/arena-3.c 
b/gcc/testsuite/gcc.target/bpf/arena-3.c
index f91698700b2c..fc3d0b35056f 100644
--- a/gcc/testsuite/gcc.target/bpf/arena-3.c
+++ b/gcc/testsuite/gcc.target/bpf/arena-3.c
@@ -8,6 +8,7 @@
    the object fail to link with "failed to find symbol for variable".  */
 
 /* { dg-do compile } */
+/* { dg-require-effective-target bpf_arena } */
 /* { dg-options "-O2 -masm=pseudoc -gbtf -dA" } */
 
 int __arena g;
diff --git a/gcc/testsuite/gcc.target/bpf/arena-arg-1.c 
b/gcc/testsuite/gcc.target/bpf/arena-arg-1.c
index c379e718a5e9..1f32e65d9ec8 100644
--- a/gcc/testsuite/gcc.target/bpf/arena-arg-1.c
+++ b/gcc/testsuite/gcc.target/bpf/arena-arg-1.c
@@ -2,6 +2,7 @@
    through a cast to the generic address space.  */
 
 /* { dg-do compile } */
+/* { dg-require-effective-target bpf_arena } */
 /* { dg-options "-O2 -masm=pseudoc" } */
 
 struct s { int a; int b; };
diff --git a/gcc/testsuite/gcc.target/bpf/arena-btf-1.c 
b/gcc/testsuite/gcc.target/bpf/arena-btf-1.c
index 588879a456e5..e6befd255187 100644
--- a/gcc/testsuite/gcc.target/bpf/arena-btf-1.c
+++ b/gcc/testsuite/gcc.target/bpf/arena-btf-1.c
@@ -4,6 +4,7 @@
    itself, so the tag is what a consumer sees.  */
 
 /* { dg-do compile } */
+/* { dg-require-effective-target bpf_arena } */
 /* { dg-options "-O2 -gbtf -dA" } */
 
 /* The macro expands to the keyword followed by the tag; the self
diff --git a/gcc/testsuite/lib/target-supports.exp 
b/gcc/testsuite/lib/target-supports.exp
index 0550058053d6..631088cf2c09 100644
--- a/gcc/testsuite/lib/target-supports.exp
+++ b/gcc/testsuite/lib/target-supports.exp
@@ -4709,6 +4709,19 @@ proc check_effective_target_scalar_all_fma { } {
     return 0
 }
 
+# Return 1 if the compiler accepts the BPF __arena address space.  It does
+# only if configure found an assembler that can assemble addr_space_cast,
+# so like tls this decides even dg-do compile tests.
+
+proc check_effective_target_bpf_arena { } {
+    if { ![istarget bpf-*-*] } {
+       return 0
+    }
+    return [check_no_compiler_messages bpf_arena assembly {
+       int __arena *p;
+    }]
+}
+
 # Return 1 if the target supports compiling fixed-point,
 # 0 otherwise.
 
-- 
2.53.1-Meta

Reply via email to