dmd optimizer now converted to D!

Ivan Kazmenko gassa at mail.ru
Thu Jul 5 12:50:18 UTC 2018


On Wednesday, 4 July 2018 at 17:22:22 UTC, H. S. Teoh wrote:
> ... dmd *is* capable of things like strength reduction and code 
> lifting, but as Walter himself has said, it does *not* 
> implement loop unrolling.

Ow!  I always thought it did loop unrolling in some cases, I was 
just never lucky when I checked.  And now you and Walter say its 
implementation started only recently.

Good to know the actual state of things.  Manual loop unrolling 
did help me a couple of times with C++ and D.

-----

By the way, what's a relatively painless way to manually unroll a 
loop in D?  As a simple example, consider:

     for (int i = 0; i < 4 * n; i++)
         a[i] += i;

With C[++], I did simply like this:

     for (int j = 0; j < 4 * n; j += 4) {
#define doit(i) a[i] += i
         doit(j + 0);
         doit(j + 1);
         doit(j + 2);
         doit(j + 3);
     }

This looks long, but on the positive side, it does not actually 
alter the expression: however complex and obscure the "a[i] += i" 
would be in a real example, it can remain untouched.

With D, I used mixins, and they were cumbersome.  Now that we 
have static foreach, it's just this:

     for (int i = 0; i < 4 * n; i += 4)
         static foreach (k; 0..4)
             a[i + k] += i + k;

This looks very nice to me, but still not ideal: a static-foreach 
argument cannot encapsulate a runtime variable, so we have to 
repeat "i + k" twice.  This can get cumbersome for a more complex 
example.  Is there any better way?  To prevent introducing bugs 
when micro-optimizing, I'd like the loop body to remain as 
unchanged as it can be.

Ivan Kazmenko.



More information about the Digitalmars-d mailing list