<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none"><!--P{margin-top:0;margin-bottom:0;} @font-face
        {font-family:Wingdings}
@font-face
        {font-family:"Cambria Math"}
@font-face
        {font-family:Calibri}
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Calibri",sans-serif}
a:link, span.MsoHyperlink
        {color:#0563C1;
        text-decoration:underline}
a:visited, span.MsoHyperlinkFollowed
        {color:#954F72;
        text-decoration:underline}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {margin-top:0in;
        margin-right:0in;
        margin-bottom:0in;
        margin-left:.5in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Calibri",sans-serif}
span.EmailStyle17
        {font-family:"Calibri",sans-serif;
        color:windowtext}
.MsoChpDefault
        {font-family:"Calibri",sans-serif}
@page WordSection1
        {margin:1.0in 1.0in 1.0in 1.0in}
div.WordSection1
        {}
ol
        {margin-bottom:0in}
ul
        {margin-bottom:0in}--></style>
</head>
<body dir="ltr" style="font-size:12pt;color:#000000;background-color:#FFFFFF;font-family:Calibri,Arial,Helvetica,sans-serif;">
<p>Adding an "immarg" attribute makes sense; thanks for working on this.</p>
<p><br>
</p>
<p>-Eli<br>
</p>
<p><br>
</p>
<div style="color: rgb(33, 33, 33);">
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="divRplyFwdMsg" dir="ltr"><font style="font-size:11pt" face="Calibri, sans-serif" color="#000000"><b>From:</b> llvm-dev <llvm-dev-bounces@lists.llvm.org> on behalf of Arsenault, Matthew via llvm-dev <llvm-dev@lists.llvm.org><br>
<b>Sent:</b> Tuesday, February 5, 2019 5:58 AM<br>
<b>To:</b> llvm-dev<br>
<b>Subject:</b> [EXT] [llvm-dev] [RFC] Enforcing immediate operands for intrinsics</font>
<div> </div>
</div>
<div>
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt">Hi,</span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"> </span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">I would like to solve the longstanding need for a way to indicate which parameters to an intrinsic are required to be immediates. It should be possible to declare in tablegen which parameters must be a trivial
 constant, or else the IR is invalid.</span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"> </span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">The verifier could then reject invalid intrinsic calls, so code handling the intrinsics doesn’t need to worry about invalid arguments. Currently any code that deals with such intrinsics needs to do type checks
 on the argument to avoid crashing on valid IR. This isn’t done particularly consistently (e.g. see r352904, or the follow-up r353097 for a recent example fix). From the codegen side, we do things like folding invalid intrinsic calls to undef during custom
 lowering, which is more boilerplate which shouldn’t be necessary.</span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"> </span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">It’s also necessary in a few some passes to know it’s illegal to replace an argument with a constant. llvm::canReplaceOperandWithVariable currently has to conservatively assume any intrinsic arguments can’t
 be touched.</span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"> </span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">I have 2 versions of partial implementations of this.</span></p>
<ol start="1" style="margin-top:0in" type="1">
<li class="MsoListParagraph" style="margin-left:0in"><span style="font-size:11.0pt">Uses a new intrinsic query table to return a bitmask of which operands need to be constant</span></li><li class="MsoListParagraph" style="margin-left:0in"><span style="font-size:11.0pt">Introduces a new parameter attribute</span></li></ol>
<p class="MsoNormal"><span style="font-size:11.0pt"> </span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">My current preference is for option 2. I initially expected to create the table, but then I was creating an uglier way of tracking parameter properties that exactly tracked alongside the attribute handling.
 It seems cleaner to just put it there, even though it seems a bit overkill and looks slightly strange.</span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"> </span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">The rules for the attribute will look like:</span></p>
<ul style="margin-top:0in" type="disc">
<li class="MsoListParagraph" style="margin-left:0in"><span style="font-size:11.0pt">Only allowed on intrinsics declarations. It is not allowed on an arbitrary function</span></li><li class="MsoListParagraph" style="margin-left:0in"><span style="font-size:11.0pt">Not allowed on individual call sites</span></li><li class="MsoListParagraph" style="margin-left:0in"><span style="font-size:11.0pt">The parameter must be a trivial constant leaf (i.e. ConstantInt, ConstantFP, or Undef). No aggregates or vectors are allowed</span></li><li class="MsoListParagraph" style="margin-left:0in"><span style="font-size:11.0pt">It will be incompatible with all other parameter attributes such as sret or returned</span></li></ul>
<p class="MsoNormal"><span style="font-size:11.0pt"> </span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">For bikeshedding the name, I’m currently calling the attribute “constant”, but I think this is a bad name. It doesn’t allow arbitrary constants (such as ConstantExprs), so I think something more like “immarg”
 ‘or “immediate” would be better.</span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"> </span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">-Matt</span></p>
</div>
</div>
</div>
</body>
</html>