]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libgccjit: Add support for setting the link section of global variables [PR100688]
authorAntoni Boucher <bouanto@zoho.com>
Sun, 12 Dec 2021 21:16:21 +0000 (16:16 -0500)
committerAntoni Boucher <bouanto@zoho.com>
Sun, 12 Dec 2021 21:16:23 +0000 (16:16 -0500)
2021-12-12  Antoni Boucher  <bouanto@zoho.com>

gcc/jit/
PR target/100688
* docs/topics/compatibility.rst (LIBGCCJIT_ABI_18): New ABI
tag.
* docs/topics/expressions.rst: Add documentation for the
function gcc_jit_lvalue_set_link_section.
* jit-playback.h: New function (set_link_section).
* jit-recording.c: New function (set_link_section) and
support for setting the link section.
* jit-recording.h: New function (set_link_section) and new
field m_link_section.
* libgccjit.c: New function (gcc_jit_lvalue_set_link_section).
* libgccjit.h: New function (gcc_jit_lvalue_set_link_section).
* libgccjit.map (LIBGCCJIT_ABI_18): New ABI tag.

gcc/testsuite/
PR target/100688
* jit.dg/all-non-failing-tests.h: Mention new test
link-section-assembler.
* jit.dg/test-link-section-assembler.c: New test.
* jit.dg/jit.exp: New helper function to test that the
assembly contains a pattern.

gcc/jit/docs/topics/compatibility.rst
gcc/jit/docs/topics/expressions.rst
gcc/jit/jit-playback.h
gcc/jit/jit-recording.c
gcc/jit/jit-recording.h
gcc/jit/libgccjit.c
gcc/jit/libgccjit.h
gcc/jit/libgccjit.map
gcc/testsuite/jit.dg/all-non-failing-tests.h
gcc/testsuite/jit.dg/jit.exp
gcc/testsuite/jit.dg/test-link-section-assembler.c [new file with mode: 0644]

index 2ad6e4232f73529c082d2e0b5f362afbf87503b6..c6c14f0a7612fceb6c18bad9f94ddcf09884d4a4 100644 (file)
@@ -293,3 +293,12 @@ entrypoints:
 thread-local storage model of a variable:
 
   * :func:`gcc_jit_lvalue_set_tls_model`
+
+.. _LIBGCCJIT_ABI_18:
+
+``LIBGCCJIT_ABI_18``
+-----------------------
+``LIBGCCJIT_ABI_18`` covers the addition of an API entrypoint to set the link
+section of a variable:
+
+  * :func:`gcc_jit_lvalue_set_link_section`
index 386b80d8f8b34374e953b88007f20e5622402ab9..280e0eaea6806d5001381cc2565a972575a70797 100644 (file)
@@ -576,6 +576,27 @@ where the rvalue is computed by reading from the storage area.
 
       #ifdef LIBGCCJIT_HAVE_gcc_jit_lvalue_set_tls_model
 
+.. function:: void
+              gcc_jit_lvalue_set_link_section (gcc_jit_lvalue *lvalue,
+                                               const char *section_name)
+
+   Set the link section of a variable.
+   The parameter ``section_name`` must be non-NULL and must contain the
+   leading dot. Analogous to:
+
+   .. code-block:: c
+
+     int variable __attribute__((section(".section")));
+
+   in C.
+
+   This entrypoint was added in :ref:`LIBGCCJIT_ABI_18`; you can test for
+   its presence using
+
+   .. code-block:: c
+
+      #ifdef LIBGCCJIT_HAVE_gcc_jit_lvalue_set_link_section
+
 Global variables
 ****************
 
index c9839c21a66439552f4afc2bcd81a974296936de..21ddffb228ffcbb3388a5a18dd7d781204c1b07a 100644 (file)
@@ -681,6 +681,12 @@ public:
     set_decl_tls_model (as_tree (), tls_model);
   }
 
+  void
+  set_link_section (const char* name)
+  {
+    set_decl_section_name (as_tree (), name);
+  }
+
 private:
   bool mark_addressable (location *loc);
 };
index 5ba35bda44c4421a1af460f4ed98346ba10180ee..b42407909579860caafeae2317c90f6153e1f395 100644 (file)
@@ -3724,6 +3724,11 @@ recording::lvalue::set_tls_model (enum gcc_jit_tls_model model)
     m_tls_model = model;
 }
 
