www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4169] New: building dmd with a modern gcc produces a buggy compiler

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4169

           Summary: building dmd with a modern gcc produces a buggy
                    compiler
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: braddr puremagic.com


--- Comment #0 from Brad Roberts <braddr puremagic.com> 2010-05-09 19:19:52 PDT
---
modern versions of DMD build with strict alias rules.  Somewhere in DMD the
rules are violated sufficiently to produce a compiler that doesn't work
correctly enough to past the test suite.  A mostly (but possibly not
completely) reduced test case:

----
import std.math;

void foo()
{
    float f[1];
    for (int i = 0; i < f.length; i++)
        assert(isnan(f[i]));
}


int main()
{
    foo();
    return 0;
}
----

With dmd as shipped, the test passes.  With dmd build with g++ 4.4, it fails. 
Adding -fno-strict-aliasing to the compilation flags for dmd produces a
compiler that works (at least well enough to pass the above test).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 09 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4169


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla digitalmars.com


--- Comment #1 from Walter Bright <bugzilla digitalmars.com> 2010-05-09
19:39:10 PDT ---
Wikipedia says: "To enable such optimizations in a predictable manner, the ISO
standard for the C programming language (including its newer C99 edition)
specifies that it is illegal (with some exceptions) for pointers of different
types to reference the same memory location. This rule, known as "strict
aliasing", allows impressive increases in performance[citation needed], but has
been known to break some otherwise valid code. Several software projects
intentionally violate this portion of the C99 standard. For example, Python 2.x
did so to implement reference counting,[1] and required changes to the basic
object structs in Python 3 to enable this optimisation. The Linux kernel does
this because strict aliasing causes problems with optimization of inlined
code.[2] In such cases, when compiled with gcc, the option -fno-strict-aliasing
is invoked to prevent unwanted or invalid optimizations that could produce
incorrect code."

The compiler does do a lot of aliasing, so I don't know if this is a gcc bug or
a dmd bug. The way to track it down is to compile half of dmd with
no-strict-aliasing, the other half without, see if it still fails/succeeds.
This isolates it down to which half has the problem. Rinse, repeat, until the
offending source file is isolated.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 09 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4169



--- Comment #2 from Brad Roberts <braddr puremagic.com> 2010-05-09 21:24:44 PDT
---
One better, for whomever gets to this one first, add -Wstrict-aliasing and
check to see if the reported problems are sufficient to fix it.  gcc won't
report _every_ violation, but it does report some meaningful subset of them.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 09 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4169



--- Comment #3 from Brad Roberts <braddr puremagic.com> 2010-05-15 19:02:15 PDT
---
Created an attachment (id=631)
fix all aliasing warnings found by gcc 4.4.3

This change sets fixes all the aliasing warnings found by gcc 4.4.3 with
-Wstrict-aliasing.  The dmd test suite passes now, where is it didn't before
unless built with -fno-strict-aliasing.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 15 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4169


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


--- Comment #4 from Walter Bright <bugzilla digitalmars.com> 2010-05-25
15:09:04 PDT ---
http://www.dsource.org/projects/dmd/changeset/501

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 25 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4169


Brad Roberts <braddr puremagic.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|FIXED                       |


--- Comment #5 from Brad Roberts <braddr puremagic.com> 2010-05-25 20:55:50 PDT
---
Looks like you missed one file's changes:

diff --git a/src/mtype.c b/src/mtype.c
--- a/src/mtype.c
+++ b/src/mtype.c
   -2832,14 +2832,17    Expression *TypeBasic::defaultInit(Loc loc)
      * so that uninitialised variables can be
      * detected even if exceptions are disabled.
      */
-    unsigned short snan[8] = { 0, 0, 0, 0xA000, 0x7FFF };
+    union {
+        unsigned short us[8];
+        long double    ld;
+    } snan = {{ 0, 0, 0, 0xA000, 0x7FFF }};
     /*
      * Although long doubles are 10 bytes long, some
      * C ABIs pad them out to 12 or even 16 bytes, so
      * leave enough space in the snan array.
      */
     assert(REALSIZE <= sizeof(snan));
-    d_float80 fvalue = *(long double*)snan;
+    d_float80 fvalue = snan.ld;
 #endif

 #if LOGDEFAULTINIT


The rest of the compiler builds w/o aliasing warnings on my box from tip of svn
now.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 25 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4169


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |FIXED


--- Comment #6 from Walter Bright <bugzilla digitalmars.com> 2010-05-26
12:47:48 PDT ---
http://www.dsource.org/projects/dmd/changeset/502

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 26 2010