From patchwork Wed Aug 26 08:14:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 1351698 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: 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=sc/NfROG; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BbzDW59XWz9sSJ for ; Wed, 26 Aug 2020 18:14:51 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 84FB23857C4D; Wed, 26 Aug 2020 08:14:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 84FB23857C4D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1598429689; bh=g/kg1s6kCiMOu3BwCMOj/wUpdBP1BztfLILqXXTjwqg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=sc/NfROGpdyJ3jlvPFpKvCrDQgX8UvjS7pxH6qr/WO4JXkFYYmi+cOiq88UmnWi+k DqFDsjIyxhcrRVeWw4RfW9N4KANeNBC22++bN1VU/rZGyYYjSAADkp3VbPLPE6tROq JGLlBXFPAYRoitfCnjFMmLC6gOAq+N4I47Wxa5dE= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout-p-202.mailbox.org (mout-p-202.mailbox.org [80.241.56.172]) by sourceware.org (Postfix) with ESMTPS id 0E64A3857C4D for ; Wed, 26 Aug 2020 08:14:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 0E64A3857C4D Received: from smtp2.mailbox.org (smtp2.mailbox.org [80.241.60.241]) (using TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)) (No client certificate requested) by mout-p-202.mailbox.org (Postfix) with ESMTPS id 4BbzDP6CKXzQlPq; Wed, 26 Aug 2020 10:14:45 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by gerste.heinlein-support.de (gerste.heinlein-support.de [91.198.250.173]) (amavisd-new, port 10030) with ESMTP id qbPc0dMC6RCg; Wed, 26 Aug 2020 10:14:38 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [committed] d: Fix no NRVO when returning an array of a non-POD struct Date: Wed, 26 Aug 2020 10:14:36 +0200 Message-Id: <20200826081436.451013-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-MBO-SPAM-Probability: * X-Rspamd-Score: 0.48 / 15.00 / 15.00 X-Rspamd-Queue-Id: 8524717BD X-Rspamd-UID: 2bbc11 X-Spam-Status: No, score=-15.9 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, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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@gcc.gnu.org Sender: "Gcc-patches" Hi, This patch fixes another NRVO bug in the D front-end where arrays of non-POD types were not being returns by reference. TREE_ADDRESSABLE was not propagated from the RECORD_TYPE to the ARRAY_TYPE, so NRVO code generation was not being triggered. Regstrapped on x86_64-linux-gnu/-m32/-mx32, committed to mainline. Regards Iain --- gcc/d/ChangeLog: PR d/96157 * d-codegen.cc (d_build_call): Handle TREE_ADDRESSABLE static arrays. * types.cc (make_array_type): Propagate TREE_ADDRESSABLE from base type to static array. gcc/testsuite/ChangeLog: PR d/96157 * gdc.dg/pr96157a.d: New test. * gdc.dg/pr96157b.d: New test. --- gcc/d/d-codegen.cc | 4 +-- gcc/d/types.cc | 7 +++-- gcc/testsuite/gdc.dg/pr96157a.d | 24 +++++++++++++++++ gcc/testsuite/gdc.dg/pr96157b.d | 46 +++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gdc.dg/pr96157a.d create mode 100644 gcc/testsuite/gdc.dg/pr96157b.d diff --git a/gcc/d/d-codegen.cc b/gcc/d/d-codegen.cc index 73a6a340ccf..6a7ecc50645 100644 --- a/gcc/d/d-codegen.cc +++ b/gcc/d/d-codegen.cc @@ -1987,11 +1987,11 @@ d_build_call (TypeFunction *tf, tree callable, tree object, targ = build2 (COMPOUND_EXPR, TREE_TYPE (t), targ, t); } - /* Parameter is a struct passed by invisible reference. */ + /* Parameter is a struct or array passed by invisible reference. */ if (TREE_ADDRESSABLE (TREE_TYPE (targ))) { Type *t = arg->type->toBasetype (); - StructDeclaration *sd = t->isTypeStruct ()->sym; + StructDeclaration *sd = t->baseElemOf ()->isTypeStruct ()->sym; /* Nested structs also have ADDRESSABLE set, but if the type has neither a copy constructor nor a destructor available, then we diff --git a/gcc/d/types.cc b/gcc/d/types.cc index 0d05e4d82b3..994d0b9195b 100644 --- a/gcc/d/types.cc +++ b/gcc/d/types.cc @@ -186,8 +186,11 @@ make_array_type (Type *type, unsigned HOST_WIDE_INT size) return t; } - return build_array_type (build_ctype (type), - build_index_type (size_int (size - 1))); + tree t = build_array_type (build_ctype (type), + build_index_type (size_int (size - 1))); + /* Propagate TREE_ADDRESSABLE to the static array type. */ + TREE_ADDRESSABLE (t) = TREE_ADDRESSABLE (TREE_TYPE (t)); + return t; } /* Builds a record type whose name is NAME. NFIELDS is the number of fields, diff --git a/gcc/testsuite/gdc.dg/pr96157a.d b/gcc/testsuite/gdc.dg/pr96157a.d new file mode 100644 index 00000000000..11df19ea700 --- /dev/null +++ b/gcc/testsuite/gdc.dg/pr96157a.d @@ -0,0 +1,24 @@ +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96157 +// { dg-do run { target native } } +// { dg-skip-if "needs gcc/config.d" { ! d_runtime } } + +struct S +{ + @disable this(this); // triggers nrvo + int v; +} + +__gshared void* p; + +S[1000] foo() nothrow +{ + typeof(return) d; + p = &d; + return d; +} + +void main() +{ + auto d = foo(); + assert(p == &d); +} diff --git a/gcc/testsuite/gdc.dg/pr96157b.d b/gcc/testsuite/gdc.dg/pr96157b.d new file mode 100644 index 00000000000..40a7e0ba787 --- /dev/null +++ b/gcc/testsuite/gdc.dg/pr96157b.d @@ -0,0 +1,46 @@ +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96157 +// { dg-options "-fno-moduleinfo -fno-rtti" } +// { dg-do compile } + +int[] testYearsBC; + +struct FilterResult +{ + int[] input; + bool primed; + + this(int[] r) + { + this.input = r; + } + + int front() + { + return input[0]; + } +}; + +FilterResult filter(int[] range) +{ + return FilterResult(range); +} + +int[] chain(int[] rs) +{ + return rs; +} + +struct SysTime +{ + this(int); +} + +void test() +{ + while (1) + { + FilterResult val = filter(chain(testYearsBC)); + int year = val.front(); + SysTime(0); + } +}