Index: llvm/lib/LTO/Caching.cpp =================================================================== --- llvm/lib/LTO/Caching.cpp +++ llvm/lib/LTO/Caching.cpp @@ -17,6 +17,7 @@ #include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" +#include "llvm/Support/Process.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; @@ -72,7 +73,20 @@ MBOrErr.getError().message() + "\n"); // This is atomic on POSIX systems. - if (auto EC = sys::fs::rename(TempFilename, EntryPath)) + // On Windows, it can fail with permission denied if the destination + // file already exists. Since the existing file should be semantically + // equivalent to the one we are trying to write, we count that as + // a success if we can open the destination file for reading. + auto EC = sys::fs::rename(TempFilename, EntryPath); + if (EC == errc::permission_denied) { + int FD; + EC = sys::fs::openFileForRead(EntryPath, FD); + if (!EC) { + sys::Process::SafelyCloseFileDescriptor(FD); + sys::fs::remove(TempFilename); + } + } + if (EC) report_fatal_error(Twine("Failed to rename temporary file ") + TempFilename + " to " + EntryPath + ": " + EC.message() + "\n");