@@ -450,17 +450,17 @@ public enum VariableScopeInstruction {
450450 scopeBegin. uses. lazy. filter { $0. endsLifetime || $0. instruction is ExtendLifetimeInst }
451451 }
452452
453- // TODO: with SIL verification, we might be able to make varDecl non-Optional.
454- public var varDecl : VarDecl ? {
455- if let debugVarDecl = instruction. debugResultDecl {
456- return debugVarDecl
457- }
453+ // TODO: assert that VarDecl is valid whenever isFromVarDecl returns tyue.
454+ public func findVarDecl( ) -> VarDecl ? {
458455 // SILGen may produce double var_decl instructions for the same variable:
459456 // %box = alloc_box [var_decl] "x"
460457 // begin_borrow %box [var_decl]
461458 //
462- // Assume that, if the begin_borrow or move_value does not have its own debug_value, then it must actually be
463- // associated with its operand's var_decl.
459+ // Therefore, first check if begin_borrow or move_value has any debug_value users.
460+ if let debugVarDecl = instruction. findVarDeclFromDebugUsers ( ) {
461+ return debugVarDecl
462+ }
463+ // Otherwise, assume that the var_decl is associated with its operand's var_decl.
464464 return instruction. operands [ 0 ] . value. definingInstruction? . findVarDecl ( )
465465 }
466466}
@@ -472,14 +472,14 @@ extension Instruction {
472472 return varDeclInst. varDecl
473473 }
474474 if let varScopeInst = VariableScopeInstruction ( self ) {
475- return varScopeInst. varDecl
475+ return varScopeInst. findVarDecl ( )
476476 }
477- return debugResultDecl
477+ return findVarDeclFromDebugUsers ( )
478478 }
479479
480- var debugResultDecl : VarDecl ? {
480+ func findVarDeclFromDebugUsers ( ) -> VarDecl ? {
481481 for result in results {
482- if let varDecl = result. debugUserDecl {
482+ if let varDecl = result. findVarDeclFromDebugUsers ( ) {
483483 return varDecl
484484 }
485485 }
@@ -488,14 +488,14 @@ extension Instruction {
488488}
489489
490490extension Value {
491- var debugValDecl : VarDecl ? {
491+ public func findVarDecl ( ) -> VarDecl ? {
492492 if let arg = self as? Argument {
493- return arg. varDecl
493+ return arg. findVarDecl ( )
494494 }
495- return debugUserDecl
495+ return findVarDeclFromDebugUsers ( )
496496 }
497497
498- var debugUserDecl : VarDecl ? {
498+ func findVarDeclFromDebugUsers ( ) -> VarDecl ? {
499499 for use in uses {
500500 if let debugVal = use. instruction as? DebugValueInst {
501501 return debugVal. varDecl
0 commit comments