Index: lld/COFF/LTO.cpp =================================================================== --- lld/COFF/LTO.cpp +++ lld/COFF/LTO.cpp @@ -144,14 +144,17 @@ std::vector Ret; for (unsigned I = 0; I != MaxTasks; ++I) { - if (Buff[I].empty()) + std::string FileName = Config->OutputFile; + if (I) + FileName += std::to_string(I); + FileName += ".lto.obj"; + if (Buff[I].empty()) { + if (Config->SaveTemps) + sys::fs::remove(FileName); continue; - if (Config->SaveTemps) { - if (I == 0) - saveBuffer(Buff[I], Config->OutputFile + ".lto.obj"); - else - saveBuffer(Buff[I], Config->OutputFile + Twine(I) + ".lto.obj"); } + if (Config->SaveTemps) + saveBuffer(Buff[I], FileName); Ret.emplace_back(Buff[I].data(), Buff[I].size()); } Index: lld/ELF/LTO.cpp =================================================================== --- lld/ELF/LTO.cpp +++ lld/ELF/LTO.cpp @@ -198,14 +198,17 @@ pruneCache(Config->ThinLTOCacheDir, Config->ThinLTOCachePolicy); for (unsigned I = 0; I != MaxTasks; ++I) { - if (Buff[I].empty()) + std::string FileName = Config->OutputFile; + if (I) + FileName += std::to_string(I); + FileName += ".lto.o"; + if (Buff[I].empty()) { + if (Config->SaveTemps) + sys::fs::remove(FileName); continue; - if (Config->SaveTemps) { - if (I == 0) - saveBuffer(Buff[I], Config->OutputFile + ".lto.o"); - else - saveBuffer(Buff[I], Config->OutputFile + Twine(I) + ".lto.o"); } + if (Config->SaveTemps) + saveBuffer(Buff[I], FileName); InputFile *Obj = createObjectFile(MemoryBufferRef(Buff[I], "lto.tmp")); Ret.push_back(Obj); } Index: llvm/test/ThinLTO/X86/delete-unused-temps.ll =================================================================== --- /dev/null +++ llvm/test/ThinLTO/X86/delete-unused-temps.ll @@ -0,0 +1,14 @@ +; Test that llvm-lto2 deletes unused temps. + +; RUN: opt -module-summary -o %t.bc %s + +; RUN: llvm-lto2 run %t.bc -o %t2 +; RUN: ls %t2* | count 2 + +; RUN: llvm-lto2 run %t.bc -o %t2 -thinlto-distributed-indexes +; RUN: ls %t2* | count 1 + +; CHECK: Format: ELF64-x86-64 + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" Index: llvm/test/tools/gold/X86/thinlto.ll =================================================================== --- llvm/test/tools/gold/X86/thinlto.ll +++ llvm/test/tools/gold/X86/thinlto.ll @@ -83,6 +83,9 @@ ; RUN: llvm-readobj -h %t5.o | FileCheck %s --check-prefix=FORMAT ; RUN: llvm-nm %t5.o | count 0 +; Test to ensure that gold deleted unneeded file which existed before. +; RUN: not ls %t5.o1 2>/dev/null + ; NM: T f ; NM2: T {{f|g}} ; FORMAT: Format: ELF64-x86-64 Index: llvm/tools/gold/gold-plugin.cpp =================================================================== --- llvm/tools/gold/gold-plugin.cpp +++ llvm/tools/gold/gold-plugin.cpp @@ -702,11 +702,16 @@ Cleanup.push_back(Filename); } -/// Return the desired output filename given a base input name, a flag +/// Returns the desired output filename given a base input name. +static std::string getOutputFileName(StringRef InFilename, int TaskID) { + return (TaskID > 0 ? InFilename + utostr(TaskID) : InFilename).str(); +} + +/// Return the desired output file given a base input name, a flag /// indicating whether a temp file should be generated, and an optional task id. /// The new filename generated is returned in \p NewFilename. -static int getOutputFileName(StringRef InFilename, bool TempOutFile, - SmallString<128> &NewFilename, int TaskID) { +static int getOutputFile(StringRef InFilename, bool TempOutFile, + SmallString<128> &NewFilename, int TaskID) { int FD = -1; if (TempOutFile) { std::error_code EC = @@ -715,9 +720,7 @@ message(LDPL_FATAL, "Could not create temporary file: %s", EC.message().c_str()); } else { - NewFilename = InFilename; - if (TaskID > 0) - NewFilename += utostr(TaskID); + NewFilename = getOutputFileName(InFilename, TaskID); std::error_code EC = sys::fs::openFileForWrite(NewFilename, FD, sys::fs::F_None); if (EC) @@ -913,8 +916,8 @@ auto AddStream = [&](size_t Task) -> std::unique_ptr { IsTemporary[Task] = !SaveTemps; - int FD = getOutputFileName(Filename, /*TempOutFile=*/!SaveTemps, - Filenames[Task], Task); + int FD = getOutputFile(Filename, /*TempOutFile=*/!SaveTemps, + Filenames[Task], Task); return llvm::make_unique( llvm::make_unique(FD, true)); }; @@ -946,6 +949,8 @@ for (unsigned I = 0; I != MaxTasks; ++I) if (!Filenames[I].empty()) recordFile(Filenames[I].str(), IsTemporary[I]); + else if (SaveTemps) + Cleanup.push_back(getOutputFileName(Filename, I)); if (!options::extra_library_path.empty() && set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK) Index: llvm/tools/llvm-lto2/llvm-lto2.cpp =================================================================== --- llvm/tools/llvm-lto2/llvm-lto2.cpp +++ llvm/tools/llvm-lto2/llvm-lto2.cpp @@ -286,10 +286,17 @@ if (HasErrors) return 1; + auto MakeStreamFileName = [&](size_t Task) -> std::string { + return OutputFilename + "." + utostr(Task); + }; + + size_t MaxTasks = Lto.getMaxTasks(); + for (unsigned I = 0; I != MaxTasks; ++I) + sys::fs::remove(MakeStreamFileName(I)); + auto AddStream = [&](size_t Task) -> std::unique_ptr { - std::string Path = OutputFilename + "." + utostr(Task); - + std::string Path = MakeStreamFileName(Task); std::error_code EC; auto S = llvm::make_unique(Path, EC, sys::fs::F_None); check(EC, Path);