www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - CTFE difference between dmd and ldc2

reply Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
Hello all,

I've recently been working on a port of the mir.random Mersenne 
Twister implementation to Phobos, and I've run into a bit of 
weirdness with CTFE.

Specifically, I use some CTFE functionality to generate a default 
starting state for the generator:
https://github.com/WebDrake/mersenne-twister-range/blob/dda0bb006ee4633ae4fa227f9bad1453ba0e9885/src/mersenne_twister_range.d#L126
https://github.com/WebDrake/mersenne-twister-range/blob/dda0bb006ee4633ae4fa227f9bad1453ba0e9885/src/mersenne_twister_range.d#L166

However, it turns out that while this works while compiling with 
the latest ldc2 beta (v1.1.0-beta6, which uses the dmd 2.071.2 
frontend), it doesn't when compiled with the latest dmd master or 
with dmd 2.072.1 (as installed from the deb package downloaded 
from dlang.org).

Specifically, if we look at the unittests where the generator is 
initialized with implicit construction, versus where the default 
seed is explicitly supplied:
https://github.com/WebDrake/mersenne-twister-range/blob/dda0bb006ee4633ae4fa227f9bad1453ba0e9885/src/mersenne_twister_range.d#L399-L407

... different results emerge when compiled with dmd; it turns out 
that in the implicit-construction case, only the `state.z` 
variable is set (the rest are all zeroes), whereas with the 
explicit seeding, all elements of the `state` variable are set 
correctly.

When ldc2 is used, this doesn't happen and in both cases the 
`state` variable is set correctly.

Can anyone advise what could be going wrong here?  This looks 
like a nasty CTFE bug to me :-(

Thanks & best wishes,

     -- Joe
Dec 27 2016
parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Tuesday, 27 December 2016 at 17:50:15 UTC, Joseph Rushton 
Wakeling wrote:
 Hello all,
 [ ... ]
 Can anyone advise what could be going wrong here?  This looks 
 like a nasty CTFE bug to me :-(

 Thanks & best wishes,

     -- Joe
I doubt that this is a CTFE bug since there should be little difference in the ctfe code between ldc and dmd. That said, it is of course a possibility. I'll have a look.
Dec 27 2016
next sibling parent Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
On Tuesday, 27 December 2016 at 17:56:07 UTC, Stefan Koch wrote:
 I doubt that this is a CTFE bug since there should be little 
 difference in the ctfe code between ldc and dmd.
 That said, it is of course a possibility.

 I'll have a look.
Thanks! It's very weird how, of the values in the `state` variable, one winds up being set correctly and the others are all zero. That might suggest that the `state` variable _is_ being set correctly and then something else is happening that zeroes out most of the values ... ?
Dec 27 2016
prev sibling parent reply Johan Engelen <j j.nl> writes:
On Tuesday, 27 December 2016 at 17:56:07 UTC, Stefan Koch wrote:
 On Tuesday, 27 December 2016 at 17:50:15 UTC, Joseph Rushton 
 Wakeling wrote:
 Hello all,
 [ ... ]
 Can anyone advise what could be going wrong here?  This looks 
 like a nasty CTFE bug to me :-(

 Thanks & best wishes,

     -- Joe
I doubt that this is a CTFE bug since there should be little difference in the ctfe code between ldc and dmd. That said, it is of course a possibility.
Do you see the same with dmd 2.071? (that's the same front-end code as the LDC version tested) -Johan
Dec 27 2016
parent reply Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
On Tuesday, 27 December 2016 at 22:34:50 UTC, Johan Engelen wrote:
 On Tuesday, 27 December 2016 at 17:56:07 UTC, Stefan Koch wrote:
 I doubt that this is a CTFE bug since there should be little 
 difference in the ctfe code between ldc and dmd.
 That said, it is of course a possibility.
Do you see the same with dmd 2.071? (that's the same front-end code as the LDC version tested)
Sorry for delay in following up on this. Yes, the same problem occurs with dmd 2.071 (as installed from the deb package downloaded from dlang.org).
Dec 29 2016
parent reply Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
On Thursday, 29 December 2016 at 09:24:23 UTC, Joseph Rushton 
Wakeling wrote:
 On Tuesday, 27 December 2016 at 22:34:50 UTC, Johan Engelen 
 wrote:
 Do you see the same with dmd 2.071? (that's the same front-end 
 code as the LDC version tested)
Sorry for delay in following up on this. Yes, the same problem occurs with dmd 2.071 (as installed from the deb package downloaded from dlang.org).
Specifically, I tested with 2.071.2, which I understand is the exact same frontend version as LDC 1.1.0-beta6. So, looks like the issue could be backend-related?
Dec 29 2016
parent reply Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
On Thursday, 29 December 2016 at 09:57:25 UTC, Joseph Rushton 
Wakeling wrote:
 On Thursday, 29 December 2016 at 09:24:23 UTC, Joseph Rushton 
 Wakeling wrote:
 Sorry for delay in following up on this.  Yes, the same 
 problem occurs with dmd 2.071 (as installed from the deb 
 package downloaded from dlang.org).
Specifically, I tested with 2.071.2, which I understand is the exact same frontend version as LDC 1.1.0-beta6. So, looks like the issue could be backend-related?
Just to re-raise the issue: it's a blocker for what would otherwise be quite a nice and useful PR for Phobos: https://github.com/dlang/phobos/pull/5011 Assuming a fix is not on the cards any time soon, if anyone could suggest an alternative way to achieve the desired result, I'd be very grateful. I should probably also create a formal issue for this. Any thoughts on how best to break it down into a minimal example? It does not appear easy to do so at first glance :-\
Jan 07 2017
parent Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
On Saturday, 7 January 2017 at 22:55:55 UTC, Joseph Rushton 
Wakeling wrote:
 I should probably also create a formal issue for this.  Any 
 thoughts on how best to break it down into a minimal example?  
 It does not appear easy to do so at first glance :-\
Turned out to be easier than I anticipated. It was not a CTFE problem but one of default initialization of struct fields: https://issues.dlang.org/show_bug.cgi?id=17073 In short, `void` default initialization seems to take priority with dmd regardless of anything else.
Jan 08 2017