From: Antoni Boucher <[email protected]>

gcc/ChangeLog:

        * config.gcc: Mention new aarch64-jit.o file.
        * config/aarch64/aarch64-jit.cc: Detect CPU features.
        * config/aarch64/aarch64-jit.h: New target hook.
        * config/aarch64/t-aarch64: Mention new aarch64-jit.cc file.
        * config/i386/i386-jit.cc: Remove target-dependent type
        detection.

gcc/jit/ChangeLog:

        * jit-target.cc: Move target-dependent type detection to common
        file.
        (jit_add_target_info_space): New function.
        * jit-target.h (jit_add_target_info_space): New function.

gcc/testsuite/ChangeLog:

        * jit.dg/all-non-failing-tests.h: Mention new test.
        * jit.dg/test-target-info-aarch64.c: New test.

Co-authored-by: winstonallo <[email protected]>
---
 gcc/config.gcc                                |  1 +
 gcc/config/aarch64/aarch64-jit.cc             | 70 +++++++++++++++++++
 gcc/config/aarch64/aarch64-jit.h              | 22 ++++++
 gcc/config/aarch64/t-aarch64                  |  4 ++
 gcc/config/i386/i386-jit.cc                   | 18 -----
 gcc/jit/jit-target.cc                         | 47 +++++++++++++
 gcc/jit/jit-target.h                          |  4 +-
 gcc/testsuite/jit.dg/all-non-failing-tests.h  |  3 +
 .../jit.dg/test-target-info-aarch64.c         | 61 ++++++++++++++++
 9 files changed, 211 insertions(+), 19 deletions(-)
 create mode 100644 gcc/config/aarch64/aarch64-jit.cc
 create mode 100644 gcc/config/aarch64/aarch64-jit.h
 create mode 100644 gcc/testsuite/jit.dg/test-target-info-aarch64.c

diff --git a/gcc/config.gcc b/gcc/config.gcc
index c678b801f705..b61dc86a0516 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -359,6 +359,7 @@ aarch64*-*-*)
        c_target_objs="aarch64-c.o"
        cxx_target_objs="aarch64-c.o"
        d_target_objs="aarch64-d.o"
+       jit_target_objs="aarch64-jit.o"
        extra_objs="aarch64-builtins.o aarch-common.o aarch64-elf-metadata.o 
aarch64-sve-builtins.o aarch64-sve-builtins-shapes.o 
aarch64-sve-builtins-base.o aarch64-sve-builtins-sve2.o 
aarch64-sve-builtins-sme.o cortex-a57-fma-steering.o aarch64-speculation.o 
aarch-bti-insert.o aarch64-early-ra.o aarch64-ldp-fusion.o 
aarch64-sched-dispatch.o"
        target_gtfiles="\$(srcdir)/config/aarch64/aarch64-protos.h 
\$(srcdir)/config/aarch64/aarch64-builtins.h 
\$(srcdir)/config/aarch64/aarch64-builtins.cc 
\$(srcdir)/config/aarch64/aarch64-sve-builtins.h 
\$(srcdir)/config/aarch64/aarch64-sve-builtins.cc"
        target_has_targetm_common=yes
