<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Tue, Oct 9, 2018 at 9:39 PM Friedman, Eli <<a href="mailto:efriedma@codeaurora.org">efriedma@codeaurora.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">On 10/9/2018 11:31 AM, Gleb Popov via llvm-dev wrote:<br>
> Hello LLVM Devs.<br>
><br>
> In my compiler I attach some arbitrary data to functions by creating <br>
> BBs with inline assembly. However, these blocks are "unused" from LLVM <br>
> point of view and get erased from the function.<br>
><br>
> To counter that I started adding checks for conditions that are <br>
> guaranteed to be true or false. I ended up with calling <br>
> @llvm.returnaddress(i32 0) intrinsic and comparing the result with 0. <br>
> It worked well until in one function I had two such calls and SROA <br>
> replaced one of checks with constant 1 and erased the BB.<br>
><br>
> I should probably stop trying to fool LLVM and "do it right", but <br>
> don't have any idea how. Note that I can't use global variables for a <br>
> reason, so the data has to be encoded in a BB using inline assembly. <br>
> All I need is just prevent optimizations from erasing it.<br>
<br>
A reachable inline asm won't be erased if LLVM thinks it has some <br>
side-effect.  The simplest way to do this is the "sideeffect" marking <br>
(in C++, it's a parameter to InlineAsm::get()).  See <br>
<a href="http://llvm.org/docs/LangRef.html#inline-assembler-expressions" rel="noreferrer" target="_blank">http://llvm.org/docs/LangRef.html#inline-assembler-expressions</a> .<br></blockquote><div><br></div><div>The problem is exactly reachability. Here is a simple example:</div><div><br></div><div>define void @foo() {<br>entry:</div><div>  ...</div><div>  ret void</div><div>data:</div><div>  call void asm sideeffect inteldialect ".byte 0xB2", "~{dirflag},~{fpsr},~{flags}"()</div><div>  call void asm sideeffect inteldialect ".byte 0xB9", "~{dirflag},~{fpsr},~{flags}"()</div><div>  ...<br></div><div>}</div><div><br></div><div>To make "data" reachable I change entry's terminator to br %tobool, label %exit, label %data, where %tobool is a result of icmp eq that is always true. However, I can't come up with such a condition that didn't get erased by SROA.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
-Eli<br>
<br>
-- <br>
Employee of Qualcomm Innovation Center, Inc.<br>
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project<br>
<br>
</blockquote></div></div></div></div></div>