-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Builtin.isbitwisetakable #18702
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
Builtin.isbitwisetakable #18702
Conversation
8fee6ca
to
c31ef92
Compare
Correct. I believe the current isKnownUniquelyReferenced behavior is a bug. There was some unresolved debate about this a while ago. Anyone checking for a weak reference needs to obtain a strong reference first, but that check could occur in between a separate isKnownUniquelyReferenced check and the object mutation. Even if it isn't a bug now, it certainly wouldn't hurt to make Could you file a bug against the Swift runtime and see what @gparker42 and @mikeash have to say about it? |
The typical pattern is that you have a Of course, as @atrick says, it wouldn't really hurt to fix this. I'm just not sure how useful it is. Does this change have any different requirements? It seems like it's still in the same general case as copy-on-write types in general, where you wouldn't have any weak or unowned references to the objects that this would be used for. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add runtime tests to test/stdlib/Builtins.swift?
Like the "_isPOD" test in that file.
The |
Whoops, no, that's yet another issue. Hm. |
@mikeash I think we're in the same undefined behavior territory regardless of whether the weakly referenced object is mutable collection storage or realloc'd collection storage. This is generally the scenario I'm concerned about:
I agree that for private storage classes, we can avoid the issue. |
In the bug @jrose-apple pointed to, @lorentey points out that the current behavior is actually useful, contradicting what I said in my previous comment: So, we should introduce a new runtime call that checks all refcounts and let the user decide which to use. The existing runtime call is sufficient for the stdlib (although stdlib authors will need to be very careful about thread safety!). @weissi Do you have an example of the weak reference bug that we can use to motivate a change? Could you add your use case to the motivation in SR-5633? I suspect that you can go ahead an use the existing |
c31ef92
to
07bff1b
Compare
@atrick 1) thanks, I filed a bug, in fact I did that before seeing a bug @aschwaighofer thanks, added the test all: I'm seeing that |
Yes they are. The context of a closure is a reference to the context object. A reference is bitwise takable. |
Oh of course, thank you, I was one level of indirection off and thought about the stuff that could be captured 🙈 |
@aschwaighofer / @atrick / @gottesmm wondered what the next steps necessary are to move this along. Should I add docs to SIL.rst? |
@swift-ci Please test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Build failed |
Build failed |
07bff1b
to
bd2de10
Compare
fixed the merge conflict and will kick off tests again. @aschwaighofer can we merge after they go green? |
@swift-ci please test |
what am I trying to achieve
The end goal of this PR and a bunch of follow-ups is to implement support for
realloc
ating Swift objects (to be used for tail-allocated storage) after confirming with a Swift builtin that the type is 'bit-wise takable'. See also the Swift Forums discussion swift_tailRealloc.what is PR doing?
This pull request only adds
Builtin.isbitwisetakable
.state of this PR
This is probably incomplete. This is my first more substantial Swift contribution and I only got here with a lot of @gottesmm's help, in fact most of this was written together with @gottesmm.
open questions/todo
isKnownUniquelyReferenced
. Unfortunately, AFAIKisKnownUniquelyReferenced
returnstrue
for 1 strong + 1 weak reference. And having aweak
reference to the buffer would be really bad withrealloc
used. Can we changeisKnownUniquelyReferenced
to returnfalse
as soon as there is any reference (including weaks)? As @atrick suggested I filed a bug fixing this._isBitwiseTakable((() -> Void).self)
istrue
which doesn't make sense to me