diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp index 93086494b8f6d..887e9321b7f73 100644 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -1418,10 +1418,15 @@ std::unique_ptr CompilerInstance::cloneForModuleCompileImpl( auto WrapGenModuleAction = getGenModuleActionWrapper(); Instance.setGenModuleActionWrapper(WrapGenModuleAction); - // Share an output manager. assert(hasOutputBackend() && "Expected an output manager to already be set up"); - Instance.setOutputBackend(&getOutputBackend()); + if (ThreadSafeConfig) { + // Create a clone of the existing output (pointing to the same destination). + Instance.setOutputBackend(getOutputBackend().clone()); + } else { + // Share the existing output manager. + Instance.setOutputBackend(&getOutputBackend()); + } if (ThreadSafeConfig) { Instance.setModuleDepCollector(ThreadSafeConfig->getModuleDepCollector());