<div dir="ltr"><div>Ah makes sense completely, I forgot about it being pass by value. Thanks again for clearing this up.<br></div><div><br></div><div>Also unrelated but: I just discovered LLVM has its own discord and I posted my question there first, is it popular? Or is mailing list still more responsive?<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Feb 23, 2021 at 7:18 PM Johannes Doerfert <<a href="mailto:johannesdoerfert@gmail.com">johannesdoerfert@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
On 2/23/21 6:08 PM, K Jelesnianski wrote:<br>
> Thank you so much for your reply!<br>
><br>
> I want my pass to run as late as possible to not be messed up by any of the<br>
> regular internal optimizations. I need a reference of i32 %b (regularly we<br>
> see this in IR as i32* %b) so if I understand you I think I need an<br>
> indirection.<br>
> In that case is it safe to just create an AllocaInst ? Does adding the<br>
> AllocaInst effect the way the data flows between functions?<br>
><br>
> The reason I am saying this is: Following my above example code, assuming I<br>
> am interested in the local variable "m" declared and set in main() (so a<br>
> memory slot dedicated for "m" is created in the stack, right?), "m" is<br>
> passed to foo(m), nothing is altered, "m" is further passed to bar().<br>
> If I created the AllocaInst in bar and assign %b to it, will I be creating<br>
> a new stack memory slot for bar's Argument "b" and will then I be passing<br>
> in the memory address of "b" (context of bar()) and not "m" (context of<br>
> main()).<br>
><br>
> I want to, if possible, pass the stack address of "m" (from the context of<br>
> main) to my profiling function call that is currently in the context of<br>
> bar().<br>
<br>
Your code *does not* pass the address of `m` in `main` to `foo`.<br>
It passes `m` by value to `foo`, thus the argument `b`is a copy<br>
of `m` if you want. You cannot recover the address of `m` in `foo`<br>
by any legal means. In fact, the optimizer will realize there is<br>
no need for stack allocations and put both `m` and `b` into (virtual)<br>
registers instead. If you want to trace a stack allocation, trace<br>
it early. If you "wait" and it is remove because it was not observed/<br>
needed, it's gone for good.<br>
<br>
For your code, change it to:<br>
<br>
void foo(int *bptr) { int b = *bptr; ... }<br>
void main(...) {<br>
   ...<br>
   *m = ...<br>
   ...<br>
   foo(&m);<br>
}<br>
<br>
and then you can profile the `i32* %bptr` argument of foo which is<br>
the "stack" address of `m` in `main`.<br>
<br>
~ Johannes<br>
<br>
<br>
<br>
> On Tue, Feb 23, 2021 at 6:19 PM Johannes Doerfert <<br>
> <a href="mailto:johannesdoerfert@gmail.com" target="_blank">johannesdoerfert@gmail.com</a>> wrote:<br>
><br>
>> If you want to access the `llvm::AllocaInst` generated by Clang for<br>
>> the argument you should run your pass early in the pipeline. If you<br>
>> just want to see the value passed to `bar`, take the `llvm::Argument`<br>
>> and pass it to your profiling function. If you need indirection for<br>
>> some reason, create a new `llvm::AllocaInst`, store the `llvm::Argument`<br>
>> you are interested in in it, and pass the `llvm::AllocaInst` to your<br>
>> function.<br>
>><br>
>> I hope this helps.<br>
>><br>
>> ~ Johannes<br>
>><br>
>><br>
>> On 2/23/21 3:19 PM, K Jelesnianski via llvm-dev wrote:<br>
>>> Dear LLVM mailing list,<br>
>>><br>
>>> I am currently having difficulty with figuring out how to get a function<br>
>>> parameter Value* when I am compiling with -O2. It seems -O2 performs an<br>
>>> optimization that optimizes out the local variable formed for the<br>
>> function<br>
>>> parameter.<br>
>>> I have a pass where I am inserting new function calls, and need to pass<br>
>> the<br>
>>> function parameters variable address. With -O0 this issue does not come<br>
>> up<br>
>>> as each function parameter when using -O0 gets its own AllocaInst and I<br>
>> can<br>
>>> leverage that Value* to pass to my new function call. I have tried to<br>
>>> leverage llvm.dbg information but it seems LLVM is not inserting<br>
>>> llvm.dbg.addr for me to use.<br>
>>><br>
>>> Below is the code example I am working with:<br>
>>> /*  example.c */<br>
>>> int fd_in;<br>
>>> char* ut_addr;<br>
>>><br>
>>> __attribute__((noinline)) void bar (int b){<br>
>>>          my_pass_function( &b );  // NEW (what I want to be done by my<br>
>> pass)<br>
>>>          ut_addr = (char*) mmap( 0, b, my_prot, MAP_PRIVATE, fd_in, 0);<br>
>>> }<br>
>>><br>
>>> __attribute__((noinline)) void foo (int f){<br>
>>>           bar( f );<br>
>>> }<br>
>>><br>
>>> int main(int argc, char** argv){<br>
>>>           int m;<br>
>>>           struct stat statbuf;<br>
>>>           char *input;<br>
>>><br>
>>>           if( (fd_in = open ( input, O_RDONLY)) < 0){        /* open the<br>
>>> input file */<br>
>>>                   return 0;<br>
>>>           }<br>
>>>           if( fstat( fd_in , &statbuf ) < 0 ){         /* find size of<br>
>> input<br>
>>> file */<br>
>>>                   return 0;<br>
>>>           }<br>
>>><br>
>>>           m = statbuf.st_size;<br>
>>>           foo( m );<br>
>>>           return 0;<br>
>>> }<br>
>>> -----------------------------------------------------------------<br>
>>> The original IR I get for bar() is below, as you can see the function<br>
>>> parameter is the "raw" value "i32 %b" passed into the function and not a<br>
>>> variable that I can leverage, get its Value* . Hence when I run my newly<br>
>>> instrumented code, I get a segmentation fault.<br>
>>><br>
>>> define void @bar(i32 %b) local_unnamed_addr #0 !dbg !35 {<br>
>>> entry:<br>
>>>     call void @llvm.dbg.value(metadata i32 %b, metadata !39, metadata<br>
>>> !DIExpression()), !dbg !44<br>
>>>     call void @llvm.dbg.value(metadata i64 1000, metadata !40, metadata<br>
>>> !DIExpression()), !dbg !44<br>
>>>     call void @llvm.dbg.value(metadata i32 1, metadata !43, metadata<br>
>>> !DIExpression()), !dbg !44<br>
>>>     %conv = sext i32 %b to i64, !dbg !45<br>
>>>     %0 = load i32, i32* @fd_in, align 4, !dbg !46, !tbaa !47<br>
>>>     %call = tail call i8* @mmap(i8* null, i64 %conv, i32 1, i32 2, i32 %0,<br>
>>> i64 0) #7, !dbg !51<br>
>>>     store i8* %call, i8** @ut_addr, align 8, !dbg !52, !tbaa !53<br>
>>>     ret void, !dbg !55<br>
>>> }<br>
>>><br>
>>><br>
>>> _______________________________________________<br>
>>> LLVM Developers mailing list<br>
>>> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
>>> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>