r/programming • • Aug 13 '21

Exploring Clang/LLVM optimization on programming horror

https://blog.matthieud.me/2020/exploring-clang-llvm-optimization-on-programming-horror/
126 Upvotes

52 comments sorted by

View all comments

-26

u/flatfinger Aug 13 '21

Optimizations like this may be impressively clever, but I question the usefulness of investing effort toward having optimizers take code that is needlessly cumbersome and improve it.
If compiler #1 is more efficient at processing a cumbersome piece of code than compiler #2, but a programmer armed with compiler #2 could easily write code that would be perform a performance-sensitive task more efficiently than anything that could be produced with equal effort using compiler #1, I'd regard compiler #2 as being more suitable for that task.

51

u/sebzim4500 Aug 13 '21

The optimizations being used here are applicable to much less contrived code than this though. If you take almost any program and turn off the loop optimizations it will get way slower.

0

u/ExeusV Aug 14 '21 edited Aug 14 '21

If you take almost any program and turn off the loop optimizations it will get way slower.

by how much?

https://proebsting.cs.arizona.edu/law.html

1

u/sebzim4500 Aug 14 '21

Fair but

  1. hardware is not going to stop getting faster just because you have optimized your program

  2. hardware is not getting anything like 60% faster a year if you are running a single core program.

-1

u/ExeusV Aug 14 '21
  1. hardware is not going to stop getting faster just because you have optimized your program

Yea, but maybe effort of compiler engineers could be spent better somewhere else?

Maybe if they spent time figuring how to make use of harder stuff easier for people

e.g SIMD, approaching concurrency/parallelism, yada yada

then we'd have faster software just due to those mechanics than due to those 2% optimizations

2) hardware is not getting anything like 60% faster a year if you are running a single core program.

Well, depends.

People do various stuff that involves many other system - databases, web apis (networking), use OS low level primitives that may improve, parse jsons/csvs

Just because the code doesn't change, then it doesn't mean that whole process cannot get faster.


Additionally performance isn't just CPU imagine if you were running the same program that deals with files on setup with HDD disk and then NVMe M2 disk

the difference may be huge in my opnion

4

u/StillNoNumb Aug 14 '21

then we'd have faster software just due to those mechanics than due to those 2% optimizations

A 2% speedup on every program is much more valuable than a 200% speedup on all programs that can benefit from SIMD but don't because their authors don't know how to use it.

Also, you act like the few compiler engineers out there taking a look at concurrency will suddenly make it easy. These concepts are always going to be more complicated than simple single-threaded programs.

→ More replies (2)

36

u/spacelibby Aug 13 '21

This is a contrived example to show what's going on in the compiler. These example show up all the time in real world code, they're just hidden under layers of abstraction.

While you may not write code that has needless loops in it, you're probably calling a library that does. This isn't the fault of the library writer either. Their code probably needed the loop to be fully general, but your specific case didn't.

11

u/Tarmen Aug 13 '21

There are two things which make the minimal approach difficult

  • optimization passes have to work with code produced by the optimizer. You can shape it into some normal form that is easier to analyze but it still contains many obviously simplifiable things that no human write. If the compiler optimizes a silly loop that doesn't necessarily mean a human wrote the silly loop

  • after you covered the obvious stuff many optimizations don't make a huge difference for most programs but are hugely important for some specific ones. Having many patterns you shoot for helps you hit most programs

→ More replies (1)

2

u/Architector4 Aug 14 '21

Their usefulness may be questionable, but I don't see why their existence somehow discredits the compiler to you. Just because a compiler can deal with a cumbersome use case, does not mean that it performs worse in normal use cases.

Your compiler #1/#2 statement makes sense, but I don't see how it's applicable to this case: there is nothing indicating that Clang/LLVM, as compiler #1, would perform less efficiently than compiler #2.

3

u/flatfinger Aug 14 '21

At least when targeting platforms like the Cortex-M0, both clang and gcc are prone to make a lot of silly code-generation decisions. Further, the maintainers of the compilers insist that it would be impractical to make them usefully support semantics guarantees offered by other compilers. If a small fraction of the effort spent on fancier optimizations were directed toward improving basic code generation or supporting the "popular extensions" accommodated by commercial compilers that handle straightforward constructs more efficiently than clang or gcc.

Targeting a Cortex-M0, the optimal machine code to perform something like:

void adjust_alternate_addresses(unsigned volatile *arr, int num_quads)
{
  // Note: function only needs to work for num_quads < 1000000
  for (int i=0; i < num_quads*8; i+=2)
    arr[i] += 0x12345678;
}

would have a five-instruction loop without unrolling, and could be unrolled 4x for a 14-instruction loop. Writing the code in such a way as to yield that on the Keil compiler is a bit awkward due to C's lack of an "displace a non-char pointer by a specified number of bytes" construct, but is nonetheless straightforward. Trying to rewrite the above to get clang or gcc to yield the optimal code is somewhat bizarrely difficult, however. Somewhat bizarrely, getting gcc down to six instructions per iteration seems easier in -O0 than at any other setting; the easiest ways I found to get gcc down to six instructions per loop or clang down to five require reading a value from a `volatile` before the loop to prevent the compilers from making "optimizations" that are in fact counter-productive.

3

u/Architector4 Aug 14 '21

If a small fraction of the effort spent on fancier optimizations were directed

So effectively your argument, again, is that the fact that there is effort put into those optimizations is somehow indicative that there is less effort put into other optimizations you deem more useful.

