<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <div class="moz-cite-prefix">On 10/9/2018 1:50 PM, Gleb Popov wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CALH631mVEpP-Y_U_otsD7o4gONXy9J3=c5908cQ27Ptp8c=0_A@mail.gmail.com">
      <meta http-equiv="content-type" content="text/html; charset=utf-8">
      <div dir="ltr"><br>
        <br>
        <div class="gmail_quote">
          <div dir="ltr">On Tue, Oct 9, 2018 at 11:42 PM Friedman, Eli
            <<a href="mailto:efriedma@codeaurora.org"
              moz-do-not-send="true">efriedma@codeaurora.org</a>>
            wrote:<br>
          </div>
          <blockquote class="gmail_quote" style="margin:0 0 0
            .8ex;border-left:1px #ccc solid;padding-left:1ex">
            <div text="#000000" bgcolor="#FFFFFF">
              <div class="m_-3403561980851300129moz-cite-prefix">On
                10/9/2018 1:03 PM, Gleb Popov wrote:<br>
              </div>
              <blockquote type="cite">
                <div dir="ltr"><br>
                  <br>
                  <div class="gmail_quote">
                    <div dir="ltr">On Tue, Oct 9, 2018 at 10:39 PM
                      Friedman, Eli <<a
                        href="mailto:efriedma@codeaurora.org"
                        target="_blank" moz-do-not-send="true">efriedma@codeaurora.org</a>>
                      wrote:<br>
                    </div>
                    <blockquote class="gmail_quote" style="margin:0 0 0
                      .8ex;border-left:1px #ccc solid;padding-left:1ex">
                      <div text="#000000" bgcolor="#FFFFFF">
                        <div
                          class="m_-3403561980851300129m_4919720555699682535moz-cite-prefix">On
                          10/9/2018 11:58 AM, Gleb Popov wrote:<br>
                        </div>
                        <blockquote type="cite">
                          <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"
                                        target="_blank"
                                        moz-do-not-send="true">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"
                                        moz-do-not-send="true">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>
                                </div>
                              </div>
                            </div>
                          </div>
                        </blockquote>
                        <br>
                        Even if you manage to trick LLVM into emitting
                        the inline asm, it won't be in a predictable
                        location in the emitted assembly; some LLVM
                        transforms will rearrange the code in a
                        function.<br>
                      </div>
                    </blockquote>
                    <div><br>
                    </div>
                    <div>Won't @llvm.returnaddress() always get me
                      correct location of my inline asm block?</div>
                  </div>
                </div>
              </blockquote>
              <br>
              I'm very confused... how could you possibly use
              @llvm.returnaddress to return the address of a block of
              code that's never executed?<br>
            </div>
          </blockquote>
          <div><br>
          </div>
          <div>Sorry, it is a typo. I meant blockaddress constant, not
            @llvm.returnaddress intrinsic.</div>
        </div>
      </div>
    </blockquote>
    <br>
    Oh, that makes more sense.  Still, there isn't any rule that
    prohibits LLVM from inserting code there; you're basically just
    hoping to get lucky.  (blockaddress is designed to be used with
    indirectbr; any other use is probably wrong.)<br>
    <br>
    And given you're explicitly storing the address of the data anyway,
    I'm not sure what you're trying to accomplish by embedding the data
    using inline asm; there are simpler ways to ensure your data is
    placed in the same section.<br>
    <br>
    -Eli<br>
    <pre class="moz-signature" cols="72">-- 
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project</pre>
  </body>
</html>