@@ -370,7 +370,7 @@ static void prepareSILFunctionForOptimization(ModuleDecl *, SILFunction *F) {
370
370
371
371
namespace {
372
372
373
- struct OwnershipModelEliminator : SILModuleTransform {
373
+ struct OwnershipModelEliminator : SILFunctionTransform {
374
374
bool SkipTransparent;
375
375
bool SkipStdlibModule;
376
376
@@ -379,53 +379,50 @@ struct OwnershipModelEliminator : SILModuleTransform {
379
379
380
380
void run () override {
381
381
if (DumpBefore.size ()) {
382
- getModule ()->dump (DumpBefore.c_str ());
382
+ getFunction ()->dump (DumpBefore.c_str ());
383
383
}
384
384
385
- auto &Mod = *getModule ();
385
+ auto *F = getFunction ();
386
+ auto &Mod = getFunction ()->getModule ();
386
387
387
388
// If we are supposed to skip the stdlib module and we are in the stdlib
388
389
// module bail.
389
390
if (SkipStdlibModule && Mod.isStdlibModule ()) {
390
391
return ;
391
392
}
392
393
393
- for (auto &F : Mod) {
394
- // If F does not have ownership, skip it. We have no further work to do.
395
- if (!F.hasOwnership ())
396
- continue ;
397
-
398
- // If we were asked to not strip ownership from transparent functions in
399
- // /our/ module, continue.
400
- if (SkipTransparent && F.isTransparent ())
401
- continue ;
402
-
403
- // Verify here to make sure ownership is correct before we strip.
404
- {
405
- // Add a pretty stack trace entry to tell users who see a verification
406
- // failure triggered by this verification check that they need to re-run
407
- // with -sil-verify-all to actually find the pass that introduced the
408
- // verification error.
409
- //
410
- // DISCUSSION: This occurs due to the crash from the verification
411
- // failure happening in the pass itself. This causes us to dump the
412
- // SILFunction and emit a msg that this pass (OME) is the culprit. This
413
- // is generally correct for most passes, but not for OME since we are
414
- // verifying before we have even modified the function to ensure that
415
- // all ownership invariants have been respected before we lower
416
- // ownership from the function.
417
- llvm::PrettyStackTraceString silVerifyAllMsgOnFailure (
418
- " Found verification error when verifying before lowering "
419
- " ownership. Please re-run with -sil-verify-all to identify the "
420
- " actual pass that introduced the verification error." );
421
- F.verify ();
422
- }
394
+ if (!F->hasOwnership ())
395
+ return ;
423
396
424
- if (stripOwnership (F)) {
425
- auto InvalidKind =
426
- SILAnalysis::InvalidationKind::BranchesAndInstructions;
427
- invalidateAnalysis (&F, InvalidKind);
428
- }
397
+ // If we were asked to not strip ownership from transparent functions in
398
+ // /our/ module, return.
399
+ if (SkipTransparent && F->isTransparent ())
400
+ return ;
401
+
402
+ // Verify here to make sure ownership is correct before we strip.
403
+ {
404
+ // Add a pretty stack trace entry to tell users who see a verification
405
+ // failure triggered by this verification check that they need to re-run
406
+ // with -sil-verify-all to actually find the pass that introduced the
407
+ // verification error.
408
+ //
409
+ // DISCUSSION: This occurs due to the crash from the verification
410
+ // failure happening in the pass itself. This causes us to dump the
411
+ // SILFunction and emit a msg that this pass (OME) is the culprit. This
412
+ // is generally correct for most passes, but not for OME since we are
413
+ // verifying before we have even modified the function to ensure that
414
+ // all ownership invariants have been respected before we lower
415
+ // ownership from the function.
416
+ llvm::PrettyStackTraceString silVerifyAllMsgOnFailure (
417
+ " Found verification error when verifying before lowering "
418
+ " ownership. Please re-run with -sil-verify-all to identify the "
419
+ " actual pass that introduced the verification error." );
420
+ F->verify ();
421
+ }
422
+
423
+ if (stripOwnership (*F)) {
424
+ auto InvalidKind = SILAnalysis::InvalidationKind::BranchesAndInstructions;
425
+ invalidateAnalysis (InvalidKind);
429
426
}
430
427
431
428
// If we were asked to strip transparent, we are at the beginning of the
@@ -435,12 +432,19 @@ struct OwnershipModelEliminator : SILModuleTransform {
435
432
FunctionBodyDeserializationNotificationHandler;
436
433
std::unique_ptr<DeserializationNotificationHandler> ptr;
437
434
if (SkipTransparent) {
438
- ptr.reset (new NotificationHandlerTy (
439
- prepareNonTransparentSILFunctionForOptimization));
435
+ if (!Mod.hasRegisteredDeserializationNotificationHandlerForNonTransparentFuncOME ()) {
436
+ ptr.reset (new NotificationHandlerTy (
437
+ prepareNonTransparentSILFunctionForOptimization));
438
+ Mod.registerDeserializationNotificationHandler (std::move (ptr));
439
+ Mod.setRegisteredDeserializationNotificationHandlerForNonTransparentFuncOME ();
440
+ }
440
441
} else {
441
- ptr.reset (new NotificationHandlerTy (prepareSILFunctionForOptimization));
442
+ if (!Mod.hasRegisteredDeserializationNotificationHandlerForAllFuncOME ()) {
443
+ ptr.reset (new NotificationHandlerTy (prepareSILFunctionForOptimization));
444
+ Mod.registerDeserializationNotificationHandler (std::move (ptr));
445
+ Mod.setRegisteredDeserializationNotificationHandlerForAllFuncOME ();
446
+ }
442
447
}
443
- Mod.registerDeserializationNotificationHandler (std::move (ptr));
444
448
}
445
449
};
446
450
0 commit comments