Skip to content

Commit 551862c

Browse files
authored
Merge pull request #29861 from gottesmm/pr-a7dde2547b804476719e9b64324acca8f5d05680
[ownership] Change BorrowScopeIntroducingValue::{visit,gather}LocalScopeEndingUses to use isConsumingUse instead of checking for isa<EndBorrowInst>().
2 parents 532a3f1 + 5f45e0f commit 551862c

File tree

2 files changed

+89
-39
lines changed

2 files changed

+89
-39
lines changed

include/swift/SIL/OwnershipUtils.h

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,12 @@ struct BorrowScopeOperandKind {
236236
}
237237

238238
void print(llvm::raw_ostream &os) const;
239-
SWIFT_DEBUG_DUMP;
239+
SWIFT_DEBUG_DUMP { print(llvm::dbgs()); }
240240
};
241241

242+
llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
243+
BorrowScopeOperandKind kind);
244+
242245
/// An operand whose user instruction introduces a new borrow scope for the
243246
/// operand's value. The value of the operand must be considered as implicitly
244247
/// borrowed until the user's corresponding end scope instruction.
@@ -258,25 +261,10 @@ struct BorrowScopeOperand {
258261
return BorrowScopeOperand(*kind, op);
259262
}
260263

261-
void visitEndScopeInstructions(function_ref<void(Operand *)> func) const {
262-
switch (kind) {
263-
case BorrowScopeOperandKind::BeginBorrow:
264-
for (auto *use : cast<BeginBorrowInst>(op->getUser())->getUses()) {
265-
if (isa<EndBorrowInst>(use->getUser())) {
266-
func(use);
267-
}
268-
}
269-
return;
270-
case BorrowScopeOperandKind::BeginApply: {
271-
auto *user = cast<BeginApplyInst>(op->getUser());
272-
for (auto *use : user->getTokenResult()->getUses()) {
273-
func(use);
274-
}
275-
return;
276-
}
277-
}
278-
llvm_unreachable("Covered switch isn't covered");
279-
}
264+
void visitEndScopeInstructions(function_ref<void(Operand *)> func) const;
265+
266+
void print(llvm::raw_ostream &os) const;
267+
SWIFT_DEBUG_DUMP { print(llvm::dbgs()); }
280268

281269
private:
282270
/// Internal constructor for failable static constructor. Please do not expand
@@ -286,7 +274,7 @@ struct BorrowScopeOperand {
286274
};
287275

288276
llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
289-
BorrowScopeOperandKind kind);
277+
const BorrowScopeOperand &operand);
290278

291279
struct BorrowScopeIntroducingValueKind {
292280
using UnderlyingKindTy = std::underlying_type<ValueKind>::type;
@@ -336,7 +324,7 @@ struct BorrowScopeIntroducingValueKind {
336324
}
337325

338326
void print(llvm::raw_ostream &os) const;
339-
SWIFT_DEBUG_DUMP;
327+
SWIFT_DEBUG_DUMP { print(llvm::dbgs()); }
340328
};
341329

342330
llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
@@ -419,6 +407,9 @@ struct BorrowScopeIntroducingValue {
419407
SmallPtrSetImpl<SILBasicBlock *> &visitedBlocks,
420408
DeadEndBlocks &deadEndBlocks) const;
421409

410+
void print(llvm::raw_ostream &os) const;
411+
SWIFT_DEBUG_DUMP { print(llvm::dbgs()); }
412+
422413
private:
423414
/// Internal constructor for failable static constructor. Please do not expand
424415
/// its usage since it assumes the code passed in is well formed.
@@ -427,6 +418,9 @@ struct BorrowScopeIntroducingValue {
427418
: kind(kind), value(value) {}
428419
};
429420

421+
llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
422+
const BorrowScopeIntroducingValue &value);
423+
430424
/// Look up through the def-use chain of \p inputValue, recording any "borrow"
431425
/// introducing values that we find into \p out. If at any point, we find a
432426
/// point in the chain we do not understand, we bail and return false. If we are

lib/SIL/OwnershipUtils.cpp

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,62 @@ bool swift::isOwnershipForwardingInst(SILInstruction *i) {
8787
return isOwnershipForwardingValueKind(SILNodeKind(i->getKind()));
8888
}
8989

