Index: lib/LTO/ThinLTOCodeGenerator.cpp =================================================================== --- lib/LTO/ThinLTOCodeGenerator.cpp +++ lib/LTO/ThinLTOCodeGenerator.cpp @@ -25,9 +25,11 @@ #include "llvm/Bitcode/BitcodeWriterPass.h" #include "llvm/ExecutionEngine/ObjectMemoryBuffer.h" #include "llvm/IR/DiagnosticPrinter.h" +#include "llvm/IR/DebugInfo.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Mangler.h" +#include "llvm/IR/Verifier.h" #include "llvm/IRReader/IRReader.h" #include "llvm/LTO/LTO.h" #include "llvm/Linker/Linker.h" @@ -50,6 +52,7 @@ #include "llvm/Transforms/IPO/PassManagerBuilder.h" #include "llvm/Transforms/ObjCARC.h" #include "llvm/Transforms/Utils/FunctionImportUtils.h" +#include "llvm/Transforms/ObjCARC.h" #include @@ -62,6 +65,7 @@ extern cl::opt LTODiscardValueNames; extern cl::opt LTORemarksFilename; extern cl::opt LTOPassRemarksWithHotness; +extern cl::opt LTOStripInvalidDebugInfo; } namespace { @@ -195,7 +199,8 @@ PMB.OptLevel = OptLevel; PMB.LoopVectorize = true; PMB.SLPVectorize = true; - PMB.VerifyInput = true; + // Already did this in verifyMergedModule(). + PMB.VerifyInput = false; PMB.VerifyOutput = false; legacy::PassManager PM; @@ -224,6 +229,30 @@ return GUIDPreservedSymbols; } +namespace { +class ThinLTODiagnosticInfo : public DiagnosticInfo { + const Twine &Msg; +public: + ThinLTODiagnosticInfo(const Twine &DiagMsg, + DiagnosticSeverity Severity = DS_Error) + : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {} + void print(DiagnosticPrinter &DP) const override { DP << Msg; } +}; +} + +/// Verify the module and strip broken debug info. +static void verifyMergedModule(Module &TheModule) { + bool BrokenDebugInfo = false; + if (verifyModule(TheModule, &dbgs(), + LTOStripInvalidDebugInfo ? &BrokenDebugInfo : nullptr)) + report_fatal_error("Broken module found, compilation aborted!"); + if (BrokenDebugInfo) { + TheModule.getContext().diagnose(ThinLTODiagnosticInfo( + "Invalid debug info found, debug info will be stripped", DS_Warning)); + StripDebugInfo(TheModule); + } +} + std::unique_ptr codegenModule(Module &TheModule, TargetMachine &TM) { SmallVector OutputBuffer; @@ -408,6 +437,7 @@ // "Benchmark"-like optimization: single-source case bool SingleModule = (ModuleMap.size() == 1); + verifyMergedModule(TheModule); if (!SingleModule) { promoteModule(TheModule, Index); @@ -436,6 +466,8 @@ saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc"); } + // Verify again to catch all cross-imports. + verifyMergedModule(TheModule); optimizeModule(TheModule, TM, OptLevel, Freestanding); saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc"); @@ -762,6 +794,7 @@ * Perform ThinLTO CodeGen. */ std::unique_ptr ThinLTOCodeGenerator::codegen(Module &TheModule) { + verifyMergedModule(TheModule); initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple())); return codegenModule(TheModule, *TMBuilder.create()); } Index: test/LTO/X86/strip-debug-info.ll =================================================================== --- test/LTO/X86/strip-debug-info.ll +++ test/LTO/X86/strip-debug-info.ll @@ -6,10 +6,30 @@ ; RUN: -o %t.o %S/Inputs/strip-debug-info.bc 2>&1 | \ ; RUN: FileCheck %s -allow-empty -check-prefix=CHECK-WARN ; RUN: llvm-nm %t.o | FileCheck %s +; RUN: not llvm-lto -thinlto -thinlto-action=codegen \ +; RUN: -lto-strip-invalid-debug-info=false \ +; RUN: %S/Inputs/strip-debug-info.bc -disable-verify 2>&1 | \ +; RUN: FileCheck %s -allow-empty -check-prefix=CHECK-ERR +; RUN: llvm-lto -thinlto -thinlto-action=codegen \ +; RUN: -lto-strip-invalid-debug-info=true \ +; RUN: %S/Inputs/strip-debug-info.bc -disable-verify 2>&1 | \ +; RUN: FileCheck %s -allow-empty -check-prefix=CHECK-WARN +; RUN: not llvm-lto -thinlto -thinlto-action=run \ +; RUN: -lto-strip-invalid-debug-info=false \ +; RUN: %S/Inputs/strip-debug-info.bc -disable-verify 2>&1 | \ +; RUN: FileCheck %s -allow-empty -check-prefix=CHECK-ERR +; RUN: llvm-lto -thinlto -thinlto-action=run \ +; RUN: -lto-strip-invalid-debug-info=true \ +; RUN: %S/Inputs/strip-debug-info.bc -disable-verify 2>&1 | \ +; RUN: FileCheck %s -allow-empty -check-prefix=CHECK-WARN ; CHECK-ERR: Broken module found, compilation aborted ; CHECK-WARN: Invalid debug info found, debug info will be stripped +; CHECK-WARN-NOT: Broken module found ; CHECK: foo +target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-apple-macosx10.12" + define void @foo() { ret void }