+void recording::lvalue::set_link_section (const char *name)
+{
+  m_link_section = new_string (name);
+}
+
 /* The implementation of class gcc::jit::recording::param.  */
 
 /* Implementation of pure virtual hook recording::memento::replay_into
@@ -4568,8 +4573,8 @@ static const enum tls_model tls_models[] = {
 void
 recording::global::replay_into (replayer *r)
 {
-    playback::lvalue *global = m_initializer
-    ? r->new_global_initialized (playback_location (r, m_loc),
+  playback::lvalue *global = m_initializer
+  ? r->new_global_initialized (playback_location (r, m_loc),
                                 m_kind,
                                 m_type->playback_type (),
                                 m_type->dereference ()->get_size (),
@@ -4577,13 +4582,17 @@ recording::global::replay_into (replayer *r)
                                 / m_type->dereference ()->get_size (),
                                 m_initializer,
                                 playback_string (m_name))
-    : r->new_global (playback_location (r, m_loc),
+  : r->new_global (playback_location (r, m_loc),
                     m_kind,
                     m_type->playback_type (),
                     playback_string (m_name));
+
   if (m_tls_model != GCC_JIT_TLS_MODEL_NONE)
     global->set_tls_model (recording::tls_models[m_tls_model]);
 
+  if (m_link_section != NULL)
+    global->set_link_section (m_link_section->c_str ());
+
   set_playback_obj (global);
 }
 
@@ -4713,6 +4722,12 @@ recording::global::write_reproducer (reproducer &r)
             id,
             tls_model_enum_strings[m_tls_model]);
 
+  if (m_link_section != NULL)
+    r.write ("  gcc_jit_lvalue_set_link_section (%s, /* gcc_jit_lvalue *lvalue */\n"
+       "                                  \"%s\"); /* */\n",
+     id,
+     m_link_section->c_str ());
+
   if (m_initializer)
     switch (m_type->dereference ()->get_size ())
       {
index 72fa30c631e3a06f77290ef8cf708f6d04d49a77..cedb24720cfdfdf4bf45c212622e70c7035bb224 100644 (file)
@@ -1133,7 +1133,8 @@ public:
          location *loc,
          type *type_)
     : rvalue (ctxt, loc, type_),
-    m_tls_model (GCC_JIT_TLS_MODEL_NONE)
+    m_tls_model (GCC_JIT_TLS_MODEL_NONE),
+    m_link_section (NULL)
     {}
 
   playback::lvalue *
@@ -1156,9 +1157,11 @@ public:
   virtual const char *access_as_lvalue (reproducer &r);
   virtual bool is_global () const { return false; }
   void set_tls_model (enum gcc_jit_tls_model model);
+  void set_link_section (const char *name);
 
 protected:
   enum gcc_jit_tls_model m_tls_model;
+  string *m_link_section;
 };
 
 class param : public lvalue
