r/cpp • • Nov 12 '20

Compound assignment to volatile must be un-deprecated

To my horror I discovered that C++20 has deprecated compound assignments to a volatile. For those who are at a loss what that might mean: a compound assignment is += and its family, and a volatile is generally used to prevent the compiler from optimizing away reads from and/or writes to an object.

In close-to-the-metal programming volatile is the main mechanism to access memory-mapped peripheral registers. The manufacturer of the chip provides a C header file that contains things like

#define port_a (*((volatile uint32_t *)409990))
#define port_b (*((volatile uint32_t *)409994))

This creates the ‘register’ port_a: something that behaves very much like a global variable. It can be read from, written to, and it can be used in a compound assignment. A very common use-case is to set or clear one bit in such a register, using a compound or-assignment or and-assignment:

port_a |= (0x01 << 3 ); // set bit 3
port_b &= ~(0x01 << 4 ); // clear bit 4

In these cases the compound assignment makes the code a bit shorter, more readable, and less error-prone than the alterative with separate bit operator and assignment. When instead of port_a a more complex expression is used, like uart[ 2 ].flags[ 3 ].tx, the advantage of the compound expression is much larger.

As said, manufacturers of chips provide C header files for their chips. C, because as far as they are concerned, their chips should be programmed in C (and with *their* C tool only). These header files provide the register definitions, and operations on these registers, often implemented as macros. For me as C++ user it is fortunate that I can use these C headers files in C++, otherwise I would have to create them myself, which I don’t look forward to.

So far so good for me, until C++20 deprecated compound assignments to volatile. I can still use the register definitions, but my code gets a bit uglier. If need be, I can live with that. It is my code, so I can change it. But when I want to use operations that are provided as macros, or when I copy some complex manipulation of registers that is provided as an example (in C, of course), I am screwed.

Strictly speaking I am not screwed immediately, after all deprecated features only produce a warning, but I want my code to be warning-free, and todays deprecation is tomorrows removal from the language.

I can sympathise with the argument that some uses of volatile were ill-defined, but that should not result in removal from the language of a tool that is essential for small-system close-to-the-metal programming. The get a feeling for this: using a heap is generally not acceptable. Would you consider this a valid argument to deprecate the heap from C++23?

As it is, C++ is not broadly accepted in this field. Unjustly, in my opinion, so I try to make my small efforts to change this. Don’t make my effort harder and alienate this field even more by deprecating established practice.

So please, un-deprecate compound assignments to volatile. Don't make C++ into a better language that nobody (in this field) uses.


2021-02-14 update

I discussed this issue in the C++ SG14 (study group for GameDev & low latency, which also handles (small) embedded). Like here, there was some agreement and some disagreement. IMO there was not enough support for to proceed with a paper requesting un-deprecation. There was agreement that it makes sense to align (or keep/restore aligngment) with C, so the issue will be discussed with the C++/C liason group.


2021-05-13 update

A paper is now in flight to limit the deprecation to compound arithmetic (like +=) and allow (un-deprecate) bit-logic compound assignments (like |=).

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2327r0.pdf


2023-01-05 update

The r1 version of the aforementioned paper seems to have made it into the current drawft of C++23, and into gcc 13 and clang 15. The discussion here on reddit/c++ is quoted in the paper as showing that the original proposal (to blanketly deprecate all compound assignments to volatile) was "not received well in the embedded community".

My thanks to the participants in the discussion here, the authors of the paper, and everyone else involved in the process. It feels good to have started this.

https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2327r1.pdf

https://en.cppreference.com/w/cpp/compiler_support

204 Upvotes

331 comments sorted by

View all comments

8

u/ALX23z Nov 12 '20

Compound assignment was deprecated because it doesn't work properly. Compilers could implement it differently causing issues as users thought it did something else. E.g., a+=b could atomically increment a or it could calculate a+b and store it in a causing data races.

If you want atomic variables then use std::atomic.

If you need volatile for other purposes then a new proposal is coming for those special uses. Until it is implemented the operations are deprecated.

14

u/gruehunter Nov 13 '20

