<div dir="ltr">I agree, for the reasons you outline, LLVM needs to do more to support producing DLLs on Windows. However, I don't really have time to dig into your proposal and the issues you are running into.<div><br></div><div>Personally, I think LLVM should move away from that .def file generation python script, and towards source-level annotations. It would allow us to use fvisibility=hidden in the shared library build on other platforms. That would greatly reduce the number of dynamic symbols in LLVM, which I understand is desirable. Today we used Bsymbolic-functions, so we may have already captured most of the benefits of fvisibility=hidden, but hidden visibility is always better for performance and binary size.</div><div><br></div><div>I put out this alternative proposal mainly to see if there is any enthusiasm for it. If not, you are welcome to continue forward with our existing solutions. But if there is broad interest in adding API annotations to LLVM, I think that would be a better way to go long term.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, May 30, 2021 at 7:17 AM Cristian Adam via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.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">

  
  <div>
    <p>Hi,</p>
    <p>Clang 12 can be configured on Windows with MinGW (either GNU or
      LLVM) with the following CMake parameters:<br>
    </p>
    <ul>
      <li><font face="monospace">LLVM_BUILD_LLVM_DYLIB=ON</font></li>
      <li><font face="monospace">LLVM_LINK_LLVM_DYLIB=ON</font></li>
      <li><font face="monospace">CLANG_LINK_CLANG_DYLIB=ON</font></li>
    </ul>
    <p>which has some effect on the binary size of the build.</p>
    <p>I configured the llvm-project with the following parameters:</p>
    <ul>
      <li><font face="monospace">CMAKE_BUILD_TYPE=Release</font></li>
      <li><font face="monospace">LLVM_TARGETS_TO_BUILD=X86</font></li>
      <li><font face="monospace">LLVM_ENABLE_PROJECTS=clang;clang-tools-extra</font><br>
      </li>
    </ul>
    <p>The installed (stripped) build of Clang 12 with <a href="https://github.com/mstorsjo/llvm-mingw/releases/tag/20210423" target="_blank">llvm-mingw
        release 12.0.0</a> resulted in:</p>
    <ul>
      <li>Normal build: 1,76 GB<br>
      </li>
      <li>shlib build: 481 MB</li>
    </ul>
    <p>Due to the nature of MSVC regarding default visibility of symbols
      (hidden by default, whereas MinGW has visible by default), one
      needs to generate a .def file with the symbols needed to be
      exported.</p>
    <p>This is done already in two cases for <font face="monospace">LLVM_BUILD_LLVM_C_DYLIB
      </font>(llvm/tools/llvm-shlib/gen-msvc-exports.py) and for <font face="monospace">LLVM_EXPORT_SYMBOLS_FOR_PLUGINS </font>(llvm/utils/extract_symbols.py).</p>
    <p>I've put together a <a href="https://github.com/cristianadam/llvm-project/commit/3a3b8a7df17a49ba7c0153b0c9a7ee25705ede46" target="_blank">patch</a>
      that enables LLVM_DYLIB and CLANG_DYLIB for MSVC.</p>
    <p>I tested with clang-cl from the official Clang 12 x64 Windows
      binary release:</p>
    <ul>
      <li>Normal build: 1,42 GB</li>
      <li>shlib build: 536 MB</li>
    </ul>
    <p>The shlib release build compiled and linked fine with LLVM.dll
      and clang-cpp.dll, unfortunately it crashes at runtime. For
      example llvm-nm:</p>
    <p><font face="monospace">$ llvm-nm<br>
        PLEASE submit a bug report to <a href="https://bugs.llvm.org/" target="_blank">https://bugs.llvm.org/</a> and include
        the crash backtrace.<br>
        Stack dump:<br>
        0.      Program arguments: llvm-nm<br>
        #0 0x00007ffd32807d43 llvm::StringMap<llvm::cl::Option
        *,llvm::MallocAllocator>::begin
        C:\Projects\llvm-project\repo\llvm\include\llvm\ADT\StringMap.h:204:0<br>
        #1 0x00007ffd32807d43 llvm::cl::HideUnrelatedOptions(class
        llvm::cl::OptionCategory &, class llvm::cl::SubCommand
        &)
        C:\Projects\llvm-project\repo\llvm\lib\Support\CommandLine.cpp:2589:0<br>
        #2 0x00007ff689df2b13 llvm::StringRef::StringRef
        C:\Projects\llvm-project\repo\llvm\include\llvm\ADT\StringRef.h:107:0<br>
        #3 0x00007ff689df2b13 main
        C:\Projects\llvm-project\repo\llvm\tools\llvm-nm\llvm-nm.cpp:2232:0<br>
        #4 0x00007ff689e26d04 invoke_main
D:\agent\_work\10\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78:0<br>
        #5 0x00007ff689e26d04 __scrt_common_main_seh
D:\agent\_work\10\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288:0<br>
        #6 0x00007ffd9a7f7034 (C:\Windows\System32\KERNEL32.DLL+0x17034)<br>
        #7 0x00007ffd9b742651 (C:\Windows\SYSTEM32\ntdll.dll+0x52651)</font><br>
    </p>
    <p>This crash is due to <font face="monospace">llvm::cl::HideUnrelatedOptions
      </font>which accesses<font face="monospace"> TopLevelSubCommand, </font>which
      is defined as<font face="monospace">:<br>
        extern ManagedStatic<SubCommand> TopLevelSubCommand;<br>
      </font></p>
    <p>The MSVC 2019 build behaves in the same way as the clang-cl
      build.</p>
    <p>I have tried building without <font face="monospace">LLVM_ENABLE_THREADS</font>,
      or by linking to the CRT statically <font face="monospace">LLVM_USE_CRT_RELEASE=MT</font>,
      didn't help.</p>
    <p>The MSVC 2019 build sizes were:</p>
    <ul>
      <li>Normal build: 1,74 GB</li>
      <li>shlib build: 949 MB</li>
    </ul>
    <p>I would appreciate any help in getting the shlib build running.
      It works fine with llvm-mingw, I think it should also work with
      clang-cl / cl.<br>
      <br>
      Cheers,<br>
      Cristian.<br>
    </p>
  </div>

_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>