Skip to content

[NFC] cleanup Instruction/Value.findVarDecl() APIs #81290

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 2 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ private struct LifetimeVariable {

private init(introducer: Value, _ context: some Context) {
if let arg = introducer as? FunctionArgument {
self.varDecl = arg.varDecl
self.varDecl = arg.findVarDecl()
self.sourceLoc = arg.sourceLoc
self.isArgument = true
self.isClosureCapture = arg.isClosureCapture
Expand Down
6 changes: 3 additions & 3 deletions SwiftCompilerSources/Sources/SIL/Argument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ public class Argument : Value, Hashable {

public var isLexical: Bool { false }

public var varDecl: VarDecl? {
public func findVarDecl() -> VarDecl? {
if let varDecl = bridged.getVarDecl().getAs(VarDecl.self) {
return varDecl
}
return debugUserDecl
return findVarDeclFromDebugUsers()
}

public var sourceLoc: SourceLoc? { varDecl?.nameLoc }
public var sourceLoc: SourceLoc? { findVarDecl()?.nameLoc }

public static func ==(lhs: Argument, rhs: Argument) -> Bool {
lhs === rhs
Expand Down
30 changes: 15 additions & 15 deletions SwiftCompilerSources/Sources/SIL/Instruction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -450,17 +450,17 @@ public enum VariableScopeInstruction {
scopeBegin.uses.lazy.filter { $0.endsLifetime || $0.instruction is ExtendLifetimeInst }
}

// TODO: with SIL verification, we might be able to make varDecl non-Optional.
public var varDecl: VarDecl? {
if let debugVarDecl = instruction.debugResultDecl {
return debugVarDecl
}
// TODO: assert that VarDecl is valid whenever isFromVarDecl returns tyue.
public func findVarDecl() -> VarDecl? {
// SILGen may produce double var_decl instructions for the same variable:
// %box = alloc_box [var_decl] "x"
// begin_borrow %box [var_decl]
//
// Assume that, if the begin_borrow or move_value does not have its own debug_value, then it must actually be
// associated with its operand's var_decl.
// Therefore, first check if begin_borrow or move_value has any debug_value users.
if let debugVarDecl = instruction.findVarDeclFromDebugUsers() {
return debugVarDecl
}
// Otherwise, assume that the var_decl is associated with its operand's var_decl.
return instruction.operands[0].value.definingInstruction?.findVarDecl()
}
}
Expand All @@ -472,14 +472,14 @@ extension Instruction {
return varDeclInst.varDecl
}
if let varScopeInst = VariableScopeInstruction(self) {
return varScopeInst.varDecl
return varScopeInst.findVarDecl()
}
return debugResultDecl
return findVarDeclFromDebugUsers()
}

var debugResultDecl: VarDecl? {
func findVarDeclFromDebugUsers() -> VarDecl? {
for result in results {
if let varDecl = result.debugUserDecl {
if let varDecl = result.findVarDeclFromDebugUsers() {
return varDecl
}
}
Expand All @@ -488,14 +488,14 @@ extension Instruction {
}

extension Value {
var debugValDecl: VarDecl? {
public func findVarDecl() -> VarDecl? {
if let arg = self as? Argument {
return arg.varDecl
return arg.findVarDecl()
}
return debugUserDecl
return findVarDeclFromDebugUsers()
}

var debugUserDecl: VarDecl? {
func findVarDeclFromDebugUsers() -> VarDecl? {
for use in uses {
if let debugVal = use.instruction as? DebugValueInst {
return debugVal.varDecl
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/LifetimeDependence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1132,8 +1132,8 @@ class LifetimeDependenceChecker {
// The 'newValue' dependence kind must match the getter's dependence kind
// because generated the implementation '_modify' accessor composes the
// getter's result with the setter's 'newValue'. In particular, if the
// result type is non-Escapable then the setter must not depend on
// 'newValue'.
// result type is Escapable then the getter does not have any lifetime
// dependency, so the setter cannot depend on 'newValue'.
if (!paramTypeInContext->isEscapable()) {
targetDeps = std::move(targetDeps)
.add(newValIdx, LifetimeDependenceKind::Inherit);
Expand Down