libgccjit: Implement sizeof operator

Message ID 8d25e331b0c28ca9b632bf3fb3a239661592af78.camel@zoho.com
State Accepted
Delegated to: David Malcolm
Headers
Series libgccjit: Implement sizeof operator |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gcc_check--master-aarch64 success Testing passed

Commit Message

Antoni Boucher Dec. 22, 2023, 3:25 p.m. UTC
  Hi.
This patch adds the support of the sizeof operator.
I was wondering if this new API entrypoint should take a location as a
parameter. What do you think?
Thanks for the review.
  

Comments

David Malcolm Jan. 9, 2024, 4:33 p.m. UTC | #1
On Fri, 2023-12-22 at 10:25 -0500, Antoni Boucher wrote:
> Hi.
> This patch adds the support of the sizeof operator.
> I was wondering if this new API entrypoint should take a location as
> a
> parameter. What do you think?

I'd prefer it if it did (even if it's currently ignored internally),
but it's not a big deal.

> Thanks for the review.

The patch is OK as-is.

Thanks
Dave
  
Antoni Boucher Jan. 10, 2024, 10:38 p.m. UTC | #2
On Tue, 2024-01-09 at 11:33 -0500, David Malcolm wrote:
> On Fri, 2023-12-22 at 10:25 -0500, Antoni Boucher wrote:
> > Hi.
> > This patch adds the support of the sizeof operator.
> > I was wondering if this new API entrypoint should take a location
> > as
> > a
> > parameter. What do you think?
> 
> I'd prefer it if it did (even if it's currently ignored internally),
> but it's not a big deal.

The reason it doesn't have a location is because it directly produces a
constant, rather like gcc_jit_context_new_rvalue_from_int, which
doesn't have a location either.
So, I wanted to confirm that you think it's needed to have a location.

Also, I realized I misnamed memento_of_new_sizeof and will rename it to
memento_of_sizeof.

> 
> > Thanks for the review.
> 
> The patch is OK as-is.
> 
> Thanks
> Dave
>
  
David Malcolm Jan. 10, 2024, 11:36 p.m. UTC | #3
On Wed, 2024-01-10 at 17:38 -0500, Antoni Boucher wrote:
> On Tue, 2024-01-09 at 11:33 -0500, David Malcolm wrote:
> > On Fri, 2023-12-22 at 10:25 -0500, Antoni Boucher wrote:
> > > Hi.
> > > This patch adds the support of the sizeof operator.
> > > I was wondering if this new API entrypoint should take a location
> > > as
> > > a
> > > parameter. What do you think?
> > 
> > I'd prefer it if it did (even if it's currently ignored
> > internally),
> > but it's not a big deal.
> 
> The reason it doesn't have a location is because it directly produces
> a
> constant, rather like gcc_jit_context_new_rvalue_from_int, which
> doesn't have a location either.
> So, I wanted to confirm that you think it's needed to have a
> location.

Fair enough; it's not needed.

> 
> Also, I realized I misnamed memento_of_new_sizeof and will rename it
> to
> memento_of_sizeof.

Ah, ok.  Fixing that is preapproved, though make sure it still builds
and test it a little before pushing!

Please also doublecheck the ABI version numbers, since they inevitably
tend to get out of sync on branches.

Dave

> 
> > 
> > > Thanks for the review.
> > 
> > The patch is OK as-is.
> > 
> > Thanks
> > Dave
> > 
>
  

Patch

From e86e00efae450f04bc92ae6e4e61cf92c38d9b7d Mon Sep 17 00:00:00 2001
From: Antoni Boucher <bouanto@zoho.com>
Date: Tue, 19 Sep 2023 22:10:47 -0400
Subject: [PATCH] libgccjit: Implement sizeof operator

gcc/jit/ChangeLog:

	* docs/topics/compatibility.rst (LIBGCCJIT_ABI_26): New ABI tag.
	* docs/topics/expressions.rst: Document gcc_jit_context_new_sizeof.
	* jit-playback.cc (new_sizeof): New method.
	* jit-playback.h (new_sizeof): New method.
	* jit-recording.cc (recording::context::new_sizeof,
	recording::memento_of_new_sizeof::replay_into,
	recording::memento_of_new_sizeof::make_debug_string,
	recording::memento_of_new_sizeof::write_reproducer): New methods.
	* jit-recording.h (class memento_of_new_sizeof): New class.
	* libgccjit.cc (gcc_jit_context_new_sizeof): New function.
	* libgccjit.h (gcc_jit_context_new_sizeof): New function.
	* libgccjit.map: New function.

