d: Merge upstream dmd 8508c4e68.
authorIain Buclaw <ibuclaw@gdcproject.org>
Sun, 28 Jun 2020 15:46:18 +0000 (17:46 +0200)
committerIain Buclaw <ibuclaw@gdcproject.org>
Sun, 28 Jun 2020 16:04:20 +0000 (18:04 +0200)
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.

Reviewed-on: https://github.com/dlang/dmd/pull/11335

gcc/d/ChangeLog:

* dmd/MERGE: Merge upstream dmd 8508c4e68.

gcc/d/dmd/MERGE
gcc/d/dmd/cond.c

index 7de89351482cd1a4accc3af904fcb7b33c8c1e16..b99e9f3353d990381ee67dd5bb321a4263713306 100644 (file)
@@ -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.
index 12fef59d8207312c51c9db282a75b68f77323b2f..beda133ffdb64c9f5afff181912e9b59220ef922 100644 (file)
@@ -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,