On 31/05/2014 10:56 p.m., bearophile wrote:
My opinions about D array bound checks are slowly changing. I still
think "-boundscheck=off" is useful and good to have. But now I am giving
increasing importance to compiler logic that optimizes away bound checks
safely. People more and more want a safe system language, more and more
persons don't disable array bound tests. This means that optimizing away
bound checks is becoming more and more important in D. And D can't
ignore this need any more. Even adding logic to remove 20% of the bound
checks in numeric code is going to help D because I think more and more
people will not disable bound checks in D.


The following notes are derived from a post by Chris in D.learn:
http://forum.dlang.org/thread/[email protected]


Is it possible to optimize away all array bound checks in code like this?

void main() {
     int[5] data;
     foreach (const i; 0 .. 10)
         data[i] = 0;
     foreach (immutable i; 0 .. 10)
         data[i] = 0;
     int[10] big;
     foreach (const i, x; big)
         data[i] = x;
}


But the compiler must recognize this as correct code:


void main() {
     int[5] data;
     foreach (const i; 0 .. 10)
         if (i < 5)
             data[i] = 0;
}


Can this logic be added to D?


More info on the topic:
http://en.wikipedia.org/wiki/Bounds-checking_elimination
http://ssw.jku.at/Research/Papers/Wuerthinger07/Wuerthinger07.pdf

Bye,
bearophile

The first two foreach statements assignment statements should be compile errors.
I'm actually a little bit surprised that we don't already test for this.
But I spose that would actually be quite hard.
Perhaps if the foreach statement value is ctfe'able we can compare it upon assignment as a value.

Hmm I wonder if there is any more CTFE'able tricks we can do in the compiler.. to improve error checking.

Reply via email to