News for hackers. Stuff that matters.
Re:What Every C Programmer Should Know About Undefined Behav... by mpweiher on Thursday January 05, 2017 @01:29AM (#13325192)
"You're holding it wrong", compiler-writer's version.

IIRC, the Linux kernel has a rule that as a kernel developer, you are not allowed to break userspace, even if you think userspace is wrong. This is an apologia for why compiler writers think they should not have to be held to similar standards.

The argument is always something along the lines that UB in the C standard means they get to do whatever they want, including such nonsense as eliminating entire loops because the last access is out of bounds or going down an if-else-branch other than what the if says.

That this argumentation is specious in the extreme is shown by the fact that C compilers worked fine (and with less code breakages) before there even was a C standard, so all behavior was "UB".

The unstated assumption is that the optimizations enabled by this are absolutely necessary. That overstates the importance of compiler optimizations in general, and certainly the importance of these particular optimizations. (see Proebsting's Law)[2].

There may be code-bases where these extra optimizations are beneficial or even essential, fine, let's add a -Ogoforit or -Oletsgocrazy flag.

Rebuttal here: What every compiler writer should know about undefined behavior or "Optimization" based on undefined behavior hurts performance[1]

Short summary is that this trend towards exploiting UB breaks a lot of code that has been working for decades, and the fixes required to bring them in-line with the new regime imposed in order to enable these new optimizations makes it slower than the old code without those optimization. Yay.

[1] http://www.complang.tuwien.ac.at/kps2015/proceedings/KPS_201...

[2] http://proebsting.cs.arizona.edu 'I may be best known for "Proebsting's Law", which asserts that compiler optimizations have yielded annual performance gains an order of magnitude worse than hardware performance gains. The law probably would have gone unnoticed had it not been for the protests by those receiving funds to do compiler optimization research.'

Re:What Every C Programmer Should Know About Undefined Behav... by haberman on Thursday January 05, 2017 @02:18AM (#13325368)
Others have gone down this path and realized it's not as easy as it sounds: http://blog.regehr.org/archives/1287
Re:What Every C Programmer Should Know About Undefined Behav... by mpweiher on Thursday January 05, 2017 @04:11AM (#13325814)
Yes, but it depends a lot what you consider "down this path". John's approach seemed to be to define the UB, and to me it looks like he found out why it was left as UB in the spec.

Not exploiting the UB for optimizations in ways that break lots of code is actually trivially easy. We have been doing it for 30+ years and we just have to stop adding new optimizations that break, and possibly revert some of the ones that are already in there.

Re:What Every C Programmer Should Know About Undefined Behav... by gpderetta on Thursday January 05, 2017 @05:19AM (#13326053)
> Not exploiting the UB for optimizations in ways that break lots of code is actually trivially easy

Here lies madness. What optimizations are not allowed exactly? Strict aliasing? Assuming dereferenced pointers are not null? Assuming all array accesses are always in bound? Assuming that integers don't wrap? Assuming that allocated memory is not accessed after a free? Reusing function frame slots of variables with non overlapping lifetimes? Not laying down local variables in the most obvious way? Caching and reordering non-volatile memory accesses? Accessing local variables of functions that have returned?

I'm sure you will say obviously not allowed to the first few and obviously allowed to the last few, but at some point all of these optimizations (and more) did break some code as some point.

Re:What Every C Programmer Should Know About Undefined Behav... by mpweiher on Thursday January 05, 2017 @11:30AM (#13328477)
> Here lies madness.

Nah. You're looking at it the wrong way. You are saying madness lies in going there. We don't have to "go there". We can simply not leave there. Not doing something is easy. In fact, it's my secret super-power. If you want, I'd be happy to teach you.

Re:What Every C Programmer Should Know About Undefined Behav... by haberman on Thursday January 05, 2017 @12:17PM (#13329040)
Compilers already have these optimizations. Doing nothing would be leaving those optimizations in.

If what you want is a straightforward translation of C into machine code, there is always -O0.

Re:What Every C Programmer Should Know About Undefined Behav... by mpweiher on Thursday January 05, 2017 @03:22PM (#13330991)
> Compilers already have these optimizations.

git co <previous-hash>

You're welcome!

Re:What Every C Programmer Should Know About Undefined Behav... by haberman on Thursday January 05, 2017 @05:11PM (#13331909)
You're the person who wants this. Maybe you should do that? Also make sure you're willing to give up support for newer language standards, CPU architectures, and countless bugfixes (even C++98 support was getting pretty noticeable bugfixes in GCC 10 years ago).
Re:What Every C Programmer Should Know About Undefined Behav... by lmm on Thursday January 05, 2017 @05:29AM (#13326086)
Why would anyone who didn't want these optimizations be using C in this day and age? Your link briefly mentions Rust, D and Go, but only to say the author hasn't really looked at them, when they seem to be exactly the solution the author wants.
Re:What Every C Programmer Should Know About Undefined Behav... by qwertyuiop924 on Thursday January 05, 2017 @07:43AM (#13326657)
Here's why: C is dang near as close to the hardware as you can get without touching it, it runs everywhere, and it has so little abstraction that it's hard to incur unexpected costs. Rust is closer to C++ here, and the other languages on your list are so far outside the ballpark that they can't even touch the edge of it.

It's not the only language of this sort, but C also has a lot of code written in it, which is a major advantage.

Re:What Every C Programmer Should Know About Undefined Behav... by dragonwriter on Thursday January 05, 2017 @09:39AM (#13327384)
> C is dang near as close to the hardware as you can get without touching it

Not really; it's structurally nothing like assembly, which is as close as you can get to the hardware without just handcoding machine code.

Now, C -- for a structured language with static typing -- provides very little in terms of safety features that get in the way of direct hardware manipulation, whereas lots of languages with superficially similar structure aim to be safe in some ways by default which makes some hardware manipulation more difficult, or at least more verbose, than it would be in C. So, in some ways it gets less in the way of freedom to manipulate the hardware than some other structured programming languages.

But it's not particularly close to the hardware at all.

Re:What Every C Programmer Should Know About Undefined Behav... by nickpsecurity on Thursday January 05, 2017 @10:27AM (#13327819)
To support your argument, p-code is closer to what a portable, assembly language looks like. It's an abstract machine that can be trivially targeted to about any CISC or RISC machine. Let amateurs without compiler knowledge port Pascal/P to around 70 architectures in 2 years.

https://en.wikipedia.org/wiki/P-code_machine

Linoleum was another:

http://anynowhere.com/bb/layout/html/frameset.html

The closest in C land was C-- by Jones et al which still looks heavy compared to p-code:

http://www.cs.tufts.edu/~nr/c--/index.html

Re:What Every C Programmer Should Know About Undefined Behav... by qwertyuiop924 on Thursday January 05, 2017 @10:03AM (#13327578)
>Not really; it's structurally nothing like assembly, which is as close as you can get to the hardware without just handcoding machine code.

Here, assembly is touching the hardware. If you write your code in assembly, it's not portable to other architectures, even hypothetically.

>provides very little in terms of safety features that get in the way of direct hardware manipulation

Exactly. So it's a heck of a lot closer to the hardware than most 3GLs.

Re:What Every C Programmer Should Know About Undefined Behav... by lmm on Thursday January 05, 2017 @09:26AM (#13327278)
> C is dang near as close to the hardware as you can get without touching it

Not really true in the context of the optimizations done by modern C compilers (e.g. autovectorization).

Re:What Every C Programmer Should Know About Undefined Behav... by qwertyuiop924 on Thursday January 05, 2017 @10:06AM (#13327602)
In some ways. But, as I discussed (dragonwriter brought it up) in a cousin comment, C still does direct hardware access better than most other 3GLs. And that matters sometimes.
Re:What Every C Programmer Should Know About Undefined Behav... by dbaupp on Thursday January 05, 2017 @12:39PM (#13329307)
C itself isn't that close. Vendor extensions and relying on the compiler to pattern match certain code are needed to get access to actual hardware features, like, for instance, rotating the bits of an integer, vector instructions and efficient integer overflow detection.
Re:What Every C Programmer Should Know About Undefined Behav... by wonko1 on Thursday January 05, 2017 @02:45AM (#13325466)
But the problem is that everyone really wants their builds to run with -O3 (which should, and is, -Ogoforit).

It's really common for bugs to only appear when compiling with optimization.

Reasonably, when those bugs appear, people go fix them.

My guess is in LLVM these optimization are turned off for debug builds. But who wants to be running debug builds all the time?

Re:What Every C Programmer Should Know About Undefined Behav... by lmm on Thursday January 05, 2017 @07:14AM (#13326514)
A lot of the time non-optimized builds are fine. Unfortunately C (or rather C implementations) advertise the opportunity to make your code "as fast as possible" assuming you haven't made any mistakes, and too many programmers think "of course I want my code to run as fast as possible, and of course I don't make mistakes".
Re:What Every C Programmer Should Know About Undefined Behav... by hermitdev on Thursday January 05, 2017 @02:33PM (#13330551)
This is one of the reasons I insist on my C/C++ code compiling with "-Wall -Wextra -Werror" and a few other switches.
Re:What Every C Programmer Should Know About Undefined Behav... by nwmcsween on Friday January 06, 2017 @04:46AM (#13335057)
No, there needs to be a article on -Wall -Wextra and -Werror being harmful, these are metaflags that:

1. Vary between compiler versions.

2. Vary between compiler implementations.

3. Often contain somewhat useless warnings (Wparentheses) that can change with changes in 1. or 2.

Re:What Every C Programmer Should Know About Undefined Behav... by wonko1 on Thursday January 05, 2017 @07:39AM (#13326633)
I think it's partly that if you're not looking for speed improvements then C, as a language, begins to look less attractive.

There are certainly safer alternatives, but not faster ones.

I'd probably agree though, in most cases users don't need the fastest builds (most OS tools). It would be interesting actually to be able to enabled optimized versions of functions as a runtime flag. But I've never seen that done.