index 7ccb76a6a194aa395c4f78eba886696bfa64ce1d..59cef614d4b7a9f25c60f603725f5e6d53fc87c8 100644 (file)
@@ -2235,6 +2235,19 @@ gcc_jit_lvalue_set_tls_model (gcc_jit_lvalue *lvalue,
   lvalue->set_tls_model (model);
 }
 
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::lvalue::set_link_section method in jit-recording.c.  */
+
+void
+gcc_jit_lvalue_set_link_section (gcc_jit_lvalue *lvalue,
+                           const char *section_name)
+{
+  RETURN_IF_FAIL (section_name, NULL, NULL, "NULL section_name");
+  lvalue->set_link_section (section_name);
+}
+
 /* Public entrypoint.  See description in libgccjit.h.
 
    After error-checking, the real work is done by the
index 5aa2e40c7a4cbee74de0c3c8f03cbe1306d41bc8..024c8d79f4b6bd4c378b32c8b43e6eff7162d4a5 100644 (file)
@@ -1110,6 +1110,20 @@ extern void
 gcc_jit_lvalue_set_tls_model (gcc_jit_lvalue *lvalue,
                            enum gcc_jit_tls_model model);
 
+#define LIBGCCJIT_HAVE_gcc_jit_lvalue_set_link_section
+
+/* Set the link section of a global variable; analogous to:
+     __attribute__((section(".section_name")))
+   in C.
+
+   This API entrypoint was added in LIBGCCJIT_ABI_18; you can test for its
+   presence using
+     #ifdef LIBGCCJIT_HAVE_gcc_jit_lvalue_set_link_section
+*/
+extern void
+gcc_jit_lvalue_set_link_section (gcc_jit_lvalue *lvalue,
+                           const char *section_name);
+
 extern gcc_jit_lvalue *
 gcc_jit_function_new_local (gcc_jit_function *func,
                            gcc_jit_location *loc,
index 98d693fd00ce86205e05d2ad725fa12e5d002a00..b17671163c7ad946606b42f131de54e94df227b8 100644 (file)
@@ -231,3 +231,8 @@ LIBGCCJIT_ABI_17 {
   global:
     gcc_jit_lvalue_set_tls_model;
 } LIBGCCJIT_ABI_16;
+
+LIBGCCJIT_ABI_18 {
+  global:
+    gcc_jit_lvalue_set_link_section;
+} LIBGCCJIT_ABI_17;
index 350a30b3328270d586ff00a2478bd8c241130fe0..3e8ccbca60ea3e9219aec5644b6fea806667bf00 100644 (file)
 #undef create_code
 #undef verify_code
 
+/* test-link-section-assembler.c: This can't be in the testcases array as it
+   doesn't have a verify_code implementation.  */
+
 /* test-linked-list.c */
 #define create_code create_code_linked_list
 #define verify_code verify_code_linked_list
index 10b98bdc74b8506369ec6300248007cb8cfecf47..3568dbb9d6334aed3c49d750d335e1034d899c72 100644 (file)
@@ -864,6 +864,39 @@ proc jit-verify-assembler { args } {
     jit-run-executable ${executable_from_asm} ${dg-output-text}
 }
 
+# Assuming that a .s file has been written out named
+# OUTPUT_FILENAME, check that the argument matches the
+# output file.
+# For use by the test-link-section-assembler.c testcase.
+proc jit-verify-assembler-output { args } {
+    verbose "jit-verify-assembler: $args"
+
+    set dg-output-text [lindex $args 0]
+    verbose "dg-output-text: ${dg-output-text}"
+
+    upvar 2 name name
+    verbose "name: $name"
+
+    upvar 2 prog prog
+    verbose "prog: $prog"
+    set asm_filename [jit-get-output-filename $prog]
+    verbose "  asm_filename: ${asm_filename}"
+
+    # Read the assembly file.
+    set f [open $asm_filename r]
+    set content [read $f]
+    close $f
+
+    # Verify that the assembly matches the regex.
+    if { ![regexp ${dg-output-text} $content] } {
+       fail "${asm_filename} output pattern test, is ${content}, should match ${dg-output-text}"
+       verbose "Failed test for output pattern ${dg-output-text}" 3
+    } else {
+       pass "${asm_filename} output pattern test, ${dg-output-text}"
+       verbose "Passed test for output pattern ${dg-output-text}" 3
+    }
+
+}
 # Assuming that a .o file has been written out named
 # OUTPUT_FILENAME, invoke the driver to try to turn it into
 # an executable, and try to run the result.
diff --git a/gcc/testsuite/jit.dg/test-link-section-assembler.c b/gcc/testsuite/jit.dg/test-link-section-assembler.c
new file mode 100644 (file)
index 0000000..a90b00e
--- /dev/null
@@ -0,0 +1,37 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "libgccjit.h"
+
+#define TEST_COMPILING_TO_FILE
+#define OUTPUT_KIND      GCC_JIT_OUTPUT_KIND_ASSEMBLER
+#define OUTPUT_FILENAME  "output-of-test-link-section-assembler.c.s"
+#include "harness.h"
+
+void
+create_code (gcc_jit_context *ctxt, void *user_data)
+{
+  /* Let's try to inject the equivalent of:
+     int foo __attribute__((section(".section")));
+  */
+  gcc_jit_type *int_type =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
+  gcc_jit_lvalue *foo =
+    gcc_jit_context_new_global (
+      ctxt, NULL, GCC_JIT_GLOBAL_EXPORTED, int_type, "foo");
+  gcc_jit_lvalue_set_link_section(foo, ".my_section");
+
+  gcc_jit_function *func_main =
+    gcc_jit_context_new_function (ctxt, NULL,
+                                 GCC_JIT_FUNCTION_EXPORTED,
+                                 int_type,
+                                 "main",
+                                 0, NULL,
+                                 0);
+  gcc_jit_rvalue *zero = gcc_jit_context_zero (ctxt, int_type);
+  gcc_jit_block *block = gcc_jit_function_new_block (func_main, NULL);
+  gcc_jit_block_end_with_return (block, NULL, zero);
+}
+
+/* { dg-final { jit-verify-output-file-was-created "" } } */
+/* { dg-final { jit-verify-assembler-output ".section  .my_section" } } */