loop_descriptions.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // Copyright (c) 2017 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 <memory>
  15. #include <vector>
  16. #include "gmock/gmock.h"
  17. #include "source/opt/loop_descriptor.h"
  18. #include "source/opt/pass.h"
  19. #include "test/opt/assembly_builder.h"
  20. #include "test/opt/function_utils.h"
  21. #include "test/opt/pass_fixture.h"
  22. #include "test/opt/pass_utils.h"
  23. namespace spvtools {
  24. namespace opt {
  25. namespace {
  26. using ::testing::UnorderedElementsAre;
  27. using PassClassTest = PassTest<::testing::Test>;
  28. /*
  29. Generated from the following GLSL
  30. #version 330 core
  31. layout(location = 0) out vec4 c;
  32. void main() {
  33. int i = 0;
  34. for(; i < 10; ++i) {
  35. }
  36. }
  37. */
  38. TEST_F(PassClassTest, BasicVisitFromEntryPoint) {
  39. const std::string text = R"(
  40. OpCapability Shader
  41. %1 = OpExtInstImport "GLSL.std.450"
  42. OpMemoryModel Logical GLSL450
  43. OpEntryPoint Fragment %2 "main" %3
  44. OpExecutionMode %2 OriginUpperLeft
  45. OpSource GLSL 330
  46. OpName %2 "main"
  47. OpName %5 "i"
  48. OpName %3 "c"
  49. OpDecorate %3 Location 0
  50. %6 = OpTypeVoid
  51. %7 = OpTypeFunction %6
  52. %8 = OpTypeInt 32 1
  53. %9 = OpTypePointer Function %8
  54. %10 = OpConstant %8 0
  55. %11 = OpConstant %8 10
  56. %12 = OpTypeBool
  57. %13 = OpConstant %8 1
  58. %14 = OpTypeFloat 32
  59. %15 = OpTypeVector %14 4
  60. %16 = OpTypePointer Output %15
  61. %3 = OpVariable %16 Output
  62. %2 = OpFunction %6 None %7
  63. %17 = OpLabel
  64. %5 = OpVariable %9 Function
  65. OpStore %5 %10
  66. OpBranch %18
  67. %18 = OpLabel
  68. OpLoopMerge %19 %20 None
  69. OpBranch %21
  70. %21 = OpLabel
  71. %22 = OpLoad %8 %5
  72. %23 = OpSLessThan %12 %22 %11
  73. OpBranchConditional %23 %24 %19
  74. %24 = OpLabel
  75. OpBranch %20
  76. %20 = OpLabel
  77. %25 = OpLoad %8 %5
  78. %26 = OpIAdd %8 %25 %13
  79. OpStore %5 %26
  80. OpBranch %18
  81. %19 = OpLabel
  82. OpReturn
  83. OpFunctionEnd
  84. )";
  85. // clang-format on
  86. std::unique_ptr<IRContext> context =
  87. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  88. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  89. Module* module = context->module();
  90. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  91. << text << std::endl;
  92. const Function* f = spvtest::GetFunction(module, 2);
  93. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  94. EXPECT_EQ(ld.NumLoops(), 1u);
  95. Loop& loop = ld.GetLoopByIndex(0);
  96. EXPECT_EQ(loop.GetHeaderBlock(), spvtest::GetBasicBlock(f, 18));
  97. EXPECT_EQ(loop.GetLatchBlock(), spvtest::GetBasicBlock(f, 20));
  98. EXPECT_EQ(loop.GetMergeBlock(), spvtest::GetBasicBlock(f, 19));
  99. EXPECT_FALSE(loop.HasNestedLoops());
  100. EXPECT_FALSE(loop.IsNested());
  101. EXPECT_EQ(loop.GetDepth(), 1u);
  102. }
  103. /*
  104. Generated from the following GLSL:
  105. #version 330 core
  106. layout(location = 0) out vec4 c;
  107. void main() {
  108. for(int i = 0; i < 10; ++i) {}
  109. for(int i = 0; i < 10; ++i) {}
  110. }
  111. But it was "hacked" to make the first loop merge block the second loop header.
  112. */
  113. TEST_F(PassClassTest, LoopWithNoPreHeader) {
  114. const std::string text = R"(
  115. OpCapability Shader
  116. %1 = OpExtInstImport "GLSL.std.450"
  117. OpMemoryModel Logical GLSL450
  118. OpEntryPoint Fragment %2 "main" %3
  119. OpExecutionMode %2 OriginUpperLeft
  120. OpSource GLSL 330
  121. OpName %2 "main"
  122. OpName %4 "i"
  123. OpName %5 "i"
  124. OpName %3 "c"
  125. OpDecorate %3 Location 0
  126. %6 = OpTypeVoid
  127. %7 = OpTypeFunction %6
  128. %8 = OpTypeInt 32 1
  129. %9 = OpTypePointer Function %8
  130. %10 = OpConstant %8 0
  131. %11 = OpConstant %8 10
  132. %12 = OpTypeBool
  133. %13 = OpConstant %8 1
  134. %14 = OpTypeFloat 32
  135. %15 = OpTypeVector %14 4
  136. %16 = OpTypePointer Output %15
  137. %3 = OpVariable %16 Output
  138. %2 = OpFunction %6 None %7
  139. %17 = OpLabel
  140. %4 = OpVariable %9 Function
  141. %5 = OpVariable %9 Function
  142. OpStore %4 %10
  143. OpStore %5 %10
  144. OpBranch %18
  145. %18 = OpLabel
  146. OpLoopMerge %27 %20 None
  147. OpBranch %21
  148. %21 = OpLabel
  149. %22 = OpLoad %8 %4
  150. %23 = OpSLessThan %12 %22 %11
  151. OpBranchConditional %23 %24 %27
  152. %24 = OpLabel
  153. OpBranch %20
  154. %20 = OpLabel
  155. %25 = OpLoad %8 %4
  156. %26 = OpIAdd %8 %25 %13
  157. OpStore %4 %26
  158. OpBranch %18
  159. %27 = OpLabel
  160. OpLoopMerge %28 %29 None
  161. OpBranch %30
  162. %30 = OpLabel
  163. %31 = OpLoad %8 %5
  164. %32 = OpSLessThan %12 %31 %11
  165. OpBranchConditional %32 %33 %28
  166. %33 = OpLabel
  167. OpBranch %29
  168. %29 = OpLabel
  169. %34 = OpLoad %8 %5
  170. %35 = OpIAdd %8 %34 %13
  171. OpStore %5 %35
  172. OpBranch %27
  173. %28 = OpLabel
  174. OpReturn
  175. OpFunctionEnd
  176. )";
  177. // clang-format on
  178. std::unique_ptr<IRContext> context =
  179. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  180. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  181. Module* module = context->module();
  182. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  183. << text << std::endl;
  184. const Function* f = spvtest::GetFunction(module, 2);
  185. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  186. EXPECT_EQ(ld.NumLoops(), 2u);
  187. Loop* loop = ld[27];
  188. EXPECT_EQ(loop->GetPreHeaderBlock(), nullptr);
  189. EXPECT_NE(loop->GetOrCreatePreHeaderBlock(), nullptr);
  190. }
  191. /*
  192. Generated from the following GLSL + --eliminate-local-multi-store
  193. #version 330 core
  194. in vec4 c;
  195. void main() {
  196. int i = 0;
  197. bool cond = c[0] == 0;
  198. for (; i < 10; i++) {
  199. if (cond) {
  200. return;
  201. }
  202. else {
  203. return;
  204. }
  205. }
  206. bool cond2 = i == 9;
  207. }
  208. */
  209. TEST_F(PassClassTest, NoLoop) {
  210. const std::string text = R"(; SPIR-V
  211. ; Version: 1.0
  212. ; Generator: Khronos Glslang Reference Front End; 3
  213. ; Bound: 47
  214. ; Schema: 0
  215. OpCapability Shader
  216. %1 = OpExtInstImport "GLSL.std.450"
  217. OpMemoryModel Logical GLSL450
  218. OpEntryPoint Fragment %4 "main" %16
  219. OpExecutionMode %4 OriginUpperLeft
  220. OpSource GLSL 330
  221. OpName %4 "main"
  222. OpName %16 "c"
  223. OpDecorate %16 Location 0
  224. %2 = OpTypeVoid
  225. %3 = OpTypeFunction %2
  226. %6 = OpTypeInt 32 1
  227. %7 = OpTypePointer Function %6
  228. %9 = OpConstant %6 0
  229. %10 = OpTypeBool
  230. %11 = OpTypePointer Function %10
  231. %13 = OpTypeFloat 32
  232. %14 = OpTypeVector %13 4
  233. %15 = OpTypePointer Input %14
  234. %16 = OpVariable %15 Input
  235. %17 = OpTypeInt 32 0
  236. %18 = OpConstant %17 0
  237. %19 = OpTypePointer Input %13
  238. %22 = OpConstant %13 0
  239. %30 = OpConstant %6 10
  240. %39 = OpConstant %6 1
  241. %46 = OpUndef %6
  242. %4 = OpFunction %2 None %3
  243. %5 = OpLabel
  244. %20 = OpAccessChain %19 %16 %18
  245. %21 = OpLoad %13 %20
  246. %23 = OpFOrdEqual %10 %21 %22
  247. OpBranch %24
  248. %24 = OpLabel
  249. %45 = OpPhi %6 %9 %5 %40 %27
  250. OpLoopMerge %26 %27 None
  251. OpBranch %28
  252. %28 = OpLabel
  253. %31 = OpSLessThan %10 %45 %30
  254. OpBranchConditional %31 %25 %26
  255. %25 = OpLabel
  256. OpSelectionMerge %34 None
  257. OpBranchConditional %23 %33 %36
  258. %33 = OpLabel
  259. OpReturn
  260. %36 = OpLabel
  261. OpReturn
  262. %34 = OpLabel
  263. OpBranch %27
  264. %27 = OpLabel
  265. %40 = OpIAdd %6 %46 %39
  266. OpBranch %24
  267. %26 = OpLabel
  268. OpReturn
  269. OpFunctionEnd
  270. )";
  271. std::unique_ptr<IRContext> context =
  272. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  273. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  274. Module* module = context->module();
  275. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  276. << text << std::endl;
  277. const Function* f = spvtest::GetFunction(module, 4);
  278. LoopDescriptor ld{context.get(), f};
  279. EXPECT_EQ(ld.NumLoops(), 0u);
  280. }
  281. /*
  282. Generated from following GLSL with latch block artificially inserted to be
  283. separate from continue.
  284. #version 430
  285. void main(void) {
  286. float x[10];
  287. for (int i = 0; i < 10; ++i) {
  288. x[i] = i;
  289. }
  290. }
  291. */
  292. TEST_F(PassClassTest, LoopLatchNotContinue) {
  293. const std::string text = R"(OpCapability Shader
  294. %1 = OpExtInstImport "GLSL.std.450"
  295. OpMemoryModel Logical GLSL450
  296. OpEntryPoint Fragment %2 "main"
  297. OpExecutionMode %2 OriginUpperLeft
  298. OpSource GLSL 430
  299. OpName %2 "main"
  300. OpName %3 "i"
  301. OpName %4 "x"
  302. %5 = OpTypeVoid
  303. %6 = OpTypeFunction %5
  304. %7 = OpTypeInt 32 1
  305. %8 = OpTypePointer Function %7
  306. %9 = OpConstant %7 0
  307. %10 = OpConstant %7 10
  308. %11 = OpTypeBool
  309. %12 = OpTypeFloat 32
  310. %13 = OpTypeInt 32 0
  311. %14 = OpConstant %13 10
  312. %15 = OpTypeArray %12 %14
  313. %16 = OpTypePointer Function %15
  314. %17 = OpTypePointer Function %12
  315. %18 = OpConstant %7 1
  316. %2 = OpFunction %5 None %6
  317. %19 = OpLabel
  318. %3 = OpVariable %8 Function
  319. %4 = OpVariable %16 Function
  320. OpStore %3 %9
  321. OpBranch %20
  322. %20 = OpLabel
  323. %21 = OpPhi %7 %9 %19 %22 %30
  324. OpLoopMerge %24 %23 None
  325. OpBranch %25
  326. %25 = OpLabel
  327. %26 = OpSLessThan %11 %21 %10
  328. OpBranchConditional %26 %27 %24
  329. %27 = OpLabel
  330. %28 = OpConvertSToF %12 %21
  331. %29 = OpAccessChain %17 %4 %21
  332. OpStore %29 %28
  333. OpBranch %23
  334. %23 = OpLabel
  335. %22 = OpIAdd %7 %21 %18
  336. OpStore %3 %22
  337. OpBranch %30
  338. %30 = OpLabel
  339. OpBranch %20
  340. %24 = OpLabel
  341. OpReturn
  342. OpFunctionEnd
  343. )";
  344. std::unique_ptr<IRContext> context =
  345. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  346. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  347. Module* module = context->module();
  348. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  349. << text << std::endl;
  350. const Function* f = spvtest::GetFunction(module, 2);
  351. LoopDescriptor ld{context.get(), f};
  352. EXPECT_EQ(ld.NumLoops(), 1u);
  353. Loop& loop = ld.GetLoopByIndex(0u);
  354. EXPECT_NE(loop.GetLatchBlock(), loop.GetContinueBlock());
  355. EXPECT_EQ(loop.GetContinueBlock()->id(), 23u);
  356. EXPECT_EQ(loop.GetLatchBlock()->id(), 30u);
  357. }
  358. TEST_F(PassClassTest, UnreachableMerge) {
  359. const std::string text = R"(
  360. OpCapability Shader
  361. OpMemoryModel Logical GLSL450
  362. OpEntryPoint Fragment %1 "main"
  363. OpExecutionMode %1 OriginUpperLeft
  364. %void = OpTypeVoid
  365. %3 = OpTypeFunction %void
  366. %1 = OpFunction %void None %3
  367. %4 = OpLabel
  368. OpBranch %5
  369. %5 = OpLabel
  370. OpLoopMerge %6 %7 None
  371. OpBranch %8
  372. %8 = OpLabel
  373. OpBranch %9
  374. %9 = OpLabel
  375. OpBranch %7
  376. %7 = OpLabel
  377. OpBranch %5
  378. %6 = OpLabel
  379. OpUnreachable
  380. OpFunctionEnd
  381. )";
  382. std::unique_ptr<IRContext> context =
  383. BuildModule(SPV_ENV_UNIVERSAL_1_3, nullptr, text,
  384. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  385. Module* module = context->module();
  386. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  387. << text << std::endl;
  388. const Function* f = spvtest::GetFunction(module, 1);
  389. LoopDescriptor ld{context.get(), f};
  390. EXPECT_EQ(ld.NumLoops(), 1u);
  391. }
  392. } // namespace
  393. } // namespace opt
  394. } // namespace spvtools