Thread overview
Warning: volatile does NOT do what you think it does. WRT. DS or similar development.
Jan 12, 2009
downs
Jan 12, 2009
grauzone
Jan 12, 2009
downs
Jan 12, 2009
Walter Bright
Jan 13, 2009
downs
Jan 12, 2009
Sean Kelly
January 12, 2009
Yesterday, I tried porting parts of libnds (the open-source utility library for the Nintendo DS) to D.

Worked relatively fine too, templates work, structs work, volatile DOESN'T WORK.

Let me repeat that. Volatile Doesn't Work.

This was my code:

>
> struct VolatilePointerProxy(T) {
>   T* ptr;
>   T access(T i) { volatile (*ptr) = i; return i; }
>   T access() { T res; volatile res = *ptr; return res; }
>   mixin(PropertyForward!("access"));
> }

This prevents the compiler from optimizing memory accesses past access().

It does *NOT* prevent the compiler from removing "redundant" calls to access.

Like, say, three subsequent 0 writes to MATRIX_MULT4x4. ¹

It took me four hours to find this problem. Foolishly, I trusted volatile to behave at least a _little_ like it does in C.

Walter: I'm certain that you thought changing the behavior of volatile was a good idea at the time, ill-defined though it was, but let me assure you, the act left a gaping hole in the language.

In any case, eventually I settled on this workakludge:

> struct VolatilePointerProxy(T) {
>   T* ptr;
>   pragma(GNU_attribute, noinline) T access(T i) { (*ptr) = i; return i; }
>   pragma(GNU_attribute, noinline) T access() { return *ptr; }
>   mixin(PropertyForward!("access"));
> }

This implies redundant function calls, but at least it works.

(Note: If anybody can tell me how to copy a short from memory to memory in ARM assembly, I'd be much obliged)

To have some good news for a change, here's the pay-off : http://img55.imageshack.us/img55/6811/victory2wv7.jpg .. D on the DS *is feasible*.

 --downs

¹ This is definitely the problem: I dropped three 0 calls in the main function and saw them compile to a single 0 write.
January 12, 2009
Maybe that's why Walter just decided to remove volatile in D2.0:

http://www.digitalmars.com/d/1.0/statement.html#VolatileStatement
http://www.digitalmars.com/d/2.0/statement.html#VolatileStatement
http://www.digitalmars.com/d/2.0/changelog.html#new2_013

I guess in D2.0, volatile accesses will be explicitly done with read_atomic()/write_atomic() style functions?

PS: downs, why do you still trust the compiler, after all the bugs you have found?
January 12, 2009
grauzone wrote:
> Maybe that's why Walter just decided to remove volatile in D2.0:
> 
> http://www.digitalmars.com/d/1.0/statement.html#VolatileStatement http://www.digitalmars.com/d/2.0/statement.html#VolatileStatement http://www.digitalmars.com/d/2.0/changelog.html#new2_013
> 
> I guess in D2.0, volatile accesses will be explicitly done with
> read_atomic()/write_atomic() style functions?
> 
> PS: downs, why do you still trust the compiler, after all the bugs you have found?

I'm addicted to D. It's an abusive, violent relationship. :)
January 12, 2009
downs wrote:
> Walter: I'm certain that you thought changing the behavior of
> volatile was a good idea at the time, ill-defined though it was, but
> let me assure you, the act left a gaping hole in the language.

You are using the GDC back end, and I don't know what it does with the volatile information provided by the front end. Please add a bugzilla bug report for GDC on this.
January 12, 2009
downs wrote:
> Yesterday, I tried porting parts of libnds (the open-source utility library for the Nintendo DS) to D.
> 
> Worked relatively fine too, templates work, structs work, volatile DOESN'T WORK.
> 
> Let me repeat that. Volatile Doesn't Work.
> 
> This was my code:
> 
>> struct VolatilePointerProxy(T) {
>>   T* ptr;
>>   T access(T i) { volatile (*ptr) = i; return i; }
>>   T access() { T res; volatile res = *ptr; return res; }
>>   mixin(PropertyForward!("access"));
>> }
> 
> This prevents the compiler from optimizing memory accesses past access().
> 
> It does *NOT* prevent the compiler from removing "redundant" calls to access.

That's not a legal optimization of 'volatile'.  I'd file a bug report.


Sean
January 13, 2009
Walter Bright wrote:
> downs wrote:
>> Walter: I'm certain that you thought changing the behavior of volatile was a good idea at the time, ill-defined though it was, but let me assure you, the act left a gaping hole in the language.
> 
> You are using the GDC back end, and I don't know what it does with the volatile information provided by the front end. Please add a bugzilla bug report for GDC on this.

Filed. Thanks for the advice.