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.

David

Reply via email to