Skip to content

[capture-promotion] Add debugging messages to capture promotion. #8451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions lib/SILOptimizer/IPO/CapturePromotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,19 +702,24 @@ isNonEscapingUse(Operand *O, SmallVectorImpl<SILInstruction*> &Mutations) {
static bool
examineAllocBoxInst(AllocBoxInst *ABI, ReachabilityInfo &RI,
llvm::DenseMap<PartialApplyInst*, unsigned> &IM) {
DEBUG(llvm::dbgs() << "Visiting alloc box: " << *ABI);
SmallVector<SILInstruction*, 32> Mutations;

// Scan the box for interesting uses.
for (Operand *O : ABI->getUses()) {
if (auto *PAI = dyn_cast<PartialApplyInst>(O->getUser())) {
DEBUG(llvm::dbgs() << " Found partial: " << *PAI);

unsigned OpNo = O->getOperandNumber();
assert(OpNo != 0 && "Alloc box used as callee of partial apply?");

// If we've already seen this partial apply, then it means the same alloc
// box is being captured twice by the same closure, which is odd and
// unexpected: bail instead of trying to handle this case.
if (IM.count(PAI))
if (IM.count(PAI)) {
DEBUG(llvm::dbgs() << " Already seen... bailing!\n");
return false;
}

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

auto *Fn = PAI->getReferencedFunction();
if (!Fn || !Fn->isDefinition())
if (!Fn || !Fn->isDefinition()) {
DEBUG(llvm::dbgs() << " Not a direct function definition "
"reference. Bailing!\n");
return false;
}

SILArgument *BoxArg = getBoxFromIndex(Fn, Index);

Expand All @@ -737,20 +745,29 @@ examineAllocBoxInst(AllocBoxInst *ABI, ReachabilityInfo &RI,
auto BoxTy = BoxArg->getType().castTo<SILBoxType>();
assert(BoxTy->getLayout()->getFields().size() == 1
&& "promoting compound box not implemented yet");
if (BoxTy->getFieldType(M, 0).isAddressOnly(M))
if (BoxTy->getFieldType(M, 0).isAddressOnly(M)) {
DEBUG(llvm::dbgs()
<< " Box is an address only argument... Bailing!\n");
return false;
}

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

// Record the index and continue.
DEBUG(llvm::dbgs() << " Can be optimized!\n");
DEBUG(llvm::dbgs() << " Index: " << Index << "\n");
IM.insert(std::make_pair(PAI, Index));
continue;
}

if (auto *PBI = dyn_cast<ProjectBoxInst>(O->getUser())) {
DEBUG(llvm::dbgs() << " Found project box: " << *PBI);
// Check for mutations of the address component.
SILValue Addr = PBI;
// If the AllocBox is used by a mark_uninitialized, scan the MUI for
Expand All @@ -762,15 +779,22 @@ examineAllocBoxInst(AllocBoxInst *ABI, ReachabilityInfo &RI,
}

for (Operand *AddrOp : Addr->getUses()) {
if (!isNonEscapingUse(AddrOp, Mutations))
if (!isNonEscapingUse(AddrOp, Mutations)) {
DEBUG(llvm::dbgs() << " Has escaping user of addr... bailing: "
<< *AddrOp->getUser());
return false;
}
}
continue;
}

// Verify that this use does not otherwise allow the alloc_box to
// escape.
if (!isNonEscapingUse(O, Mutations))
if (!isNonEscapingUse(O, Mutations)) {
DEBUG(llvm::dbgs() << " Have unknown escaping user: "
<< *O->getUser());
return false;
}
}

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

DEBUG(llvm::dbgs()
<< "Checking for any mutations that invalidate captures...\n");
// Loop over all mutations to possibly invalidate captures.
for (auto *I : Mutations) {
auto Iter = IM.begin();
Expand All @@ -797,17 +823,22 @@ examineAllocBoxInst(AllocBoxInst *ABI, ReachabilityInfo &RI,
// block is after the partial_apply.
if (RI.isReachable(PAI->getParent(), I->getParent()) ||
(PAI->getParent() == I->getParent() && isAfter(PAI, I))) {
DEBUG(llvm::dbgs() << " Invalidating: " << *PAI);
DEBUG(llvm::dbgs() << " Because of user: " << *I);
auto Prev = Iter++;
IM.erase(Prev);
continue;
}
++Iter;
}
// If there are no valid captures left, then stop.
if (IM.empty())
if (IM.empty()) {
DEBUG(llvm::dbgs() << " Ran out of valid captures... bailing!\n");
return false;
}
}

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

Expand Down Expand Up @@ -976,6 +1007,8 @@ class CapturePromotionPass : public SILModuleTransform {

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