Skip to content

Commit f7d49a9

Browse files
committed
SIL: add the Argument.set(reborrow:) and Argument.hasBorrowEndingUse APIs
1 parent 6b8c6a3 commit f7d49a9

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

SwiftCompilerSources/Sources/Optimizer/PassManager/Context.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,13 @@ extension BasicBlock {
628628
}
629629
}
630630

631+
extension Argument {
632+
func set(reborrow: Bool, _ context: some MutatingContext) {
633+
context.notifyInstructionsChanged()
634+
bridged.setReborrow(reborrow)
635+
}
636+
}
637+
631638
extension AllocRefInstBase {
632639
func setIsStackAllocatable(_ context: some MutatingContext) {
633640
context.notifyInstructionsChanged()

SwiftCompilerSources/Sources/SIL/Argument.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public class Argument : Value, Hashable {
2424
return bridged.getParent().block
2525
}
2626

27-
var bridged: BridgedArgument { BridgedArgument(obj: SwiftObject(self)) }
28-
27+
public var bridged: BridgedArgument { BridgedArgument(obj: SwiftObject(self)) }
28+
2929
public var index: Int {
3030
return parentBlock.arguments.firstIndex(of: self)!
3131
}
@@ -165,6 +165,18 @@ public struct Phi {
165165
return nil
166166
}
167167

168+
// Returns true if the phi has an end_borrow or a re-borrowing branch as user.
169+
// This should be consistent with the `isReborrow` flag.
170+
public var hasBorrowEndingUse: Bool {
171+
let phiValue: Value = borrowedFrom ?? value
172+
return phiValue.uses.contains {
173+
switch $0.ownership {
174+
case .endBorrow, .reborrow: return true
175+
default: return false
176+
}
177+
}
178+
}
179+
168180
public static func ==(lhs: Phi, rhs: Phi) -> Bool {
169181
lhs.value === rhs.value
170182
}

include/swift/SIL/SILBridging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,7 @@ struct BridgedArgument {
864864
BRIDGED_INLINE swift::SILArgument * _Nonnull getArgument() const;
865865
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedBasicBlock getParent() const;
866866
BRIDGED_INLINE bool isReborrow() const;
867+
BRIDGED_INLINE void setReborrow(bool reborrow) const;
867868
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedDeclObj getVarDecl() const;
868869
BRIDGED_INLINE void copyFlags(BridgedArgument fromArgument) const;
869870
};

include/swift/SIL/SILBridgingImpl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,9 @@ BridgedBasicBlock BridgedArgument::getParent() const {
672672
}
673673

674674
bool BridgedArgument::isReborrow() const { return getArgument()->isReborrow(); }
675+
void BridgedArgument::setReborrow(bool reborrow) const {
676+
getArgument()->setReborrow(reborrow);
677+
}
675678

676679
OptionalBridgedDeclObj BridgedArgument::getVarDecl() const {
677680
return {llvm::dyn_cast_or_null<swift::VarDecl>(getArgument()->getDecl())};

0 commit comments

Comments
 (0)