28
28
#include " swift/IRGen/Linking.h"
29
29
#include " swift/Runtime/RuntimeFnWrappersGen.h"
30
30
#include " swift/Runtime/Config.h"
31
+ #include " swift/Subsystems.h"
31
32
#include " clang/AST/ASTContext.h"
32
33
#include " clang/Basic/CharInfo.h"
33
34
#include " clang/Basic/TargetInfo.h"
@@ -1614,12 +1615,7 @@ void IRGenModule::emitAutolinkInfo() {
1614
1615
}
1615
1616
}
1616
1617
1617
- void IRGenModule::cleanupClangCodeGenMetadata () {
1618
- // Remove llvm.ident that ClangCodeGen might have left in the module.
1619
- auto *LLVMIdent = Module.getNamedMetadata (" llvm.ident" );
1620
- if (LLVMIdent)
1621
- Module.eraseNamedMetadata (LLVMIdent);
1622
-
1618
+ void emitSwiftVersionNumberIntoModule (llvm::Module *Module) {
1623
1619
// LLVM's object-file emission collects a fixed set of keys for the
1624
1620
// image info.
1625
1621
// Using "Objective-C Garbage Collection" as the key here is a hack,
@@ -1629,21 +1625,29 @@ void IRGenModule::cleanupClangCodeGenMetadata() {
1629
1625
const char *ObjectiveCGarbageCollection = " Objective-C Garbage Collection" ;
1630
1626
uint8_t Major, Minor;
1631
1627
std::tie (Major, Minor) = version::getSwiftNumericVersion ();
1632
- uint32_t Value = (Major << 24 ) | (Minor << 16 ) | (swiftVersion << 8 );
1633
-
1634
- if (Module.getModuleFlag (ObjectiveCGarbageCollection)) {
1628
+ uint32_t Value =
1629
+ (Major << 24 ) | (Minor << 16 ) | (IRGenModule::swiftVersion << 8 );
1630
+ auto &llvmContext = Module->getContext ();
1631
+ if (Module->getModuleFlag (ObjectiveCGarbageCollection)) {
1635
1632
bool FoundOldEntry = replaceModuleFlagsEntry (
1636
- Module. getContext (), Module, ObjectiveCGarbageCollection,
1633
+ llvmContext, * Module, ObjectiveCGarbageCollection,
1637
1634
llvm::Module::Override,
1638
- llvm::ConstantAsMetadata::get (
1639
- llvm::ConstantInt::get (Int32Ty , Value)));
1635
+ llvm::ConstantAsMetadata::get (llvm::ConstantInt::get (
1636
+ llvm::Type::getInt32Ty (llvmContext) , Value)));
1640
1637
1641
1638
(void )FoundOldEntry;
1642
1639
assert (FoundOldEntry && " Could not replace old module flag entry?" );
1643
1640
} else
1644
- Module.addModuleFlag (llvm::Module::Override,
1645
- ObjectiveCGarbageCollection,
1646
- Value);
1641
+ Module->addModuleFlag (llvm::Module::Override, ObjectiveCGarbageCollection,
1642
+ Value);
1643
+ }
1644
+
1645
+ void IRGenModule::cleanupClangCodeGenMetadata () {
1646
+ // Remove llvm.ident that ClangCodeGen might have left in the module.
1647
+ auto *LLVMIdent = Module.getNamedMetadata (" llvm.ident" );
1648
+ if (LLVMIdent)
1649
+ Module.eraseNamedMetadata (LLVMIdent);
1650
+ emitSwiftVersionNumberIntoModule (&Module);
1647
1651
}
1648
1652
1649
1653
bool IRGenModule::finalize () {
@@ -1862,3 +1866,40 @@ bool IRGenModule::isConcurrencyAvailable() {
1862
1866
AvailabilityContext::forDeploymentTarget (ctx);
1863
1867
return deploymentAvailability.isContainedIn (ctx.getConcurrencyAvailability ());
1864
1868
}
1869
+
1870
+ // / Pretend the other files that drivers/build systems expect exist by
1871
+ // / creating empty files. Used by UseSingleModuleLLVMEmission when
1872
+ // / num-threads > 0.
1873
+ bool swift::writeEmptyOutputFilesFor (
1874
+ const ASTContext &Context,
1875
+ std::vector<std::string>& ParallelOutputFilenames,
1876
+ const IRGenOptions &IRGenOpts) {
1877
+
1878
+ for (auto fileName : ParallelOutputFilenames) {
1879
+ // The first output file, was use for genuine output.
1880
+ if (fileName == ParallelOutputFilenames[0 ])
1881
+ continue ;
1882
+
1883
+ std::unique_ptr<llvm::LLVMContext> llvmContext (new llvm::LLVMContext ());
1884
+ std::unique_ptr<clang::CodeGenerator> clangCodeGen (
1885
+ createClangCodeGenerator (const_cast <ASTContext&>(Context),
1886
+ *llvmContext, IRGenOpts, fileName, " " ));
1887
+ auto *llvmModule = clangCodeGen->GetModule ();
1888
+
1889
+ auto *clangImporter = static_cast <ClangImporter *>(
1890
+ Context.getClangModuleLoader ());
1891
+ llvmModule->setTargetTriple (
1892
+ clangImporter->getTargetInfo ().getTargetOpts ().Triple );
1893
+
1894
+ // Add LLVM module flags.
1895
+ auto &clangASTContext = clangImporter->getClangASTContext ();
1896
+ clangCodeGen->HandleTranslationUnit (
1897
+ const_cast <clang::ASTContext &>(clangASTContext));
1898
+
1899
+ emitSwiftVersionNumberIntoModule (llvmModule);
1900
+
1901
+ swift::performLLVM (IRGenOpts, const_cast <ASTContext&>(Context),
1902
+ llvmModule, fileName);
1903
+ }
1904
+ return false ;
1905
+ }
0 commit comments