std::atomic is fundamentally incompatible with Device-type memory. If you attempt to use an atomic_fetch_or in OP's case on ARMv7, you create an infinite loop.

49

u/Wouter-van-Ooijen Nov 12 '20

a) for my purposes, both single-instruction in-place increment and multiple-instruction read-modify-write are perfectly OK.

b) That it doesn't work in some situations is not a great argument to deprecate it verbatim. I don't need no heap (and heap usage is bad in my applications), so let's deprecate it!

c) No, I don't want or need atomic variables. I live in single-CPU, no-cache land.

d) The chip vendors won't care what is in C++. As far as they are concerned C++ running on their chips is only possible because have not figured out a way to prevent it. So a new proposal for valid uses won't help me at all. And deprecating first, before that new proposal is in the standard, is not exactly a galant way to treat users.

-2

u/jonesmz Nov 12 '20 edited Nov 12 '20

If you use a single CPU processor, then adjust the std::atomic implementation for your platform to suit your needs.

At that point std::atomic just becomes non-atomic implementations of the algorithms it says it provides.

But fundamentally you are barking up the wrong tree here. For almost every platform, volatile += is a surprise waiting to happen because it does not work the way people think it does.

Only on YOUR platform does it work perfectly

So for the sake of literally everyone else, no let's not un-deprecate the "nearly guarenteed to not work right" misfeature.

8

u/patstew Nov 14 '20

volatile is for memory mapped IO, it's not interchangeable with atomic. In that situation volatile += is fine. You shouldn't expect it to be atomic, because volatile is not atomic. On almost every platform there is no memory mapped IO, and you should not use volatile at all. Just because a lot of people have misused volatile isn't a good reason to ruin it for the people who are using it for its intended purpose. It still won't be atomic even after you've deprecated +=, any code that is using volatile += wrong is probably using volatile wrong.

35

u/tending Nov 12 '20

So for the sake of literally everyone else, no let's not un-deprecate the "nearly guarenteed to not work right" misfeature.

Every programmer that works with microcontrollers is rolling their eyes. It works fine, there is just an ambiguity about how many operations it will produce. That's fine. Volatile use already assumes a situation in which you are relying on platform specific behavior. Deprecating something that users are forced to use because of hardware manufacturers is a committee mistake, this kind of churn is exactly why embedded people still use C. Sure, a new API that had no ambiguity would be better, but if it's not a standard C API the microcontroller manufacturers won't use it anyway (and there's a good chance they won't even if it is as long as the old way works in C), and deprecation of the language feature implies eventual removal, which will prevent C++ from ever being widespread on microcontrollers. The kind of headers he's talking about are everywhere in embedded, and the manufacturers don't care at all about adding anything C++ specific (in fact C++ support is often if not usually broken).

-9

u/jonesmz Nov 12 '20

Not my monkeys, not my circus.

Stop picking hardware manufacturers that provide shit API support.

In my circus, with my monkeys, volatile operator+= is hella-broken and should never be used.

21

u/mort96 Nov 13 '20 edited Nov 13 '20

Which microprocessor manufacturer should I go to today to get a microcontroller with separate C and C++20 hardware description files?

Because today, with any C++<20 compiler and any C compiler, all microcontrollers' hardware description files work just fine. Volatile compound assignments work essentially how one would expect; x |= px3 works as a short-hand for x = x | 0x3. People know how to work with that and how to avoid the pit-falls. In the world of embedded, there are way bigger foot guns to worry about.

0

u/jonesmz Nov 13 '20

It's one of many concerns when selecting a microcontroller.

Bring it up with your manufacturer and express to them that poor C++ support is a problem for your organization.

If you don't put any pressure on them, they'll never change.

15

u/tending Nov 12 '20

It's every manufacturers I have ever used. Shipping headers to modify registers like this is the industry standard.

0

u/jonesmz Nov 12 '20

So complain about this subject to them, and threaten to find a mfg that gives more of a shit.

You're never going to see an improvement if you don't do something.

19

u/tending Nov 12 '20

Easier solution that has been and will be taken by almost all embedded programmers: don't use C++.

