<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Tue, Nov 3, 2015 at 10:19 PM Chris Lattner <<a href="mailto:clattner@apple.com">clattner@apple.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">On Nov 3, 2015, at 9:26 AM, Manuel Klimek <<a href="mailto:klimek@google.com" target="_blank">klimek@google.com</a>> wrote:<br><div><blockquote type="cite">I’m sorry I missed this part of the discussion, but IMO, but the right answer is to build a “CIL” analog to “SIL”.  The problems with the existing Clang CFG are that:<br><div><div dir="ltr"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><br></div><div>a) it is not tested as part of IRGen, so it falls out of date.</div><div>b) it is not a proper IR, which can be serialized/deserialized etc.  This makes it very difficult to write tests for.</div><div>c) its “operations” or “instructions" are defined as AST nodes, so its “CILGen” stage doesn’t allow any lowering of operations.</div></div></blockquote><div><br></div><div>Those are all arguments for not using the current clang CFG (which I agree with).</div><div><br></div><div>What are your arguments against implementing a type system on top of llvm IR (that lives on a similar level as debug info) that is not language specific per se, but allows frontends to model their language semantics and have pointers back to their AST? Do you believe such a type system would inherently be coupled to language semantics, and thus not possible to build in a generic enough (and still useful) way? Or are there other problems?</div></div></div></div></blockquote><br></div></div><div style="word-wrap:break-word"><div>Two problems: it doesn’t solve the problem I think need to be solved, and would end up with a really awkward/inelegant solution if it could be made to work.</div><div><br></div><div>The problems that need to be solved:</div><div><br></div><div>1) You need an augmented source level type system to do the transformations that are interesting at this level.  It is the full complexity of the AST represented by Clang, as well as some minor extensions for things that get exposed by the process of lowering.  It is extremely unclear to me how you’d handle this. Using debug information doesn’t work well given that you’ll need multiple types associated with some operations.  Debug info and MDNodes in general would also be an extremely awkward way to express things.</div></div></blockquote><div><br></div><div>I agree this is needed, and that it would need a well thought out design. I'd also think some explorative coding would be necessary to identify how we'd do that (or whether it's even possible).</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>2) You need a full suite of [lowered] source level operations that have little to do with the LLVM IR operations like getelementptr.  Even operations that are common (e.g. load and store) need to be expressed in the source level type system, not the IR type system, so they probably can’t be used (depending on the approach you use for #1).  This can be expressed in LLVM IR as intrinsics, but all the intrinsics would be language specific, so you wouldn’t achieve your language agnostic goal.</div></div></blockquote><div><br></div><div>I'd expect the LLVM IR to be annotated with the types; I agree that we'd need to annotate basically all generated IR with the types, and that that would be a substantial effort.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>3) The only good way I know of to generate good source-level diagnostics (which should include source ranges, etc) is to point back to an AST node (Expr*, Decl*, etc) that it came from.  This mapping is obviously highly frontend-specific, and the lifetime issues managing this are also interesting.  I don’t know how this would be expressed in LLVM IR.</div></div></blockquote><div><br></div><div>I'd expect to have a mechanism in the new higher level type system to point back at frontend specific nodes, if the frontend choses to do so. I agree that lifetime would be interesting, but that seems not insurmountable, but rather a straight forward engineering problem (I'm much more concerned about whether it's possible to define a higher level type system that is language agnostic enough to fit LLVM, but is expressive enough to fit the use cases of the frontends).</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>4) In terms of layering of the library stack, Clang should depend on LLVM, and LLVM IR is intentionally very low in the stack.</div></div></blockquote><div><br></div><div>Agreed, and it should stay that way. I'd expect 2 pieces to live in LLVM: </div><div>- a higher level type system that we can annotate IR with, and which will be kept sound by a subset of the passes (especially the early ones)</div><div>- interfaces in that type system for frontend specific callbacks, so frontend can store backrefs to their AST nodes for accurate diagnostics if they chose to do so</div><div>Do you think that would already contradict the layering requirements?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div>5) Almost all clients of this data structure would be source-language specific (keep in mind that the type system and operations are all language specific) so there would be little reuse anyway.  You’re right that you’d be able to reuse things like “class BasicBlock”, but that isn’t where the complexity is: things like ilist already do the interesting stuff for it, and is shared.</div></div><div><br></div><div>When thinking about this, it is important to consider the specific clients that you’d want to support.</div></div></blockquote><div><br></div><div>Agreed.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>  Even things like the -Wunreachable diagnostic in clang is totally language specific (what kind of crazy language defaults variables to being uninitialized memory in the first place??).  The most interesting diagnostics in this space that Clang (and the static analyzer) want to reason about tend to be language specific as well (e.g. the objc retain/release checker).  </div></div></blockquote><div><br></div><div>On the other hand, we have some evidence for checks that are less language specific (or have very generic components). Thread safety analysis comes to mind. We're basically building the same things for all languages; there are language specific pieces we need the frontends to generate, but the gist of the issue is a lower level type checking system.</div><div>Another thing we'd like is that we can use more static analysis for optimization (for example, devirtualization; iirc in your talk you mentioned you use SIL for that). I am not an expert here, so I'll believe you if you say this is not possible :) On the other hand, some of the ideas behind dependent type systems look to me like they could be useful in llvm (dependent type parameters, for example), and seem non-trivial enough, so that re-implementing them in a new IR for each targeted language would be very costly, when instead it could make LLVM as a platform interesting to a new set of applications.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>While it is obvious that the typestate engine for a checker like a nil dereference check can be shared, this is true regardless of the IR.    These sorts of state machines are quite simple, the complexity is in the analyses they depend on (e.g. alias analysis, which is pretty language specific) and in the code that deals with each kind of AST node/IR operation.</div></div></blockquote><div><br></div><div>Yep, I think the main challenge will be to come up with a type system on top of LLVM IR that is expressive enough so frontends can map their types to it in a way that we can write language independent analysis passes. The proof is in the pudding, of course, but I'm not (yet) convinced it's impossible :D</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>I’ve heard many proposed approaches to encode source level information in IR, but they all have major disadvantages, which is why none of them have been successful.  That said, it could be that you have a specific approach in mind that I haven’t envisioned.  What are you thinking of? </div></div></blockquote><div><br></div><div>I agree that encoding source level information is probably not enough; I'd expect that we actually need to encode a language independent type system on top of LLVM IR; so far I'm not aware somebody has tried that - if that's the case, I'd be interested to learn more about the attempts and their shortcomings.</div><div><br></div><div>Cheers,</div><div>/Manuel</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div><br></div></div><div>-Chris</div><div><br></div></div></blockquote></div></div>