I'm currently considering porting some code that I have from OpenGL/GLSL to Vulkan/SPIR-V, and part of that code generates GLSL at runtime, so I'll have to generate SPIR-V instead. What I'm wondering is how I should relate to optimizations in the generated SPIR-V.
Particularly, I can't really find any information on what kind of expectations I should have on the driver's compiler. Should I expect it to do aggressive optimizations on its own and thus try to keep the SPIR-V code clean and keep as much "original intent" as possible in it for the compiler to look at? Or should I expect it to do fairly simplistic code generation, and try to do as aggressive optimization as possible when generating SPIR-V?
Just perhaps for particular examples, which of these kinds of things should I do when generating SPIR-V?
- Eliminate redundant stores-then-loads of local variables?
- Loop unrolling or peeling / function inlining?
- Constant propagation / common subexpression elimination?
- Keep as much as possible in SSA form instead of loads and stores to local variables?
My very naïve expectation is that the compiler will want to adapt its optimization passes to the particulars of the hardware and that I should therefore try and keep my SPIR-V code clean and high-level, where such things as loop unrolling would destroy information and keep the driver from deciding on its own unrolling, but I'm really just guessing without any real information.