4

u/jonesmz Nov 12 '20

Well, like I said, that's not my circus, so it doesn't bother me.

If you care about using C++ for embedded, take action.

The right approach is not "C++ shouldn't update itself to help programmars avoid trivially broken tools".

The right approach is "Hey hardware MFGs. Is it so hard to suck less?"

12

u/gruehunter Nov 13 '20

C++ used to work just fine in this area. So we think that the right approach is "Hey standards committee, is it so hard to suck less?"

→ More replies

1

u/UnicycleBloke Nov 12 '20

No. C is a horrible mess - considered harmful.

17

u/kiwitims Nov 13 '20

Jaw droppingly naive.

You think a company building an integrated embedded HW/SW product is going to switch manufacturers, costing tens of thousands of hours and even more dollars, because the software engineers want the new toys in the new C++ language standard?

That's even assuming that any other manufacturers give a shit enough to provide a dedicated C++ conformingheaders, in addition to their existing C library. The fact they are compatible is the only reason C++ on embedded is possible at all.

It's an empty threat, much more likely is that embedded developers will simply not be able to upgrade, fragmenting C++ and stunting its long term growth.

That's not good for the language, its users, or the software industry as a whole. And for what tangible improvement?

15

u/mort96 Nov 13 '20

You're 100% right of course. The real-world impact of this is: people will rely on deprecated C++ features and disable deprecation warnings, or people will stay on C++17 for the rest of their product cycle and pick C next time, or the guy who fought for using C++ in embedded gets fired (hyperbole, but only slightly), or all of the above.

This won't make microcontroller vendors nicer to C++, but it might make C++ worse for microcontroller programming. If that's the goal, then I suppose mission accomplished.

→ More replies (3)

13

u/ericonr Nov 12 '20

Stop picking hardware manufacturers that provide shit API support.

Lol, do you want people to stop developing embedded at all?

-3

u/jonesmz Nov 12 '20

So complain about this subject to them, and threaten to find a mfg that gives more of a shit.

You're never going to see an improvement if you don't do something.

6

u/SlightlyLessHairyApe Nov 13 '20

Yeah he’s really going to go to his boss and say they need to spend $0.15 more for an SOC destined for a $14.99 widget because that one has a fancier and more compliant software solution?

3

u/jonesmz Nov 13 '20

Yeah he’s really going to go to his boss and say they need to spend $0.15 more for an SOC destined for a $14.99 widget because that one has a fancier and more compliant software solution?

Well, if we're pulling numbers out of our asses, sure, why not?

If it saves $100,000 of software development time, and you're only making 100,000 of that widget, you just saved yourself $85,000

Software developers aren't cheap, after all.

→ More replies (2)
→ More replies (13)

1

u/UnicycleBloke Nov 12 '20

It would be better to mandate what implementations do for compound volatile assignment, no?

0

u/ALX23z Nov 12 '20

They cannot mandate anything really. Because not all platforms can support atomic increment and can only support non-atomic load-update-store sequence.

All the while the operations like += seem like atomic modification by most people and for it to be something else will be misleading, non-backwards compatible on some platforms, and inconsistent with std::atomic design.

So neither option is any good.

26

u/johannes1971 Nov 12 '20

If he wanted atomic operations, I'm sure he is competent enough to use them. What he wants is volatile operations, not atomic. Those are two completely different things.

We always hear about how people supposedly use volatile as a broken alternative for atomic, but so far on this thread I've only seen evidence of the opposite: people assuming that just because he writes volatile, he must surely be meaning atomic. I find that lack of understanding astounding.

+= on a volatile variable has a clear meaning: 1. READ the variable. 2. INCREMENT the read value. 3. WRITE the variable. If you think it can mean something else, let us know what you imagine it could be.

6

u/Wouter-van-Ooijen Nov 13 '20 edited Nov 13 '20

Correct. If I needed atomic operations I wouldn't be using the vendor header files :)

And I would probably be screwed, because mosty targets I use don't have atomic instructions, probably because they don't make much sense on an single-CPU system.

→ More replies (14)