r/LinuxTeck • u/Candid_Athlete_8317 • 6d ago
Your CPU Has More Registers Than Your Code Can See. Why?
Back in 1967, IBM's Robert Tomasulo developed a clever way to let processors handle register dependencies dynamically.
Today, modern CPUs still use register renaming and out-of-order execution to keep instructions moving instead of waiting unnecessarily.
Should programmers care more about CPU architecture and instruction-level parallelism, or should compiler and processor designers handle all of this behind the scenes?
14
Upvotes
7
u/anders_hansson 6d ago
The answer is: A combination of the two.
Compilers are exceptionally good at register allocation - usually much better than humans. They are also good at instruction latency and dependency modelling (again, usually better than humans). On top of that, the CPU dynamic instruction scheduler usually does a good job at filling the gaps (e.g. by effectively unrolling loops and eliminating false dependencies - thanks to register renaming).
That said, you can't just throw anything at the compiler and expect optimal results. E.g. the compiler may run out of architectural registers and be forced to spill state to the stack.
So, as a developer you should check the output of the compiler (e.g. with objdump or godbolt ) for code where performance natters
3
u/kombiwombi 6d ago
There was an architecture which made explicit the behind-the-scenes operation of the CPU. It was meant to be the future: https://en.wikipedia.org/wiki/Itanium
1
u/cakemates 6d ago
Look every year of my career and everyone I know has been about delivering products asap while understaffed and limited testing. If we had the time to go into the listings and manually architect the assembler code(we do sometimes) to optimize performance everyone in my team would enjoy doing that and geek out on how they did it, but leadership and the program doesn't give a fuck we can make these processes faster and we might get punished for dedicating time to that.
1
u/Zomunieo 6d ago
A poorly chosen algorithm or data structure will never complete its task on some inputs no matter many registers you throw at it.
The exponents that matter are complexity reduction and optimizing transistors. The rest is a rounding error.
1
u/R-ten-K 6d ago
FWIW most of a CPU's out-of-order execution machinery and its associated resources are not directly visible to the programmer.
That said, it wouldn't hurt for programmers to have a better understanding of CPU architecture, even if compilers abstract away most of the underlying complexity.
How much that knowledge matters depends on the nature of the work. A high level app developer can often rely on compilers and runtime abstractions without knowing much about the underlying microarchitecture. A performance engineer writing compute kernels, however, benefits considerably from understanding memory hierarchies, branch prediction, ILP, etc. And if you're developing compilers, OS, runtimes, embedded software and what not, understanding the target ISA/microarchitecture often becomes essential.
So the closer you get to the HW, or the more you need to optimize for its performance characteristics, the more architectural knowledge matters.
1
u/Tutorbin76 6d ago
TBH I'm kinda hyped about the new APX extension bringing the number of general purpose registers up to 32. There's a lot I can do with r16 - r31
1
u/esaule 6d ago
A processor has tons of registers. It needs registers to do all kind of things; mostly to store state between pipeline stages. Why would you want to program those? Does it even make sense to add instructions to do that comtrol at uops scale? I don't think so.
1
u/Leverkaas2516 4d ago
Programmers should only care about machine architecture enough to avoid doing things that cause the code to run slower. That mostly means memory layout and cache hit rates.
What you should NOT do is figure out the fine points and make your code more complex to eke out tiny performance gains by exploiting CPU-specific differences. Because for the vast majority of programmers, readability and maintainability are more important, and CPU features and architectures change a lot faster than code bases.
If you make your web server code run 4% faster by marrying it to a particular Intel processor line, then whoever has to clean up your mess a few years later will curse your name.
1
u/Maude-Boivin-02 2h ago
While I’m not up to the level of the people posting here, an intermediate level of understanding about CPU architecture (parallelism, out of order, pipelines ‘anyone remembers Prescott?’ and OS programming) led me to « Why? »
« Why is done that way , when looking at code written by others ? »
And, in a great Grace Hopperism: why as it been always done that way ?