3822 – Invalid optimization of alloca called with constant size

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3822 - Invalid optimization of alloca called with constant size
Summary: Invalid optimization of alloca called with constant size
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 critical
Assignee: yebblies
URL:
Keywords: pull, wrong-code
Depends on:
Blocks:
Reported: 2010年02月18日 11:17 UTC by bearophile_hugs
Modified: 2015年06月09日 05:11 UTC (History)
5 users (show)

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010年02月18日 11:17:11 UTC
import std.stdio: printf;
import std.c.stdlib: alloca;
void main() {
 const int n = 8;
 for (int i; i < 2; i++)
 printf("%p\n", alloca(n));
}
It prints two times the same address, I don't know why, I think this can be wrong.
Comment 1 BCS 2010年02月18日 12:03:35 UTC
I've never used alloca so I'm not sure, so this is a guess:
alloca does stack allocation and the body of the for statement forms a scope on the stack (this in this case contains no named variables). I'm guessing that when that scope is exited, the allocation automatically gets deallocated.
Comment 2 bearophile_hugs 2010年02月18日 12:32:38 UTC
(In reply to comment #1)
> I've never used alloca so I'm not sure, so this is a guess:
> 
> alloca does stack allocation and the body of the for statement forms a scope on
> the stack (this in this case contains no named variables). I'm guessing that
> when that scope is exited, the allocation automatically gets deallocated.
You can be right, thank you. Then it's very good for Phobos docs to say that alloca is relative to a scope and not to a function.
The description of alloca() that I have seen says:
The alloca() function allocates space in the stack frame of the caller, and returns a pointer to the allocated block. This temporary space is automatically freed when the function from which alloca() is called returns.
While if you are right D alloca frees space when the scope of alloca ends and not when the function ends.
Comment 3 bearophile_hugs 2010年06月07日 04:04:04 UTC
Maybe the alloca() used by dmd frees memory as soon as the current scope is left, instead of deferring all deallocation until function exit. See:
http://compilers.iecc.com/comparch/article/91-12-079
D documentation has to explain how exactly its alloca() works.
Comment 4 nfxjfg 2010年06月07日 04:17:53 UTC
C code that compiles in D without modification should work exactly as it does in C.
This means this is a rather bad code gen bug.
Comment 5 nfxjfg 2010年06月26日 14:40:47 UTC
There's D code in druntime that assumes memory allocated by alloca() is valid until the end of the function:
http://dsource.org/projects/druntime/browser/trunk/src/rt/adi.d#L242
Maybe the codegen for alloca() within loops is broken, or something.
Comment 6 yebblies 2012年02月10日 22:08:28 UTC
The issue here is that n is a compile-time constant, so the call to alloca is optimized away completely, and always reserving the extra space. This optimization is not valid if the call to alloca might be repeated.
Comment 7 yebblies 2012年02月10日 22:29:13 UTC
https://github.com/D-Programming-Language/dmd/pull/707 
Comment 8 bearophile_hugs 2012年02月11日 04:12:35 UTC
(In reply to comment #7)
> https://github.com/D-Programming-Language/dmd/pull/707 
Thank you for your patch, yebblies!
Comment 9 yebblies 2012年02月11日 04:36:09 UTC
(In reply to comment #8)
> Thank you for your patch, yebblies!
You're welcome.
Comment 10 github-bugzilla 2012年02月18日 23:17:48 UTC
Commit pushed to master at https://github.com/D-Programming-Language/dmd
https://github.com/D-Programming-Language/dmd/commit/3fc9ee7064c05d4c8db6f1aac0a8896caa954e44
Merge pull request #707 from yebblies/issue3822
Issue 3822 - Invalid optimization of alloca called with constant size
Comment 11 github-bugzilla 2012年02月18日 23:44:49 UTC
Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd
https://github.com/D-Programming-Language/dmd/commit/c492323b6edc44a2bac4ea0edaed1e757854b9d0
fix Issue 3822 - Invalid optimization of alloca called with constant size


AltStyle によって変換されたページ (->オリジナル) /