<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - GHC calling convention: Function prologue frame pointer invalidates the %rbp register"
   href="https://bugs.llvm.org/show_bug.cgi?id=45006">45006</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>GHC calling convention: Function prologue frame pointer invalidates the %rbp register
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>9.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Backend: X86
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>mail@daniel-mendler.de
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>craig.topper@gmail.com, llvm-bugs@lists.llvm.org, llvm-dev@redking.me.uk, spatel+llvm@rotateright.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=23165" name="attach_23165" title="Includes avx/rt.bc, avx/rt.s, no-avx/rt.bc, no-avx/rt.s">attachment 23165</a> <a href="attachment.cgi?id=23165&action=edit" title="Includes avx/rt.bc, avx/rt.s, no-avx/rt.bc, no-avx/rt.s">[details]</a></span>
Includes avx/rt.bc, avx/rt.s, no-avx/rt.bc, no-avx/rt.s

I am observing a bug on x86-64 using the GHC calling convention (cc 10), which
only
occurs if AVX is enabled.

The register %rbp is reserved by the GHC calling convention, however LLVM
creates
a frame pointer in the prologue, invalidating the %rbp register if AVX is
enabled.
The %rbp register is only restored in the epilogue of the function.

This bug has been observed previously in GHC. However it seems the bug was only
triggered
by GHC with LLVM 3.2. The bug is still present in LLVM 9, which I am using
right now.
Note that I am not using GHC for code generation, but another compiler, which
produces similar code
and relies on the GHC calling convention.
See <a href="https://gitlab.haskell.org/ghc/ghc/issues/9391">https://gitlab.haskell.org/ghc/ghc/issues/9391</a> for the original bug report
on the GHC bugtracker.

I am appending the following test files:

avx/rt.bc
avx/rt.s generated via llc-9 from avx/rt.bc
no-avx/rt.bc
no-avx/rt.s generated via llc-9 from avx/rt.bc

The miscompiled function is interpInitModuleCont:

~~~
define internal ghccc i32 @interpInitModuleCont(%struct._ChiRegStore_* noalias,
i64* noalias, i64* noalias, i64* noalias, i64, i64, i64, i64, i64, i64)
~~~

Compiled with AVX enabled:

~~~
interpInitModuleCont:
        pushq   %rbp
        movq    %rsp, %rbp <~~~ %rbp is invalidated
        ...
        movq    %rbp, %rsp
        popq    %rbp       <~~~ %rbp is restored
        vzeroupper
        jmpq    *%rax                   # TAILCALL
~~~

If AVX is disabled using -mno-avx in the frontend, the invalid
prologue/epilogue is not generated, the %rbp register is not invalidated
and things work as expected. The resulting program runs fine.
I did not test other architectures supporting the GHC calling convention,
but I suspect that the bug is x86 specific due the logic in
X86FrameLowering::hasFP.

$ llc-9 -version
LLVM (<a href="http://llvm.org/">http://llvm.org/</a>):
  LLVM version 9.0.1

  Optimized build.
  Default target: x86_64-pc-linux-gnu
  Host CPU: skylake</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>