Optimizing out unnecessary allocations

Artur Skawina art.08.09 at gmail.com
Wed Oct 17 13:37:42 PDT 2012


On 10/17/12 20:10, David Nadlinger wrote:
> I recently revived an LDC optimization pass (originally written by Frits van Bommel) that can remove dead allocations and promote small GC allocations to stack memory by recognizing the related druntime calls.
> 
> However, I didn't commit it to the main branch yet because it causes a test case in the DMD test suite to fail: https://github.com/D-Programming-Language/dmd/blob/eaa03fefeb1a698f586d5f5a09068f3433bf4b29/test/runnable/testgc2.d
> 
> The test is intended to make sure that the garbage collector properly handles out-of-memory situations, but in doing so assumes that the compiler can't touch the array allocations. However, when building with optimizations on, LDC recognizes the fact that the allocations are unused and subsequently removes them, causing the "assert(0)"s to be hit:
> 
> ———
> try {
>   long[] l = new long[ptrdiff_t.max];
>   assert(0);
> }
> catch (OutOfMemoryError o){}
> ———
> 
> Thus, my question: Is it legal for a D compiler to eliminate dead GC allocations (given that there are no side effects due to constructors, …)?
> 
> I'd strongly argue in favor of such optimizations to be legal, because not allowing them would eliminate quite a few optimization possibilities. There is also precedent for such behavior; many C compilers treat malloc() in a similar fashion.


Well, I think such optimizations are fine (as long as documented and there
exists alternatives), but note that this testcase checks for the case where
the object size calculation overflows. Ie it must not succeed.
Would ignoring the error when nothing accesses the object make sense?

artur


More information about the Digitalmars-d mailing list