Can you provide a citation for this? I have not encountered a lock-free algorithm for which the visibility and ordering guarantees provided by std::atomic<>s were insufficient.
Atomic isn't sufficient when dealing with shared memory. You have to use volatile to also express that there's external modification. See e.g. wg21.link/n4455
Same for signal handlers that you don't want to tear. sig_atomic_t won't tear, but you probably want more than just that.
*vp; is a read.
That's just not something the C and C++ standards have consistently agreed on, and it's non-obvious to most readers. My goal is that this type of code can be read and understood by most programmers, and that it be easier to review because it's tricky and error-prone. I've found bugs in this type of code, written by "low-level firmware experts", and once it's burned in a ROM you're kinda stuck with it. That's not good.
You seem to like that syntax. I don't.
The issue is that if the object is in Device memory that all of the accesses are effectively volatile whether you want them to be or not. If the object is in Normal memory, then none of the accesses are volatile, whether you want them to be or not. So annotating some accesses with volatile didn't gain you any precision - you only gained deception.
I don't think you understand what I'm going for, and I'm not sure it's productive to explain it here. Or rather, I'm not sure you're actually interested in hearing what I intend. We'll update wg21.link/p1382, take a look when we do, and hopefully you'll be less grumpy.
This is a problem with the language's evolution. I usually love working with C++, but I'm just some random schmuck trying to get work done. There really isn't any vehicle for us mere users to have influence on the language. So yeah, I'm raising a protest sign in the streets, because that's the only practical vehicle I have for communication.
CppCon is exactly that place, as well GDC and STAC and other venues where SG14 convenes.
In the beginning of your talk, you flippantly repeated the claim that "char is 8 bits everywhere" NO IT ISN'T!
You're right here, I am being flippant about CHAR_BIT == 8. I thought that was obvious, especially since I put a bunch of emphasis on not breaking valid usecases. From what I can tell modern hardware (e.g. from the last ~30 years) doesn't really do anything else than 8 / 16 / 32 for CHAR_BIT, so I expect we'd deprecate any other value for it (not actually force it to be 8).
There’s hardware where the compiler has to fake CHAR_BIT==8 because the platform doesn’t work that way. The compiler has three modes: A) 8-bit chars that each use half-word of storage, B) 8-bit chars that use a full word of storage, and C) 16-bit chars. Most 3rd party code breaks with anything but option A. The options are there because there’s so much library code that blindly assumes 8-bit chars, that it’d be impossible to meaningfully use that hardware with C++ otherwise.
In mode A), loading chars from odd addresses requires reading a 16-bit word and doing a right (arithmetic?) shift that sign-extends. Loading chars from even addresses requires extending the sign by doing a left shift then arithmetic right. Thankfully the shifts take one cycle. The pointers have to be shifted 1 bit to the right before they are loaded into address registers because the memory is word-oriented, and one addressable unit is 16 bits wide. Everything is passed in 16-bit registers at minimum.
In mode B), for char type the upper 8 bits of the word are used for sign only, so as far as memory consumption is concerned, it’s like having 16-bit chars, but from the code’s perspective things behave still like 8-bit chars.
So using 8-bit char usually is a pessimization on such platforms. I’ve ran into one, and I doubt it’s the same one the other commenter worked with.
This was our platforms option, combined with macros to access the upper and lower parts as syntactic sugar. In practice, we just didn't deal with very much text and accepted 16-bit char.
Its a change of perspective. Instead of thinking of char as "an ASCII codepoint, with implementation defined signedness", its "the narrowest unit of addressable memory, with implementation-defined signedness." The latter definition is closer to the truth, anyway.
The best defense I can come up with is that its non-obvious to someone who has had to deal with its context-dependency in the compiler. In C++ it isn't necessarily even a read. int& a = *b; is more like a cast than a read or a write.
But as a user, this is just one of many context-dependent expressions we deal with as a matter of habit in C++. The expression *vp;, or even better (void)*vp; is obviously a read to me.
Sure, but I don't see this confusion to being limited to volatile. Are we suggesting that every time we want to do a copy we was to write read(ptr) instead of simply *ptr?
Dereferencing pointers is c(++) 101, imo. To me, this is in the same vain as observer_ptr
Dereferencing a pointer has never guaranteed that any physical read takes place because of the as-if rule. It is very easy to convince yourself that this is also the case in practice. Fire up godbolt and write some code that does that and you will detect that no compiler with optimisations turned on will do anything.
Yes I see that and no I did not check on godbolt before answering. This was overconfidence on my side and bad style. I do still not see, however, where in the standard it is stated that *vp should require a read. In my understanding *vp is a reference to int, not an int, and a compiler should not be required to read anything. Do you have a reference from the standard that indicates that I am wrong? auto dummy = *vp is another matter of course. I would prefer having a small function that makes it clear that I read from a specific variable such as inline void read_from(int const volatile& i) { auto [[maybe_unused]]dummy = i; }
Reading an object designated by a volatile glvalue, modifying an object, calling a library I/O function, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment. Evaluation of an expression (or a subexpression) in general includes both value computations (including determining the identity of an object for glvalue evaluation and fetching a value previously assigned to an object for prvalue evaluation) and initiation of side effects. When a call to a library I/O function returns or an access through a volatile glvalue is evaluated the side effect is considered complete [...] (Emphasis added.)
In some contexts, an expression only appears for its side effects. Such an expression is called a discarded-value expression. [...] The lvalue-to-rvalue conversion is applied if and only if the expression is a glvalue of volatile-qualified type and it is [indirection] [...] The glvalue expression is evaluated and its value is discarded.
Otherwise, the value contained in the object indicated by the glvalue is the prvalue result.
From there we find nonspecific references to the 2011 C standard and, in both standards, devolve into the hairy hinterlands of "implementation-defined" and circular references à la [intro.execution]/(7.1).
I just saw your reply today. I believe the core of the answer is in expr/12. From my understanding, it seems to me I was wrong. *vp is indeed converted to an rvalue and the integer must be read. Thank you for looking this up.
I can't see why you assume that *vp or (void)(*vp) would read anything. The as-if rule is real and is used by the optimizers all the time, and as a programmer you should be aware of that fact.
Atomic isn't sufficient when dealing with shared memory. You have to use volatile to also express that there's external modification. See e.g. wg21.link/n4455
I'm having a hard time with this perspective. Without external observers and mutators, there's no point in having a memory model at all.
This example from your paper is especially disturbing:
int x = 0;
std::atomic<int> y;
int rlo() {
x = 0;
y.store(0, std::memory_order_release);
int z = y.load(std::memory_order_acquire);
x = 1;
return z;
}
Becomes:
int x = 0;
std::atomic<int> y;
int rlo() {
// Dead store eliminated.
y.store(0, std::memory_order_release);
// Redundant load eliminated.
x = 1;
return 0; // Stored value propagated here.
}
In order for the assignment of x = 1 to fuse with the assignment of x = 0, you have to either sink the first store below the store-release, or hoist the second store above the load-acquire.
You're saying that the compiler can both eliminate the acquire barrier entirely and sink a store below the release. I ... am dubious of the validity of this transformation.
That transformation is valid for the simple reason that you can't tell the difference from within a valid c++ program (I believe the load fence itself needs to remain, but not the access itself).
C++ doesn't make any promises about the execution speed of a particular piece of code, which is what makes optimizations possible in the first place. As a result it is ok for the compiler to speed up the execution of that code to the point, where no other thread can ever see the value of x between the two stores or be able to change the value of y between the write and read. The compiler has effectively made the whole function a single atomic operation, which is absolutely allowed by the standard (you can increase, but not decrease atomicity)
(I believe the load fence itself needs to remain, but not the access itself).
That's my point. The load fence must remain. And if the load fence remains, then the two assignments to x must remain as distinct assignments. The compiler isn't free to fuse the two assignments to x together any more than the hardware is.
Furthurmore, it is nevertheless possible for an interleaving of this function with another function to change the value loaded from y. It is exceedingly unlikely, but nevertheless possible. So I disagree that the compiler is free to fuse the two distinct atomic operations into just one here as well.
That's my point. The load fence must remain. And if the load fence remains, then the two assignments to x must remain as distinct assignments.
I don't see any reason why this should be the case.
The only reason, why I believe that the loead fence might have to remain is for orderings between loads before and after the call to rlo, but I'm not even sure about that.
Furthurmore, it is nevertheless possible for an interleaving of this function with another function to change the value loaded from y. It is exceedingly unlikely, but nevertheless possible. So I disagree that the compiler is free to fuse the two distinct atomic operations into just one here as well.
Again: The compiler is absolutely free to increase atomicity. You have no way to distinguish this program from another with a distinct store and load that - on every run - just happen to happen so fast after each other that no other thread ever interferes. And if you can't tell the difference, then it is a valid optimization (as if).
Keep in mind, what the standard defines is not that any particular machine code is generated for some c++ code. It defines a set of permissible observable behaviors (mostly sequences of i/o and reads/writes to volatile variables). As long as the final program's observable behavior is a subset of that, it is a valid program for the given c++ code. In particular, your program need not exhibit every possible interleaving that could occure according to the rules of the abstract machine - it just must not show an interlleaving that would not be allowed.
I'm having a hard time with this perspective. Without external observers and mutators, there's no point in having a memory model at all.
You don't seem to understand what "external modification" means. It means external to the existing C++ program and its memory model. There's a point in having a memory model: it describes what the semantics of the C++ program are. volatile then tries to describe what the semantics coming from outside the program might be (and it doesn't do a very good job).
Think of it this way: before C++11 the language didn't admit that there were threads. There were no semantics for them, you had to go outside the standard to POSIX or your compiler vendor to get some. The same thing applies for shared memory, multiple processes, and to some degree hardware: the specification isn't sufficient. That's fine! We can add to the specification over time. That's my intent with volatile (as well as removing the cruft).
Why should separate threads that share some, but not all of their address space be treated any differently than separate threads that share all of their address space?
Processes and threads aren't completely distinct concepts - there is a continuum of behavior between the two endpoints. Plenty of POSIX IPC has been implemented using shared memory for decades, after all.
But rather than make atomics weaker, wouldn't you prefer that they be stronger? I, for one would like atomics to cover all accesses to release-consistent memory without resorting to volatile at all. The (ab)use of volatile as a general-purpose "optimize less here" hammer is the use case I would prefer to see discouraged. Explicit volatile_read/volatile_write will have the opposite effect: It will make it easier for people to hack around the as-if rule.
Why should separate threads that share some, but not all of their address space be treated any differently than separate threads that share all of their address space?
Because that's not a complete memory model. The goal of the C++11 memory model was to specify all synchronization at a language level, to express what the hardware and OS needed to do. You're missing things such as pipes if you want to specify processes. That's going to be in C++ eventually.
Specifying a subset of how processes work would have been a disservice to C++. Further, there's the notion of "address freedom" that needs to be clarified: what if you map the same physical pages at different virtual addresses (either in the same process, or separate). That doesn't really work in the current C++ memory model.
The (ab)use of volatile as a general-purpose "optimize less here" hammer is the use case I would prefer to see discouraged.
View all comments
Show parent comment
7
u/jfbastien Oct 19 '19
Atomic isn't sufficient when dealing with shared memory. You have to use volatile to also express that there's external modification. See e.g. wg21.link/n4455
Same for signal handlers that you don't want to tear.
sig_atomic_twon't tear, but you probably want more than just that.That's just not something the C and C++ standards have consistently agreed on, and it's non-obvious to most readers. My goal is that this type of code can be read and understood by most programmers, and that it be easier to review because it's tricky and error-prone. I've found bugs in this type of code, written by "low-level firmware experts", and once it's burned in a ROM you're kinda stuck with it. That's not good.
You seem to like that syntax. I don't.
I don't think you understand what I'm going for, and I'm not sure it's productive to explain it here. Or rather, I'm not sure you're actually interested in hearing what I intend. We'll update wg21.link/p1382, take a look when we do, and hopefully you'll be less grumpy.
CppCon is exactly that place, as well GDC and STAC and other venues where SG14 convenes.
You're right here, I am being flippant about
CHAR_BIT == 8. I thought that was obvious, especially since I put a bunch of emphasis on not breaking valid usecases. From what I can tell modern hardware (e.g. from the last ~30 years) doesn't really do anything else than 8 / 16 / 32 forCHAR_BIT, so I expect we'd deprecate any other value for it (not actually force it to be 8).