<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </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 --- - incompatibility w/gcc re: ODR, template instantiation, optimization across translation units"
   href="https://llvm.org/bugs/show_bug.cgi?id=27853">27853</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>incompatibility w/gcc re: ODR, template instantiation, optimization across translation units
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </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>C++
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>wallstprog@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=16403" name="attach_16403" title="demonstration of described behavior">attachment 16403</a> <a href="attachment.cgi?id=16403&action=edit" title="demonstration of described behavior">[details]</a></span>
demonstration of described behavior

We've noticed a major incompatability between clang & gcc in the way that
template classes are instantiated when optimization is in effect across
multiple translation units.

There's not much to be found in a Google search, but this post
(<a href="https://whatofhow.wordpress.com/2015/03/17/odr-rtti-dso/">https://whatofhow.wordpress.com/2015/03/17/odr-rtti-dso/</a>) seems to suggest
that this incompatibility is intentional.  

This recently bit us with code similar to attached, which was attempting to
define a single using a local static object.  The code works as expected with
g++ (4.3.3, 4.8.2, 5.3.0) at all optimization levels.  Also works as expected
with clang at -O0, but at -O3 the instance() method (and associated local
static) appear to be inlined, which appears to violate ODR(?).  The results are
consistent with clang 3.7.0, trunk(r264914) and trunk(r269323).

The results of running the attached code with different compilers and
optimization levels:

$ CXX=g++ ./test.sh -O0
test1.so:0000000000000717 W Singleton<Test>::instance()
test2.so:0000000000000717 W Singleton<Test>::instance()
test1.so:0000000000200a80 u Singleton<Test>::instance()::obj
test2.so:0000000000200a80 u Singleton<Test>::instance()::obj
$ CXX=g++ ./test.sh -O1
test1.so:0000000000200910 u Singleton<Test>::instance()::obj
test2.so:0000000000200910 u Singleton<Test>::instance()::obj
$ CXX=g++ ./test.sh -O2
test1.so:0000000000200910 u Singleton<Test>::instance()::obj
test2.so:0000000000200910 u Singleton<Test>::instance()::obj
$ CXX=g++ ./test.sh -O3
test1.so:0000000000200910 u Singleton<Test>::instance()::obj
test2.so:0000000000200910 u Singleton<Test>::instance()::obj
$ CXX=clang++ ./test.sh -O0
test1.so:0000000000000770 W Singleton<Test>::instance()
test2.so:0000000000000770 W Singleton<Test>::instance()
test1.so:0000000000200af0 V Singleton<Test>::instance()::obj
test2.so:0000000000200af0 V Singleton<Test>::instance()::obj
$ CXX=clang++ ./test.sh -O1
test1.so:0000000000000740 W Singleton<Test>::instance()
test2.so:0000000000000740 W Singleton<Test>::instance()
test1.so:0000000000200a90 V Singleton<Test>::instance()::obj
test2.so:0000000000200a90 V Singleton<Test>::instance()::obj
$ CXX=clang++ ./test.sh -O2
$ CXX=clang++ ./test.sh -O3
$

So, I have the following questions:
- Is this behavior intended?
- Is this behavior correct?

Any references to rationale for this discrepancy would be much appreciated.

Thanks!</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>