<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-reply;
font-family:"Calibri","sans-serif";
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri","sans-serif";}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Different intrinsics sounds like a good solution to me.
</span><span style="font-size:11.0pt;font-family:Wingdings;color:#1F497D">J</span><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">So what happens with the case where a variable is registerized but later we decide to spill it? Presumably we'd have a dbg.addr to point to the spill slot.
In past compilers I've used, spill slots were treated analogous to register allocation, i.e. some effort was made to minimize the number of spill slots and a variable might be spilled to different slots at different points. If LLVM does that, then dbg.addr
will have to be allowed to associated different addresses with the variable. On the other hand, if LLVM allocates a unique memory "home" for each spilled variable, then dbg.addr can retain the property you suggest, that the address expression is always the
same.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">--paulr<o:p></o:p></span></p>
<p class="MsoNormal"><a name="_MailEndCompose"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></a></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> Reid Kleckner [mailto:rnk@google.com]
<br>
<b>Sent:</b> Thursday, September 07, 2017 10:47 AM<br>
<b>To:</b> llvm-dev; David Blaikie; Adrian Prantl; Robinson, Paul; Alex Bradbury; Chandler Carruth<br>
<b>Subject:</b> RFC: Unify debug and optimized variable locations with llvm.dbg.addr [was: DW_OP_LLVM_memory]<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">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".<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">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:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">1. Transforms assume that allocas will have at most one dbg.declare use.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">3. The name suggests that it's a declaration, and we probably shouldn't try to re-declare a variable twice.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">This should handle both of the examples from the original RFC (<a href="http://lists.llvm.org/pipermail/llvm-dev/2017-September/117141.html">http://lists.llvm.org/pipermail/llvm-dev/2017-September/117141.html</a>).<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">For this case:<o:p></o:p></p>
</div>
<div>
<div>
<p class="MsoNormal"> int x = 42; // Can DSE<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> dostuff(x); // Can propagate 42<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> x = computation(); // Post-dominates `x = 42` store<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> escape(&x);<o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">After DSE, we should get:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> int x; // dbg.value(!"x", 42)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> dostuff(42);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> x = computation();<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> // dbg.addr(!"x", &x)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> escape(&x);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">The new capability illustrated here is that dbg.addr can move and when it moves its position matters and affects the final location list.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Another example:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> int x = 1; // Clang -O0 emits dbg.addr(!"x", %x.addr)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> int y = 1;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> escape(&x, &y);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> x = 2; // DSE will delete, inserting dbg.value(!"x", i32 2)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> y = 2; // Cannot delete, user may break here to observe x<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> x = 3; // DSE will insert dbg.addr(!"x", %x.addr) before the killing store<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> escape(&x, &y);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">This shows that there may be multiple llvm.dbg.addr intrinsic calls.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">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. 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. If we allowed this, mem2reg would have to consider inputs that look like this:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> %x.addr = alloca i32<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> %y.addr = alloca i32<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> call void llvm.dbg.addr(i32* %x.addr, !"x")<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> store i32 0, i32* %x.addr<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> %v = load i32, i32* %x.addr<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> store i32 %v, i32* %y.addr ; copy the variable<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> call void llvm.dbg.addr(i32* %y.addr, !"x") ; x now lives in y<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> store i32 42, i32* %x.addr ; mem2reg would translate this to dbg.value(i32 42, !"x"), which is incorrect<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">----<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">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.<o:p></o:p></p>
</div>
</div>
</div>
</div>
</body>
</html>