[DMD] Loop index incorrectly optimised out for -release -O

Basile.B b2.temp at gmx.com
Sun Oct 10 02:41:55 UTC 2021


On Saturday, 9 October 2021 at 21:09:19 UTC, Teodor Dutu wrote:
> Hi,
>
> I discovered a bug in DMD whereby, during optimisation, the 
> loop index variable is removed, despite being used afterwards. 
> I reproduced the bug in [this 
> issue](https://issues.dlang.org/show_bug.cgi?id=22372).
>
> [...]
>
> I am unsure what to make of this, since the logs seem to 
> contradict my experiment with LDC. Has anyone else encountered 
> this? How did you proceed?
>

It is about optimization, it's about loop so a good candidate to 
check is dmd.backend.gloop.d.

you have several optim functions there:

- `elimbasivs(ref loop l)`
- `elimfrivivs(ref loop l)`
- `elimopeqs(ref loop l)`
- `elimspec(ref loop l)`

enable the logging functions and recompile dmd.

It appears that the bug occurs in `elimbasivs`, for example if 
you only enable [this 
printf](https://github.com/dlang/dmd/blob/feab6d82364c8f2c4344e71292f2619c78da480f/src/dmd/backend/gloop.d#L2966)

then dmd, during compilation of the test case, outputs

> Eliminating basic IV 'i'

this is confirmed because if you recompile DMD with an early 
return added in `elimbasivs` then the program does not contain 
the bug.

Now to fix the bug, it's another piece of cake. At first glance 
I'd say that there's a missing condition to `continue` (e.g skip 
the optim) in the code before line 2966.

(I've used a [slightly modifed test 
case](https://dpaste.org/sL4F) btw, 1. to bisect using digger 
more easily, 2. to be sure that the message was not for another 
loop as `i` is a common name)


More information about the Digitalmars-d mailing list