Who's afraid of a big bad optimizing compiler?
Who's afraid of a big bad optimizing compiler?
Posted Jul 17, 2019 20:14 UTC (Wed) by law@redhat.com (guest, #31677)Parent article: Who's afraid of a big bad optimizing compiler?
Twiddling optimization flags, or worse yet, writing source that is specific to a set of transformations the compiler does or does not do today is a losing proposition. Such techniques may work today, they may work tomorrow, but they're really a case of writing code to a particular compiler implementation and could well fail in the future as compilers continue to evolve and more aggressively optimize.
The right thing, particularly for user space code, is to learn the C11/C++11 memory model and use those facilities. Do this and you're light years ahead on making code that works today, tomorrow and into the future regardless of what the compiler developers do.
I realize the kernel is special and kernel developers are going to do their own thing, but what they do should not be held up as an example of "the right way" in the general case.
Posted Jul 18, 2019 4:31 UTC (Thu)
by wtarreau (subscriber, #51152)
[Link] (3 responses)
Posted Jul 18, 2019 19:33 UTC (Thu)
by k8to (guest, #15413)
[Link]
The point is still a concern, of course.
Posted Jul 18, 2019 21:19 UTC (Thu)
by law@redhat.com (guest, #31677)
[Link]
Posted Jul 18, 2019 21:38 UTC (Thu)
by pbonzini (subscriber, #60935)
[Link]
Posted Jul 26, 2019 10:53 UTC (Fri)
by ksandstr (guest, #60862)
[Link]
I'd like to point out that it's not sufficient to tag values subject to concurrent write, but rather all consumers of those values should be marked by use of explicitly atomic primitives. Such as those in stdatomic.h, which will presumably stay consistent[0] despite e.g. future ultra-LTO exposing damage previously hidden by an intermodule boundary. This is because the semantics of concurrently-written data are generally different from those of data in a single-threaded or "classic C" program where it isn't intended that changes to shared state are visible outside of the current thread or the reverse, so it's necessary to indicate non-classic usage to the reader at point of interaction and declaration both.
>The right thing, particularly for user space code, is to learn the C11/C++11 memory model and use those facilities.
The C11 memory model can also be studied as first the "classic C" of single-threaded programs, where the compiler may yield an unrecognizably bizarre but correct result for reasons in the article; and by then laying over a set of diffs for "concurrently interacting C", which is just as the old but allows source to indicate that certain parts expect to publish or consume and should appear accordingly from out-of-thread[1]. Most importantly the latter allows execution of the former unmodified when it's known that concurrent interactions won't occur, such as between callsites of POSIX-style mutexes where synchronization is hidden behind a function call boundary, or indicated to a deeply LTO compiler by syscall or atomic primitive.
This is in contrast to ideas about forcing correct concurrent interactions through manipulation of the compiler output (sans memory-clobbering asm volatile), which seem like a sediment of bubblegum fixes compared to C11 atomics, or to even the C99 idea of the standard language as semantics that constrain the compiler over a naïve 1:1-ish mapping between statement and machine code.
[0] unimplementable ordering semantics aside; how come nobody smelled the cr^Wsufficiently smart compiler from a mile away? (and how come I used a born-defunct option in real code, thinking it'd one day do something besides introduce subtle breakage?)
Posted Jul 26, 2019 18:11 UTC (Fri)
by andresfreund (subscriber, #69562)
[Link] (1 responses)
Posted Aug 15, 2019 19:15 UTC (Thu)
by PaulMcKenney (✭ supporter ✭, #9624)
[Link]
Who's afraid of a big bad optimizing compiler?
Who's afraid of a big bad optimizing compiler?
Who's afraid of a big bad optimizing compiler?
Who's afraid of a big bad optimizing compiler?
Who's afraid of a big bad optimizing compiler?
[1] e.g. in a shared memory segment
Who's afraid of a big bad optimizing compiler?
Who's afraid of a big bad optimizing compiler?