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: 1801232 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=yfGIn4Ri; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QrrNM0RNHz20ZV for ; Thu, 29 Jun 2023 04:54:26 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 383563858422 for ; Wed, 28 Jun 2023 18:54:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 383563858422 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1687978464; 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=yfGIn4RiH63KzQ9oetXnPfipHlWapMPNxqLpWZ99O/v5inSCVtc6qqguxJLphUecS 46l6Jarb+xfkicteX8RB/Ee5kwiSytohEYyPmj3nxFepdaVxqFQJ7MaO0ON4gunuw8 /2JFajRZ54EoFMoxlM1BHVfe5VaIc0Iu32HNeubU= 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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" } }