Skip to content

Commit 8aa25d2

Browse files
committed
[addr-move-function] Rename ConsumingClosure* -> Closure.
These operands are not actually consumed. They are instead vars that are represented as inout_aliasable closure arguments (or defer arguments) that we convert to @out parameters after performing move checking.
1 parent 1dc4b47 commit 8aa25d2

File tree

1 file changed

+41
-39
lines changed

1 file changed

+41
-39
lines changed

lib/SILOptimizer/Mandatory/MoveKillsCopyableAddressesChecker.cpp

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,8 @@ struct UseState {
285285
llvm::SmallDenseMap<SILInstruction *, unsigned, 4> destroyToIndexMap;
286286
SmallBlotSetVector<SILInstruction *, 4> reinits;
287287
llvm::SmallDenseMap<SILInstruction *, unsigned, 4> reinitToIndexMap;
288-
llvm::SmallMapVector<Operand *, ClosureOperandState, 1>
289-
consumingClosureOperands;
290-
llvm::SmallDenseMap<Operand *, unsigned, 1> consumingClosureOperandToIndexMap;
288+
llvm::SmallMapVector<Operand *, ClosureOperandState, 1> closureUses;
289+
llvm::SmallDenseMap<Operand *, unsigned, 1> closureOperandToIndexMap;
291290

292291
void insertMarkUnresolvedMoveAddr(MarkUnresolvedMoveAddrInst *inst) {
293292
if (!seenMarkMoves.insert(inst).second)
@@ -305,9 +304,9 @@ struct UseState {
305304
reinits.insert(inst);
306305
}
307306

308-
void insertConsumingClosureOperand(Operand *op) {
309-
consumingClosureOperandToIndexMap[op] = consumingClosureOperands.size();
310-
consumingClosureOperands[op] = {};
307+
void insertClosureOperand(Operand *op) {
308+
closureOperandToIndexMap[op] = closureUses.size();
309+
closureUses[op] = {};
311310
}
312311

313312
void clear() {
@@ -320,8 +319,8 @@ struct UseState {
320319
destroyToIndexMap.clear();
321320
reinits.clear();
322321
reinitToIndexMap.clear();
323-
consumingClosureOperands.clear();
324-
consumingClosureOperandToIndexMap.clear();
322+
closureUses.clear();
323+
closureOperandToIndexMap.clear();
325324
}
326325

327326
SILFunction *getFunction() const { return address->getFunction(); }
@@ -440,7 +439,7 @@ bool GatherLexicalLifetimeUseVisitor::visitUse(Operand *op,
440439
auto *func = fas.getCalleeFunction();
441440
if (!func || !func->isDefer())
442441
return false;
443-
useState.insertConsumingClosureOperand(op);
442+
useState.insertClosureOperand(op);
444443
return true;
445444
}
446445
}
@@ -505,14 +504,14 @@ downwardScanForMoveOut(MarkUnresolvedMoveAddrInst *mvi, UseState &useState,
505504
if (auto fas = FullApplySite::isa(&next)) {
506505
LLVM_DEBUG(llvm::dbgs() << "DownwardScan: ClosureCheck: " << **fas);
507506
for (auto &op : fas.getArgumentOperands()) {
508-
auto iter = useState.consumingClosureOperands.find(&op);
509-
if (iter == useState.consumingClosureOperands.end()) {
507+
auto iter = useState.closureUses.find(&op);
508+
if (iter == useState.closureUses.end()) {
510509
continue;
511510
}
512511

513512
LLVM_DEBUG(llvm::dbgs()
514513
<< "DownwardScan: ClosureCheck: Matching Operand: "
515-
<< fas.getAppliedArgIndex(op) << '\n');
514+
<< fas.getAppliedArgIndex(op));
516515
*foundInst = &next;
517516
*foundOperand = &op;
518517
switch (iter->second.result) {
@@ -523,10 +522,12 @@ downwardScanForMoveOut(MarkUnresolvedMoveAddrInst *mvi, UseState &useState,
523522
case DownwardScanResult::MoveOut:
524523
llvm_unreachable("unhandled");
525524
case DownwardScanResult::ClosureConsume:
525+
LLVM_DEBUG(llvm::dbgs() << ". ClosureConsume.\n");
526526
llvm::copy(iter->second.pairedConsumingInsts,
527527
std::back_inserter(foundClosureInsts));
528528
break;
529529
case DownwardScanResult::ClosureUse:
530+
LLVM_DEBUG(llvm::dbgs() << ". ClosureUse.\n");
530531
llvm::copy(iter->second.pairedUseInsts,
531532
std::back_inserter(foundClosureInsts));
532533
break;
@@ -572,8 +573,7 @@ static bool upwardScanForUseOut(SILInstruction *inst, UseState &useState) {
572573
return false;
573574
if (auto fas = FullApplySite::isa(&iter)) {
574575
for (auto &op : fas.getArgumentOperands()) {
575-
if (useState.consumingClosureOperands.find(&op) !=
576-
useState.consumingClosureOperands.end())
576+
if (useState.closureUses.find(&op) != useState.closureUses.end())
577577
return false;
578578
}
579579
}
@@ -602,8 +602,7 @@ static bool upwardScanForDestroys(SILInstruction *inst, UseState &useState) {
602602
return false;
603603
if (auto fas = FullApplySite::isa(&iter)) {
604604
for (auto &op : fas.getArgumentOperands()) {
605-
if (useState.consumingClosureOperands.find(&op) !=
606-
useState.consumingClosureOperands.end())
605+
if (useState.closureUses.find(&op) != useState.closureUses.end())
607606
return false;
608607
}
609608
}
@@ -626,8 +625,7 @@ static bool upwardScanForInit(SILInstruction *inst, UseState &useState) {
626625
return false;
627626
if (auto fas = FullApplySite::isa(&iter)) {
628627
for (auto &op : fas.getArgumentOperands()) {
629-
if (useState.consumingClosureOperands.find(&op) !=
630-
useState.consumingClosureOperands.end())
628+
if (useState.closureUses.find(&op) != useState.closureUses.end())
631629
return false;
632630
}
633631
}
@@ -650,14 +648,14 @@ namespace {
650648
/// since there must be some sort of use for the closure to even reference it
651649
/// and the compiler emits assigns when it reinitializes vars this early in the
652650
/// pipeline.
653-
struct ConsumingClosureArgDataflowState {
651+
struct ClosureArgDataflowState {
654652
SmallVector<SILInstruction *, 32> livenessWorklist;
655653
SmallVector<SILInstruction *, 32> consumingWorklist;
656654
PrunedLiveness livenessForConsumes;
657655
UseState &useState;
658656

659657
public:
660-
ConsumingClosureArgDataflowState(UseState &useState) : useState(useState) {}
658+
ClosureArgDataflowState(UseState &useState) : useState(useState) {}
661659

662660
bool process(
663661
SILArgument *arg, ClosureOperandState &state,
@@ -688,7 +686,7 @@ struct ConsumingClosureArgDataflowState {
688686

689687
} // namespace
690688

691-
bool ConsumingClosureArgDataflowState::handleSingleBlockCase(
689+
bool ClosureArgDataflowState::handleSingleBlockCase(
692690
SILArgument *address, ClosureOperandState &state) {
693691
// Walk the instructions from the beginning of the block to the end.
694692
for (auto &inst : *address->getParent()) {
@@ -741,9 +739,10 @@ bool ConsumingClosureArgDataflowState::handleSingleBlockCase(
741739
return false;
742740
}
743741

744-
bool ConsumingClosureArgDataflowState::performLivenessDataflow(
742+
bool ClosureArgDataflowState::performLivenessDataflow(
745743
const BasicBlockSet &initBlocks, const BasicBlockSet &livenessBlocks,
746744
const BasicBlockSet &consumingBlocks) {
745+
LLVM_DEBUG(llvm::dbgs() << "ClosureArgLivenessDataflow. Start!\n");
747746
bool foundSingleLivenessUse = false;
748747
auto *fn = useState.getFunction();
749748
auto *frontBlock = &*fn->begin();
@@ -752,8 +751,9 @@ bool ConsumingClosureArgDataflowState::performLivenessDataflow(
752751
for (unsigned i : indices(livenessWorklist)) {
753752
auto *&user = livenessWorklist[i];
754753

755-
if (frontBlock == user->getParent())
754+
if (frontBlock == user->getParent()) {
756755
continue;
756+
}
757757

758758
bool success = false;
759759
for (auto *predBlock : user->getParent()->getPredecessorBlocks()) {
@@ -782,7 +782,7 @@ bool ConsumingClosureArgDataflowState::performLivenessDataflow(
782782
return foundSingleLivenessUse;
783783
}
784784

785-
bool ConsumingClosureArgDataflowState::performConsumingDataflow(
785+
bool ClosureArgDataflowState::performConsumingDataflow(
786786
const BasicBlockSet &initBlocks, const BasicBlockSet &consumingBlocks) {
787787
auto *fn = useState.getFunction();
788788
auto *frontBlock = &*fn->begin();
@@ -821,9 +821,9 @@ bool ConsumingClosureArgDataflowState::performConsumingDataflow(
821821
return foundSingleConsumingUse;
822822
}
823823

824-
void ConsumingClosureArgDataflowState::classifyUses(
825-
BasicBlockSet &initBlocks, BasicBlockSet &livenessBlocks,
826-
BasicBlockSet &consumingBlocks) {
824+
void ClosureArgDataflowState::classifyUses(BasicBlockSet &initBlocks,
825+
BasicBlockSet &livenessBlocks,
826+
BasicBlockSet &consumingBlocks) {
827827

828828
for (auto *user : useState.inits) {
829829
if (upwardScanForInit(user, useState)) {
@@ -868,7 +868,7 @@ void ConsumingClosureArgDataflowState::classifyUses(
868868
}
869869
}
870870

871-
bool ConsumingClosureArgDataflowState::process(
871+
bool ClosureArgDataflowState::process(
872872
SILArgument *address, ClosureOperandState &state,
873873
SmallBlotSetVector<SILInstruction *, 8> &postDominatingConsumingUsers) {
874874
clear();
@@ -915,6 +915,8 @@ bool ConsumingClosureArgDataflowState::process(
915915
if (performLivenessDataflow(initBlocks, livenessBlocks, consumingBlocks)) {
916916
for (unsigned i : indices(livenessWorklist)) {
917917
if (auto *ptr = livenessWorklist[i]) {
918+
LLVM_DEBUG(llvm::dbgs()
919+
<< "ClosureArgLivenessDataflow. Liveness User: " << *ptr);
918920
state.pairedUseInsts.push_back(ptr);
919921
}
920922
}
@@ -932,8 +934,10 @@ bool ConsumingClosureArgDataflowState::process(
932934
// debug_value user. If we have many we can't handle it since something in
933935
// SILGen is emitting weird code. Our tests will ensure that SILGen does not
934936
// diverge by mistake. So we are really just being careful.
935-
if (hasMoreThanOneDebugUse(address))
937+
if (hasMoreThanOneDebugUse(address)) {
938+
// Failing b/c more than one debug use!
936939
return false;
940+
}
937941

938942
SWIFT_DEFER { livenessForConsumes.clear(); };
939943
auto *frontBlock = &*fn->begin();
@@ -1417,7 +1421,7 @@ bool DataflowState::cleanupAllDestroyAddr(
14171421
LLVM_DEBUG(llvm::dbgs() << " Visiting consuming closures!\n");
14181422
for (int index = consumingClosureIndices.find_first(); index != -1;
14191423
index = consumingClosureIndices.find_next(index)) {
1420-
auto &pair = *std::next(useState.consumingClosureOperands.begin(), index);
1424+
auto &pair = *std::next(useState.closureUses.begin(), index);
14211425
auto *op = pair.first;
14221426
LLVM_DEBUG(llvm::dbgs() << " Consuming closure: " << *op->getUser());
14231427
for (auto *predBlock : op->getUser()->getParent()->getPredecessorBlocks()) {
@@ -1478,7 +1482,7 @@ bool DataflowState::cleanupAllDestroyAddr(
14781482
// rather than multiple times for multiple vars.
14791483
for (int index = consumingClosureIndices.find_first(); index != -1;
14801484
index = consumingClosureIndices.find_next(index)) {
1481-
auto &pair = *std::next(useState.consumingClosureOperands.begin(), index);
1485+
auto &pair = *std::next(useState.closureUses.begin(), index);
14821486
auto *closureUse = pair.first;
14831487
if (!closureUse)
14841488
continue;
@@ -1526,9 +1530,8 @@ bool DataflowState::process(
15261530
SmallBitVector indicesOfPairedConsumingClosureUses;
15271531
auto getIndicesOfPairedConsumingClosureUses = [&]() -> SmallBitVector & {
15281532
if (indicesOfPairedConsumingClosureUses.size() !=
1529-
useState.consumingClosureOperands.size())
1530-
indicesOfPairedConsumingClosureUses.resize(
1531-
useState.consumingClosureOperands.size());
1533+
useState.closureUses.size())
1534+
indicesOfPairedConsumingClosureUses.resize(useState.closureUses.size());
15321535
return indicesOfPairedConsumingClosureUses;
15331536
};
15341537

@@ -1616,9 +1619,8 @@ bool DataflowState::process(
16161619
if (iter != consumingClosureBlocks.end()) {
16171620
LLVM_DEBUG(llvm::dbgs() << " Is reinit Block! Setting up for "
16181621
"later deletion if possible!\n");
1619-
auto indexIter =
1620-
useState.consumingClosureOperandToIndexMap.find(iter->second);
1621-
assert(indexIter != useState.consumingClosureOperandToIndexMap.end());
1622+
auto indexIter = useState.closureOperandToIndexMap.find(iter->second);
1623+
assert(indexIter != useState.closureOperandToIndexMap.end());
16221624
getIndicesOfPairedConsumingClosureUses().set(indexIter->second);
16231625
continue;
16241626
}
@@ -1928,7 +1930,7 @@ struct MoveKillsCopyableAddressesChecker {
19281930
UseState useState;
19291931
DataflowState dataflowState;
19301932
UseState closureUseState;
1931-
ConsumingClosureArgDataflowState closureUseDataflowState;
1933+
ClosureArgDataflowState closureUseDataflowState;
19321934
SILOptFunctionBuilder &funcBuilder;
19331935
llvm::SmallMapVector<FullApplySite, SmallBitVector, 8>
19341936
applySiteToPromotedArgIndices;
@@ -2110,7 +2112,7 @@ bool MoveKillsCopyableAddressesChecker::check(SILValue address) {
21102112
//
21112113
// This summary will let us treat the whole closure's effect on the closure
21122114
// operand as if it was a single instruction.
2113-
for (auto &pair : useState.consumingClosureOperands) {
2115+
for (auto &pair : useState.closureUses) {
21142116
auto *operand = pair.first;
21152117
auto &closureState = pair.second;
21162118

0 commit comments

Comments
 (0)