This makes me wonder if a better shape for Volatile<T> would be for it to wrap an UnsafeMutablePointer<T>. That would make it trivially bitwise copyable, and hopefully it would still be reasonably easy to import a volatile C constant by constructing Volatile(&foo) at the bridging layer.
This would, of course, have the downside of not being able to easily write Span<Volatile<T>> for larger buffers of volatile values. But given the behavior you’ve mentioned, it seems like a VolatileSpan<T> sort of type would be better here because it would allow you to more clearly document the expected ordering/behavior of bulk reads and writes.
Yeah, something like VolatileRef<T> and VolatileSpan<T> would be one approach to fix the issues I brought up. Another possible approach is to have types like VolatileCell<T> and ConstVolatileCell<T> that (like Cell and ConstCell) only have special semantics when borrowed, and behave like normal values otherwise.
I haven't had a chance to read this whole thread, but as a person who hacks up dumb stuff with Embedded Swift, Volatile sounds like it would definitely help me with MMIO (without needing swift-mmio or a build hack to access it from C-space).
This is not just "running a safe-ish subset of operations on memory that another core is concurrently modifying without using synchronization primitives"; sometimes the actual bus lines are wired up to not-CPU-controlled RAM.
(I recognize this is a niche, but I was happy to see Volatile show up in my Swift Forums Summary email.)
I agree with this. Volatile isn't really a synchronization mechanism, it's a memory access semantic. I'd prefer that it go into the standard library, partly for that reason, and partly because it's how we want to interoperate with volatile-qualified values from C(++)... and I don't want interoperability to have different behavior/visibility depending on whether one imports Synchronization.
I do wonder whether we should call this type VolatileCell to put it in the same family of the other Cell types: an aliasable location with specific access semantics.
On ConstCell...
It is deliberate, because the "non-mutation-permitting" version of the cell doesn't mean immutable the way that Span or Ref do. All it means that you can't modify the data; it might still change under you. For that reason, I think ConstCell isn't the right name for this type, because it's not "constant" in any sense that applies in Swift. It's basically the C notion of const, but that's not something that we should use for naming. Personally, I'd prefer ReadOnlyCell: it's wordy but clear.
Unsafe(Buffer)Pointer is the primary counter-argument to my reasoning above. The pointee can change out from underneath you, as with Cell. I consider that to be part of the fundamental unsafety of the type rather than a pattern to emulate.
And then, the aesthetic argument: outside of interoperability with C, I expect uses of the ConstCell/ReadOnlyCell to be exceedingly rare. It would be unfortunate to burn the short name (Cell) on that.
Naming aside, I do wish it were easier to convert from a Span<Cell<T>> to a Span<ConstCell<T>> (ditto for Ref), because I expect that's going to be common in interop scenarios. How awful would this be?
extension Span {
func constCells<T>() -> Span<ConstCell<T>> where Element == Cell<T> { ... }
}
On the conversions...
I'd rather we remove the API. One can do this with the unsafe pointer APIs. We don't need to make it easy.
About unsafeBorrow...
This statement is only true for APIs directly on the array, within the closure passed to Array.withUnsafeMutableBufferPointer, and assuming that buffer isn't used in a way that corrupts the data. For example, one can corrupt the contents of the buffer by breaking memory exclusivity, or escape the buffer outside of the closure, each of which will cause later array operations to be broken.
I think Range.init(uncheckedBounds:) isn't a good comparison here because it's so trivial to reason about the safety invariants.
Span.withUnsafeMutableBufferPointer is an interesting comparison, because Span doesn't (and can't) "know" about the mutation in the way that Array does, so it's more similar to Cell.
I don't agree with this statement. I think Array's unsafe buffer pointer API is an example, and Span's equivalent API is a more relevant one.
On RefCell and runtime exclusivity checking
This is an interesting idea. One could provide yielding accessors that call into the Swift runtime's swift_beginAccess / swift_endAccess, which is what provides dynamic exclusivity checking.
Right.
It would have to be a separate type, because the normal get/set operations on the value would also have to trigger exclusivity checks. Part of the intent of Cell as it is today is that it doesn't do dynamic exclusivity checking.
On Sendable...
I'd actually like to bring the Sendable conformance back when the underlying element type is Sendable and FullyInhabited. The FullyInhabited constraint is extremely strong: you can use Span APIs to scribble on individual bytes of a FullyInhabited type, and it's memory-safe to do so. Concurrency doesn't change that, it just adds more sources where individual-byte-scribbling can happen if you're doing this kind of mutation. For many of the named use cases of Cell---shared memory, C interoperability---those memory-scribbling sources are already quite plentiful.
On the other hand, the workarounds for not having Sendable on Cell are pretty annoying, because it means that you can't (for example) share a Span<Cell<Double>> across isolation domains without wrapping up that whole type in an @unchecked Sendable. This scatters a lot of apparent (but not new) unsafety throughout a code base, for not much benefit.
I think we should either have the Sendable conformance on both (my preference) or neither.
I agree with both or neither! From a semantics perspective, you convinced me offline that it wouldn't be so bad to let Cell be Sendable for FullyInhabited[1]. I can only think of it leading to safety issues in cases like where Cell<Int> is used to do unchecked indexing... but one shouldn't do that! Store it in some larger structure and mutate it normally, or create a wrapper for Int (you should do this anyway to enforce its only created after bounds / invariant checking...) which won't be FullyInhabited. I think it's been brought up before but we could consider an even stronger protocol (SingleWord?) for types which are always updated as a single operation (and so won't tear) but I don't think this is super necessary and it would be platform dependent which is weird.
I think we need to figure out the answer to this before we can let people do "unconcerning" data races in safe code. The worst you can do in safe code today is copy out and overwrite without actually doing a CAS, which is not great (it's sort of a "high level" data race) but not the same as a data race from a UB perspective AFAIK.
assuming this doesn't allow safe code to introduce what LLVM considers UB ↩︎
Those examples require the buffer pointer to be unsafely dereferenced, such as by using UnsafeBufferPointer.subscript(_:). (If I understand correctly, escaping the buffer pointer is only problematic if the buffer pointer is subsequently unsafely dereferenced; it isn't inherently undefined behavior as long as the buffer pointer is unused after being escaped.[1]) And they are incorrect regardless of what safe code does afterwards.
The contract of both Array.withUnsafeBufferPointer and Span.withUnsafeBufferPointer is that the buffer pointer passed to the closure is not used incorrectly. The only way to use the buffer pointer incorrectly is to perform some unsafe operation on it, namely unsafely dereferencing it. So the contract of withUnsafeBufferPointer only depends on the behavior of subsequent unsafe code, not the behavior of subsequent safe code.
The contract of Cell.unsafeBorrow is that otherwise-safe operations of the Cell (such as Cell.value) are not used for the duration of the borrow. If we see Cell.unsafeBorrow being used, we can't know if we're upholding its contract unless we look at the behavior of subsequent safe code; that's what I think makes it different from withUnsafeBufferPointer.
To clarify why I think that's bad: it compromises the ability of types to communicate invariants in safe code. If we have a Span and we're currently writing safe code, we can rely on its invariants, even if we've also used withUnsafeBufferPointer (as long as our unsafe code is correct, e.g. we don't dereference the buffer pointer after escaping it). If we have a Cell and we're currently writing safe code, we can't be fully confident that its invariants are upheld, because we might have previously used Cell.unsafeBorrow; if so, then all it takes to break the contract is a subsequent safe operation like Cell.value.
However, I now realize that Cell.unsafeBorrow wouldn't be the only example, because Span.unsafeCells() would be another.
That is, this would be okay: let danglingPtr = withUnsafePointer(x) { ptr in ptr } as long as danglingPtr itself is unused. ↩︎
One thing that I think we'd have to be wary of to avoid arbitrary UB is not so much tearing loads, but redundant loads. Given an ordinary load in LLVM IR, the compiler might choose to actually load multiple times (say because it would have to spill a register to the stack anyway), expecting the same value to be available if there are no apparent writes to the memory location on the same thread. If a Cell is Sendable, we might have to implement the value getter using a volatile and/or atomic load to rule that out.
I believe that, in C, and presumably in LLVM as well, it is formally UB to atomically read a variable if another thread simultaneously non-atomically writes to it; for it to not UB, the write must be atomic as well. (I'm not sure if the same would apply to a volatile read.) Admittedly, I can't think of an optimization that would exploit this UB, but if one hypothetically existed, even an atomic (or volatile) read would be dangerous.[1] Personally, I'd find it uncomfortable if Swift emits LLVM IR that formally exhibits UB, just because there currently happens to not be an optimization exploiting that UB.
Perhaps such an optimization is more likely if cross-language LLVM LTO is used. ↩︎
Yes, on the AliasedSpan I asked about exactly what it means for load/store semantics and we would probably have to answer these questions to bring the Sendable conformance back:
If our threshold of acceptable slop is "you get some arbitrary but well-behaved value", I think we could get away with combining loads. It would be as if both loads observed the same value, but as long as those loaded values behave coherently afterward I think we're OK. But yeah, we definitely wouldn't be able to allow redundant reloads, since that would give us potentially different representations of the "same" value.
Thanks @Alejandro . The reason for asking about both _Cell and back deployment was in case it’s possible to rename _Cell and therefore make this deployable back to when _Cell was introduced (Swift 6.0)?
Well _Cell is not Sendable because it's just an implementation detail for Mutex which declares its own Sendable conformance, but if we decide that Cell and ConstCell should be Sendable where T: Sendable & FullyInhabited then yes it would be different.
I know this proposal is just introducing the library types, but I think it could be clearer about the wider thinking for imported types. For example, ConstCell is necessary because we want to make Cell support nonmutating mutation because we want the safe import of non-const C/C++ pointers and arrays to still use non-MutableRef and Span so that they aren’t burdened with non-copyability.
(You might think that, on a more basic level, we also don’t want the exclusivity restrictions of a mutating access. However, on that basic level, we also do not want the exclusivity restrictions of a nonmutating access. I am fairly convinced that we have to model cells as involving new basic kinds of aliased access. Once you accept that, it also gives us clearer directions for a lot of other related problems. I’ll talk through this in a separate reply.)
(The non-import uses of Cell also want this property of nonmutating mutation, but if the import uses didn’t, we would clearly just want to use different types to model them.)
On the access front, the current proposal is making a valiant attempt at a purely type-based modeling of aliases, but I think this effort would be more successful if we acknowledged that aliased and non-aliased accesses need to be distinguished in the language.
That is, we can currently classify accesses into various basic kinds: initialization, consumption/deinitialization, reading/borrowing, mutation, and assignment. Of these, the first two really must be non-aliased, at least for arbitrary types: there’s no way to allow the construction or destruction of an object to overlap with uses of it without allowing all sorts of initialization unsafety. And assignment is always “instantaneous”, in the sense that there isn’t generally any way to synchronously overlap the assignment: we produce the new value, then assign it in as an uninterrupted step. (Concurrent code can overlap the assignment, but this is inherently problematic: it’s a data race and has an undefined effect even if the language somehow limits it from causing any other damage.)
But with reads and modifications we can usefully talk about what might happen if we allow them to overlap. And while we don’t want that to infect the standard model of Swift programming, we can still allow people to opt in to it in specific places. For that, we need two things: the idea, above, of aliased read and modification accesses; and an idea of aliased storage.
The basic rule here is that something about the storage declaration determines how you access it:
Ordinary storage in Swift is not aliased, which means we will enforce exclusivity for accesses to it, even aliased accesses. If something asks for aliased read access to non-aliased storage, semantically Swift will perform a non-aliased read, and the client will just ignore the exclusivity guarantees we gave it.[1] This is still safe as long as we only do memory-safe things during the access, which should be true even for these aliased accesses.
Aliased storage would not enforce exclusivity and would only offer aliased accesses at an implementation level. Non-aliased accesses can be emulated for copyable types by copying into temporary, non-aliased storage and then accessing that. But of course this has semantic consequences: a non-aliased access (of either kind) will not observe changes in the original storage that happen after the read begins, and a non-aliased modification will assign the temporary back in its entirety and therefore will silently overwrite any overlapping changes to the original storage. We may want to limit these emulations or make the user acknowledge them somehow.
So, for example, we could have borrowing(aliased) parameters. In the caller, the access done to the argument when passing to such a parameter is naturally an aliased read. In the callee, the parameter is aliased storage. If you pass the parameter as the argument to another aliased parameter, the argument can be safely forwarded without introducing copies and therefore without changing its lifetime.
The implication for Cell is two-fold. The first is that the functions for creating aliased Refs and Spans — I can’t seem to find these in the proposal, but surely they must be coming — can take borrowing(aliased) or inout(aliased) parameters, weakening the requirements on the code that produces them. And the second is that, rather than forcing a copy on every access to the safe property value by forcing the access through get/set, we instead give valuealiasedborrow and mutate accessors, reflecting the weaker guarantees they offer. A copy is then required in the caller if it needs non-aliased access — but if it doesn’t, we can safely forward without one.
Finally, C and C++ functions taking borrowed or inout parameters would generally allow them to be aliased,[2] and things like fields of foreign reference types would generally be considered aliased as well.
All of this has a number of immediate beneficial consequences:
Safe accesses to cell values (i.e. not using unsafeBorrow) should not need a copy if an aliased access is okay. In particular, passing them to C++ methods will generally be doable without a copy. In particular particular, this means non-copyable values in aliased storage can be worked with totally safely unless you need to use something from Swift that requires exclusive access; if you’re just e.g. calling imported methods on an imported non-copyable type, you should be totally fine.
We have a language mechanism for projecting members of aliased storage, e.g. to get an aliased Ref to a member of a different aliased Ref. Otherwise we’d need to figure out some way to, I guess, project a key path.
We should significantly reduce the number of Cells we need in imported APIs, especially in foreign reference types. They should only be necessary when we’re bringing something in as a Ref or Span. This should reduce the clutter of the resulting code a lot.
We could also weaken the exclusivity checking to permitting overlapping aliased accesses. We still have to do exclusivity checking, however, to establish that these accesses don’t overlap non-aliased accesses that they would conflict with. For example, we can have simultaneous aliased and non-aliased reads, and we can have simultaneous aliased reads and modifications, but we cannot allow modifications of either kind to overlap with non-aliased reads, and we cannot allow any other access to overlap with non-aliased modifications.
The dynamic exclusivity runtime would have to be modified to implement these weakened rules, so developers on Apple platforms would have an OS compatibility issue to deal with. ↩︎
Regarding aliasing and mutation: this is an implementation aspect of Cell I’d like to bring up. We’re faced with two options:
A. Make all access to Cell, ConstCell, or any raw-layout type opaque to aliasing and mutation analysis throughout the compiler.
B. Assume any Swift variable is subject to aliasing and mutation at any time.
We should go with option A because the aliasing and mutation guarantees provided by Swift’s exclusivity rules are one of the most important memory optimization strategies that we have.
In practice, this means the value in a Cell can only be accessed through Builtin.addressOfRawLayout, that builtin must be preserved throughout compilation, and the optimizer can’t reason about the provenance of the address it gets from the builtin. Lowering to LLVM needs to preserve the builtin's guarantees because noalias can potentially be applied to raw-layout values--the implementation doesn’t handle this yet.
I think it’s worth mentioning this in the proposal because implementers need to understand and agree on a common strategy. It will be difficult to maintain without bugs creeping in because there’s a strong incentive for optimizations to work around any barriers we put up for correctness, and there's no way to generally verify safety in this respect. If the rules are written down in an authoritative place, we can refer people to it whenever touching related code.
ConstCell should not be Sendable, even with type constraints. Sending a ConstCell across threads is equivalent to passing an inout across threads from a correctness perspective. The concurrency model is about more than memory safety. It also protects against data corruption caused by race conditions. Those issues are incredibly difficult to reproduce and diagnose. Atomic is meant to be Sendable. It has all the benefits of Cell and comes with concurrency semantics.
Let's find an alternative to VolatileCell. It would be a shame to add the concept of volatile in the Swift language at the level of data types. Atomic should be used instead signal the intention to share values across threads, it has meaningful concurrency semantics, and lets you express the desired memory ordering.
Any true need for volatile behavior (who knows what the actual semantics are) is so niche that it makes more sense to call into volatile load/store primitives in those cases. It seems reasonable for those primitives to be in the standard library.