8 ms·
The author concedes that he isn't a compiler engineer, but doesn't seem to think that perhaps compiler engineers have good reason for having their compilers gen
by MaxBarraclough 8y ago
The author concedes that he isn't a compiler engineer, but doesn't seem to think that perhaps compiler engineers have good reason for having their compilers generate code that behaves bizarrely if there's undefined behavior.
There is, of course: performance.
If you want a compiler that generates code which behaves intuitively even under UB, that requirement is at odds with compiler optimisation. Compiler optimisation generally means doing strange and unexpected things to your code, and the reason C has undefined behaviour in the first place (unlike, say, Java) is to maximally enable compiler optimisation.
In other words, C values performance over safety, and that's part of the point.
If you want to catch undefined behaviour at runtime and treat it as an error, the tooling for doing that is better than ever: Clang's 'UBSan', for instance.
Edit
Aside: I have a personal favourite 'incident' pertaining to exactly this tension (between performance and intuitive behavior). Torvalds vents his frustrations on the GCC mailing list, regarding legal-but-counterintuitive behaviour of code generated for the Alpha architecture: https://gcc.gnu.org/ml/gcc/2012-02/msg00038.html https://gcc.gnu.org/ml/gcc/2012-02/msg00038.html
- jstimpfle 8y ago> C has undefined behaviour in the first place (unlike, say, Java) is to maximally enable compiler optimisation. My understanding is that C has undefined behaviour where there is no universally sensible implementation across all architectures. > Compiler optimisation generally means doing strange and unexpected things In the first place it should make code faster. I'm no one to judge, but the general reception seems to be that exploiting undefined behaviour (by assuming it will never happen) rarely brings obvious speed advantages. "Strange and unexpected things" can be a bad tradeoff for a tiny speed advantage. For example, I'll boldly state that optimizing out a "if (p == NULL)" only because p was dereferenced earlier never makes sense. Code containing such a check is never performance sensitive enough to justify the tradeoff (or it's really bad code). And without being a compiler engineer I assume that it should be easy to change compilers to NOT do this optimization (I assume the not-NULL inference is a special case in the code that can simply be removed). > In other words, C values performance over safety, and that's part of the point. Many people value C because of control and allowing one to not paint oneself in a corner. Performance is only a side effect.
- chrisseaton 8y ago> I assume the not-NULL inference is a special case in the code that can simply be removed No I would think it happens naturally as a result of more generally giving abstract values stamps, narrowing the stamps based on expressions, and then simplifying expressions using those stamps. I would guess you wouldn't want stamps and optimisations based on them removed entirely.
- oddity 8y ago> > C has undefined behaviour in the first place (unlike, say, Java) is to maximally enable compiler optimisation. > My understanding is that C has undefined behaviour where there is no universally sensible implementation across all architectures. These are two ways of looking at the same thing. A naive translation of C statements to what seems like the most straightforward way to do them on some target architecture is an optimization over emulating some other architecture’s behavior. Consider the problem of translating one arch’s assembly to another’s. Essentially, C needs to be defined at a certain level of abstraction for all of this to work, but it also happens that detecting holes in this abstraction is undecideable.
- jstimpfle 8y agoThat's OT. There isn't any confusion what "optimization" means here.
- MaxBarraclough 8y agoPerhaps I'm being pedantic, but that's not really what 'optimisation' means. Optimisation refers to code transformations which result in output code which has better performance (whether in terms of space or time). Changing the definition of the source language, isn't optimisation. Whether the source language can cleanly be mapped to the target machine, may be significant to performance, but it's not optimisation.
- mpweiher 8y ago> in output code which has better performance Without changing the output. That's the crucial part of the contract current compilers are breaking. And then try to lawyer it away by claiming that the original was undefined anyway so they can do whatever the hell they want.
- pjmlp 8y agoP̶a̶u̶l̶ Fran Allen is a well known compiler engineer, Turing awardee and she pretty clear has another point of view regarding C optimization practices.
- MaxBarraclough 8y agoDo go on.
- pjmlp 8y ago"Oh, it was quite a while ago. I kind of stopped when C came out. That was a big blow. We were making so much good progress on optimizations and transformations. We were getting rid of just one nice problem after another. When C came out, at one of the SIGPLAN compiler conferences, there was a debate between Steve Johnson from Bell Labs, who was supporting C, and one of our people, Bill Harrison, who was working on a project that I had at that time supporting automatic optimization...The nubbin of the debate was Steve's defense of not having to build optimizers anymore because the programmer would take care of it. That it was really a programmer's issue.... Seibel: Do you think C is a reasonable language if they had restricted its use to operating-system kernels? Allen: Oh, yeah. That would have been fine. And, in fact, you need to have something like that, something where experts can really fine-tune without big bottlenecks because those are key problems to solve. By 1960, we had a long list of amazing languages: Lisp, APL, Fortran, COBOL, Algol 60. These are higher-level than C. We have seriously regressed, since C developed. C has destroyed our ability to advance the state of the art in automatic optimization, automatic parallelization, automatic mapping of a high-level language to the machine. This is one of the reasons compilers are ... basically not taught much anymore in the colleges and universities." -- Fran Allen interview, Excerpted from: Peter Seibel. Coders at Work: Reflections on the Craft of Programming
- MaxBarraclough 8y agoI presume that interview was several decades ago? High-level languages are doing extremely well these days. In certain domains, no-one writes C. No-one writes a website in C. Desktop GUI applications are a mixed bag - some use C/C++ (inappropriately, in my opinion), and some use 'managed' languages. Incidentally I studied compilers at university. I agree that any supposedly serious computer science course that doesn't study them, is cheating their students.
- raverbashing 8y ago> perhaps compiler engineers have good reason for having their compilers generate code that behaves bizarrely if there's undefined behavior. > There is, of course: performance. It is a reason. But it is not a good one. Especially when you're not ignoring the possible outcomes and just ""optimizing"" code away (and even disregarding the behaviour of their own platform)
- MaxBarraclough 8y agoThat is the philosophy of most modern languages. It isn't the philosophy of C. If C took that approach, it would lose many of its unique advantages. Whether performance matters, depends wholly on what you're doing. There can be no one-size-fits-all programming language.
- raverbashing 8y ago> If C took that approach, it would lose many of its unique advantages. Such as? > Whether performance matters, depends wholly on what you're doing. There can be no one-size-fits-all programming language. Correct. But I think the performance side is overrated because C does not have the needed expressiveness to use most modern performance enhancements (like SIMD, auto vectorizing requires a lot of work and checking of conditions, and is not very efficient) and what were important optimizations 40 yrs ago today are reckless (like no boundary checks) (This discussion doesn't apply to embedded systems for the most part, those today a lot of them are faster than 15y.o. PCs)
- MaxBarraclough 8y ago> Such as? Extremely high performance, compact binaries, and its ability to be used both for cross-platform functionality, and for low-level and platform-specific functionality. All the reasons C took over the world (every major operating-system and game-engine, for starters), rather than its competitors. Kernel developers and game-engine developers care about performance. Embedded developers care about binary size. It doesn't do to pretend that C's advantages there don't really matter. C isn't a 'big government' language like Java. It isn't meant to be. It's meant to be more of a very-high-level assembly language. Programming languages are a matter of tradeoffs, not of the 'one true path'. If you want a language with good safety assurances, look elsewhere. > C does not have the needed expressiveness to use most modern performance enhancements (like SIMD, auto vectorizing requires a lot of work and checking of conditions, and is not very efficient) Auto-vectorisation is a tough nut to crack, sure. That's not specific to C though. Even frameworks like OpenCL are based on C. > what were important optimizations 40 yrs ago today are reckless (like no boundary checks) C has a pretty terrible track-record on security, yes. Precisely because it's not a safe language. I believe Clang now has a flag to add runtime boundary-checks. For many applications, I agree with you that it would be worth the performance penalty to enable that runtime check in all builds.
- mpweiher 8y ago> compiler engineers have good reason Sure they have reasons. And for them, these reasons are "good". The problem is that what is good for the compiler engineers has ceased to align with what is good for most compiler users. The reasons for this are complex, but one huge one is that we all generally don't pay for compilers any longer, at least not directly. If you have paying customers, you don't pull stunts like that. Michael Stonebraker talked about a very similar phenomenon in database systems and research: https://www.youtube.com/watch?v=DJFKl_5JTnA https://www.youtube.com/watch?v=DJFKl_5JTnA The observation I found particularly interesting is that the few commercial companies that still interact with research, and I think the same goes for compiler/optimizer engineering, are the "whales", companies like Microsoft, Amazon, Google and Facebook. Their needs are very different from most everyone else who doesn't run datacenter where a 0.1% performance increase can mean millions of dollars of cost savings and therefore be worth quite a bit of pain. Particularly when it is pain for others. > C values performance over safety No. Well, yes, because it doesn't value safety much at all. Certainly not when I started, which was with K&R C, so no type-checking at all for function calls, for example. What C values is DWITYTD, "Do What I Tell You To Do". C makes it possible for programmers to write code that performs well, it doesn't try to make code perform well. That's a crucial difference.