diff --git a/gcc/config/aarch64/aarch64-jit.cc 
b/gcc/config/aarch64/aarch64-jit.cc
new file mode 100644
index 000000000000..5e0b2b115d3b
--- /dev/null
+++ b/gcc/config/aarch64/aarch64-jit.cc
@@ -0,0 +1,70 @@
+/* Subroutines for the jit front end on the AArch64 architecture.
+   Copyright (C) 2025 Free Software Foundation, Inc.
+
+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/>.  */
+
+#define IN_TARGET_CODE 1
+
+#define INCLUDE_STRING
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "target.h"
+#include "tm.h"
+#include "tm_jit.h"
+#include <sys/auxv.h>
+#include "jit/jit-target.h"
+#include "jit/jit-target-def.h"
+
+/* Implement TARGET_JIT_REGISTER_CPU_TARGET_INFO.  */
+
+#ifndef CROSS_DIRECTORY_STRUCTURE
+extern const char *host_detect_local_cpu (int argc, const char **argv);
+#endif
+
+void
+aarch64_jit_register_target_info (void)
+{
+  const char *params[] = {"arch"};
+#ifndef CROSS_DIRECTORY_STRUCTURE
+  const char* local_cpu = host_detect_local_cpu (2, params);
+  if (local_cpu != NULL)
+  {
+    std::string arch = local_cpu;
+    free (const_cast <char *> (local_cpu));
+
+    const char* arg = "-march=";
+    size_t arg_pos = arch.find (arg) + strlen (arg);
+    size_t end_pos = arch.find (" ", arg_pos);
+
+    std::string cpu = arch.substr (arg_pos, end_pos - arg_pos);
+    jit_target_set_arch (cpu);
+  }
+#endif
+
+#define AARCH64_OPT_EXTENSION(NAME, IDENT, REQUIRES, EXPLICIT_ON, \
+  EXPLICIT_OFF, FEATURE_STRING) \
+if (AARCH64_HAVE_ISA (IDENT)) jit_add_target_info_space ("target_feature", 
NAME, \
+  FEATURE_STRING);
+#include "aarch64-option-extensions.def"
+
+  if (TARGET_BTI)
+    jit_add_target_info ("target_feature", "bti");
+  // TODO: features dit, dpb, dpb2, lor, pan, pmuv3, ras, spe, vh
+
+#define AARCH64_ARCH(NAME, CORE, ARCH_IDENT, ARCH_REV, FLAGS) \
+if (AARCH64_HAVE_ISA (ARCH_IDENT)) jit_add_target_info ("target_feature", 
NAME);
+#include "aarch64-arches.def"
+}
diff --git a/gcc/config/aarch64/aarch64-jit.h b/gcc/config/aarch64/aarch64-jit.h
new file mode 100644
index 000000000000..6775d5a0c908
--- /dev/null
+++ b/gcc/config/aarch64/aarch64-jit.h
@@ -0,0 +1,22 @@
+/* Definitions for the jit front end on the AArch64 architecture.
+   Copyright (C) 2025 Free Software Foundation, Inc.
+
+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/>.  */
+
+/* Defined in aarch64-jit.cc.  */
+extern void aarch64_jit_register_target_info (void);
+
+/* Target hooks for jit language.  */
+#define TARGET_JIT_REGISTER_CPU_TARGET_INFO aarch64_jit_register_target_info
diff --git a/gcc/config/aarch64/t-aarch64 b/gcc/config/aarch64/t-aarch64
index 71242f05b091..5463fb1885a3 100644
--- a/gcc/config/aarch64/t-aarch64
+++ b/gcc/config/aarch64/t-aarch64
@@ -160,6 +160,10 @@ aarch64-d.o: $(srcdir)/config/aarch64/aarch64-d.cc
        $(COMPILE) $<
        $(POSTCOMPILE)
 
+aarch64-jit.o: $(srcdir)/config/aarch64/aarch64-jit.cc
+       $(COMPILE) $<
+       $(POSTCOMPILE)
+
 PASSES_EXTRA += $(srcdir)/config/aarch64/aarch64-passes.def
 
 cortex-a57-fma-steering.o: $(srcdir)/config/aarch64/cortex-a57-fma-steering.cc 
\
diff --git a/gcc/config/i386/i386-jit.cc b/gcc/config/i386/i386-jit.cc
index 73ca5905c18b..318e7a8124e8 100644
--- a/gcc/config/i386/i386-jit.cc
+++ b/gcc/config/i386/i386-jit.cc
@@ -59,24 +59,6 @@ ix86_jit_register_target_info (void)
   }
 #endif
 
