The SPIR-V optimiser spirv-opt includes multiple options with the purpose of removing redundant values, including the following.
--eliminate-dead-const - Eliminate dead constants.
--remove-duplicates - Removes duplicate types, decorations, capabilities and extension instructions.
--unify-const - Remove the duplicated constants.
However, none of the above related optimisations are able to remove a redundant specialisation constant, where 'redundant' means it shares the same SpecId and default value with a separately declared <id>. The following assembly is of the module test.spvasm.
The two entry points add two specialisation constants together, and store the result into a storage buffer. ep1 adds spec1 and spec3, and ep2 adds spec2 and spec3. All specialisation constants have the same default value, and spec1 and spec2 have the same SpecId, making spec2 redundant. spec3 is included as an example of what would happen if spec2 was removed due to its redundancy, and was hence replaced or combined with spec1.
If you assemble this module into SPIR-V, apply the optimisations listed above, and then disassemble back into assembly as follows:
The resultant module test2.spvasm will be equivalent to the original module test.spvasm, including containing the redundant specialisation constant. Both test.spv and test2.spv pass the SPIR-V validator, and all SPIRV-Tools are of the following --version.
The SPIR-V optimiser
spirv-opt
includes multiple options with the purpose of removing redundant values, including the following.--eliminate-dead-const
- Eliminate dead constants.--remove-duplicates
- Removes duplicate types, decorations, capabilities and extension instructions.--unify-const
- Remove the duplicated constants.However, none of the above related optimisations are able to remove a redundant specialisation constant, where 'redundant' means it shares the same SpecId and default value with a separately declared
<id>
. The following assembly is of the moduletest.spvasm
.The two entry points add two specialisation constants together, and store the result into a storage buffer.
ep1
addsspec1
andspec3
, andep2
addsspec2
andspec3
. All specialisation constants have the same default value, andspec1
andspec2
have the same SpecId, makingspec2
redundant.spec3
is included as an example of what would happen ifspec2
was removed due to its redundancy, and was hence replaced or combined withspec1
.If you assemble this module into SPIR-V, apply the optimisations listed above, and then disassemble back into assembly as follows:
The resultant module
test2.spvasm
will be equivalent to the original moduletest.spvasm
, including containing the redundant specialisation constant. Bothtest.spv
andtest2.spv
pass the SPIR-V validator, and all SPIRV-Tools are of the following--version
.