replace_invalid_opc_test.cpp 23 KB

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