Skip to content

Commit e116df3

Browse files
committed
Introduce return_borrow instruction
1 parent 0e4dc2f commit e116df3

32 files changed

+239
-4
lines changed

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,6 +1857,18 @@ final public class ReturnInst : TermInst, UnaryInstruction {
18571857
public override var isFunctionExiting: Bool { true }
18581858
}
18591859

1860+
final public class ReturnBorrowInst : TermInst {
1861+
public var returnValue: Value { operands[0].value }
1862+
public var enclosingOperands: OperandArray {
1863+
let ops = operands
1864+
return ops[1..<ops.count]
1865+
}
1866+
public var enclosingValues: LazyMapSequence<LazySequence<OperandArray>.Elements, Value> {
1867+
enclosingOperands.values
1868+
}
1869+
public override var isFunctionExiting: Bool { true }
1870+
}
1871+
18601872
final public class ThrowInst : TermInst, UnaryInstruction {
18611873
public var thrownValue: Value { operand.value }
18621874
public override var isFunctionExiting: Bool { true }

SwiftCompilerSources/Sources/SIL/Registration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ private func registerSILClasses() {
247247

248248
register(UnreachableInst.self)
249249
register(ReturnInst.self)
250+
register(ReturnBorrowInst.self)
250251
register(ThrowInst.self)
251252
register(ThrowAddrInst.self)
252253
register(YieldInst.self)

include/swift/SIL/AddressWalker.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ TransitiveAddressWalker<Impl>::walk(SILValue projectedAddress) {
185185
llvm_unreachable("Never takes an address");
186186
// Point uses.
187187
case TermKind::ReturnInst:
188+
case TermKind::ReturnBorrowInst:
188189
case TermKind::ThrowInst:
189190
case TermKind::YieldInst:
190191
case TermKind::TryApplyInst:

include/swift/SIL/SILBuilder.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2658,6 +2658,12 @@ class SILBuilder {
26582658
getFunction(), getSILDebugLocation(Loc), ReturnValue));
26592659
}
26602660

2661+
ReturnBorrowInst *createReturnBorrow(SILLocation Loc, SILValue returnValue,
2662+
ArrayRef<SILValue> enclosingValues) {
2663+
return insertTerminator(ReturnBorrowInst::create(
2664+
getSILDebugLocation(Loc), returnValue, enclosingValues, getModule()));
2665+
}
2666+
26612667
ThrowInst *createThrow(SILLocation Loc, SILValue errorValue) {
26622668
return insertTerminator(
26632669
new (getModule()) ThrowInst(getSILDebugLocation(Loc), errorValue));

include/swift/SIL/SILCloner.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3417,6 +3417,22 @@ SILCloner<ImplClass>::visitReturnInst(ReturnInst *Inst) {
34173417
getOpValue(Inst->getOperand())));
34183418
}
34193419

3420+
template <typename ImplClass>
3421+
void SILCloner<ImplClass>::visitReturnBorrowInst(ReturnBorrowInst *rbi) {
3422+
getBuilder().setCurrentDebugScope(getOpScope(rbi->getDebugScope()));
3423+
if (!getBuilder().hasOwnership()) {
3424+
return recordClonedInstruction(
3425+
rbi, getBuilder().createReturn(getOpLocation(rbi->getLoc()),
3426+
getOpValue(rbi->getReturnValue())));
3427+
}
3428+
3429+
auto enclosingValues = getOpValueArray<8>(rbi->getEnclosingValues());
3430+
recordClonedInstruction(
3431+
rbi, getBuilder().createReturnBorrow(getOpLocation(rbi->getLoc()),
3432+
getOpValue(rbi->getReturnValue()),
3433+
enclosingValues));
3434+
}
3435+
34203436
template<typename ImplClass>
34213437
void
34223438
SILCloner<ImplClass>::visitThrowInst(ThrowInst *Inst) {

include/swift/SIL/SILInstruction.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10123,6 +10123,7 @@ class TermInst : public NonValueInstruction {
1012310123
case TermKind::UnwindInst:
1012410124
case TermKind::UnreachableInst:
1012510125
case TermKind::ReturnInst:
10126+
case TermKind::ReturnBorrowInst:
1012610127
case TermKind::ThrowInst:
1012710128
case TermKind::ThrowAddrInst:
1012810129
case TermKind::YieldInst:
@@ -10252,6 +10253,34 @@ class ReturnInst
1025210253
}
1025310254
};
1025410255

