From patchwork Sun Jun 28 19:31:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 1318635 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=RlIz7uMr; 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 49w13B217Gz9s6w for ; Mon, 29 Jun 2020 05:32:03 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 27F0A3890405; Sun, 28 Jun 2020 19:32:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 27F0A3890405 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1593372720; bh=i2gQFirzEwI2uN1LbrigAG95SaA1mr5tODZY7n99p2o=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=RlIz7uMrb0gFw2Y0KXPl9YATyXXCjpcjHcDVhq6CgMIDcynWFSui7Y3rTxR8IjOCd oebAEswpMHpB76dLKj4PmbXavuhaMnDzgQWRT5QgCiB1xJF5XTNjFvnEK1/QMJML87 40mYcPODgFoc6cMYowDM0B8B70im9NDWWjwjnv7w= 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 1FC08387084F for ; Sun, 28 Jun 2020 19:31:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 1FC08387084F Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 49w12z3RHNzQlJp; Sun, 28 Jun 2020 21:31:55 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter04.heinlein-hosting.de (spamfilter04.heinlein-hosting.de [80.241.56.122]) (amavisd-new, port 10030) with ESMTP id Q9WAsJTR4r_l; Sun, 28 Jun 2020 21:31:52 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [committed] d: Merge upstream dmd 8508c4e68. Date: Sun, 28 Jun 2020 21:31:49 +0200 Message-Id: <20200628193149.540936-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-MBO-SPAM-Probability: 8 X-Rspamd-Score: 1.27 / 15.00 / 15.00 X-Rspamd-Queue-Id: 323891808 X-Rspamd-UID: 949582 X-Spam-Status: No, score=-16.7 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 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 merges the D front-end implementation with upstream dmd 8508c4e68. Fixes a performance bug where 'static foreach' would take an exponentially long time to expand during CTFE. In the following example: static foreach (i; 0..30000) {} Compilation time had been reduced from around 40 to 0.08 seconds. Memory consumption is also reduced from 3.5GB to 55MB. Bootstrapped and regression tested on x86_64-linux-gnu, committed to mainline, and backported to the gcc-10 branch. Regards Iain --- gcc/d/ChangeLog: * dmd/MERGE: Merge upstream dmd 8508c4e68. --- gcc/d/dmd/MERGE | 2 +- gcc/d/dmd/cond.c | 73 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/gcc/d/dmd/MERGE b/gcc/d/dmd/MERGE index 7de89351482..b99e9f3353d 100644 --- a/gcc/d/dmd/MERGE +++ b/gcc/d/dmd/MERGE @@ -1,4 +1,4 @@ -4be011355dd2c5e2e54b99f9369d5faeabca2ca5 +8508c4e683f065eb3deab76b610f7fecb3258a8e The first line of this file holds the git revision number of the last merge done from the dlang/dmd repository. diff --git a/gcc/d/dmd/cond.c b/gcc/d/dmd/cond.c index 12fef59d820..beda133ffdb 100644 --- a/gcc/d/dmd/cond.c +++ b/gcc/d/dmd/cond.c @@ -92,13 +92,23 @@ static void lowerArrayAggregate(StaticForeach *sfe, Scope *sc) el = el->ctfeInterpret(); if (el->op == TOKint64) { - dinteger_t length = el->toInteger(); - Expressions *es = new Expressions(); - for (size_t i = 0; i < length; i++) + Expressions *es; + if (ArrayLiteralExp *ale = aggr->isArrayLiteralExp()) { - IntegerExp *index = new IntegerExp(sfe->loc, i, Type::tsize_t); - Expression *value = new IndexExp(aggr->loc, aggr, index); - es->push(value); + // Directly use the elements of the array for the TupleExp creation + es = ale->elements; + } + else + { + size_t length = (size_t)el->toInteger(); + es = new Expressions(); + es->setDim(length); + for (size_t i = 0; i < length; i++) + { + IntegerExp *index = new IntegerExp(sfe->loc, i, Type::tsize_t); + Expression *value = new IndexExp(aggr->loc, aggr, index); + (*es)[i] = value; + } } sfe->aggrfe->aggr = new TupleExp(aggr->loc, es); sfe->aggrfe->aggr = semantic(sfe->aggrfe->aggr, sc); @@ -307,13 +317,50 @@ static void lowerNonArrayAggregate(StaticForeach *sfe, Scope *sc) Expression *catass = new CatAssignExp(aloc, new IdentifierExp(aloc, idres), res[1]); s2->push(createForeach(sfe, aloc, pparams[1], new ExpStatement(aloc, catass))); s2->push(new ReturnStatement(aloc, new IdentifierExp(aloc, idres))); - Expression *aggr = wrapAndCall(aloc, new CompoundStatement(aloc, s2)); - sc = sc->startCTFE(); - aggr = semantic(aggr, sc); - aggr = resolveProperties(sc, aggr); - sc = sc->endCTFE(); - aggr = aggr->optimize(WANTvalue); - aggr = aggr->ctfeInterpret(); + + Expression *aggr; + Type *indexty; + + if (sfe->rangefe && (indexty = ety->semantic(aloc, sc))->isintegral()) + { + sfe->rangefe->lwr->type = indexty; + sfe->rangefe->upr->type = indexty; + IntRange lwrRange = getIntRange(sfe->rangefe->lwr); + IntRange uprRange = getIntRange(sfe->rangefe->upr); + + const dinteger_t lwr = sfe->rangefe->lwr->toInteger(); + dinteger_t upr = sfe->rangefe->upr->toInteger(); + size_t length = 0; + + if (lwrRange.imin <= uprRange.imax) + length = (size_t)(upr - lwr); + + Expressions *exps = new Expressions(); + exps->setDim(length); + + if (sfe->rangefe->op == TOKforeach) + { + for (size_t i = 0; i < length; i++) + (*exps)[i] = new IntegerExp(aloc, lwr + i, indexty); + } + else + { + --upr; + for (size_t i = 0; i < length; i++) + (*exps)[i] = new IntegerExp(aloc, upr - i, indexty); + } + aggr = new ArrayLiteralExp(aloc, indexty->arrayOf(), exps); + } + else + { + aggr = wrapAndCall(aloc, new CompoundStatement(aloc, s2)); + sc = sc->startCTFE(); + aggr = semantic(aggr, sc); + aggr = resolveProperties(sc, aggr); + sc = sc->endCTFE(); + aggr = aggr->optimize(WANTvalue); + aggr = aggr->ctfeInterpret(); + } assert(!!sfe->aggrfe ^ !!sfe->rangefe); sfe->aggrfe = new ForeachStatement(sfe->loc, TOKforeach, pparams[2], aggr,