Skip to content

Commit a9df586

Browse files
author
Joe Shajrawi
authored
Merge pull request #19119 from shajrawi/mayrelease
[Exclusivity] Handle mayRelease instructions conservatively in AccessnforcementOpts and LICM
2 parents 638272d + 7297b81 commit a9df586

File tree

4 files changed

+249
-117
lines changed

4 files changed

+249
-117
lines changed

lib/SILOptimizer/LoopTransforms/LICM.cpp

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -534,9 +534,21 @@ static bool handledEndAccesses(BeginAccessInst *BI, SILLoop *Loop) {
534534
return true;
535535
}
536536

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+
537548
static bool analyzeBeginAccess(BeginAccessInst *BI,
538549
SmallVector<BeginAccessInst *, 8> &BeginAccesses,
539550
SmallVector<FullApplySite, 8> &fullApplies,
551+
WriteSet &MayWrites,
540552
AccessedStorageAnalysis *ASA,
541553
DominanceInfo *DT) {
542554
if (BI->getEnforcement() != SILAccessEnforcement::Dynamic) {
@@ -571,12 +583,26 @@ static bool analyzeBeginAccess(BeginAccessInst *BI,
571583
// If the apply is “sandwiched” between the begin and end access,
572584
// there’s no reason we can’t hoist out of the loop.
573585
auto *applyInstr = fullApply.getInstruction();
574-
if (!DT->dominates(BI, applyInstr))
586+
if (!isCoveredByScope(BI, DT, applyInstr))
575587
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;
579603
}
604+
if (!isCoveredByScope(BI, DT, mayWrite))
605+
return false;
580606
}
581607

582608
return true;
@@ -698,7 +724,8 @@ void LoopTreeOptimization::analyzeCurrentLoop(
698724
LLVM_DEBUG(llvm::dbgs() << "Some end accesses can't be handled\n");
699725
continue;
700726
}
701-
if (analyzeBeginAccess(BI, BeginAccesses, fullApplies, ASA, DomTree)) {
727+
if (analyzeBeginAccess(BI, BeginAccesses, fullApplies, MayWrites, ASA,
728+
DomTree)) {
702729
SpecialHoist.insert(BI);
703730
}
704731
}

0 commit comments

Comments
 (0)