<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 15 (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:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Monaco;
panose-1:0 0 0 0 0 0 0 0 0 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
{mso-style-priority:34;
margin-top:0in;
margin-right:0in;
margin-bottom:0in;
margin-left:.5in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
p.gmail-p1, li.gmail-p1, div.gmail-p1
{mso-style-name:gmail-p1;
mso-margin-top-alt:auto;
margin-right:0in;
mso-margin-bottom-alt:auto;
margin-left:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
span.gmail-s1
{mso-style-name:gmail-s1;}
span.gmail-apple-converted-space
{mso-style-name:gmail-apple-converted-space;}
span.EmailStyle22
{mso-style-type:personal-compose;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
/* List Definitions */
@list l0
{mso-list-id:1006010163;
mso-list-type:hybrid;
mso-list-template-ids:868506142 626148172 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;}
@list l0:level1
{mso-level-start-at:0;
mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:20.5pt;
text-indent:-.25in;
font-family:Wingdings;
mso-fareast-font-family:"Times New Roman";
mso-bidi-font-family:Calibri;}
@list l0:level2
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:56.5pt;
text-indent:-.25in;
font-family:"Courier New";}
@list l0:level3
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:92.5pt;
text-indent:-.25in;
font-family:Wingdings;}
@list l0:level4
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:128.5pt;
text-indent:-.25in;
font-family:Symbol;}
@list l0:level5
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:164.5pt;
text-indent:-.25in;
font-family:"Courier New";}
@list l0:level6
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:200.5pt;
text-indent:-.25in;
font-family:Wingdings;}
@list l0:level7
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:236.5pt;
text-indent:-.25in;
font-family:Symbol;}
@list l0:level8
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:272.5pt;
text-indent:-.25in;
font-family:"Courier New";}
@list l0:level9
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:308.5pt;
text-indent:-.25in;
font-family:Wingdings;}
@list l1
{mso-list-id:1231113294;
mso-list-type:hybrid;
mso-list-template-ids:-887716250 -1980972940 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;}
@list l1:level1
{mso-level-start-at:0;
mso-level-number-format:bullet;
mso-level-text:-;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:"Calibri",sans-serif;
mso-fareast-font-family:Calibri;}
@list l1:level2
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:"Courier New";}
@list l1:level3
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:Wingdings;}
@list l1:level4
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:Symbol;}
@list l1:level5
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:"Courier New";}
@list l1:level6
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:Wingdings;}
@list l1:level7
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:Symbol;}
@list l1:level8
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:"Courier New";}
@list l1:level9
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:Wingdings;}
@list l2
{mso-list-id:2121490091;
mso-list-type:hybrid;
mso-list-template-ids:-1365341244 -310317724 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;}
@list l2:level1
{mso-level-start-at:0;
mso-level-number-format:bullet;
mso-level-text:-;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:50.5pt;
text-indent:-.25in;
font-family:"Calibri",sans-serif;
mso-fareast-font-family:"Times New Roman";}
@list l2:level2
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:86.5pt;
text-indent:-.25in;
font-family:"Courier New";}
@list l2:level3
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:122.5pt;
text-indent:-.25in;
font-family:Wingdings;}
@list l2:level4
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:158.5pt;
text-indent:-.25in;
font-family:Symbol;}
@list l2:level5
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:194.5pt;
text-indent:-.25in;
font-family:"Courier New";}
@list l2:level6
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:230.5pt;
text-indent:-.25in;
font-family:Wingdings;}
@list l2:level7
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:266.5pt;
text-indent:-.25in;
font-family:Symbol;}
@list l2:level8
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:302.5pt;
text-indent:-.25in;
font-family:"Courier New";}
@list l2:level9
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:338.5pt;
text-indent:-.25in;
font-family:Wingdings;}
ol
{margin-bottom:0in;}
ul
{margin-bottom:0in;}
--></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" style="margin-left:20.5pt;text-indent:-.25in">Hi Everyone,<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:20.5pt;text-indent:-.25in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:20.5pt;text-indent:-.25in">We’ve reached to a working prototype (With some caveats mentioned at the end) for this problem, I need to have your opinion on this before we move further WRT.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:20.5pt;text-indent:-.25in"><o:p> </o:p></p>
<ul style="margin-top:0in" type="disc">
<li class="MsoListParagraph" style="margin-left:-15.5pt;mso-list:l0 level1 lfo1">
<a id="OWAAM9CE35000CC414F7289B27681D0F805E7" href="mailto:dblaikie@gmail.com"><span style="font-family:"Calibri",sans-serif;text-decoration:none">@David Blaikie</span></a>I have no idea what the LLVM DWARF representation for this would look like - short of
making even more fine-grained scopes in the DILexicalScope hierarchy, which sounds really expensive from a memory perspective. That's really where I worry that the cost to this feature might outweigh the benefit (& might be why no one's done this in the past)
- but data should tell us that. As much as in-tree development is preferred, this might be the sort of thing worth prototyping out of tree first to see if it can be made viable before adding the complexity to the LLVM project proper - but I'm open to ideas/suggestions.<o:p></o:p></li></ul>
<p class="MsoListParagraph" style="margin-left:20.5pt"><o:p> </o:p></p>
<p class="MsoNormal">Problem breakdown:<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:20.5pt"><b><u>End representation at (Object or ASM level):<o:p></o:p></u></b></p>
<ul style="margin-top:0in" type="disc">
<li class="MsoListParagraph" style="margin-left:14.5pt;mso-list:l2 level1 lfo3">Need a label(Symbol) to extract **exact location(Or address at which the variable defined(or allocated))**, for which an Machine Instruction is needed(in this case DBG_VALUE leveraged).
<o:p></o:p></li><li class="MsoListParagraph" style="margin-left:14.5pt;mso-list:l2 level1 lfo3">Take the difference of this label(address) and **low_pc** of the **Lexical Block**, synthesize the “DW_AT_start_scope” using this difference(Offset) attach this attribute to the
Scoped variable.<o:p></o:p></li></ul>
<p class="MsoListParagraph" style="margin-left:1.0in"><o:p> </o:p></p>
<p class="MsoNormal" style="text-indent:.25in"><b><u>Implementation LLVM-to-DWARF:<o:p></o:p></u></b></p>
<ul style="margin-top:0in" type="disc">
<li class="MsoListParagraph" style="margin-left:0in;mso-list:l1 level1 lfo2">Implementation strategy is a bit conservative in a sense, I didn’t introduce any new metadata or new instruction(IR or MachineInstr).<o:p></o:p></li></ul>
<p class="MsoListParagraph"><o:p> </o:p></p>
<ul style="margin-top:0in" type="disc">
<li class="MsoListParagraph" style="margin-left:0in;mso-list:l1 level1 lfo2">For the unoptimized case, this variable will be stack allocated(with Frame Index per alloca instruction) and dbg.declare is used.<o:p></o:p></li><li class="MsoListParagraph" style="margin-left:0in;mso-list:l1 level1 lfo2">Lower this dbg.declare(associated with the variable of interest) to generate a DBG_VALUE instruction for this dbg.declare intrinsic(in both FastISel and SelectionDAGISel Instruction
Selection) Distinguishing with other local variables(if present) is accomplished based on the premises of enclosing scope(i.e Lexical Block).<o:p></o:p></li><li class="MsoListParagraph" style="margin-left:0in;mso-list:l1 level1 lfo2">Request a label(Symbol) before this instruction(DBG_VALUE). This label will be use later for associating the variable with it and calculating offset to be inserted as DW_AT_start_scope.<o:p></o:p></li><li class="MsoListParagraph" style="margin-left:0in;mso-list:l1 level1 lfo2">Extend DbgVariable to contain 2 symbols<o:p></o:p></li><ul style="margin-top:0in" type="circle">
<li class="MsoListParagraph" style="margin-left:0in;mso-list:l1 level2 lfo2">VarSym -> Symbol associated with this variable<o:p></o:p></li><li class="MsoListParagraph" style="margin-left:0in;mso-list:l1 level2 lfo2">ScopeBeginSym -> Symbol associated with the enclosing Lexical Scope LowPC<o:p></o:p></li></ul>
<li class="MsoListParagraph" style="margin-left:0in;mso-list:l1 level1 lfo2">When constructing actual DIE and DW_AT_start_scope use these symbols difference to emit as offset within the variable DIE. Again distinguishing with other variables is accomplished
using above mentioned simple check.<o:p></o:p></li></ul>
<p class="MsoListParagraph"><o:p> </o:p></p>
<p class="MsoNormal">Resultant DWARF after this for the test case:<o:p></o:p></p>
<p class="MsoNormal">0x0000006d: DW_TAG_lexical_block<o:p></o:p></p>
<p class="MsoNormal"> DW_AT_low_pc (0x00000000002016d1)<o:p></o:p></p>
<p class="MsoNormal"> DW_AT_high_pc (0x0000000000201729)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">0x0000007a: DW_TAG_variable<o:p></o:p></p>
<p class="MsoNormal"> DW_AT_location (0x00000000:<o:p></o:p></p>
<p class="MsoNormal"> [0x00000000002016e8, 0x0000000000201731): DW_OP_breg6 RBP-24)<o:p></o:p></p>
<p class="MsoNormal"> DW_AT_name ("Local")<o:p></o:p></p>
<p class="MsoNormal"> DW_AT_decl_file ("test.c")<o:p></o:p></p>
<p class="MsoNormal"> DW_AT_decl_line (7)<o:p></o:p></p>
<p class="MsoNormal"> DW_AT_type (0x0000008b "int")<o:p></o:p></p>
<p class="MsoNormal"> DW_AT_start_scope (0x00000017)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Summarizing results:<o:p></o:p></p>
<ul style="margin-top:0in" type="disc">
<li class="MsoListParagraph" style="margin-left:0in;mso-list:l1 level1 lfo2">Debugging behavior as expected(of course with a small change in GDB(as mentioned in very first mail)).<o:p></o:p></li><li class="MsoListParagraph" style="margin-left:0in;mso-list:l1 level1 lfo2">No regressions found on trunk!<o:p></o:p></li><li class="MsoListParagraph" style="margin-left:0in;mso-list:l1 level1 lfo2">Cost to benefit: Compile time costs and debug info size increment not yet evaluated.<o:p></o:p></li></ul>
<p class="MsoListParagraph"><o:p> </o:p></p>
<p class="MsoNormal">Concerns still need to be addressed:<o:p></o:p></p>
<ul style="margin-top:0in" type="disc">
<li class="MsoListParagraph" style="margin-left:0in;mso-list:l1 level1 lfo2">DBG_VALUE instructions in most cases end up creating location lists(due to their special handling), unfortunately in this case too apart from “DW_AT_start_scope” Location lists are
also<o:p></o:p></li></ul>
<p class="MsoListParagraph">getting attached to the variable. Is this Okay ?<o:p></o:p></p>
<p class="MsoListParagraph"><o:p> </o:p></p>
<p class="MsoNormal">Could you guys please let us know your thoughts/comments on the overall approach taken here.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Note:<o:p></o:p></p>
<p class="MsoNormal">This sort of bug(improvement based on DW_AT_start_scope) is also reported(By someone else, not me) in GCC here:<o:p></o:p></p>
<p class="MsoNormal"><a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93844">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93844</a><o:p></o:p></p>
<p class="MsoNormal"><b><u><o:p><span style="text-decoration:none"> </span></o:p></u></b></p>
<p class="MsoNormal">Thank You!<o:p></o:p></p>
<p class="MsoNormal">Sourabh.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b>From:</b> David Blaikie <dblaikie@gmail.com> <br>
<b>Sent:</b> Thursday, April 16, 2020 1:46 AM<br>
<b>To:</b> Vedant Kumar <vedant_kumar@apple.com><br>
<b>Cc:</b> Sourabh Singh Tomar <sourav0311@gmail.com>; Achra, Nitika <Nitika.Achra@amd.com>; llvm-dev@lists.llvm.org; Kumar N, Bhuvanendra <Bhuvanendra.KumarN@amd.com>; George, Jini Susan <JiniSusan.George@amd.com>; Sharma, Alok Kumar <AlokKumar.Sharma@amd.com>;
Tomar, Sourabh Singh <SourabhSingh.Tomar@amd.com>; Adrian Prantl <aprantl@apple.com><br>
<b>Subject:</b> Re: [llvm-dev] Seeking clarification and way forward on limited scope variables.<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">[CAUTION: External Email] <o:p></o:p></p>
<div>
<div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On Wed, Apr 15, 2020 at 11:52 AM Vedant Kumar <<a href="mailto:vedant_kumar@apple.com">vedant_kumar@apple.com</a>> wrote:<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<p class="MsoNormal">Hi Sourabh, <o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Thanks for raising this issue. To answer your question, (afaik) there isn’t anyone working on DW_AT_start_scope support in tree. We’re looking for a solution to this problem for Swift debugging, where it's important not to make a debug
location for a variable available until its (guaranteed) initialization is complete.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">If at all possible, I’d /much/ rather we use the existing location list machinery to solve this problem. Fundamentally, we’re looking for a way to express when a location for a variable becomes available, and location lists give us that
already.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">To test this out, I took the IR for your test case, replaced all calls to “dbg.declare(…)” with “dbg.value(…, DW_OP_deref)”, and compiled with `-g -O0 -mllvm -fast-isel=false` (apparently FastISel doesn’t know what to do with frame-index
dbg.values, but this is simple to fix). This seemed to work pretty well, and the DWARF looked legit:<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">0x0000005f: DW_TAG_variable<br>
DW_AT_location (DW_OP_fbreg -20)<br>
DW_AT_name ("Local")<br>
<br>
0x0000006d: DW_TAG_lexical_block<br>
DW_AT_low_pc (0x0000000100000f4f)<br>
DW_AT_high_pc (0x0000000100000f84)<br>
<br>
0x0000007a: DW_TAG_variable<br>
DW_AT_location (0x00000000<br>
[0x0000000100000f63, 0x0000000100000f8c): DW_OP_breg6 RBP-24)<br>
DW_AT_name ("Local”)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">```<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">I did find one lldb bug where it didn’t know to look up a variable in the parent scope when stopped at your second printf() call (line 6). But that's a general bug: we’d have to fix it even if we used DW_AT_start_scope.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">The upshot of sticking to location lists is that fixing any bugs we find improves optimized debugging workflows. And Swift -Onone debugging workflows as well, since Swift runs certain mandatory optimizations which can make the DWARF at
-Onone fairly complex.<o:p></o:p></p>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><br>
I think these are different problems, and that location lists can't be used to express when the scope of a variable starts - Sourabh's example at the end shows why: that the debugger wouldn't search outside the scope, it would instead (correctly) report the
variable's value as unavailable.<br>
<br>
"I did find one lldb bug where it didn’t know to look up a variable in the parent scope when stopped at your second printf() call (line 6)."<br>
<br>
I don't think that's not a bug, though/that change would be incorrect in general (& no way to differentiate the correct and incorrect places without scope_start), for example:<br>
<br>
<br>
<o:p></o:p></p>
<p class="gmail-p1" style="margin:0in;margin-bottom:.0001pt"><span class="gmail-s1"><span style="font-size:7.5pt;font-family:"Monaco",serif;color:#F2F2F2">void f1();</span></span><span style="font-size:7.5pt;font-family:"Monaco",serif;color:#F2F2F2"><br>
<span class="gmail-s1">int i;</span><o:p></o:p></span></p>
<p class="gmail-p1" style="margin:0in;margin-bottom:.0001pt;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;background-color:rgba(0,0,0,0.85)">
<span class="gmail-s1"><span style="font-size:7.5pt;font-family:"Monaco",serif;color:#F2F2F2">void f2() {</span></span><span style="font-size:7.5pt;font-family:"Monaco",serif;color:#F2F2F2"><o:p></o:p></span></p>
<p class="gmail-p1" style="margin:0in;margin-bottom:.0001pt;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;background-color:rgba(0,0,0,0.85)">
<span class="gmail-apple-converted-space"><span style="font-size:7.5pt;font-family:"Monaco",serif;color:#F2F2F2">
</span></span><span class="gmail-s1"><span style="font-size:7.5pt;font-family:"Monaco",serif;color:#F2F2F2">int i = 3;</span></span><span style="font-size:7.5pt;font-family:"Monaco",serif;color:#F2F2F2"><o:p></o:p></span></p>
<p class="gmail-p1" style="margin:0in;margin-bottom:.0001pt;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;background-color:rgba(0,0,0,0.85)">
<span class="gmail-apple-converted-space"><span style="font-size:7.5pt;font-family:"Monaco",serif;color:#F2F2F2">
</span></span><span class="gmail-s1"><span style="font-size:7.5pt;font-family:"Monaco",serif;color:#F2F2F2">f1();</span></span><span style="font-size:7.5pt;font-family:"Monaco",serif;color:#F2F2F2"><o:p></o:p></span></p>
<p class="gmail-p1" style="margin:0in;margin-bottom:.0001pt;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;background-color:rgba(0,0,0,0.85)">
<span class="gmail-apple-converted-space"><span style="font-size:7.5pt;font-family:"Monaco",serif;color:#F2F2F2">
</span></span><span class="gmail-s1"><span style="font-size:7.5pt;font-family:"Monaco",serif;color:#F2F2F2">int j;</span></span><span style="font-size:7.5pt;font-family:"Monaco",serif;color:#F2F2F2"><o:p></o:p></span></p>
<p class="gmail-p1" style="margin:0in;margin-bottom:.0001pt;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;background-color:rgba(0,0,0,0.85)">
<span class="gmail-apple-converted-space"><span style="font-size:7.5pt;font-family:"Monaco",serif;color:#F2F2F2">
</span></span><span class="gmail-s1"><span style="font-size:7.5pt;font-family:"Monaco",serif;color:#F2F2F2">i = j;</span></span><span style="font-size:7.5pt;font-family:"Monaco",serif;color:#F2F2F2"><o:p></o:p></span></p>
<p class="gmail-p1" style="margin:0in;margin-bottom:.0001pt;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;background-color:rgba(0,0,0,0.85)">
<span class="gmail-apple-converted-space"><span style="font-size:7.5pt;font-family:"Monaco",serif;color:#F2F2F2">
</span></span><span class="gmail-s1"><span style="font-size:7.5pt;font-family:"Monaco",serif;color:#F2F2F2">f1();</span></span><span style="font-size:7.5pt;font-family:"Monaco",serif;color:#F2F2F2"><o:p></o:p></span></p>
<p class="gmail-p1" style="margin:0in;margin-bottom:.0001pt;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;background-color:rgba(0,0,0,0.85)">
<span class="gmail-s1"><span style="font-size:7.5pt;font-family:"Monaco",serif;color:#F2F2F2">}</span></span><span style="font-size:7.5pt;font-family:"Monaco",serif;color:#F2F2F2"><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt"> <br>
If you search into outer scopes whenever a location list doesn't cover an address - then at the second call to 'f1' the debugger would interpret 'i' as referring to global 'i' when it should be referring to local 'i' just with no known value. Printing global
'i' could be quite confusing/misleading.<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">best,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">vedant<o:p></o:p></p>
</div>
<div>
<div>
<p class="MsoNormal"><br>
<br>
<o:p></o:p></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<p class="MsoNormal">On Apr 15, 2020, at 9:53 AM, David Blaikie via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">As always, concerned about the size growth in object files this might produce - though looks like the DWARF spec avoids the worst of this in unoptimized code by using an offset relative to the start of the enclosing scope, so it doesn't
require a relocation in that case.<br>
<br>
I have no idea what the LLVM DWARF representation for this would look like - short of making even more fine-grained scopes in the DILexicalScope hierarchy, which sounds really expensive from a memory perspective. That's really where I worry that the cost to
this feature might outweigh the benefit (& might be why no one's done this in the past) - but data should tell us that. As much as in-tree development is preferred, this might be the sort of thing worth prototyping out of tree first to see if it can be made
viable before adding the complexity to the LLVM project proper - but I'm open to ideas/suggestions.<o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On Wed, Apr 15, 2020 at 3:15 AM Sourabh Singh Tomar <<a href="mailto:sourav0311@gmail.com" target="_blank">sourav0311@gmail.com</a>> wrote:<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<div>
<p class="MsoNormal">Hello Everyone,<o:p></o:p></p>
</div>
<p class="MsoNormal"> <o:p></o:p></p>
<div>
<p class="MsoNormal">I need to have your thoughts on this.<o:p></o:p></p>
</div>
<p class="MsoNormal"> <o:p></o:p></p>
<div>
<p class="MsoNormal">Consider the following test case --<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">-------------------------------------------<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> 1 int main(int Argc, char **Argv) {<br>
2 int Local = 6;<br>
3 printf("%d\n",Local);<br>
4<br>
5 {<br>
6 printf("%d\n",Local);<br>
7 int Local = 7;<br>
8 printf("%d\n",Local);<br>
9 }<br>
10<br>
11 return 0;<br>
12 }<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">--------------------------------------------<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">When compiled in debug mode with compilers including (trunk gcc and trunk clang) and debugging with GDB at Line No.6, the following behavior is observed<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">Breakpoint 1, main (Argc=1, Argv=0x7fffffffe458) at MainScope.c:6<br>
6 printf("%d\n",Local);<br>
(gdb) print Local<br>
$1 = 2102704 -- some Garbage value, <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">(gdb) info addr Local<br>
Symbol "Local" is a variable at frame base reg $rbp offset 0+-24. -- <i>This is location of *Local* declared inside scope, but as you may notice that the variable being referred here is from the outer scope.</i><o:p></o:p></p>
</div>
<p class="MsoNormal"> <o:p></o:p></p>
<div>
<p class="MsoNormal">This problem persists with both GDB and LLDB. Since we have entered the Lexical Scope and when we try to print value of *Local*, it will look into the *current scope* and fetch the value if the variable exists in scope(in case variable
doesn't exist, GDB searches for it in the outer scope). <o:p></o:p></p>
</div>
<p class="MsoNormal"> <o:p></o:p></p>
<div>
<p class="MsoNormal">This is regardless of whether the variable has actually came into scope(or actually defined) at Line No. 7. Since DWARF already defined the location(on stack) which will be valid for the lifetime of the variable, contrary to when the variable
is actually defined(or allocated) which is in this case Line No. 7.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">---------------------------------------------<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> 0x0000006d: DW_TAG_lexical_block<br>
DW_AT_low_pc (0x00000000002016d1)<br>
DW_AT_high_pc (0x000000000020170b) <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">0x0000007a: DW_TAG_variable<br>
DW_AT_location (DW_OP_fbreg -24)<br>
DW_AT_name ("Local")<br>
DW_AT_decl_file ("MainScope.c")<br>
DW_AT_decl_line (7)<br>
DW_AT_type (0x0000008a "int")<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">----------------------------------------------<o:p></o:p></p>
</div>
<p class="MsoNormal"> <o:p></o:p></p>
<div>
<p class="MsoNormal">The DWARF specification provides the DW_AT_start_scope attribute to deal with this issue (Sec 3.9 Declarations with Reduced Scope DWARFv5). This attribute aims at limiting the scope of variables within the lexical scope in which it is defined
to from where it has been declared/ defined.<o:p></o:p></p>
</div>
<p class="MsoNormal"> <o:p></o:p></p>
<div>
<p class="MsoNormal">In order to fix this issue, we want to modify llvm so that DW_AT_start_scope is emitted for the variable in the inner block (in the above example). This limits the scope of the inner block variable to start from the point of its declaration.
<o:p></o:p></p>
</div>
<p class="MsoNormal"> <o:p></o:p></p>
<div>
<p class="MsoNormal">For POC, we inserted DW_AT_start_scope in this inner *Local* variable, resultant dwarf after this.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">-----------------------------<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">0x0000006d: DW_TAG_lexical_block<br>
DW_AT_low_pc (0x00000000002016d1)<br>
DW_AT_high_pc (0x000000000020170b)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">0x0000007a: DW_TAG_variable<br>
<b><i> DW_AT_start_scope (0x17) -- restricted within a subset(starting from the point of definition(specified as an offset)) of entire ranges covered by Lex Block.</i></b><br>
DW_AT_location (DW_OP_fbreg -24)<br>
DW_AT_name ("Local")<br>
DW_AT_decl_file ("MainScope.c")<br>
DW_AT_decl_line (7)<br>
DW_AT_type (0x00000092 "int")<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">----------------------------<o:p></o:p></p>
</div>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<div>
<p class="MsoNormal">We also modified ‘gdb’ to interpret DW_AT_start_scope so that the scope of the variable is limited from the PC where the value of DW_AT_start_scope is. If the debugger is stopped at a point within the same lexical block but at a PC before
DW_AT_start_scope, then gdb follows the normal search mechanism of searching in consecutive super blocks till it gets a match or it reaches the global block. After the modification, GDB is able to correctly show the value *6* in our example.<o:p></o:p></p>
</div>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal"> <o:p></o:p></p>
<div>
<p class="MsoNormal">After incorporating changes --<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> Breakpoint 1, main (Argc=1, Argv=0x7fffffffe458) at MainScope.c:6<br>
6 printf("%d\n",Local);<br>
(gdb) print Local<br>
<b><i>$1 = 6 --- Value retrieved from outer scope</i></b><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">(gdb) info addr Local<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">Symbol "Local" is a variable at frame base reg $rbp offset 0+-20.<o:p></o:p></p>
</div>
<p class="MsoNormal"> <o:p></o:p></p>
<div>
<p class="MsoNormal">Could you guys please let us know your thoughts or suggestions on this? Was/ Is there is an existing effort already going on to deal with this problem?
<o:p></o:p></p>
</div>
<p class="MsoNormal"> <o:p></o:p></p>
<p class="MsoNormal">Even though location lists can be used to deal with this scenario, in my experience, location lists are emitted at higher optimization levels, and with the usage of location lists in this example, gdb prints out <optimized out> (as expected)
if it is stopped at a PC in the same lexical block but before the point of declaration of the local variable. <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Thank You,<br>
Sourabh Singh Tomar.<o:p></o:p></p>
</div>
</div>
</blockquote>
</div>
<p class="MsoNormal">_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.llvm.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fllvm-dev&data=02%7C01%7Csourabhsingh.tomar%40amd.com%7Ce380b64c3f054f9c7ead08d7e179c87b%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637225785481382842&sdata=TxcGddPstXlJIBDdarp6KCKd4OqE2YJkJF2JBrSnANc%3D&reserved=0" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><o:p></o:p></p>
</div>
</blockquote>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</body>
</html>