<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Thu, Sep 7, 2017 at 10:46 AM Reid Kleckner <<a href="mailto:rnk@google.com">rnk@google.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I chatted with Chandler in person and came up with what I think is a much simpler design than my previous design in the thread titled "RFC: Introduce DW_OP_LLVM_memory to describe variables in memory with dbg.value".<div><br></div><div>The crux of the problem in that thread is that we need a representation, particularly in the middle-end, to describe a variables address, at a particular program point.</div><div><br></div><div>The reason we can't just use dbg.declare (at least as specified today) is that it doesn't have this "particular program point" property:</div><div>1. Transforms assume that allocas will have at most one dbg.declare use.</div><div>2. dbg.declares of static allocas are used to fill in a variable frame index side table. They are not translated into DBG_VALUE machine instructions that have real program points, so we can't mix them with dbg.value. A value cannot be `i32 42` at one point, and then be back in memory later.</div><div>3. The name suggests that it's a declaration, and we probably shouldn't try to re-declare a variable twice.</div><div><br></div><div>The backend actually already has this in MachineInstr::isIndirectDebugValue(). If that returns true, it means that the result of evaluating the DIExpression on the MachineOperand will be the address of the local variable. The middle-end lacks this ability.</div><div><br></div><div>Rather than futzing around with the DIExpression on dbg.value to encode this bit of information (address or value), we can encode it in the intrinsic name.</div><div><br></div><div>This should handle both of the examples from the original RFC (<a href="http://lists.llvm.org/pipermail/llvm-dev/2017-September/117141.html" target="_blank">http://lists.llvm.org/pipermail/llvm-dev/2017-September/117141.html</a>).</div><div><br></div><div>For this case:</div><div><div>  int x = 42;  // Can DSE</div><div>  dostuff(x); // Can propagate 42</div><div>  x = computation();  // Post-dominates `x = 42` store</div><div>  escape(&x);</div></div><div><br></div><div>After DSE, we should get:</div><div>  int x; // dbg.value(!"x", 42)</div><div>  dostuff(42);</div><div>  x = computation();</div><div>  // dbg.addr(!"x", &x)</div><div>  escape(&x);</div><div><br></div><div>The new capability illustrated here is that dbg.addr can move and when it moves its position matters and affects the final location list.</div><div><br></div><div>Another example:</div><div>  int x = 1; // Clang -O0 emits dbg.addr(!"x", %x.addr)</div><div>  int y = 1;</div><div>  escape(&x, &y);</div><div>  x = 2; // DSE will delete, inserting dbg.value(!"x", i32 2)</div><div>  y = 2; // Cannot delete, user may break here to observe x</div><div>  x = 3; // DSE will insert dbg.addr(!"x", %x.addr) before the killing store</div><div>  escape(&x, &y);<br></div><div><br></div><div>This shows that there may be multiple llvm.dbg.addr intrinsic calls.</div><div><br></div><div>One complication is that we *don't* want to allow dbg.addr calls to *move* the location of a variable from one memory location to another. </div></div></blockquote><div><br>That seems pretty restrictive... <br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>So, for the same concrete local variable, all dbg.addr calls should agree on the memory location. This simplifies mem2reg SSA promotion, so that it doesn't have to care about the program ordering of dbg.value and dbg.addr calls. </div></div></blockquote><div><br></div><div>I feel like maybe that's worth supporting - but I guess it can be done later if it becomes useful/isn't needed right now?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div> If we allowed this, mem2reg would have to consider inputs that look like this:</div><div>  %x.addr = alloca i32</div><div>  %y.addr = alloca i32<br></div><div>  call void llvm.dbg.addr(i32* %x.addr, !"x")<br></div><div>  store i32 0, i32* %x.addr</div><div>  %v = load i32, i32* %x.addr</div><div>  store i32 %v, i32* %y.addr ; copy the variable</div><div>  call void llvm.dbg.addr(i32* %y.addr, !"x")  ; x now lives in y</div><div>  store i32 42, i32* %x.addr ; mem2reg would translate this to dbg.value(i32 42, !"x"), which is incorrect  </div></div></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><br></div><div>mem2reg should only have to worry about the program order of alloca loads and stores to form SSA. It shouldn't also have to reason about concrete variables.</div><div><br></div><div>----</div><div><br></div><div>In the short term, we can add this intrinsic, make instcombine use it, and lower it to indirect DBG_VALUE machine instrs. That should be a bug spotfix.</div><div><br></div><div>Once we do that, we can make more optimization passes dbg.addr aware. Obviously, mem2reg needs to handle it in mostly the same way that it handles dbg.declare today. It just needs to remove possibly multiple dbg.addrs while today it removes only one dbg.declare.<br></div><div><br></div><div>Then, we can add a flag to transition clang from dbg.declare to dbg.addr. Now -O0 code will emit indirect DBG_VALUE instructions. We'll have to fix the backend to cope better with these, so that we emit location lists that are equivalent to what we get today from the frame index side table.</div></div>
</blockquote></div></div>