Skip to content

[ownership] Add a section to SIL.rst that describes the semantics of safe interior pointers in Ownership SIL. #34618

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

@gottesmm gottesmm commented Nov 7, 2020

This is just part of my ongoing effort to document more explicitly Ownership SSA
in SIL.rst.

NOTE: I realized while writing this, we can also do this form of verification
with owned values. I don't know how easy/hard it would be though. Its nice to
have that owned values do not need to look for these. But that is something that
we may want to do.

@gottesmm gottesmm force-pushed the pr-1639094f806f1130063c975fee6b1f74f2babb6a branch 2 times, most recently from b31336a to 3ba5a5d Compare November 7, 2020 01:55
@meg-gupta
Copy link
Contributor

Thanks for writing this! LGTM.

Copy link
Contributor

@zoecarver zoecarver left a comment

Choose a reason for hiding this comment

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

Mostly nits. I don't think any of my comments are too important.

docs/SIL.rst Outdated

// %2 is an interior pointer into %1. Since %2 is an address, it's uses are
// not treated as uses of the underlying borrowed object %1 in the ownership
// system. This is because at the ownership level objects with none
Copy link
Contributor

Choose a reason for hiding this comment

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

"with .none ownership"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am going to None_ so it jumps back, but good catch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah. Actually I didn't see this is in line SIL so I will do None. We shouldn't use .none or it sounds like we are talking about optionals.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, sounds good. There's another place that needs to be changed too, then.

docs/SIL.rst Outdated
// %2 is an interior pointer into the Klass k. Since %2 is an address and
// address have .none ownership, it's uses are not treated as uses of the
// underlying object %1.
%2 = ref_element_addr %1 : $Klass, #Klass.k // %1 is a $*KlassWrapper
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mean:

// %2 is a $*KlassWrapper

Maybe it would help to put class Klass { ... } somewhere in SIL.rst.

docs/SIL.rst Outdated
existential container. Must be the type for which the box was initially
allocated for and not for an “opened” archetype.

(*) We still need to finish adding support for project_box, but all other ones
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: "all others are" or "all other instructions are".

@gottesmm gottesmm force-pushed the pr-1639094f806f1130063c975fee6b1f74f2babb6a branch from 3ba5a5d to 291841f Compare November 7, 2020 23:37
Copy link
Contributor

@zoecarver zoecarver left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for adding this information!

Copy link
Contributor

@atrick atrick left a comment

Choose a reason for hiding this comment

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

I think it could be more clear that the transitive search for address uses stops at any instruction that isn't in your approved list of interior pointer operations. Typically, those uses do not propagate the pointer value, so they are safe. Other uses escape the address value such as pointer_to_address. Those uses are unsafe--the user is reponsible for managing unsafe pointer lifetimes and the compiler must not extend those pointer lifetimes.


* `project_box`_ - projects a pointer out of a reference counted box. (*)
* `ref_element_addr`_ - projects a field out of a reference counted class.
* `ref_tail_addr`_ - projects out a pointer to a class’s tail allocated array
Copy link
Contributor

Choose a reason for hiding this comment

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

tail_addr is another instruction that uses the ref_tail_addr and produces a new address

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 does, but it isn't an interior pointer instruction since it doesn't convert an object to an address. It just translates addresses to addresses. IOW one can never use a tail_addr to be be a mixed projection like operation.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's correct. tail_addr is just an address projection, in fact all the other address projections are also relevant to the discussion. So I think what we're saying here is that an "interior pointer operation" needs its own borrow scope, and all transitive uses after propagating through address projections need to be within that borrow scope. It still may be worth pointing out that address_to_pointer is not considered an address projection, so it effectively escapes the interior address from the borrow scope... it's up to the the programmers and optimizer to manage that pointer lifetime without any help from the ownership verifier.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes.

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 Just pushed in a bit more prose. Tell me what you think.

@gottesmm gottesmm force-pushed the pr-1639094f806f1130063c975fee6b1f74f2babb6a branch from 291841f to ea22692 Compare November 8, 2020 03:33
…safe interior pointers in Ownership SIL.

This is just part of my ongoing effort to document more explicitly Ownership SSA
in SIL.rst.

NOTE: I realized while writing this, we might be able toalso do this form of
verification with owned values. I don't know how easy/hard it would be
though. Its nice to have that owned values do not need to look for these when
optimizing. So that would need to be balanced against expanding this.
@gottesmm gottesmm force-pushed the pr-1639094f806f1130063c975fee6b1f74f2babb6a branch from ea22692 to 9712e45 Compare November 8, 2020 23:20
@gottesmm
Copy link
Contributor Author

gottesmm commented Nov 9, 2020

@swift-ci smoke 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.

LGTM

@gottesmm gottesmm merged commit 4aa1ef3 into swiftlang:main Nov 9, 2020
@gottesmm gottesmm deleted the pr-1639094f806f1130063c975fee6b1f74f2babb6a branch November 9, 2020 18:34
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