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 #4 by k.hara.pg — 2014-05-25T15:03:55Z
Comment #5 by github-bugzilla — 2014-05-25T22:00:40Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/dce5083778cfb960eff8b3a3a754af74801266a0 fix Issue 12642 - Avoid some heap allocation cases for fixed-size arrays https://github.com/D-Programming-Language/dmd/commit/b5b5ecc0afde7c60a3ea1c27a03d75e828437068 Merge pull request #3582 from 9rnsr/fix_nogc Issue 12642 - Avoid some heap allocation cases for fixed-size arrays
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
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/f8c228cf20b656db91ef478b20a34d290888647b Additional issue 12642 fix for vector literal and @nogc https://github.com/D-Programming-Language/dmd/commit/d060bdb123b93bd69de95c4494bdbbc40260aa8b Merge pull request #3587 from 9rnsr/fix12642 Additional issue 12642 fix for vector literal and @nogc