Index: clang/lib/CodeGen/CodeGenAction.cpp =================================================================== --- clang/lib/CodeGen/CodeGenAction.cpp +++ clang/lib/CodeGen/CodeGenAction.cpp @@ -953,6 +953,21 @@ return M; }; + sys::fs::file_status Status; + if (auto E = sys::fs::status(CI.getCodeGenOpts().ThinLTOIndexFile, Status, + true)) { + return DiagErrors(make_error( + "Error loading index file '" + CI.getCodeGenOpts().ThinLTOIndexFile + + "'", + E)); + } + // Empty ThinLTOIndexFile signals that we don't need this module during + // linking. So we should not run ThinLTO backend even if it contains the + // ThinLTO module. Backend even may fail because of lack of necessary + // information which should be provided by ThinLTOIndex. + if (!Status.getSize()) + return CreateEmptyModule(); + Expected> BMsOrErr = getBitcodeModuleList(MBRef); if (!BMsOrErr) return DiagErrors(BMsOrErr.takeError()); Index: clang/test/CodeGen/thinlto_backend.ll =================================================================== --- clang/test/CodeGen/thinlto_backend.ll +++ clang/test/CodeGen/thinlto_backend.ll @@ -9,16 +9,14 @@ ; CHECK-WARNING: error: invalid argument '-fthinlto-index={{.*}}' only allowed with '-x ir' ; Ensure we get expected error for missing index file -; RUN: %clang -O2 -o %t4.o -x ir %t1.o -c -fthinlto-index=bad.thinlto.bc 2>&1 | FileCheck %s -check-prefix=CHECK-ERROR1 +; RUN: not %clang -O2 -o %t4.o -x ir %t1.o -c -fthinlto-index=bad.thinlto.bc 2>&1 | FileCheck %s -check-prefix=CHECK-ERROR1 ; CHECK-ERROR1: Error loading index file 'bad.thinlto.bc' ; Ensure we ignore empty index file, and run non-ThinLTO compilation which ; would not import f2 ; RUN: touch %t4.thinlto.bc ; RUN: %clang -target x86_64-unknown-linux-gnu -O2 -o %t4.o -x ir %t1.o -c -fthinlto-index=%t4.thinlto.bc -; RUN: llvm-nm %t4.o | FileCheck --check-prefix=CHECK-OBJ-IGNORE-EMPTY %s -; CHECK-OBJ-IGNORE-EMPTY: T f1 -; CHECK-OBJ-IGNORE-EMPTY: U f2 +; RUN: llvm-nm %t4.o | wc -l | FileCheck %s --check-prefix=CHECK-OBJ-EMPTY ; Ensure we don't fail with index and non-ThinLTO object file, and output must ; be empty file.