How to recognize the declaring code scopes of stack variables


If I want to know where the stack variables are declared? For example, whether it is declared within a loop or not? Like variables a[100] and temp.

int a[100];
for( int i = 0; i < N; i++){
int temp;

Can this be done in LLVM IR? Or should be implemented in Clang.



Clang can/now emits lifetime intrinsics to mark this information, I
believe - but I'm not sure if they'll suit your needs.

What are you trying to do with this information?

When parallelize the loop with OpenMP like models, I need to know what
variables will be shared among different threads.

I imagined you'd want to recognize loops, not scopes. Sounds like a
backend kind of optimization type thing, just detecting values that
don't escape the loop. (this would catch cases where a variable is
declared outside the loop but never actually used in such a way, eg:

int i;
for (...) {
  i = ...;

// no use of 'i' here

Hi, David
yes, it is similar to your description. And do you know any methods to do
this in LLVM IR?

I don't know the mid-level optimizers especially well - I doubt
there's a thing that does exactly what you need - but a combination of
existing passes/analyses might be able to tell you what you need.
Sorry I can't be more precise, it's just not my area. I imagine it's
possibly more a case of treating any values that don't enter phi's
after the loop as separate - and then ensuring that the values that do
enter phi's after the loop body are appropriately shared.

- David