Skip to content

Commit 8d37812

Browse files
authored
Merge pull request #8451 from gottesmm/capture_promotion_logging
2 parents 50a12df + c819774 commit 8d37812

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

lib/SILOptimizer/IPO/CapturePromotion.cpp

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -702,19 +702,24 @@ isNonEscapingUse(Operand *O, SmallVectorImpl<SILInstruction*> &Mutations) {
702702
static bool
703703
examineAllocBoxInst(AllocBoxInst *ABI, ReachabilityInfo &RI,
704704
llvm::DenseMap<PartialApplyInst*, unsigned> &IM) {
705+
DEBUG(llvm::dbgs() << "Visiting alloc box: " << *ABI);
705706
SmallVector<SILInstruction*, 32> Mutations;
706707

707708
// Scan the box for interesting uses.
708709
for (Operand *O : ABI->getUses()) {
709710
if (auto *PAI = dyn_cast<PartialApplyInst>(O->getUser())) {
711+
DEBUG(llvm::dbgs() << " Found partial: " << *PAI);
712+
710713
unsigned OpNo = O->getOperandNumber();
711714
assert(OpNo != 0 && "Alloc box used as callee of partial apply?");
712715

713716
// If we've already seen this partial apply, then it means the same alloc
714717
// box is being captured twice by the same closure, which is odd and
715718
// 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");
717721
return false;
722+
}
718723

719724
SILModule &M = PAI->getModule();
720725
auto closureType = PAI->getType().castTo<SILFunctionType>();
@@ -726,8 +731,11 @@ examineAllocBoxInst(AllocBoxInst *ABI, ReachabilityInfo &RI,
726731
unsigned Index = OpNo - 1 + closureConv.getNumSILArguments();
727732

728733
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");
730737
return false;
738+
}
731739

732740
SILArgument *BoxArg = getBoxFromIndex(Fn, Index);
733741

@@ -737,20 +745,29 @@ examineAllocBoxInst(AllocBoxInst *ABI, ReachabilityInfo &RI,
737745
auto BoxTy = BoxArg->getType().castTo<SILBoxType>();
738746
assert(BoxTy->getLayout()->getFields().size() == 1
739747
&& "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");
741751
return false;
752+
}
742753

743754
// Verify that this closure is known not to mutate the captured value; if
744755
// it does, then conservatively refuse to promote any captures of this
745756
// value.
746-
if (!isNonMutatingCapture(BoxArg))
757+
if (!isNonMutatingCapture(BoxArg)) {
758+
DEBUG(llvm::dbgs() << " Is a mutating capture... Bailing!\n");
747759
return false;
760+
}
748761

749762
// Record the index and continue.
763+
DEBUG(llvm::dbgs() << " Can be optimized!\n");
764+
DEBUG(llvm::dbgs() << " Index: " << Index << "\n");
750765
IM.insert(std::make_pair(PAI, Index));
751766
continue;
752767
}
768+
753769
if (auto *PBI = dyn_cast<ProjectBoxInst>(O->getUser())) {
770+
DEBUG(llvm::dbgs() << " Found project box: " << *PBI);
754771
// Check for mutations of the address component.
755772
SILValue Addr = PBI;
756773
// If the AllocBox is used by a mark_uninitialized, scan the MUI for
@@ -762,15 +779,22 @@ examineAllocBoxInst(AllocBoxInst *ABI, ReachabilityInfo &RI,
762779
}
763780

764781
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());
766785
return false;
786+
}
767787
}
768788
continue;
769789
}
790+
770791
// Verify that this use does not otherwise allow the alloc_box to
771792
// escape.
772-
if (!isNonEscapingUse(O, Mutations))
793+
if (!isNonEscapingUse(O, Mutations)) {
794+
DEBUG(llvm::dbgs() << " Have unknown escaping user: "
795+
<< *O->getUser());
773796
return false;
797+
}
774798
}
775799

776800
// Helper lambda function to determine if instruction b is strictly after
@@ -787,6 +811,8 @@ examineAllocBoxInst(AllocBoxInst *ABI, ReachabilityInfo &RI,
787811
return false;
788812
};
789813

814+
DEBUG(llvm::dbgs()
815+
<< "Checking for any mutations that invalidate captures...\n");
790816
// Loop over all mutations to possibly invalidate captures.
791817
for (auto *I : Mutations) {
792818
auto Iter = IM.begin();
@@ -797,17 +823,22 @@ examineAllocBoxInst(AllocBoxInst *ABI, ReachabilityInfo &RI,
797823
// block is after the partial_apply.
798824
if (RI.isReachable(PAI->getParent(), I->getParent()) ||
799825
(PAI->getParent() == I->getParent() && isAfter(PAI, I))) {
826+
DEBUG(llvm::dbgs() << " Invalidating: " << *PAI);
827+
DEBUG(llvm::dbgs() << " Because of user: " << *I);
800828
auto Prev = Iter++;
801829
IM.erase(Prev);
802830
continue;
803831
}
804832
++Iter;
805833
}
806834
// 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");
808837
return false;
838+
}
809839
}
810840

841+
DEBUG(llvm::dbgs() << " We can optimize this box!\n");
811842
return true;
812843
}
813844

@@ -976,6 +1007,8 @@ class CapturePromotionPass : public SILModuleTransform {
9761007

9771008
void CapturePromotionPass::processFunction(SILFunction *F,
9781009
SmallVectorImpl<SILFunction*> &Worklist) {
1010+
DEBUG(llvm::dbgs() << "******** Performing Capture Promotion on: "
1011+
<< F->getName() << "********\n");
9791012
// This is a map from each partial apply to a set of indices of promotable
9801013
// box variables.
9811014
PartialApplyIndicesMap IndicesMap;

0 commit comments

Comments
 (0)