digitalmars.D - [DMD] Loop index incorrectly optimised out for -release -O
- Teodor Dutu (28/28) Oct 09 2021 Hi,
- Iain Buclaw (3/14) Oct 09 2021 Just to clarify, those are all DMD. The difference in those
- Teodor Dutu (2/17) Oct 10 2021 Oh I see. Thanks for pointing it out.
- Basile.B (23/33) Oct 09 2021 It is about optimization, it's about loop so a good candidate to
- Imperatorn (2/7) Oct 10 2021 Yup, I was also dissecting gloop last night
- Walter Bright (3/4) Oct 15 2021 Thanks for doing the detective work in finding where the problem is. Mad...
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). Using run.dlang.io, I tried to find a regression, but all supported DMD versions (2.060 and newer) are printing the same incorrect output (`Exception: i = 0; n = 1`). My experiment can be found [here](https://run.dlang.io/is/uYfTzl). This seems to be a backend issue, as both LDC and LDC-beta are working fine. However, when analysing the CI outputs from [the PR](https://github.com/dlang/dmd/pull/13116) which made me come across this bug, I noticed that all 3 of DMD, LDC and GDC are failing the same test, which I've also been able to reproduce myself: - LDC: https://cirrus-ci.com/task/6291197929979904?logs=test_druntime#L1449 - DMD: https://cirrus-ci.com/task/5728247976558592?logs=test_druntime#L1390 - GDC: https://cirrus-ci.com/task/4883823046426624?logs=test_druntime#L1411 The failure of this test is caused by the issue I mentioned above and the code with which I reproduced the bug is based on it. 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? Thanks, Teodor
Oct 09 2021
On Saturday, 9 October 2021 at 21:09:19 UTC, Teodor Dutu wrote:However, when analysing the CI outputs from [the PR](https://github.com/dlang/dmd/pull/13116) which made me come across this bug, I noticed that all 3 of DMD, LDC and GDC are failing the same test, which I've also been able to reproduce myself: - LDC: https://cirrus-ci.com/task/6291197929979904?logs=test_druntime#L1449 - DMD: https://cirrus-ci.com/task/5728247976558592?logs=test_druntime#L1390 - GDC: https://cirrus-ci.com/task/4883823046426624?logs=test_druntime#L1411Just to clarify, those are all DMD. The difference in those pipelines is who built DMD.
Oct 09 2021
On Saturday, 9 October 2021 at 22:05:15 UTC, Iain Buclaw wrote:On Saturday, 9 October 2021 at 21:09:19 UTC, Teodor Dutu wrote:Oh I see. Thanks for pointing it out.However, when analysing the CI outputs from [the PR](https://github.com/dlang/dmd/pull/13116) which made me come across this bug, I noticed that all 3 of DMD, LDC and GDC are failing the same test, which I've also been able to reproduce myself: - LDC: https://cirrus-ci.com/task/6291197929979904?logs=test_druntime#L1449 - DMD: https://cirrus-ci.com/task/5728247976558592?logs=test_druntime#L1390 - GDC: https://cirrus-ci.com/task/4883823046426624?logs=test_druntime#L1411Just to clarify, those are all DMD. The difference in those pipelines is who built DMD.
Oct 10 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, outputsEliminating 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)
Oct 09 2021
On Sunday, 10 October 2021 at 02:41:55 UTC, Basile.B wrote:On Saturday, 9 October 2021 at 21:09:19 UTC, Teodor Dutu wrote:Yup, I was also dissecting gloop last night[...]It is about optimization, it's about loop so a good candidate to check is dmd.backend.gloop.d. [...]
Oct 10 2021
On 10/9/2021 7:41 PM, Basile.B wrote:[...]Thanks for doing the detective work in finding where the problem is. Made it much quicker for me to fix it.
Oct 15 2021