LunarG is pleased to announce the release of new options in spirv-opt designed to reduce SPIR-V size.
We have introduced passes to spirv-opt that use classic code optimization techniques to address code inefficiencies in a SPIR-V module. Combined with spirv-remap , which we use to remove module-level dead types and functions, these passes can reduce raw SPIR-V size by over 60%, and bring SPIR-V sizes within 40% of DX Byte Code.
Since the inception of SPIR-V, there has been an interest in optimizing its code, particularly with the goal of reducing its size. Understandably, the raw SPIR-V emitted from the frontend glslangValidator is verbose, particularly with regard to function scope variables and their loads and stores. Significant opportunities for size reduction come from 1) eliminating these loads and stores, 2) eliminating dead code due to branches with constant conditionals and 3) eliminating replicated accesses to uniform variables, including images and samplers.
New spirv-opt passes added by LunarG use standard code optimization techniques (Inlining, Store-Load Elimination, Dead Code Elimination, Dead Branch Elimination, etc.) to remove functionally unnecessary instructions at the function level.
To achieve ultimate size reduction, these passes should be followed by applying spirv-remap with the –dce all and –strip all options to remove module level dead and debug instructions. Moving this capability from spirv-remap to spirv-opt is left for future work.
Expected Benefits in SPIR-V
Over a small, random sample set of HLSL shaders (vertex and fragment), the following size reductions were achieved:
glslang – 188K
glslang + spirv-opt + spirv-remap – 77K
DX Byte Code – 56K
These passes have been initially designed to work with SPIR-V modules for graphics APIs such as Vulkan and OpenGL, which use logical addressing. Modules with physical addressing, such as those for the OpenCL API will regrettably not derive much benefit at this time. The scope of these passes was restricted to speed implementation and delivery of their benefits to the graphics community. The structured control flow of shaders and the simplified memory accesses of logical addressing simplify these passes. Optimization of kernels is left for future work.
Read the white paper, by Greg Fischer of LunarG, titled SPIR-V Shader Size Reduction Using spirv-opt_v1.1 for more details about how to use these optimizations. Learn about the specific passes and how they can be used so you can best decide how to reduce the size of SPIR-V shaders.