2828#include " swift/IRGen/Linking.h"
2929#include " swift/Runtime/RuntimeFnWrappersGen.h"
3030#include " swift/Runtime/Config.h"
31+ #include " swift/Subsystems.h"
3132#include " clang/AST/ASTContext.h"
3233#include " clang/Basic/CharInfo.h"
3334#include " clang/Basic/TargetInfo.h"
@@ -1636,12 +1637,7 @@ void IRGenModule::emitAutolinkInfo() {
16361637 }
16371638}
16381639
1639- void IRGenModule::cleanupClangCodeGenMetadata () {
1640- // Remove llvm.ident that ClangCodeGen might have left in the module.
1641- auto *LLVMIdent = Module.getNamedMetadata (" llvm.ident" );
1642- if (LLVMIdent)
1643- Module.eraseNamedMetadata (LLVMIdent);
1644-
1640+ void emitSwiftVersionNumberIntoModule (llvm::Module *Module) {
16451641 // LLVM's object-file emission collects a fixed set of keys for the
16461642 // image info.
16471643 // Using "Objective-C Garbage Collection" as the key here is a hack,
@@ -1651,21 +1647,29 @@ void IRGenModule::cleanupClangCodeGenMetadata() {
16511647 const char *ObjectiveCGarbageCollection = " Objective-C Garbage Collection" ;
16521648 uint8_t Major, Minor;
16531649 std::tie (Major, Minor) = version::getSwiftNumericVersion ();
1654- uint32_t Value = (Major << 24 ) | (Minor << 16 ) | (swiftVersion << 8 );
1655-
1656- if (Module.getModuleFlag (ObjectiveCGarbageCollection)) {
1650+ uint32_t Value =
1651+ (Major << 24 ) | (Minor << 16 ) | (IRGenModule::swiftVersion << 8 );
1652+ auto &llvmContext = Module->getContext ();
1653+ if (Module->getModuleFlag (ObjectiveCGarbageCollection)) {
16571654 bool FoundOldEntry = replaceModuleFlagsEntry (
1658- Module. getContext (), Module, ObjectiveCGarbageCollection,
1655+ llvmContext, * Module, ObjectiveCGarbageCollection,
16591656 llvm::Module::Override,
1660- llvm::ConstantAsMetadata::get (
1661- llvm::ConstantInt::get (Int32Ty , Value)));
1657+ llvm::ConstantAsMetadata::get (llvm::ConstantInt::get (
1658+ llvm::Type::getInt32Ty (llvmContext) , Value)));
16621659
16631660 (void )FoundOldEntry;
16641661 assert (FoundOldEntry && " Could not replace old module flag entry?" );
16651662 } else
1666- Module.addModuleFlag (llvm::Module::Override,
1667- ObjectiveCGarbageCollection,
1668- Value);
1663+ Module->addModuleFlag (llvm::Module::Override, ObjectiveCGarbageCollection,
1664+ Value);
1665+ }
1666+
1667+ void IRGenModule::cleanupClangCodeGenMetadata () {
1668+ // Remove llvm.ident that ClangCodeGen might have left in the module.
1669+ auto *LLVMIdent = Module.getNamedMetadata (" llvm.ident" );
1670+ if (LLVMIdent)
1671+ Module.eraseNamedMetadata (LLVMIdent);
1672+ emitSwiftVersionNumberIntoModule (&Module);
16691673}
16701674
16711675bool IRGenModule::finalize () {
@@ -1884,3 +1888,40 @@ bool IRGenModule::isConcurrencyAvailable() {
18841888 AvailabilityContext::forDeploymentTarget (ctx);
18851889 return deploymentAvailability.isContainedIn (ctx.getConcurrencyAvailability ());
18861890}
1891+
1892+ // / Pretend the other files that drivers/build systems expect exist by
1893+ // / creating empty files. Used by UseSingleModuleLLVMEmission when
1894+ // / num-threads > 0.
1895+ bool swift::writeEmptyOutputFilesFor (
1896+ const ASTContext &Context,
1897+ std::vector<std::string>& ParallelOutputFilenames,
1898+ const IRGenOptions &IRGenOpts) {
1899+
1900+ for (auto fileName : ParallelOutputFilenames) {
1901+ // The first output file, was use for genuine output.
1902+ if (fileName == ParallelOutputFilenames[0 ])
1903+ continue ;
1904+
1905+ std::unique_ptr<llvm::LLVMContext> llvmContext (new llvm::LLVMContext ());
1906+ std::unique_ptr<clang::CodeGenerator> clangCodeGen (
1907+ createClangCodeGenerator (const_cast <ASTContext&>(Context),
1908+ *llvmContext, IRGenOpts, fileName, " " ));
1909+ auto *llvmModule = clangCodeGen->GetModule ();
1910+
1911+ auto *clangImporter = static_cast <ClangImporter *>(
1912+ Context.getClangModuleLoader ());
1913+ llvmModule->setTargetTriple (
1914+ clangImporter->getTargetInfo ().getTargetOpts ().Triple );
1915+
1916+ // Add LLVM module flags.
1917+ auto &clangASTContext = clangImporter->getClangASTContext ();
1918+ clangCodeGen->HandleTranslationUnit (
1919+ const_cast <clang::ASTContext &>(clangASTContext));
1920+
1921+ emitSwiftVersionNumberIntoModule (llvmModule);
1922+
1923+ swift::performLLVM (IRGenOpts, const_cast <ASTContext&>(Context),
1924+ llvmModule, fileName);
1925+ }
1926+ return false ;
1927+ }
0 commit comments