loop_descriptions.cpp 13 KB

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