assembly_builder_test.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // Copyright (c) 2016 Google Inc.
  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 AssemblyBuilderTest = PassTest<::testing::Test>;
  21. TEST_F(AssemblyBuilderTest, MinimalShader) {
  22. AssemblyBuilder builder;
  23. std::vector<const char*> expected = {
  24. // clang-format off
  25. "OpCapability Shader",
  26. "OpCapability Float64",
  27. "%1 = OpExtInstImport \"GLSL.std.450\"",
  28. "OpMemoryModel Logical GLSL450",
  29. "OpEntryPoint Vertex %main \"main\"",
  30. "OpName %void \"void\"",
  31. "OpName %main_func_type \"main_func_type\"",
  32. "OpName %main \"main\"",
  33. "OpName %main_func_entry_block \"main_func_entry_block\"",
  34. "%void = OpTypeVoid",
  35. "%main_func_type = OpTypeFunction %void",
  36. "%main = OpFunction %void None %main_func_type",
  37. "%main_func_entry_block = OpLabel",
  38. "OpReturn",
  39. "OpFunctionEnd",
  40. // clang-format on
  41. };
  42. SinglePassRunAndCheck<NullPass>(builder.GetCode(), JoinAllInsts(expected),
  43. /* skip_nop = */ false);
  44. }
  45. TEST_F(AssemblyBuilderTest, ShaderWithConstants) {
  46. AssemblyBuilder builder;
  47. builder
  48. .AppendTypesConstantsGlobals({
  49. // clang-format off
  50. "%bool = OpTypeBool",
  51. "%_PF_bool = OpTypePointer Function %bool",
  52. "%bt = OpConstantTrue %bool",
  53. "%bf = OpConstantFalse %bool",
  54. "%int = OpTypeInt 32 1",
  55. "%_PF_int = OpTypePointer Function %int",
  56. "%si = OpConstant %int 1",
  57. "%uint = OpTypeInt 32 0",
  58. "%_PF_uint = OpTypePointer Function %uint",
  59. "%ui = OpConstant %uint 2",
  60. "%float = OpTypeFloat 32",
  61. "%_PF_float = OpTypePointer Function %float",
  62. "%f = OpConstant %float 3.1415",
  63. "%double = OpTypeFloat 64",
  64. "%_PF_double = OpTypePointer Function %double",
  65. "%d = OpConstant %double 3.14159265358979",
  66. // clang-format on
  67. })
  68. .AppendInMain({
  69. // clang-format off
  70. "%btv = OpVariable %_PF_bool Function",
  71. "%bfv = OpVariable %_PF_bool Function",
  72. "%iv = OpVariable %_PF_int Function",
  73. "%uv = OpVariable %_PF_uint Function",
  74. "%fv = OpVariable %_PF_float Function",
  75. "%dv = OpVariable %_PF_double Function",
  76. "OpStore %btv %bt",
  77. "OpStore %bfv %bf",
  78. "OpStore %iv %si",
  79. "OpStore %uv %ui",
  80. "OpStore %fv %f",
  81. "OpStore %dv %d",
  82. // clang-format on
  83. });
  84. std::vector<const char*> expected = {
  85. // clang-format off
  86. "OpCapability Shader",
  87. "OpCapability Float64",
  88. "%1 = OpExtInstImport \"GLSL.std.450\"",
  89. "OpMemoryModel Logical GLSL450",
  90. "OpEntryPoint Vertex %main \"main\"",
  91. "OpName %void \"void\"",
  92. "OpName %main_func_type \"main_func_type\"",
  93. "OpName %main \"main\"",
  94. "OpName %main_func_entry_block \"main_func_entry_block\"",
  95. "OpName %bool \"bool\"",
  96. "OpName %_PF_bool \"_PF_bool\"",
  97. "OpName %bt \"bt\"",
  98. "OpName %bf \"bf\"",
  99. "OpName %int \"int\"",
  100. "OpName %_PF_int \"_PF_int\"",
  101. "OpName %si \"si\"",
  102. "OpName %uint \"uint\"",
  103. "OpName %_PF_uint \"_PF_uint\"",
  104. "OpName %ui \"ui\"",
  105. "OpName %float \"float\"",
  106. "OpName %_PF_float \"_PF_float\"",
  107. "OpName %f \"f\"",
  108. "OpName %double \"double\"",
  109. "OpName %_PF_double \"_PF_double\"",
  110. "OpName %d \"d\"",
  111. "OpName %btv \"btv\"",
  112. "OpName %bfv \"bfv\"",
  113. "OpName %iv \"iv\"",
  114. "OpName %uv \"uv\"",
  115. "OpName %fv \"fv\"",
  116. "OpName %dv \"dv\"",
  117. "%void = OpTypeVoid",
  118. "%main_func_type = OpTypeFunction %void",
  119. "%bool = OpTypeBool",
  120. "%_PF_bool = OpTypePointer Function %bool",
  121. "%bt = OpConstantTrue %bool",
  122. "%bf = OpConstantFalse %bool",
  123. "%int = OpTypeInt 32 1",
  124. "%_PF_int = OpTypePointer Function %int",
  125. "%si = OpConstant %int 1",
  126. "%uint = OpTypeInt 32 0",
  127. "%_PF_uint = OpTypePointer Function %uint",
  128. "%ui = OpConstant %uint 2",
  129. "%float = OpTypeFloat 32",
  130. "%_PF_float = OpTypePointer Function %float",
  131. "%f = OpConstant %float 3.1415",
  132. "%double = OpTypeFloat 64",
  133. "%_PF_double = OpTypePointer Function %double",
  134. "%d = OpConstant %double 3.14159265358979",
  135. "%main = OpFunction %void None %main_func_type",
  136. "%main_func_entry_block = OpLabel",
  137. "%btv = OpVariable %_PF_bool Function",
  138. "%bfv = OpVariable %_PF_bool Function",
  139. "%iv = OpVariable %_PF_int Function",
  140. "%uv = OpVariable %_PF_uint Function",
  141. "%fv = OpVariable %_PF_float Function",
  142. "%dv = OpVariable %_PF_double Function",
  143. "OpStore %btv %bt",
  144. "OpStore %bfv %bf",
  145. "OpStore %iv %si",
  146. "OpStore %uv %ui",
  147. "OpStore %fv %f",
  148. "OpStore %dv %d",
  149. "OpReturn",
  150. "OpFunctionEnd",
  151. // clang-format on
  152. };
  153. SinglePassRunAndCheck<NullPass>(builder.GetCode(), JoinAllInsts(expected),
  154. /* skip_nop = */ false);
  155. }
  156. TEST_F(AssemblyBuilderTest, SpecConstants) {
  157. AssemblyBuilder builder;
  158. builder.AppendTypesConstantsGlobals({
  159. "%bool = OpTypeBool",
  160. "%uint = OpTypeInt 32 0",
  161. "%int = OpTypeInt 32 1",
  162. "%float = OpTypeFloat 32",
  163. "%double = OpTypeFloat 64",
  164. "%v2int = OpTypeVector %int 2",
  165. "%spec_true = OpSpecConstantTrue %bool",
  166. "%spec_false = OpSpecConstantFalse %bool",
  167. "%spec_uint = OpSpecConstant %uint 1",
  168. "%spec_int = OpSpecConstant %int 1",
  169. "%spec_float = OpSpecConstant %float 1.25",
  170. "%spec_double = OpSpecConstant %double 1.2345678",
  171. // Spec constants defined below should not have SpecID.
  172. "%spec_add_op = OpSpecConstantOp %int IAdd %spec_int %spec_int",
  173. "%spec_vec = OpSpecConstantComposite %v2int %spec_int %spec_int",
  174. "%spec_vec_x = OpSpecConstantOp %int CompositeExtract %spec_vec 0",
  175. });
  176. std::vector<const char*> expected = {
  177. // clang-format off
  178. "OpCapability Shader",
  179. "OpCapability Float64",
  180. "%1 = OpExtInstImport \"GLSL.std.450\"",
  181. "OpMemoryModel Logical GLSL450",
  182. "OpEntryPoint Vertex %main \"main\"",
  183. "OpName %void \"void\"",
  184. "OpName %main_func_type \"main_func_type\"",
  185. "OpName %main \"main\"",
  186. "OpName %main_func_entry_block \"main_func_entry_block\"",
  187. "OpName %bool \"bool\"",
  188. "OpName %uint \"uint\"",
  189. "OpName %int \"int\"",
  190. "OpName %float \"float\"",
  191. "OpName %double \"double\"",
  192. "OpName %v2int \"v2int\"",
  193. "OpName %spec_true \"spec_true\"",
  194. "OpName %spec_false \"spec_false\"",
  195. "OpName %spec_uint \"spec_uint\"",
  196. "OpName %spec_int \"spec_int\"",
  197. "OpName %spec_float \"spec_float\"",
  198. "OpName %spec_double \"spec_double\"",
  199. "OpName %spec_add_op \"spec_add_op\"",
  200. "OpName %spec_vec \"spec_vec\"",
  201. "OpName %spec_vec_x \"spec_vec_x\"",
  202. "OpDecorate %spec_true SpecId 200",
  203. "OpDecorate %spec_false SpecId 201",
  204. "OpDecorate %spec_uint SpecId 202",
  205. "OpDecorate %spec_int SpecId 203",
  206. "OpDecorate %spec_float SpecId 204",
  207. "OpDecorate %spec_double SpecId 205",
  208. "%void = OpTypeVoid",
  209. "%main_func_type = OpTypeFunction %void",
  210. "%bool = OpTypeBool",
  211. "%uint = OpTypeInt 32 0",
  212. "%int = OpTypeInt 32 1",
  213. "%float = OpTypeFloat 32",
  214. "%double = OpTypeFloat 64",
  215. "%v2int = OpTypeVector %int 2",
  216. "%spec_true = OpSpecConstantTrue %bool",
  217. "%spec_false = OpSpecConstantFalse %bool",
  218. "%spec_uint = OpSpecConstant %uint 1",
  219. "%spec_int = OpSpecConstant %int 1",
  220. "%spec_float = OpSpecConstant %float 1.25",
  221. "%spec_double = OpSpecConstant %double 1.2345678",
  222. "%spec_add_op = OpSpecConstantOp %int IAdd %spec_int %spec_int",
  223. "%spec_vec = OpSpecConstantComposite %v2int %spec_int %spec_int",
  224. "%spec_vec_x = OpSpecConstantOp %int CompositeExtract %spec_vec 0",
  225. "%main = OpFunction %void None %main_func_type",
  226. "%main_func_entry_block = OpLabel",
  227. "OpReturn",
  228. "OpFunctionEnd",
  229. // clang-format on
  230. };
  231. SinglePassRunAndCheck<NullPass>(builder.GetCode(), JoinAllInsts(expected),
  232. /* skip_nop = */ false);
  233. }
  234. TEST_F(AssemblyBuilderTest, AppendNames) {
  235. AssemblyBuilder builder;
  236. builder.AppendNames({
  237. "OpName %void \"another_name_for_void\"",
  238. "I am an invalid OpName instruction and should not be added",
  239. "OpName %main \"another name for main\"",
  240. });
  241. std::vector<const char*> expected = {
  242. // clang-format off
  243. "OpCapability Shader",
  244. "OpCapability Float64",
  245. "%1 = OpExtInstImport \"GLSL.std.450\"",
  246. "OpMemoryModel Logical GLSL450",
  247. "OpEntryPoint Vertex %main \"main\"",
  248. "OpName %void \"void\"",
  249. "OpName %main_func_type \"main_func_type\"",
  250. "OpName %main \"main\"",
  251. "OpName %main_func_entry_block \"main_func_entry_block\"",
  252. "OpName %void \"another_name_for_void\"",
  253. "OpName %main \"another name for main\"",
  254. "%void = OpTypeVoid",
  255. "%main_func_type = OpTypeFunction %void",
  256. "%main = OpFunction %void None %main_func_type",
  257. "%main_func_entry_block = OpLabel",
  258. "OpReturn",
  259. "OpFunctionEnd",
  260. // clang-format on
  261. };
  262. SinglePassRunAndCheck<NullPass>(builder.GetCode(), JoinAllInsts(expected),
  263. /* skip_nop = */ false);
  264. }
  265. } // namespace
  266. } // namespace opt
  267. } // namespace spvtools