Skip to content
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

[SR-6709] [DebugInfo] Capture Promotion doesn't preserve debug informations #49258

Closed
dcci mannequin opened this issue Jan 5, 2018 · 8 comments
Closed

[SR-6709] [DebugInfo] Capture Promotion doesn't preserve debug informations #49258

dcci mannequin opened this issue Jan 5, 2018 · 8 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software debug info Area → compiler → IRGen: Debug information emission SILOptimizedDebugInfo

Comments

@dcci
Copy link
Mannequin

dcci mannequin commented Jan 5, 2018

Previous ID SR-6709
Radar rdar://problem/36325274
Original Reporter @dcci
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, CompilerCrash, DebugInfo, SILOptimizedDebugInfo
Assignee @dcci
Priority Medium

md5: c8597b2abafcdc1100387a399c53ea2a

Issue Description:

sil_scope 1 { loc "red.swift":1:6 parent @_T03red19captureStackPromoteSiycyF : $@convention(thin) () -> @owned @callee_guaranteed () -> Int }
sil_scope 2 { loc "red.swift":4:1 parent 1 }

// captureStackPromote()
sil hidden @_T03red19captureStackPromoteSiycyF : $@convention(thin) () -> @owned @callee_guaranteed () -> Int {
bb0:
  %0 = alloc_box ${ var Int }, var, name "x", loc "red.swift":2:7, scope 2 // users: %16, %7, %1
  %1 = project_box %0 : ${ var Int }, 0, loc "red.swift":2:7, scope 2 // users: %9, %6
  %2 = metatype $@thin Int.Type, loc "red.swift":2:11, scope 2 // user: %5
  %3 = integer_literal $Builtin.Int2048, 1, loc "red.swift":2:11, scope 2 // user: %5
  // function_ref Int.init(_builtinIntegerLiteral:)
  %4 = function_ref @_T0Si22_builtinIntegerLiteralSiBi2048__tcfC : $@convention(method) (Builtin.Int2048, @thin Int.Type) -> Int, loc "red.swift":2:11, scope 2 // user: %5
  %5 = apply %4(%3, %2) : $@convention(method) (Builtin.Int2048, @thin Int.Type) -> Int, loc "red.swift":2:11, scope 2 // user: %6
  store %5 to [trivial] %1 : $*Int, loc "red.swift":2:11, scope 2 // id: %6
  %7 = copy_value %0 : ${ var Int }, loc "red.swift":3:11, scope 2 // users: %12, %8
  %8 = project_box %7 : ${ var Int }, 0, loc "red.swift":3:11, scope 1 // user: %11
  mark_function_escape %1 : $*Int, loc "red.swift":3:11, scope 2 // id: %9
  // function_ref specialized closure #​1 in captureStackPromote()
  %10 = function_ref @_T03red19captureStackPromoteSiycyFSiycfU_Tf2i_n : $@convention(thin) (Int) -> Int, loc "red.swift":3:11, scope 2 // user: %13
  %11 = load [trivial] %8 : $*Int, loc "red.swift":3:11, scope 2 // user: %13
  destroy_value %7 : ${ var Int }, loc "red.swift":3:11, scope 2 // id: %12
  %13 = partial_apply [callee_guaranteed] %10(%11) : $@convention(thin) (Int) -> Int, loc "red.swift":3:11, scope 2 // users: %14, %15
  debug_value %13 : $@callee_guaranteed () -> Int, let, name "f", loc "red.swift":3:7, scope 2 // id: %14
  destroy_value %13 : $@callee_guaranteed () -> Int, loc "red.swift":4:1, scope 2 // id: %15
  destroy_value %0 : ${ var Int }, loc "red.swift":4:1, scope 2 // id: %16
  unreachable , loc "red.swift":4:1, scope 2      // id: %17
} // end sil function '_T03red19captureStackPromoteSiycyF'

cmdline:

/Users/davide/swift/build/Ninja-RelWithDebInfoAssert/swift-macosx-x86_64/bin/swiftc red.swift -c -Onone -Xllvm -sil-print-debuginfo

testcase:

func captureStackPromote() -> () -> Int {
  var x = 1
  let f = { x }
  return f
}
@dcci
Copy link
Mannequin Author

dcci mannequin commented Jan 5, 2018

cc: @adrian-prantl/@eeckstein

@dcci
Copy link
Mannequin Author

dcci mannequin commented Jan 5, 2018

Output before stack promotion:

*** SIL module before Guaranteed Passes transformation (0) ***
sil_scope 1 { loc "red.swift":1:6 parent @_T03red19captureStackPromoteSiycyF : $@convention(thin) () -> @owned @callee_guaranteed () -> Int }
sil_scope 2 { loc "red.swift":4:1 parent 1 }

