From patchwork Wed Jun 28 18:53:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 114002 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp9138638vqr; Wed, 28 Jun 2023 11:54:48 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5Nt/j1keQ/od8SNySVc2qTl/7nUOaC+aMg6/duo/8L7f+q+gqib4sYiLx24p/jcvDQ7H3S X-Received: by 2002:a19:3813:0:b0:4f8:7697:5207 with SMTP id f19-20020a193813000000b004f876975207mr16142629lfa.23.1687978488415; Wed, 28 Jun 2023 11:54:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687978488; cv=none; d=google.com; s=arc-20160816; b=nNmEO3/LjqMU8ylyriYuSvhjnLZVY5A8XBcaIeNhsRvwmbmOLIpUD0/9fz0mG8hRb5 DuZJndGx9WQW7NozVWsSFOn+HW/eBuO9losFC5zjyBup561dYawg5s4geqDwtwhGNVt3 hgNY8zHxbM7dkHoQzPH7cBDtXGfOV9DOSGsiY2hoN/ranhsETxPc7TDKof7hUm2XASk0 oY/BxJbYMoA4eXi1sTWDVAk2uLfpG5nXlSXkBaB+ZzQSre902RtVWUafOBswEQ6vplnu WtlLBui13weSAomYOMpB1feZmIhrV0vbaINncaNNqdpP0i+KSh+subJcItRyOW1zRNA2 XCMQ== 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=bJSXqRrS1BBX7XRhqlAkAwlKobANdv4L0G+/8fXNG/4=; fh=D2tRalIjW6KCgVuqwFHStdMb9uqe8HSQc2WEvRJ6uLE=; b=BnZuPRQXqhrWxwhSIajsfri0wzlovLJBgtMhnXzShwOxebyubS0mJBPswhIN3c6ofh vVKqMD2luuBiPvOxAcBl18VPzAcjl/R1pvL0Dr02Y+D+HZLxXCnPo9sJRVYfgC6fo+Ef APzME8EBzDoA47f5YIaCS+CmfJKIclN0o1ZtnxUv8lk2sBk0BYJt7pBm9zfMOXuqXwK3 /iogNtC+2Do3vDEOI2jfAWYqy7Y7eE9z84vXC8s1BP0wXpXpRSjzCa5jvcOtcOui93xC /Pyqw2dSGPN9bZhnQXBj1yoF9Sm91BSpHYA38oEWmJ9desXA4JXGrgwPLlz/QosdXitb C8uA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ZlcWfMKb; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id d22-20020aa7d5d6000000b0051d96936b9asi4279300eds.338.2023.06.28.11.54.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jun 2023 11:54:48 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ZlcWfMKb; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 1E7F4385840D for ; Wed, 28 Jun 2023 18:54:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1E7F4385840D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1687978487; bh=bJSXqRrS1BBX7XRhqlAkAwlKobANdv4L0G+/8fXNG/4=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=ZlcWfMKbkOersD86PcH2jQQ1GXG9dOylIfRAOS4lWoCTVovh3gVmkGI58VLlymBXM 5FJgzmTvWQ2m4E1e3BWrHn/tqWCRjRyN9BQ9Rah7hDPadtXQe+PxkvyfecW2+uF4Tt iuijnSsAJz7+BFeA5xemZ8bzCV7fdf3OBeDOJphE= 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 3D5843858D37 for ; Wed, 28 Jun 2023 18:54:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3D5843858D37 Received: from smtp1.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:b231:465::1]) (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 4QrrMs625hz9stv; Wed, 28 Jun 2023 20:54:01 +0200 (CEST) To: gcc-patches@gcc.gnu.org Cc: Iain Buclaw Subject: [committed] d: Fix wrong code-gen when returning structs by value. Date: Wed, 28 Jun 2023 20:53:57 +0200 Message-Id: <20230628185357.2326251-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4QrrMs625hz9stv X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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?1769973731515434081?= X-GMAIL-MSGID: =?utf-8?q?1769973731515434081?= Hi, Since r13-1104, structs in the D have had compute_record_mode called too early on them, causing them to return differently depending on the order that types are generated in, and whether there are forward references. This patch moves the call to compute_record_mode into its own function, and calls it after all fields have been given a size. Bootstrapped on i686-apple-darwin17 - previously it failed at stage2 - as well as bootstrapped and regression tested on x86_64-linux-gnu/-m32. Committed to mainline, and backported to releases/gcc-13. Regards, Iain. --- PR d/106977 PR target/110406 gcc/d/ChangeLog: * types.cc (finish_aggregate_mode): New function. (finish_incomplete_fields): Call finish_aggregate_mode. (finish_aggregate_type): Replace call to compute_record_mode with finish_aggregate_mode. gcc/testsuite/ChangeLog: * gdc.dg/torture/pr110406.d: New test. --- gcc/d/types.cc | 39 ++++++++++++++++++++++--- gcc/testsuite/gdc.dg/torture/pr110406.d | 25 ++++++++++++++++ 2 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gdc.dg/torture/pr110406.d diff --git a/gcc/d/types.cc b/gcc/d/types.cc index bdf07f83d4b..ef2d80e5bd4 100644 --- a/gcc/d/types.cc +++ b/gcc/d/types.cc @@ -573,6 +573,35 @@ layout_aggregate_type (AggregateDeclaration *decl, tree type, } } +/* Given a record type TYPE compute the finalized record mode if all fields have + had their types resolved and sizes determined. */ + +void +finish_aggregate_mode (tree type) +{ + for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) + { + /* Fields of type `typeof(*null)' have no size, so let them force the + record type mode to be computed as BLKmode. */ + if (TYPE_MAIN_VARIANT (TREE_TYPE (field)) == noreturn_type_node) + break; + + if (DECL_SIZE (field) == NULL_TREE) + return; + } + + compute_record_mode (type); + + /* Propagate computed mode to all variants of this aggregate type. */ + for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) + { + if (t == type) + continue; + + SET_TYPE_MODE (t, TYPE_MODE (type)); + } +} + /* If the aggregate type TYPE completes the type of any previous field declarations, lay them out now. */ @@ -596,6 +625,9 @@ finish_incomplete_fields (tree type) } relayout_decl (field); + + /* Relayout of field may change the mode of its RECORD_TYPE. */ + finish_aggregate_mode (DECL_FIELD_CONTEXT (field)); } /* No more forward references to process. */ @@ -615,9 +647,6 @@ finish_aggregate_type (unsigned structsize, unsigned alignsize, tree type) SET_TYPE_ALIGN (type, alignsize * BITS_PER_UNIT); TYPE_PACKED (type) = (alignsize == 1); - /* Set the back-end type mode. */ - compute_record_mode (type); - /* Layout all fields now the type is complete. */ for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) { @@ -662,6 +691,9 @@ finish_aggregate_type (unsigned structsize, unsigned alignsize, tree type) } } + /* Set the back-end type mode after all fields have had their size set. */ + finish_aggregate_mode (type); + /* Fix up all forward-referenced variants of this aggregate type. */ for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) { @@ -673,7 +705,6 @@ finish_aggregate_type (unsigned structsize, unsigned alignsize, tree type) TYPE_SIZE (t) = TYPE_SIZE (type); TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type); TYPE_PACKED (type) = TYPE_PACKED (type); - SET_TYPE_MODE (t, TYPE_MODE (type)); SET_TYPE_ALIGN (t, TYPE_ALIGN (type)); TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type); } diff --git a/gcc/testsuite/gdc.dg/torture/pr110406.d b/gcc/testsuite/gdc.dg/torture/pr110406.d new file mode 100644 index 00000000000..c380e4bdec8 --- /dev/null +++ b/gcc/testsuite/gdc.dg/torture/pr110406.d @@ -0,0 +1,25 @@ +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110406 +// { dg-do compile { target i?86-*-* x86_64-*-* } } +// { dg-options "-fdump-tree-optimized" } +struct cpuid_abcd_t +{ + uint eax; + uint ebx; + uint ecx; + uint edx; +}; + +cpuid_abcd_t cpuid_insn(const uint in_eax) +{ + cpuid_abcd_t ret = void; + asm { "cpuid" + : "=a" (ret.eax), + "=b" (ret.ebx), + "=c" (ret.ecx), + "=d" (ret.edx) + : "a" (in_eax) + :; + } + return ret; +} +// { dg-final { scan-tree-dump-not "MEM " "optimized" } }