According to the SPIR-V 1.3 specification, as I understand it, all instructions are segmented in 4 byte words. This is different from traditional byte code IR languages like Pythons byte code and Java's JVM bytecode, which if I understand correctly literally segment operations as individual bytes. I've even heard people offhandedly talk about how this is specifically a good thing ("word code"), but never elaborate on why. Why did Khronos group decide to go down this route for its SPIR-V definition, instead of using a single byte format, or some other arbitrary size?
1 Answers
There are many reasons, but probably the biggest is that it solves the endian issue in a simple, efficient, and extensible fashion.
SPIR-V is defined to be little-endian-encoded. So on a big-endian system, step 1 has to be converting the data to the proper endian. In a more byte-oriented assembly style, you would have to read and interpret each opcode, then figure out which operands have to be byte-swapped. Basically, endian-swapping requires interpreting the data. With SPIR-V, you just transpose every 4 bytes: a simple and efficient loop.
Plus, SPIR-V is extensible; extensions can add to the set of opcodes. In a byte-oriented assembly, you would need to be able to understand every extension opcode in order to do endian conversion. With SPIR-V, you don't care if you can understand all of the opcodes; just transpose every 4 bytes and you're done. So you can write a tool that can at least read SPIR-V assembly even if it doesn't understand all of the opcodes.
This makes SPIR-V a bit bulkier compared to other byte-codes, but it's a lot easier to chew.

- 449,505
- 63
- 781
- 982
-
What do you mean by transpose every 4 bytes, swapping the byte order of every 4 bytes? so [0, 1, 2, 3] [4, 5, 6, 7] becomes [3, 2, 1, 0], [7, 6, 5, 4]? – Krupip Apr 20 '18 at 15:22
-
@snb: Yes; that's what "transpose" means ;) It's what you do if you're reading a file that's in a different endian from your native endian. SPIR-V just makes that a simple loop. – Nicol Bolas Apr 20 '18 at 15:28
-
Ah, ok that makes sense. I'm going to wait to accept the answer since you answered like 30 seconds after I posted my question though, so I want to wait and give other people incentive to answer as well. – Krupip Apr 20 '18 at 15:29
-
@snb generally if Nicol answers a question on Vulkan, OpenGL, GLSL, etc... you can just mark it accepted right away. – Jherico Apr 20 '18 at 21:29
-
@Jherico Lol yeah I noticed that, we need more heroes like Nicol – Krupip Apr 21 '18 at 03:06