Skip to content

Fix copy-to-borrow optimization's end_borrow insertion #79810

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 1 commit into from
Mar 7, 2025

Conversation

meg-gupta
Copy link
Contributor

allLifetimeEndingInstructions collects insertion points for end_borrows, and can end up having duplicate entries in the case of enums with non payloaded cases.

Unique the entries so we don't end up with multiple end_borrows

Fixes rdar://146212574

Copy link
Contributor

@nate-chandler nate-chandler left a comment

Choose a reason for hiding this comment

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

lgtm

@meg-gupta
Copy link
Contributor Author

@swift-ci test

Copy link
Contributor

@eeckstein eeckstein left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this!

// FIXME: Use an InstructionSetVector
var allLifetimeEndingInstructions : InstructionWorklist {
var worklist = InstructionWorklist(context)
worklist.pushIfNotVisited(contentsOf: Array(destroys.lazy.map { $0 }) + Array(nonDestroyingLiverangeExits))
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need to "convert" the lazy map sequence to an array, because pushIfNotVisited accepts any kind of sequence.
Instead you can call pushIfNotVisited twice:

  worklist.pushIfNotVisited(contentsOf: destroys.lazy.map { $0 })
  worklist.pushIfNotVisited(contentsOf: nonDestroyingLiverangeExits)

@@ -124,8 +124,11 @@ private struct Uses {
// E.g. the none-case of a switch_enum of an Optional.
private(set) var nonDestroyingLiverangeExits: Stack<Instruction>

var allLifetimeEndingInstructions: [Instruction] {
Array(destroys.lazy.map { $0 }) + Array(nonDestroyingLiverangeExits)
// FIXME: Use an InstructionSetVector
Copy link
Contributor

Choose a reason for hiding this comment

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

An InstructionWorklist is basically an instruction set-vector. We will probably not add an InstructionSetVector in swift. So this comment doesn't make sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It worked out here with inlining the worklist construction at the use-site. InstructionSetVector may still be valuable in some cases. To use a InstructionWorklist in this PR I had to originally copy it to a var and then iterate by popping the elements.

@@ -323,7 +326,12 @@ private func createEndBorrows(for beginBorrow: Value, atEndOf liverange: Instruc
// destroy_value %2
// destroy_value %3 // The final destroy. Here we need to create the `end_borrow`(s)
//
for endInst in collectedUses.allLifetimeEndingInstructions {
var worklist = collectedUses.allLifetimeEndingInstructions
Copy link
Contributor

Choose a reason for hiding this comment

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

Given that the return value of allLifetimeEndingInstructions is not a simple Array anymore, but a worklist which has to be deinitialized, it's probably simpler to remove the var allLifetimeEndingInstructions and inline the worklist construction directly here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Thanks

@meg-gupta meg-gupta force-pushed the fixctb branch 2 times, most recently from 4b49e19 to c130be1 Compare March 6, 2025 18:55
@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta
Copy link
Contributor Author

@swift-ci test source compatibility

@meg-gupta meg-gupta marked this pull request as ready for review March 6, 2025 19:18
@meg-gupta meg-gupta enabled auto-merge March 6, 2025 19:18
allLifetimeEndingInstructions collects insertion points for end_borrows,
and can end up having duplicate entries in the case of enums with non payloaded cases.

Unique the entries so we don't end up with multiple end_borrows

Fixes rdar://146212574
@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta
Copy link
Contributor Author

@swift-ci test source compatibility

@meg-gupta
Copy link
Contributor Author

swiftlang/swift-testing#1002

@swift-ci please test Linux

@meg-gupta meg-gupta merged commit 1b916b2 into swiftlang:main Mar 7, 2025
4 of 7 checks passed
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