@@ -702,19 +702,24 @@ isNonEscapingUse(Operand *O, SmallVectorImpl<SILInstruction*> &Mutations) {
702
702
static bool
703
703
examineAllocBoxInst (AllocBoxInst *ABI, ReachabilityInfo &RI,
704
704
llvm::DenseMap<PartialApplyInst*, unsigned > &IM) {
705
+ DEBUG (llvm::dbgs () << " Visiting alloc box: " << *ABI);
705
706
SmallVector<SILInstruction*, 32 > Mutations;
706
707
707
708
// Scan the box for interesting uses.
708
709
for (Operand *O : ABI->getUses ()) {
709
710
if (auto *PAI = dyn_cast<PartialApplyInst>(O->getUser ())) {
711
+ DEBUG (llvm::dbgs () << " Found partial: " << *PAI);
712
+
710
713
unsigned OpNo = O->getOperandNumber ();
711
714
assert (OpNo != 0 && " Alloc box used as callee of partial apply?" );
712
715
713
716
// If we've already seen this partial apply, then it means the same alloc
714
717
// box is being captured twice by the same closure, which is odd and
715
718
// unexpected: bail instead of trying to handle this case.
716
- if (IM.count (PAI))
719
+ if (IM.count (PAI)) {
720
+ DEBUG (llvm::dbgs () << " Already seen... bailing!\n " );
717
721
return false ;
722
+ }
718
723
719
724
SILModule &M = PAI->getModule ();
720
725
auto closureType = PAI->getType ().castTo <SILFunctionType>();
@@ -726,8 +731,11 @@ examineAllocBoxInst(AllocBoxInst *ABI, ReachabilityInfo &RI,
726
731
unsigned Index = OpNo - 1 + closureConv.getNumSILArguments ();
727
732
728
733
auto *Fn = PAI->getReferencedFunction ();
729
- if (!Fn || !Fn->isDefinition ())
734
+ if (!Fn || !Fn->isDefinition ()) {
735
+ DEBUG (llvm::dbgs () << " Not a direct function definition "
736
+ " reference. Bailing!\n " );
730
737
return false ;
738
+ }
731
739
732
740
SILArgument *BoxArg = getBoxFromIndex (Fn, Index);
733
741
@@ -737,20 +745,29 @@ examineAllocBoxInst(AllocBoxInst *ABI, ReachabilityInfo &RI,
737
745
auto BoxTy = BoxArg->getType ().castTo <SILBoxType>();
738
746
assert (BoxTy->getLayout ()->getFields ().size () == 1
739
747
&& " promoting compound box not implemented yet" );
740
- if (BoxTy->getFieldType (M, 0 ).isAddressOnly (M))
748
+ if (BoxTy->getFieldType (M, 0 ).isAddressOnly (M)) {
749
+ DEBUG (llvm::dbgs ()
750
+ << " Box is an address only argument... Bailing!\n " );
741
751
return false ;
752
+ }
742
753
743
754
// Verify that this closure is known not to mutate the captured value; if
744
755
// it does, then conservatively refuse to promote any captures of this
745
756
// value.
746
- if (!isNonMutatingCapture (BoxArg))
757
+ if (!isNonMutatingCapture (BoxArg)) {
758
+ DEBUG (llvm::dbgs () << " Is a mutating capture... Bailing!\n " );
747
759
return false ;
760
+ }
748
761
749
762
// Record the index and continue.
763
+ DEBUG (llvm::dbgs () << " Can be optimized!\n " );
764
+ DEBUG (llvm::dbgs () << " Index: " << Index << " \n " );
750
765
IM.insert (std::make_pair (PAI, Index));
751
766
continue ;
752
767
}
768
+
753
769
if (auto *PBI = dyn_cast<ProjectBoxInst>(O->getUser ())) {
770
+ DEBUG (llvm::dbgs () << " Found project box: " << *PBI);
754
771
// Check for mutations of the address component.
755
772
SILValue Addr = PBI;
756
773
// If the AllocBox is used by a mark_uninitialized, scan the MUI for
@@ -762,15 +779,22 @@ examineAllocBoxInst(AllocBoxInst *ABI, ReachabilityInfo &RI,
762
779
}
763
780
764
781
for (Operand *AddrOp : Addr->getUses ()) {
765
- if (!isNonEscapingUse (AddrOp, Mutations))
782
+ if (!isNonEscapingUse (AddrOp, Mutations)) {
783
+ DEBUG (llvm::dbgs () << " Has escaping user of addr... bailing: "
784
+ << *AddrOp->getUser ());
766
785
return false ;
786
+ }
767
787
}
768
788
continue ;
769
789
}
790
+
770
791
// Verify that this use does not otherwise allow the alloc_box to
771
792
// escape.
772
- if (!isNonEscapingUse (O, Mutations))
793
+ if (!isNonEscapingUse (O, Mutations)) {
794
+ DEBUG (llvm::dbgs () << " Have unknown escaping user: "
795
+ << *O->getUser ());
773
796
return false ;
797
+ }
774
798
}
775
799
776
800
// Helper lambda function to determine if instruction b is strictly after
@@ -787,6 +811,8 @@ examineAllocBoxInst(AllocBoxInst *ABI, ReachabilityInfo &RI,
787
811
return false ;
788
812
};
789
813
814
+ DEBUG (llvm::dbgs ()
815
+ << " Checking for any mutations that invalidate captures...\n " );
790
816
// Loop over all mutations to possibly invalidate captures.
791
817
for (auto *I : Mutations) {
792
818
auto Iter = IM.begin ();
@@ -797,17 +823,22 @@ examineAllocBoxInst(AllocBoxInst *ABI, ReachabilityInfo &RI,
797
823
// block is after the partial_apply.
798
824
if (RI.isReachable (PAI->getParent (), I->getParent ()) ||
799
825
(PAI->getParent () == I->getParent () && isAfter (PAI, I))) {
826
+ DEBUG (llvm::dbgs () << " Invalidating: " << *PAI);
827
+ DEBUG (llvm::dbgs () << " Because of user: " << *I);
800
828
auto Prev = Iter++;
801
829
IM.erase (Prev);
802
830
continue ;
803
831
}
804
832
++Iter;
805
833
}
806
834
// If there are no valid captures left, then stop.
807
- if (IM.empty ())
835
+ if (IM.empty ()) {
836
+ DEBUG (llvm::dbgs () << " Ran out of valid captures... bailing!\n " );
808
837
return false ;
838
+ }
809
839
}
810
840
841
+ DEBUG (llvm::dbgs () << " We can optimize this box!\n " );
811
842
return true ;
812
843
}
813
844
@@ -976,6 +1007,8 @@ class CapturePromotionPass : public SILModuleTransform {
976
1007
977
1008
void CapturePromotionPass::processFunction (SILFunction *F,
978
1009
SmallVectorImpl<SILFunction*> &Worklist) {
1010
+ DEBUG (llvm::dbgs () << " ******** Performing Capture Promotion on: "
1011
+ << F->getName () << " ********\n " );
979
1012
// This is a map from each partial apply to a set of indices of promotable
980
1013
// box variables.
981
1014
PartialApplyIndicesMap IndicesMap;
0 commit comments