@@ -374,7 +374,7 @@ void swift::executePassPipelinePlan(SILModule *SM,
374374
375375SILPassManager::SILPassManager (SILModule *M, bool isMandatory,
376376 irgen::IRGenModule *IRMod)
377- : Mod(M), IRMod(IRMod),
377+ : Mod(M), IRMod(IRMod), irgen( nullptr ),
378378 swiftPassInvocation(this ),
379379 isMandatory(isMandatory), deserializationNotificationHandler(nullptr ) {
380380#define SIL_ANALYSIS (NAME ) \
@@ -901,6 +901,27 @@ void SILPassManager::execute() {
901901 }
902902}
903903
904+ irgen::IRGenModule *SILPassManager::getIRGenModule () {
905+ // We need an IRGenModule to get the actual sizes from type lowering.
906+ // Creating an IRGenModule involves some effort, let's cache it for the
907+ // whole pass.
908+ if (IRMod == nullptr ) {
909+ SILModule *module = getModule ();
910+
911+ auto *irgenOpts = module ->getIRGenOptionsOrNull ();
912+ if (!irgenOpts)
913+ return nullptr ;
914+
915+ if (irgen == nullptr )
916+ irgen = new irgen::IRGenerator (*irgenOpts, *module );
917+ auto targetMachine = irgen->createTargetMachine ();
918+ assert (targetMachine && " failed to create target" );
919+ IRMod = new irgen::IRGenModule (*irgen, std::move (targetMachine));
920+ }
921+
922+ return IRMod;
923+ }
924+
904925// / D'tor.
905926SILPassManager::~SILPassManager () {
906927
@@ -940,6 +961,16 @@ SILPassManager::~SILPassManager() {
940961 " Deleting a locked analysis. Did we forget to unlock ?" );
941962 delete A;
942963 }
964+
965+ if (irgen) {
966+ // If irgen is set, we also own the IRGenModule
967+ if (IRMod) {
968+ delete IRMod;
969+ IRMod = nullptr ;
970+ }
971+ delete irgen;
972+ irgen = nullptr ;
973+ }
943974}
944975
945976void SILPassManager::notifyOfNewFunction (SILFunction *F, SILTransform *T) {
@@ -1382,22 +1413,7 @@ void SwiftPassInvocation::finishedInstructionPassRun() {
13821413}
13831414
13841415irgen::IRGenModule *SwiftPassInvocation::getIRGenModule () {
1385- // We need an IRGenModule to get the actual sizes from type lowering.
1386- // Creating an IRGenModule involves some effort, let's cache it for the
1387- // whole pass.
1388- if (irgenModule == nullptr && irgen == nullptr ) {
1389- SILModule *module = getPassManager ()->getModule ();
1390-
1391- auto *irgenOpts = module ->getIRGenOptionsOrNull ();
1392- if (!irgenOpts)
1393- return nullptr ;
1394-
1395- irgen = new irgen::IRGenerator (*irgenOpts, *module );
1396- auto targetMachine = irgen->createTargetMachine ();
1397- assert (targetMachine && " failed to create target" );
1398- irgenModule = new irgen::IRGenModule (*irgen, std::move (targetMachine));
1399- }
1400- return irgenModule;
1416+ return passManager->getIRGenModule ();
14011417}
14021418
14031419void SwiftPassInvocation::endPass () {
@@ -1430,16 +1446,7 @@ void SwiftPassInvocation::endTransformFunction() {
14301446 assert (numNodeSetsAllocated == 0 && " Not all NodeSets deallocated" );
14311447}
14321448
1433- SwiftPassInvocation::~SwiftPassInvocation () {
1434- if (irgenModule) {
1435- delete irgenModule;
1436- irgenModule = nullptr ;
1437- }
1438- if (irgen) {
1439- delete irgen;
1440- irgen = nullptr ;
1441- }
1442- }
1449+ SwiftPassInvocation::~SwiftPassInvocation () {}
14431450
14441451// ===----------------------------------------------------------------------===//
14451452// OptimizerBridging
0 commit comments