90+
//===----------------------------------------------------------------------===//
91+
// Borrow Scope Operand
92+
//===----------------------------------------------------------------------===//
93+
94+
void BorrowScopeOperandKind::print(llvm::raw_ostream &os) const {
95+
switch (value) {
96+
case Kind::BeginBorrow:
97+
os << "BeginBorrow";
98+
return;
99+
case Kind::BeginApply:
100+
os << "BeginApply";
101+
return;
102+
}
103+
llvm_unreachable("Covered switch isn't covered?!");
104+
}
105+
106+
llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os,
107+
BorrowScopeOperandKind kind) {
108+
kind.print(os);
109+
return os;
110+
}
111+
112+
void BorrowScopeOperand::print(llvm::raw_ostream &os) const {
113+
os << "BorrowScopeOperand:\n"
114+
"Kind: " << kind << "\n"
115+
"Value: " << op->get()
116+
<< "User: " << op->getUser();
117+
}
118+
119+
llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os,
120+
const BorrowScopeOperand &operand) {
121+
operand.print(os);
122+
return os;
123+
}
124+
125+
void BorrowScopeOperand::visitEndScopeInstructions(
126+
function_ref<void(Operand *)> func) const {
127+
switch (kind) {
128+
case BorrowScopeOperandKind::BeginBorrow:
129+
for (auto *use : cast<BeginBorrowInst>(op->getUser())->getUses()) {
130+
if (isa<EndBorrowInst>(use->getUser())) {
131+
func(use);
132+
}
133+
}
134+
return;
135+
case BorrowScopeOperandKind::BeginApply: {
136+
auto *user = cast<BeginApplyInst>(op->getUser());
137+
for (auto *use : user->getTokenResult()->getUses()) {
138+
func(use);
139+
}
140+
return;
141+
}
142+
}
143+
llvm_unreachable("Covered switch isn't covered");
144+
}
145+
90146
//===----------------------------------------------------------------------===//
91147
// Borrow Introducers
92148
//===----------------------------------------------------------------------===//
@@ -106,10 +162,10 @@ void BorrowScopeIntroducingValueKind::print(llvm::raw_ostream &os) const {
106162
llvm_unreachable("Covered switch isn't covered?!");
107163
}
108164

109-
void BorrowScopeIntroducingValueKind::dump() const {
110-
#ifndef NDEBUG
111-
print(llvm::dbgs());
112-
#endif
165+
void BorrowScopeIntroducingValue::print(llvm::raw_ostream &os) const {
166+
os << "BorrowScopeIntroducingValue:\n"
167+
"Kind: " << kind << "\n"
168+
"Value: " << value;
113169
}
114170

115171
void BorrowScopeIntroducingValue::getLocalScopeEndingInstructions(
@@ -120,12 +176,12 @@ void BorrowScopeIntroducingValue::getLocalScopeEndingInstructions(
120176
case BorrowScopeIntroducingValueKind::SILFunctionArgument:
121177
llvm_unreachable("Should only call this with a local scope");
122178
case BorrowScopeIntroducingValueKind::BeginBorrow:
123-
llvm::copy(cast<BeginBorrowInst>(value)->getEndBorrows(),
124-
std::back_inserter(scopeEndingInsts));
125-
return;
126179
case BorrowScopeIntroducingValueKind::LoadBorrow:
127-
llvm::copy(cast<LoadBorrowInst>(value)->getEndBorrows(),
128-
std::back_inserter(scopeEndingInsts));
180+
for (auto *use : value->getUses()) {
181+
if (use->isConsumingUse()) {
182+
scopeEndingInsts.push_back(use->getUser());
183+
}
184+
}
129185
return;
130186
}
131187
llvm_unreachable("Covered switch isn't covered?!");
@@ -137,16 +193,10 @@ void BorrowScopeIntroducingValue::visitLocalScopeEndingUses(
137193
switch (kind) {
138194
case BorrowScopeIntroducingValueKind::SILFunctionArgument:
139195
llvm_unreachable("Should only call this with a local scope");
140-
case BorrowScopeIntroducingValueKind::BeginBorrow:
141-
for (auto *use : value->getUses()) {
142-
if (isa<EndBorrowInst>(use->getUser())) {
143-
visitor(use);
144-
}
145-
}
146-
return;
147196
case BorrowScopeIntroducingValueKind::LoadBorrow:
197+
case BorrowScopeIntroducingValueKind::BeginBorrow:
148198
for (auto *use : value->getUses()) {
149-
if (isa<EndBorrowInst>(use->getUser())) {
199+
if (use->isConsumingUse()) {
150200
visitor(use);
151201
}
152202
}
@@ -212,6 +262,12 @@ llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os,
212262
return os;
213263
}
214264

265+
llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os,
266+
const BorrowScopeIntroducingValue &value) {
267+
value.print(os);
268+
return os;
269+
}
270+
215271
bool BorrowScopeIntroducingValue::areInstructionsWithinScope(
216272
ArrayRef<SILInstruction *> instructions,
217273
SmallVectorImpl<SILInstruction *> &scratchSpace,

0 commit comments

Comments
 (0)