<div dir="ltr">There's a related problem. Possibly we can solve both at the same time.<div><br></div><div>There is currently a gaping hole in the IR-level semantics for module-level asm statements. Those global asm statements are quite commonly used to define global symbols. However, the set of symbols defined by an object file is quite important for the compiler to know and reason about, even before the assembler has run. This is true *especially* when we're doing linking before assembling (as with LTO). <div><br></div><div>We do not currently have any way to annotate what symbols a global module-level asm defines.</div><div><br></div><div>We currently hack around things by essentially running the assembler backend in order to list the symbols defined by IR. Which is a _super-ugly_ layering violation and leads to the ridiculous situation where "llvm-ar" including all the assembler backends.</div><div><br></div><div>To (eventually) fix this, I think it should be somehow required at the IR level to declare the names of all symbols defined by an asm statement.</div><div><br></div><div>But, if we do that at the IR level, what do we then do in clang to populate it, since the C-level syntax doesn't have that? Well, it'd be great if we could add C-level syntax to specify it. But, I'm not sure we can reasonably add that, and even if we can, it'd be an eternity  before it could be _required_. So, then, that just means clang instead needs to be the one scanning the asm to find symbol definitions, in order to generate the proper IR. While still ugly, that seems like a better situation than what we have now.</div><div><br></div><div><br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Feb 14, 2019 at 1:06 PM via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</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 lang="EN-US">
<div class="gmail-m_5794905577885792697WordSection1">
<p class="MsoNormal"><span style="color:rgb(31,73,125)">hi Todd,<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)">I am no expert in the area of asm statements, and they are not a clang-specific extension AFAIK, so whatever syntactic and semantic changes might be necessary ought to be specified in conjunction with the other
 compiler projects that support the feature.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)">I am not the right person to pursue this; mainly I wanted to call out that this kind of request has come up multiple times and so we should do something about it.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)">--paulr<u></u><u></u></span></p>
<p class="MsoNormal"><a name="m_5794905577885792697__MailEndCompose"><span style="color:rgb(31,73,125)"><u></u> <u></u></span></a></p>
<div style="border-top:none;border-right:none;border-bottom:none;border-left:1.5pt solid blue;padding:0in 0in 0in 4pt">
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(181,196,223);padding:3pt 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10pt;font-family:Tahoma,sans-serif">From:</span></b><span style="font-size:10pt;font-family:Tahoma,sans-serif"> Snider, Todd [mailto:<a href="mailto:t-snider@ti.com" target="_blank">t-snider@ti.com</a>]
<br>
<b>Sent:</b> Thursday, February 14, 2019 12:53 PM<br>
<b>To:</b> Robinson, Paul; <a href="mailto:efriedma@quicinc.com" target="_blank">efriedma@quicinc.com</a><br>
<b>Cc:</b> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<b>Subject:</b> RE: [llvm-dev] [RFC] Potential extension to asm statement functionality<u></u><u></u></span></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)">Hi Paul,<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)">Regarding the “No Touchie!” constraint idea for asm statements: would this be a new qualifier (like volatile) that could be applied to the asm statement?  <u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)">Since the constraint is not necessarily associated with an input or output operand, it seems that introducing the constraint via the qualifier field might work.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)">All,<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)">The volatile qualifier on an asm statement already indicates that the statement should be assumed to have side effects.
<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)">Could we extend the functionality of volatile to also mean the asm statement is not duplicable?<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)">~ Todd<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)"><u></u> <u></u></span></p>
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(181,196,223);padding:3pt 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10pt;font-family:Tahoma,sans-serif">From:</span></b><span style="font-size:10pt;font-family:Tahoma,sans-serif"> Snider, Todd
<br>
<b>Sent:</b> Wednesday, February 13, 2019 11:40 AM<br>
<b>To:</b> '<a href="mailto:paul.robinson@sony.com" target="_blank">paul.robinson@sony.com</a>'; <a href="mailto:efriedma@quicinc.com" target="_blank">efriedma@quicinc.com</a><br>
<b>Cc:</b> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<b>Subject:</b> RE: [llvm-dev] [RFC] Potential extension to asm statement functionality<u></u><u></u></span></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)">The proposed “lbl” constraint below:
<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)">   </span><span style="font-size:9pt;font-family:"Courier New";color:rgb(31,73,125)">__asm __volatile__ (“\t.global\t%0\n%0:\n” : “lbl” (my_hook_fcn));<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9pt;font-family:"Courier New";color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)">is not quite a “No Touchie!” constraint, but it does allow the user to set the isNotDuplicable flag on the INLINEASM that comes out of the asm statement in order to circumvent optimizations like Tail Duplication. 
 But setting the isNotDuplicable flag is not really enough. If the function that contains the “lbl” constrained asm statement is inlined into a function in the same compilation unit, the compiler will error out with a symbol redefinition error.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)">We may need something on the level of a “No Touchie!” constraint.
