Thread overview
ldc doesn't elide bounds check
Mar 05
Kagamin
Mar 05
Kagamin
Mar 07
kinke
March 05
void f(byte[] a, byte[] b)
{
	if(a.length<b.length)b=b[0..a.length];
	foreach(i,c;b)
	{
		a[i]=c;
		if(c==10)break;
	}
}
void g(ulong a, ulong b)
{
	if(a<b)b=a;
	foreach(i;0..b)
	{
		assert(i<=a);
		if(i==10)break;
	}
}

In the function f the code for assert failure is retained, but in the function g it disappears at optimization levels -Os and above. Is it just me is there some limit for the optimizer?
March 05
Ah, I see, for assert(i<a); the failure handler is retained again, but I wonder why.
March 07
On Monday, 5 March 2018 at 19:36:20 UTC, Kagamin wrote:
> void f(byte[] a, byte[] b)
> {
> 	if(a.length<b.length)b=b[0..a.length];
> 	foreach(i,c;b)
> 	{
> 		a[i]=c;
> 		if(c==10)break;
> 	}
> }
> void g(ulong a, ulong b)
> {
> 	if(a<b)b=a;
> 	foreach(i;0..b)
> 	{
> 		assert(i<=a);
> 		if(i==10)break;
> 	}
> }
>
> In the function f the code for assert failure is retained, but in the function g it disappears at optimization levels -Os and above. Is it just me is there some limit for the optimizer?

Thx for noticing; I filed an LDC issue about it (https://github.com/ldc-developers/ldc/issues/2607), although gcc and clang are also unable to optimize away these checks for analogous C code: https://godbolt.org/g/AfyMJM