reduce_load_size_test.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. // Copyright (c) 2018 Google LLC
  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 <string>
  15. #include "test/opt/pass_fixture.h"
  16. #include "test/opt/pass_utils.h"
  17. namespace {
  18. const double kDefaultLoadReductionThreshold = 0.9;
  19. } // namespace
  20. namespace spvtools {
  21. namespace opt {
  22. namespace {
  23. using ReduceLoadSizeTest = PassTest<::testing::Test>;
  24. TEST_F(ReduceLoadSizeTest, cbuffer_load_extract) {
  25. // Originally from the following HLSL:
  26. // struct S {
  27. // uint f;
  28. // };
  29. //
  30. //
  31. // cbuffer gBuffer { uint a[32]; };
  32. //
  33. // RWStructuredBuffer<S> gRWSBuffer;
  34. //
  35. // uint foo(uint p[32]) {
  36. // return p[1];
  37. // }
  38. //
  39. // [numthreads(1,1,1)]
  40. // void main() {
  41. // gRWSBuffer[0].f = foo(a);
  42. // }
  43. const std::string test =
  44. R"(
  45. OpCapability Shader
  46. OpMemoryModel Logical GLSL450
  47. OpEntryPoint GLCompute %main "main"
  48. OpExecutionMode %main LocalSize 1 1 1
  49. OpSource HLSL 600
  50. OpName %type_gBuffer "type.gBuffer"
  51. OpMemberName %type_gBuffer 0 "a"
  52. OpName %gBuffer "gBuffer"
  53. OpName %S "S"
  54. OpMemberName %S 0 "f"
  55. OpName %type_RWStructuredBuffer_S "type.RWStructuredBuffer.S"
  56. OpName %gRWSBuffer "gRWSBuffer"
  57. OpName %main "main"
  58. OpDecorate %_arr_uint_uint_32 ArrayStride 16
  59. OpMemberDecorate %type_gBuffer 0 Offset 0
  60. OpDecorate %type_gBuffer Block
  61. OpMemberDecorate %S 0 Offset 0
  62. OpDecorate %_runtimearr_S ArrayStride 4
  63. OpMemberDecorate %type_RWStructuredBuffer_S 0 Offset 0
  64. OpDecorate %type_RWStructuredBuffer_S BufferBlock
  65. OpDecorate %gBuffer DescriptorSet 0
  66. OpDecorate %gBuffer Binding 0
  67. OpDecorate %gRWSBuffer DescriptorSet 0
  68. OpDecorate %gRWSBuffer Binding 1
  69. %uint = OpTypeInt 32 0
  70. %uint_32 = OpConstant %uint 32
  71. %_arr_uint_uint_32 = OpTypeArray %uint %uint_32
  72. %type_gBuffer = OpTypeStruct %_arr_uint_uint_32
  73. %_ptr_Uniform_type_gBuffer = OpTypePointer Uniform %type_gBuffer
  74. %S = OpTypeStruct %uint
  75. %_runtimearr_S = OpTypeRuntimeArray %S
  76. %type_RWStructuredBuffer_S = OpTypeStruct %_runtimearr_S
  77. %_ptr_Uniform_type_RWStructuredBuffer_S = OpTypePointer Uniform %type_RWStructuredBuffer_S
  78. %int = OpTypeInt 32 1
  79. %void = OpTypeVoid
  80. %15 = OpTypeFunction %void
  81. %int_0 = OpConstant %int 0
  82. %_ptr_Uniform__arr_uint_uint_32 = OpTypePointer Uniform %_arr_uint_uint_32
  83. %uint_0 = OpConstant %uint 0
  84. %_ptr_Uniform_uint = OpTypePointer Uniform %uint
  85. %gBuffer = OpVariable %_ptr_Uniform_type_gBuffer Uniform
  86. %gRWSBuffer = OpVariable %_ptr_Uniform_type_RWStructuredBuffer_S Uniform
  87. %main = OpFunction %void None %15
  88. %20 = OpLabel
  89. ; CHECK: [[ac1:%\w+]] = OpAccessChain {{%\w+}} %gBuffer %int_0
  90. ; CHECK: [[ac2:%\w+]] = OpAccessChain {{%\w+}} [[ac1]] %uint_1
  91. ; CHECK: [[ld:%\w+]] = OpLoad {{%\w+}} [[ac2]]
  92. ; CHECK: OpStore {{%\w+}} [[ld]]
  93. %21 = OpAccessChain %_ptr_Uniform__arr_uint_uint_32 %gBuffer %int_0
  94. %22 = OpLoad %_arr_uint_uint_32 %21 ; Load of 32-element array.
  95. %23 = OpCompositeExtract %uint %22 1
  96. %24 = OpAccessChain %_ptr_Uniform_uint %gRWSBuffer %int_0 %uint_0 %int_0
  97. OpStore %24 %23
  98. OpReturn
  99. OpFunctionEnd
  100. )";
  101. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  102. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
  103. SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
  104. SinglePassRunAndMatch<ReduceLoadSize>(test, false,
  105. kDefaultLoadReductionThreshold);
  106. }
  107. TEST_F(ReduceLoadSizeTest, cbuffer_load_extract_not_affected_by_debug_instr) {
  108. // Originally from the following HLSL:
  109. // struct S {
  110. // uint f;
  111. // };
  112. //
  113. //
  114. // cbuffer gBuffer { uint a[32]; };
  115. //
  116. // RWStructuredBuffer<S> gRWSBuffer;
  117. //
  118. // uint foo(uint p[32]) {
  119. // return p[1];
  120. // }
  121. //
  122. // [numthreads(1,1,1)]
  123. // void main() {
  124. // gRWSBuffer[0].f = foo(a);
  125. // }
  126. const std::string test =
  127. R"(
  128. OpCapability Shader
  129. %ext = OpExtInstImport "OpenCL.DebugInfo.100"
  130. OpMemoryModel Logical GLSL450
  131. OpEntryPoint GLCompute %main "main"
  132. OpExecutionMode %main LocalSize 1 1 1
  133. OpSource HLSL 600
  134. %file_name = OpString "test"
  135. %float_name = OpString "float"
  136. %main_name = OpString "main"
  137. %f_name = OpString "f"
  138. OpName %type_gBuffer "type.gBuffer"
  139. OpMemberName %type_gBuffer 0 "a"
  140. OpName %gBuffer "gBuffer"
  141. OpName %S "S"
  142. OpMemberName %S 0 "f"
  143. OpName %type_RWStructuredBuffer_S "type.RWStructuredBuffer.S"
  144. OpName %gRWSBuffer "gRWSBuffer"
  145. OpName %main "main"
  146. OpDecorate %_arr_uint_uint_32 ArrayStride 16
  147. OpMemberDecorate %type_gBuffer 0 Offset 0
  148. OpDecorate %type_gBuffer Block
  149. OpMemberDecorate %S 0 Offset 0
  150. OpDecorate %_runtimearr_S ArrayStride 4
  151. OpMemberDecorate %type_RWStructuredBuffer_S 0 Offset 0
  152. OpDecorate %type_RWStructuredBuffer_S BufferBlock
  153. OpDecorate %gBuffer DescriptorSet 0
  154. OpDecorate %gBuffer Binding 0
  155. OpDecorate %gRWSBuffer DescriptorSet 0
  156. OpDecorate %gRWSBuffer Binding 1
  157. %uint = OpTypeInt 32 0
  158. %uint_32 = OpConstant %uint 32
  159. %_arr_uint_uint_32 = OpTypeArray %uint %uint_32
  160. %type_gBuffer = OpTypeStruct %_arr_uint_uint_32
  161. %_ptr_Uniform_type_gBuffer = OpTypePointer Uniform %type_gBuffer
  162. %S = OpTypeStruct %uint
  163. %_runtimearr_S = OpTypeRuntimeArray %S
  164. %type_RWStructuredBuffer_S = OpTypeStruct %_runtimearr_S
  165. %_ptr_Uniform_type_RWStructuredBuffer_S = OpTypePointer Uniform %type_RWStructuredBuffer_S
  166. %int = OpTypeInt 32 1
  167. %void = OpTypeVoid
  168. %15 = OpTypeFunction %void
  169. %int_0 = OpConstant %int 0
  170. %_ptr_Uniform__arr_uint_uint_32 = OpTypePointer Uniform %_arr_uint_uint_32
  171. %uint_0 = OpConstant %uint 0
  172. %_ptr_Uniform_uint = OpTypePointer Uniform %uint
  173. %gBuffer = OpVariable %_ptr_Uniform_type_gBuffer Uniform
  174. %gRWSBuffer = OpVariable %_ptr_Uniform_type_RWStructuredBuffer_S Uniform
  175. %null_expr = OpExtInst %void %ext DebugExpression
  176. %src = OpExtInst %void %ext DebugSource %file_name
  177. %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
  178. %dbg_tf = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
  179. %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_tf
  180. %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 0 0 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
  181. %dbg_f = OpExtInst %void %ext DebugLocalVariable %f_name %dbg_tf %src 0 0 %dbg_main FlagIsLocal
  182. %main = OpFunction %void None %15
  183. %20 = OpLabel
  184. %s = OpExtInst %void %ext DebugScope %dbg_main
  185. ; CHECK: [[ac1:%\w+]] = OpAccessChain {{%\w+}} %gBuffer %int_0
  186. ; CHECK: [[ac2:%\w+]] = OpAccessChain {{%\w+}} [[ac1]] %uint_1
  187. ; CHECK: [[ld:%\w+]] = OpLoad {{%\w+}} [[ac2]]
  188. ; CHECK: OpStore {{%\w+}} [[ld]]
  189. %21 = OpAccessChain %_ptr_Uniform__arr_uint_uint_32 %gBuffer %int_0
  190. %22 = OpLoad %_arr_uint_uint_32 %21 ; Load of 32-element array.
  191. %value = OpExtInst %void %ext DebugValue %dbg_f %22 %null_expr
  192. %23 = OpCompositeExtract %uint %22 1
  193. %24 = OpAccessChain %_ptr_Uniform_uint %gRWSBuffer %int_0 %uint_0 %int_0
  194. OpStore %24 %23
  195. OpReturn
  196. OpFunctionEnd
  197. )";
  198. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  199. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
  200. SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
  201. SinglePassRunAndMatch<ReduceLoadSize>(test, false,
  202. kDefaultLoadReductionThreshold);
  203. }
  204. TEST_F(ReduceLoadSizeTest, cbuffer_load_extract_vector) {
  205. // Originally from the following HLSL:
  206. // struct S {
  207. // uint f;
  208. // };
  209. //
  210. //
  211. // cbuffer gBuffer { uint4 a; };
  212. //
  213. // RWStructuredBuffer<S> gRWSBuffer;
  214. //
  215. // uint foo(uint p[32]) {
  216. // return p[1];
  217. // }
  218. //
  219. // [numthreads(1,1,1)]
  220. // void main() {
  221. // gRWSBuffer[0].f = foo(a);
  222. // }
  223. const std::string test =
  224. R"(OpCapability Shader
  225. OpMemoryModel Logical GLSL450
  226. OpEntryPoint GLCompute %main "main"
  227. OpExecutionMode %main LocalSize 1 1 1
  228. OpSource HLSL 600
  229. OpName %type_gBuffer "type.gBuffer"
  230. OpMemberName %type_gBuffer 0 "a"
  231. OpName %gBuffer "gBuffer"
  232. OpName %S "S"
  233. OpMemberName %S 0 "f"
  234. OpName %type_RWStructuredBuffer_S "type.RWStructuredBuffer.S"
  235. OpName %gRWSBuffer "gRWSBuffer"
  236. OpName %main "main"
  237. OpMemberDecorate %type_gBuffer 0 Offset 0
  238. OpDecorate %type_gBuffer Block
  239. OpMemberDecorate %S 0 Offset 0
  240. OpDecorate %_runtimearr_S ArrayStride 4
  241. OpMemberDecorate %type_RWStructuredBuffer_S 0 Offset 0
  242. OpDecorate %type_RWStructuredBuffer_S BufferBlock
  243. OpDecorate %gBuffer DescriptorSet 0
  244. OpDecorate %gBuffer Binding 0
  245. OpDecorate %gRWSBuffer DescriptorSet 0
  246. OpDecorate %gRWSBuffer Binding 1
  247. %uint = OpTypeInt 32 0
  248. %uint_32 = OpConstant %uint 32
  249. %v4uint = OpTypeVector %uint 4
  250. %type_gBuffer = OpTypeStruct %v4uint
  251. %_ptr_Uniform_type_gBuffer = OpTypePointer Uniform %type_gBuffer
  252. %S = OpTypeStruct %uint
  253. %_runtimearr_S = OpTypeRuntimeArray %S
  254. %type_RWStructuredBuffer_S = OpTypeStruct %_runtimearr_S
  255. %_ptr_Uniform_type_RWStructuredBuffer_S = OpTypePointer Uniform %type_RWStructuredBuffer_S
  256. %int = OpTypeInt 32 1
  257. %void = OpTypeVoid
  258. %15 = OpTypeFunction %void
  259. %int_0 = OpConstant %int 0
  260. %_ptr_Uniform_v4uint = OpTypePointer Uniform %v4uint
  261. %uint_0 = OpConstant %uint 0
  262. %_ptr_Uniform_uint = OpTypePointer Uniform %uint
  263. %gBuffer = OpVariable %_ptr_Uniform_type_gBuffer Uniform
  264. %gRWSBuffer = OpVariable %_ptr_Uniform_type_RWStructuredBuffer_S Uniform
  265. %main = OpFunction %void None %15
  266. %20 = OpLabel
  267. %21 = OpAccessChain %_ptr_Uniform_v4uint %gBuffer %int_0
  268. %22 = OpLoad %v4uint %21
  269. %23 = OpCompositeExtract %uint %22 1
  270. %24 = OpAccessChain %_ptr_Uniform_uint %gRWSBuffer %int_0 %uint_0 %int_0
  271. OpStore %24 %23
  272. OpReturn
  273. OpFunctionEnd
  274. )";
  275. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  276. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
  277. SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
  278. SinglePassRunAndCheck<ReduceLoadSize>(test, test, true, false,
  279. kDefaultLoadReductionThreshold);
  280. }
  281. TEST_F(ReduceLoadSizeTest, cbuffer_load_5_extract) {
  282. // All of the elements of the value loaded are used, so we should not
  283. // change the load.
  284. const std::string test =
  285. R"(OpCapability Shader
  286. OpMemoryModel Logical GLSL450
  287. OpEntryPoint GLCompute %main "main"
  288. OpExecutionMode %main LocalSize 1 1 1
  289. OpSource HLSL 600
  290. OpName %type_gBuffer "type.gBuffer"
  291. OpMemberName %type_gBuffer 0 "a"
  292. OpName %gBuffer "gBuffer"
  293. OpName %S "S"
  294. OpMemberName %S 0 "f"
  295. OpName %type_RWStructuredBuffer_S "type.RWStructuredBuffer.S"
  296. OpName %gRWSBuffer "gRWSBuffer"
  297. OpName %main "main"
  298. OpDecorate %_arr_uint_uint_5 ArrayStride 16
  299. OpMemberDecorate %type_gBuffer 0 Offset 0
  300. OpDecorate %type_gBuffer Block
  301. OpMemberDecorate %S 0 Offset 0
  302. OpDecorate %_runtimearr_S ArrayStride 4
  303. OpMemberDecorate %type_RWStructuredBuffer_S 0 Offset 0
  304. OpDecorate %type_RWStructuredBuffer_S BufferBlock
  305. OpDecorate %gBuffer DescriptorSet 0
  306. OpDecorate %gBuffer Binding 0
  307. OpDecorate %gRWSBuffer DescriptorSet 0
  308. OpDecorate %gRWSBuffer Binding 1
  309. %uint = OpTypeInt 32 0
  310. %uint_5 = OpConstant %uint 5
  311. %_arr_uint_uint_5 = OpTypeArray %uint %uint_5
  312. %type_gBuffer = OpTypeStruct %_arr_uint_uint_5
  313. %_ptr_Uniform_type_gBuffer = OpTypePointer Uniform %type_gBuffer
  314. %S = OpTypeStruct %uint
  315. %_runtimearr_S = OpTypeRuntimeArray %S
  316. %type_RWStructuredBuffer_S = OpTypeStruct %_runtimearr_S
  317. %_ptr_Uniform_type_RWStructuredBuffer_S = OpTypePointer Uniform %type_RWStructuredBuffer_S
  318. %int = OpTypeInt 32 1
  319. %void = OpTypeVoid
  320. %15 = OpTypeFunction %void
  321. %int_0 = OpConstant %int 0
  322. %_ptr_Uniform__arr_uint_uint_5 = OpTypePointer Uniform %_arr_uint_uint_5
  323. %uint_0 = OpConstant %uint 0
  324. %_ptr_Uniform_uint = OpTypePointer Uniform %uint
  325. %gBuffer = OpVariable %_ptr_Uniform_type_gBuffer Uniform
  326. %gRWSBuffer = OpVariable %_ptr_Uniform_type_RWStructuredBuffer_S Uniform
  327. %main = OpFunction %void None %15
  328. %20 = OpLabel
  329. %21 = OpAccessChain %_ptr_Uniform__arr_uint_uint_5 %gBuffer %int_0
  330. %22 = OpLoad %_arr_uint_uint_5 %21
  331. %23 = OpCompositeExtract %uint %22 0
  332. %24 = OpCompositeExtract %uint %22 1
  333. %25 = OpCompositeExtract %uint %22 2
  334. %26 = OpCompositeExtract %uint %22 3
  335. %27 = OpCompositeExtract %uint %22 4
  336. %28 = OpIAdd %uint %23 %24
  337. %29 = OpIAdd %uint %28 %25
  338. %30 = OpIAdd %uint %29 %26
  339. %31 = OpIAdd %uint %20 %27
  340. %32 = OpAccessChain %_ptr_Uniform_uint %gRWSBuffer %int_0 %uint_0 %int_0
  341. OpStore %32 %31
  342. OpReturn
  343. OpFunctionEnd
  344. )";
  345. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  346. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
  347. SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
  348. SinglePassRunAndCheck<ReduceLoadSize>(test, test, true, false,
  349. kDefaultLoadReductionThreshold);
  350. }
  351. TEST_F(ReduceLoadSizeTest, cbuffer_load_fully_used) {
  352. // The result of the load (%22) is used in an instruction that uses the whole
  353. // load and has only 1 in operand. This trigger issue #1559.
  354. const std::string test =
  355. R"(OpCapability Shader
  356. OpMemoryModel Logical GLSL450
  357. OpEntryPoint GLCompute %main "main"
  358. OpExecutionMode %main LocalSize 1 1 1
  359. OpSource HLSL 600
  360. OpName %type_gBuffer "type.gBuffer"
  361. OpMemberName %type_gBuffer 0 "a"
  362. OpName %gBuffer "gBuffer"
  363. OpName %S "S"
  364. OpMemberName %S 0 "f"
  365. OpName %type_RWStructuredBuffer_S "type.RWStructuredBuffer.S"
  366. OpName %gRWSBuffer "gRWSBuffer"
  367. OpName %main "main"
  368. OpMemberDecorate %type_gBuffer 0 Offset 0
  369. OpDecorate %type_gBuffer Block
  370. OpMemberDecorate %S 0 Offset 0
  371. OpDecorate %_runtimearr_S ArrayStride 4
  372. OpMemberDecorate %type_RWStructuredBuffer_S 0 Offset 0
  373. OpDecorate %type_RWStructuredBuffer_S BufferBlock
  374. OpDecorate %gBuffer DescriptorSet 0
  375. OpDecorate %gBuffer Binding 0
  376. OpDecorate %gRWSBuffer DescriptorSet 0
  377. OpDecorate %gRWSBuffer Binding 1
  378. %uint = OpTypeInt 32 0
  379. %uint_32 = OpConstant %uint 32
  380. %v4uint = OpTypeVector %uint 4
  381. %float = OpTypeFloat 32
  382. %v4float = OpTypeVector %float 4
  383. %type_gBuffer = OpTypeStruct %v4uint
  384. %_ptr_Uniform_type_gBuffer = OpTypePointer Uniform %type_gBuffer
  385. %S = OpTypeStruct %uint
  386. %_runtimearr_S = OpTypeRuntimeArray %S
  387. %type_RWStructuredBuffer_S = OpTypeStruct %_runtimearr_S
  388. %_ptr_Uniform_type_RWStructuredBuffer_S = OpTypePointer Uniform %type_RWStructuredBuffer_S
  389. %int = OpTypeInt 32 1
  390. %void = OpTypeVoid
  391. %15 = OpTypeFunction %void
  392. %int_0 = OpConstant %int 0
  393. %_ptr_Uniform_v4uint = OpTypePointer Uniform %v4uint
  394. %uint_0 = OpConstant %uint 0
  395. %_ptr_Uniform_uint = OpTypePointer Uniform %uint
  396. %gBuffer = OpVariable %_ptr_Uniform_type_gBuffer Uniform
  397. %gRWSBuffer = OpVariable %_ptr_Uniform_type_RWStructuredBuffer_S Uniform
  398. %main = OpFunction %void None %15
  399. %20 = OpLabel
  400. %21 = OpAccessChain %_ptr_Uniform_v4uint %gBuffer %int_0
  401. %22 = OpLoad %v4uint %21
  402. %23 = OpCompositeExtract %uint %22 1
  403. %24 = OpConvertUToF %v4float %22
  404. %25 = OpAccessChain %_ptr_Uniform_uint %gRWSBuffer %int_0 %uint_0 %int_0
  405. OpStore %25 %23
  406. OpReturn
  407. OpFunctionEnd
  408. )";
  409. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  410. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
  411. SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
  412. SinglePassRunAndCheck<ReduceLoadSize>(test, test, true, false,
  413. kDefaultLoadReductionThreshold);
  414. }
  415. TEST_F(ReduceLoadSizeTest, replace_cbuffer_load_fully_used) {
  416. const std::string test =
  417. R"(
  418. OpCapability Shader
  419. OpCapability SampledBuffer
  420. OpMemoryModel Logical GLSL450
  421. OpEntryPoint Fragment %main "main" %out_var_SV_Target0
  422. OpExecutionMode %main OriginUpperLeft
  423. OpSource HLSL 600
  424. OpName %type_MaterialInstancing_cbuffer "type.MaterialInstancing_cbuffer"
  425. OpMemberName %type_MaterialInstancing_cbuffer 0 "MaterialInstancing_constants"
  426. OpName %MaterialInstancing_Constants "MaterialInstancing_Constants"
  427. OpMemberName %MaterialInstancing_Constants 0 "offset0"
  428. OpMemberName %MaterialInstancing_Constants 1 "params"
  429. OpName %InstancingParams_Constants "InstancingParams_Constants"
  430. OpMemberName %InstancingParams_Constants 0 "offset1"
  431. OpName %MaterialInstancing_cbuffer "MaterialInstancing_cbuffer"
  432. OpName %out_var_SV_Target0 "out.var.SV_Target0"
  433. OpName %main "main"
  434. OpDecorate %out_var_SV_Target0 Location 0
  435. OpDecorate %MaterialInstancing_cbuffer DescriptorSet 6
  436. OpDecorate %MaterialInstancing_cbuffer Binding 0
  437. OpMemberDecorate %InstancingParams_Constants 0 Offset 0
  438. OpMemberDecorate %MaterialInstancing_Constants 0 Offset 0
  439. OpMemberDecorate %MaterialInstancing_Constants 1 Offset 16
  440. OpMemberDecorate %type_MaterialInstancing_cbuffer 0 Offset 0
  441. OpDecorate %type_MaterialInstancing_cbuffer Block
  442. %int = OpTypeInt 32 1
  443. %int_0 = OpConstant %int 0
  444. %v4int = OpTypeVector %int 4
  445. %InstancingParams_Constants = OpTypeStruct %v4int
  446. %MaterialInstancing_Constants = OpTypeStruct %v4int %InstancingParams_Constants
  447. %type_MaterialInstancing_cbuffer = OpTypeStruct %MaterialInstancing_Constants
  448. %_ptr_Uniform_type_MaterialInstancing_cbuffer = OpTypePointer Uniform %type_MaterialInstancing_cbuffer
  449. %_ptr_Output_int = OpTypePointer Output %int
  450. %void = OpTypeVoid
  451. %60 = OpTypeFunction %void
  452. %_ptr_Uniform_MaterialInstancing_Constants = OpTypePointer Uniform %MaterialInstancing_Constants
  453. %MaterialInstancing_cbuffer = OpVariable %_ptr_Uniform_type_MaterialInstancing_cbuffer Uniform
  454. %out_var_SV_Target0 = OpVariable %_ptr_Output_int Output
  455. %main = OpFunction %void None %60
  456. %80 = OpLabel
  457. %131 = OpAccessChain %_ptr_Uniform_MaterialInstancing_Constants %MaterialInstancing_cbuffer %int_0
  458. %132 = OpLoad %MaterialInstancing_Constants %131
  459. ; CHECK: [[ac1:%\w+]] = OpAccessChain {{%\w+}} %MaterialInstancing_cbuffer %int_0
  460. ; CHECK: [[ac2:%\w+]] = OpAccessChain {{%\w+}} [[ac1]] %uint_0
  461. ; CHECK: OpLoad %v4int [[ac2]]
  462. ; CHECK: [[ac3:%\w+]] = OpAccessChain {{%\w+}} [[ac1]] %uint_1
  463. ; CHECK: [[ac4:%\w+]] = OpAccessChain {{%\w+}} [[ac3]] %uint_0
  464. ; CHECK: OpLoad %v4int [[ac4]]
  465. %134 = OpCompositeExtract %v4int %132 0
  466. %135 = OpCompositeExtract %InstancingParams_Constants %132 1
  467. %136 = OpCompositeExtract %v4int %135 0
  468. %149 = OpCompositeExtract %int %134 0
  469. %185 = OpCompositeExtract %int %136 0
  470. %156 = OpIAdd %int %149 %185
  471. OpStore %out_var_SV_Target0 %156
  472. OpReturn
  473. OpFunctionEnd
  474. )";
  475. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  476. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
  477. SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
  478. SinglePassRunAndMatch<ReduceLoadSize>(test, false, 1.1);
  479. }
  480. TEST_F(ReduceLoadSizeTest, replace_array_with_spec_constant_size) {
  481. const std::string test =
  482. R"(
  483. OpCapability ClipDistance
  484. OpExtension " "
  485. OpMemoryModel Logical GLSL450
  486. OpEntryPoint Fragment %1 " "
  487. OpExecutionMode %1 OriginUpperLeft
  488. %void = OpTypeVoid
  489. %3 = OpTypeFunction %void
  490. %int = OpTypeInt 32 1
  491. %uint = OpTypeInt 32 0
  492. %6 = OpSpecConstant %uint 538976288
  493. %_arr_int_6 = OpTypeArray %int %6
  494. %_struct_8 = OpTypeStruct %_arr_int_6
  495. %_struct_9 = OpTypeStruct %_struct_8
  496. %_ptr_Uniform__struct_9 = OpTypePointer Uniform %_struct_9
  497. ; CHECK: [[var:%\w+]] = OpVariable %_ptr_Uniform__struct_9 Uniform
  498. %11 = OpVariable %_ptr_Uniform__struct_9 Uniform
  499. %int_0 = OpConstant %int 0
  500. %_ptr_Uniform__arr_int_6 = OpTypePointer Uniform %_arr_int_6
  501. %1 = OpFunction %void None %3
  502. %14 = OpLabel
  503. ; CHECK: [[ac:%\w+]] = OpAccessChain %_ptr_Uniform__arr_int_6 [[var]] %int_0 %int_0
  504. ; CHECK: [[new_ac:%\w+]] = OpAccessChain %_ptr_Uniform_int [[ac]] %uint_538976288
  505. ; CHECK: [[ld:%\w+]] = OpLoad %int [[new_ac]]
  506. ; CHECK: %18 = OpIAdd %int [[ld]] [[ld]]
  507. %15 = OpAccessChain %_ptr_Uniform__arr_int_6 %11 %int_0 %int_0
  508. %16 = OpLoad %_arr_int_6 %15
  509. %17 = OpCompositeExtract %int %16 538976288
  510. %18 = OpIAdd %int %17 %17
  511. OpUnreachable
  512. OpFunctionEnd
  513. )";
  514. SinglePassRunAndMatch<ReduceLoadSize>(test, false,
  515. kDefaultLoadReductionThreshold);
  516. }
  517. } // namespace
  518. } // namespace opt
  519. } // namespace spvtools