-  if (targetm.scalar_mode_supported_p (TImode))
-  {
-    jit_target_add_supported_target_dependent_type (GCC_JIT_TYPE_UINT128_T);
-    jit_target_add_supported_target_dependent_type (GCC_JIT_TYPE_INT128_T);
-  }
-
-  if (float16_type_node != NULL && TYPE_PRECISION (float16_type_node) == 16)
-    jit_target_add_supported_target_dependent_type (GCC_JIT_TYPE_FLOAT16);
-
-  if (float32_type_node != NULL && TYPE_PRECISION (float32_type_node) == 32)
-    jit_target_add_supported_target_dependent_type (GCC_JIT_TYPE_FLOAT32);
-
-  if (float64_type_node != NULL && TYPE_PRECISION (float64_type_node) == 64)
-    jit_target_add_supported_target_dependent_type (GCC_JIT_TYPE_FLOAT64);
-
-  if (float128_type_node != NULL && TYPE_PRECISION (float128_type_node) == 128)
-    jit_target_add_supported_target_dependent_type (GCC_JIT_TYPE_FLOAT128);
-
 #define ADD_TARGET_INFO jit_add_target_info
 #include "i386-rust-and-jit.inc"
 #undef ADD_TARGET_INFO
diff --git a/gcc/jit/jit-target.cc b/gcc/jit/jit-target.cc
index 40b47971c44c..4ecd0014a1dc 100644
--- a/gcc/jit/jit-target.cc
+++ b/gcc/jit/jit-target.cc
@@ -15,6 +15,7 @@ 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/>.  */
 
+#define INCLUDE_SSTREAM
 #define INCLUDE_STRING
 #define INCLUDE_ALGORITHM
 #include "config.h"
@@ -30,18 +31,42 @@ along with GCC; see the file COPYING3.  If not see
 #include "tm_p.h"
 #include "target.h"
 #include "calls.h"
+#include <iterator>
 
 #include "jit-playback.h"
 #include "jit-target.h"
 
 /* Initialize all variables of the Target structure.  */
 
