7 ms·
No Sane Compiler Would Optimize Atomics
- qb45 11y agoI love the WTF potential of this one: Another interesting optimization is to use potentially shared memory locations (on the stack, heap and globals) as scratch storage, if the compiler can prove that they are not accessed in other threads concurrently. [..] For example the following transformation could occur: // Some code, but no synchronization. *p = 1; // Can be on stack, heap or global. Becomes: // ... *p = RAX; // Spill temporary value. // ... RAX = *p; // Restore temporary value. // ... *p = 1; Since we write to p and there is no synchronization operations, other threads do not read/write p without exercising undefined behavior. We can therefore use it as scratch storage.
- mike_hock 11y agoI see no WTF here. Concurrent reads from * p have to be allowed to produce any possible value whatsoever, since if the variable is larger than a system word, the compiler may have to break up the write into multiple instructions (and some architectures don't even have instruction-level isolation). The language doesn't give you any stricter concurrency guarantees just because a variable is sizeof(void* ). And that's good. It makes the language cleaner. No bullshit special cases that require you to know implementation details. You want atomic behavior, use std::atomic (or a lock), period.
- xorblurb 11y agoThis prevents debugging, probably does not really optimize (except 1% in useless microbenchmarks), and increases compiler complexity and the risk of bugs in it. A similar thing has already happen in the past: compiler started to introduce speculative writes on code pathes where the write did not occur in the source code, and this broke multithreaded programs -- IIRC there was no threading model at that time (the current one forbid to do that kind of thing, and it is probably impossible to get a sound threading model that does not forbid that) but instead of saying that they were technically allowed to do that compiler authors recognized that because it made multithreaded programs impossible to write, that they had to fix their shit. In the C standard, crashing is not a specified behavior, so when an obscure architecture crashes on a specific construct, the standard has to technically say that this is undefined behavior. That compiler authors now interpret most of undefined behavior as a license to be stupid on ALL architecture instead using their brain and allowing non-portable programs to exist is not them being smart optimizing all our code-bases for free but just a proof they do not understand a part of what they are doing, including the standard they pretend to know so much. So the position of "all the standard, nothing but the standard" is utter bullshit. Another example: the current published atomic specification is subtly unsound. Do compiler authors do the right thing instead of trying to implement the broken spec, while waiting for the spec to be fixed? On that point yes -- at least for now. So when, on other subjects, they introduce new bugs in compiled code compared to what all previously known compilers produced - and without proving this really improves performance like crazy to justify taking that kind of risk - they are just being dangerous asses. -O2 has shown to be able to be fast enough without (reasonable) code breaking behavior in the past. In most case you wants to keep the same behavior as -O0 built programs. An optimisation that does not preserve behaviors reasonably expected by skilled programmers and requires that every programmer knows the standard by heart at any point of their work under penalty of crashes and security holes is not an optimisation but a piece of crap. This is blurry but the C / C++ standards are blurry and their usage even more so -- and more importantly they do not forbid to stay safe and sane to begin with -- so trying to do formal logic instead of doing engineering by thinking about the whole picture and history is completely stupid.
- mike_hock 11y agoIn this case, the programmer cannot reasonably expect that a concurrent read produces a meaningful result. There are good reasons to use clean, abstract formal logic over brutish "engineering" that results in a myriad of unmaintainable "if x, y, and z, do this, but not if also a or b, then we do that." That said, I agree that compilers should not needlessly optimize based on undefined behavior in cases where a sensible architecture-specific behavior can be defined (but turning a "read can produce any value" into a "read can produce high 32 bits from old write and low 32 bits from new write" isn't even worth specifying).
- amluto 11y ago> Another example: the current published atomic specification is subtly unsound. Can you explain or point us somewhere?
- gpderetta 11y ago'the current published atomic specification is subtly unsound' Are you talking about memory_order_consume? Although I believe that correctness proofs have so far excluded memory_order_consume, I do not think the current spec has been shown to be unsound; it is just generally regarded as hard to implement and hard to use correctly. In practice most compilers (conformingly) just lower it to memory_order_acquire; even Linus has been considering doing away with all the dependent load trickery in the kernel and just convert all of them to explicit load acquires. x86 and SPARC are TSO machines and have zero cost acquires, modern POWERs can also be run in a similar sane TSO mode and ARM64 has cheap explicit load acquire and store release (which I believe were added explicitly to allow simple mapping of the C and C++ memory model). Even Itanic has (I assume cheap) load acquire and store release operations. Other insane architectures that actually require dependant loads for performance can go and have anatomically impossible sex with themselves. edit: spelling
- jfbastien 11y agoThe latest on consume: http://wg21.link/p0190r0 http://wg21.link/p0190r0 The spec is pretty sound, but unimplemented because that's not how compilers work right now for the generality that consume wants. I think the above proposal is likely to work because it's more restrictive towards how consume works, but we need implementation experience. Linux uses consume extensively for RCU, and I don't think it'll go away soon because of the benefits it provides to Power and ARM. It works through a gentleperson's agreement between Linux's RCU implementation in C and GCC.
- qb45 11y ago> I see no WTF here. Not WTF in itself, but has WTF potential. The way I read it is: If you forget to put synchronization around accesses to global state, in addition to the usual problems, we will also overwrite your globals with random garbage. Which sounds great when you think about debugging affected code. > sizeof(void* ) You think that's equivalent to machine word? On 16-bit x86, for example, pointers can be either 16 or 32 bit ;)
- mike_hock 11y ago> You think that's equivalent to machine word? On 16-bit x86, for example, pointers can be either 16 or 32 bit ;) The 16bit x86 memory model is absolutely atrocious. And IDGAF what you call a "machine word" on x86, it's a 64 bit architecture grafted onto a historically grown 32 bit architecture grafted onto a historically grown 16 bit architecture grafted onto an 8 bit architecture. And when Intel tried to do a clean reboot for the 64-bit part, the market chose the grafted-on abomination instead. Also, you can have 128 bit atomic loads if you use an xmm register. But that's for the compiler to figure out when you declare an std::atomic of a 16-byte large type, not for you to assume on a plain variable, which was my point.
- raverbashing 11y ago(If it's not clear you need to navigate the presentation with both left/right and down keys) You know what? This is getting pathological. It seems keywords are added to ensure serialization/atomicity, then compilers find a way to optimize it away/make it useless, then another thing "now it's the right way" gets added. Rinse, repeat And most things that are added have some quirks and people trying to play it smart
- the8472 11y agoWe already have volatile for "yes, please do this as two separate increments, don't coalesce them". And atomics give you ordering guarantees and mappings to atomic instructions. And fence methods give you full fences where needed. So we already have all those things you might need, they just got decomposed into different aspects that can be applied separately.
- masklinn 11y agoIf you use <space> it'll go to the next slide, whether down or to the side.
- sjolsen 11y ago>It seems keywords are added to ensure serialization/atomicity, then compilers find a way to optimize it away/make it useless No, what happens is keywords (or more broadly, semantics) are added which provide certain guarantees given certain preconditions. Then, developers write code that depends on those guarantees and fails to meet the preconditions, but happens to work anyway. At some point, the implementation adds some optimization which -- while still providing the appropriate guarantees when the preconditions are met -- breaks that code. I know C and C++ can be a royal pain in the ass sometimes, but I have little sympathy for developers who flip the "I know what I'm doing, please assume my code is right and make it run fast" switch, then get upset with the compiler when it turns out that their code actually _isn't_ right and the compiler performs an optimization they weren't expecting. If you don't want the compiler to reorder your memory access, then don't fuck around with the memory_order flags.
- raverbashing 11y ago
- alblue 11y agoThe presentation is unreadable as it jumps all over the place and requires you to press keys to navigate.
- kingosticks 11y agoWhat sane person would write a presentation like this. Horrible to try and read, particularly on a phone. Whatever his message was is lost on me.
- masklinn 11y agoIt's reveal.js, a pox on the already poxy world of presentations.
- raverbashing 11y agoPress space (as per some other comment there), but I agree
- nikanj 11y agoSomeone forgot to install a space bar to my iphone
- raverbashing 11y ago:) You can probably get around swiping down then right
- paulddraper 11y ago
- yosefk 11y agoTorvalds on memory ordering and clever compilers (http://yarchive.net/comp/linux/memory_barriers.html http://yarchive.net/comp/linux/memory_barriers.html): "Compiler people tend to think that people want smart compilers. I as a compiler power-user can emphatically tell you that that isn't true. People want _reliable_ compilers, and they want compilers that can be told to just get the hell out of the way when needed." I guess I'm largely at the same side of the argument, on the theory that atomics etc. should be used very sparingly by people who know what they're doing and spend time to hand-optimize their code and think it through semantically (and these people aren't helped by the semantics of atomics becoming more complicated for the sake of optimization opportunities), as opposed to other language mechanisms which are used all over the place by everyone, and so with these mechanisms you do gain a lot from clever optimizations. But I understand the other side of this argument...
- sgift 11y agoTorvalds' argument makes for a nice quote but reality tells a different story: If people just wanted a reliable compiler they would use the oldest compiler they could find which is able to compile their code. If it is old all bugs, problems and faults are known. You know how to work around them and nothing can surprise you anymore. The performance may not be great, it may be a bit of a pain to do some things, but you know it all, so it is not great, but reliable. What people really want is something which is reliable and smart and fast and .. people love smart compilers/programs/whatever, only when those programs fail them they cry "why did you try in the first place? No one wants this" There's a thin line between smart and crazy. Trying to be always on the right side is one of the hardest parts of software design.
- Animats 11y agoC/C++ language designers used to take the position that concurrency was an OS issue, not a language issue. That came unglued as processors did more and more out of order operations. C/C++ language doesn't really understand concurrency. Even locking is iffy, because the language doesn't know what data is locked by a lock. Rust, and to a lesser extent Java, associate locks with data, which gives the compiler some info to guide what it can't optimize. C/C++ have a whole series of kludges - "volatile", "atomic", etc., which patch holes in race conditions but don't provide a coherent concurrency model. Which is why we see papers like this.
- haberman 11y ago> C/C++ have a whole series of kludges - "volatile", "atomic", etc., which patch holes in race conditions but don't provide a coherent concurrency model. Are you aware that C11/C++11 -- the language standards that added atomics -- also defined a coherent memory model?
- caseymarquis 11y agoVolatile is required for working with memory which is subject to change without notice. This is required for signaling that the value may be changed by a physical peice of hardware outside of your control. It's pretty useful.
- pjmlp 11y agoNowadays I look at UNIX (or POSIX) as the actual C runtime. But even that doesn't change much, as the semantics are not consistent across implementations.
- gpderetta 11y ago"C/C++ language designers used to take the position that concurrency was an OS issue, not a language issue" That was the case until Bohem's 'Threads Cannot be Implemented as a Library' position paper, which lead the C++ Standard Committee to formally define the C++ memory model (whose correctness was also independently proven [1]). This standardization work was greatly inspired by the corresponding Java MM, and in a break from the past, it explicitly disallowed a few compiler optimizations and even made the C++ standard unimplementable on a few quirky older architectures for the sake of sanity. Today there are no arguments on whether a concurrent algorithm (and compiler optimizations) is conforming or not, as there are both dynamic and static checkers that can verify it against the C++ memory model. I wish that the same rigorous formalization work were to be done for the strict aliasing rules of the language, which are still a mess of underspecified, ambiguous and mutually contradicting rules scattered all over the standard document. [1] the original drafts had in fact issues which were found when attempting the correctness proof; these flaws have been corrected, except there is a still standing issue with out-of-thin-air values; it is not believed to have any impact on code with current compiler and cpus, but there is an ongoing effort to correct it.
- kolapuriya 11y agoI haven't dealt with concurrency in C++ yet, but I definitely agree with the "don't write assembly, file bugs for the compiler instead" methodology. And not because I don't like ASM -- some of my favourite code consists of ASM hacks -- but because better compilers help everyone.