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.

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             | 140 ++++++++++++++++++
 gcc/config/aarch64/aarch64-jit.h              |  22 +++
 gcc/config/aarch64/t-aarch64                  |   4 +
 gcc/config/i386/i386-jit.cc                   |   6 -
 gcc/jit/jit-target.cc                         |  23 +++
 gcc/testsuite/jit.dg/all-non-failing-tests.h  |   3 +
 .../jit.dg/test-target-info-aarch64.c         |  61 ++++++++
 8 files changed, 254 insertions(+), 6 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 2f478e2a493..c6fda73513d 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 00000000000..b340c54c686
--- /dev/null
+++ b/gcc/config/aarch64/aarch64-jit.cc
@@ -0,0 +1,140 @@
+/* Subroutines for the jit front end on the AArch64 architecture.
+   Copyright (C) 2024 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
+
+  if (TARGET_AES)
+    jit_add_target_info ("target_feature", "aes");
+  if (TARGET_BF16_FP)
+    jit_add_target_info ("target_feature", "bf16");
+  if (TARGET_BTI)
+    jit_add_target_info ("target_feature", "bti");
+  if (TARGET_COMPLEX)
+    jit_add_target_info ("target_feature", "fcma");
+  if (TARGET_CRC32)
+    jit_add_target_info ("target_feature", "crc");
+  if (TARGET_DOTPROD)
+    jit_add_target_info ("target_feature", "dotprod");
+  if (TARGET_SVE_F32MM)
+    jit_add_target_info ("target_feature", "f32mm");
+  if (TARGET_SVE_F64MM)
+    jit_add_target_info ("target_feature", "f64mm");
+  if (TARGET_F16FML)
+    jit_add_target_info ("target_feature", "fhm");
+  if (TARGET_FP_F16INST)
+    jit_add_target_info ("target_feature", "fp16");
+  if (TARGET_FRINT)
+    jit_add_target_info ("target_feature", "frintts");
+  if (TARGET_I8MM)
+    jit_add_target_info ("target_feature", "i8mm");
+  if (TARGET_JSCVT)
+    jit_add_target_info ("target_feature", "jsconv");
+  if (TARGET_LSE)
+    jit_add_target_info ("target_feature", "lse");
+  if (TARGET_MEMTAG)
+    jit_add_target_info ("target_feature", "mte");
+  if (TARGET_PAUTH)
+  {
+    jit_add_target_info ("target_feature", "paca");
+    jit_add_target_info ("target_feature", "pacg");
+  }
+  if (TARGET_RNG)
+    jit_add_target_info ("target_feature", "rand");
+  if (TARGET_RCPC)
+    jit_add_target_info ("target_feature", "rcpc");
+  if (TARGET_RCPC2)
+    jit_add_target_info ("target_feature", "rcpc2");
+  if (TARGET_SIMD_RDMA)
+    jit_add_target_info ("target_feature", "rdm");
+  if (TARGET_SB)
+    jit_add_target_info ("target_feature", "sb");
+  if (TARGET_SHA2)
+    jit_add_target_info ("target_feature", "sha2");
+  if (TARGET_SHA3)
+    jit_add_target_info ("target_feature", "sha3");
+  if (TARGET_SIMD)
+    jit_add_target_info ("target_feature", "neon");
+  if (TARGET_SM4)
+    jit_add_target_info ("target_feature", "sm4");
+  if (TARGET_SVE)
+    jit_add_target_info ("target_feature", "sve");
+  if (TARGET_SVE2)
+    jit_add_target_info ("target_feature", "sve2");
+  if (TARGET_SVE2_AES)
+    jit_add_target_info ("target_feature", "sve2-aes");
+  if (TARGET_SVE2_BITPERM)
+    jit_add_target_info ("target_feature", "sve2-bitperm");
+  if (TARGET_SVE2_SHA3)
+    jit_add_target_info ("target_feature", "sve2-sha3");
+  if (TARGET_SVE2_SM4)
+    jit_add_target_info ("target_feature", "sve2-sm4");
+  if (TARGET_TME)
+    jit_add_target_info ("target_feature", "tme");
+  // TODO: features dit, dpb, dpb2, flagm, lor, pan, pmuv3, ras, spe, ssbs, vh
+
+  if (AARCH64_HAVE_ISA (V8_1A))
+    jit_add_target_info ("target_feature", "v8.1a");
+  if (AARCH64_HAVE_ISA (V8_2A))
+    jit_add_target_info ("target_feature", "v8.2a");
+  if (AARCH64_HAVE_ISA (V8_3A))
+    jit_add_target_info ("target_feature", "v8.3a");
+  if (AARCH64_HAVE_ISA (V8_4A))
+    jit_add_target_info ("target_feature", "v8.4a");
+  if (AARCH64_HAVE_ISA (V8_5A))
+    jit_add_target_info ("target_feature", "v8.5a");
+  if (AARCH64_HAVE_ISA (V8_6A))
+    jit_add_target_info ("target_feature", "v8.6a");
+  if (AARCH64_HAVE_ISA (V8_7A))
+    jit_add_target_info ("target_feature", "v8.7a");
+}
diff --git a/gcc/config/aarch64/aarch64-jit.h b/gcc/config/aarch64/aarch64-jit.h
new file mode 100644
index 00000000000..b8a777adcc9
--- /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) 2024 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 71242f05b09..5463fb1885a 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 c1e2929a473..318e7a8124e 100644
--- a/gcc/config/i386/i386-jit.cc
+++ b/gcc/config/i386/i386-jit.cc
@@ -59,12 +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);
-  }
-
 #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 40b47971c44..e40077f0b82 100644
--- a/gcc/jit/jit-target.cc
+++ b/gcc/jit/jit-target.cc
@@ -36,12 +36,35 @@ along with GCC; see the file COPYING3.  If not see
 
 /* 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
diff --git a/gcc/testsuite/jit.dg/all-non-failing-tests.h b/gcc/testsuite/jit.dg/all-non-failing-tests.h
index 4aa18e3b767..c89745b4d68 100644
--- a/gcc/testsuite/jit.dg/all-non-failing-tests.h
+++ b/gcc/testsuite/jit.dg/all-non-failing-tests.h
@@ -427,6 +427,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 00000000000..179431b08e4
--- /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;
+}

Reply via email to