News for hackers. Stuff that matters.
Re:On the Madness of Optimizing Compilers by chadaustin on Tuesday March 08, 2016 @02:46AM (#11244001)
Speaking of madness, James Hague drives me a little crazy. I get it - people tend to focus too much on performance. But it seems like his blog is a series of repeated arguments that nobody needs maximum performance out of their machine. Try compiling your web browser or operating system with -O0 and then tell me the performance is satisfactory. Or your video encoder. Or VMware. There is a huge market for high-performance software where performance comes from more than a few inner loops, and optimizing compilers, while complicated and huge and occasionally buggy, are necessary for the user experience we desire.
Re:On the Madness of Optimizing Compilers by rtpg on Tuesday March 08, 2016 @03:48AM (#11244163)
I think this advice is for people who don't necessarily need it.

I don't know if there's a name for it, but I've started reading "Don't do X" as "Don't do X unless you have a realllllly good reason to." Nothing in life is absolute, after all.

Similar advice is "Use a GC". There are (similar) use cases where you shouldn't use a GC. But if you don't have a strong case for paying the price of not using a GC (in terms of potential bugs and code complexity), then using a GC by default is pretty good advice.

But yeah, if you're writing VMWare, you're hopefully also skilled enough in the domain to know that you should try to get maximum performance. But if you're writing some shell scripts or webapp, a lot of the time your bit-twiddling optimisations will be noise relative to the variation from I/O alone.

Re:On the Madness of Optimizing Compilers by zamalek on Tuesday March 08, 2016 @04:41AM (#11244340)
Take data-oriented design as an example: if you really read into it, it does actually relegate the compiler to something marginally better than a dumb code generator. The mundane optimizations (inlining, constant propagation, etc.) are expected of the compiler and the developer focuses on things like using the stack well (from the article) or being cache friendly.

Saying that C++ categorically cannot be used to create good machine code is ignoring much evidence to the contrary.

Re:On the Madness of Optimizing Compilers by vmorgulis on Tuesday March 08, 2016 @04:04AM (#11244215)
> But it seems like his blog is a series of repeated arguments that nobody needs maximum performance out of their machine.

Not false. He seems sometimes contradictory.

http://www.dadgum.com/james/performance.html

Re:On the Madness of Optimizing Compilers by junke on Tuesday March 08, 2016 @05:03AM (#11244403)
I don't understand what is contradictory here.

> The golden rule of programming has always been that clarity and correctness matter much more than the utmost speed. Very few people will argue with that. And yet do we really believe it? If we did, then 99% of all programs would be written in something like Python. Or Erlang. Even traditional disclaimers such as "except for video games, which need to stay close to the machine level" usually don't hold water any more.

He claims that you can usually choose any language you want to develop your software and that it will not make any visible difference. Systems are so fast nowadays that the trying to optimize for speed will need to an effort that is probably not justified.

Re:On the Madness of Optimizing Compilers by w0utert on Tuesday March 08, 2016 @05:49AM (#11244572)
That may be true, but suggesting this means you could just as well write everything in python or some other 'simple language' just doesn't make any sense. First of all because they all introduce their own unique set of problems (e.g. no type safety, much less static, compile-time checking, the need for a runtime, etc), second, because the way people typically use them promote programming styles that are inefficient in ways far beyond some constant-time compiler optimizations (using dict or list for everything, for example, or having no concept of data locality and cache-friendliness), and third, because it's perfectly possible to write C/C++ code that is efficient enough, readable, and easy to maintain. I agree that most of the time the performance of the most obvious implementation of some part of a program is more than sufficient, but to me that just proves the point the problem is not with compiled languages like C/C++, as you can use 'readable, maintainable' as the default way to implement things, and only drop back to obscure tricks if you really need to (which is rarely).

I don't really get the point the article is trying to get across anyway. I don't care about the complexity of the compiler at all, and 99.99999% of the time I don't care about what the assembly it spews out looks like. I'm just using the compiler as a tool, I don't need or want to know how difficult it was to get it to compile my code. The only thing I care about is that it produces valid code, and that the code gets a speedup when I compile with optimization enabled. That's about the extent of my interest in the compiler, and modern compilers have served me very well over the years. Why complain that they need to be complex to produce faster code?

Re:On the Madness of Optimizing Compilers by vmorgulis on Tuesday March 08, 2016 @02:10PM (#11247675)
> Why complain that they need to be complex to produce faster code?

Maybe because the resources could be invested in something (like Erlang).

I share similar views but for another subject (web browsers).

Re:On the Madness of Optimizing Compilers by vmorgulis on Tuesday March 08, 2016 @02:02PM (#11247611)
I wonder if his position towards (optimizing) compilers is not related to the near monopoly of C++ in game programming (he is a professional).

BTW I like a lot his blog.

Re:On the Madness of Optimizing Compilers by pjmlp on Tuesday March 08, 2016 @05:01AM (#11244397)
All good examples, however the problem are those developers micro-benchmarking each code line they write even if the user of the program doesn't care if it takes 1ms or 1s to achieve what they are doing.

This mentality is quite common in certain programming circles.

Re:On the Madness of Optimizing Compilers by joosters on Tuesday March 08, 2016 @06:08AM (#11244631)
They can do that all they like as long as the binary still works in all the same situations. Fine by me. I even gave the compiler permission to do it with the -O flags.

Or are you trying to dictate what compiler writers should be focusing on? They're free to choose what to work on, so don't use their compiler if you disagree with it.

Re:On the Madness of Optimizing Compilers by pjmlp on Tuesday March 08, 2016 @07:21AM (#11244807)
> They can do that all they like as long as the binary still works in all the same situations. Fine by me. I even gave the compiler permission to do it with the -O flags.

If we are talking about C and C++, the binary doesn't have to work according to the UB rules.

> Or are you trying to dictate what compiler writers should be focusing on? They're free to choose what to work on, so don't use their compiler if you disagree with it

Hence why I never code in C unless there isn't any option, and avoid C style code as much as possible when using C++.

Re:On the Madness of Optimizing Compilers by joosters on Tuesday March 08, 2016 @08:17AM (#11244987)
If your program includes undefined behaviour, the compiler cannot magically make your code work, optimisation or not. At best, it can guess what you meant, but guessing is impossible to get completely right. The only way out is to fix your code.
Re:On the Madness of Optimizing Compilers by pjmlp on Tuesday March 08, 2016 @08:38AM (#11245094)
I was talking of UB being an open door to break applications, given that the standard allows for "anything goes" in terms of code generation.

I bet very few C developers, less than 1% outside ISO tables and compiler vendor teams, know ANSI C from cover-to-cover and are able to write UB free code when one sticks to the letter of the standard.

Re:On the Madness of Optimizing Compilers by MichaelBurge on Tuesday March 08, 2016 @02:31AM (#11243965)
If I'm writing code that actually needs to be optimized, then I usually put it into its own object file and tweak the compiler flags as necessary to get the assembly to look the way I want. The C compiler supporting this saves me time.

For any other language except C(Haskell, Java, C++, C#, Go, etc.), I don't actually care that much about compiler performance as long as it does the obvious optimizations. Generally you can look at performance at a higher semantic level as soon as your code starts implying calls to malloc() - and conversely, you can start looking at the assembly as soon as you've removed all calls to malloc() in the offending section.

The other thing that transcends compilers or languages are the actual bits that get stored in memory or on disk. Even if I'm writing Perl, it makes a difference if I'm packing something into a contiguous array or just packing a bunch of pointers or references to all over my heap. Or if I can shave a couple bytes off every record in a hundred million records.

Javascript might be the one exception, since people using it don't have any other choice. I don't know - is there such a thing as an FFI for Javascript, using a browser extension or something?

Re:On the Madness of Optimizing Compilers by dclowd9901 on Tuesday March 08, 2016 @03:40AM (#11244141)
Asm.js lets you write a strictly typed subset of JS. Most prototypes of it are compiled C running (compiled to JavaScript, yes) 3D games. When compiled, Asm hints the JIT so that it can further optimize around the loose typing of JS.
Re:On the Madness of Optimizing Compilers by evincarofautumn on Tuesday March 08, 2016 @03:55AM (#11244188)
> put it into its own object file and tweak the compiler flags as necessary

This is a good approach, as are finer-grained optimisation pragmas & attributes, #ifdeffed as needed.

Ideally, I’d like the compiler to do only cursory optimisations by default, just enough that I can read the assembly and not say “wow, this is stupid”.

For anything else, I’d rather have a dialogue with the profiler & compiler heuristics, wherein they suggest optimisations, but I choose which ones I want using optimisation pragmas, and the compiler does the grunt work of program transformation.

For example, the compiler would say “this function is only ever called in cold paths; I suggest adding the ‘cold’ attribute” or “this loop can be vectorized; I suggest adding the ‘vectorize’ attribute”.

Re:On the Madness of Optimizing Compilers by gliese1337 on Tuesday March 08, 2016 @03:16AM (#11244076)
Native objects are probably as close as you come in most implementations. Windows Scripting Host does have a more general ability to call foreign code from inside JScript (the Microsoft implementation of ECMAScript). I used to write quite a bit of JScript code that called out into windows executables and DLLs.
Re:On the Madness of Optimizing Compilers by sanxiyn on Tuesday March 08, 2016 @02:37AM (#11243979)
> I've seen arguments that some people desperately need every last bit of performance, and even a few cycles inside a loop is the difference between a viable product and failure.

This is a strawman. The usual argument is that 1% better performance is, well, 1% better. That's a whole server if you have 100 servers. People are not desperate, but do want an additional server for free.

> Assuming that's true, then they should be crafting assembly code by hand.

This, and "a few cycles inside a loop", is another strawman. What usually happens is a good compiler saves a bit everywhere, which helps when the program is optimized to flat profile without hotspot. Since saving is everywhere, "crafting assembly" amounts to lots and lots of assembly, not just a few in strategic places.

Re:On the Madness of Optimizing Compilers by trungaczne on Tuesday March 08, 2016 @02:51AM (#11244017)
To add to this, performance is very important in certain domains.

Take game development for instance, if your code runs a bit faster that's more frames you're rendering per second. This can mean the difference between a laggy, unplayable mess and a good gaming experience on an average machine (a laptop, or a non-gaming rig).

Re:On the Madness of Optimizing Compilers by randomsearch on Tuesday March 08, 2016 @03:04AM (#11244050)
Most software is not games or scientific computing. Most software is not speed critical. This is why most new software is not written in C.
Re:On the Madness of Optimizing Compilers by adrianN on Tuesday March 08, 2016 @03:35AM (#11244130)
Most new software does run on devices with batteries though. Having the CPU sleep more is a good thing to have.
Re:On the Madness of Optimizing Compilers by randomsearch on Tuesday March 08, 2016 @04:20AM (#11244275)
This is an interesting thought.

Do we know that most new software runs in batteries? Do you have a reference for that? It would be useful.

Other question is - does a sleeping CPU always win, or are other optimisations such as memory usage, screen usage, and communications, more important? I know for sure that the CPU is not the main consumer of power on a bunch of Android phones that were used in an academic study.

Re:On the Madness of Optimizing Compilers by adrianN on Tuesday March 08, 2016 @04:34AM (#11244311)
I don't have hard numbers. Most computation is probably done on servers. But power matters there as well (a lot). Most devices sold today are laptops and smart phones, I think. Some people claim that the IoT will be a thing real soon now, with embedded sensors everywhere.

Probably the CPU is not the main power sink on mobile devices (screen and radio are I think). But cranking up the optimization level is free for the developer.

Re:On the Madness of Optimizing Compilers by randomsearch on Tuesday March 08, 2016 @04:54AM (#11244378)
So, this was my point - is it really free for the developer? For example, GPGPU compilers full of subtle bugs cost significant development time.
Re:On the Madness of Optimizing Compilers by adrianN on Tuesday March 08, 2016 @05:23AM (#11244468)
GPGPU is still relatively new and the tooling is still under work. But compiling with g++ -O2 instead of -O0 is pretty safe
Re:On the Madness of Optimizing Compilers by trungaczne on Tuesday March 08, 2016 @03:48AM (#11244164)
Not all software need to be speed critical but all softwares benefit from being faster. Part of the reason why we can afford to be less optimal when writing software is because many libraries have already been optimised for us (and it's not uncommon for these libraries to map to C or assembly code to utilise the hardware more efficiently), ranging from serialisation libraries to crytography libraries. This is not to mention the OS that we run the software on and other tools that the software depend on (such as databases). A small compiler optimisation improvement across the entire ecosystem can have a dramatic effect.
Re:On the Madness of Optimizing Compilers by randomsearch on Tuesday March 08, 2016 @04:15AM (#11244261)
> but all softwares benefit from being faster.

It's not so simple. If you're trading off simplicity in the compiler for that speed,for example, it may be a net loss.

If speed was the only concern, we'd all go out and buy super computers.

Re:On the Madness of Optimizing Compilers by Tiksi on Tuesday March 08, 2016 @06:57AM (#11244740)
Both chrome and firefox lag all over the place on a modern quad core box with 16GB of ram and hardware accelerated rendering on a 8GB r290. Most software might not be speed critical, but those few milliseconds not saved add up quickly, and performance should definitely be a goal of most (end user) software. I mean if you're only running the software on your own servers, then by all means ignore performance as much as you please if it's a worthwhile tradeoff for you. But please don't ignore speed when it comes to software other people have to run.
Re:On the Madness of Optimizing Compilers by zimpenfish on Tuesday March 08, 2016 @07:07AM (#11244773)
Chrome and Firefox suffer from having to run other people's (likely unoptimised) software in their sandbox.

You can optimise as much as you like but then John Q Madbugger comes along with their insane Javascript (not looking at Flickr here, no sir) and suddenly your performance is in the toilet.

Re:On the Madness of Optimizing Compilers by Tiksi on Tuesday March 08, 2016 @07:45AM (#11244877)
Yeah, the bloatloads of javascript are probably the worst offenders of ignoring performance of software other people have to run, sometimes even reimplementing features the browser already has, just slower and worse (Listen Zendesk, my browser has been doing tabs just fine for a decade, implementing them in javascript and entirely hijacking normal browser navigation behavior doesn't make anything better for anyone).

Thinking over it some more, browsers themselves were probably not a good piece of software to call out on performance considering how much optimization is actually done to increase their performance. The horrid things people do on top of them deserve all the blame they get though, and then some.

Re:On the Madness of Optimizing Compilers by zimpenfish on Tuesday March 08, 2016 @07:58AM (#11244914)
The QNAP web interface implements a whole internal windowing system in Javascript. It is the stupidest dumbest nonsense I've seen for years and is blitheringly painful to use.
Re:On the Madness of Optimizing Compilers by dkopi on Tuesday March 08, 2016 @03:01AM (#11244044)
And a good compiler doesn't only optimize for speed. It also optimizes for code size, memory usage.

I do however agree with underlying premise of this post: Code that works 100% of the time is better than higher performant code that explodes in your face every once in a while.

Re:On the Madness of Optimizing Compilers by 0x07c0 on Tuesday March 08, 2016 @05:28AM (#11244484)
>And a good compiler doesn't only optimize for speed. It also optimizes for code size, memory usage.

No they don't. Code size is rarely a problem (embedded probably cares..). Optimizations will in most cases lead to larger code, loop unrolling, inline expansion etc. In c the compiler cant't, in most cases do anything about memory usage. (Probably could pack structs.. , but that is not something the compiler would do I think, you would break ABI with non optimized code). In managed languages, memory usages is probably more depended on the runtime system GC, then compilers.

GCC has a flag for code size optimization -Os https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

Re:On the Madness of Optimizing Compilers by dkopi on Tuesday March 08, 2016 @09:53AM (#11245529)
From that exact link: "Turning on optimization flags makes the compiler attempt to improve the performance and/or code size at the expense of compilation time and possibly the ability to debug the program."

"With -O, the compiler tries to reduce code size and execution time"

So actually yes, The compiler optimizes for both size AND speed. Sometimes you can optimize for maximum speed (-O3), and sometimes optimize for minimum code size (-Os). You can even optimize for best debugging or best compilation times, and forget about size/speed.

As for memory usage -

1. First of all - code uses up memory. smaller programs = less memory used for holding the exectuable code.

2. Consider the various ways you could compile a switch(-case) statement. You could build it as a set of conditional branches one after the other, you could compile it into a binary tree search (or a hash dictionary lookup), and you could jump to an address found in a lookup array table.

This former method is also called a "branch table". https://en.wikipedia.org/wiki/Branch_table

A branch table would be the fastest method for implementing a switch with a few sequential values (0,1,2,3,4,5) But would be a really bad idea for sparse values (0,200,5000,10000,200000).

Choosing not to use a branch table for sparse values is an optimization for Memory usage. And compilers make this decision all the time.

Re:On the Madness of Optimizing Compilers by golergka on Tuesday March 08, 2016 @04:33AM (#11244308)
> Code that works 100% of the time is better than higher performant code that explodes in your face every once in a while.

That depends. In game programming, if I had a choice between a render that will crash once every 10 hours and produce 60 fps (in some model envrionment) vs a render that never crashes but produces 45 fps, I would choose the first one.

Re:On the Madness of Optimizing Compilers by iofj on Tuesday March 08, 2016 @06:15AM (#11244647)
Ditto for math libraries, machine learning libraries + tools, statistics calculations and matrix multiplier kernels.

Hell, I'd take a 1% chance of blowup for a 10% speedup any day.

Re:On the Madness of Optimizing Compilers by pif on Tuesday March 08, 2016 @06:44AM (#11244709)
> I'd take a 1% chance of blowup for a 10% speedup any day.

Are you serious? Would you base your research career on software that you don't trust? I wouldn't.

Please, notice that 1% chance of full blowup means a not-null probability of nice-looking-but-actually-false results from time to time, and that's a risk no researcher is allowed to accept.

Re:On the Madness of Optimizing Compilers by iofj on Tuesday March 08, 2016 @10:13AM (#11245665)
> Are you serious? Would you base your research career on software that you don't trust? I wouldn't.

I guess you haven't seen the quality of academic code recently. The choices are basing your credibility on slow badly written code or fast badly written code.

Re:On the Madness of Optimizing Compilers by 0x07c0 on Tuesday March 08, 2016 @06:52AM (#11244726)
That is what we have fast math for :)
Re:On the Madness of Optimizing Compilers by randomsearch on Tuesday March 08, 2016 @03:03AM (#11244048)
> That's a whole server if you have 100 servers. People are not desperate, but do want an additional server for free.

Renting an extra server costs practically nothing comparing to wage costs for a tech company.

So I'd say they'd care more about the compiler being reliable and predictable.

Re:On the Madness of Optimizing Compilers by DannyBee on Tuesday March 08, 2016 @04:11AM (#11244245)
Maybe when it's 1 server.

I have a team that regularly generates savings of XX% of the entire datacenter CPU cycles of a very large fleet through better compiler optimization (that pretty much never breaks any of our programs. In fact, we break them more through almost anything other than better optimization. Shocking, i know, but you can test this stuff really well it turns out, and write sanitizers to find behavior you wouldn't discover at compile time and ...)

I know at least another 100 large companies where the cost savings of doing this kind of thing also totals in the tens to hundreds of millions, per year.

and that's just the normal X% a year, not the "we put a team of experts targeting your use case and they got you 100x" type of savings.

So yeah, there are people for whom this matters ... a lot, and dismissing them casually is not a great idea :)

Truthfully, IME, compiler-optimization induced bugs are so infrequent compared to normal you-wrote-really-wrong code bugs that IMHO it's missing the forest for the trees. (now, maybe people suck more at tracking them down, but ...)

Heck, I had once screwed up some code in GCC that caused it to essentially remove random stores and loads from the program being compiled. It only caused one visible failure across a bunch of real world apps that had serious validation suites.

From reading hacker news, you'd get the idea compiler optimizations follows your code down alleyways to mug it. In truth, people are so busy overflowing buffers and whatnot that we are lucky if we can break their code due to optimizing.

Re:On the Madness of Optimizing Compilers by titzer on Tuesday March 08, 2016 @05:13AM (#11244427)
I don't disagree with the above, but I do see some circumstances where dropping down a carefully hand-tuned page-long assembly loop is warranted. It's not something I'd recommend in the general case, and only if the problem is likely to remain completely well specified for a long enough time to pay off. That is not generally the case when the assembly blob starts getting appreciably larger than a single loop, e.g. crosses function boundaries, needs to coordinate complex data structure layouts, etc.
Re:On the Madness of Optimizing Compilers by randomsearch on Tuesday March 08, 2016 @04:17AM (#11244264)
How many software engineers are working on data centre scale systems?

Performance gains of 1% just don't matter to most programmers.

Re:On the Madness of Optimizing Compilers by asdfaoeu on Tuesday March 08, 2016 @03:21AM (#11244094)
> Assuming that's true, then they should be crafting assembly code by hand.

This guy, what a fucking joke. Yeah to avoid bugs instead of using these fancy compilers (which rarely if ever are the cause of bugs) we should be assembling our programs by hand.

Re:On the Madness of Optimizing Compilers by titzer on Tuesday March 08, 2016 @03:28AM (#11244112)
I've seen people go to extreme lengths to teach compilers particular optimizations that only apply in extremely narrow circumstances. Worse, they usually end up additionally have to introduce a way for a compiler to assume invariants that would break normal programs (e.g. overcome weaknesses in alias analysis). I've seen people go to extreme lengths and rely on C++ undefined behavior to get some weird quirk out of some specific machine instruction. All of that is extremely brittle; move to a new compiler version, new compiler, or even slightly alter the code, and suddenly the optimization either stops working or the compiler generates wrong code.

At some point, for extremely hot loops, there may be no choice but to write a few pages of assembly by hand. If this saves tens of thousands of lines of compiler code that doesn't generalize, and is used on thousands or millions of machines, it's worth it, IMO.

Re:On the Madness of Optimizing Compilers by yxhuvud on Tuesday March 08, 2016 @02:40AM (#11243987)
> The straightforward "every program all the time" compiler is likely within 2-3x of the fully optimized version (for most things)

Citation needed. Pulling numbers out of your ass doesn't an argument make.

Re:On the Madness of Optimizing Compilers by 0x07c0 on Tuesday March 08, 2016 @04:58AM (#11244392)
That number my have be true pre 2000, but with the move to parallel architectures it is grossly wrong. Optimization is not about saving a few cycles here and there. It is about using the whole machine, multiple chips, all with multiple cores. This cores are vector cores (AVX/SSE, altivec, etc). The chips are on different NUMA domains, the cores has pipelines, caches, etc, that has to be exploited... Compilers really don't do to much about this. This is now the job of the programmer. Speedup for codes that can exploit this, at least 20-30x. This adds up to loot of computer time... (add multiple accelerators (GPGPUs, MIC), and you can have 200-300x speedup on one node)
Re:On the Madness of Optimizing Compilers by chubot on Tuesday March 08, 2016 @02:55AM (#11244027)
I agree it's not really an argument, but I think the implementation of the Go compilers is evidence towards his point. The compilers were straightforward and didn't use fancy representations or optimizations AFAIK (being based on old Plan 9 compilers).

And with that philosophy, I believe it was within 2-3x of C++ in most benchmarks, despite being in a higher level language (though no doubt it's much slower on some benchmarks).

That said, they continue to optimize the compilers (e.g. https://news.ycombinator.com/item?id=9099744). People obviously do want speed; 2x slower isn't acceptable in many cases.

Re:On the Madness of Optimizing Compilers by mpweiher on Tuesday March 08, 2016 @04:00AM (#11244202)
On Proebsting's Law[1].

"The results of our experiment suggest that Proebsting’s Law is probably true. The reality is somewhat grimmer than Proebsting initially supposed."

"Proebsting has suggested that the compiler and programming language research communities should focus less on optimization and more on programmer productivity"

[1] http://www.cs.virginia.edu/~techrep/CS-2001-12.pdf

Re:On the Madness of Optimizing Compilers by rlpb on Tuesday March 08, 2016 @02:58AM (#11244036)
"If you get any of this wrong, even in an edge case, the user is presented with non-working code for a valid program, and the compiler writer has failed at his or her one task."

Perhaps languages could be designed so as to make this task easier?

In language design we end up torn between making things easier for the programmer and making things easier for the compiler-designed-for-performance. Doing both things well is perhaps something that language design will be able to improve upon in the future.

Re:On the Madness of Optimizing Compilers by akkartik on Tuesday March 08, 2016 @03:11AM (#11244064)
A degenerate case is my project, which makes people write assembly, but in a very safe way that it can check. You lose the expressivity of conventional HLLs, but still get their safety benefits.

https://github.com/akkartik/mu

Re:On the Madness of Optimizing Compilers by jcr on Tuesday March 08, 2016 @04:38AM (#11244324)
After reading the mu link you posted, I was going to ask you to submit it as a "Show HN" post... but it seems I'm about a month too late:

https://news.ycombinator.com/item?id=11092067

It's great reading. thanks.

Re:On the Madness of Optimizing Compilers by carussell on Tuesday March 08, 2016 @03:14AM (#11244069)
See also: http://cr.yp.to/qhasm/literature.html

> The programmer using such a system will write his beautifully-structured, but possibly inefficient, program P; then he will interactively specify transformations that make it efficient.

Re:On the Madness of Optimizing Compilers by bijection on Tuesday March 08, 2016 @03:23AM (#11244100)
Halide is a neat example of this approach in an image processing context [1] [2]

[1] http://halide-lang.org/

[2] https://people.csail.mit.edu/jrk/jrkthesis.pdf "Decoupling Algorithms from the Organization of Computation for High Performance Image Processing"

Re:On the Madness of Optimizing Compilers by dkopi on Tuesday March 08, 2016 @03:10AM (#11244061)
Languages and compilers actually do have constructs for programmers to make things easier for the compiler.

"inline" and "register" are two very known examples. Other examples are function attributes in gcc: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

Re:On the Madness of Optimizing Compilers by nkurz on Tuesday March 08, 2016 @03:48AM (#11244160)
I presume you're aware that you've chosen two controversial examples? "register" has been formally deprecated in C++, and has been ignored by (almost?) all C compilers for decades: http://stackoverflow.com/questions/20618008/replacement-for-...

And most current wisdom is that "inline" no longer serves as a useful performance suggestion to the compiler, but instead should only be used as a linkage directive to allow multiple declarations of the same function: http://stackoverflow.com/questions/29796264/is-there-still-a...

Re:On the Madness of Optimizing Compilers by dkopi on Tuesday March 08, 2016 @04:01AM (#11244208)
Great points. As compilers evolve, optimizers are even more knowledgeable of what should be put in a register or inline that the programmer.

But those are still 2 well known features that were added intentionally to help improve performance.

Re:On the Madness of Optimizing Compilers by 0x07c0 on Tuesday March 08, 2016 @06:14AM (#11244642)
restrict is probably a better example! https://en.wikipedia.org/wiki/Restrict
Re:On the Madness of Optimizing Compilers by dkopi on Tuesday March 08, 2016 @07:51AM (#11244895)
great example
Re:On the Madness of Optimizing Compilers by legulere on Tuesday March 08, 2016 @02:58AM (#11244038)
So this article says that optimizing compilers are unreliable? I don't think this is the case.

Compiler bugs are pretty rare, a bigger problem are insane language standards that have undefined behaviour lingering behind every corner.

Re:On the Madness of Optimizing Compilers by randomsearch on Tuesday March 08, 2016 @03:07AM (#11244055)
Compiler bugs are not rare, quite the opposite, and this is often because of premature optimisation.

For example, GPGPU compilers are awful:

http://www.doc.ic.ac.uk/~afd/homepages/papers/pdfs/2015/PLDI...

Re:On the Madness of Optimizing Compilers by DannyBee on Tuesday March 08, 2016 @04:13AM (#11244255)
It's more that compiler vendors who compete mainly on the basis of worthless benchmarks do awful things.

This is one of the reasons gcc/llvm won: they don't care about that stuff. I mean, they care about performance, but not at all costs like these guys care.

At some point, people wanted compilers that did less stupid things. So they moved to the vendors who did that.

The reason it's so awful in the GPGPU space is because none of these vendors produce compilers yet. Instead, the vendors you have are those whose main goal in life is to generate better performance numbers than the other guy.

Ask anyone about the old days of fortran compilers and early optimizing C compilers and what have you.

What exists now is essentially "what happened after the market pushed back". Sadly, it didn't go as far as some people want (in either direction).

Re:On the Madness of Optimizing Compilers by loup-vaillant on Tuesday March 08, 2016 @07:04AM (#11244762)
> This is one of the reasons gcc/llvm won: they don't care about that stuff. I mean, they care about performance, but not at all costs like these guys care.

Really? http://www.complang.tuwien.ac.at/kps2015/proceedings/KPS_201... https://news.ycombinator.com/item?id=11219874

While GCC and LLVM do care about implementing the standard correctly, they show little mercy to the poor developer who failed to avoid one of the many sharp corners of undefined behaviour.

Re:On the Madness of Optimizing Compilers by DannyBee on Wednesday March 09, 2016 @12:31PM (#11254289)
Yes. Really.

There was a long thread about this just a few days ago (https://news.ycombinator.com/item?id=11219874)

I already commented there, and since i didn't always have pleasant things to say, i'm not going to repeat them here.

Re:On the Madness of Optimizing Compilers by loup-vaillant on Tuesday March 08, 2016 @03:27AM (#11244106)
(Edit: oops, I haven't read your second line…)

[Indeed,] There's more to it than bugs.

Undefined behaviour for instance is generally understood by the programmer as being runtime driven craziness. Divide by zero for instance will raise a clean error (generally resulting in a clean crash) on most platform.

Popular C compilers such as GCC and LLVM however, have a different understanding, based on the wording of the standard. They are allowed to assume that undefined behaviour simply doesn't happen, and use that assumptions in optimizations such as dead code elimination or alias analysis. With this stuff, a program that would have exhibited undefined behaviour if compiled straightforwardly is allowed to go bananas at any time. This is compile time driven craziness, a rather different beast.

A classic example of this is signed integer overflow. You perform some arithmetic that may overflow, then check for overflow after the fact. Most architectures perform two's complement modulo arithmetic, so it works without problem. Except that in C, this is undefined behaviour. So your compiler is allowed to assume it never happens. Which means your test that `x >= x+1` is going to be "true" all the time. Which means that your error handling branch is dead code. And that could allow nasty stuff such as buffer overflows.

You could say it's the user's fault for relying on undefined behaviour (that is nevertheless well defined on x86 and ARM assembly). But you gotta admit that's not very intuitive. Yes, in some obscure platforms such an overflow could make your CPU jump in a random place. Yes, relying on this is not portable. But C is supposed to be close to the metal. Few C users would expect the compiler to optimize it all away even if it would work on their platform.

On the other hand, things like strict aliasing are explicitly about enabling compiler optimizations.

Re:On the Madness of Optimizing Compilers by MichaelBurge on Tuesday March 08, 2016 @03:16AM (#11244074)
I run into so many compiler bugs. Examples: g++, ghc, booc. I remember one time playing around with Unity3D, and for some reason decided to use the Boo language to write my code. I ended up having to patch booc to work around about a dozen different bugs.

There's also the time I caused a kernel panic on CentOS 5 by running g++ on my code. It did so reliably, so I moved development to CentOS 6.

Older gdbs on CentOS 4 will segfault if you run it on a binary compiled with ghc and ask for a backtrace.

One time, I wrote a Perl module that unintentionally trashed the Perl interpreter's error tracking information. So if the module compiled, it worked fine. But if you had even a single variable mis-spelled or missing semicolon, all you'd get from perl is the error "Unknown error".

Or even something as simple as writing a negative integer literal bugs out ghc: https://ghc.haskell.org/trac/ghc/ticket/9533

Compilers are absolutely riddled with bugs.

Re:On the Madness of Optimizing Compilers by mkj on Tuesday March 08, 2016 @07:28AM (#11244824)
Your examples sound more like bugs in the Linux kernel or gdb.
Re:On the Madness of Optimizing Compilers by riscy on Tuesday March 08, 2016 @03:33AM (#11244121)
This is a meaningless article. A compiler that chooses to perform an optimization (transformation) which does not preserve the defined semantics of the language has a bug, by the definition of what a compiler is. What is the argument?
Re:On the Madness of Optimizing Compilers by jfoutz on Tuesday March 08, 2016 @03:50AM (#11244169)
something like -Ofast is well defined. the thing is, it defines another language closely related to the source language but with some extra constraints. So you have C, and also C'. It may be the case that all the C in your program happens to be valid C', but probably not. If you're not acutely aware of the rules of C', you won't get what you want. And, some poor hapless C programmer may find themselves working on a C' project and not know it, or the expert is suddenly a beginner again, because they need to learn the extra rules of the game.

It's all well defined. it's a social problem. it's something to be aware of.

Re:On the Madness of Optimizing Compilers by riscy on Tuesday March 08, 2016 @10:11AM (#11245650)
> If you're not acutely aware of the rules of C', you won't get what you want.

The same can be said of any language that lacks a rigorous definition; misusing -Ofast is just an example of one. Even then, the definition can be too large or tricky for anybody to truly understand, but that's a bit of a grey area... at some point you have to draw the line and say the programmer made a mistake. The only sensible thing to do is draw the line at the definition of the language, otherwise there is chaos.

Re:On the Madness of Optimizing Compilers by sharpneli on Tuesday March 08, 2016 @07:32AM (#11244837)
I don't understand the point of the article. Using optimizations in a compiler is fully voluntary act. You can just compile everything with -O0 and be done with it.

Some of us actually do need them. Soft realtime programs (games as an example) are perfect example where they are crucial. Reaching same level of performance using non optimizing compiler would balloon the development costs up multiple times.

I would understand the complaint if compilers would mess things up and you couldn't get around it.

Re:On the Madness of Optimizing Compilers by gambiting on Tuesday March 08, 2016 @05:55AM (#11244593)
"I've seen arguments that some people desperately need every last bit of performance, and even a few cycles inside a loop is the difference between a viable product and failure. Assuming that's true, then they should be crafting assembly code by hand, or they should be writing a custom code generator with domain-specific knowledge built-in."

Well, I work in the games industry and that "last bit of performance" is quite frequently the difference between having a 60fps game and something that doesn't quite hit the vsync and looks a lot worse. Seriously, this is one of the very few industries where I can say with a straight face "don't do context switching because the 5 microsecond delay is too much" and "a missed L2 cache hit costs us 50 cycles, and it a major problem here".

Re:On the Madness of Optimizing Compilers by Rexxar on Tuesday March 08, 2016 @06:50AM (#11244723)
I think the problem is not too much optimizations but the undefined behaviors/compiler bugs detection. There should be an "optimized debug" mode that apply all optimizations requested but add asserts to check every assumptions made by the compiler. In this way we can run the "optimized debug" binary and check it doesn't trigger any assert.
Re:On the Madness of Optimizing Compilers by dalailambda on Tuesday March 08, 2016 @04:35AM (#11244316)
While I understand the argument the author is making, I think it's important to consider that while a mild focus on performance could only benefit your code a little, applied across your entire stack, the performance benefits start to add up, and become worthwhile.
Re:On the Madness of Optimizing Compilers by lmm on Tuesday March 08, 2016 @03:55AM (#11244186)
But why would people who don't need that last 1% of performance be using C at all in this day and age? The article talks about crafting assembly by hand, but C is often spoken of as a cross-platform assembler.
Re:On the Madness of Optimizing Compilers by mpweiher on Tuesday March 08, 2016 @04:12AM (#11244249)
I blame:

(a) free compilers

(b) the C standard

Crazy talk, surely! Let me explain: each of these is a Good Thing™, but together they have put us in our current predicament. It used to be that compilers were reasonably expensive products, so as a vendor you had a clear incentive not to piss off your customers. In addition (in the very olden days), there as no C standard so the definition of "correct" was very customer-based: if customer code compiled well, you were doing a good job, if not, you would probably suffer.

With compilers becoming free, the incentive structure changed, customers now matter very little. Instead, you get ahead in that pecking order by improving some SPEC benchmark by 0.2%. At the same time, the C standard, which was worded very inclusively so even compilers for odd architectures could be ANSI/ISO compliant, was used as a substitute for "my customers' code works", and people started to say silly things like "if you have undefined behaviour, demons can fly out your nose".

Well: I had a pre-ANSI compiler. ALL its behaviour was "undefined". And yet it took fewer liberties with the code than many of today's compilers do. Because the creators (a) were reasonable people and (b) had real customers.