<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 - [LTO] -fwhole-program-vtables causes bad generated code in hybrid lto/non-lto build"
   href="https://bugs.llvm.org/show_bug.cgi?id=45712">45712</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[LTO] -fwhole-program-vtables causes bad generated code in hybrid lto/non-lto build
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Windows NT
          </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>Interprocedural Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>sylvain.audi@ubisoft.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, peter@pcc.me.uk, rnk@google.com, tejohnson@google.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>When trying to enable Thin-LTO on one of our games, we found invalid generated
code which caused an infinite loop when we enabled -the
`-fwhole-program-vtables`.
The bug also appeared when we tried LTO.

I mentionned this bug in comments of D55153

Note that we build the game against some libraries that were not built with
LTO/ThinLTO.


Here it is, reduced as a relatively small repro:

$cat main.cpp
#include "inc.h"
extern void UseExternal();
int main() {
  UseExternal();
  return 0;
}

$cat inc.h
class A {
public:
  virtual void f() {
    pNext->f();
  }
  A* pNext = nullptr;
};

class B : public A {
public:
  virtual void f() {}
};

$cat external.cpp
#include "inc.h"
static A s_A;
static B s_B;
void UseExternal() {
  s_A.pNext = &s_B;
  (&s_A)->f();
}

$cat def.cpp
#include "inc.h"
A g_A;

$clang-cl main.cpp def.cpp -c /Z7 /O2 /GR- -flto -fwhole-program-vtables
$clang-cl extern.cpp -c /Z7 /O2 /GR-
$lld-link /NODEFAULTLIB /ENTRY:main /OUT:main.exe def.obj main.obj external.obj

In the generated code, we can see that A::f() is generated as an endless loop.

14000103f: cc       int3
140001040: eb fe    jmp    0x140001040     ; endless loop
140001042: cc       int3 

It looks like it was wrongly devirtualized:
  virtual void f() {
    pNext->f();
  }
  where pNext->f() becomes an endless recursion.

IMPORTANT: The bug appears only if def.obj is the first object in the lld-link
command.

This bug is pretty fragile to reproduce.
The repro above is with clang-cl / lld-link; I didn't manage to repro it under
clang/ld.lld.</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>