WebAssembly image dithering example

Johan Engelen j at j.nl
Tue Aug 7 16:53:41 UTC 2018


On Sunday, 5 August 2018 at 14:06:25 UTC, kinke wrote:
> On Sunday, 5 August 2018 at 03:01:46 UTC, Allen Garvey wrote:
>> The thing that's counter-intuitive to me is that if you write 
>> the pointer address as 0 right in the code it gets interpreted 
>> as null, but if you pass in the same value as 0, everything 
>> works as expected.
>
> If you provide the start address as null literal directly in 
> the D code, the optimizer infers that you're going to read from 
> address 0x3 in the first iteration. If you provide the start 
> address as argument from outside code not available during 
> optimization, LLVM cannot make any assumptions in this regard.

To add: dereferencing `null` is UB with LDC, and the optimizer 
makes use of that. (I didn't look at the code, but reading 
address 3 is OK. Probably you are indexing off of the null ptr, 
which is UB)

There is the `null-pointer-is-valid` attribute that you can put 
on functions to make `null` dereference valid defined behavior. 
In D, that'd be :
```
import ldc.attributes;

void invalid() {
     int* i = null;
     *i = 1;
}

@llvmAttr("null-pointer-is-valid", "true")
void valid() {
     int* i = null;
     *i = 1;
}
```

Probably needs trunk LLVM to work. (I think the attribute is new)

-Johan





More information about the digitalmars-d-ldc mailing list