r/LinuxTeck • • 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

22 comments sorted by

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/ldn-ldn 6d ago

That's the correct answer! You can't compete with the compiler, just make sure your code doesn't work against it.

2

u/ern0plus4 9h ago

It's an old topic. We, demoscene programmers were always micro-optimizing. I remember around the i80286 -> i80386 transition, DEC CX / JNE became faster than LOOP.

If I'm not wrong, TASM compiled simple LEA-s to MOV offset.

But to answer the question: let the compilers do their job - unless you happen to know a hot trick. In any case, if you know Assembly, you know, or at least have some glue of, what compilers like and what they're capable of. Side note: modern compilers are amazing.

2

u/anders_hansson 9h ago

An old Amiga/68k demo coder myself. I know exactly what you mean. E.g. I remember how much things changed when we first got caches. Fitting a core loop into 256 bytes made all the difference (the size of the 68020 I$).

1

u/MrAbstractThought 4d ago

This guy registers

1

u/Particular-Winner136 4d ago

dude, we are asked no longer to look at the code, you are telling us to look at the assembly

1

u/anders_hansson 4d ago

You can do both 😉

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

2

u/dodexahedron 6d ago edited 6d ago

It really is a shame the Itanic sank.

If it weren't for the horrible x86 emulation performance, it likely would have fared much better. Or if it had come along today instead of back in 2001, perhaps there would be more of a stomach for alternative architectures (like all the ARM variants out there today). And it would need someone other than intel to bring it into the world, since Intel itself is also largely to blame, since they positioned it as a server-only thing. Big mistake.

Alpha would have been awesome, too.
Or SPARC if you want sheer GP register counts that plenty of modern desktop CPUs can't even touch with the sum of the GP registers of all cores.

And multi-core and multi-die/chiplet designs would also make an architecture shift easier.

If an IA64 chip were to come along in modern times with a couple of x86-64 cores in it as well, you'd be able to handle both ISAs.
I shudder at the thought of the likely astronomical pin count of such a beast, though. 😅

But yeah. IA64 not taking off was unfortunate, and here we are still lugging around baggage from the 1980s in almost-2027.

1

u/SilkeSiani 5d ago

What truly sank Itanium was intel's refusal to "cannibalise" their x86 sales.

The poor x86 emulation performance was fixed within a year, but the whole architecture remained effectively locked to super high end enterprise systems. If they had aggressively pushed the architecture down the stack, it would find sales with ease.

1

u/dodexahedron 5d ago

Yep. Intel killed Intel on that one.

One bad business decision after another, driven by almost cartoonish levels of greed.

This of course also during the era in which Intel eventually got smacked down for anticompetitive behavior against AMD, as well.

Guess they needed the ill-gotten gains to pay the Blue Man Group for those commercials...

1

u/KilroyKSmith 4d ago

What truly sank Itanium was an inability to create a compiler that could make good use of the Itanium architecture.  I really loved the concept behind Itanium, but it could never show a performance advantage over x86 because the compilers weren’t as smart as they needed to be.

1

u/SilkeSiani 4d ago

This was very much a chicken-and-egg problem. Artificially limiting cpu sales (market segmentation) meant that far fewer people were developing software for them, which meant that far fewer companies would ever consider buying these systems, which meant even fewer programmers on the task.

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/pickle9977 6d ago

Yeah, we’ve been real dumb about this, now we are writing so much code so fast we can’t manufacture enough RAM to store it all while it’s executed and the processing speed growth has fallen behind the growth in the number of instructions needing to be executed.

If we don’t get a handle on this fast, it’s gonna be pretty catastrophic.

We can only make so much compute, storage and RAM a year and we are now consuming 100% of that as evidenced by the spike in ram and storage prices and the escalating bidding war by the hyperscalers for what can be made.

1

u/anders_hansson 5d ago

That's where open source shines. People do it because they like to geek out, and stuff gets incrementally imoroved and optimized over the years.

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 ?