<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)">Perhaps a new builtin function that resolves to a global label definition is a better alternative for this use case?<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)">~ Todd<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)"><u></u> <u></u></span></p>
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(181,196,223);padding:3pt 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10pt;font-family:Tahoma,sans-serif">From:</span></b><span style="font-size:10pt;font-family:Tahoma,sans-serif"> <a href="mailto:paul.robinson@sony.com" target="_blank">paul.robinson@sony.com</a> [mailto:<a href="mailto:paul.robinson@sony.com" target="_blank">paul.robinson@sony.com</a>]
<br>
<b>Sent:</b> Tuesday, February 12, 2019 2:32 PM<br>
<b>To:</b> Snider, Todd; <a href="mailto:efriedma@quicinc.com" target="_blank">efriedma@quicinc.com</a><br>
<b>Cc:</b> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<b>Subject:</b> [EXTERNAL] RE: [llvm-dev] [RFC] Potential extension to asm statement functionality<u></u><u></u></span></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)">This tactic has been tried before, I'm pretty sure.  It's an attempt to do simple instrumentation without having to hack the compiler itself (you do some manual coding or preprocessing, and poof you get useful
 info in your object file).  But our compiler is too clever.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)">Some sort of "No Touchee!" constraint would help this use-case a lot.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)">--paulr<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)"><u></u> <u></u></span></p>
<div style="border-top:none;border-right:none;border-bottom:none;border-left:1.5pt solid blue;padding:0in 0in 0in 4pt">
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(181,196,223);padding:3pt 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10pt;font-family:Tahoma,sans-serif">From:</span></b><span style="font-size:10pt;font-family:Tahoma,sans-serif"> llvm-dev [mailto:<a href="mailto:llvm-dev-bounces@lists.llvm.org" target="_blank">llvm-dev-bounces@lists.llvm.org</a>]
<b>On Behalf Of </b>Snider, Todd via llvm-dev<br>
<b>Sent:</b> Tuesday, February 12, 2019 3:06 PM<br>
<b>To:</b> Eli Friedman; <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<b>Subject:</b> Re: [llvm-dev] [RFC] Potential extension to asm statement functionality<u></u><u></u></span></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)">The team I am working with is using asm statements containing label definitions as a way of instrumentation so that when an application is loaded into their debug and test framework, the labels will cause breakpoints
 to be set at strategic points where they can query the state of the processor that the application is running on.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)">~ Todd<u></u><u></u></span></p>
