<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 --- - COFF writer crash with -O2 but not -O1: missing associated COMDAT section for section .CRT$XCU"
   href="https://llvm.org/bugs/show_bug.cgi?id=28220">28220</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>COFF writer crash with -O2 but not -O1: missing associated COMDAT section for section .CRT$XCU
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </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>new bugs
          </td>
        </tr>

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

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

        <tr>
          <th>CC</th>
          <td>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=16602" name="attach_16602" title="IR of foo.cpp with -O1">attachment 16602</a> <a href="attachment.cgi?id=16602&action=edit" title="IR of foo.cpp with -O1">[details]</a></span>
IR of foo.cpp with -O1

I don't actually know if this is a codegen or optimizer bug, but it only
manifests at -O2 so the optimizer at least influences it. Anyway, here's the
code:

template<typename T>
struct DumbPointer
{
    DumbPointer() : ItsNull(nullptr) {}
    T* ItsNull;
};

template<typename T>
struct __declspec(dllimport) GetMyT    // Note: dllimport needed to reproduce
{
    static DumbPointer<T> Instance;
};
template<typename T>
DumbPointer<T> GetMyT<T>::Instance; // Will cause a warning with
-Wglobal-constructors

static void* P = GetMyT<void>::Instance.ItsNull;

This compiles with clang -O1, but crashes with -O2, using: clang -cc1 -triple
x86_64-pc-windows-msvc19 -fms-compatibility -std=c++14 -emit-obj -O<1|2>
foo.cpp -o foo.obj. LLVM/Clang used is 3.9 273191 from trunk and 3.8 stable.
The error is: fatal error: error in backend: Missing associated COMDAT section
for section .CRT$XCU. This comes from WinCOFFObjectWriter.

I will attach the LLVM IR files (which I'm not very good at reading), but the
relevant difference in the generated x86 assembly seems to be that the -O2
version has an associative .CRT$XCU (global ctor) section and the -O1 version
does not. However, the full 25 MB preprocessed .cpp file I reduced this from
actually has two *added* .CRT$XCU sections when compiled with -O2 (4 with -O2
of which 3 associative, compared to 2 with -O1 of which 1 associative).
Regardless this leads to the same crash in the COFF writer.

Using opt+llc I was able to pinpoint the source of the change to the
-elim-avail-extern pass, and indeed after compiling LLVM with the first half of
the pass commented out I was able to run the entire .cpp -> .obj compilation on
both the sample and the 25 MB file using -O2 with no crash. (By the way, why is
there no way to disable a specific optimizer pass? I found OptBisect, but that
seems rather useless to me if you want to single out one pass, since it skips
every pass after the given threshold. I know you can specify the whole pass
list manually, but it would be much simpler to do -O2
-skip-pass=elim-avail-extern.)

So my question is: is the optimizer pass wrong in eliminating initializers of
globals that are dllimport, or is the COFF writer choking on something it
should be able to patch up? If it's the former, shouldn't the bitcode verifier
have been able to catch it?</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>