@@ -129,7 +129,8 @@ static cl::opt<bool> AlignModuleToPageSize(
129129
130130std::tuple<llvm::TargetOptions, std::string, std::vector<std::string>,
131131 std::string>
132- swift::getIRTargetOptions (const IRGenOptions &Opts, ASTContext &Ctx) {
132+ swift::getIRTargetOptions (const IRGenOptions &Opts, ASTContext &Ctx,
133+ std::shared_ptr<llvm::cas::ObjectStore> CAS) {
133134 // Things that maybe we should collect from the command line:
134135 // - relocation model
135136 // - code model
@@ -150,6 +151,9 @@ swift::getIRTargetOptions(const IRGenOptions &Opts, ASTContext &Ctx) {
150151 // Set option to select the CASBackendMode.
151152 TargetOpts.MCOptions .CASObjMode = Opts.CASObjMode ;
152153
154+ // Set CAS and CASID callbacks.
155+ TargetOpts.MCOptions .CAS = std::move (CAS);
156+
153157 auto *Clang = static_cast <ClangImporter *>(Ctx.getClangModuleLoader ());
154158
155159 // Set UseInitArray appropriately.
@@ -1104,7 +1108,8 @@ static void setPointerAuthOptions(PointerAuthOptions &opts,
11041108}
11051109
11061110std::unique_ptr<llvm::TargetMachine>
1107- swift::createTargetMachine (const IRGenOptions &Opts, ASTContext &Ctx) {
1111+ swift::createTargetMachine (const IRGenOptions &Opts, ASTContext &Ctx,
1112+ std::shared_ptr<llvm::cas::ObjectStore> CAS) {
11081113 CodeGenOptLevel OptLevel = Opts.shouldOptimize ()
11091114 ? CodeGenOptLevel::Default // -Os
11101115 : CodeGenOptLevel::None;
@@ -1114,8 +1119,8 @@ swift::createTargetMachine(const IRGenOptions &Opts, ASTContext &Ctx) {
11141119 std::string CPU;
11151120 std::string EffectiveClangTriple;
11161121 std::vector<std::string> targetFeaturesArray;
1117- std::tie (TargetOpts, CPU, targetFeaturesArray, EffectiveClangTriple)
1118- = getIRTargetOptions (Opts, Ctx);
1122+ std::tie (TargetOpts, CPU, targetFeaturesArray, EffectiveClangTriple) =
1123+ getIRTargetOptions (Opts, Ctx, std::move (CAS) );
11191124 const llvm::Triple &EffectiveTriple = llvm::Triple (EffectiveClangTriple);
11201125 std::string targetFeatures;
11211126 if (!targetFeaturesArray.empty ()) {
@@ -1168,12 +1173,12 @@ swift::createTargetMachine(const IRGenOptions &Opts, ASTContext &Ctx) {
11681173 return std::unique_ptr<llvm::TargetMachine>(TargetMachine);
11691174}
11701175
1171- IRGenerator::IRGenerator (const IRGenOptions &options, SILModule &module )
1172- : Opts(options), SIL( module ), QueueIndex( 0 ) {
1173- }
1176+ IRGenerator::IRGenerator (const IRGenOptions &options, SILModule &module ,
1177+ std::shared_ptr<llvm::cas::ObjectStore> CAS)
1178+ : Opts(options), SIL( module ), CAS(std::move(CAS)), QueueIndex( 0 ) { }
11741179
11751180std::unique_ptr<llvm::TargetMachine> IRGenerator::createTargetMachine () {
1176- return ::createTargetMachine (Opts, SIL.getASTContext ());
1181+ return ::createTargetMachine (Opts, SIL.getASTContext (), CAS );
11771182}
11781183
11791184// With -embed-bitcode, save a copy of the llvm IR as data in the
@@ -1479,7 +1484,7 @@ GeneratedModule IRGenRequest::evaluate(Evaluator &evaluator,
14791484 auto *primaryFile =
14801485 dyn_cast_or_null<SourceFile>(desc.Ctx .dyn_cast <FileUnit *>());
14811486
1482- IRGenerator irgen (Opts, *SILMod);
1487+ IRGenerator irgen (Opts, *SILMod, desc. CAS );
14831488
14841489 auto targetMachine = irgen.createTargetMachine ();
14851490 if (!targetMachine) return GeneratedModule::null ();
@@ -1687,7 +1692,7 @@ static void performParallelIRGeneration(IRGenDescriptor desc) {
16871692 auto SILMod = std::unique_ptr<SILModule>(desc.SILMod );
16881693 auto *M = desc.getParentModule ();
16891694
1690- IRGenerator irgen (Opts, *SILMod);
1695+ IRGenerator irgen (Opts, *SILMod, desc. CAS );
16911696
16921697 // Enter a cleanup to delete all the IGMs and their associated LLVMContexts
16931698 // that have been associated with the IRGenerator.
@@ -1726,6 +1731,16 @@ static void performParallelIRGeneration(IRGenDescriptor desc) {
17261731
17271732 // Create the IR emitter.
17281733 auto outputName = *OutputIter++;
1734+ if (desc.casBackend ) {
1735+ targetMachine->Options .MCOptions .ResultCallBack =
1736+ [=](const llvm::cas::CASID &id) -> llvm::Error {
1737+ if (auto Err = desc.casBackend ->storeMCCASObjectID (outputName, id))
1738+ return Err;
1739+
1740+ return llvm::Error::success ();
1741+ };
1742+ }
1743+
17291744 IRGenModule *IGM = new IRGenModule (
17301745 irgen, std::move (targetMachine), nextSF, desc.ModuleName , outputName,
17311746 nextSF->getFilename (), nextSF->getPrivateDiscriminator ().str ());
@@ -1929,16 +1944,19 @@ GeneratedModule swift::performIRGeneration(
19291944 swift::ModuleDecl *M, const IRGenOptions &Opts,
19301945 const TBDGenOptions &TBDOpts, std::unique_ptr<SILModule> SILMod,
19311946 StringRef ModuleName, const PrimarySpecificPaths &PSPs,
1947+ std::shared_ptr<llvm::cas::ObjectStore> CAS,
19321948 ArrayRef<std::string> parallelOutputFilenames,
19331949 ArrayRef<std::string> parallelIROutputFilenames,
1934- llvm::GlobalVariable **outModuleHash) {
1950+ llvm::GlobalVariable **outModuleHash,
1951+ cas::SwiftCASOutputBackend *casBackend) {
19351952 // Get a pointer to the SILModule to avoid a potential use-after-move.
19361953 const auto *SILModPtr = SILMod.get ();
19371954 const auto &SILOpts = SILModPtr->getOptions ();
19381955 auto desc = IRGenDescriptor::forWholeModule (
19391956 M, Opts, TBDOpts, SILOpts, SILModPtr->Types , std::move (SILMod),
1940- ModuleName, PSPs, /* symsToEmit*/ std::nullopt , parallelOutputFilenames,
1941- parallelIROutputFilenames, outModuleHash);
1957+ ModuleName, PSPs, std::move (CAS), /* symsToEmit*/ std::nullopt ,
1958+ parallelOutputFilenames, parallelIROutputFilenames, outModuleHash,
1959+ casBackend);
19421960
19431961 if (Opts.shouldPerformIRGenerationInParallel () &&
19441962 !parallelOutputFilenames.empty () &&
@@ -1951,20 +1969,20 @@ GeneratedModule swift::performIRGeneration(
19511969 return evaluateOrFatal (M->getASTContext ().evaluator , IRGenRequest{desc});
19521970}
19531971
1954- GeneratedModule swift::
1955- performIRGeneration ( FileUnit *file, const IRGenOptions &Opts,
1956- const TBDGenOptions &TBDOpts ,
1957- std::unique_ptr<SILModule> SILMod ,
1958- StringRef ModuleName, const PrimarySpecificPaths &PSPs ,
1959- StringRef PrivateDiscriminator ,
1960- llvm::GlobalVariable **outModuleHash ) {
1972+ GeneratedModule swift::performIRGeneration (
1973+ FileUnit *file, const IRGenOptions &Opts, const TBDGenOptions &TBDOpts ,
1974+ std::unique_ptr<SILModule> SILMod, StringRef ModuleName ,
1975+ const PrimarySpecificPaths &PSPs ,
1976+ std::shared_ptr<llvm::cas::ObjectStore> CAS, StringRef PrivateDiscriminator ,
1977+ llvm::GlobalVariable **outModuleHash ,
1978+ cas::SwiftCASOutputBackend *casBackend ) {
19611979 // Get a pointer to the SILModule to avoid a potential use-after-move.
19621980 const auto *SILModPtr = SILMod.get ();
19631981 const auto &SILOpts = SILModPtr->getOptions ();
19641982 auto desc = IRGenDescriptor::forFile (
19651983 file, Opts, TBDOpts, SILOpts, SILModPtr->Types , std::move (SILMod),
1966- ModuleName, PSPs, PrivateDiscriminator,
1967- /* symsToEmit*/ std::nullopt , outModuleHash);
1984+ ModuleName, PSPs, std::move (CAS), PrivateDiscriminator,
1985+ /* symsToEmit*/ std::nullopt , outModuleHash, casBackend );
19681986 return evaluateOrFatal (file->getASTContext ().evaluator , IRGenRequest{desc});
19691987}
19701988
@@ -2038,7 +2056,7 @@ void swift::createSwiftModuleObjectFile(SILModule &SILMod, StringRef Buffer,
20382056bool swift::performLLVM (const IRGenOptions &Opts, ASTContext &Ctx,
20392057 llvm::Module *Module, StringRef OutputFilename) {
20402058 // Build TargetMachine.
2041- auto TargetMachine = createTargetMachine (Opts, Ctx);
2059+ auto TargetMachine = createTargetMachine (Opts, Ctx, /* CAS= */ nullptr );
20422060 if (!TargetMachine)
20432061 return true ;
20442062
0 commit comments