10256+
class ReturnBorrowInst final
10257+
: public InstructionBaseWithTrailingOperands<
10258+
SILInstructionKind::ReturnBorrowInst, ReturnBorrowInst, TermInst> {
10259+
friend SILBuilder;
10260+
10261+
ReturnBorrowInst(SILDebugLocation DebugLoc, ArrayRef<SILValue> operands);
10262+
10263+
static ReturnBorrowInst *create(SILDebugLocation DebugLoc, SILValue value,
10264+
ArrayRef<SILValue> enclosingValues,
10265+
SILModule &M);
10266+
10267+
public:
10268+
SILValue getReturnValue() const { return getAllOperands()[0].get(); }
10269+
10270+
ArrayRef<Operand> getEnclosingValueOperands() const {
10271+
return getAllOperands().drop_front();
10272+
}
10273+
10274+
OperandValueArrayRef getEnclosingValues() const {
10275+
return OperandValueArrayRef(getEnclosingValueOperands());
10276+
}
10277+
10278+
SuccessorListTy getSuccessors() {
10279+
// No Successors.
10280+
return SuccessorListTy();
10281+
}
10282+
};
10283+
1025510284
/// ThrowInst - Throw a typed error, returning it via the direct error result.
1025610285
class ThrowInst
1025710286
: public UnaryInstructionBase<SILInstructionKind::ThrowInst, TermInst>

include/swift/SIL/SILNodes.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,8 @@ ABSTRACT_INST(TermInst, SILInstruction)
731731
TermInst, None, DoesNotRelease)
732732
TERMINATOR(ReturnInst, return,
733733
TermInst, None, DoesNotRelease)
734+
TERMINATOR(ReturnBorrowInst, return_borrow,
735+
TermInst, None, DoesNotRelease)
734736
TERMINATOR(ThrowInst, throw,
735737
TermInst, None, DoesNotRelease)
736738
TERMINATOR(ThrowAddrInst, throw_addr,

include/swift/SILOptimizer/Utils/SCCVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ class SCCVisitor {
130130

131131
case TermKind::UnreachableInst:
132132
case TermKind::ReturnInst:
133+
case TermKind::ReturnBorrowInst:
133134
case TermKind::SwitchValueInst:
134135
case TermKind::ThrowInst:
135136
case TermKind::ThrowAddrInst:

lib/IRGen/IRGenSIL.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,9 @@ class IRGenSILFunction :
15041504
void visitBranchInst(BranchInst *i);
15051505
void visitCondBranchInst(CondBranchInst *i);
15061506
void visitReturnInst(ReturnInst *i);
1507+
void visitReturnBorrowInst(ReturnBorrowInst *i) {
1508+
llvm_unreachable("unimplemented");
1509+
}
15071510
void visitThrowInst(ThrowInst *i);
15081511
void visitThrowAddrInst(ThrowAddrInst *i);
15091512
void visitUnwindInst(UnwindInst *i);

lib/SIL/IR/OperandOwnership.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,12 @@ OperandOwnership OperandOwnershipClassifier::visitReturnInst(ReturnInst *i) {
636636
llvm_unreachable("covered switch");
637637
}
638638

639+
OperandOwnership
640+
OperandOwnershipClassifier::visitReturnBorrowInst(ReturnBorrowInst *rbi) {
641+
return getOperandIndex() == 0 ? OperandOwnership::GuaranteedForwarding
642+
: OperandOwnership::EndBorrow;
643+
}
644+
639645
OperandOwnership OperandOwnershipClassifier::visitAssignInst(AssignInst *i) {
640646
if (getValue() != i->getSrc()) {
641647
return OperandOwnership::TrivialUse;

0 commit comments

Comments
 (0)