remove_unused_interface_variables_test.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright (c) 2021 ZHOU He
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "test/opt/assembly_builder.h"
  15. #include "test/opt/pass_fixture.h"
  16. #include "test/opt/pass_utils.h"
  17. namespace spvtools {
  18. namespace opt {
  19. namespace {
  20. using RemoveUnusedInterfaceVariablesTest = PassTest<::testing::Test>;
  21. static const std::string expected = R"(OpCapability Shader
  22. OpMemoryModel Logical GLSL450
  23. OpEntryPoint Fragment %_Z5func1v "_Z5func1v" %out_var_SV_TARGET
  24. OpEntryPoint Fragment %_Z5func2v "_Z5func2v" %out_var_SV_TARGET_0
  25. OpExecutionMode %_Z5func1v OriginUpperLeft
  26. OpExecutionMode %_Z5func2v OriginUpperLeft
  27. OpSource HLSL 630
  28. OpName %type_cba "type.cba"
  29. OpMemberName %type_cba 0 "color"
  30. OpName %cba "cba"
  31. OpName %out_var_SV_TARGET "out.var.SV_TARGET"
  32. OpName %out_var_SV_TARGET_0 "out.var.SV_TARGET"
  33. OpName %_Z5func1v "_Z5func1v"
  34. OpName %_Z5func2v "_Z5func2v"
  35. OpDecorate %out_var_SV_TARGET Location 0
  36. OpDecorate %out_var_SV_TARGET_0 Location 0
  37. OpDecorate %cba DescriptorSet 0
  38. OpDecorate %cba Binding 0
  39. OpMemberDecorate %type_cba 0 Offset 0
  40. OpDecorate %type_cba Block
  41. %int = OpTypeInt 32 1
  42. %int_0 = OpConstant %int 0
  43. %float = OpTypeFloat 32
  44. %v4float = OpTypeVector %float 4
  45. %type_cba = OpTypeStruct %v4float
  46. %_ptr_Uniform_type_cba = OpTypePointer Uniform %type_cba
  47. %_ptr_Output_v4float = OpTypePointer Output %v4float
  48. %void = OpTypeVoid
  49. %14 = OpTypeFunction %void
  50. %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
  51. %cba = OpVariable %_ptr_Uniform_type_cba Uniform
  52. %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
  53. %out_var_SV_TARGET_0 = OpVariable %_ptr_Output_v4float Output
  54. %_Z5func1v = OpFunction %void None %14
  55. %16 = OpLabel
  56. %17 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0
  57. %18 = OpLoad %v4float %17
  58. OpStore %out_var_SV_TARGET %18
  59. OpReturn
  60. OpFunctionEnd
  61. %_Z5func2v = OpFunction %void None %14
  62. %19 = OpLabel
  63. %20 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0
  64. %21 = OpLoad %v4float %20
  65. OpStore %out_var_SV_TARGET_0 %21
  66. OpReturn
  67. OpFunctionEnd
  68. )";
  69. TEST_F(RemoveUnusedInterfaceVariablesTest, RemoveUnusedVariable) {
  70. const std::string text = R"(OpCapability Shader
  71. OpMemoryModel Logical GLSL450
  72. OpEntryPoint Fragment %_Z5func1v "_Z5func1v" %out_var_SV_TARGET %out_var_SV_TARGET_0
  73. OpEntryPoint Fragment %_Z5func2v "_Z5func2v" %out_var_SV_TARGET %out_var_SV_TARGET_0
  74. OpExecutionMode %_Z5func1v OriginUpperLeft
  75. OpExecutionMode %_Z5func2v OriginUpperLeft
  76. OpSource HLSL 630
  77. OpName %type_cba "type.cba"
  78. OpMemberName %type_cba 0 "color"
  79. OpName %cba "cba"
  80. OpName %out_var_SV_TARGET "out.var.SV_TARGET"
  81. OpName %out_var_SV_TARGET_0 "out.var.SV_TARGET"
  82. OpName %_Z5func1v "_Z5func1v"
  83. OpName %_Z5func2v "_Z5func2v"
  84. OpDecorate %out_var_SV_TARGET Location 0
  85. OpDecorate %out_var_SV_TARGET_0 Location 0
  86. OpDecorate %cba DescriptorSet 0
  87. OpDecorate %cba Binding 0
  88. OpMemberDecorate %type_cba 0 Offset 0
  89. OpDecorate %type_cba Block
  90. %int = OpTypeInt 32 1
  91. %int_0 = OpConstant %int 0
  92. %float = OpTypeFloat 32
  93. %v4float = OpTypeVector %float 4
  94. %type_cba = OpTypeStruct %v4float
  95. %_ptr_Uniform_type_cba = OpTypePointer Uniform %type_cba
  96. %_ptr_Output_v4float = OpTypePointer Output %v4float
  97. %void = OpTypeVoid
  98. %14 = OpTypeFunction %void
  99. %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
  100. %cba = OpVariable %_ptr_Uniform_type_cba Uniform
  101. %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
  102. %out_var_SV_TARGET_0 = OpVariable %_ptr_Output_v4float Output
  103. %_Z5func1v = OpFunction %void None %14
  104. %16 = OpLabel
  105. %17 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0
  106. %18 = OpLoad %v4float %17
  107. OpStore %out_var_SV_TARGET %18
  108. OpReturn
  109. OpFunctionEnd
  110. %_Z5func2v = OpFunction %void None %14
  111. %19 = OpLabel
  112. %20 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0
  113. %21 = OpLoad %v4float %20
  114. OpStore %out_var_SV_TARGET_0 %21
  115. OpReturn
  116. OpFunctionEnd
  117. )";
  118. SinglePassRunAndCheck<RemoveUnusedInterfaceVariablesPass>(text, expected,
  119. true, true);
  120. }
  121. TEST_F(RemoveUnusedInterfaceVariablesTest, FixMissingVariable) {
  122. const std::string text = R"(OpCapability Shader
  123. OpMemoryModel Logical GLSL450
  124. OpEntryPoint Fragment %_Z5func1v "_Z5func1v"
  125. OpEntryPoint Fragment %_Z5func2v "_Z5func2v"
  126. OpExecutionMode %_Z5func1v OriginUpperLeft
  127. OpExecutionMode %_Z5func2v OriginUpperLeft
  128. OpSource HLSL 630
  129. OpName %type_cba "type.cba"
  130. OpMemberName %type_cba 0 "color"
  131. OpName %cba "cba"
  132. OpName %out_var_SV_TARGET "out.var.SV_TARGET"
  133. OpName %out_var_SV_TARGET_0 "out.var.SV_TARGET"
  134. OpName %_Z5func1v "_Z5func1v"
  135. OpName %_Z5func2v "_Z5func2v"
  136. OpDecorate %out_var_SV_TARGET Location 0
  137. OpDecorate %out_var_SV_TARGET_0 Location 0
  138. OpDecorate %cba DescriptorSet 0
  139. OpDecorate %cba Binding 0
  140. OpMemberDecorate %type_cba 0 Offset 0
  141. OpDecorate %type_cba Block
  142. %int = OpTypeInt 32 1
  143. %int_0 = OpConstant %int 0
  144. %float = OpTypeFloat 32
  145. %v4float = OpTypeVector %float 4
  146. %type_cba = OpTypeStruct %v4float
  147. %_ptr_Uniform_type_cba = OpTypePointer Uniform %type_cba
  148. %_ptr_Output_v4float = OpTypePointer Output %v4float
  149. %void = OpTypeVoid
  150. %14 = OpTypeFunction %void
  151. %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
  152. %cba = OpVariable %_ptr_Uniform_type_cba Uniform
  153. %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
  154. %out_var_SV_TARGET_0 = OpVariable %_ptr_Output_v4float Output
  155. %_Z5func1v = OpFunction %void None %14
  156. %16 = OpLabel
  157. %17 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0
  158. %18 = OpLoad %v4float %17
  159. OpStore %out_var_SV_TARGET %18
  160. OpReturn
  161. OpFunctionEnd
  162. %_Z5func2v = OpFunction %void None %14
  163. %19 = OpLabel
  164. %20 = OpAccessChain %_ptr_Uniform_v4float %cba %int_0
  165. %21 = OpLoad %v4float %20
  166. OpStore %out_var_SV_TARGET_0 %21
  167. OpReturn
  168. OpFunctionEnd
  169. )";
  170. SinglePassRunAndCheck<RemoveUnusedInterfaceVariablesPass>(text, expected,
  171. true, true);
  172. }
  173. } // namespace
  174. } // namespace opt
  175. } // namespace spvtools