From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=gdcproject.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gdcproject.org 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) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gdcproject.org; s=MBO0001; t=1687978441; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=bJSXqRrS1BBX7XRhqlAkAwlKobANdv4L0G+/8fXNG/4=; b=MKEJJQAj7NnpDdaLmZlnJSX52U+hO/ApAjIl3qv6nuG9EXFHnnk9TOGd2BwShBLN8QADR4 yeHeVmmHYr/IzZFxjA08ZNDdNiZWUhPgX9G5dpEMXh5t9z//sewaVSWLdSmx7rTQmiOBu2 O7xeS5VjumAyG/fHwqEqzJHAalH3qq7Nzfpu2v3DHSSY3N9zxzQoTNu419XMs7dCRtz5qX SJC4AO1v1ZjjB1PXsdiNCVoOrzzAW/9fhzHiY9hrenRs1LzMOTmSCJqD4mxdXbRVyO8fOX K4C9YDdiq2sMioDHGsPLWPo1Tb6uvmhmkWdIajURbQ/xrhJVEzg488mtYhH0Uw== From: Iain Buclaw 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 Content-Transfer-Encoding: 8bit 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 List-Id: 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" } } -- 2.39.2