DMD fails to optimize code like the following, when foreach aggregate is staticIota!(0, x >= 100).
Reduced test case:
template TypeTuple(TList...)
{
alias TypeTuple = TList;
}
package template staticIota(int beg, int end)
{
static if (beg + 1 >= end)
{
static if (beg >= end)
{
alias staticIota = TypeTuple!();
}
else
{
alias staticIota = TypeTuple!(+beg);
}
}
else
{
enum mid = beg + (end - beg) / 2;
alias staticIota = TypeTuple!(staticIota!(beg, mid), staticIota!(mid, end));
}
}
void test()
{
int row;
foreach (s; staticIota!(0, 100))
{
if (++row) row = 0;
}
}
$ dmd -O -c test.d
Internal error: backend/go.c 242
Comment #1 by ketmar — 2015-03-10T00:20:05Z
Created attachment 1490
workaround
this is caused by the same bug that causes Issue #14214: too many "code comma expressions".
i'm almost sure that both bugs triggered by the same code path, as i did a quick workaround for #14214 (not failing on processing too many OPcomma blocks), and it fixes this bug too.
yet i'm not familiar with codegen, so i will not mark this as duplicate, as this need further investigation from a person with codegen/optimizer knowledge.
i attached the workaround for those who want to dive into optimizer. it degrades optimizing quality in such pathological cases, but it at least allows optimizer to process other code.
Comment #2 by ketmar — 2015-03-10T00:22:53Z
ah, and changed the hardware, as this is general arch-independent optimizer bug.
Comment #3 by razvan.nitu1305 — 2023-03-07T16:45:54Z
I cannot reproduce this. The code compiles successfully now.