Skip to content

[stdlib] Change _withUnsafeGuaranteedRef to use Builtin.convertUnownedUnsafeToGuaranteed. #30033

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

Conversation

gottesmm
Copy link
Contributor

This builtin (which lowers to raw SIL that doesn't use an actual builtin
instruction) allows us to access an unmanaged value at +0 with a language
guarantee rather than relying on the optimizer.

Previously, we did not do this directly since without OSSA, we were scared that
the frontend/optimizer would not be able to safely emit this code. Now that we
have ownership ssa, we are able to ensure that the frontend always copies the +0
value passed into the closure if the value +0 escapes from the closure (either
via a return, storing into memory, or by passing off as a +1 parameter to a
function).

rdar://59735604

@gottesmm gottesmm requested a review from lorentey February 24, 2020 20:46
@gottesmm
Copy link
Contributor Author

@swift-ci test

@gottesmm
Copy link
Contributor Author

@swift-ci benchmark

@gottesmm
Copy link
Contributor Author

@swift-ci test source compatibility

…dUnsafeToGuaranteed.

This builtin (which lowers to raw SIL that doesn't use an actual builtin
instruction) allows us to access an unmanaged value at +0 with a language
guarantee rather than relying on the optimizer.

Previously, we did not do this directly since without OSSA, we were scared that
the frontend/optimizer would not be able to safely emit this code. Now that we
have ownership ssa, we are able to ensure that the frontend always copies the +0
value passed into the closure if the value +0 escapes from the closure (either
via a return, storing into memory, or by passing off as a +1 parameter to a
function).

rdar://59735604
@gottesmm gottesmm force-pushed the pr-89ff0d5130b608cdfae1e68dfc8c4b10de0f78cd branch from 63061af to 7820ddc Compare February 24, 2020 20:49
@gottesmm
Copy link
Contributor Author

@swift-ci test

@gottesmm
Copy link
Contributor Author

@swift-ci benchmark

@gottesmm
Copy link
Contributor Author

@swift-ci test source compatibility

@gottesmm
Copy link
Contributor Author

@swift-ci test

@gottesmm
Copy link
Contributor Author

@swift-ci benchmark

@aschwaighofer
Copy link
Contributor

As a follow-up we should remove UnsafeGuaranteedPeephole.cpp

@gottesmm
Copy link
Contributor Author

@aschwaighofer SGTM

@swift-ci
Copy link
Contributor

Performance: -O

Code size: -O

Performance: -Osize

Improvement OLD NEW DELTA RATIO
PrefixWhileCountableRange 14 13 -7.1% 1.08x (?)

Code size: -Osize

Performance: -Onone

Code size: -swiftlibs

How to read the data The tables contain differences in performance which are larger than 8% and differences in code size which are larger than 1%.

If you see any unexpected regressions, you should consider fixing the
regressions before you merge the PR.

Noise: Sometimes the performance results (not code size!) contain false
alarms. Unexpected regressions which are marked with '(?)' are probably noise.
If you see regressions which you cannot explain you can try to run the
benchmarks again. If regressions still show up, please consult with the
performance team (@eeckstein).

Hardware Overview
  Model Name: Mac mini
  Model Identifier: Macmini8,1
  Processor Name: Intel Core i7
  Processor Speed: 3.2 GHz
  Number of Processors: 1
  Total Number of Cores: 6
  L2 Cache (per Core): 256 KB
  L3 Cache: 12 MB
  Memory: 64 GB

Copy link
Member

@lorentey lorentey left a comment

Choose a reason for hiding this comment

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

LGTM. Is there a way to add a test to catch future regressions, like the one that broke the original implementation?

@lorentey
Copy link
Member

cc @aschwaighofer

Builtin.unsafeGuaranteedEnd(token)
return result
var tmp = self
let fakeBase: Int? = nil
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you should explain what fakeBase is for, and why a "real" base isn't actually required in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The idea is that the user is stating with this API that they are putting a _fixLifetime around as per the documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@atrick I am happy to do that in a follow on commit.

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 7820ddc

@gottesmm
Copy link
Contributor Author

@lorentey best way is by adding a benchmark

@gottesmm
Copy link
Contributor Author

@swift-ci smoke test linux platform

@gottesmm
Copy link
Contributor Author

linux failure was in sourcekit-lsp everything else passed.

@gottesmm
Copy link
Contributor Author

@swift-ci smoke test linux platform

@gottesmm
Copy link
Contributor Author

@lorentey I have the perfect benchmark for this actually already. I have a version of Prims that uses UnsafeUnowned directly using Unmanaged, but instead of using _wUGR, it uses takeUnretainedValue I will add a version that uses this API instead and land it before this change.

@lorentey
Copy link
Member

@gottesmm Ah, this reminded me I still have a benchmark somewhere with three binary tree implementations (vanilla ADT with @indirect case, UnsafeMutablePointer, Unmanaged) that exercises this

@lorentey
Copy link
Member

(But what I meant is a unit test that simply checks for unexpected retain/release calls, so that we get an immediate and clear signal. Benchmarks don't run on every merge...)

@gottesmm
Copy link
Contributor Author

Ok. I’ll do it in a follow on commit. Clack needs this.

@gottesmm
Copy link
Contributor Author

Or nm. Won’t let me do it on my phone.

@gottesmm
Copy link
Contributor Author

I’m going to put in the test and retest

@gottesmm
Copy link
Contributor Author

@lorentey The benchmark for Prims is here: #30049

@gottesmm
Copy link
Contributor Author

I am just going to use Karoy's benchmark that just landed to show the impact here. Then I am going to merge and fix the other issues people brought up in a follow on commit.

@gottesmm
Copy link
Contributor Author

@swift-ci smoke benchmark

5 similar comments
@gottesmm
Copy link
Contributor Author

@swift-ci smoke benchmark

@gottesmm
Copy link
Contributor Author

@swift-ci smoke benchmark

@gottesmm
Copy link
Contributor Author

@swift-ci smoke benchmark

@gottesmm
Copy link
Contributor Author

@swift-ci smoke benchmark

@gottesmm
Copy link
Contributor Author

@swift-ci smoke benchmark

@gottesmm
Copy link
Contributor Author

@swift-ci benchmark

@gottesmm
Copy link
Contributor Author

Finally, swift-ci does my bidding!

@gottesmm
Copy link
Contributor Author

I'm going to merge this now. The benchmarks will show up when they do. Karoy says locally he validated the perf.

@gottesmm gottesmm merged commit 9e8d65f into swiftlang:master Feb 25, 2020
@gottesmm gottesmm deleted the pr-89ff0d5130b608cdfae1e68dfc8c4b10de0f78cd branch February 25, 2020 22:25
@swift-ci
Copy link
Contributor

!!! Couldn't read commit file !!!

1 similar comment
@swift-ci
Copy link
Contributor

!!! Couldn't read commit file !!!

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.

5 participants