D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7953 - DMD Error: variable r used before set when compiling with -O
Summary: DMD Error: variable r used before set when compiling with -O
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P2 normal
Assignee: No Owner
URL:
Keywords: SIMD
Depends on:
Blocks:
 
Reported: 2012-04-20 12:30 UTC by Zoadian
Modified: 2017-07-19 03:28 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Zoadian 2012-04-20 12:30:21 UTC
When compiling this code with dmd -O it says:
test.d(55) Error: variable r used before set
i might have overlooked something, but shouldn't that compile?
dmd 2.059 here.


import std.stdio;
import std.math;
import core.simd;
import std.datetime;
import std.conv;

/**
Benchmarks:


suicide@zoaHTPC /media/code $ dmd test.d
_VEC: [TickDuration(2014413920)]
SIMD: [TickDuration(2057610817)]
suicide@zoaHTPC /media/code $ dmd test.d -O
_VEC: [TickDuration(1589025986)]
SIMD: [TickDuration(2050198165)]
suicide@zoaHTPC /media/code $ dmd test.d -O -release
_VEC: [TickDuration(1738538836)]
SIMD: [TickDuration(1172608340)]
suicide@zoaHTPC /media/code $ dmd test.d -O -release -inline
_VEC: [TickDuration(1570424587)]
SIMD: [TickDuration(1184942170)]
suicide@zoaHTPC /media/code $ dmd test.d -O -release -noboundscheck
_VEC: [TickDuration(752330929)]
SIMD: [TickDuration(1128528117)]

*/


 string gen(int D) {
        string s;
        for(size_t i = 0; i < D-1; i+=4)
        {
            s ~= "v1.array = a1["~to!string(i)~".."~to!string(i+4)~"];";
            s ~= "v2.array = a2["~to!string(i)~".."~to!string(i+4)~"];";
            s ~= "r = __simd(XMM.ADDPS, v1,v2);";            
            
            
            //~ s ~= "res["~to!string(i)~".."~to!string(i+4)~"] = r.array;";
            s ~= "res.ptr["~to!string(i)~"] = r.ptr["~to!string(i)~"];";
            s ~= "res.ptr["~to!string(i+1)~"] = r.ptr["~to!string(i+1)~"];";
            s ~= "res.ptr["~to!string(i+2)~"] = r.ptr["~to!string(i+2)~"];";
            s ~= "res.ptr["~to!string(i+3)~"] = r.ptr["~to!string(i+3)~"];";
        }
        return s;
    }

T[D] simdAdd(T, int D)(T[D] a1, T[D] a2) {
    T[D] res;
    float4 v1;
    float4 v2;
    float4 r;
    
   //writeln(gen(D));
    mixin(gen(D));
    return res;
}

T[D] addV(T, int D)(T[D] a1, T[D] a2) {
    T[D] r;
    //foreach(x;0..10)
        foreach(i;0..D)
            r[i] = a1[i]+a2[i];
    return r;
}


void main()
{
    float[8] v1 = [1,2,3,4,1,2,3,4];
    float[8] v2 = [1,2,3,4,1,2,3,4];
    void test1(){addV(v1,v2);}
    void test2(){simdAdd(v1,v2);}
    
   // test2();
    //~ writeln("STD: ", benchmark!(addS!(int,4)([1,2,3,4],[1,2,3,4]))(10_000_000));
    writeln("_VEC: ", benchmark!(test1)(10_000_000));
    writeln("SIMD: ", benchmark!(test2)(10_000_000));
}
Comment 1 Zoadian 2012-04-20 12:32:53 UTC
first one:
suicide@zoaHTPC /media/code $ dmd test.d -release -noboundscheck -O
test.d(55): Error: variable r used before set


another one:
suicide@zoaHTPC /media/code $ dmd test.d -release -noboundscheck -O -inline
/usr/include/phobos2/std/datetime.d(29600): Error: variable std.path.sep is depr
ecated
/usr/include/phobos2/std/datetime.d(29601): Error: variable std.path.sep is depr
ecated
/usr/include/phobos2/std/path.d(2715): Error: alias std.path.onOutOfMemoryError
is deprecated
Comment 2 SomeDude 2012-04-21 13:56:42 UTC
Please reduce your code to the minimum that shows the behaviour.
Comment 3 SomeDude 2012-04-21 14:17:06 UTC
I've reduced the test case to this:

