Bug 12642 – Avoid some heap allocation cases for fixed-size arrays
Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-04-25T10:46:00Z
Last change time
2014-05-26T10:11:59Z
Keywords
pull, rejects-valid
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2014-04-25T10:46:44Z
The @nogc annotation of dmd 2.066alpha refuses the following programs:
__gshared int[1] data1;
int[1] bar() @nogc {
int x;
return [x];
}
void main() @nogc {
int x;
data1 = [x];
int[1] data2;
data2 = [x];
}
test.d(4,12): Error: array literals in @nogc function bar may cause GC allocation
test.d(8,13): Error: array literals in @nogc function main may cause GC allocation
test.d(10,13): Error: array literals in @nogc function main may cause GC allocation
But is it possible to avoid heap allocations in those cases, to allow those programs to compile?
This similar code compiles:
__gshared int[1] data1;
int[1] bar() @nogc {
int x;
typeof(return) tmp;
tmp[0] = x;
return tmp;
}
void main() @nogc {
int x;
data1[0] = x;
int[1] data2;
data2[0] = x;
}
Comment #1 by k.hara.pg — 2014-04-25T11:10:40Z
If you remove @nogc annotation, all array literals will be allocated on stack.
So this is pure front-end issue, and may be fixed easily.
Comment #2 by bearophile_hugs — 2014-04-26T11:25:22Z
Comment #3 by bearophile_hugs — 2014-04-27T13:51:10Z
Another comment by Timon Gehr:
> The front end already distinguishes dynamic and static array literals
> (in a limited form), this distinction should simply carry through to
> code generation and static array literals should be allowed in @nogc code.
Comment #6 by bearophile_hugs — 2014-05-26T05:25:04Z
There is one more missing case, is this worth opening another ER, or not?
import core.simd;
ulong2 foo() @nogc {
return [0, 0];
}
void main() {}
test.d(3,12): Error: array literal in @nogc function foo may cause GC allocation
Comment #7 by k.hara.pg — 2014-05-26T08:38:41Z
(In reply to bearophile_hugs from comment #6)
> There is one more missing case, is this worth opening another ER, or not?
>
>
> import core.simd;
> ulong2 foo() @nogc {
> return [0, 0];
> }
> void main() {}
>
>
> test.d(3,12): Error: array literal in @nogc function foo may cause GC
> allocation
https://github.com/D-Programming-Language/dmd/pull/3587
Comment #8 by github-bugzilla — 2014-05-26T09:27:46Z