public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug d/95174] New: [D] Incorrect compiled functions involving const fixed size arrays
@ 2020-05-17 12:07 witold.baryluk+gcc at gmail dot com
  2020-05-18 14:12 ` [Bug d/95174] " ibuclaw at gdcproject dot org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: witold.baryluk+gcc at gmail dot com @ 2020-05-17 12:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95174

            Bug ID: 95174
           Summary: [D] Incorrect compiled functions involving const fixed
                    size arrays
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: d
          Assignee: ibuclaw at gdcproject dot org
          Reporter: witold.baryluk+gcc at gmail dot com
  Target Milestone: ---

https://explore.dgnu.org/z/LppySp


```
void f(immutable(float[64]) x, float[64] o) {
   o[] = x[] * 2.0f;
}
```

and

```
void f(immutable(float[64]) x, float[64] o) {
    foreach (i; 0 .. 64) {
        o[i] = x[i] * 2.0f;
    }
}
```

and

```
void f(immutable(float[64]) x, float[64] o) {
    o[1] = x[5] + x[7];
}

```

Is incorrectly compiled to 'nop; ret'


It appears DMD (v2.092) also essentially do the same, and do not perform any
computations in the function.

LDC2 (1.20.1, based on DMD v2.090.1) does generate correct code in some cases
(fully unrolled and fully vectorized in this specific case), but in some other
also do nothing and simply does 'ret' in the function.



As a bonus:

```
void fffff(immutable(float[4]) x, float[4] o) {
   o[2] = x[1] + x[3];
}

import std.stdio : writeln;

void main() {
  immutable(float[4]) k = [7.0f, 5.3f, 1.2f, 3.2f];
  float[4] o;
  fffff(k, o);
  writeln(o);
}

```

prints '[nan, nan, nan, nan]', but it should: '[nan, nan, 8.5, nan]'.


I got the same results using my local gdc version 10.1.0 on amd64.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug d/95174] [D] Incorrect compiled functions involving const fixed size arrays
  2020-05-17 12:07 [Bug d/95174] New: [D] Incorrect compiled functions involving const fixed size arrays witold.baryluk+gcc at gmail dot com
@ 2020-05-18 14:12 ` ibuclaw at gdcproject dot org
  2020-05-19  1:02 ` witold.baryluk+gcc at gmail dot com
  2020-05-19  3:10 ` ibuclaw at gdcproject dot org
  2 siblings, 0 replies; 4+ messages in thread
From: ibuclaw at gdcproject dot org @ 2020-05-18 14:12 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95174

Iain Buclaw <ibuclaw at gdcproject dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #1 from Iain Buclaw <ibuclaw at gdcproject dot org> ---
(In reply to Witold Baryluk from comment #0)
> https://explore.dgnu.org/z/LppySp
> 
> 
[--snip--]
> 
> Is incorrectly compiled to 'nop; ret'
> 

It is correct, all those functions do nothing.  What you're missing is a 'ref'
or 'out' storage class on the 'o' parameter.

Rationale, static arrays are value types in D (they don't saturate to pointers
like in C).

See https://dlang.org/spec/arrays.html#static-arrays

> 
> As a bonus:
> 
> ```
> void fffff(immutable(float[4]) x, float[4] o) {
>    o[2] = x[1] + x[3];
> }
> 
> import std.stdio : writeln;
> 
> void main() {
>   immutable(float[4]) k = [7.0f, 5.3f, 1.2f, 3.2f];
>   float[4] o;
>   fffff(k, o);
>   writeln(o);
> }
> 
> ```
> 
> prints '[nan, nan, nan, nan]', but it should: '[nan, nan, 8.5, nan]'.
> 
> 

All NaNs is the correct result here too.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug d/95174] [D] Incorrect compiled functions involving const fixed size arrays
  2020-05-17 12:07 [Bug d/95174] New: [D] Incorrect compiled functions involving const fixed size arrays witold.baryluk+gcc at gmail dot com
  2020-05-18 14:12 ` [Bug d/95174] " ibuclaw at gdcproject dot org
@ 2020-05-19  1:02 ` witold.baryluk+gcc at gmail dot com
  2020-05-19  3:10 ` ibuclaw at gdcproject dot org
  2 siblings, 0 replies; 4+ messages in thread
From: witold.baryluk+gcc at gmail dot com @ 2020-05-19  1:02 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95174

--- Comment #2 from Witold Baryluk <witold.baryluk+gcc at gmail dot com> ---
Doh. Of course. My bad. Sorry.


static arrays are value type, dynamic arrays are reference type.

Changing signature to:

```
void f(immutable(float[64]) x, float[] o);
```


solves the problem.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug d/95174] [D] Incorrect compiled functions involving const fixed size arrays
  2020-05-17 12:07 [Bug d/95174] New: [D] Incorrect compiled functions involving const fixed size arrays witold.baryluk+gcc at gmail dot com
  2020-05-18 14:12 ` [Bug d/95174] " ibuclaw at gdcproject dot org
  2020-05-19  1:02 ` witold.baryluk+gcc at gmail dot com
@ 2020-05-19  3:10 ` ibuclaw at gdcproject dot org
  2 siblings, 0 replies; 4+ messages in thread
From: ibuclaw at gdcproject dot org @ 2020-05-19  3:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95174

--- Comment #3 from Iain Buclaw <ibuclaw at gdcproject dot org> ---
(In reply to Witold Baryluk from comment #2)
> Doh. Of course. My bad. Sorry.
> 
> 
> static arrays are value type, dynamic arrays are reference type.
> 


Dynamic arrays are still value types, the value passed around is a pointer and
length. For example:

void f(immutable(float[64]) x, float[] o) {
   o[] = x[] * 2.0f;
}

float[64] x = 1.0f;
float[] o;
f(x, o);
// o does not change here.
assert(o.length == 0);
assert(o.ptr is null);

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-05-19  3:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-17 12:07 [Bug d/95174] New: [D] Incorrect compiled functions involving const fixed size arrays witold.baryluk+gcc at gmail dot com
2020-05-18 14:12 ` [Bug d/95174] " ibuclaw at gdcproject dot org
2020-05-19  1:02 ` witold.baryluk+gcc at gmail dot com
2020-05-19  3:10 ` ibuclaw at gdcproject dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).