import core.simd;
import std.conv;

 string gen(int D) {
        string s;
        s ~= "r = __simd(XMM.ADDPS, v1,v2);";
        return "res.ptr["~to!string(0)~"] = r.ptr["~to!string(0)~"];";
    }

T[D] simdAdd(T, int D)(T[D] a1, T[D] a2) {
    T[D] res;
    float4 r;
    mixin(gen(D));
    return res;
}

void main()
{
    float[8] v1 = [1,2,3,4,1,2,3,4];
    float[8] v2 = [1,2,3,4,1,2,3,4];
    simdAdd(v1,v2);
}

but I can't reproduce the error message, so maybe I've overlooked something.
However, interestingly, this compiles with -O flag and crashes the compiler without.

PS E:\DigitalMars\dmd2\samples> rdmd bug.d
Internal error: ..\ztc\cg87.c 1699
PS E:\DigitalMars\dmd2\samples> rdmd -O bug.d
PS E:\DigitalMars\dmd2\samples>
Comment 4 SomeDude 2012-04-21 14:19:31 UTC
(In reply to comment #3)
> I've reduced the test case to this:
> 
> ...
> 
> but I can't reproduce the error message, so maybe I've overlooked something.
> However, interestingly, this compiles with -O flag and crashes the compiler
> without.
> 
> PS E:\DigitalMars\dmd2\samples> rdmd bug.d
> Internal error: ..\ztc\cg87.c 1699
> PS E:\DigitalMars\dmd2\samples> rdmd -O bug.d
> PS E:\DigitalMars\dmd2\samples>

The ICE may be related to issue 7951.
Comment 5 kekeniro2 2014-01-22 18:54:54 UTC
(In reply to comment #0)
> When compiling this code with dmd -O it says:
> test.d(55) Error: variable r used before set
> i might have overlooked something, but shouldn't that compile?

I tried to test on Windows.
I suppose this is an _expected_ behavior.
Maybe, 'r.ptr[4]' and its followings lines should be 'r.ptr[0]' and so on.

Please help, SIMD expert... :)

==================================================================
Reduced Code:
//DMDFLAGS=-O -m64
import core.simd;

alias float[8] F8;
F8 simdAdd(F8 a1, F8 a2) {
    F8 res;
    float4 v1;
    float4 v2;
    float4 r;

    // 0..4 OK
    v1.array = a1[0..4];
    v2.array = a2[0..4];
    r = __simd(XMM.ADDPS, v1,v2);
    res.ptr[0] = r.ptr[0];
    res.ptr[1] = r.ptr[1];
    res.ptr[2] = r.ptr[2];
    res.ptr[3] = r.ptr[3];
    
    // 4..8 NG
    v1.array = a1[4..8];
    v2.array = a2[4..8];
    r = __simd(XMM.ADDPS, v1,v2);
    res.ptr[4] = r.ptr[4]; // Error: variable r used before set
    res.ptr[5] = r.ptr[5];
    res.ptr[6] = r.ptr[6];
    res.ptr[7] = r.ptr[7];
    
    return res;
}
Comment 6 Walter Bright 2016-04-25 05:51:48 UTC
    float4 r;
    ...
    res.ptr[4] = r.ptr[4]; // Error: variable r used before set

The error here is that there are only 4 entries in float4, and you're indexing the 5th one. If the function is marked @safe, it won't compile.

Not a compiler bug.
Comment 7 Walter Bright 2016-04-25 05:53:23 UTC
(In reply to SomeDude from comment #3)
> but I can't reproduce the error message, so maybe I've overlooked something.
> However, interestingly, this compiles with -O flag and crashes the compiler
> without.
> 
> PS E:\DigitalMars\dmd2\samples> rdmd bug.d
> Internal error: ..\ztc\cg87.c 1699

This works without error in the current compiler.