Skip to content

Commit e68e087

Browse files
author
Joe Shajrawi
authored
Merge pull request #18560 from shajrawi/merge_accesses
[AccessEnforcementOpts] Add mergeAccesses analysis + optimization
2 parents 254be25 + 12adde2 commit e68e087

File tree

6 files changed

+1530
-282
lines changed

6 files changed

+1530
-282
lines changed

lib/SILOptimizer/LoopTransforms/LICM.cpp

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "swift/SIL/SILArgument.h"
1919
#include "swift/SIL/SILBuilder.h"
2020
#include "swift/SIL/SILInstruction.h"
21+
#include "swift/SILOptimizer/Analysis/AccessedStorageAnalysis.h"
2122
#include "swift/SILOptimizer/Analysis/AliasAnalysis.h"
2223
#include "swift/SILOptimizer/Analysis/Analysis.h"
2324
#include "swift/SILOptimizer/Analysis/ArraySemantic.h"
@@ -361,6 +362,7 @@ class LoopTreeOptimization {
361362
AliasAnalysis *AA;
362363
SideEffectAnalysis *SEA;
363364
DominanceInfo *DomTree;
365+
AccessedStorageAnalysis *ASA;
364366
bool Changed;
365367

366368
/// True if LICM is done on high-level SIL, i.e. semantic calls are not
@@ -380,8 +382,9 @@ class LoopTreeOptimization {
380382
public:
381383
LoopTreeOptimization(SILLoop *TopLevelLoop, SILLoopInfo *LI,
382384
AliasAnalysis *AA, SideEffectAnalysis *SEA,
383-
DominanceInfo *DT, bool RunsOnHighLevelSil)
384-
: LoopInfo(LI), AA(AA), SEA(SEA), DomTree(DT), Changed(false),
385+
DominanceInfo *DT, AccessedStorageAnalysis *ASA,
386+
bool RunsOnHighLevelSil)
387+
: LoopInfo(LI), AA(AA), SEA(SEA), DomTree(DT), ASA(ASA), Changed(false),
385388
RunsOnHighLevelSIL(RunsOnHighLevelSil) {
386389
// Collect loops for a recursive bottom-up traversal in the loop tree.
387390
BotUpWorkList.push_back(TopLevelLoop);
@@ -528,9 +531,10 @@ static bool handledEndAccesses(BeginAccessInst *BI, SILLoop *Loop) {
528531
return true;
529532
}
530533

531-
static bool
532-
analyzeBeginAccess(BeginAccessInst *BI,
533-
SmallVector<BeginAccessInst *, 8> &BeginAccesses) {
534+
static bool analyzeBeginAccess(BeginAccessInst *BI,
535+
SmallVector<BeginAccessInst *, 8> &BeginAccesses,
536+
SmallVector<FullApplySite, 8> &fullApplies,
537+
AccessedStorageAnalysis *ASA) {
534538
if (BI->getEnforcement() != SILAccessEnforcement::Dynamic) {
535539
return false;
536540
}
@@ -550,8 +554,18 @@ analyzeBeginAccess(BeginAccessInst *BI,
550554
findAccessedStorageNonNested(OtherBI));
551555
};
552556

553-
return (
554-
std::all_of(BeginAccesses.begin(), BeginAccesses.end(), safeBeginPred));
557+
if (!std::all_of(BeginAccesses.begin(), BeginAccesses.end(), safeBeginPred))
558+
return false;
559+
560+
for (auto fullApply : fullApplies) {
561+
FunctionAccessedStorage callSiteAccesses;
562+
ASA->getCallSiteEffects(callSiteAccesses, fullApply);
563+
SILAccessKind accessKind = BI->getAccessKind();
564+
if (callSiteAccesses.mayConflictWith(accessKind, storage))
565+
return false;
566+
}
567+
568+
return true;
555569
}
556570

557571
// Analyzes current loop for hosting/sinking potential:
@@ -575,6 +589,8 @@ void LoopTreeOptimization::analyzeCurrentLoop(
575589
SmallVector<FixLifetimeInst *, 8> FixLifetimes;
576590
// Contains begin_access, we might be able to hoist them.
577591
SmallVector<BeginAccessInst *, 8> BeginAccesses;
592+
// Contains all applies - used for begin_access
593+
SmallVector<FullApplySite, 8> fullApplies;
578594

579595
for (auto *BB : Loop->getBlocks()) {
580596
for (auto &Inst : *BB) {
@@ -618,6 +634,9 @@ void LoopTreeOptimization::analyzeCurrentLoop(
618634
LLVM_FALLTHROUGH;
619635
}
620636
default: {
637+
if (auto fullApply = FullApplySite::isa(&Inst)) {
638+
fullApplies.push_back(fullApply);
639+
}
621640
checkSideEffects(Inst, MayWrites);
622641
if (canHoistUpDefault(&Inst, Loop, DomTree, RunsOnHighLevelSIL)) {
623642
HoistUp.insert(&Inst);
@@ -660,7 +679,7 @@ void LoopTreeOptimization::analyzeCurrentLoop(
660679
LLVM_DEBUG(llvm::dbgs() << "Some end accesses can't be handled\n");
661680
continue;
662681
}
663-
if (analyzeBeginAccess(BI, BeginAccesses)) {
682+
if (analyzeBeginAccess(BI, BeginAccesses, fullApplies, ASA)) {
664683
SpecialHoist.insert(BI);
665684
}
666685
}
@@ -711,14 +730,15 @@ class LICM : public SILFunctionTransform {
711730
DominanceAnalysis *DA = PM->getAnalysis<DominanceAnalysis>();
712731
AliasAnalysis *AA = PM->getAnalysis<AliasAnalysis>();
713732
SideEffectAnalysis *SEA = PM->getAnalysis<SideEffectAnalysis>();
733+
AccessedStorageAnalysis *ASA = getAnalysis<AccessedStorageAnalysis>();
714734
DominanceInfo *DomTree = nullptr;
715735

716736
LLVM_DEBUG(llvm::dbgs() << "Processing loops in " << F->getName() << "\n");
717737
bool Changed = false;
718738

719739
for (auto *TopLevelLoop : *LoopInfo) {
720740
if (!DomTree) DomTree = DA->get(F);
721-
LoopTreeOptimization Opt(TopLevelLoop, LoopInfo, AA, SEA, DomTree,
741+
LoopTreeOptimization Opt(TopLevelLoop, LoopInfo, AA, SEA, DomTree, ASA,
722742
RunsOnHighLevelSil);
723743
Changed |= Opt.optimize();
724744
}

lib/SILOptimizer/PassManager/PassPipeline.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ void addHighLevelLoopOptPasses(SILPassPipelinePlan &P) {
199199
P.addHighLevelLICM();
200200
// Simplify CFG after LICM that creates new exit blocks
201201
P.addSimplifyCFG();
202+
// LICM might have added new merging potential by hoisting
203+
// we don't want to restart the pipeline - ignore the
204+
// potential of merging out of two loops
205+
P.addAccessEnforcementOpts();
202206
// Start of loop unrolling passes.
203207
P.addArrayCountPropagation();
204208
// To simplify induction variable.
@@ -457,6 +461,10 @@ static void addLateLoopOptPassPipeline(SILPassPipelinePlan &P) {
457461
P.addLICM();
458462
// Simplify CFG after LICM that creates new exit blocks
459463
P.addSimplifyCFG();
464+
// LICM might have added new merging potential by hoisting
465+
// we don't want to restart the pipeline - ignore the
466+
// potential of merging out of two loops
467+
P.addAccessEnforcementOpts();
460468

461469
// Optimize overflow checks.
462470
P.addRedundantOverflowCheckRemoval();

0 commit comments

Comments
 (0)