<div dir="ltr"><div><div><div>Hi Hal,<br><br></div>Thank you for your email, that makes a lot of sense to me. I am working on some tools to use memory profiling to speculatively replace memory loads and stores with value forwarding in hardware implementations. I'd like to compare the profiled data to static alias analysis, so it would be super useful if there was a way to answer the questions about aliasing across backedges that AliasAnalysis considers meaningless. Perhaps the MemoryDependence would allow this?<br>
<br></div>Thank you,<br></div>Jeremy<br><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Aug 20, 2014 at 11:58 PM, Hal Finkel <span dir="ltr"><<a href="mailto:hfinkel@anl.gov" target="_blank">hfinkel@anl.gov</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">----- Original Message -----<br>
> From: "Jeremy Salwen" <<a href="mailto:jeremysalwen@gmail.com">jeremysalwen@gmail.com</a>><br>
> To: "Daniel Berlin" <<a href="mailto:dberlin@dberlin.org">dberlin@dberlin.org</a>><br>
> Cc: "llvmdev" <<a href="mailto:llvmdev@cs.uiuc.edu">llvmdev@cs.uiuc.edu</a>><br>
> Sent: Wednesday, August 20, 2014 7:58:46 PM<br>
> Subject: Re: [LLVMdev] Alias Analysis Semantics<br>
><br>
> Hi Daniel,<br>
><br>
> Sorry for taking so long to respond. I spoke with a colleague more<br>
> familiar with llvm who thought he could clear up my confusion, but<br>
> we both came out of the conversation confused. I will try my best to<br>
> explain the ambiguity.<br>
><br>
> In an DAG, alias queries would be completely unambiguous. Every<br>
> instruction would only be executed once, and every SSA value really<br>
> would have a single static assignment. Any instruction would have a<br>
> single value per execution, and when you ask alias(x,y) it would be<br>
> a question of whether those single memory addresses overlapped.<br>
><br>
> However, once backedges are introduced, and instruction can be<br>
> executed multiple times. Every time the execution passes over a<br>
> backedge, any instructions that are executed again, may have<br>
> completely different values. Now, when you ask alias(x,y), it's<br>
> ambiguous which pairs of addresses that query considers.<br>
><br>
> In some cases, it seems clear. For example, in the loops we have been<br>
> talking about, it seems like you are saying that it only compares<br>
> the values in the same iteration, but does not say anything about<br>
> aliasing between iterations. These are perfectly reasonable<br>
> semantics for loops, but it does not define what the alias queries<br>
> mean in general. For example, consider the following CFG:<br>
><br>
><br>
><br>
</div></div><div class="">> If we ask a query alias(x,y) about instructions in basic blocks B and<br>
> C respectively, what will those answers mean? There is always a<br>
> backedge between their executions so are they considered to be<br>
> different iterations and therefore they never alias?<br>
<br>
</div>Perhaps Danny will have a better answer, but I think about it this way: AliasAnalysis only answers queries about variable relationships at points along the dynamic execution path of the program. For a query to be well formed, there much be some point (some instruction in the LLVM IR) at which both values being queried are simultaneously live. In your example, there is no block dominated by both B and C, and so no point in the program where it would be legal to refer to both x and y. As a result, the query itself is meaningless. Do you have some transformation or analysis in mind that would issue such a query?<br>
<br>
-Hal<br>
<div class="HOEnZb"><div class="h5"><br>
><br>
><br>
> I get that there may be some way of understanding the alias queries<br>
> in which "dynamic instructions" do not matter to the answer, but<br>
> whatever that way is, it must also be possible to use that way to<br>
> explain things in terms of the "dynamic instructions" that the CPU<br>
> ends up executing. And at least for me that would resolve any<br>
> confusion I have about the AliasAnalysis.<br>
><br>
><br>
> Thank you again for your time,<br>
><br>
> Jeremy<br>
><br>
><br>
><br>
><br>
> On Thu, Aug 14, 2014 at 10:46 AM, Daniel Berlin < <a href="mailto:dberlin@dberlin.org">dberlin@dberlin.org</a><br>
> > wrote:<br>
><br>
><br>
><br>
> Let me take your previous example in an attempt to hopefully explain<br>
> why "dynamic instructions" do not matter to the answer:<br>
> You asked about:<br>
><br>
><br>
> > std::vector<int> A(100);<br>
> >> int* x,y;<br>
> >> x=&A[0];<br>
> >><br>
> >> for(int i=0; i<100; i++) {<br>
> >> y=&A[i];<br>
> >> *y=*x;<br>
> >> x=&A[i+1];<br>
> >> }<br>
> ><br>
><br>
><br>
><br>
> This is not what it looks like in LLVM.<br>
><br>
><br>
> In LLVM, it looks like this:<br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
> std::vector<int> A(100);<br>
><br>
> int* x,y;<br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
> x_1=GEP A, 0, 0<br>
><br>
><br>
><br>
> for(int i=0; i<100; i++) {<br>
> i_2 = phi (0, i_3)<br>
> x_2 = phi(x_1, x_3)<br>
> y_1 = GEP A, 0, i_2<br>
> temp = load x_2<br>
> store y_1, temp<br>
> temp2 = add i_2, 1<br>
> x_3 = GEP A, 0, temp2<br>
> i_3 = add i_2, 1<br>
> }<br>
><br>
><br>
> As you can see, every time you redefine the value of the pointer x to<br>
> a new value, it gets a new name.<br>
><br>
><br>
> alias(x_1, y_1) = mayalias<br>
> alias(x_2, y_1) = mustalias<br>
> alias(x_3, y_1) = mayalias<br>
><br>
><br>
><br>
><br>
> The only interesting question, IMHO, is whether alias(x_3,y_1) is<br>
> MayAlias or NoAlias.<br>
> x_2 clearly has the same value as y_1 at all times throughout the<br>
> program.<br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
> On Thu, Aug 14, 2014 at 6:54 AM, Daniel Berlin < <a href="mailto:dberlin@dberlin.org">dberlin@dberlin.org</a><br>
> > wrote:<br>
><br>
><br>
><br>
><br>
> On Thu, Aug 14, 2014 at 6:37 AM, Daniel Berlin < <a href="mailto:dberlin@dberlin.org">dberlin@dberlin.org</a><br>
> > wrote:<br>
> > On Wed, Aug 13, 2014 at 8:35 PM, Jeremy Salwen <<br>
> > <a href="mailto:jeremysalwen@gmail.com">jeremysalwen@gmail.com</a> > wrote:<br>
> >> Hey Daniel,<br>
> >><br>
> >> Thanks again for the help. I'm still a bit confused about the<br>
> >> interface to<br>
> >> the alias analysis. It seems like we are talking about different<br>
> >> interfaces.<br>
> > Sorry, yes, i was thinking of a different interface.<br>
> ><br>
> >> Has it changed from what the documentation says? As far as I<br>
> >> can tell, the documentation takes a specific Value*, and no<br>
> >> information<br>
> >> about which dynamic execution it is talking about.<br>
> ><br>
> > The actual standard low level interface is:<br>
> ><br>
> > virtual AliasResult alias(const Location &LocA, const Location<br>
> > &LocB);<br>
> > (There is a value wrapper for this)<br>
> ><br>
> > I think part of the confusion here is that you keep redefining<br>
> > pointers and expecting the results of queries to change.<br>
> ><br>
> > Outside of globals, LLVM is in SSA form. Thus, these are queries<br>
> > will<br>
> > end up being about specific pointers that will only be defined<br>
> > once.<br>
> > In all of your examples, you have asked about local variables.<br>
> > These<br>
> > local variables will be put in SSA. Every time you redefine the<br>
> > pointer value, it will generate a new definition that is separate<br>
> > from<br>
> > the old ones.<br>
> > If you transform your examples above into globals, you may get<br>
> > different answers.<br>
> ><br>
> ><br>
> ><br>
> >><br>
> >> When you say "Right. It's a query about specific loads and<br>
> >> stores." it<br>
> >> sounds like you are saying that it does consider dynamic<br>
> >> executions, and<br>
> >> that you can make queries about specific dynamic executions. I<br>
> >> can't seem<br>
> >> to find this option anywhere in the API..<br>
> > Yes, sorry, I was thinking of the MemoryDependence interface, which<br>
> > does give you that information.<br>
> > AliasAnalysis does not.<br>
> ><br>
> >><br>
> >> Perhaps someone else could clear the confusion, because I feel<br>
> >> like there is<br>
> >> a gap of communication.<br>
> ><br>
> ><br>
> >><br>
> >> For example, in this code what would the result be for the first<br>
> >> store and<br>
> >> the first load?<br>
> ><br>
> >><br>
> >>> #include <cstdio><br>
> >>> #include <cstdlib><br>
> >>> int main(int c, char** argv) {<br>
> >>> int* A=(int*)malloc(100*sizeof(int));<br>
> >>><br>
> >>> if(c>1) {<br>
> >>> for(int i=0; i<5; i++) {<br>
> >>> A[i]=5;<br>
> >>> }<br>
> >>> } else {<br>
> >>> A[1]=5;<br>
> >>> }<br>
> >>> printf("%d\n",A[4]);<br>
> >>> free(A);<br>
> >>> }<br>
> >><br>
> >><br>
> ><br>
> >> MustAlias? Because whenever they are executed, they do alias. Or<br>
> >> MayAlias?<br>
> > Since you seem to be having trouble going all the way down the<br>
> > rabbit<br>
> > hole here, let me take a different approach.<br>
> ><br>
> > In this example, A[i] is not an LLVM location or value *.<br>
> > You will never see A[i] in the LLVM IR to query about.<br>
> > You will see two pointers as this:<br>
> > %i.0 = phi i32 [ 0, %4 ], [ %11, %10 ]<br>
> > %8 = sext i32 %i.0 to i64<br>
> > %9 = getelementptr inbounds i32* %2, i64 %8<br>
> > store i32 5, i32* %9, align 4<br>
> ><br>
> > and<br>
> > %16 = getelementptr inbounds i32* %2, i64 4<br>
> > %17 = load i32* %16, align 4<br>
> ><br>
> > The query will be alias(%9, %16)<br>
> ><br>
><br>
><br>
> > In this case, it cannot prove it is MustAlias, but it is actually<br>
> > MustAlias.<br>
> > It will, however, return MayAlias because it is not powerful enough<br>
> > to prove it.<br>
><br>
> Sigh, too early in the morning, pasted the right output, wrote the<br>
> answer while looking at the wrong pass output.<br>
> These two pointers will always be MayAlias.<br>
> LLVM later actually deletes the loop, and transforms the above into a<br>
> memset + a load from memset, and ends up with two mustalias pointers<br>
> in that case.<br>
><br>
><br>
><br>
</div></div><div class="HOEnZb"><div class="h5">> _______________________________________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a> <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
><br>
<br>
</div></div><span class="HOEnZb"><font color="#888888">--<br>
Hal Finkel<br>
Assistant Computational Scientist<br>
Leadership Computing Facility<br>
Argonne National Laboratory<br>
</font></span></blockquote></div><br></div>