What should happen here?

Johan j at j.nl
Wed Sep 22 18:16:37 UTC 2021


On Wednesday, 22 September 2021 at 12:31:48 UTC, Steven 
Schveighoffer wrote:
> On 9/22/21 8:12 AM, Kagamin wrote:
>> On Monday, 20 September 2021 at 18:49:12 UTC, Steven 
>> Schveighoffer wrote:
>>> This to me seems like "leaving a pointer to it on the stack". 
>>> I'm not sure how else I would do that specifically? Plus, 
>>> this option is the only "free" one -- the others all require 
>>> much more complication. Adding a pointer to the stack is 
>>> free. It's just, I don't know how to tell the compiler to do 
>>> that besides declaring it.
>> 
>> If you use the pointer after the call, that's an easy way to 
>> ensure that the pointer is kept around long enough.
>
> And by the way I tried naive usage, and the compiler saw right 
> through that:
>
> ```d
> auto c = new C;
> scope(exit) auto fake = c; // still collected early
> ```

I think anything that is (close to) zero-overhead is what the 
optimizer understands and is therefore not going to get the 
behavior that you want, besides an intrinsic to tell the 
optimizer to keep that pointer value alive in some storage that 
is scanned by GC (reachable memory or registers).

In the absence of such intrinsic [*], what you can do is pass the 
value to something about which we explicitly tell the optimizer 
that it does not understand it. Cryptic? ;)

https://d.godbolt.org/z/M3zbzK4sq
```
    import ldc.llvmasm;
    __asm("", "r", c);
```
Probably this is also expressible in the new inline assembly that 
both GDC and LDC support.

-Johan

[*] 
https://lists.llvm.org/pipermail/llvm-dev/2016-July/102322.html  
Where it popped up in debuggability considerations.


More information about the Digitalmars-d mailing list