// captureStackPromote()
sil hidden @_T03red19captureStackPromoteSiycyF : $@convention(thin) () -> @owned @callee_guaranteed () -> Int {
bb0:
  %0 = alloc_box ${ var Int }, var, name "x", loc "red.swift":2:7, scope 2 // users: %13, %8, %1
  %1 = project_box %0 : ${ var Int }, 0, loc "red.swift":2:7, scope 2 // users: %9, %6
  %2 = metatype $@thin Int.Type, loc "red.swift":2:11, scope 2 // user: %5
  %3 = integer_literal $Builtin.Int2048, 1, loc "red.swift":2:11, scope 2 // user: %5
  // function_ref Int.init(_builtinIntegerLiteral:)
  %4 = function_ref @_T0Si22_builtinIntegerLiteralSiBi2048__tcfC : $@convention(method) (Builtin.Int2048, @thin Int.Type) -> Int, loc "red.swift":2:11, scope 2 // user: %5
  %5 = apply %4(%3, %2) : $@convention(method) (Builtin.Int2048, @thin Int.Type) -> Int, loc "red.swift":2:11, scope 2 // user: %6
  store %5 to [trivial] %1 : $*Int, loc "red.swift":2:11, scope 2 // id: %6
  // function_ref closure #​1 in captureStackPromote()
  %7 = function_ref @_T03red19captureStackPromoteSiycyFSiycfU_ : $@convention(thin) (@guaranteed { var Int }) -> Int, loc "red.swift":3:11, scope 2 // user: %10
  %8 = copy_value %0 : ${ var Int }, loc "red.swift":3:11, scope 2 // user: %10
  mark_function_escape %1 : $*Int, loc "red.swift":3:11, scope 2 // id: %9
  %10 = partial_apply [callee_guaranteed] %7(%8) : $@convention(thin) (@guaranteed { var Int }) -> Int, loc "red.swift":3:11, scope 2 // users: %12, %11
  debug_value %10 : $@callee_guaranteed () -> Int, let, name "f", loc "red.swift":3:7, scope 2 // id: %11
  destroy_value %10 : $@callee_guaranteed () -> Int, loc "red.swift":4:1, scope 2 // id: %12
  destroy_value %0 : ${ var Int }, loc "red.swift":4:1, scope 2 // id: %13
  unreachable , loc "red.swift":4:1, scope 2      // id: %14
} // end sil function '_T03red19captureStackPromoteSiycyF'

sil_scope 1 { loc "red.swift":3:11 parent @_T03red19captureStackPromoteSiycyFSiycfU_ : $@convention(thin) (@guaranteed { var Int }) -> Int }
sil_scope 2 { loc "red.swift":3:15 parent 1 }

@dcci
Copy link
Mannequin Author

dcci mannequin commented Jan 5, 2018

It seems that the `project_box` instruction introduced by the pass gets the wrong scope

  %8 = project_box %7 : ${ var Int }, 0, loc "red.swift":3:11, scope 1 // user: %11

I'll debug this further now that I have a nicely reduced test case 🙂

@adrian-prantl
Copy link
Member

That looks like a legit bug. Probably the fix is a variant of `s/SILBuilder/SILBuilderWithScope/g`.

@adrian-prantl
Copy link
Member

@swift-ci create

@dcci
Copy link
Mannequin Author

dcci mannequin commented Jan 5, 2018

This was a case where we didn't attach the correct scope when creating the `project_box` instruction 🙁

Patch:

$ git diff
diff --git a/lib/SILOptimizer/IPO/CapturePromotion.cpp b/lib/SILOptimizer/IPO/CapturePromotion.cpp
index 0875a1f9a1..2b16ad8672 100644
--- a/lib/SILOptimizer/IPO/CapturePromotion.cpp
+++ b/lib/SILOptimizer/IPO/CapturePromotion.cpp
@@ -1175,7 +1175,7 @@ constructClonedFunction(PartialApplyInst *PAI, FunctionRefInst *FRI,
 static SILValue getOrCreateProjectBoxHelper(SILValue PartialOperand) {
   // If we have a copy_value, just create a project_box on the copy and return.
   if (auto *CVI = dyn_cast<CopyValueInst>(PartialOperand)) {
-    SILBuilder B(std::next(CVI->getIterator()));
+    SILBuilderWithScope B(std::next(CVI->getIterator()));
     return B.createProjectBox(CVI->getLoc(), CVI, 0);
   }

I'll submit a PR soon

@dcci
Copy link
Mannequin Author

dcci mannequin commented Jan 8, 2018

Fixed. #13802

@dcci
Copy link
Mannequin Author

dcci mannequin commented Jan 8, 2018

#13802

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software debug info Area → compiler → IRGen: Debug information emission SILOptimizedDebugInfo
Projects
None yet
Development

No branches or pull requests

2 participants