Bug ID 188
Summary Optimized GDC with no-bounds-checking skips operations in foreach if given statically
Product GDC
Version 4.9.x
Hardware x86_64
OS Linux
Status NEW
Severity critical
Priority Normal
Component gdc
Assignee ibuclaw@gdcproject.org
Reporter liran@weka.io

Reproducible on commit b022dd4cac195d85e9c3a6a37f2501a07ade455a from April 7th
on the 4.9 branch.

Please consider the following code:
---------------------
module test;

import std.stdio;

enum R = 4;
enum S = 2;


int main() {
    uint[R+S] a = 6;
    uint[R+S] b = 9;
    writeln("a, b : ", a, b);
    ushort c = 3;
    foreach(i; 0..R) {
        b[i .. (i+1)] += a[i .. (i+1)];
        a[i .. (i+1)] = b[i .. (i+1)] ^ c;
    }
    writeln("After operation: a,b: ", a, b);
    return 0;
}
-------------------------

And the following output, first from dmd, then gdc:

bash-4.3# dmd test.d -ofdtest && ./dtest
a, b : [6, 6, 6, 6, 6, 6][9, 9, 9, 9, 9, 9]
After operation: a,b: [12, 12, 12, 12, 6, 6][15, 15, 15, 15, 9, 9]

bash-4.3# /opt/gdc/bin/gdc   -fno-bounds-check  -O  test.d -ogtest && ./gtest
a, b : [6, 6, 6, 6, 6, 6][9, 9, 9, 9, 9, 9]
After operation: a,b: [12, 6, 6, 6, 6, 6][15, 9, 9, 9, 9, 9] # WRONG!!!

When GDC compiles the code with static range in the foreach, only the first
index is being assigned and only the first calculation is being performed.

In this example there is also S set, but it reproduces perfectly without the S,
or when it's 0. I just added it to show that the code cannot be fixed with
foreach(i; 0 .. b.length).

I put two examples of operations (addition and xor), but it actually does not
matter. Any operation that I tried reproduces the problem (as long as there is
no function call in the foreach() ).


You are receiving this mail because: