[prev in list] [next in list] [prev in thread] [next in thread] 

List:       llvm-dev
Subject:    Re: [llvm-dev] Passing inormation from pass to lowering
From:       Reid Kleckner via llvm-dev <llvm-dev () lists ! llvm ! org>
Date:       2020-03-30 22:55:18
Message-ID: CACs=tyL=MtQqJ+kp+Wi4sYoFq-r7a3CgB+6WbO=EJvMevxkmNw () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


I'd recommend looking at how tail call lowering is implemented. The
TAILJMP* family of pseudo instructions are modelled as calls throughout
CodeGen, but later in X86MCInstLower.cpp they are translated to plain JMP
instructions. This avoids branch folding and the like from assuming things
about the control flow of blocks ending in JMP_1. Similarly, you probably
need to define a family of such call-like pseudo instructions, and lower
them later.

Alternatively, you could try to get by without new instructions, and
instead staple a bunch of extra special operands to the regular CALL*
family of instructions. Later, you would change the way such instructions
are "printed" depending on the presence of your operand. CALL* is a
variadic instruction, so you can put more or less anything in the operand
list, such as perhaps metadata. This approach runs the risk of rewriting
CALL instructions that didn't carry your special annotations, or having
other CodeGen passes fold call instructions in ways that are incompatible
with your transformation.

On Mon, Mar 30, 2020 at 1:04 PM Felix Berlakovich <felix@berlakovich.at>
wrote:

> The one thing I can‘t seem to figure out is how to lower a jump to another
> function at any stage before AsmPrinter. The methods to construct an
> unconditional branch always seem to expect a (machine) basic block and I
> don't know how to get the first basic block of **another** function.
>
>
>
> Is DAG.getNode(ISD::BR, dl, MVT::Other, ???) the right approach?
>
> And why isn't there a X86::BR, but only a X86::BRCOND?
>
>
>
> The only implementation I could find that actually creates a jump to a
> function is Readactor (https://github.com/securesystemslab/multicompiler),
> but it does it in the AsmPrinter.
>
>
>
> Thanks for your help!
>
>
>
> Regards
>
>
>
> Felix
>
>
>
> *Von:* Reid Kleckner <rnk@google.com>
> *Gesendet:* Samstag, 28. März 2020 17:42
> *An:* Felix Berlakovich <felix@berlakovich.at>
> *Cc:* llvm-dev@lists.llvm.org
> *Betreff:* Re: [llvm-dev] Passing inormation from pass to lowering
>
>
>
> I would encourage you to look at the "cfguard" bundle added for Windows
> Control Flow Integrity. AsmPrinter is probably far too late for the
> transform you want to do. You probably need to implement it somewhere in
> call lowering, so assuming this is for x86, you would implement this in
> X86ISelLowering::LowerCall.
>
>
>
> I think it would be less invasive to attach your metadata to the existing
> call instructions, and then change code generation later, rather than
> replacing the IR instructions completely.
>
> The implementation for cfguard is mostly here:
>
>
> http://github.com/llvm/llvm-project/commit/d157a9bc8ba1085cc4808c6941412322a7fd884e
>
>
>
>
> On Fri, Mar 27, 2020 at 1:28 AM Felix Berlakovich via llvm-dev <
> llvm-dev@lists.llvm.org> wrote:
>
> Hi!
>
>
>
> I have written a ModulePass that calculates various things and adds custom
> metadata attributes to certain instructions. Depending on the attributes, I
> would like to change the machine code of these instructions. For example, I
> would like to replace certain calls with jumps, but as far as I can tell
> the IR metadata is not accessible anymore on the level of machine
> instructions (e.g. in the AsmPrinter). What is the best way to pass the
> information calculated by the pass (e.g the attributes) to the part where
> the target specific instructions are emitted?
>
>
>
> Regards
>
>
>
> Felix
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev@lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>
>

[Attachment #5 (text/html)]

<div dir="ltr">I&#39;d recommend looking at how tail call lowering is implemented. \
The TAILJMP* family of pseudo instructions are modelled as calls throughout CodeGen, \
but later in X86MCInstLower.cpp they are translated to plain JMP instructions. This \
avoids branch folding and the like from assuming things about the control flow of \
blocks ending in JMP_1. Similarly, you probably need to define a family of such \
call-like pseudo instructions, and lower them \
later.<div><br></div><div>Alternatively, you could try to get by without new \
instructions, and instead staple a bunch of extra special operands to the regular \
CALL* family of instructions. Later, you would change the way such instructions are \
&quot;printed&quot; depending on the presence of your operand. CALL* is a variadic \
instruction, so you can put more or less anything in the operand list, such as \
perhaps metadata. This approach runs the risk of rewriting CALL instructions that \
didn&#39;t carry your special annotations, or having other CodeGen passes fold call \
instructions in ways that are incompatible with your \
transformation.</div></div><br><div class="gmail_quote"><div dir="ltr" \
class="gmail_attr">On Mon, Mar 30, 2020 at 1:04 PM Felix Berlakovich &lt;<a \
href="mailto:felix@berlakovich.at">felix@berlakovich.at</a>&gt; \
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_5990150016179909720WordSection1">
<p class="MsoNormal"><span \
style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)">The one \
thing I can‘t seem to figure out is how to lower a jump to another function at any \
stage before AsmPrinter. The methods to construct an unconditional  branch always \
seem to expect a (machine) basic block and I don't know how to get the first basic \
block of *<b>another</b>* function. <u></u><u></u></span></p>
<p class="MsoNormal"><span \
style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)"><u></u>  \
<u></u></span></p> <p class="MsoNormal"><span \
style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)">Is \
DAG.getNode(ISD::BR, dl, MVT::Other, ???) the right approach? \
<u></u><u></u></span></p> <p class="MsoNormal"><span \
style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)">And why \
isn't there a X86::BR, but only a X86::BRCOND? <u></u><u></u></span></p>
<p class="MsoNormal"><span \
style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)"><u></u>  \
<u></u></span></p> <p class="MsoNormal"><span \
style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)">The only \
implementation I could find that actually creates a jump to a function is Readactor \
(<a href="https://github.com/securesystemslab/multicompiler" \
target="_blank">https://github.com/securesystemslab/multicompiler</a>),  but it does \
it in the AsmPrinter.<u></u><u></u></span></p> <p class="MsoNormal"><span \
style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)"><u></u>  \
<u></u></span></p> <p class="MsoNormal"><span \
style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)">Thanks for \
your help!<u></u><u></u></span></p> <p class="MsoNormal"><span \
style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)"><u></u>  \
<u></u></span></p> <p class="MsoNormal"><span \
style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)">Regards<u></u><u></u></span></p>
 <p class="MsoNormal"><span \
