From patchwork Fri Jan 19 21:54:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antoni Boucher X-Patchwork-Id: 189681 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp1301850dyb; Fri, 19 Jan 2024 13:55:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IG6SOhhRd4QbeN8Wz7fkOYha+Gtf5K5WQHF1jGHzQCuq5eHckjkk/ITyD2A/M4v3SwV7wnw X-Received: by 2002:a0c:cd12:0:b0:681:7b9b:5d0d with SMTP id b18-20020a0ccd12000000b006817b9b5d0dmr515362qvm.20.1705701349486; Fri, 19 Jan 2024 13:55:49 -0800 (PST) ARC-Seal: i=3; a=rsa-sha256; t=1705701349; cv=pass; d=google.com; s=arc-20160816; b=wuOx4rsXUsHkN7FKgu0zOjdyZsJNjZwz3XNmpxs95HV+iSB4IUsnN2suJz4/j2d8Ls /QFY3Rxv1bR5j0nE7azs9u47DNhldhkHd9txMJUekv+XOMnUC3kn+JVcsmc56JO4HvG+ qcLM/uGB6pmDvArQqfNfpvZ9SElaWQdhJa9G9agqhoGIX4C96RpTVBZ6W1JUyqBW1M6o TcN5mXjwGbK7TbSxDyC2qyeXUaNYFGZCggBZep5sp8gGoGG6D+VfwjHMeUy47FSt6I79 CEe/Q842btB+lCU1p8I+cBFIWQeK5rXx4Xa5fWUuOHv7b7vNJ7THqSSSNyllp9yJpk6Y q10w== ARC-Message-Signature: i=3; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:feedback-id:mime-version :user-agent:autocrypt:date:to:from:subject:message-id:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=QWbsjVkAtiEyLeBxOZsjBp+3tz0vvp3cEQ1DbXrvAnc=; fh=4J/FPGKEux+uOF9lQ+T6ENKipdanaZJQWCnDvTR8SOw=; b=Gj7IIi7X93NstsgdJQTF3pvNngb9bhYJmo4bkvKTSW+z+zHjMapb3Cn6IoWp8fHqEo b1H8qhM/Rh+pJuS8AVe+KZKKexUOacLqFpPdxFeWguJrQgycezzMkmRLuSz1Bwg1ou08 zF9cCrDd6YQpHxGHqMJJyUzHQARnTdOQW6SY4GMml+2PH0VyPCkujG2717LBfs0RBszl Cwk6NdQ3+cNvXgmiwlTSSO0h2cvYSLnvXoZifZAgWoQ0TeH32pHrCOyaUQumq16n8w9X h6ABgE165RX6u8Tocz+c5igk0jzvI3sm9jz7qwUoO2lwKsGaK8VvyEoh4e6boxhpOpaz QMNQ== ARC-Authentication-Results: i=3; mx.google.com; dkim=pass header.i=@zoho.com header.s=zm2022 header.b=e7PZeFNt; arc=pass (i=2); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=zoho.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id w17-20020a0cdf91000000b0067f99bbc781si179438qvl.109.2024.01.19.13.55.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 13:55:49 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@zoho.com header.s=zm2022 header.b=e7PZeFNt; arc=pass (i=2); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=zoho.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2A25F3858281 for ; Fri, 19 Jan 2024 21:55:49 +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 C9D433858409; Fri, 19 Jan 2024 21:54:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C9D433858409 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 C9D433858409 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=1705701297; cv=pass; b=J2wW6Woafxi0tzdfavijsKsY3Ke2lPkRO9Z2Tu/3d4wx4PHQbyVY2Q014Ro6zRlxNP+3iTYTYK9fYph2MXcqt4Jo6/mUQrA3OldcBG+8W2HKbEs35BmxctXi88I/VPrd8abk8WJpQOor782RhtzYzm/orXvakJb2PArVfmj1Yk4= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1705701297; c=relaxed/simple; bh=bf5OdzxT4/Eersw42NRHcxHe2nG2bo139TGOUhs6v8k=; h=DKIM-Signature:Message-ID:Subject:From:To:Date:MIME-Version; b=eB51a5QuIQktYSiobJy4YoiWm/sQzXdF6VghhN1nTYTqSB4bqGunsbhJLjKL9IIYDJGYz/6cRWEOyw7eJax3c6h/4Wt2BHTL58XoW9JMXMEqN/0h0btRHh+Wtcyziw4BGj06YTAFtThiaE7t/0exfA1Nx37Pjbqw3HRbu2mIqDQ= ARC-Authentication-Results: i=2; server2.sourceware.org ARC-Seal: i=1; a=rsa-sha256; t=1705701292; cv=none; d=zohomail.com; s=zohoarc; b=ZPAxrXXbPYXvDhFunlQsw70MpiO46Y1ax4I5OXbWTyCevpCqt9Cp+L6rGjZBCCAbUqTajeFpEKMZFd9D4nZ5M/5GbeGTBViqibSSWRfIBZKPxaPdM1P8HR/hc6nIElSfudVtZgL3nQyHE6gITh2eOkfwhsBqMyqD3M/JxbIKVek= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1705701292; h=Content-Type:Date:Date:From:From:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To:Cc; bh=QWbsjVkAtiEyLeBxOZsjBp+3tz0vvp3cEQ1DbXrvAnc=; b=UIFgvO+h/ql9UBrorC1NVoetFI2xB1gcQ8NGOk5OmmQtih3gti595FE9m9JZDYMYWFOwkouY/LNJz7Z2Nuyu2cuXb6HiteAYHu4vyDKx/EAi4nIW+9DtGIZmkKOrwp7oP4zjCHgAzy0pT7Wa3PG7PDnhNDnrYsh9S32G9Ui20VA= 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=1705701292; s=zm2022; d=zoho.com; i=bouanto@zoho.com; h=Message-ID:Subject:Subject:From:From:To:To:Date:Date:Content-Type:MIME-Version:Feedback-ID:Message-Id:Reply-To:Cc; bh=QWbsjVkAtiEyLeBxOZsjBp+3tz0vvp3cEQ1DbXrvAnc=; b=e7PZeFNtHE2Cx/3O+BaE8kHmTtHPKA1NzEWY3UJROdjb4cbucAcBpoPwWW0FWX33 WHgu5CIJzDGW8OoBZBKGU6X8BvEYzwwtfy7O3dqmboQ4MKSz+6UJxgyBWFjyaSAd/JO /VIlceRIson+XVHoaB7J9C0c0iY15L4ADuUo4zX0= Received: from [192.168.1.172] (38.87.11.6 [38.87.11.6]) by mx.zohomail.com with SMTPS id 1705701289859645.8227817590432; Fri, 19 Jan 2024 13:54:49 -0800 (PST) Message-ID: <2148e8aee390d0f768ecd10fab0928d86783622a.camel@zoho.com> Subject: [PATCH] libgccjit: Add support for creating temporary variables From: Antoni Boucher To: "gcc-patches@gcc.gnu.org" , "jit@gcc.gnu.org" Date: Fri, 19 Jan 2024 16:54:48 -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/rfeBqDEx9mVlhDWgq6Az3CZoD+3CqiCKVqmDuHTWz4kwrd9AM5eVcLvvLKnZIdoIp+G5Ao6BvaGlZyfenN1iOSjLy2NXNt4MnUt0lUYEP5KSIIRhHQ8xkUbj7eWUmaahkxhNb3fH3sAPwGnRZrPpb4rgYzNmSk63wWMh9M2xk+rLb7kBDQRTkjC0AQgAumZzsAV/UFWI+dpzebQfma36kKYZZFuseant5sq/HWP553XQ/U6ttJiKyN5MpCqtxvCAoRplf42YhlHuFqgf73WJxoJ6Y+sdyqoBSwlR+ gzAneAmsa8gmmY0wawH0Z2leazjKuS7mJjVEQZg0ZGsiCVRGeRnDqFGzDEzDc9ngWKSoTq0fKzlGy1X85OrtmUrvEbhSo6HP+FoeunHkIqrxu3w3vDoFEXxVQlKI6V3I4nCz5n6DB8WR3L7nsiiTnOiGirPw1ngvWFLW86kkA4FJpayc8Xl3va3SLY+2y4yuROboX2DVI4AC/Qeug/mDiBicPxkP6YfUartQRMe6obkEQARAQABiQE8BBgBCAAmAhsMFiEEOELC4Uw1Jeb66YE6RVeGAwR4zcEFAlz4QRsFCRLMEecACgkQRVeGAwR4zcE56ggAgTgrJInBKC+7552Dpccuo6Clh3wZfjlNLv9/6r5lKEbaNzaTrfhPiAP4WgnluIUmj8amOFLFJpj+BAVNOXpZ4D2R3o9ch8z7fot+fW4Yw+PKIxH4I2xEys8ndoEB3aiQwHjKcGIhkIU7uyMJFQr2aWjdTY0gmXw0YZueHOSLgo7uX4XKxB8fEO/yto/Tff2YBAAq+AtNwt+Gh5YS9rZw7rwUTWMi84yVOlc+zRE79E9NJkvdTwX7IJYo64VzIRNfgHsn7QNdVzuM1XIFHl+Glk6cIlI8s6BO7nEoCn3hTF104fQTAO3fEs+XXZOKXo2lk8faowEoPq5r58StrV0nyg== User-Agent: Evolution 3.50.3 MIME-Version: 1.0 X-Zoho-Virus-Status: 1 X-Zoho-AV-Stamp: zmail-av-1.1.0/204.902.92 Feedback-ID: rr08011228f5c11221917e9ea5188dcab200008efdd9432056266a16dcedc422c7dd01210fb50cf5d08504d873:zu08011226679b66f3ebb98a250b31b90900008c521ed2b07478162c3fefc2e46f6a3318a7bbd006395868:rf080112321761841ca45a4a8bcd0d72e0000018078252707fd1bb6be9e6ffd1a9eae7ac6368d9d96465f4cf436356b849dec7d2b4e82d:ZohoMail X-ZohoMailClient: External X-Spam-Status: No, score=-11.6 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788557498175067604 X-GMAIL-MSGID: 1788557498175067604 Hi. This patch adds a new way to create local variable that won't generate debug info: it is to be used for compiler-generated variables. Thanks for the review. From 6f69e9db77f3c7e019fae74414ba5eed15298514 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Thu, 18 Jan 2024 16:54:59 -0500 Subject: [PATCH] libgccjit: Add support for creating temporary variables gcc/jit/ChangeLog: * docs/topics/compatibility.rst (LIBGCCJIT_ABI_26): New ABI tag. * docs/topics/functions.rst: Document gcc_jit_function_new_temp. * jit-playback.cc (new_local): Add new is_temp parameter. * jit-playback.h: Add new is_temp parameter. * jit-recording.cc (recording::function::new_temp): New method. (recording::local::replay_into): Support temporary variables. (recording::local::write_reproducer): Support temporary variables. * jit-recording.h (new_temp): New method. (m_is_temp): New field. * libgccjit.cc (gcc_jit_function_new_temp): New function. * libgccjit.h (gcc_jit_function_new_temp): New function. * libgccjit.map: New function. gcc/testsuite/ChangeLog: * jit.dg/all-non-failing-tests.h: Mention test-temp.c. * jit.dg/test-temp.c: New test. --- gcc/jit/docs/topics/compatibility.rst | 9 ++++ gcc/jit/docs/topics/functions.rst | 20 +++++++ gcc/jit/jit-playback.cc | 21 ++++++-- gcc/jit/jit-playback.h | 3 +- gcc/jit/jit-recording.cc | 52 +++++++++++++----- gcc/jit/jit-recording.h | 17 ++++-- gcc/jit/libgccjit.cc | 31 +++++++++++ gcc/jit/libgccjit.h | 7 +++ gcc/jit/libgccjit.map | 5 ++ gcc/testsuite/jit.dg/all-non-failing-tests.h | 3 ++ gcc/testsuite/jit.dg/test-temp.c | 56 ++++++++++++++++++++ 11 files changed, 205 insertions(+), 19 deletions(-) create mode 100644 gcc/testsuite/jit.dg/test-temp.c diff --git a/gcc/jit/docs/topics/compatibility.rst b/gcc/jit/docs/topics/compatibility.rst index cbf5b414d8c..5d62e264a00 100644 --- a/gcc/jit/docs/topics/compatibility.rst +++ b/gcc/jit/docs/topics/compatibility.rst @@ -390,3 +390,12 @@ on functions and variables: * :func:`gcc_jit_function_add_string_attribute` * :func:`gcc_jit_function_add_integer_array_attribute` * :func:`gcc_jit_lvalue_add_string_attribute` + +.. _LIBGCCJIT_ABI_27: + +``LIBGCCJIT_ABI_27`` +-------------------- +``LIBGCCJIT_ABI_27`` covers the addition of a functions to create a new +temporary variable: + + * :func:`gcc_jit_function_new_temp` diff --git a/gcc/jit/docs/topics/functions.rst b/gcc/jit/docs/topics/functions.rst index 804605ea939..230caf42466 100644 --- a/gcc/jit/docs/topics/functions.rst +++ b/gcc/jit/docs/topics/functions.rst @@ -171,6 +171,26 @@ Functions underlying string, so it is valid to pass in a pointer to an on-stack buffer. +.. function:: gcc_jit_lvalue *\ + gcc_jit_function_new_temp (gcc_jit_function *func,\ + gcc_jit_location *loc,\ + gcc_jit_type *type) + + Create a new local variable within the function, of the given type. + This function is similar to :func:`gcc_jit_function_new_local`, but + it is to be used for compiler-generated variables (as opposed to + user-defined variables in the language to be compiled) and these + variables won't show up in the debug info. + + The parameter ``type`` must be non-`void`. + + This entrypoint was added in :ref:`LIBGCCJIT_ABI_26`; you can test + for its presence using + + .. code-block:: c + + #ifdef LIBGCCJIT_HAVE_gcc_jit_function_new_temp + .. function:: size_t \ gcc_jit_function_get_param_count (gcc_jit_function *func) diff --git a/gcc/jit/jit-playback.cc b/gcc/jit/jit-playback.cc index 84df6c100e6..cb6b2f66276 100644 --- a/gcc/jit/jit-playback.cc +++ b/gcc/jit/jit-playback.cc @@ -31,6 +31,7 @@ along with GCC; see the file COPYING3. If not see #include "toplev.h" #include "tree-cfg.h" #include "convert.h" +#include "gimple-expr.h" #include "stor-layout.h" #include "print-tree.h" #include "gimplify.h" @@ -1950,13 +1951,27 @@ new_local (location *loc, type *type, const char *name, const std::vector> &attributes) + std::string>> &attributes, + bool is_temp) { gcc_assert (type); - gcc_assert (name); - tree inner = build_decl (UNKNOWN_LOCATION, VAR_DECL, + tree inner; + if (is_temp) + { + inner = build_decl (UNKNOWN_LOCATION, VAR_DECL, + create_tmp_var_name ("JITTMP"), + type->as_tree ()); + DECL_ARTIFICIAL (inner) = 1; + DECL_IGNORED_P (inner) = 1; + DECL_NAMELESS (inner) = 1; + } + else + { + gcc_assert (name); + inner = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (name), type->as_tree ()); + } DECL_CONTEXT (inner) = this->m_inner_fndecl; /* Prepend to BIND_EXPR_VARS: */ diff --git a/gcc/jit/jit-playback.h b/gcc/jit/jit-playback.h index 05bafcd21c4..d285c5a99af 100644 --- a/gcc/jit/jit-playback.h +++ b/gcc/jit/jit-playback.h @@ -527,7 +527,8 @@ public: type *type, const char *name, const std::vector> &attributes); + std::string>> &attributes, + bool is_temp); block* new_block (const char *name); diff --git a/gcc/jit/jit-recording.cc b/gcc/jit/jit-recording.cc index 6ffadbea127..9d7a445e18c 100644 --- a/gcc/jit/jit-recording.cc +++ b/gcc/jit/jit-recording.cc @@ -4190,7 +4190,24 @@ recording::function::new_local (recording::location *loc, type *type, const char *name) { - local *result = new local (this, loc, type, new_string (name)); + local *result = new local (this, loc, type, new_string (name), false); + m_ctxt->record (result); + m_locals.safe_push (result); + return result; +} + +/* Create a recording::local instance and add it to + the functions's context's list of mementos, and to the function's + list of locals. + + Implements the post-error-checking part of + gcc_jit_function_new_temp. */ + +recording::lvalue * +recording::function::new_temp (recording::location *loc, + type *type) +{ + local *result = new local (this, loc, type, NULL, true); m_ctxt->record (result); m_locals.safe_push (result); return result; @@ -6770,7 +6787,8 @@ recording::local::replay_into (replayer *r) ->new_local (playback_location (r, m_loc), m_type->playback_type (), playback_string (m_name), - m_string_attributes); + m_string_attributes, + m_is_temp); if (m_reg_name != NULL) obj->set_register_name (m_reg_name->c_str ()); @@ -6801,16 +6819,26 @@ void recording::local::write_reproducer (reproducer &r) { const char *id = r.make_identifier (this, "local"); - r.write (" gcc_jit_lvalue *%s =\n" - " gcc_jit_function_new_local (%s, /* gcc_jit_function *func */\n" - " %s, /* gcc_jit_location *loc */\n" - " %s, /* gcc_jit_type *type */\n" - " %s); /* const char *name */\n", - id, - r.get_identifier (m_func), - r.get_identifier (m_loc), - r.get_identifier_as_type (m_type), - m_name->get_debug_string ()); + if (m_is_temp) + r.write (" gcc_jit_lvalue *%s =\n" + " gcc_jit_function_new_temp (%s, /* gcc_jit_function *func */\n" + " %s, /* gcc_jit_location *loc */\n" + " %s); /* gcc_jit_type *type */\n", + id, + r.get_identifier (m_func), + r.get_identifier (m_loc), + r.get_identifier_as_type (m_type)); + else + r.write (" gcc_jit_lvalue *%s =\n" + " gcc_jit_function_new_local (%s, /* gcc_jit_function *func */\n" + " %s, /* gcc_jit_location *loc */\n" + " %s, /* gcc_jit_type *type */\n" + " %s); /* const char *name */\n", + id, + r.get_identifier (m_func), + r.get_identifier (m_loc), + r.get_identifier_as_type (m_type), + m_name->get_debug_string ()); } /* The implementation of class gcc::jit::recording::statement. */ diff --git a/gcc/jit/jit-recording.h b/gcc/jit/jit-recording.h index cd2e0adbe30..559f44f1989 100644 --- a/gcc/jit/jit-recording.h +++ b/gcc/jit/jit-recording.h @@ -1330,6 +1330,10 @@ public: type *type, const char *name); + lvalue * + new_temp (location *loc, + type *type); + block* new_block (const char *name); @@ -2092,10 +2096,11 @@ private: class local : public lvalue { public: - local (function *func, location *loc, type *type_, string *name) + local (function *func, location *loc, type *type_, string *name, bool is_temp) : lvalue (func->m_ctxt, loc, type_), m_func (func), - m_name (name) + m_name (name), + m_is_temp (is_temp) { set_scope (func); } @@ -2109,7 +2114,12 @@ public: void write_to_dump (dump &d) final override; private: - string * make_debug_string () final override { return m_name; } + string * make_debug_string () final override { + if (m_is_temp) + return m_ctxt->new_string ("temp"); + else + return m_name; + } void write_reproducer (reproducer &r) final override; enum precedence get_precedence () const final override { @@ -2119,6 +2129,7 @@ private: private: function *m_func; string *m_name; + bool m_is_temp; }; class statement : public memento diff --git a/gcc/jit/libgccjit.cc b/gcc/jit/libgccjit.cc index 9616f3802b8..1687a347e18 100644 --- a/gcc/jit/libgccjit.cc +++ b/gcc/jit/libgccjit.cc @@ -2790,6 +2790,37 @@ gcc_jit_function_new_local (gcc_jit_function *func, return (gcc_jit_lvalue *)func->new_local (loc, type, name); } +/* Public entrypoint. See description in libgccjit.h. + + After error-checking, the real work is done by the + gcc::jit::recording::function::new_temp method in jit-recording.cc. */ + +gcc_jit_lvalue * +gcc_jit_function_new_temp (gcc_jit_function *func, + gcc_jit_location *loc, + gcc_jit_type *type) +{ + RETURN_NULL_IF_FAIL (func, NULL, loc, "NULL function"); + gcc::jit::recording::context *ctxt = func->m_ctxt; + JIT_LOG_FUNC (ctxt->get_logger ()); + /* LOC can be NULL. */ + RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED, + ctxt, loc, + "Cannot add temps to an imported function"); + RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type"); + RETURN_NULL_IF_FAIL_PRINTF1 ( + type->has_known_size (), + ctxt, loc, + "unknown size for temp (type: %s)", + type->get_debug_string ()); + RETURN_NULL_IF_FAIL ( + !type->is_void (), + ctxt, loc, + "void type for temp"); + + return (gcc_jit_lvalue *)func->new_temp (loc, 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 235cab053e0..05349d589d6 100644 --- a/gcc/jit/libgccjit.h +++ b/gcc/jit/libgccjit.h @@ -1384,6 +1384,13 @@ gcc_jit_function_new_local (gcc_jit_function *func, gcc_jit_type *type, const char *name); +extern gcc_jit_lvalue * +gcc_jit_function_new_temp (gcc_jit_function *func, + gcc_jit_location *loc, + gcc_jit_type *type); + +#define LIBGCCJIT_HAVE_gcc_jit_function_new_temp + /********************************************************************** Statement-creation. **********************************************************************/ diff --git a/gcc/jit/libgccjit.map b/gcc/jit/libgccjit.map index dfb8a9d51fb..598513e2584 100644 --- a/gcc/jit/libgccjit.map +++ b/gcc/jit/libgccjit.map @@ -284,3 +284,8 @@ LIBGCCJIT_ABI_26 { gcc_jit_lvalue_add_string_attribute; gcc_jit_function_add_integer_array_attribute; } LIBGCCJIT_ABI_25; + +LIBGCCJIT_ABI_27 { + global: + gcc_jit_function_new_temp; +} LIBGCCJIT_ABI_26; diff --git a/gcc/testsuite/jit.dg/all-non-failing-tests.h b/gcc/testsuite/jit.dg/all-non-failing-tests.h index a3bd0358c02..a141198f1b5 100644 --- a/gcc/testsuite/jit.dg/all-non-failing-tests.h +++ b/gcc/testsuite/jit.dg/all-non-failing-tests.h @@ -346,6 +346,9 @@ /* test-setting-alignment.c: This can't be in the testcases array as it is target-specific. */ +/* test-temp.c: This can't be in the testcases array as it + is target-specific. */ + /* test-string-literal.c */ #define create_code create_code_string_literal #define verify_code verify_code_string_literal diff --git a/gcc/testsuite/jit.dg/test-temp.c b/gcc/testsuite/jit.dg/test-temp.c new file mode 100644 index 00000000000..9b1ba9b0100 --- /dev/null +++ b/gcc/testsuite/jit.dg/test-temp.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include + +#include "libgccjit.h" + +#define TEST_COMPILING_TO_FILE +#define OUTPUT_KIND GCC_JIT_OUTPUT_KIND_ASSEMBLER +#define OUTPUT_FILENAME "output-of-test-test-temp.c.s" +#include "harness.h" + +void +create_code (gcc_jit_context *ctxt, void *user_data) +{ + /* Let's try to inject the equivalent of: +int +func () +{ + int temp = 10; + return temp; +} + */ + 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, + "func", + 0, NULL, 0); + + gcc_jit_block *initial = + gcc_jit_function_new_block (func, "initial"); + + gcc_jit_lvalue *temp = + gcc_jit_function_new_temp (func, NULL, int_type); + + gcc_jit_rvalue *ten = + gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 10); + gcc_jit_block_add_assignment (initial, NULL, temp, ten); + + gcc_jit_block_end_with_return(initial, NULL, + gcc_jit_lvalue_as_rvalue (temp)); +} + +void +verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) +{ + CHECK_NON_NULL (result); +} + +/* { dg-final { jit-verify-output-file-was-created "" } } */ +/* { dg-final { jit-verify-assembler-output-not "JITTMP" } } */ -- 2.43.0