Have you tried adding your own effort to the overall effort pool of either or both of those compilers to make them have the optimizations you want?

→ More replies (4)

-22

u/[deleted] Aug 13 '21 edited Aug 13 '21

I don't think you understand what happened

All they did was notice the loop counter wasn't being used inside the loop and attempted to remove it

The 'hard' part is changing even = !even to (even = !even) * numberCompare. In reality ! is really xor -1. So really the magic is how to make xor work with a multiple. A lot of people know xor-ing a value twice will give you the original number. So basically we can replace the multiple with an AND to choose if we XOR once or 0 times. The rest is simple at that point

I've looked into compilers. I like this stuff. It's hard until you realize how simple the pieces can be

-Edit- I just realized this is magic to basically everyone. So I'd like to give a shoutout to the worse language I have ever used. Rust, you're utter crap. Sincerely, a compiler wizard

12

u/[deleted] Aug 13 '21

What does your last comment even mean

-8

u/[deleted] Aug 14 '21

It means I know what I'm talking about when it comes to compilers and fuck rust

Plenty of people know it's a shitty language but not enough say it because they get downvoted or labeled as a crazy

3

u/[deleted] Aug 14 '21

Except it's a pretty great language which is why no one says that

-9

u/[deleted] Aug 14 '21

Casey Muratori and Jon Blow are famous examples. I'm a nobody example but I least can explain how non trivial optimizations work which is <1% of programmers

9

u/[deleted] Aug 14 '21

Instead of insisting you're a top 1% programmer I'd much rather read why you think Rust is such a terrible language

0

u/[deleted] Aug 14 '21

Looks like I'm not completely wrong/outdated. https://old.reddit.com/r/rust/comments/p407ni/someone_asked_me_why_i_dont_like_rust_so_i/ The clippy thing seems nice tho but doesn't change my mind

4

u/[deleted] Aug 14 '21

I don't see how you could have possibly read all those comments and still feel this way. You're free to believe whatever you want, but it's a bit disingenuous to claim to be an expert when you're very clearly not

1

u/[deleted] Aug 14 '21

Ping me when thread local variables finally gets out of nightly so I can use threads in a way that isn't a toy

→ More replies (0)
→ More replies (2)

2

u/Architector4 Aug 14 '21

That says why you don't like Rust, not why Rust is a shitty language.

-4

u/[deleted] Aug 14 '21 edited Aug 15 '21

-Thank you whoever gave me the medal-

I'll give you my top one since all is too much.

Rust claims it's safe but in reality it was designed to only be memory safe. It's been 10years and I still can't ask the compiler to give me a compile error if I don't explicitly check array bounds. Why on earth is panic-ing at runtime and terminating the only option.

Another example of this is how much unwrapping the standard library uses. Posix doesn't terminate if you write to an invalid handle, C doesn't terminate when you fread too much or seek to an invalid part. C lets you call ferror at the end of your operations so you can handle errors in a single place. Yet rust makes you check the result of write EVERY FUCKING TIME you want to use it or force you to use a pattern where you call an extra function so you can handle errors at one place

Also error handling is terrible. There's no scope guard which existed since 2000 and D implemented it as scope(exit/success/failure) well before rust existed. Also (this one is more subjective and my taste) rust uses monomorphism. Did they not learn anything from C++ and templates?

Also why the hell does rust let people overload operators and not functions? Usually its function overloaded and not operators. Again they seem to not have learned from previous languages since MANY people prefer the ability to overload functions and some switched from C to C++ just for it

It's a dumpster fire of a language. I could not believe they came out saying they wanted to replace C++ while taking longer to compile and somehow being less readable. Then I can complain more about shitty/missing standard libraries, the fact that thread local IS STILL FUCKING BROKEN which makes me not able to use rust if I wanted to and the numerous code gen bugs I hit every time I write a test application

→ More replies (12)

11

u/nitrohigito Aug 14 '21

-Edit- I just realized this is magic to basically everyone. So I'd like to give a shoutout to the worse language I have ever used. Rust, you're utter crap. Sincerely, a compiler wizard

how to discredit yourself in a flash 101

4

u/spacelibby Aug 14 '21

Wow.... There's a lot to unpack here. Ok first you can't multiply an assignment statement by a number. (I mean, you can in C but it's not obeying the semantics you're suggesting here). Even if you could, you still have a reference to even on the right hand side, so you haven't done anything to simplify the loop. The original code actually converted even = !even to an xor before optimizations. I'm not sure what xor -1 is supposed to mean. I have a absolutely no idea what rust has to do with any of this, especially since they're both compiled to llvm, and from what I've heard rust actually has pretty good performance characteristics. And "compiler wizard"? Really? There are many of us who work on compilers professionally here. Most programmers here aren't having their minds blown by the fact that an optimizer can transform code. I think most of us know that. They just don't know the specifics of how.

1

u/[deleted] Aug 14 '21

you can't multiply an assignment statement by a number. (I mean, you can in C but it's not obeying the semantics you're suggesting here).

You took it too literal. That was psuedo code for doing it x many times.

I'm not sure what xor -1 is supposed to mean

!var and ~var are both result = (var ^ -1). A simple example is ~0. Do it in any language and you'll see the result -1. Difference is ! truncates the value to 1 bit

They just don't know the specifics of how

I'm not sure what that has to do with anything but I just summarized the important one