<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Sep 6, 2017 at 5:01 PM, David Blaikie <span dir="ltr"><<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><span><div dir="ltr">On Wed, Sep 6, 2017 at 2:01 PM Reid Kleckner <<a href="mailto:rnk@google.com" target="_blank">rnk@google.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"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Sep 6, 2017 at 10:01 AM, David Blaikie <span dir="ltr"><<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>></span> wrote:</div></div></div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div class="gmail_quote">I guess you described this already, but talking it through for myself/maybe others will find this useful:<br><br>So since we don't have DW_OP_regN for LLVM registers, we could sort of assume the implicit first value on the stack is a pseudo-OP_regN of the LLVM SSA register.<br></div></div></div></blockquote></div></div></div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"></div></blockquote><div><br></div><div>Yep, that's how we use DIExpressions in both IR and MIR: The LHS of the dbg.value and DBG_VALUE instructions are a register-like value that gets pushed onto the expression stack. The DWARF asmprinter does some expression folding to clean things up, but that's the model.</div></div></div></div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div class="gmail_quote">To support that, all existing uses would need no changes to match the DWARF model of registers being implicitly direct values.<br><br>Code that wanted to describe the register as containing the memory address of the interesting thing would use DW_OP_stack_value to say "this location description that is a register is really an address you should follow to find the value, not a direct value itself"?<br><br>But code that wanted to describe a variable as being 3 bytes ahead of a pointer in an LLVM SSA register would only have "plus 3" in the expression stack, since then it's no longer a direct value but is treated as a pointer to the value. I guess this is where the ambiguity would come in - currently how does "plus 3" get interpreted when seen in LLVM IR, I guess that's meant to describe reg value + 3 as being the immediate value of the variable? (so it's implicitly OP_stack_value? & OP_stack_value is added somewhere in the DWARF backend?)<br></div></div></div></blockquote><div><br></div></div></div></div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>Our model today is inconsistent.</div></div></div></div></blockquote></span><div><br>Inconsistent between what and what? LLVM and DWARF? Yeah, I guess there's some mismatch between the semantics, though I'm still having trouble wrapping my head around it.</div></div></div></blockquote><div><br></div><div>I mean LLVM's model is internally inconsistent. We have bugs like the RVO one that you filed (<a href="https://llvm.org/pr34513">https://llvm.org/pr34513</a>), where we forget if the debug value is an address or a value.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><span><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> In LLVM IR today, the SSA value of the dbg.value *is* the interesting value, it is not the address, and we typically use empty DIExpressions. If the value is ultimately register allocated and the DIExpression is empty, we will emit a DW_OP_regN location expression. If the value is spilled, we usually don't need to append DW_OP_stack_value because the location is now a memory location, which can be described by DW_OP_[f]breg.</div><div><br></div><div>Today, passes that want to add "plus 3" to a DIExpression go out of their way to add DW_OP_stack_value to the DIExpression because the backend won't do it for us, even though dbg.value normally describes the value, not an address.</div><div><br></div><div>To explore the alternative DW_OP_stack_value model, here's how I'd go about it:</div><div>1. Replace llvm.dbg.value with new intrinsic, llvm.dbg.loc, to make the semantic change clear. It can express both an address or a value, depending on the DIExpression.</div><div>2. Auto-upgrade llvm.dbg.value to llvm.dbg.loc. Append DW_OP_stack_value to the DIExpression argument of the intrinsic.</div><div>3. Auto-upgrade llvm.dbg.declare to llvm.dbg.loc, leave the DIExpression alone. The LHS of llvm.dbg.declare is already the address of the variable.</div><div>4. Eliminate the second operand of DBG_VALUE MachineInstrs. Indirect DBG_VALUES are now expressed with a DIExpression that lacks DW_OP_stack_value at the end.</div><div>5. Teach our DWARF expression emitter to combine the new expressions as necessary. In particular, we can elide DW_OP_stack_value for DBG_VALUEs with physical register operands. They just use DW_OP_regN, which is implicitly a value location.</div><div>6. Teach all passes that spill virtual registers used by DBG_VALUE to remove DW_OP_stack_value from the DIExpression, or add DW_OP_deref as appropriate.</div><div><br></div><div>This should be equivalent to DW_OP_LLVM_memory, and more inline with DWARF location expression semantics, but it has a large migration cost.</div><div><br></div><div>---</div><div><br></div><div>I think part of the reason I wanted to move in the DW_OP_LLVM_memory direction is that I originally wanted to add a memory offset operand to it. Our actual use cases for complex DWARF expressions typically come from things like safestack, ASan, and blocks. What these all have in common is that they gather up a number of variables and move them off into a struct in heap memory. This is very similar to what happens when we spill a virtual register: instead of describing a register, we modify the expression to load the value from some FP register with an offset. I think the right representation for these transforms is basically a "chain of loads".</div></div></div></div></blockquote></span><div><br>Don't think I've got any mental model of what you mean by this phrase ('chain of loads') - could you provide an example or the like?<br></div></div></div></blockquote><div><br></div><div>Suppose you have a captured variable with __block shared storage, and then suppose you compile it with ASan and safestack, and then the safestack pointer is spilled. To compute the value, the debugger starts from a register, goes to an offset, and loads a pointer, repeating the process until it finds the value. As we proceed through codegen, we effectively build up the chain.</div><div><br></div><div>1. To implement __block in Clang, we use dbg.declare(%block_descriptor, DW_OP_deref, DW_OP_constu_plus $offset)</div><div>2. Assuming the block descriptor lived in an alloca (which it doesn't, but assume it does for argument's sake), asan will move that alloca onto the heap to implement use-after-return detection. It will prepend "DW_OP_deref, DW_OP_constu, $offset" to the DIExpression.</div><div>3. If ASan put its value in an alloca and safestack wanted to move that alloca to the safe stack (again bear with me), it would do the same: prepend deref+offset.</div><div>4. Finally, spilling the safe stack pointer to the control stack would mean prepending deref+offset.</div><div><br></div><div>This seems like a really common pattern. Right now this offsetting and loading has to be expressed as separate location expression opcodes. The DW_OP_deref opcode functions like a load sequencing operation that can only appear between two offsets, although an offset could be zero, in which case there would be no DW_OP_constu_plus opcode. I'm suggesting we move to a representation where the offset and the deref are one. Think "semicolon as sequencing operator" vs. "semicolon as statement terminator". When we use DW_OP_LLVM_memory or DW_OP_deref, the variable must always live in memory, we're always doing address calculation. We should try to make our representation more closely match the set of things we actually want to do and support.</div><div><br></div><div>That's kind of the gist of what I had in mind. I didn't think it was worth it, which is why I pared the proposal down to just "the opposite of DW_OP_stack_value".</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div></div><span><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> I was imagining that DW_OP_LLVM_memory with an offset would be that load chain link.</div><div><br></div><div>The idea behind this representation is that it should make it easy for spilling transforms to prepend a load chain onto the expression, rather than them having to individually discover if DW_OP_deref is needed, or call some common helper like DIExpression::prepend. It should always be valid to push on a new load with an offset.</div></div></div></div></blockquote></span><div><br>When would that not be valid today/without LLVM_memory? Sorry, again - it's all a bit fuzzy in my head.<br><br>There'd be some canonicalization opportunities, but not seeing the correctness issues with being able to prepend onto the location list -  seems like that might be true with LLVM_memory too... maybe?<br></div></div></div></blockquote><div><br></div><div>The correctness issue with today's prepending of offsets and deref is that it's hard to know when to insert deref, because we don't know if an expression describes an address or a value. We have code like this in buildDbgValueForSpill:</div><div><div>  // If the DBG_VALUE already was a memory location, add an extra</div><div>  // DW_OP_deref. Otherwise just turning this from a register into a</div><div>  // memory/indirect location is sufficient.</div><div>  if (IsIndirect)</div><div>    Expr = DIExpression::prepend(Expr, DIExpression::WithDeref);</div></div><div><br></div><div>We modify DBG_VALUEs for spills in several other places in codegen and they don't all correctly insert DW_OP_deref. The load chain representation should make it easy to just modify the offset on the front or add a new load depending on what's being done.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div></div><span><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>It also has the advantage that it will be easier to translate to CodeView than arbitrary DWARF expressions, which we are currently canonicalizing into a load chain and then attempting to emit.<br></div></div></div></div></blockquote></span><div><br>*nod* my worry is ending up with 3 different representations - DWARF, CodeView, and the increasingly divergent IRDWARF (especially since it's "sort of like DWARF" which makes the few divergences more costly/difficult).<br> </div><span><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div></div><div>Does that make sense? I'm starting to feel like I should either pursue the more ambitious load chain design, </div></div></div></div></blockquote></span><div><br>What would that look like?<br></div></div></div></blockquote><div><br></div><div>Just `DW_OP_LLVM_memory, 8, DW_OP_LLVM_memory, 20, ...` in DIExpression through IR. It's OK to insert more DWARF opcodes between the links, it's just non-canonical if they are pointer offsetting opcodes that could be folded into the memory opcode. The DWARF expression backend would fold it into the same location expressions we have today.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div></div><span><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>or consistently apply DW_OP_stack_value to llvm.dbg.loc (alternative names welcome).<br></div></div></div></div></blockquote></span><div><br>Would have to think some more - maybe there's a way to avoid the rename? But yeah, don't have a problem with llvm.dbg.loc - as you say/implied, it'd match the new semantics better.<br></div></div></div></blockquote><div><br></div><div>I don't think so. =/ I think the rename is the only safe way to maintain bitcode compatibility.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div>But really, your original proposal's probably OK/close enough to go ahead. I don't feel that strongly.<br></div></div></div></blockquote><div><br></div><div>Makes sense. That's basically where I ended up, but now I'm reconsidering the dbg.loc+DW_OP_stack_value thing, to bring DIExpressions closer to DWARF.</div></div></div></div>