18
18
#include " swift/SIL/SILArgument.h"
19
19
#include " swift/SIL/SILBuilder.h"
20
20
#include " swift/SIL/SILInstruction.h"
21
+ #include " swift/SILOptimizer/Analysis/AccessedStorageAnalysis.h"
21
22
#include " swift/SILOptimizer/Analysis/AliasAnalysis.h"
22
23
#include " swift/SILOptimizer/Analysis/Analysis.h"
23
24
#include " swift/SILOptimizer/Analysis/ArraySemantic.h"
@@ -361,6 +362,7 @@ class LoopTreeOptimization {
361
362
AliasAnalysis *AA;
362
363
SideEffectAnalysis *SEA;
363
364
DominanceInfo *DomTree;
365
+ AccessedStorageAnalysis *ASA;
364
366
bool Changed;
365
367
366
368
// / True if LICM is done on high-level SIL, i.e. semantic calls are not
@@ -380,8 +382,9 @@ class LoopTreeOptimization {
380
382
public:
381
383
LoopTreeOptimization (SILLoop *TopLevelLoop, SILLoopInfo *LI,
382
384
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 ),
385
388
RunsOnHighLevelSIL (RunsOnHighLevelSil) {
386
389
// Collect loops for a recursive bottom-up traversal in the loop tree.
387
390
BotUpWorkList.push_back (TopLevelLoop);
@@ -528,9 +531,10 @@ static bool handledEndAccesses(BeginAccessInst *BI, SILLoop *Loop) {
528
531
return true ;
529
532
}
530
533
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) {
534
538
if (BI->getEnforcement () != SILAccessEnforcement::Dynamic) {
535
539
return false ;
536
540
}
@@ -550,8 +554,18 @@ analyzeBeginAccess(BeginAccessInst *BI,
550
554
findAccessedStorageNonNested (OtherBI));
551
555
};
552
556
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 ;
555
569
}
556
570
557
571
// Analyzes current loop for hosting/sinking potential:
@@ -575,6 +589,8 @@ void LoopTreeOptimization::analyzeCurrentLoop(
575
589
SmallVector<FixLifetimeInst *, 8 > FixLifetimes;
576
590
// Contains begin_access, we might be able to hoist them.
577
591
SmallVector<BeginAccessInst *, 8 > BeginAccesses;
592
+ // Contains all applies - used for begin_access
593
+ SmallVector<FullApplySite, 8 > fullApplies;
578
594
579
595
for (auto *BB : Loop->getBlocks ()) {
580
596
for (auto &Inst : *BB) {
@@ -618,6 +634,9 @@ void LoopTreeOptimization::analyzeCurrentLoop(
618
634
LLVM_FALLTHROUGH;
619
635
}
620
636
default : {
637
+ if (auto fullApply = FullApplySite::isa (&Inst)) {
638
+ fullApplies.push_back (fullApply);
639
+ }
621
640
checkSideEffects (Inst, MayWrites);
622
641
if (canHoistUpDefault (&Inst, Loop, DomTree, RunsOnHighLevelSIL)) {
623
642
HoistUp.insert (&Inst);
@@ -660,7 +679,7 @@ void LoopTreeOptimization::analyzeCurrentLoop(
660
679
LLVM_DEBUG (llvm::dbgs () << " Some end accesses can't be handled\n " );
661
680
continue ;
662
681
}
663
- if (analyzeBeginAccess (BI, BeginAccesses)) {
682
+ if (analyzeBeginAccess (BI, BeginAccesses, fullApplies, ASA )) {
664
683
SpecialHoist.insert (BI);
665
684
}
666
685
}
@@ -711,14 +730,15 @@ class LICM : public SILFunctionTransform {
711
730
DominanceAnalysis *DA = PM->getAnalysis <DominanceAnalysis>();
712
731
AliasAnalysis *AA = PM->getAnalysis <AliasAnalysis>();
713
732
SideEffectAnalysis *SEA = PM->getAnalysis <SideEffectAnalysis>();
733
+ AccessedStorageAnalysis *ASA = getAnalysis<AccessedStorageAnalysis>();
714
734
DominanceInfo *DomTree = nullptr ;
715
735
716
736
LLVM_DEBUG (llvm::dbgs () << " Processing loops in " << F->getName () << " \n " );
717
737
bool Changed = false ;
718
738
719
739
for (auto *TopLevelLoop : *LoopInfo) {
720
740
if (!DomTree) DomTree = DA->get (F);
721
- LoopTreeOptimization Opt (TopLevelLoop, LoopInfo, AA, SEA, DomTree,
741
+ LoopTreeOptimization Opt (TopLevelLoop, LoopInfo, AA, SEA, DomTree, ASA,
722
742
RunsOnHighLevelSil);
723
743
Changed |= Opt.optimize ();
724
744
}
0 commit comments