gcc/testsuite/ChangeLog:

	* jit.dg/all-non-failing-tests.h: New test.
	* jit.dg/test-sizeof.c: New test.
---
 gcc/jit/docs/topics/compatibility.rst        |  7 +++
 gcc/jit/docs/topics/expressions.rst          | 14 ++++++
 gcc/jit/jit-playback.cc                      | 10 ++++
 gcc/jit/jit-playback.h                       |  3 ++
 gcc/jit/jit-recording.cc                     | 52 ++++++++++++++++++++
 gcc/jit/jit-recording.h                      | 28 +++++++++++
 gcc/jit/libgccjit.cc                         | 18 +++++++
 gcc/jit/libgccjit.h                          | 12 +++++
 gcc/jit/libgccjit.map                        |  5 ++
 gcc/testsuite/jit.dg/all-non-failing-tests.h | 10 ++++
 gcc/testsuite/jit.dg/test-sizeof.c           | 50 +++++++++++++++++++
 11 files changed, 209 insertions(+)
 create mode 100644 gcc/testsuite/jit.dg/test-sizeof.c

diff --git a/gcc/jit/docs/topics/compatibility.rst b/gcc/jit/docs/topics/compatibility.rst
index ebede440ee4..00cf88a4666 100644
--- a/gcc/jit/docs/topics/compatibility.rst
+++ b/gcc/jit/docs/topics/compatibility.rst
@@ -378,3 +378,10 @@  alignment of a variable:
 --------------------
 ``LIBGCCJIT_ABI_25`` covers the addition of
 :func:`gcc_jit_type_get_restrict`
+
+.. _LIBGCCJIT_ABI_26:
+
+``LIBGCCJIT_ABI_26``
+--------------------
+``LIBGCCJIT_ABI_26`` covers the addition of
+:func:`gcc_jit_context_new_sizeof`
diff --git a/gcc/jit/docs/topics/expressions.rst b/gcc/jit/docs/topics/expressions.rst
index 42cfee36302..a18909f4890 100644
--- a/gcc/jit/docs/topics/expressions.rst
+++ b/gcc/jit/docs/topics/expressions.rst
@@ -126,6 +126,20 @@  Simple expressions
    underlying string, so it is valid to pass in a pointer to an on-stack
    buffer.
 
+.. function:: gcc_jit_rvalue *\
+              gcc_jit_context_new_sizeof (gcc_jit_context *ctxt, \
+                                          gcc_jit_type *type)
+
+   Generate an rvalue that is equal to the size of ``type``.
+
+   The parameter ``type`` must be non-NULL.
+
+   This is equivalent to this C code:
+
+   .. code-block:: c
+
+     sizeof (type)
+
 Constructor expressions
 ***********************
 
