From patchwork Fri Jul 7 19:29:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 117262 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f45:0:b0:3ea:f831:8777 with SMTP id v5csp3493347vqx; Fri, 7 Jul 2023 12:30:15 -0700 (PDT) X-Google-Smtp-Source: APBJJlG+E0oaX0ORoLwJbeINZ1yASuBmB5x1JtYiQ3ahVnf9ru/11vUzTCclpatonHx9zlRoDDxQ X-Received: by 2002:a17:907:2383:b0:98d:63c5:d132 with SMTP id vf3-20020a170907238300b0098d63c5d132mr4166036ejb.72.1688758215315; Fri, 07 Jul 2023 12:30:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688758215; cv=none; d=google.com; s=arc-20160816; b=g6YuOq4GsbssqIg1ka06EnoBuKLDqvDCcIuE+8VXQkPYMXiPC/YvHGKDQYZrA2id8S QE2rIyUp93BLLNNMeLeXLCgCG6AbH9Ye33tAnvCD1ZFF22w5la6f7DFij6o0bDAx5IYm L4Iitzo+c4CETTriuGQBmUFM+YndCjw6kZKdpgPx5uK/01fXHdBwyf9yuBaaeRspeY8P zjTBKcbzP8EKFDx5sU6Zwey1+yf0iox6TClrK0L91yXWAGDXQKjlgEC2e5rHTfYkAQBa m5LWYqqNQingtcdlpc01J13ioVVrSRz9nEGq1ttYUZbomUfgsV4xDVm/2qOphjMnljLe n9SA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=aWLzqrREDLqBKovbrca0CPbjX73zw2CtxlXXMMqPy2w=; fh=ZGxb8ZqE1Z3K8UpiAjGR6jhgkuT763swdNPJbYMDnng=; b=oBg4ME0Hl5uN8Sb5CPmahhVcH0zQFeyfq2jTdGkJnc6Hzqqv+53Mu3qTfeQo1WK4/n WUPlC7IKgsD3o0lAnGJ/cWajNPdABGpe2NDmzPo6AUmwD3Esb0neGvthLkOXW31k/WX/ zQkYKTj0sxYvjOfJaNLgPlCHHJr46Cg4f5WT24uNMPbElBhneJ1OJmA+Ew411Y1FuMC4 TOd34+jPWpENBRr4bZMsWO29rZuZVWfo6HTYPrdRR4e5XmH2yoQP7jxiuZe94tKYVX2Y 7YP3NQRniLabgcld8+lZT4f6Uzx96+JasmXMji3I3z2QU3yvM+S6Zbyo2748+nLnBnFl lQBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=PTFm4vGW; 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=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id k9-20020a1709063e0900b009920612b89asi2297921eji.351.2023.07.07.12.30.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jul 2023 12:30:15 -0700 (PDT) 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=@gcc.gnu.org header.s=default header.b=PTFm4vGW; 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=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 38CEE385DC14 for ; Fri, 7 Jul 2023 19:30:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 38CEE385DC14 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1688758214; bh=aWLzqrREDLqBKovbrca0CPbjX73zw2CtxlXXMMqPy2w=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=PTFm4vGWbpkzsCpj5kjoO/m9oI+yOG31vyf3p7dlMtuHJOSzmtcbdDEs4SB8FKTmh xlNq4FKZW5xMXdTvX2ODyHK+YL5g6AlA34y8uzG/A8d0RqYEKNOu923f+z7bu7i2i3 W4thPxUJVCdBiGaBG2y9gfZOed1Tnod1FkEG7gBs= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [80.241.56.171]) by sourceware.org (Postfix) with ESMTPS id DE2B83858430 for ; Fri, 7 Jul 2023 19:29:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DE2B83858430 Received: from smtp102.mailbox.org (smtp102.mailbox.org [10.196.197.102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 4QyNkY5Z7tz9sTL; Fri, 7 Jul 2023 21:29:25 +0200 (CEST) To: gcc-patches@gcc.gnu.org Cc: Iain Buclaw Subject: [committed] d: Fix PR 108842: Cannot use enum array with -fno-druntime Date: Fri, 7 Jul 2023 21:29:23 +0200 Message-Id: <20230707192923.465324-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Iain Buclaw via Gcc-patches From: Iain Buclaw Reply-To: Iain Buclaw Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770791334556129776?= X-GMAIL-MSGID: =?utf-8?q?1770791334556129776?= Hi, This patch restricts generating of CONST_DECLs for D manifest constants to just scalars without pointers. It shouldn't happen that a reference to a manifest constant has not been expanded within a function body during codegen, but it has been found to occur in older versions of the D front-end (PR98277), so if the decl of a non-scalar constant is requested, just return its initializer as an expression. Bootstrapped and regresson tested on x86_64-linux-gnu/-m32, committed to mainline, and backported to the gcc-11, gcc-12, and gcc-13 release branches. Regards, Iain. --- PR d/108842 gcc/d/ChangeLog: * decl.cc (DeclVisitor::visit (VarDeclaration *)): Only emit scalar manifest constants. (get_symbol_decl): Don't generate CONST_DECL for non-scalar manifest constants. * imports.cc (ImportVisitor::visit (VarDeclaration *)): New method. gcc/testsuite/ChangeLog: * gdc.dg/pr98277.d: Add more tests. * gdc.dg/pr108842.d: New test. --- gcc/d/decl.cc | 36 +++++++++++++++++++-------------- gcc/d/imports.cc | 9 +++++++++ gcc/testsuite/gdc.dg/pr108842.d | 4 ++++ gcc/testsuite/gdc.dg/pr98277.d | 11 ++++++++++ 4 files changed, 45 insertions(+), 15 deletions(-) create mode 100644 gcc/testsuite/gdc.dg/pr108842.d diff --git a/gcc/d/decl.cc b/gcc/d/decl.cc index 3f980851259..0375ede082b 100644 --- a/gcc/d/decl.cc +++ b/gcc/d/decl.cc @@ -782,7 +782,7 @@ public: { /* Do not store variables we cannot take the address of, but keep the values for purposes of debugging. */ - if (!d->type->isscalar ()) + if (d->type->isscalar () && !d->type->hasPointers ()) { tree decl = get_symbol_decl (d); d_pushdecl (decl); @@ -1212,6 +1212,20 @@ get_symbol_decl (Declaration *decl) return decl->csym; } + if (VarDeclaration *vd = decl->isVarDeclaration ()) + { + /* CONST_DECL was initially intended for enumerals and may be used for + scalars in general, but not for aggregates. Here a non-constant + value is generated anyway so as its value can be used. */ + if (!vd->canTakeAddressOf () && !vd->type->isscalar ()) + { + gcc_assert (vd->_init && !vd->_init->isVoidInitializer ()); + Expression *ie = initializerToExpression (vd->_init); + decl->csym = build_expr (ie, false); + return decl->csym; + } + } + /* Build the tree for the symbol. */ FuncDeclaration *fd = decl->isFuncDeclaration (); if (fd) @@ -1259,23 +1273,15 @@ get_symbol_decl (Declaration *decl) if (vd->storage_class & STCextern) DECL_EXTERNAL (decl->csym) = 1; - /* CONST_DECL was initially intended for enumerals and may be used for - scalars in general, but not for aggregates. Here a non-constant - value is generated anyway so as the CONST_DECL only serves as a - placeholder for the value, however the DECL itself should never be - referenced in any generated code, or passed to the back-end. */ - if (vd->storage_class & STCmanifest) + if (!vd->canTakeAddressOf ()) { /* Cannot make an expression out of a void initializer. */ - if (vd->_init && !vd->_init->isVoidInitializer ()) - { - Expression *ie = initializerToExpression (vd->_init); + gcc_assert (vd->_init && !vd->_init->isVoidInitializer ()); + /* Non-scalar manifest constants have already been dealt with. */ + gcc_assert (vd->type->isscalar ()); - if (!vd->type->isscalar ()) - DECL_INITIAL (decl->csym) = build_expr (ie, false); - else - DECL_INITIAL (decl->csym) = build_expr (ie, true); - } + Expression *ie = initializerToExpression (vd->_init); + DECL_INITIAL (decl->csym) = build_expr (ie, true); } /* [type-qualifiers/const-and-immutable] diff --git a/gcc/d/imports.cc b/gcc/d/imports.cc index 2efef4ed54f..3172b799cb0 100644 --- a/gcc/d/imports.cc +++ b/gcc/d/imports.cc @@ -127,6 +127,15 @@ public: this->result_ = this->make_import (TYPE_STUB_DECL (type)); } + void visit (VarDeclaration *d) final override + { + /* Not all kinds of manifest constants create a CONST_DECL. */ + if (!d->canTakeAddressOf () && !d->type->isscalar ()) + return; + + visit ((Declaration *) d); + } + /* For now, ignore importing other kinds of dsymbols. */ void visit (ScopeDsymbol *) final override { diff --git a/gcc/testsuite/gdc.dg/pr108842.d b/gcc/testsuite/gdc.dg/pr108842.d new file mode 100644 index 00000000000..5aae9e5000d --- /dev/null +++ b/gcc/testsuite/gdc.dg/pr108842.d @@ -0,0 +1,4 @@ +// { dg-do compile } +// { dg-options "-fno-rtti" } +module object; +enum int[] x = [0, 1, 2]; diff --git a/gcc/testsuite/gdc.dg/pr98277.d b/gcc/testsuite/gdc.dg/pr98277.d index 0dff142a6ef..c88c735dec8 100644 --- a/gcc/testsuite/gdc.dg/pr98277.d +++ b/gcc/testsuite/gdc.dg/pr98277.d @@ -11,3 +11,14 @@ ref int getSide(Side side, return ref int left, return ref int right) { return side == Side.left ? left : right; } + +enum SideA : int[] +{ + left = [0], + right = [1], +} + +int getSideA(SideA side, ref int left, ref int right) +{ + return side == SideA.left ? left : right; +}