When using -fsplit-lto-unit (explicitly specified or due to using
-fsanitize=cfi/-fwhole-program-vtables), the emitted LLVM IR contains a module
flag metadata "EnableSplitLTOUnit". If a module contains both type metadata
and "EnableSplitLTOUnit", ThinLTOBitcodeWriter.cpp will write two modules
into the bitcode file. Compiling the bitcode (not ThinLTO backend compilation)
will lead to an error due to parseIR requiring a single module.
% clang -flto=thin a.cc -c -o a.bc % clang -c a.bc % clang -fsplit-lto-unit -flto=thin a.cc -c -o a.bc % clang -c a.bc error: Expected a single module 1 error generated.
There are multiple ways to have just one module in a bitcode file
output: -Xclang -fno-lto-unit, not using features like -fsanitize=cfi,
using -fsanitize=cfi with -fno-split-lto-unit. I think whether a
bitcode input file contains 2 modules (internal implementation strategy)
should not be a criterion to require an additional driver option when
the user seek for a non-LTO compile action.
Let's place the extra module (if present) into CodeGenOptions::LinkBitcodeFiles
(originally for -cc1 -mlink-bitcode-file). Linker::linkModules will link the two
modules together. This patch makes the following commands work:
clang -S -emit-llvm a.bc clang -S a.bc clang -c a.bc
Perhaps this variable name could be more descriptive. Maybe "FirstM" or something like that?