diff --git a/gcc/jit/jit-playback.cc b/gcc/jit/jit-playback.cc
index dddd537f3b1..09b5e89942f 100644
--- a/gcc/jit/jit-playback.cc
+++ b/gcc/jit/jit-playback.cc
@@ -974,6 +974,16 @@  new_rvalue_from_const <void *> (type *type,
 
 /* Construct a playback::rvalue instance (wrapping a tree).  */
 
+playback::rvalue *
+playback::context::
+new_sizeof (type *type)
+{
+  tree inner = TYPE_SIZE_UNIT (type->as_tree ());
+  return new rvalue (this, inner);
+}
+
+/* Construct a playback::rvalue instance (wrapping a tree).  */
+
 playback::rvalue *
 playback::context::
 new_string_literal (const char *value)
diff --git a/gcc/jit/jit-playback.h b/gcc/jit/jit-playback.h
index b0166f8f6ce..a537a5433c3 100644
--- a/gcc/jit/jit-playback.h
+++ b/gcc/jit/jit-playback.h
@@ -139,6 +139,9 @@  public:
   new_rvalue_from_const (type *type,
 			 HOST_TYPE value);
 
+  rvalue *
+  new_sizeof (type *type);
+
   rvalue *
   new_string_literal (const char *value);
 
diff --git a/gcc/jit/jit-recording.cc b/gcc/jit/jit-recording.cc
index 9b5b8005ebe..c1e4e68cae6 100644
--- a/gcc/jit/jit-recording.cc
+++ b/gcc/jit/jit-recording.cc
@@ -1076,6 +1076,21 @@  recording::context::new_global_init_rvalue (lvalue *variable,
   gbl->set_rvalue_init (init); /* Needed by the global for write dump.  */
 }
 
+/* Create a recording::memento_of_sizeof instance and add it
+   to this context's list of mementos.
+
+   Implements the post-error-checking part of
+   gcc_jit_context_new_sizeof.  */
+
+recording::rvalue *
+recording::context::new_sizeof (recording::type *type)
+{
+  recording::rvalue *result =
+    new memento_of_new_sizeof (this, NULL, type);
+  record (result);
+  return result;
+}
+
 /* Create a recording::memento_of_new_string_literal instance and add it
    to this context's list of mementos.
 
@@ -5310,6 +5325,43 @@  memento_of_new_rvalue_from_const <void *>::write_reproducer (reproducer &r)
 
 } // namespace recording
 
+/* The implementation of class gcc::jit::recording::memento_of_new_sizeof.  */
+
+/* Implementation of pure virtual hook recording::memento::replay_into
+   for recording::memento_of_new_sizeof.  */
+
+void
+recording::memento_of_new_sizeof::replay_into (replayer *r)
+{
+  set_playback_obj (r->new_sizeof (m_type->playback_type ()));
+}
+
+/* Implementation of recording::memento::make_debug_string for
+   sizeof expressions.  */
+
+recording::string *
+recording::memento_of_new_sizeof::make_debug_string ()
+{
+  return string::from_printf (m_ctxt,
+			      "sizeof (%s)",
+			      m_type->get_debug_string ());
+}
+
+/* Implementation of recording::memento::write_reproducer for sizeof
+   expressions. */
+
+void
+recording::memento_of_new_sizeof::write_reproducer (reproducer &r)
+{
+  const char *id = r.make_identifier (this, "rvalue");
+  r.write ("  gcc_jit_rvalue *%s =\n"
+    "    gcc_jit_context_new_sizeof (%s, /* gcc_jit_context *ctxt */\n"
+    "                                %s); /* gcc_jit_type *type */\n",
+    id,
+    r.get_identifier (get_context ()),
+    r.get_identifier (m_type));
+}
+
 /* The implementation of class gcc::jit::recording::memento_of_new_string_literal.  */
 
 /* Implementation of pure virtual hook recording::memento::replay_into
diff --git a/gcc/jit/jit-recording.h b/gcc/jit/jit-recording.h
index 4a8082991fb..c35ca8afbe6 100644
--- a/gcc/jit/jit-recording.h
+++ b/gcc/jit/jit-recording.h
@@ -165,6 +165,9 @@  public:
   new_rvalue_from_const (type *type,
 			 HOST_TYPE value);
 
+  rvalue *
+  new_sizeof (type *type);
+
   rvalue *
   new_string_literal (const char *value);
 
@@ -1575,6 +1578,31 @@  private:
   HOST_TYPE m_value;
 };
 
+class memento_of_new_sizeof : public rvalue
+{
+public:
+  memento_of_new_sizeof (context *ctxt,
+			 location *loc,
+			 type *type)
+  : rvalue (ctxt, loc, ctxt->get_type (GCC_JIT_TYPE_INT)),
+    m_type (type) {}
+
+  void replay_into (replayer *r) final override;
+
+  void visit_children (rvalue_visitor *) final override {}
+
+private:
+  string * make_debug_string () final override;
+  void write_reproducer (reproducer &r) final override;
+  enum precedence get_precedence () const final override
+  {
+    return PRECEDENCE_PRIMARY;
+  }
+
+private:
+  type *m_type;
+};
+
 class memento_of_new_string_literal : public rvalue
 {
 public:
diff --git a/gcc/jit/libgccjit.cc b/gcc/jit/libgccjit.cc
index 0451b4df7f9..52230e94de5 100644
--- a/gcc/jit/libgccjit.cc
+++ b/gcc/jit/libgccjit.cc
@@ -2073,6 +2073,24 @@  gcc_jit_context_null (gcc_jit_context *ctxt,
   return gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
 }
 
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::context::new_sizeof method in
+   jit-recording.cc.  */
+
+gcc_jit_rvalue *
+gcc_jit_context_new_sizeof (gcc_jit_context *ctxt,
+			    gcc_jit_type *type)
+{
+  RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
+  RETURN_NULL_IF_FAIL (type, ctxt, NULL, "NULL type");
+  JIT_LOG_FUNC (ctxt->get_logger ());
+
+  return ((gcc_jit_rvalue *)ctxt
+	  ->new_sizeof (type));
+}
+
 /* Public entrypoint.  See description in libgccjit.h.
 
    After error-checking, the real work is done by the
diff --git a/gcc/jit/libgccjit.h b/gcc/jit/libgccjit.h
index 749f6c24177..02f3c594335 100644
--- a/gcc/jit/libgccjit.h
+++ b/gcc/jit/libgccjit.h
@@ -1093,6 +1093,18 @@  extern gcc_jit_rvalue *
 gcc_jit_context_null (gcc_jit_context *ctxt,
 		      gcc_jit_type *pointer_type);
 
+#define LIBGCCJIT_HAVE_gcc_jit_context_new_sizeof
+
+/* Generates an rvalue that is equal to the size of type.
+
+   This API entrypoint was added in LIBGCCJIT_ABI_26; you can test for its
+   presence using
+     #ifdef LIBGCCJIT_HAVE_gcc_jit_context_new_sizeof  */
+
+extern gcc_jit_rvalue *
+gcc_jit_context_new_sizeof (gcc_jit_context *ctxt,
+			    gcc_jit_type *type);
+
 /* String literals. */
 extern gcc_jit_rvalue *
 gcc_jit_context_new_string_literal (gcc_jit_context *ctxt,
diff --git a/gcc/jit/libgccjit.map b/gcc/jit/libgccjit.map
index 8b90a0e2ff3..f946250c5b6 100644
--- a/gcc/jit/libgccjit.map
+++ b/gcc/jit/libgccjit.map
@@ -276,3 +276,8 @@  LIBGCCJIT_ABI_25 {
   global:
     gcc_jit_type_get_restrict;
 } LIBGCCJIT_ABI_24;
+
+LIBGCCJIT_ABI_26 {
+  global:
+    gcc_jit_context_new_sizeof;
+} LIBGCCJIT_ABI_25;
diff --git a/gcc/testsuite/jit.dg/all-non-failing-tests.h b/gcc/testsuite/jit.dg/all-non-failing-tests.h
index e762563f9bd..079ce20b85b 100644
--- a/gcc/testsuite/jit.dg/all-non-failing-tests.h
+++ b/gcc/testsuite/jit.dg/all-non-failing-tests.h
@@ -322,6 +322,13 @@ 
 /* test-setting-alignment.c: This can't be in the testcases array as it
    is target-specific.  */
 
+/* test-sizeof.c */
+#define create_code create_code_sizeof
+#define verify_code verify_code_sizeof
+#include "test-sizeof.c"
+#undef create_code
+#undef verify_code
+
 /* test-string-literal.c */
 #define create_code create_code_string_literal
 #define verify_code verify_code_string_literal
@@ -506,6 +513,9 @@  const struct testcase testcases[] = {
   {"reflection",
    create_code_reflection ,
    verify_code_reflection },
+  {"sizeof",
+   create_code_sizeof,
+   verify_code_sizeof},
   {"string_literal",
    create_code_string_literal,
    verify_code_string_literal},
diff --git a/gcc/testsuite/jit.dg/test-sizeof.c b/gcc/testsuite/jit.dg/test-sizeof.c
new file mode 100644
index 00000000000..6aef9027b4f
--- /dev/null
+++ b/gcc/testsuite/jit.dg/test-sizeof.c
@@ -0,0 +1,50 @@ 
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+
+#include "libgccjit.h"
+
+#include "harness.h"
+
+void
+create_code (gcc_jit_context *ctxt, void *user_data)
+{
+  /* Let's try to inject the equivalent of:
+int
+my_sizeof ()
+{
+   return sizeof(int32_t);
+}
+   */
+  gcc_jit_type *int32 =
+    gcc_jit_context_get_int_type (ctxt, 4, 1);
+  gcc_jit_type *int_type =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
+
+  gcc_jit_function *func =
+    gcc_jit_context_new_function (ctxt,
+				  NULL,
+				  GCC_JIT_FUNCTION_EXPORTED,
+				  int_type,
+				  "my_sizeof",
+				  0, NULL, 0);
+
+  gcc_jit_block *initial =
+    gcc_jit_function_new_block (func, "initial");
+
+  gcc_jit_block_end_with_return(initial, NULL,
+    gcc_jit_context_new_sizeof(ctxt, int32));
+}
+
+void
+verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
+{
+  typedef int (*my_sizeof_type) ();
+  CHECK_NON_NULL (result);
+  my_sizeof_type my_sizeof =
+    (my_sizeof_type)gcc_jit_result_get_code (result, "my_sizeof");
+  CHECK_NON_NULL (my_sizeof);
+  int val = my_sizeof ();
+  CHECK_VALUE (val, 4);
+}
-- 
2.43.0