llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.75k stars 11.89k forks source link

ThinLTO on windows produces invalid code. #34257

Open llvmbot opened 7 years ago

llvmbot commented 7 years ago
Bugzilla Link 34909
Version unspecified
OS Windows NT
Reporter LLVM Bugzilla Contributor
CC @pcc

Extended Description

  1. Build an x86 Release version of clang using the standard MSVC compiler and linker.

(From an x86 MSVC build environment) cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_ENABLE_PROJECTS=clang ninja clang

  1. Build an x64 Release version of lld using the standard MSVC compiler and linker.

    (From an x64 MSVC build environment) cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_ENABLE_PROJECTS=lld ninja lld

Because clang-cl uses the default target triple based on its bitness, in order to build and link an x86 binary with clang-cl + lld, the compiler has to be built x86, and the linker has to be built x64 (or it runs out of memory). This is really lame and the compiler should detect the MSVC environment that is configured, but that isn't the way it works today.

So, continuing...

  1. Configure a third build directory that uses the compiler from step 1 and the linker from step 2, and that uses -DLLVM_ENABLE_LTO=Thin.

(From an x86 MSVC build environment) cmake -G Ninja -DLLVM_ENABLE_LTO=Thin -DCMAKE_C_COMPILER= -DCMAKE_CXX_COMPILER= -DCMAKE_LINKER= -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_ENABLE_PROJECTS="clang;lld" ........\llvm-mono\llvm

  1. Build your LTO'ed tblgen

ninja llvm-tblgen

  1. Run tblgen.

    bin\llvm-tblgen.exe -gen-attrs -I D:/src/llvm-mono/llvm/include/llvm/IR -I D:/src/llvm-mono/llvm/include D:/src/llvm-mono/llvm/include/llvm/IR/Attributes.td -o include/llvm/IR/Attributes.gen.tmp -d include/llvm/IR/Attributes.gen.d

  2. Print the value of ERRORLEVEL

    echo %ERRORLEVEL% -1073741819

This corresponds to 0xFFFFFFFFC0000005 which is access violation.

llvmbot commented 6 years ago

Zachary, does this work as desired now?

llvmbot commented 7 years ago

Was just about to post that I just tested x64 clang + lld and that doesn't exhibit the same problem. I will test out the suggested fix tomorrow, thanks!

pcc commented 7 years ago

If this only happens on 32-bit, it could be the same issue as llvm/llvm-project#33654 . Does it help if you patch in the fix linked from that bug?