8
$\begingroup$

I have played around with CPU assembly programming like Nasm, Tasm or Masm, but I'm really curious to know how GPU works now. However, i'm quite confused when I look on internet. I've heard about Cuda and OpenCL, but it is not what I'm looking for. I'd like to know how GPUs instructions are in RAM... What are the Nasm and Masm for most GPUs? What is the x86 or Z80 of GPUs (What are the different families of GPU)? Do you know a constructor opcodes reference manual? I think I really need something to compare between the two Processing Units to make it clear, because GPU assembly programming seems to be an even harder subject to learn from on internet that CPU asm programming. I've also read that "NVIDIA never released details on the instructions actually understood by their hardware", but it seems pretty surprising to me. Full post there: https://stackoverflow.com/questions/4660974/how-to-create-or-manipulate-gpu-assembler?newreg=e31519279ce949f087df6322dbf2bf4d

Thanks for your help!

$\endgroup$
11
$\begingroup$

You're tilting at windmills trying to learn "GPU assembly", and it's due to the differences between how CPUs and GPUs are made and sold.

Each CPU has what's called an instruction set architecture, for example x86 or ARMv8. The instruction set is the interface between the user of the CPU (i.e. the programmer) and the chip. The chip designer publishes the details of the instruction set so that compiler vendors can write compilers to target that instruction set. Any CPU that uses that instruction set can run the same binaries. (Extensions like SSE make that slightly untrue, but they only add new instructions: they don't change the structure of the binary.) When the vendor creates a new processor family, it could have completely different micro-architecture internally, but the same instruction set.

GPUs are not like this at all. For best efficiency, the instruction set is usually closely tied to the micro-architecture of the CPU. That means each new family of GPUs has a new instruction set. GPUs can't run binaries made for different GPUs. For this reason, the instruction set usually isn't published: instead, your interface to the GPU is the driver published by the vendor for each graphics API (OpenGL, Vulkan, DirectX, &c.). This is why the graphics APIs have functions to take the source code of a shader and run it: the compiled shader only runs on the same model or family of GPU it was compiled for.

The closest you get to GPU assembly language is SPIR-V. This is an industry-standard intermediate representation, which GPU vendors are starting to support. It's like a GPU equivalent of LLVM's intermediate representation. It allows you to do the parsing and source optimization parts of compilation up-front to get a SPIR-V file, and then the GPU driver only needs to compile that to the GPU's instruction set at load time.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks for your answer! It was rally helpful to me :). About the part with the shader example, I'm not sure if I understood well, but I think you meant that the shader, instead of being compiled and assembled, is only compiled and not assembled because instruction sets are different for each GPU, even from the same "family". $\endgroup$ – sebastien finor Jul 16 '18 at 21:25
  • 1
    $\begingroup$ @sebastienfinor What I'm trying to say about that is that there isn't necessarily an intermediate "assembly language" representation in the compilation process. $\endgroup$ – Dan Hulme Jul 17 '18 at 8:17
  • $\begingroup$ When programmable shaders were first being introduced they were programmed using a language that was very "assembly language" like. It can still be found in books and papers of the time. Some of the game programming gems books have articles that show examples. The original "orange book" has some in it as well. It is fun to look at because you will see early versions of functions like fma. It also can fill in some of those "why the heck is this done this way" questions. AND, you will quickly see that different vendors had different "languages". $\endgroup$ – pmw1234 Jul 23 at 0:19
  • $\begingroup$ There is in fact still multiple layers, but programmers don't really have access to those layers. For example GLSL compiles to SPIR-V (as already mentioned) And those are really the only layers of the language you have access to as a graphics programmer. But inside the driver code you will find mountains of vendor specific code...when this code actually gets inside the gpu it is further translated into micro code, micro code is where it all really happens, beyond that it is just a sea of numbers. Micro code is how vendors "fix" hardware bugs. By programming around them. $\endgroup$ – pmw1234 Jul 23 at 0:32
4
$\begingroup$

What you've read is correct. In general, GPU vendors do not release lists of machine instructions for their GPUs.

That said, you can do something similar to assembly programming on the GPU by using OpenGL ARB Assembly Language. It allows you to program in assembly style, writing 1 opcode and its operands per line. While this is not quite the same as writing assembly language for the CPU, it's as close as your likely to get. I believe it's deprecated in modern OpenGL (though not positive). It still worked as of OpenGL 2.1.

| improve this answer | |
$\endgroup$
  • $\begingroup$ I'll take a look. Thanks for the infos and the link :). $\endgroup$ – sebastien finor Jul 16 '18 at 21:28
4
$\begingroup$

As others have said, GPUs expose a higher-level language, which allows multiple different architectures i.e. different vendors and different GPU generations, to all support the same applications.

If, however, you're still curious, documentation for the PowerVR Series 6 instruction set is available - my favourite opcode being "FRED".

Note that instructions sets can change from generation to generation so this is mainly for illustrative purposes.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.