<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div>I think we’re talking cross-purposes at this point.  Let me try to step back and concisely restate what I’m trying to do:</div><div><br></div><div>- I am writing a frontend for a high-level language and I emit LLVM IR.</div><div>- I control the TBAA tree.  I don’t use it to express the high-level language’s types, since that would make absolutely no sense.  JavaScript doesn’t have types.  But by the time my compiler is generating LLVM IR, it will have come up with meaningful constraints on aliasing and those constraints are naturally expressed using TBAA.</div><div>- Some calls that I emit in LLVM IR are neither readnone/readonly nor do they clobber the world - instead they clobber a known set of memory locations, and that set of memory locations is already expressible in the TBAA representation that I control.</div><div><br></div><div>Which part of the above do you disagree with?</div><div><br></div><div><div>On Oct 7, 2013, at 4:57 PM, Daniel Berlin <<a href="mailto:dberlin@dberlin.org">dberlin@dberlin.org</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">On Mon, Oct 7, 2013 at 4:22 PM, Filip Pizlo <<a href="mailto:fpizlo@apple.com">fpizlo@apple.com</a>> wrote:<br><blockquote type="cite"><br><br>On Oct 7, 2013, at 3:49 PM, Daniel Berlin <<a href="mailto:dberlin@dberlin.org">dberlin@dberlin.org</a>> wrote:<br><br><br><br><br>On Wed, Sep 25, 2013 at 5:50 PM, Filip Pizlo <<a href="mailto:fpizlo@apple.com">fpizlo@apple.com</a>> wrote:<br><blockquote type="cite"><br>Hi all,<br><br>TBAA on loads and stores is super useful for conveying high-level type knowledge to LLVM transformations.  But often translating a high-level language into LLVM IR will result in runtime calls that are known at the high level to only affect (either by reading or by writing) some restricted subset of the heap.  These aren't read-only calls; that is often too strong of a guarantee.  A great example would be a GC barrier or GC allocation that may arise if LLVM is used as a backend for a compiler for a higher-level language.  There are probably other examples that arise in high-level languages, but let's use allocation as an example for now.  I can envision two ways of representing allocation in LLVM IR:<br><br>Method #1:<br><br>           %object = call @runtime_allocate(... /* pass size and/or other data */)<br><br>Method #2:<br><br>       SomeBlock:<br>           ... ; code preceding the allocation<br>           %objectFast = ... ; IR for the inlined allocation fast path, which produces %objectFast but may yield null if the fast path fails and we need to take slow path<br>           %didFail = icmp eq %objectFast, null<br>           br %didFail, label %AllocationSlowPath, label &Continue<br>       AllocationSlowPath:<br>           %objectSlow = call @runtime_allocate_slow_path(... /* pass size and/or other data */)<br>           br label %Continue<br>       Continue:<br>           %object = phi [%objectFast, SomeBlock], [%objectSlow, AllocationSlowPath]<br><br>In both of these cases, the call instruction will appear to clobber the world leading to more pessimistic optimization.  Moreover, it's not always correct to mark the allocation as being readonly; it may read or write observable state.  But if you don't like the allocation example then you can imagine that a GC store barrier would have an almost identical pattern to #2 and it would definitely not be readonly.  Anyway, such a pattern would lead to fewer loads being eliminated or hoisted, fewer stores being eliminated or sunk, etc - all due to those pesky calls.  But the high-level code generator that produces this IR will know for sure that @runtime_allocate or @runtime_allocate_slow_path can only affect a very narrow subset of the heap: namely, it will only read and write the GC's data structures.  So most of the loads and stores surrounding the allocation, that arose from source-level loads and stores, would definitely not have any interference with the call and this would !<br>be easy to convey using TBAA.<br><br>Hence if we could decorate the calls to these runtime functions as only reading or writing certain TBAA types,<br></blockquote><br><br>Out of curiosity, why is this tied to TBAA types, rather than variables or something else?<br>IE what analysis are you performing that gives you type answers instead of answers in terms of local/global variables?<br><br><br>TBAA’s utility for non-C languages (like JavaScript, in my case) isn’t for reasoning about user-facing types, but for creating what you could alternatively think of as an “abstract heaps”.  TBAA is perfect for this.<br></blockquote><br><br>No. A nested hierarchy that describes conflicts among possible<br>abstract heap locations is perfect for this. TBAA is not quite the<br>same. See below.<br><br><blockquote type="cite"><br>For example I might know that a certain function clobbers a field called ‘foo’.  In my frontend I create an "abstract heap" for each field name, and this gets lowered to LLVM TBAA.<br></blockquote><br><br>So let's start simple.<br><br>You are representing an abstract set of heap locations. Are they<br>actually in any way related to the *existing type tree* that is being<br>created for TBAA?</div></blockquote><div><br></div><div>Yes.  Because I am also creating the TBAA type tree.  I control it entirely.</div><br><blockquote type="cite"><div style="font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">Or are you adding new values to it that are not<br>really part of a type hierarchy at all, but represent some other<br>relation among abstract heaps?<br>From your description (you create new things that represent abstract<br>heap locations and lower that into a nested tree), it sounds like the<br>answer is "you are adding new locations that represent a different<br>tree than the existing TBAA”.<br></div></blockquote><div><br></div><div>What do you mean by “the existing TBAA”?  To my understanding, LLVM itself never creates TBAA nodes; they arise entirely out of the frontend that emits LLVM IR.  I control the frontend in this case.</div><div><br></div><blockquote type="cite"><div style="font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br>If that is not true, what is the actual relation to the existing information?<br>TBAA in the LLVM compiler, at least as currently produced, while<br>theoretically compatible with this scheme , is not quite the same.<br>The langref says "metadata is added to the IR to describe a type<br>system of a higher level language.”</div></blockquote><div><br></div><div>My understanding is that LLVM doesn’t produce TBAA.  It consumes TBAA.</div><div><br></div><div>Hence it’s more meaningful to reason about TBAA in terms of its semantics rather than hypothesizing about how and why someone would produce it.  We agree that TBAA is a nested set structure that describes heap locations.  I’m observing that it would be useful to express bounds on what heap locations that calls read or write.  Therefore, TBAA can be used to convey those sets.</div><br><blockquote type="cite"><div style="font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br>If you are adding a new tree just to represent new abstract heap<br>location info, you are essentially reusing it simply because it<br>represents a nested set structure.   I don't see a lot of value in<br>that sort of conflation, hence the question.<br></div></blockquote><div><br></div><div>If we agree that it would be good to have a system for bounding what a call reads or writes using nested sets, then we have two choices:</div><div><br></div><div>- Reuse TBAA, which can already be used to express nested sets of memory locations.</div><div><br></div><div>- Invent a whole new type system that is structurally identical to TBAA but that goes by a different name.</div><div><br></div><div>Are you seriously proposing the latter?</div><br><blockquote type="cite"><div style="font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br>If not, can you describe in more detail how you are generating this info?<br>Because that is what i get from your description.<br><br><blockquote type="cite">The information you are talking about here is traditional mod/ref info that is completely independent of TBAA.<br><br><br>You lost me.  I’m writing a compiler that generates LLVM IR.  I have high-level knowledge that allows me to prove the set of abstract heap locations that may be read or written by any call, load, or store.<br><br>What do you suggest as a mechanism by which I can communicate that information to LLVM?<br></blockquote><br>I'm suggesting you create an attribute that deals with abstract heap<br>locations that you are describing, instead of overloading and tying it<br>into an existing set of information that  has a different purpose<br>(TBAA, which at least in the current world, represents a nested type<br>tree that describes access rules of a language).<br></div></blockquote><div><br></div><div>So, you’re suggesting inventing a new representation for type hierarchies, that would have the following properties:</div><div><br></div><div>- The same rules as TBAA for load/store.</div><div><br></div><div>- The same rules as what I propose for call.</div><div><br></div><div>- Identical to TBAA in all other respects, except that it would have a different name.</div><div><br></div><div>Am I understanding your proposal correctly?</div><div><br></div><blockquote type="cite"><div style="font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br><blockquote type="cite"><br>Are you saying that such a mechanism already exists and that I should use that instead?<br><br>As far as I can tell, TBAA is the cleanest such mechanism that I have found thus far and its only shortcoming is that I cannot use it to annotate calls.<br></blockquote><br>The fact that some other mechanism is kinda-sorta-like-what-you-want<br>does not necessarily mean it should be tied into it :)<br></div></blockquote><blockquote type="cite"><div style="font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">Hence, I am trying to understand the actual relation between the TBAA<br>tree as it exists now</div></blockquote><div><br></div><div>What TBAA tree?  Are you referring to the TBAA format as described in <a href="http://llvm.org/docs/LangRef.html#tbaa-metadata">http://llvm.org/docs/LangRef.html#tbaa-metadata</a>, or some specific TBAA tree that exists for some specific frontend for some specific language?</div><br><blockquote type="cite"><div style="font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">, and what you are suggesting creating.<br>For example, i could perform pointer analysis in clang and generate a<br>TBAA tree from that. It would work.  It would be easy.  It doesn't<br>mean it is necessarily a great approach.<br></div></blockquote><div><br></div><div>Would your pointer analysis generate either disjoint sets, or nested sets?  For example, if you did a Steensgaard-style pointer analysis then TBAA *would* be the most ideal way of representing the results of such an analysis.</div><div><br></div><div>One way to view my front end’s aliasing knowledge is that I’m doing something like a unification-based analysis.  I actually end up with a three-level TBAA hierarchy (the world, with subtypes for my disjoint sets of heap locations; some of those sets then have further subtypes).</div><div><br></div><blockquote type="cite"><div style="font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br><blockquote type="cite"><br><br>The more specific the info you can get from the metadata, the better off you are eliminating loads/stores<br><br><br>Otherwise, yes, it's certainly incredibly helpful to the other optimizations.<br><br>However, unless there is a good reason, i'd suggest you just make it generic call read/write metadata that talks about values (be they incoming arguments, alloca'd memory, or whatever else).<br><br><br>You lost me.  How does this help a high-level compiler convey to LLVM that a particular call and a particular load or store don’t have any dependency?<br></blockquote><br><br>You add the modref info to the calls. You add attributes to the the<br>loads/stores.  It becomes another disambiguation method to tell<br>whether the load/store and the call have a dependency.<br></div></blockquote><div><br></div><div>Are you suggesting that I override AliasAnalysis?</div><div><br></div><div>I’m trying to make it possible for a frontend to convey this information using LLVM IR, rather than having to override internal LLVM classes in order to provide this information.  Hence there is a question of what format to use.  I’m arguing that TBAA is that format.</div><div><br></div><blockquote type="cite"><div style="font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br><blockquote type="cite"><br>My proposal achieves this, albeit using a coarse-grained at a coarse-grained level: you have to express your knowledge of dependencies using a hierarchy of abstract sets of heap locations (i.e. exactly what TBAA gives you).<br></blockquote><br><br>TBAA does not actually give you that. It gives you a tree that<br>describes a type hierarchy according to the memory access rules of<br>some language.</div></blockquote><div><br></div><div>I disagree.</div><div><br></div><div>It’s true that this is one use-case for TBAA, but that’s not how I use it.  I don’t believe that I’m under any obligation to use it that way.  TBAA is just a format that LLVM consumes, that can be used by a producer of LLVM IR to convey aliasing information.</div><br><blockquote type="cite"><div style="font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">This is transformable into sets of possible heap<br>locations, but it is not actually the same.</div></blockquote><blockquote type="cite"><div style="font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">I get why you may want to reuse it.<br>The structure is close to what you will want.  The attributes already<br>exist on load/stores.  However, much like the tbaa.struct info, unless<br>this actually represents the same tree that TBAA does, it should<br>probably not be part of the same tree.</div></blockquote></div><br></body></html>