r/AskComputerScience • u/thingsiliketothink • Jul 30 '20
Do GPUs have different system architecture (instruction sets) like CPUs?
I am familiar with different instruction sets on CPU like arm, x86 and PowerPC. But what about GPUs? Are they all the same? Are there cases in which I have to recompile my code for specific GPUs?
12
u/Ragingman2 Jul 30 '20
When talking to GPUs, you typically do so through graphics libraries such as OpenGL, Vulkan, or DirectX. The company that sold you the graphics card will also provide a graphics driver that can instructions in these graphics libraries into instructions to send to your GPU.
The instructions that your graphics driver generate are in a proprietary protocol known only to a few wizards at NVIDIA and AMD who actually understand how GPUs work on a low level. Us mere mortals don't get to look under the hood at these instructions.
Interestingly, many modern CPUs operate the same way. They take your x86 code and internally translate it into something better, faster, and chip specific.
6
u/lneutral Jul 30 '20
That last part is surprising to me! I'd worked with older chips' assembly and even with newer ISAs like NEON (via intrinsics in higher-level languages), but I never even questioned whether the silicon was really still using x86 instructions.
Got any suggestions for further reading?
6
u/Poddster Jul 30 '20
Google x86 microcode
The technique has been around for years. Even the first ARM used microcode.
5
u/claytonkb Jul 30 '20 edited Jul 30 '20
From a high-level perspective, a modern x86 CPU is a custom (per-generation), out-of-order, speculative, microcode RISC engine with an x86 decoder bolted on the front-end. As for what the distinction is between ordinary (macro) code and microcode, you cannot use ordinary code to short power to ground and smoke the CPU. Microcode (in principle) has no such protections, that is, the microcode can directly clobber hardware-level signals and registers, leading to any number of problems, including (in the worst-case) shorts. So, the reason the CPU manufacturer does not expose the microcode interface is not because they are mean and want to keep the high-performance capabilities all to themselves. Rather, they do not expose the microcode interface because using that interface without lighting the CPU on fire requires knowing the internal circuitry of the CPU (which is their intellectual property).
With that said, the bulk of all ordinary CPU instructions that issue through the pipeline (in execution trace order) are decoded 1-to-1 to a corresponding micro-operation. This means that almost all the instructions in the software on your computer are executing at the fastest possible speed that the CPU is capable of. Obviously, that is by design. It's mainly features like interrupts, exceptions, task-switching, paging, OS protection and virtualization that use complex microcode that is not accessible to the outside world.
3
u/Gnash_ Jul 30 '20
https://en.wikichip.org/wiki/micro-operation
WikiChip is a gold mine for these sorts of things, read their pages on specific architectures, like Haswell or Zen, for a deep dive on how it works: https://en.wikichip.org/wiki/intel/microarchitectures/haswell_(client)#Core
1
u/visvis Jul 30 '20
It gets even crazier than that, the CPU will even reorder the microinstructions and speculatively execute them if it's unsure whether they will run later. Sometimes the speculative execution needs to be aborted because it turns out those instructions are not needed, but you can still see the impact on the caches. This is why we have vulnerabilities like Spectre and Meltdown.
16
u/khedoros Jul 30 '20
Nope.
Shaders get recompiled in every game or multimedia program that is written with a programmable graphics pipeline. They need to be rebuilt for new hardware, or even for a change in driver version.