Skip to content

UnsafeCell semantics #16022

Closed
Closed
@Zoxc

Description

@Zoxc

I noticed that we currently emit noalias on function parameters of type &mut unconditionally, even if they contain UnsafeCell. This means that we cannot create aliases to the data of UnsafeCell if we pass it to a function as &mut, which implies that we cannot create aliases to mutable data at all. This seems to go against the spirit and documentation of UnsafeCell. @cmr indicated on IRC that this might be reasonable, since UnsafeCell is supposedly only used with &. I think it's a bit odd that changing from & to &mut will silently break UnsafeCell usage.

I suggest we allow UnsafeCell to work with &mut. I further suggest that we only allow pointers based on (using LLVM's definition) UnsafeCell::get and UnsafeCell::unwrap. This means we can ensure the implementation of UnsafeCell::get doesn't base it's return on &self and then mark all references as noalias.

I'm not sure if that will lead to performance improvements since it will inhibit alias analysis on the unsafe code. An expensive way to fix that would be to remove all non-conservative noalias markers, let UnsafeCell::get's return be based on &self and run LLVM passes again.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions