transformation_composite_extract_test.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // Copyright (c) 2019 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 "source/fuzz/transformation_composite_extract.h"
  15. #include "source/fuzz/instruction_descriptor.h"
  16. #include "test/fuzz/fuzz_test_util.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. namespace {
  20. TEST(TransformationCompositeExtractTest, BasicTest) {
  21. std::string shader = R"(
  22. OpCapability Shader
  23. %1 = OpExtInstImport "GLSL.std.450"
  24. OpMemoryModel Logical GLSL450
  25. OpEntryPoint Fragment %4 "main"
  26. OpExecutionMode %4 OriginUpperLeft
  27. OpSource ESSL 310
  28. OpName %4 "main"
  29. OpName %8 "a"
  30. OpName %10 "b"
  31. OpName %17 "FunnyPoint"
  32. OpMemberName %17 0 "x"
  33. OpMemberName %17 1 "y"
  34. OpMemberName %17 2 "z"
  35. OpName %19 "p"
  36. %2 = OpTypeVoid
  37. %3 = OpTypeFunction %2
  38. %6 = OpTypeInt 32 1
  39. %7 = OpTypePointer Function %6
  40. %12 = OpTypeBool
  41. %16 = OpTypeFloat 32
  42. %17 = OpTypeStruct %16 %16 %6
  43. %81 = OpTypeStruct %17 %16
  44. %18 = OpTypePointer Function %17
  45. %20 = OpConstant %6 0
  46. %23 = OpTypePointer Function %16
  47. %26 = OpConstant %6 1
  48. %30 = OpConstant %6 2
  49. %80 = OpUndef %16
  50. %4 = OpFunction %2 None %3
  51. %5 = OpLabel
  52. %8 = OpVariable %7 Function
  53. %10 = OpVariable %7 Function
  54. %19 = OpVariable %18 Function
  55. %9 = OpLoad %6 %8
  56. %11 = OpLoad %6 %10
  57. %100 = OpCompositeConstruct %17 %80 %80 %26
  58. %104 = OpCompositeConstruct %81 %100 %80
  59. %13 = OpIEqual %12 %9 %11
  60. OpSelectionMerge %15 None
  61. OpBranchConditional %13 %14 %25
  62. %14 = OpLabel
  63. %21 = OpLoad %6 %8
  64. %22 = OpConvertSToF %16 %21
  65. %101 = OpCompositeConstruct %17 %22 %80 %30
  66. %24 = OpAccessChain %23 %19 %20
  67. OpStore %24 %22
  68. OpBranch %15
  69. %25 = OpLabel
  70. %27 = OpLoad %6 %10
  71. %28 = OpConvertSToF %16 %27
  72. %102 = OpCompositeConstruct %17 %80 %28 %27
  73. %29 = OpAccessChain %23 %19 %26
  74. OpStore %29 %28
  75. OpBranch %15
  76. %15 = OpLabel
  77. %31 = OpAccessChain %23 %19 %20
  78. %32 = OpLoad %16 %31
  79. %33 = OpAccessChain %23 %19 %26
  80. %34 = OpLoad %16 %33
  81. %103 = OpCompositeConstruct %17 %34 %32 %9
  82. %35 = OpFAdd %16 %32 %34
  83. %36 = OpConvertFToS %6 %35
  84. %37 = OpAccessChain %7 %19 %30
  85. OpStore %37 %36
  86. OpReturn
  87. OpFunctionEnd
  88. )";
  89. const auto env = SPV_ENV_UNIVERSAL_1_4;
  90. const auto consumer = nullptr;
  91. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  92. ASSERT_TRUE(IsValid(env, context.get()));
  93. FactManager fact_manager;
  94. // Instruction does not exist.
  95. ASSERT_FALSE(TransformationCompositeExtract(
  96. MakeInstructionDescriptor(36, SpvOpIAdd, 0), 200, 101, {0})
  97. .IsApplicable(context.get(), fact_manager));
  98. // Id for composite is not a composite.
  99. ASSERT_FALSE(TransformationCompositeExtract(
  100. MakeInstructionDescriptor(36, SpvOpIAdd, 0), 200, 27, {})
  101. .IsApplicable(context.get(), fact_manager));
  102. // Composite does not dominate instruction being inserted before.
  103. ASSERT_FALSE(
  104. TransformationCompositeExtract(
  105. MakeInstructionDescriptor(37, SpvOpAccessChain, 0), 200, 101, {0})
  106. .IsApplicable(context.get(), fact_manager));
  107. // Too many indices for extraction from struct composite.
  108. ASSERT_FALSE(
  109. TransformationCompositeExtract(
  110. MakeInstructionDescriptor(24, SpvOpAccessChain, 0), 200, 101, {0, 0})
  111. .IsApplicable(context.get(), fact_manager));
  112. // Too many indices for extraction from struct composite.
  113. ASSERT_FALSE(
  114. TransformationCompositeExtract(
  115. MakeInstructionDescriptor(13, SpvOpIEqual, 0), 200, 104, {0, 0, 0})
  116. .IsApplicable(context.get(), fact_manager));
  117. // Out of bounds index for extraction from struct composite.
  118. ASSERT_FALSE(
  119. TransformationCompositeExtract(
  120. MakeInstructionDescriptor(13, SpvOpIEqual, 0), 200, 104, {0, 3})
  121. .IsApplicable(context.get(), fact_manager));
  122. // Result id already used.
  123. ASSERT_FALSE(TransformationCompositeExtract(
  124. MakeInstructionDescriptor(35, SpvOpFAdd, 0), 80, 103, {0})
  125. .IsApplicable(context.get(), fact_manager));
  126. TransformationCompositeExtract transformation_1(
  127. MakeInstructionDescriptor(36, SpvOpConvertFToS, 0), 201, 100, {2});
  128. ASSERT_TRUE(transformation_1.IsApplicable(context.get(), fact_manager));
  129. transformation_1.Apply(context.get(), &fact_manager);
  130. ASSERT_TRUE(IsValid(env, context.get()));
  131. TransformationCompositeExtract transformation_2(
  132. MakeInstructionDescriptor(37, SpvOpAccessChain, 0), 202, 104, {0, 2});
  133. ASSERT_TRUE(transformation_2.IsApplicable(context.get(), fact_manager));
  134. transformation_2.Apply(context.get(), &fact_manager);
  135. ASSERT_TRUE(IsValid(env, context.get()));
  136. TransformationCompositeExtract transformation_3(
  137. MakeInstructionDescriptor(29, SpvOpAccessChain, 0), 203, 104, {0});
  138. ASSERT_TRUE(transformation_3.IsApplicable(context.get(), fact_manager));
  139. transformation_3.Apply(context.get(), &fact_manager);
  140. ASSERT_TRUE(IsValid(env, context.get()));
  141. TransformationCompositeExtract transformation_4(
  142. MakeInstructionDescriptor(24, SpvOpStore, 0), 204, 101, {0});
  143. ASSERT_TRUE(transformation_4.IsApplicable(context.get(), fact_manager));
  144. transformation_4.Apply(context.get(), &fact_manager);
  145. ASSERT_TRUE(IsValid(env, context.get()));
  146. TransformationCompositeExtract transformation_5(
  147. MakeInstructionDescriptor(29, SpvOpBranch, 0), 205, 102, {2});
  148. ASSERT_TRUE(transformation_5.IsApplicable(context.get(), fact_manager));
  149. transformation_5.Apply(context.get(), &fact_manager);
  150. ASSERT_TRUE(IsValid(env, context.get()));
  151. TransformationCompositeExtract transformation_6(
  152. MakeInstructionDescriptor(37, SpvOpReturn, 0), 206, 103, {1});
  153. ASSERT_TRUE(transformation_6.IsApplicable(context.get(), fact_manager));
  154. transformation_6.Apply(context.get(), &fact_manager);
  155. ASSERT_TRUE(IsValid(env, context.get()));
  156. ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(201, {}),
  157. MakeDataDescriptor(100, {2}),
  158. context.get()));
  159. ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(202, {}),
  160. MakeDataDescriptor(104, {0, 2}),
  161. context.get()));
  162. ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(203, {}),
  163. MakeDataDescriptor(104, {0}),
  164. context.get()));
  165. ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(204, {}),
  166. MakeDataDescriptor(101, {0}),
  167. context.get()));
  168. ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(205, {}),
  169. MakeDataDescriptor(102, {2}),
  170. context.get()));
  171. ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(206, {}),
  172. MakeDataDescriptor(103, {1}),
  173. context.get()));
  174. std::string after_transformation = R"(
  175. OpCapability Shader
  176. %1 = OpExtInstImport "GLSL.std.450"
  177. OpMemoryModel Logical GLSL450
  178. OpEntryPoint Fragment %4 "main"
  179. OpExecutionMode %4 OriginUpperLeft
  180. OpSource ESSL 310
  181. OpName %4 "main"
  182. OpName %8 "a"
  183. OpName %10 "b"
  184. OpName %17 "FunnyPoint"
  185. OpMemberName %17 0 "x"
  186. OpMemberName %17 1 "y"
  187. OpMemberName %17 2 "z"
  188. OpName %19 "p"
  189. %2 = OpTypeVoid
  190. %3 = OpTypeFunction %2
  191. %6 = OpTypeInt 32 1
  192. %7 = OpTypePointer Function %6
  193. %12 = OpTypeBool
  194. %16 = OpTypeFloat 32
  195. %17 = OpTypeStruct %16 %16 %6
  196. %81 = OpTypeStruct %17 %16
  197. %18 = OpTypePointer Function %17
  198. %20 = OpConstant %6 0
  199. %23 = OpTypePointer Function %16
  200. %26 = OpConstant %6 1
  201. %30 = OpConstant %6 2
  202. %80 = OpUndef %16
  203. %4 = OpFunction %2 None %3
  204. %5 = OpLabel
  205. %8 = OpVariable %7 Function
  206. %10 = OpVariable %7 Function
  207. %19 = OpVariable %18 Function
  208. %9 = OpLoad %6 %8
  209. %11 = OpLoad %6 %10
  210. %100 = OpCompositeConstruct %17 %80 %80 %26
  211. %104 = OpCompositeConstruct %81 %100 %80
  212. %13 = OpIEqual %12 %9 %11
  213. OpSelectionMerge %15 None
  214. OpBranchConditional %13 %14 %25
  215. %14 = OpLabel
  216. %21 = OpLoad %6 %8
  217. %22 = OpConvertSToF %16 %21
  218. %101 = OpCompositeConstruct %17 %22 %80 %30
  219. %24 = OpAccessChain %23 %19 %20
  220. %204 = OpCompositeExtract %16 %101 0
  221. OpStore %24 %22
  222. OpBranch %15
  223. %25 = OpLabel
  224. %27 = OpLoad %6 %10
  225. %28 = OpConvertSToF %16 %27
  226. %102 = OpCompositeConstruct %17 %80 %28 %27
  227. %203 = OpCompositeExtract %17 %104 0
  228. %29 = OpAccessChain %23 %19 %26
  229. OpStore %29 %28
  230. %205 = OpCompositeExtract %6 %102 2
  231. OpBranch %15
  232. %15 = OpLabel
  233. %31 = OpAccessChain %23 %19 %20
  234. %32 = OpLoad %16 %31
  235. %33 = OpAccessChain %23 %19 %26
  236. %34 = OpLoad %16 %33
  237. %103 = OpCompositeConstruct %17 %34 %32 %9
  238. %35 = OpFAdd %16 %32 %34
  239. %201 = OpCompositeExtract %6 %100 2
  240. %36 = OpConvertFToS %6 %35
  241. %202 = OpCompositeExtract %6 %104 0 2
  242. %37 = OpAccessChain %7 %19 %30
  243. OpStore %37 %36
  244. %206 = OpCompositeExtract %16 %103 1
  245. OpReturn
  246. OpFunctionEnd
  247. )";
  248. ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
  249. }
  250. TEST(TransformationCompositeExtractTest, IllegalInsertionPoints) {
  251. std::string shader = R"(
  252. OpCapability Shader
  253. %1 = OpExtInstImport "GLSL.std.450"
  254. OpMemoryModel Logical GLSL450
  255. OpEntryPoint Fragment %4 "main" %51 %27
  256. OpExecutionMode %4 OriginUpperLeft
  257. OpSource ESSL 310
  258. OpName %4 "main"
  259. OpName %25 "buf"
  260. OpMemberName %25 0 "value"
  261. OpName %27 ""
  262. OpName %51 "color"
  263. OpMemberDecorate %25 0 Offset 0
  264. OpDecorate %25 Block
  265. OpDecorate %27 DescriptorSet 0
  266. OpDecorate %27 Binding 0
  267. OpDecorate %51 Location 0
  268. %2 = OpTypeVoid
  269. %3 = OpTypeFunction %2
  270. %6 = OpTypeFloat 32
  271. %7 = OpTypeVector %6 4
  272. %10 = OpConstant %6 0.300000012
  273. %11 = OpConstant %6 0.400000006
  274. %12 = OpConstant %6 0.5
  275. %13 = OpConstant %6 1
  276. %14 = OpConstantComposite %7 %10 %11 %12 %13
  277. %15 = OpTypeInt 32 1
  278. %18 = OpConstant %15 0
  279. %25 = OpTypeStruct %6
  280. %26 = OpTypePointer Uniform %25
  281. %27 = OpVariable %26 Uniform
  282. %28 = OpTypePointer Uniform %6
  283. %32 = OpTypeBool
  284. %103 = OpConstantTrue %32
  285. %34 = OpConstant %6 0.100000001
  286. %48 = OpConstant %15 1
  287. %50 = OpTypePointer Output %7
  288. %51 = OpVariable %50 Output
  289. %100 = OpTypePointer Function %6
  290. %4 = OpFunction %2 None %3
  291. %5 = OpLabel
  292. %101 = OpVariable %100 Function
  293. %102 = OpVariable %100 Function
  294. OpBranch %19
  295. %19 = OpLabel
  296. %60 = OpPhi %7 %14 %5 %58 %20
  297. %59 = OpPhi %15 %18 %5 %49 %20
  298. %29 = OpAccessChain %28 %27 %18
  299. %30 = OpLoad %6 %29
  300. %31 = OpConvertFToS %15 %30
  301. %33 = OpSLessThan %32 %59 %31
  302. OpLoopMerge %21 %20 None
  303. OpBranchConditional %33 %20 %21
  304. %20 = OpLabel
  305. %39 = OpCompositeExtract %6 %60 0
  306. %40 = OpFAdd %6 %39 %34
  307. %55 = OpCompositeInsert %7 %40 %60 0
  308. %44 = OpCompositeExtract %6 %60 1
  309. %45 = OpFSub %6 %44 %34
  310. %58 = OpCompositeInsert %7 %45 %55 1
  311. %49 = OpIAdd %15 %59 %48
  312. OpBranch %19
  313. %21 = OpLabel
  314. OpStore %51 %60
  315. OpSelectionMerge %105 None
  316. OpBranchConditional %103 %104 %105
  317. %104 = OpLabel
  318. OpBranch %105
  319. %105 = OpLabel
  320. OpReturn
  321. OpFunctionEnd
  322. )";
  323. const auto env = SPV_ENV_UNIVERSAL_1_4;
  324. const auto consumer = nullptr;
  325. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  326. ASSERT_TRUE(IsValid(env, context.get()));
  327. FactManager fact_manager;
  328. // Cannot insert before the OpVariables of a function.
  329. ASSERT_FALSE(
  330. TransformationCompositeExtract(
  331. MakeInstructionDescriptor(101, SpvOpVariable, 0), 200, 14, {0})
  332. .IsApplicable(context.get(), fact_manager));
  333. ASSERT_FALSE(
  334. TransformationCompositeExtract(
  335. MakeInstructionDescriptor(101, SpvOpVariable, 1), 200, 14, {1})
  336. .IsApplicable(context.get(), fact_manager));
  337. ASSERT_FALSE(
  338. TransformationCompositeExtract(
  339. MakeInstructionDescriptor(102, SpvOpVariable, 0), 200, 14, {1})
  340. .IsApplicable(context.get(), fact_manager));
  341. // OK to insert right after the OpVariables.
  342. ASSERT_FALSE(TransformationCompositeExtract(
  343. MakeInstructionDescriptor(102, SpvOpBranch, 1), 200, 14, {1})
  344. .IsApplicable(context.get(), fact_manager));
  345. // Cannot insert before the OpPhis of a block.
  346. ASSERT_FALSE(TransformationCompositeExtract(
  347. MakeInstructionDescriptor(60, SpvOpPhi, 0), 200, 14, {2})
  348. .IsApplicable(context.get(), fact_manager));
  349. ASSERT_FALSE(TransformationCompositeExtract(
  350. MakeInstructionDescriptor(59, SpvOpPhi, 0), 200, 14, {3})
  351. .IsApplicable(context.get(), fact_manager));
  352. // OK to insert after the OpPhis.
  353. ASSERT_TRUE(
  354. TransformationCompositeExtract(
  355. MakeInstructionDescriptor(59, SpvOpAccessChain, 0), 200, 14, {3})
  356. .IsApplicable(context.get(), fact_manager));
  357. // Cannot insert before OpLoopMerge
  358. ASSERT_FALSE(TransformationCompositeExtract(
  359. MakeInstructionDescriptor(33, SpvOpBranchConditional, 0),
  360. 200, 14, {3})
  361. .IsApplicable(context.get(), fact_manager));
  362. // Cannot insert before OpSelectionMerge
  363. ASSERT_FALSE(TransformationCompositeExtract(
  364. MakeInstructionDescriptor(21, SpvOpBranchConditional, 0),
  365. 200, 14, {2})
  366. .IsApplicable(context.get(), fact_manager));
  367. }
  368. } // namespace
  369. } // namespace fuzz
  370. } // namespace spvtools