@@ -534,9 +534,21 @@ static bool handledEndAccesses(BeginAccessInst *BI, SILLoop *Loop) {
534
534
return true ;
535
535
}
536
536
537
+ static bool isCoveredByScope (BeginAccessInst *BI, DominanceInfo *DT,
538
+ SILInstruction *applyInstr) {
539
+ if (!DT->dominates (BI, applyInstr))
540
+ return false ;
541
+ for (auto *EI : BI->getEndAccesses ()) {
542
+ if (!DT->dominates (applyInstr, EI))
543
+ return false ;
544
+ }
545
+ return true ;
546
+ }
547
+
537
548
static bool analyzeBeginAccess (BeginAccessInst *BI,
538
549
SmallVector<BeginAccessInst *, 8 > &BeginAccesses,
539
550
SmallVector<FullApplySite, 8 > &fullApplies,
551
+ WriteSet &MayWrites,
540
552
AccessedStorageAnalysis *ASA,
541
553
DominanceInfo *DT) {
542
554
if (BI->getEnforcement () != SILAccessEnforcement::Dynamic) {
@@ -571,12 +583,26 @@ static bool analyzeBeginAccess(BeginAccessInst *BI,
571
583
// If the apply is “sandwiched” between the begin and end access,
572
584
// there’s no reason we can’t hoist out of the loop.
573
585
auto *applyInstr = fullApply.getInstruction ();
574
- if (!DT-> dominates (BI, applyInstr))
586
+ if (!isCoveredByScope (BI, DT , applyInstr))
575
587
return false ;
576
- for (auto *EI : BI->getEndAccesses ()) {
577
- if (!DT->dominates (applyInstr, EI))
578
- return false ;
588
+ }
589
+
590
+ // Check may releases
591
+ // Only class and global access that may alias would conflict
592
+ const AccessedStorage::Kind kind = storage.getKind ();
593
+ if (kind != AccessedStorage::Class && kind != AccessedStorage::Global) {
594
+ return true ;
595
+ }
596
+ // TODO Introduce "Pure Swift" deinitializers
597
+ // We can then make use of alias information for instr's operands
598
+ // If they don't alias - we might get away with not recording a conflict
599
+ for (auto mayWrite : MayWrites) {
600
+ // we actually compute all MayWrites in analyzeCurrentLoop
601
+ if (!mayWrite->mayRelease ()) {
602
+ continue ;
579
603
}
604
+ if (!isCoveredByScope (BI, DT, mayWrite))
605
+ return false ;
580
606
}
581
607
582
608
return true ;
@@ -698,7 +724,8 @@ void LoopTreeOptimization::analyzeCurrentLoop(
698
724
LLVM_DEBUG (llvm::dbgs () << " Some end accesses can't be handled\n " );
699
725
continue ;
700
726
}
701
- if (analyzeBeginAccess (BI, BeginAccesses, fullApplies, ASA, DomTree)) {
727
+ if (analyzeBeginAccess (BI, BeginAccesses, fullApplies, MayWrites, ASA,
728
+ DomTree)) {
702
729
SpecialHoist.insert (BI);
703
730
}
704
731
}
0 commit comments