Skip to content

[SILOptimizer][DebugInfo] Preliminary support for DIExpression in SROA and Mem2Reg #38736

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 3 commits into from
Aug 6, 2021

Conversation

mshockwave
Copy link
Contributor

@mshockwave mshockwave commented Aug 3, 2021

SROA and Mem2Reg now can leverage DIExpression -- op_fragment, more
specifically -- to generate correct debug info for optimized SIL. Some
important highlights:

  • The new swift::salvageDebugInfo, similar to llvm::salvageDebugInfo,
    tries to restore / transfer debug info from a deleted instruction.
    Currently I only implemented this for store instruction whose
    destination is an alloc_stack value.
  • DCE now does not remove debug_value instruction whose associating
    with a function argument SSA value that is not used elsewhere. Since
    that SSA value will not disappear so we should keep the debug info.

NOTE: This PR is only for code review. It depends on and includes #38734 #38735 . Please merge this PR instead of the previous two.

@mshockwave
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Aug 3, 2021

Build failed
Swift Test Linux Platform
Git Sha - cd762e63cfc52f1c6f0c21bf7e9cde7fbc40fd20

@@ -76,6 +81,7 @@ eraseFromParentWithDebugInsts(SILInstruction *inst,
// Just matching what eraseFromParentWithDebugInsts is today.
if (nextII == inst->getIterator())
++nextII;
swift::salvageDebugInfo(inst);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be part of the SROA PR or should this be in its own patch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My original thought was that if salvageDebugInfo is put in a separate patch (or PR), there will be no use case or test there, which is a little weird. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we write a standalone test using sil-opt that runs exactly one pass that would delete an instruction? If the answer is no, then let's just keep it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently we're calling salvageDebugInfo inside InstructionDeleter, which will affect multiple Passes. We can first create a PR that only calls salvageDebugInfo in a single Pass then revise it to use InstructionDeleter in later PR...but I feel like it will be a little over-engineering

@swift-ci
Copy link
Contributor

swift-ci commented Aug 3, 2021

Build failed
Swift Test OS X Platform
Git Sha - cd762e63cfc52f1c6f0c21bf7e9cde7fbc40fd20

The `alloc_stack` instruction now can carry advanced debug info like
source-variable-specific SIL location, type, and SIL DIExpression.
Debug variables that are marked 'implicit' on its `debug_value`
instruction mean that they were generated by compiler. Optimizers are
free to remove them (if it becomes a dead code, for instance) even in
-Onone. Since they are barely used by users and keeping them might lead
to incorrect IRGen results.
@mshockwave
Copy link
Contributor Author

I just found that I might can split the changes regarding debug scope here to a separate PR. I'll update this accordingly later

@mshockwave mshockwave force-pushed the dev-sil-diexpr-optimize branch from cd762e6 to 0dc20ba Compare August 4, 2021 22:14
@mshockwave
Copy link
Contributor Author

I just found that I might can split the changes regarding debug scope here to a separate PR. I'll update this accordingly later

Since that part of changes don't really affect the feature here, I'll put it in another PR that is not depending on the current series of patches
@swift-ci please test

@mshockwave mshockwave force-pushed the dev-sil-diexpr-optimize branch from 0dc20ba to e9db7d7 Compare August 4, 2021 23:07
@mshockwave
Copy link
Contributor Author

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Aug 4, 2021

Build failed
Swift Test OS X Platform
Git Sha - 0dc20ba963614494beea0d2d7a859eafc361e06d

@swift-ci
Copy link
Contributor

swift-ci commented Aug 5, 2021

Build failed
Swift Test OS X Platform
Git Sha - e9db7d742a339fd56d1ebcaf6022c632bf73a81d

@mshockwave mshockwave force-pushed the dev-sil-diexpr-optimize branch from e9db7d7 to ab126dd Compare August 5, 2021 00:05
@mshockwave
Copy link
Contributor Author

I just found that I might can split the changes regarding debug scope here to a separate PR. I'll update this accordingly later

Since that part of changes don't really affect the feature here, I'll put it in another PR that is not depending on the current series of patches

I was wrong about that: Without the debug scope changes it will fail on a pretty common optimization pattern (specialization on implicit-generated enum functions) despite passing all tests, so I'm bringing them back and add corresponding test cases. I've also addressed all the feedbacks on that part before they were previously detached from this PR.
Sorry for this.

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Aug 5, 2021

Build failed
Swift Test Linux Platform
Git Sha - e9db7d742a339fd56d1ebcaf6022c632bf73a81d

Copy link
Contributor

@adrian-prantl adrian-prantl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, I think you've addressed all my concerns.

@swift-ci
Copy link
Contributor

swift-ci commented Aug 5, 2021

Build failed
Swift Test Linux Platform
Git Sha - ab126dd1bfa0ebc0aed6fb1d32f0c43521b791a9

@swift-ci
Copy link
Contributor

swift-ci commented Aug 5, 2021

Build failed
Swift Test OS X Platform
Git Sha - ab126dd1bfa0ebc0aed6fb1d32f0c43521b791a9

…A and Mem2Reg

SROA and Mem2Reg now can leverage DIExpression -- op_fragment, more
specifically -- to generate correct debug info for optimized SIL. Some
important highlights:
 - The new swift::salvageDebugInfo, similar to llvm::salvageDebugInfo,
   tries to restore / transfer debug info from a deleted instruction.
   Currently I only implemented this for store instruction whose
   destination is an alloc_stack value.
 - Since we now have source-variable-specific SIL location inside a
   `debug_value` instruction (and its friends), this patch teaches
   SILCloner and SILInliner to remap the debug scope there in addition
   to debug scope of the instruction.
 - DCE now does not remove `debug_value` instruction whose associating
   with a function argument SSA value that is not used elsewhere. Since
   that SSA value will not disappear so we should keep the debug info.
@mshockwave mshockwave force-pushed the dev-sil-diexpr-optimize branch from ab126dd to 9a8f2ed Compare August 6, 2021 00:28
@mshockwave
Copy link
Contributor Author

So the failure on Linux buildbot was caused by uninitialized struct type information during IRGen. I think it only happens if a debug_value instruction is the first ever SIL instruction that references a certain struct type, in which case IRGen hasn't translated that type into its internal representation (e.g. RecordTypeInfo).

A more sophisticated solution will be asking IRGen to resolve the type when this happens. But since this scenario is pretty rare and I don't want to add more complexity into this PR, for now I simply bails out the (LLVM) debug intrinsic generation process if this occurs, similar to our current resolution on non-fixed type.

@swift-ci please test

@adrian-prantl
Copy link
Contributor

A more sophisticated solution will be asking IRGen to resolve the type when this happens. But since this scenario is pretty rare and I don't want to add more complexity into this PR, for now I simply bails out the (LLVM) debug intrinsic generation process if this occurs, similar to our current resolution on non-fixed type.

Thanks for adding the comment explaining this to IRGenDebugInfo. LGTM!

@mshockwave mshockwave merged commit c3390ec into swiftlang:main Aug 6, 2021
eeckstein added a commit to eeckstein/swift that referenced this pull request Aug 26, 2021
Don't let debug_value instructions bail the optimization.

This fixes a couple of performance regressions, which were introduced by adding more debug_value instructions (swiftlang#38736).

rdar://82327743
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants