Warning about variable optimized away?


it it possible to get a kind of diagnostics if the variable
is totally optimized away? For example, in:

void foo (struct some_type *obj) {
   ... some code where 'obj' is not used ...
   bar (obj->some_member);
   ... some code where 'obj' is not used again ...
   baz (obj->some_member);

'obj' is likely to be optimized away so only 'obj->some_member' really
exists (in a register or stack location). Getting diagnostics
or preserving 'obj' may be important if there is a GC which scans
C stack and registers conservatively - if there is no direct reference
to 'obj', it's likely to be reclaimed and so 'obj->some_member'
becomes garbage.


This isn’t really feasible.

First, you are asking for a warning from LLVM’s optimizers, which we strive to avoid, as they are unstable and affected by optimization levels.


In general, you can not safely make assumptions about your optimizer preserving values. This is fundementally unsafe and is ill-advised.

For your particular example, you have a derived pointer which is live and a base pointer which is not. Have you confirmed that the derived value (&(obj->some_member)) is available? I would expect it to be. Given that, your GC has (most of) the information it needs. Conservative GCs typically consider interior pointers to objects (i.e. something which is likely a derived pointer) to be a pointer to the object for exactly this reason. Now, you can run into cases where the compiler creates a temporary pointer outside of the object (or worse, into another), but these are rare in practice. Can I ask what GC library you're using? To my knowledge, most of the major ones handle interior pointers just fine.

If you're interested in pursuing a non-conservative root scanning policy, I'd suggest you look at the statepoint intrinsics implemented here: https://github.com/AzulSystems/llvm-late-safepoint-placement