January 03, 2021 Re: alloca is slow and dangerous | ||||
---|---|---|---|---|
| ||||
Posted in reply to welkam | On Sunday, 3 January 2021 at 20:59:00 UTC, welkam wrote:
>> MS has already stated that they want to do this in C++.
> Not only MS but also linux people want default initialize everything so they both put effort in compilers to improve optimizations so they dont pay performance penalty where they should not. And in the end we benefit too.
The standard approach in PL design is to either default initialize or to statically establish that variables are not used until initialized (better, but more tricky). C++ was just a small addition to C, so that is where the semantics come from. C is the outlier here, not the norm...
|
January 04, 2021 Re: alloca is slow and dangerous | ||||
---|---|---|---|---|
| ||||
Posted in reply to welkam | On 1/3/21 2:15 PM, welkam wrote:
> On Friday, 1 January 2021 at 17:55:33 UTC, Steven Schveighoffer wrote:
>> D has alloca. It's in core.std.stdlib
>>
>> https://dlang.org/phobos/core_stdc_stdlib.html#.alloca
>>
> Does it work on all compilers and all platforms?
I don't know. I would expect it to work anywhere D is supported. Looking at the code, DMD supports it, with GNU it's an intrinsic, and with LDC it's given a pragma to tag it (presumably so it can be recognized as an intrinsic).
Are there other compilers or platforms that work with D besides DMD, LDC, and GDC? Are there some GDC or LDC platforms that don't support alloca? If so, it's possible that some don't support it. But it's not versioned that way in the code.
-Steve
|
January 04, 2021 Re: alloca is slow and dangerous | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Monday, 4 January 2021 at 15:02:32 UTC, Steven Schveighoffer wrote:
> Are there other compilers or platforms that work with D besides DMD, LDC, and GDC? Are there some GDC or LDC platforms that don't support alloca? If so, it's possible that some don't support it. But it's not versioned that way in the code.
>
There's no such thing as a platform that doesn't support alloca as far as I'm aware. As for C compilers that don't support alloca, they are niche and few.
|
January 04, 2021 Re: alloca is slow and dangerous | ||||
---|---|---|---|---|
| ||||
Posted in reply to welkam | On Sunday, 3 January 2021 at 19:15:05 UTC, welkam wrote:
> On Friday, 1 January 2021 at 17:55:33 UTC, Steven Schveighoffer wrote:
>> D has alloca. It's in core.std.stdlib
>>
>> https://dlang.org/phobos/core_stdc_stdlib.html#.alloca
>>
> Does it work on all compilers and all platforms?
Why wouldn't it?
In C/C++/D/... language land, there is no such thing as "_the_ stack". Yeah, the _compiler_ may decide to use the special instructions/register of the CPU that address "the stack", but there is no guarantee it will; nor is there a guarantee that the binary executable with CPU stack instructions will actually use the stack and not just dynamically allocate things. There are CPUs that do not have such special instructions, and there are platforms that do not use a "stack" in the common interpretation of the word to execute a D program.
Examples: Webassembly, an executable running with ASan's FakeStack enabled, a binary running in an emulator, ...
LDC emits the same LLVM IR "alloca" instruction for local variables (`int i;`) as for `alloca` function calls. Simplified: if you can write `int i;` for your platform, `core.stdc.stdlib.alloca` also works. ;)
-Johan
|
January 04, 2021 Re: alloca is slow and dangerous | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johan | On Monday, 4 January 2021 at 19:38:48 UTC, Johan wrote:
> On Sunday, 3 January 2021 at 19:15:05 UTC, welkam wrote:
>> [...]
>
> Why wouldn't it?
>
> In C/C++/D/... language land, there is no such thing as "_the_ stack". Yeah, the _compiler_ may decide to use the special instructions/register of the CPU that address "the stack", but there is no guarantee it will; nor is there a guarantee that the binary executable with CPU stack instructions will actually use the stack and not just dynamically allocate things. There are CPUs that do not have such special instructions, and there are platforms that do not use a "stack" in the common interpretation of the word to execute a D program.
> Examples: Webassembly, an executable running with ASan's FakeStack enabled, a binary running in an emulator, ...
>
> LDC emits the same LLVM IR "alloca" instruction for local variables (`int i;`) as for `alloca` function calls. Simplified: if you can write `int i;` for your platform, `core.stdc.stdlib.alloca` also works. ;)
>
> -Johan
Nothing D will ever be used on will have this issue but I think some niche architectures don't have alloca a la C. I've seen some VLIW that can't use it as easily but I can't remember which one's exactly
|
January 06, 2021 Re: alloca is slow and dangerous | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johan | On 2021-01-04 20:38, Johan wrote: > there are platforms that do not use a "stack" in the common interpretation of the word to execute a D program. > Examples: Webassembly, an executable running with ASan's FakeStack enabled, a binary running in an emulator, ... You can always implement a stack on the heap ;) -- /Jacob Carlborg |
Copyright © 1999-2021 by the D Language Foundation