+void
+jit_target_dependent_types_init ()
+{
+  if (targetm.scalar_mode_supported_p (TImode))
+  {
+    jit_target_add_supported_target_dependent_type (GCC_JIT_TYPE_UINT128_T);
+    jit_target_add_supported_target_dependent_type (GCC_JIT_TYPE_INT128_T);
+  }
+
+  if (float16_type_node != NULL && TYPE_PRECISION (float16_type_node) == 16)
+    jit_target_add_supported_target_dependent_type (GCC_JIT_TYPE_FLOAT16);
+
+  if (float32_type_node != NULL && TYPE_PRECISION (float32_type_node) == 32)
+    jit_target_add_supported_target_dependent_type (GCC_JIT_TYPE_FLOAT32);
+
+  if (float64_type_node != NULL && TYPE_PRECISION (float64_type_node) == 64)
+    jit_target_add_supported_target_dependent_type (GCC_JIT_TYPE_FLOAT64);
+
+  if (float128_type_node != NULL && TYPE_PRECISION (float128_type_node) == 128)
+    jit_target_add_supported_target_dependent_type (GCC_JIT_TYPE_FLOAT128);
+}
+
 void
 jit_target_init ()
 {
   /* Initialize target info tables, the keys required by the language are added
      last, so that the CPU handler can override.  */
   targetjitm.jit_register_cpu_target_info ();
+  jit_target_dependent_types_init ();
 }
 
 /* Add a target info key:value to JIT_TARGET_INFO for use by
@@ -59,6 +84,28 @@ jit_add_target_info (const char *key, const char *value)
     jit_target_info->m_info[key].insert (value);
 }
 
+/*
+ This splits the values by whitespace and calls jit_add_target_info for each
+ value.
+ */
+void jit_add_target_info_space (const char *key, const char *name,
+  const char *values)
+{
+  std::istringstream iss (values);
+  std::vector<std::string> split_values;
+  std::copy (std::istream_iterator<std::string> (iss),
+    std::istream_iterator<std::string> (),
+    std::back_inserter (split_values));
+
+  if (split_values.empty ())
+    jit_add_target_info (key, name);
+  else
+  {
+    for (auto value : split_values)
+      jit_add_target_info (key, value.c_str ());
+  }
+}
+
 void
 jit_target_set_arch (std::string const& arch)
 {
diff --git a/gcc/jit/jit-target.h b/gcc/jit/jit-target.h
index 626ae8179ff1..45934236dca4 100644
--- a/gcc/jit/jit-target.h
+++ b/gcc/jit/jit-target.h
@@ -55,7 +55,7 @@ struct target_info {
     bool has_target_value (const char *key, const char *value);
 
     std::unordered_map<const char *,
-       std::unordered_set<const char *, CStringHash, CStringEqual>,
+       std::unordered_set<std::string>,
        CStringHash, CStringEqual>
        m_info;
     std::string m_arch;
@@ -70,6 +70,8 @@ extern void jit_target_set_arch (std::string const& arch);
 extern void
 jit_target_add_supported_target_dependent_type (enum gcc_jit_types type_);
 extern void jit_add_target_info (const char *key, const char *value);
+extern void jit_add_target_info_space (const char *key, const char *name,
+  const char *values);
 extern target_info * jit_get_target_info ();
 
 #endif /* GCC_JIT_TARGET_H  */
diff --git a/gcc/testsuite/jit.dg/all-non-failing-tests.h 
b/gcc/testsuite/jit.dg/all-non-failing-tests.h
index fe9ad1da3f4d..9b8c690c3240 100644
--- a/gcc/testsuite/jit.dg/all-non-failing-tests.h
+++ b/gcc/testsuite/jit.dg/all-non-failing-tests.h
@@ -441,6 +441,9 @@
 /* test-target-info.c: This can't be in the testcases array as it
    is target-specific.  */
 
+/* test-target-info-aarch64.c: This can't be in the testcases array as it
+   is target-specific.  */
+
 /* test-types.c */
 #define create_code create_code_types
 #define verify_code verify_code_types
diff --git a/gcc/testsuite/jit.dg/test-target-info-aarch64.c 
b/gcc/testsuite/jit.dg/test-target-info-aarch64.c
new file mode 100644
index 000000000000..179431b08e42
--- /dev/null
+++ b/gcc/testsuite/jit.dg/test-target-info-aarch64.c
@@ -0,0 +1,61 @@
+/* { dg-do compile { target aarch64-*-* } } */
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "libgccjit.h"
+
+#define TEST_PROVIDES_MAIN
+#include "harness.h"
+
+void
+create_code (gcc_jit_context *ctxt, void *user_data)
+{
+}
+
+void
+verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
+{
+}
+
+int
+main (int argc, char **argv)
+{
+  /*  This is the same as the main provided by harness.h, but calls 
gcc_jit_context_get_target_info.  */
+  gcc_jit_context *ctxt;
+  ctxt = gcc_jit_context_acquire ();
+  if (!ctxt)
+    {
+      fail ("gcc_jit_context_acquire failed");
+      return -1;
+    }
+  gcc_jit_target_info* info = gcc_jit_context_get_target_info (ctxt);
+
+  int sse2_supported = gcc_jit_target_info_cpu_supports (info, "sse2");
+  CHECK_VALUE (sse2_supported, 0);
+
+  int supports_128bit_int =
+    gcc_jit_target_info_supports_target_dependent_type (info,
+                                                       GCC_JIT_TYPE_INT128_T);
+  CHECK_VALUE (supports_128bit_int, 1);
+  gcc_jit_target_info_release (info);
+  gcc_jit_context_release (ctxt);
+
+  int i;
+
+  for (i = 1; i <= 5; i++)
+    {
+      snprintf (test, sizeof (test),
+               "%s iteration %d of %d",
+                extract_progname (argv[0]),
+                i, 5);
+
+      //printf ("ITERATION %d\n", i);
+      test_jit (argv[0], NULL);
+      //printf ("\n");
+    }
+
+  totals ();
+
+  return 0;
+}
-- 
2.54.0

Reply via email to