<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: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">Hi Reid,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">when you were looking at "runs" of dbg.value instructions, were you looking just for a series of calls, or at the operands? I seem to remember seeing long
runs with identical operands, which of course would be completely redundant. IIRC this was especially bad when it came to a global init function inlining boodles of the same ctor for a long string of globals with the same type. LLVM tends not to do that
but application code might well do that.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Thanks,<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""> llvm-dev [mailto:llvm-dev-bounces@lists.llvm.org]
<b>On Behalf Of </b>Reid Kleckner via llvm-dev<br>
<b>Sent:</b> Monday, October 22, 2018 9:43 PM<br>
<b>To:</b> llvm-dev<br>
<b>Subject:</b> [llvm-dev] [RFC] Moving llvm.dbg.value out of the instruction stream<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<div>
<p class="MsoNormal">One of the major drawbacks of our current variable tracking debug info design is that it puts lots of llvm.dbg.value instructions in the instruction stream. After the debug info BoF at the 2018 dev meeting (last week), I started to seriously
consider a design where we track variable locations on the side of the instruction stream. People seemed to agree this idea was good enough to at least explore, so I put together some exploratory data and ideas.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Our current design tracks variable values with llvm.dbg.value instructions, which live in the instruction stream. This is crummy, because passes have to know to filter them. Today we have filtering iterators, but most passes have not been
audited to use them, and we regularly have bugs where enabling debug info changes what the optimizer does. If we move variable tracking out of the instruction stream, we can fix this entire class of bugs by design.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Second, the current design slows down basic block iteration. My intuition says that, after inlining, we have *tons* of dbg.value instructions clogging up our basic blocks, slowing down our standard optimizer pipeline. Linked list iteration
isn't fast on modern architectures, so if dbg.values are an appreciable source of elements, moving it out of the list will speed up compilation and make -g less of a compile-time bear.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">One thing this change will *not* address is the kind of use-before-def bugs we get today where dbg info unaware passes sink or hoist SSA instructions across the dbg.value that uses them. It has been suggested that we should attach the dbg.value
directly to the instruction producing the value, but I think this is a mistake, because it only handles the common case, and not the special case where the value is produced far from the point of the assignment. Even if we have the special case attachment
for value-producing instructions, we need new APIs to help passes decide how to update the debug info when sinking or hoisting across blocks.<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">Data & Motivation<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">I used my compile-time test case of the day for this: SemaChecking.cpp. I re-compiled opt with the attached patch, which gets the total number of Instructions after CGP, and the number of dbg intrinsics. I compiled SemaChecking.cpp to bitcode
with `clang -cc1 -debug-info-kind=limited -gcodeview -mllvm -instcombine-lower-dbg-declare=0` to match what Chromium uses. I think these numbers are general, though. Here are the numbers I got from my patch:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">160402 codegenprepare - Total number of llvm.dbg.* instructions<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">296080 codegenprepare - Total number of instructions<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">So, 54% of all instructions are dbg.(value|declare) instructions today! Moving them out of the main BB ilist would speed up BB iteration by 2x. That's not necessarily where we spend our time. I happen to already know that this test case
does a lot of BB scanning (PR38829), so I know it will help this test case, but it may not matter if we make local dominance not scan.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">In any case, here is the time to compile to bitcode for this example:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">-O2 -g: 89.6s<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">-O2: 53.6s<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">-O2 -gmlt: 52.6s<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">-gmlt should be slower than no debug info, so clearly my system is noisy, but I think the impact of dbg.value is still clearly observable.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">The next thing I noticed is that dbg.values tend to appear in packs. My theory is that most of this is from multiple levels of inlining of helpers that forward parameters. Here is a histogram of the length of dbg intrinsic runs:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">dbg instructions, run length | freq<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">1 13086<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">2 6587<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">3 3927<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">4 1605<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">5 1643<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">6 1414<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">7 2165<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">8 1708<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">9 226<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">10 36<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">11 26<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">12 14<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">13 1750<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">14 70<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">15 224<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">16 3<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">17 154<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">18 5<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">19 1<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">20 2<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">22 209<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">23 688<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">24 705<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">28 4<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">29 1<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">36 1<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">40 2<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">71 1<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">My understanding of the histogram above is that 52% of dbg.values are in runs of length 8 or greater. You can see some spikes here at 13 and 22-24, which I think supports my theory about inlining above.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">This example is just a standard -O2 large TU in clang. I can only imagine that the dbg.value runs get longer in ThinLTO, where the inliner runs more.<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">Design<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">dbg.value is meant to associate an SSA value with a label. My basic idea is that it's better to attach a label to the instruction it precedes rather than creating a separate instruction. However, a label should stay behind if the instruction
it precedes moves. For example, Instruction::moveAfter/moveBefore should implicitly reattach their variable tracking info to the next instruction. While working on
<a href="https://reviews.llvm.org/D51664">https://reviews.llvm.org/D51664</a>, I realized it would be pretty easy to use the ilist callbacks currently used for symbol table updating to implement this.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I think making the attachments describe the values of variables before the instruction is good, because in the general case every block has at least one instruction, which typically doesn't produce a value: the terminator. The only terminator
that produces a value is invoke, and we can attach any variable tracking info for it to the first (non-phi?) instruction in the normal successor.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I think, overall, this is just an internal representation shift. We may want to change our bitcode and assembly language to better align with our internal representation, but it should be functionaly equivalent.<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">IR syntax<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">This is kind of important, since it affects how developers think about these and keep them up to date. Consider the following C source:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"> int foo(void);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> int bar(void) {<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> int v = foo();<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> v = foo();<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> return v;<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">I'm proposing moving from something like this:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"> %v1 = call i32 @foo()<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> call void @llvm.dbg.value(metadata i32 %v1, metadata !123, metadata !DIExpression) !dbg !456<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> %v2 = call i32 @foo()<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> call void @llvm.dbg.value(metadata i32 %v2, metadata !123, metadata !DIExpression) !dbg !456<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> ret i32 %v2<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">To something like this:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"> %v1 = call i32 @foo()<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> !dbgvalue i32 %v1, variable !123, loc !456 [, expr ...]<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> %v2 = call i32 @foo()<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> !dbgvalue i32 %v2, variable !123, loc !456 [, expr ...]<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> ret i32 %v2<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">'expr' would be an optional DIExpression, expressed more compactly if possible, perhaps using our inline !DIExpression parsing for now.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">This also avoids the confusion that today the dbgloc attached to a dbg.value is not actually used to generate line table entries, it's only for tracking distinct variables created from different inlined call sites.<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">Data structures<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">What we're really trying to represent is two separate sequences. For simplicity and minimal change, I initially propose that we create a secondary linked list of some new DbgValue data structures. They would relate kind of like this:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"> inst1 -> inst2 -> inst3 -> inst4<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> \ \ \ \<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> dv1 -> dv2 -> dv3 -> dv4<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">If we were to delete inst2, dv2 would be reattached to the following instruction:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"> inst1 -> inst3 -> inst4<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> \ \ \<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> dv1 -> dv2 -> dv3 -> dv4<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">What's interesting is that once dv2 and dv3 are grouped together, it's actually a bug if we ever generate code between them. I can imagine more compact representations than a linked list, but we want to be able to efficiently join two sequences
of dbgvalues when deleting or moving a single instruction, and linked lists achieve that. The list would be owned by the BasicBlock, since if a whole block is unreachable, there's no where to put the value tracking info.<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">Actually doing it<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">LLVM currently has several unfinished migrations going on and a fair amount of technical debt. It's not clear that this project is the top priority for me or anyone else at this moment, so even if people like this idea, don't assume it
will be implemented soon. I think similar efficiency reasoning applies to the pointee-type removal API migration that David Blaikie started. Casts are very similar to dbg.values in this way.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">However, I wanted to write it all down before letting it fade from memory. Apologies for the length, I didn't have time to make a shorter RFC. =P<o:p></o:p></p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>