D - Unsafe and doomed
deadalnix
deadalnix at gmail.com
Mon Jan 6 11:21:13 PST 2014
On Monday, 6 January 2014 at 15:56:09 UTC, fra wrote:
> On Monday, 6 January 2014 at 00:20:59 UTC, deadalnix wrote:
>> On Monday, 6 January 2014 at 00:13:19 UTC, Walter Bright wrote:
>>> I'd still like to see an example, even a contrived one.
>>
>> void foo(int* ptr) {
>> *ptr;
>> if (ptr is null) {
>> // do stuff
>> }
>>
>> // do stuff.
>> }
>
>> The problem here is that the if can be removed, as you can't
>> reach that point if the pointer is null, but *ptr can also be
>> removed later as it is a dead load.
>>
>> The resulting code won't crash and do random shit instead.
>
> "Code can't be reached if pointer is null" means "The code
> could fail before reaching here". Honestly, this looks like an
> optimizer issue to me. Who the **** would remove code that
> could fail?
That is the whole node of the issue.
As a matter of fact, any load can trap. Considering this, we
either want the optimizer to prove that the load won't trap
before to optimize it away OR we consider the trap as a special
case that can be removed by the optimizer.
The thing is that the first option is highly undesirable for
performance reason, as the optimizer won't be able to remove most
loads. This isn't something small as memory is WAY slower than
CPU nowadays (a cache miss at the very least 200 cycles,
typically in the 300, add limitation in memory bandwidth and you
have an idea).
That is what I cover in short in one of my previous posts. We
could decide that the optimizer can't remove load unless it can
prove that they do not trap. That mean that most loads can't be
optimized away anymore. Or, we can decide that trapping in not
guaranteed, and then dereferencing null is undefined behavior,
which is much worse that a compile time failure.
More information about the Digitalmars-d
mailing list