replace_invalid_opc_test.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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 <string>
  15. #include <vector>
  16. #include "pass_utils.h"
  17. #include "test/opt/assembly_builder.h"
  18. #include "test/opt/pass_fixture.h"
  19. namespace spvtools {
  20. namespace opt {
  21. namespace {
  22. using ReplaceInvalidOpcodeTest = PassTest<::testing::Test>;
  23. TEST_F(ReplaceInvalidOpcodeTest, ReplaceInstruction) {
  24. const std::string text = R"(
  25. ; CHECK: [[special_const:%\w+]] = OpConstant %float -6.2598534e+18
  26. ; CHECK: [[constant:%\w+]] = OpConstantComposite %v4float [[special_const]] [[special_const]] [[special_const]] [[special_const]]
  27. ; CHECK-NOT: OpImageSampleImplicitLod
  28. ; CHECK: OpStore [[:%\w+]] [[constant]]
  29. OpCapability Shader
  30. %1 = OpExtInstImport "GLSL.std.450"
  31. OpMemoryModel Logical GLSL450
  32. OpEntryPoint Vertex %main "main" %3 %gl_VertexIndex %5
  33. OpSource GLSL 400
  34. OpSourceExtension "GL_ARB_separate_shader_objects"
  35. OpSourceExtension "GL_ARB_shading_language_420pack"
  36. OpName %main "main"
  37. OpDecorate %3 Location 0
  38. OpDecorate %gl_VertexIndex BuiltIn VertexIndex
  39. OpMemberDecorate %_struct_6 0 BuiltIn Position
  40. OpDecorate %_struct_6 Block
  41. %void = OpTypeVoid
  42. %8 = OpTypeFunction %void
  43. %float = OpTypeFloat 32
  44. %10 = OpTypeImage %float 2D 0 0 0 1 Unknown
  45. %_ptr_UniformConstant_10 = OpTypePointer UniformConstant %10
  46. %12 = OpTypeSampler
  47. %_ptr_UniformConstant_12 = OpTypePointer UniformConstant %12
  48. %14 = OpTypeSampledImage %10
  49. %v4float = OpTypeVector %float 4
  50. %v2float = OpTypeVector %float 2
  51. %_ptr_Output_v4float = OpTypePointer Output %v4float
  52. %3 = OpVariable %_ptr_Output_v4float Output
  53. %int = OpTypeInt 32 1
  54. %_ptr_Input_int = OpTypePointer Input %int
  55. %gl_VertexIndex = OpVariable %_ptr_Input_int Input
  56. %_struct_6 = OpTypeStruct %v4float
  57. %_ptr_Output__struct_6 = OpTypePointer Output %_struct_6
  58. %5 = OpVariable %_ptr_Output__struct_6 Output
  59. %int_0 = OpConstant %int 0
  60. %float_0 = OpConstant %float 0
  61. %23 = OpConstantComposite %v2float %float_0 %float_0
  62. %24 = OpVariable %_ptr_UniformConstant_10 UniformConstant
  63. %25 = OpVariable %_ptr_UniformConstant_12 UniformConstant
  64. %main = OpFunction %void None %8
  65. %26 = OpLabel
  66. %27 = OpLoad %12 %25
  67. %28 = OpLoad %10 %24
  68. %29 = OpSampledImage %14 %28 %27
  69. %30 = OpImageSampleImplicitLod %v4float %29 %23
  70. %31 = OpAccessChain %_ptr_Output_v4float %5 %int_0
  71. OpStore %31 %30
  72. OpReturn
  73. OpFunctionEnd)";
  74. SinglePassRunAndMatch<ReplaceInvalidOpcodePass>(text, false);
  75. }
  76. TEST_F(ReplaceInvalidOpcodeTest, ReplaceInstructionInNonEntryPoint) {
  77. const std::string text = R"(
  78. ; CHECK: [[special_const:%\w+]] = OpConstant %float -6.2598534e+18
  79. ; CHECK: [[constant:%\w+]] = OpConstantComposite %v4float [[special_const]] [[special_const]] [[special_const]] [[special_const]]
  80. ; CHECK-NOT: OpImageSampleImplicitLod
  81. ; CHECK: OpStore [[:%\w+]] [[constant]]
  82. OpCapability Shader
  83. %1 = OpExtInstImport "GLSL.std.450"
  84. OpMemoryModel Logical GLSL450
  85. OpEntryPoint Vertex %main "main" %3 %gl_VertexIndex %5
  86. OpSource GLSL 400
  87. OpSourceExtension "GL_ARB_separate_shader_objects"
  88. OpSourceExtension "GL_ARB_shading_language_420pack"
  89. OpName %main "main"
  90. OpDecorate %3 Location 0
  91. OpDecorate %gl_VertexIndex BuiltIn VertexIndex
  92. OpMemberDecorate %_struct_6 0 BuiltIn Position
  93. OpDecorate %_struct_6 Block
  94. %void = OpTypeVoid
  95. %8 = OpTypeFunction %void
  96. %float = OpTypeFloat 32
  97. %10 = OpTypeImage %float 2D 0 0 0 1 Unknown
  98. %_ptr_UniformConstant_10 = OpTypePointer UniformConstant %10
  99. %12 = OpTypeSampler
  100. %_ptr_UniformConstant_12 = OpTypePointer UniformConstant %12
  101. %14 = OpTypeSampledImage %10
  102. %v4float = OpTypeVector %float 4
  103. %v2float = OpTypeVector %float 2
  104. %_ptr_Output_v4float = OpTypePointer Output %v4float
  105. %3 = OpVariable %_ptr_Output_v4float Output
  106. %int = OpTypeInt 32 1
  107. %_ptr_Input_int = OpTypePointer Input %int
  108. %gl_VertexIndex = OpVariable %_ptr_Input_int Input
  109. %_struct_6 = OpTypeStruct %v4float
  110. %_ptr_Output__struct_6 = OpTypePointer Output %_struct_6
  111. %5 = OpVariable %_ptr_Output__struct_6 Output
  112. %int_0 = OpConstant %int 0
  113. %float_0 = OpConstant %float 0
  114. %23 = OpConstantComposite %v2float %float_0 %float_0
  115. %24 = OpVariable %_ptr_UniformConstant_10 UniformConstant
  116. %25 = OpVariable %_ptr_UniformConstant_12 UniformConstant
  117. %main = OpFunction %void None %8
  118. %26 = OpLabel
  119. %27 = OpFunctionCall %void %28
  120. OpReturn
  121. OpFunctionEnd
  122. %28 = OpFunction %void None %8
  123. %29 = OpLabel
  124. %30 = OpLoad %12 %25
  125. %31 = OpLoad %10 %24
  126. %32 = OpSampledImage %14 %31 %30
  127. %33 = OpImageSampleImplicitLod %v4float %32 %23
  128. %34 = OpAccessChain %_ptr_Output_v4float %5 %int_0
  129. OpStore %34 %33
  130. OpReturn
  131. OpFunctionEnd)";
  132. SinglePassRunAndMatch<ReplaceInvalidOpcodePass>(text, false);
  133. }
  134. TEST_F(ReplaceInvalidOpcodeTest, ReplaceInstructionMultipleEntryPoints) {
  135. const std::string text = R"(
  136. ; CHECK: [[special_const:%\w+]] = OpConstant %float -6.2598534e+18
  137. ; CHECK: [[constant:%\w+]] = OpConstantComposite %v4float [[special_const]] [[special_const]] [[special_const]] [[special_const]]
  138. ; CHECK-NOT: OpImageSampleImplicitLod
  139. ; CHECK: OpStore [[:%\w+]] [[constant]]
  140. ; CHECK-NOT: OpImageSampleImplicitLod
  141. ; CHECK: OpStore [[:%\w+]] [[constant]]
  142. OpCapability Shader
  143. %1 = OpExtInstImport "GLSL.std.450"
  144. OpMemoryModel Logical GLSL450
  145. OpEntryPoint Vertex %main "main" %3 %gl_VertexIndex %5
  146. OpEntryPoint Vertex %main2 "main2" %3 %gl_VertexIndex %5
  147. OpSource GLSL 400
  148. OpSourceExtension "GL_ARB_separate_shader_objects"
  149. OpSourceExtension "GL_ARB_shading_language_420pack"
  150. OpName %main "main"
  151. OpName %main2 "main2"
  152. OpDecorate %3 Location 0
  153. OpDecorate %gl_VertexIndex BuiltIn VertexIndex
  154. OpMemberDecorate %_struct_6 0 BuiltIn Position
  155. OpDecorate %_struct_6 Block
  156. %void = OpTypeVoid
  157. %8 = OpTypeFunction %void
  158. %float = OpTypeFloat 32
  159. %10 = OpTypeImage %float 2D 0 0 0 1 Unknown
  160. %_ptr_UniformConstant_10 = OpTypePointer UniformConstant %10
  161. %12 = OpTypeSampler
  162. %_ptr_UniformConstant_12 = OpTypePointer UniformConstant %12
  163. %14 = OpTypeSampledImage %10
  164. %v4float = OpTypeVector %float 4
  165. %v2float = OpTypeVector %float 2
  166. %_ptr_Output_v4float = OpTypePointer Output %v4float
  167. %3 = OpVariable %_ptr_Output_v4float Output
  168. %int = OpTypeInt 32 1
  169. %_ptr_Input_int = OpTypePointer Input %int
  170. %gl_VertexIndex = OpVariable %_ptr_Input_int Input
  171. %_struct_6 = OpTypeStruct %v4float
  172. %_ptr_Output__struct_6 = OpTypePointer Output %_struct_6
  173. %5 = OpVariable %_ptr_Output__struct_6 Output
  174. %int_0 = OpConstant %int 0
  175. %float_0 = OpConstant %float 0
  176. %23 = OpConstantComposite %v2float %float_0 %float_0
  177. %24 = OpVariable %_ptr_UniformConstant_10 UniformConstant
  178. %25 = OpVariable %_ptr_UniformConstant_12 UniformConstant
  179. %main = OpFunction %void None %8
  180. %26 = OpLabel
  181. %27 = OpLoad %12 %25
  182. %28 = OpLoad %10 %24
  183. %29 = OpSampledImage %14 %28 %27
  184. %30 = OpImageSampleImplicitLod %v4float %29 %23
  185. %31 = OpAccessChain %_ptr_Output_v4float %5 %int_0
  186. OpStore %31 %30
  187. OpReturn
  188. OpFunctionEnd
  189. %main2 = OpFunction %void None %8
  190. %46 = OpLabel
  191. %47 = OpLoad %12 %25
  192. %48 = OpLoad %10 %24
  193. %49 = OpSampledImage %14 %48 %47
  194. %50 = OpImageSampleImplicitLod %v4float %49 %23
  195. %51 = OpAccessChain %_ptr_Output_v4float %5 %int_0
  196. OpStore %51 %50
  197. OpReturn
  198. OpFunctionEnd)";
  199. SinglePassRunAndMatch<ReplaceInvalidOpcodePass>(text, false);
  200. }
  201. TEST_F(ReplaceInvalidOpcodeTest, DontReplaceInstruction) {
  202. const std::string text = R"(
  203. OpCapability Shader
  204. %1 = OpExtInstImport "GLSL.std.450"
  205. OpMemoryModel Logical GLSL450
  206. OpEntryPoint Fragment %main "main" %3 %gl_VertexIndex %5
  207. OpSource GLSL 400
  208. OpSourceExtension "GL_ARB_separate_shader_objects"
  209. OpSourceExtension "GL_ARB_shading_language_420pack"
  210. OpName %main "main"
  211. OpDecorate %3 Location 0
  212. OpDecorate %gl_VertexIndex BuiltIn VertexIndex
  213. OpMemberDecorate %_struct_6 0 BuiltIn Position
  214. OpDecorate %_struct_6 Block
  215. %void = OpTypeVoid
  216. %8 = OpTypeFunction %void
  217. %float = OpTypeFloat 32
  218. %10 = OpTypeImage %float 2D 0 0 0 1 Unknown
  219. %_ptr_UniformConstant_10 = OpTypePointer UniformConstant %10
  220. %12 = OpTypeSampler
  221. %_ptr_UniformConstant_12 = OpTypePointer UniformConstant %12
  222. %14 = OpTypeSampledImage %10
  223. %v4float = OpTypeVector %float 4
  224. %v2float = OpTypeVector %float 2
  225. %_ptr_Output_v4float = OpTypePointer Output %v4float
  226. %3 = OpVariable %_ptr_Output_v4float Output
  227. %int = OpTypeInt 32 1
  228. %_ptr_Input_int = OpTypePointer Input %int
  229. %gl_VertexIndex = OpVariable %_ptr_Input_int Input
  230. %_struct_6 = OpTypeStruct %v4float
  231. %_ptr_Output__struct_6 = OpTypePointer Output %_struct_6
  232. %5 = OpVariable %_ptr_Output__struct_6 Output
  233. %int_0 = OpConstant %int 0
  234. %float_0 = OpConstant %float 0
  235. %23 = OpConstantComposite %v2float %float_0 %float_0
  236. %24 = OpVariable %_ptr_UniformConstant_10 UniformConstant
  237. %25 = OpVariable %_ptr_UniformConstant_12 UniformConstant
  238. %main = OpFunction %void None %8
  239. %26 = OpLabel
  240. %27 = OpLoad %12 %25
  241. %28 = OpLoad %10 %24
  242. %29 = OpSampledImage %14 %28 %27
  243. %30 = OpImageSampleImplicitLod %v4float %29 %23
  244. %31 = OpAccessChain %_ptr_Output_v4float %5 %int_0
  245. OpStore %31 %30
  246. OpReturn
  247. OpFunctionEnd)";
  248. auto result = SinglePassRunAndDisassemble<ReplaceInvalidOpcodePass>(
  249. text, /* skip_nop = */ true, /* do_validation = */ false);
  250. EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
  251. }
  252. TEST_F(ReplaceInvalidOpcodeTest, MultipleEntryPointsDifferentStage) {
  253. const std::string text = R"(
  254. OpCapability Shader
  255. %1 = OpExtInstImport "GLSL.std.450"
  256. OpMemoryModel Logical GLSL450
  257. OpEntryPoint Vertex %main "main" %3 %gl_VertexIndex %5
  258. OpEntryPoint Fragment %main2 "main2" %3 %gl_VertexIndex %5
  259. OpSource GLSL 400
  260. OpSourceExtension "GL_ARB_separate_shader_objects"
  261. OpSourceExtension "GL_ARB_shading_language_420pack"
  262. OpName %main "main"
  263. OpName %main2 "main2"
  264. OpDecorate %3 Location 0
  265. OpDecorate %gl_VertexIndex BuiltIn VertexIndex
  266. OpMemberDecorate %_struct_6 0 BuiltIn Position
  267. OpDecorate %_struct_6 Block
  268. %void = OpTypeVoid
  269. %8 = OpTypeFunction %void
  270. %float = OpTypeFloat 32
  271. %10 = OpTypeImage %float 2D 0 0 0 1 Unknown
  272. %_ptr_UniformConstant_10 = OpTypePointer UniformConstant %10
  273. %12 = OpTypeSampler
  274. %_ptr_UniformConstant_12 = OpTypePointer UniformConstant %12
  275. %14 = OpTypeSampledImage %10
  276. %v4float = OpTypeVector %float 4
  277. %v2float = OpTypeVector %float 2
  278. %_ptr_Output_v4float = OpTypePointer Output %v4float
  279. %3 = OpVariable %_ptr_Output_v4float Output
  280. %int = OpTypeInt 32 1
  281. %_ptr_Input_int = OpTypePointer Input %int
  282. %gl_VertexIndex = OpVariable %_ptr_Input_int Input
  283. %_struct_6 = OpTypeStruct %v4float
  284. %_ptr_Output__struct_6 = OpTypePointer Output %_struct_6
  285. %5 = OpVariable %_ptr_Output__struct_6 Output
  286. %int_0 = OpConstant %int 0
  287. %float_0 = OpConstant %float 0
  288. %23 = OpConstantComposite %v2float %float_0 %float_0
  289. %24 = OpVariable %_ptr_UniformConstant_10 UniformConstant
  290. %25 = OpVariable %_ptr_UniformConstant_12 UniformConstant
  291. %main = OpFunction %void None %8
  292. %26 = OpLabel
  293. %27 = OpLoad %12 %25
  294. %28 = OpLoad %10 %24
  295. %29 = OpSampledImage %14 %28 %27
  296. %30 = OpImageSampleImplicitLod %v4float %29 %23
  297. %31 = OpAccessChain %_ptr_Output_v4float %5 %int_0
  298. OpStore %31 %30
  299. OpReturn
  300. OpFunctionEnd
  301. %main2 = OpFunction %void None %8
  302. %46 = OpLabel
  303. %47 = OpLoad %12 %25
  304. %48 = OpLoad %10 %24
  305. %49 = OpSampledImage %14 %48 %47
  306. %50 = OpImageSampleImplicitLod %v4float %49 %23
  307. %51 = OpAccessChain %_ptr_Output_v4float %5 %int_0
  308. OpStore %51 %50
  309. OpReturn
  310. OpFunctionEnd)";
  311. auto result = SinglePassRunAndDisassemble<ReplaceInvalidOpcodePass>(
  312. text, /* skip_nop = */ true, /* do_validation = */ false);
  313. EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
  314. }
  315. TEST_F(ReplaceInvalidOpcodeTest, DontReplaceLinkage) {
  316. const std::string text = R"(
  317. OpCapability Shader
  318. OpCapability Linkage
  319. %1 = OpExtInstImport "GLSL.std.450"
  320. OpMemoryModel Logical GLSL450
  321. OpEntryPoint Vertex %main "main" %3 %gl_VertexIndex %5
  322. OpSource GLSL 400
  323. OpSourceExtension "GL_ARB_separate_shader_objects"
  324. OpSourceExtension "GL_ARB_shading_language_420pack"
  325. OpName %main "main"
  326. OpDecorate %3 Location 0
  327. OpDecorate %gl_VertexIndex BuiltIn VertexIndex
  328. OpMemberDecorate %_struct_6 0 BuiltIn Position
  329. OpDecorate %_struct_6 Block
  330. %void = OpTypeVoid
  331. %8 = OpTypeFunction %void
  332. %float = OpTypeFloat 32
  333. %10 = OpTypeImage %float 2D 0 0 0 1 Unknown
  334. %_ptr_UniformConstant_10 = OpTypePointer UniformConstant %10
  335. %12 = OpTypeSampler
  336. %_ptr_UniformConstant_12 = OpTypePointer UniformConstant %12
  337. %14 = OpTypeSampledImage %10
  338. %v4float = OpTypeVector %float 4
  339. %v2float = OpTypeVector %float 2
  340. %_ptr_Output_v4float = OpTypePointer Output %v4float
  341. %3 = OpVariable %_ptr_Output_v4float Output
  342. %int = OpTypeInt 32 1
  343. %_ptr_Input_int = OpTypePointer Input %int
  344. %gl_VertexIndex = OpVariable %_ptr_Input_int Input
  345. %_struct_6 = OpTypeStruct %v4float
  346. %_ptr_Output__struct_6 = OpTypePointer Output %_struct_6
  347. %5 = OpVariable %_ptr_Output__struct_6 Output
  348. %int_0 = OpConstant %int 0
  349. %float_0 = OpConstant %float 0
  350. %23 = OpConstantComposite %v2float %float_0 %float_0
  351. %24 = OpVariable %_ptr_UniformConstant_10 UniformConstant
  352. %25 = OpVariable %_ptr_UniformConstant_12 UniformConstant
  353. %main = OpFunction %void None %8
  354. %26 = OpLabel
  355. %27 = OpLoad %12 %25
  356. %28 = OpLoad %10 %24
  357. %29 = OpSampledImage %14 %28 %27
  358. %30 = OpImageSampleImplicitLod %v4float %29 %23
  359. %31 = OpAccessChain %_ptr_Output_v4float %5 %int_0
  360. OpStore %31 %30
  361. OpReturn
  362. OpFunctionEnd)";
  363. auto result = SinglePassRunAndDisassemble<ReplaceInvalidOpcodePass>(
  364. text, /* skip_nop = */ true, /* do_validation = */ false);
  365. EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
  366. }
  367. TEST_F(ReplaceInvalidOpcodeTest, BarrierDontReplace) {
  368. const std::string text = R"(
  369. OpCapability Shader
  370. %1 = OpExtInstImport "GLSL.std.450"
  371. OpMemoryModel Logical GLSL450
  372. OpEntryPoint GLCompute %main "main"
  373. OpExecutionMode %main LocalSize 1 1 1
  374. OpSource GLSL 450
  375. OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
  376. OpSourceExtension "GL_GOOGLE_include_directive"
  377. OpName %main "main"
  378. %void = OpTypeVoid
  379. %3 = OpTypeFunction %void
  380. %uint = OpTypeInt 32 0
  381. %uint_2 = OpConstant %uint 2
  382. %uint_264 = OpConstant %uint 264
  383. %main = OpFunction %void None %3
  384. %5 = OpLabel
  385. OpControlBarrier %uint_2 %uint_2 %uint_264
  386. OpReturn
  387. OpFunctionEnd)";
  388. SetTargetEnv(SPV_ENV_UNIVERSAL_1_2);
  389. auto result = SinglePassRunAndDisassemble<ReplaceInvalidOpcodePass>(
  390. text, /* skip_nop = */ true, /* do_validation = */ false);
  391. EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
  392. }
  393. TEST_F(ReplaceInvalidOpcodeTest, BarrierReplace) {
  394. const std::string text = R"(
  395. ; CHECK-NOT: OpControlBarrier
  396. OpCapability Shader
  397. %1 = OpExtInstImport "GLSL.std.450"
  398. OpMemoryModel Logical GLSL450
  399. OpEntryPoint Vertex %main "main"
  400. OpExecutionMode %main LocalSize 1 1 1
  401. OpSource GLSL 450
  402. OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
  403. OpSourceExtension "GL_GOOGLE_include_directive"
  404. OpName %main "main"
  405. %void = OpTypeVoid
  406. %3 = OpTypeFunction %void
  407. %uint = OpTypeInt 32 0
  408. %uint_2 = OpConstant %uint 2
  409. %uint_264 = OpConstant %uint 264
  410. %main = OpFunction %void None %3
  411. %5 = OpLabel
  412. OpControlBarrier %uint_2 %uint_2 %uint_264
  413. OpReturn
  414. OpFunctionEnd)";
  415. SetTargetEnv(SPV_ENV_UNIVERSAL_1_2);
  416. SinglePassRunAndMatch<ReplaceInvalidOpcodePass>(text, false);
  417. }
  418. // Since version 1.3 OpControlBarriers are allowed is more shaders.
  419. // https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpControlBarrier
  420. TEST_F(ReplaceInvalidOpcodeTest, BarrierDontReplaceV13) {
  421. const std::string text = R"(
  422. OpCapability Shader
  423. %1 = OpExtInstImport "GLSL.std.450"
  424. OpMemoryModel Logical GLSL450
  425. OpEntryPoint Vertex %main "main"
  426. OpExecutionMode %main LocalSize 1 1 1
  427. OpSource GLSL 450
  428. OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
  429. OpSourceExtension "GL_GOOGLE_include_directive"
  430. OpName %main "main"
  431. %void = OpTypeVoid
  432. %3 = OpTypeFunction %void
  433. %uint = OpTypeInt 32 0
  434. %uint_2 = OpConstant %uint 2
  435. %uint_264 = OpConstant %uint 264
  436. %main = OpFunction %void None %3
  437. %5 = OpLabel
  438. OpControlBarrier %uint_2 %uint_2 %uint_264
  439. OpReturn
  440. OpFunctionEnd)";
  441. SetTargetEnv(SPV_ENV_UNIVERSAL_1_3);
  442. auto result = SinglePassRunAndDisassemble<ReplaceInvalidOpcodePass>(
  443. text, /* skip_nop = */ true, /* do_validation = */ false);
  444. EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
  445. }
  446. TEST_F(ReplaceInvalidOpcodeTest, MessageTest) {
  447. const std::string text = R"(
  448. OpCapability Shader
  449. %1 = OpExtInstImport "GLSL.std.450"
  450. OpMemoryModel Logical GLSL450
  451. OpEntryPoint Vertex %main "main" %3 %gl_VertexIndex %5
  452. OpSource GLSL 400
  453. %6 = OpString "test.hlsl"
  454. OpSourceExtension "GL_ARB_separate_shader_objects"
  455. OpSourceExtension "GL_ARB_shading_language_420pack"
  456. OpName %main "main"
  457. OpDecorate %3 Location 0
  458. OpDecorate %gl_VertexIndex BuiltIn VertexIndex
  459. OpMemberDecorate %_struct_7 0 BuiltIn Position
  460. OpDecorate %_struct_7 Block
  461. %void = OpTypeVoid
  462. %9 = OpTypeFunction %void
  463. %float = OpTypeFloat 32
  464. %11 = OpTypeImage %float 2D 0 0 0 1 Unknown
  465. %_ptr_UniformConstant_11 = OpTypePointer UniformConstant %11
  466. %13 = OpTypeSampler
  467. %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
  468. %15 = OpTypeSampledImage %11
  469. %v4float = OpTypeVector %float 4
  470. %v2float = OpTypeVector %float 2
  471. %_ptr_Output_v4float = OpTypePointer Output %v4float
  472. %3 = OpVariable %_ptr_Output_v4float Output
  473. %int = OpTypeInt 32 1
  474. %_ptr_Input_int = OpTypePointer Input %int
  475. %gl_VertexIndex = OpVariable %_ptr_Input_int Input
  476. %_struct_7 = OpTypeStruct %v4float
  477. %_ptr_Output__struct_7 = OpTypePointer Output %_struct_7
  478. %5 = OpVariable %_ptr_Output__struct_7 Output
  479. %int_0 = OpConstant %int 0
  480. %float_0 = OpConstant %float 0
  481. %24 = OpConstantComposite %v2float %float_0 %float_0
  482. %25 = OpVariable %_ptr_UniformConstant_11 UniformConstant
  483. %26 = OpVariable %_ptr_UniformConstant_13 UniformConstant
  484. %main = OpFunction %void None %9
  485. %27 = OpLabel
  486. OpLine %6 2 4
  487. %28 = OpLoad %13 %26
  488. %29 = OpLoad %11 %25
  489. %30 = OpSampledImage %15 %29 %28
  490. %31 = OpImageSampleImplicitLod %v4float %30 %24
  491. %32 = OpAccessChain %_ptr_Output_v4float %5 %int_0
  492. OpStore %32 %31
  493. OpReturn
  494. OpFunctionEnd)";
  495. std::vector<Message> messages = {
  496. {SPV_MSG_WARNING, "test.hlsl", 2, 4,
  497. "Removing ImageSampleImplicitLod instruction because of incompatible "
  498. "execution model."}};
  499. SetMessageConsumer(GetTestMessageConsumer(messages));
  500. auto result = SinglePassRunAndDisassemble<ReplaceInvalidOpcodePass>(
  501. text, /* skip_nop = */ true, /* do_validation = */ false);
  502. EXPECT_EQ(Pass::Status::SuccessWithChange, std::get<1>(result));
  503. }
  504. TEST_F(ReplaceInvalidOpcodeTest, MultipleMessageTest) {
  505. const std::string text = R"(
  506. OpCapability Shader
  507. %1 = OpExtInstImport "GLSL.std.450"
  508. OpMemoryModel Logical GLSL450
  509. OpEntryPoint Vertex %main "main" %3 %gl_VertexIndex %5
  510. OpSource GLSL 400
  511. %6 = OpString "test.hlsl"
  512. OpSourceExtension "GL_ARB_separate_shader_objects"
  513. OpSourceExtension "GL_ARB_shading_language_420pack"
  514. OpName %main "main"
  515. OpDecorate %3 Location 0
  516. OpDecorate %gl_VertexIndex BuiltIn VertexIndex
  517. OpMemberDecorate %_struct_7 0 BuiltIn Position
  518. OpDecorate %_struct_7 Block
  519. %void = OpTypeVoid
  520. %9 = OpTypeFunction %void
  521. %float = OpTypeFloat 32
  522. %11 = OpTypeImage %float 2D 0 0 0 1 Unknown
  523. %_ptr_UniformConstant_11 = OpTypePointer UniformConstant %11
  524. %13 = OpTypeSampler
  525. %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
  526. %15 = OpTypeSampledImage %11
  527. %v4float = OpTypeVector %float 4
  528. %v2float = OpTypeVector %float 2
  529. %_ptr_Output_v4float = OpTypePointer Output %v4float
  530. %3 = OpVariable %_ptr_Output_v4float Output
  531. %int = OpTypeInt 32 1
  532. %_ptr_Input_int = OpTypePointer Input %int
  533. %gl_VertexIndex = OpVariable %_ptr_Input_int Input
  534. %_struct_7 = OpTypeStruct %v4float
  535. %_ptr_Output__struct_7 = OpTypePointer Output %_struct_7
  536. %5 = OpVariable %_ptr_Output__struct_7 Output
  537. %int_0 = OpConstant %int 0
  538. %float_0 = OpConstant %float 0
  539. %24 = OpConstantComposite %v2float %float_0 %float_0
  540. %25 = OpVariable %_ptr_UniformConstant_11 UniformConstant
  541. %26 = OpVariable %_ptr_UniformConstant_13 UniformConstant
  542. %main = OpFunction %void None %9
  543. %27 = OpLabel
  544. OpLine %6 2 4
  545. %28 = OpLoad %13 %26
  546. %29 = OpLoad %11 %25
  547. %30 = OpSampledImage %15 %29 %28
  548. %31 = OpImageSampleImplicitLod %v4float %30 %24
  549. OpLine %6 12 4
  550. %41 = OpImageSampleProjImplicitLod %v4float %30 %24
  551. %32 = OpAccessChain %_ptr_Output_v4float %5 %int_0
  552. OpStore %32 %31
  553. OpReturn
  554. OpFunctionEnd)";
  555. std::vector<Message> messages = {
  556. {SPV_MSG_WARNING, "test.hlsl", 2, 4,
  557. "Removing ImageSampleImplicitLod instruction because of incompatible "
  558. "execution model."},
  559. {SPV_MSG_WARNING, "test.hlsl", 12, 4,
  560. "Removing ImageSampleProjImplicitLod instruction because of "
  561. "incompatible "
  562. "execution model."}};
  563. SetMessageConsumer(GetTestMessageConsumer(messages));
  564. auto result = SinglePassRunAndDisassemble<ReplaceInvalidOpcodePass>(
  565. text, /* skip_nop = */ true, /* do_validation = */ false);
  566. EXPECT_EQ(Pass::Status::SuccessWithChange, std::get<1>(result));
  567. }
  568. } // namespace
  569. } // namespace opt
  570. } // namespace spvtools