<p class="MsoNormal"><span style="color:rgb(31,73,125)"><u></u> <u></u></span></p>
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(181,196,223);padding:3pt 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10pt;font-family:Tahoma,sans-serif">From:</span></b><span style="font-size:10pt;font-family:Tahoma,sans-serif"> Eli Friedman [mailto:<a href="mailto:efriedma@quicinc.com" target="_blank">efriedma@quicinc.com</a>]
<br>
<b>Sent:</b> Tuesday, February 12, 2019 1:36 PM<br>
<b>To:</b> Snider, Todd; <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<b>Subject:</b> [EXTERNAL] RE: [llvm-dev] [RFC] Potential extension to asm statement functionality<u></u><u></u></span></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Can you go into a bit more detail about why someone would want to do this, as opposed to just writing a file scope inline asm, or a separate file in assembly?  I can’t think of any practical use for the fact that the label is “inside” the
 function body.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">-Eli<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<div style="border-top:none;border-right:none;border-bottom:none;border-left:1.5pt solid blue;padding:0in 0in 0in 4pt">
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 0in 0in">
<p class="MsoNormal"><b>From:</b> llvm-dev <<a href="mailto:llvm-dev-bounces@lists.llvm.org" target="_blank">llvm-dev-bounces@lists.llvm.org</a>> <b>On Behalf Of
</b>Snider, Todd via llvm-dev<br>
<b>Sent:</b> Tuesday, February 12, 2019 10:18 AM<br>
<b>To:</b> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<b>Subject:</b> [EXT] [llvm-dev] [RFC] Potential extension to asm statement functionality<u></u><u></u></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Suppose a programmer wants to inject their own global label definition into the body of a function with some guarantee that it will not be removed by the compiler.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">One way to do this is to define a global label with an asm statement knowing that the asm statement will not be invoked until after the compiler’s optimization passes have run, but the following case demonstrates that a label defined with
 an asm statement is still susceptible to being duplicated:<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><span style="font-size:9pt;font-family:"Courier New"">#include <stdint.h><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9pt;font-family:"Courier New"">uint32_t f(uint32_t x);<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9pt;font-family:"Courier New"">uint32_t g(uint32_t x);<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9pt;font-family:"Courier New""><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:9pt;font-family:"Courier New"">uint32_t f(uint32_t x) {<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9pt;font-family:"Courier New"">    uint32_t returnValue = g(x);<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9pt;font-family:"Courier New""><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:9pt;font-family:"Courier New"">    if (returnValue > 0U) {<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9pt;font-family:"Courier New"">        returnValue = 0x40000000;<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9pt;font-family:"Courier New"">    }<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9pt;font-family:"Courier New"">    else {<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9pt;font-family:"Courier New"">        returnValue = 0x80000000;<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9pt;font-family:"Courier New"">    }<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9pt;font-family:"Courier New"">    __asm __volatile__ ("\t.global my_hook_fcn\n<span style="background:yellow">my_hook_fcn:</span>\n");<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9pt;font-family:"Courier New"">    return returnValue;<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9pt;font-family:"Courier New"">}<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9pt;font-family:"Courier New""><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:9pt;font-family:"Courier New"">uint32_t g(uint32_t x) {<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9pt;font-family:"Courier New"">    return x >> 1U;<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:9pt;font-family:"Courier New"">}<u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">If the above definition of f() is compiled with optimization at level 1 or higher, the TailDuplication optimization pass will duplicate and move the asm statement and return up into the if block and the else block before the asm statement
 is invoked. When the now duplicate asm statements are later invoked, the compiler will detect a symbol redefinition error.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">To address this situation, the asm statement functionality could be extended to comprehend whether it contains a label definition, and if it does, to disallow duplication of the asm statement.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">There are a couple of different approaches that could be taken to implement this:<u></u><u></u></p>
<ol style="margin-top:0in" start="1" type="1">
<li class="MsoNormal">Parse the content of the assembly string argument to the asm statement in the compiler front-end (during EmitAsmStmt(), for example) to determine if it contains a label definition, and if it does set the
 isNotDuplicable flag on the INLINEASM record that is created to represent the asm statement in the IR. To date, there is no precedence for processing the content of the assembly string argument until the asm statement is invoked before the integrated  assembler
 starts processing the generated machine code.<u></u><u></u></li><li class="MsoNormal">Add a label constraint to the input and output operand syntax for asm statements. i.e.<u></u><u></u></li></ol>
<p class="MsoNormal" style="margin-left:1in"><span style="font-size:9pt;font-family:"Courier New"">__asm __volatile__ (“\t.global\t%0\n%0:\n” : “lbl” (my_hook_fcn));<u></u><u></u></span></p>
<p class="MsoNormal" style="margin-left:0.5in">The “lbl” constraint would tell the compiler to mark the asm statement as isNotDuplicable when an INLINEASM record is created to represent it<u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in"><u></u> <u></u></p>
<p class="MsoNormal">There are alternatives to using an asm statement for this purpose, but I wondered if such an extension would be useful/valuable to the wider community.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Thoughts? Opinions?<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Todd Snider<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Compiler Group<u></u><u></u></p>
<p class="MsoNormal">Texas Instruments Incorporated<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>

_______________________________________________<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://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>