From patchwork Fri Dec 22 15:25:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antoni Boucher X-Patchwork-Id: 82764 X-Patchwork-Delegate: dmalcolm@redhat.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5A71D38618F9 for ; Fri, 22 Dec 2023 15:26:06 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from sender4-pp-o90.zoho.com (sender4-pp-o90.zoho.com [136.143.188.90]) by sourceware.org (Postfix) with ESMTPS id 62D423858D33; Fri, 22 Dec 2023 15:25:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 62D423858D33 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=zoho.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=zoho.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 62D423858D33 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=136.143.188.90 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1703258752; cv=pass; b=GgV8fYkyM2ttv3a2Fq3yBygLqoSk6NKBNKwgZ9nMKuid0lh4aBIaCAnCBn0Z0UlUifu3CTzOVwJU0A4v/lJyLwo1/CiwaDSPAbcl0zcR7H/cIuXBNPYbYC7WhkocmXGLocN3hRWvxWFEQ/NqStGcW9BoLcCJUAuJdrLpOo1Fraw= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1703258752; c=relaxed/simple; bh=D8PLDh8VIY1nyNiAZhHblsRnZpLP4tyCsRFBROPineQ=; h=DKIM-Signature:Message-ID:Subject:From:To:Date:MIME-Version; b=dVg5sajHuqj97+vQWn3hw+nyWYiaQzlYQY3bIJP6cpmD/npFTahh1Tkmu/ic7ShfosLrNPQVqY7Rvmvd9QLV+D1rShfJ62pl0cBFHvxdVcikyAZgl/7IQEKfLDg43rX3p5J5owopGoM2ZW04SYME8AFaPotXZjEBiIFJRrd97s8= ARC-Authentication-Results: i=2; server2.sourceware.org ARC-Seal: i=1; a=rsa-sha256; t=1703258744; cv=none; d=zohomail.com; s=zohoarc; b=Q8Z4T/PBDY2K79T9KlVRYXWlP4g695p3Qbx15SpMEIbDaZEiDRM6HMD0A+OG6vHucMgTMyNmvVjrl87Mpl80kuRHgd9unksF5talSn6NGrBmdWw7SRUDls1IKYstG23AF0/ZVWrIFhN0TtEaazGms8SdEWfmKk2ZsP9QOB4VII8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1703258744; h=Content-Type:Cc:Cc:Date:Date:From:From:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=/Qsqov/L51hTe+r8sMZAY2oyT20JPFSTvhmVqzClJfk=; b=IZRzBv3+59s9xt/a2aqHZOueipu9VCUv+DM5xyr2oqNfI9zKsV+vS6K12o8I02NiW3F/gV5m3AoCyciKdi2tVkn6i4NEanh5f+teE5iWGcpaV3vMFFEfeVHwQri+IgdIsI7tebTOhqyT2hXmAIYQ+sivJVCjxzyyKv/CsDIqPOA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=zoho.com; spf=pass smtp.mailfrom=bouanto@zoho.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1703258744; s=zm2022; d=zoho.com; i=bouanto@zoho.com; h=Message-ID:Subject:Subject:From:From:To:To:Cc:Cc:Date:Date:Content-Type:MIME-Version:Feedback-ID:Message-Id:Reply-To; bh=/Qsqov/L51hTe+r8sMZAY2oyT20JPFSTvhmVqzClJfk=; b=fda7hZw0xk/YslT8jKWMtgkevD/6Yw9zQeNjZlpAfKR427GK+4Rg8pE+pHBVDT9d yATgsKXT/qty2UDv+llDxQFqLvaBFBVx7+386e8j7wkQlnvrnz8b2KidLNI4kcYFa8g UOmpb2I2TwpZmYEnfwBWcyrmuYsYGElsBs3guaNM= Received: from [192.168.1.172] (38.87.11.6 [38.87.11.6]) by mx.zohomail.com with SMTPS id 1703258743164194.48992049355718; Fri, 22 Dec 2023 07:25:43 -0800 (PST) Message-ID: <8d25e331b0c28ca9b632bf3fb3a239661592af78.camel@zoho.com> Subject: [PATCH] libgccjit: Implement sizeof operator From: Antoni Boucher To: "jit@gcc.gnu.org" , "gcc-patches@gcc.gnu.org" Cc: David Malcolm Date: Fri, 22 Dec 2023 10:25:41 -0500 Autocrypt: addr=bouanto@zoho.com; prefer-encrypt=mutual; keydata=mQENBFOSMLQBCADO5aw6Ys8thMQUNzrwAnfJX2wbgWiz0pQ01DjYj22eeIpChkoZn6LWdt4dieq30u2rFi/yQzJ02foHwI2+aL9rU6xz/x4TwqyRJQGMOqklNc3R+pdXmH4WDQkQDWmLxvc07vu+zb8Tx5A6pMDh4J2ncCEhLEUcH39Yq/yg4eBnFwUX6N7kakvHrnScGNqhnSFCacoJeMJUAR+1G7VBSBd++jmnHLnx3mj7QkRZVECJUw2zqiv1yReCC6GU4SvqLjdqm5ZGeoWOqD/NHjBRoEeOVjzp6M/qOjjWRbkJVqmvgfcD8UytSSqqboR35YFT4L+rZt2ri3T12MJb3i5syCAXABEBAAG0IUFudG9uaSBCb3VjaGVyIDxib3VhbnRvQHpvaG8uY29tPokBVgQTAQgAQAIbIwcLCQgHAwIBBhUIAgkKCwQWAgMBAh4BAheAFiEEOELC4Uw1Jeb66YE6RVeGAwR4zcEFAlz4QM4FCRLMEZoACgkQRVeGAwR4zcFBQQf/afttJrA/puADQZhrDfkgr0MFvq6iB+GCy1b8BkXimk1TOXTPt87YLehSeijNu3JkYhl5eRc87BNfU9J87KfI/KIy6hZxqlDXk16FhW9bw/7wYEA0hpb3MUn7xLElXDT0ZHaD+KTe8Oun7qfzgx5RlL6r/WODf3CkSpO085R/rfeB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a804454ab9c4d8b4c30c184481198202367f5e16ed90774fab1d:zu08011226900ee0d917139cf97a0e3bd8000014fe30d5631d868aaf6156eeab59a163d63b270a5a9037f8:rf080112323455e8059663c73e0f3568e300005e4c1e9571a33777de57328467552b9b7afc384fa8ec965f9305db2b1436139fdee8df85:ZohoMail X-ZohoMailClient: External X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org 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. From e86e00efae450f04bc92ae6e4e61cf92c38d9b7d Mon Sep 17 00:00:00 2001 From: Antoni Boucher 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 (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 ::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 +#include +#include +#include + +#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