style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)"><u></u>  \
<u></u></span></p> <p class="MsoNormal"><span \
style="font-size:11pt;font-family:Calibri,sans-serif;color:rgb(31,73,125)">Felix \
<u></u><u></u></span></p> <p class="MsoNormal"><span \
style="font-size:11pt;font-family:Calibri,sans-serif;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:0cm 0cm 0cm 4pt"> <div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt \
solid rgb(225,225,225);padding:3pt 0cm 0cm"> <p class="MsoNormal"><b><span \
style="font-size:11pt;font-family:Calibri,sans-serif">Von:</span></b><span \
style="font-size:11pt;font-family:Calibri,sans-serif"> Reid Kleckner &lt;<a \
href="mailto:rnk@google.com" target="_blank">rnk@google.com</a>&gt; <br>
<b>Gesendet:</b> Samstag, 28. März 2020 17:42<br>
<b>An:</b> Felix Berlakovich &lt;<a href="mailto:felix@berlakovich.at" \
target="_blank">felix@berlakovich.at</a>&gt;<br> <b>Cc:</b> llv</span><span lang="DE" \
style="font-size:11pt;font-family:Calibri,sans-serif"><a \
href="mailto:m-dev@lists.llvm.org" target="_blank">m-dev@lists.llvm.org</a><br> \
<b>Betreff:</b> Re: [llvm-dev] Passing inormation from pass to \
lowering<u></u><u></u></span></p> </div>
</div>
<p class="MsoNormal"><u></u>  <u></u></p>
<div>
<p class="MsoNormal">I would  encourage you to look at the &quot;cfguard&quot; bundle \
added for Windows Control Flow Integrity. AsmPrinter is probably far too late for the \
transform you want to do. You probably need to implement it somewhere  in call \
lowering, so assuming  this is for x86, you would implement this in \
X86ISelLowering::LowerCall.<u></u><u></u></p> <div>
<p class="MsoNormal"><u></u>  <u></u></p>
</div>
<div>
<p class="MsoNormal">I think it would be less invasive to attach your metadata to the \
existing call instructions, and then change code generation later, rather than \
replacing the IR instructions completely.<br> <br>
The implementation for cfguard is mostly here:<u></u><u></u></p>
<div>
<p class="MsoNormal"><a \
href="http://github.com/llvm/llvm-project/commit/d157a9bc8ba1085cc4808c6941412322a7fd884e" \
target="_blank">http://github.com/llvm/llvm-project/commit/d157a9bc8ba1085cc4808c6941412322a7fd884e</a> \
<u></u><u></u></p> </div>
</div>
</div>
<p class="MsoNormal"><u></u>  <u></u></p>
<div>
<div>
<p class="MsoNormal">On Fri, Mar 27, 2020 at 1:28 AM Felix Berlakovich via llvm-dev \
&lt;<a href="mailto:llvm-dev@lists.llvm.org" \
target="_blank">llvm-dev@lists.llvm.org</a>&gt; wrote:<u></u><u></u></p> </div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt \
solid rgb(204,204,204);padding:0cm 0cm 0cm 6pt;margin-left:4.8pt;margin-right:0cm"> \
<div> <div>
<p class="MsoNormal">Hi!<u></u><u></u></p>
<p class="MsoNormal">  <u></u><u></u></p>
<p class="MsoNormal">I have written a ModulePass that calculates various things and \
adds custom metadata attributes to certain instructions. Depending on the attributes, \
I would like to change the machine  code of these instructions. For example, I would \
like to replace certain calls with jumps, but as far as I can tell the IR metadata is \
not accessible anymore on the level of machine instructions (e.g. in the AsmPrinter). \
What is the best way to pass the information  calculated by the pass (e.g the \
attributes) to the part where the target specific instructions are \
emitted?<u></u><u></u></p> <p class="MsoNormal">  <u></u><u></u></p>
<p class="MsoNormal">Regards<u></u><u></u></p>
<p class="MsoNormal">  <u></u><u></u></p>
<p class="MsoNormal">Felix<u></u><u></u></p>
</div>
</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://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" \
target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><u></u><u></u></p>
 </blockquote>
</div>
</div>
</div>
</div>

</blockquote></div>


[Attachment #6 (text/plain)]

_______________________________________________
LLVM Developers mailing list
llvm-dev@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic