ccp_test.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  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 "gmock/gmock.h"
  16. #include "gtest/gtest.h"
  17. #include "source/opt/ccp_pass.h"
  18. #include "test/opt/pass_fixture.h"
  19. #include "test/opt/pass_utils.h"
  20. namespace spvtools {
  21. namespace opt {
  22. namespace {
  23. using CCPTest = PassTest<::testing::Test>;
  24. TEST_F(CCPTest, PropagateThroughPhis) {
  25. const std::string spv_asm = R"(
  26. OpCapability Shader
  27. %1 = OpExtInstImport "GLSL.std.450"
  28. OpMemoryModel Logical GLSL450
  29. OpEntryPoint Fragment %main "main" %x %outparm
  30. OpExecutionMode %main OriginUpperLeft
  31. OpSource GLSL 450
  32. OpName %main "main"
  33. OpName %x "x"
  34. OpName %outparm "outparm"
  35. OpDecorate %x Flat
  36. OpDecorate %x Location 0
  37. OpDecorate %outparm Location 0
  38. %void = OpTypeVoid
  39. %3 = OpTypeFunction %void
  40. %int = OpTypeInt 32 1
  41. %bool = OpTypeBool
  42. %_ptr_Function_int = OpTypePointer Function %int
  43. %int_4 = OpConstant %int 4
  44. %int_3 = OpConstant %int 3
  45. %int_1 = OpConstant %int 1
  46. %_ptr_Input_int = OpTypePointer Input %int
  47. %x = OpVariable %_ptr_Input_int Input
  48. %_ptr_Output_int = OpTypePointer Output %int
  49. %outparm = OpVariable %_ptr_Output_int Output
  50. %main = OpFunction %void None %3
  51. %4 = OpLabel
  52. %5 = OpLoad %int %x
  53. %9 = OpIAdd %int %int_1 %int_3
  54. %6 = OpSGreaterThan %bool %5 %int_3
  55. OpSelectionMerge %25 None
  56. OpBranchConditional %6 %22 %23
  57. %22 = OpLabel
  58. ; CHECK: OpCopyObject %int %int_4
  59. %7 = OpCopyObject %int %9
  60. OpBranch %25
  61. %23 = OpLabel
  62. %8 = OpCopyObject %int %int_4
  63. OpBranch %25
  64. %25 = OpLabel
  65. ; %int_4 should have propagated to both OpPhi operands.
  66. ; CHECK: OpPhi %int %int_4 {{%\d+}} %int_4 {{%\d+}}
  67. %35 = OpPhi %int %7 %22 %8 %23
  68. ; This function always returns 4. DCE should get rid of everything else.
  69. ; CHECK OpStore %outparm %int_4
  70. OpStore %outparm %35
  71. OpReturn
  72. OpFunctionEnd
  73. )";
  74. SinglePassRunAndMatch<CCPPass>(spv_asm, true);
  75. }
  76. TEST_F(CCPTest, SimplifyConditionals) {
  77. const std::string spv_asm = R"(
  78. OpCapability Shader
  79. %1 = OpExtInstImport "GLSL.std.450"
  80. OpMemoryModel Logical GLSL450
  81. OpEntryPoint Fragment %main "main" %outparm
  82. OpExecutionMode %main OriginUpperLeft
  83. OpSource GLSL 450
  84. OpName %main "main"
  85. OpName %outparm "outparm"
  86. OpDecorate %outparm Location 0
  87. %void = OpTypeVoid
  88. %3 = OpTypeFunction %void
  89. %int = OpTypeInt 32 1
  90. %bool = OpTypeBool
  91. %_ptr_Function_int = OpTypePointer Function %int
  92. %int_4 = OpConstant %int 4
  93. %int_3 = OpConstant %int 3
  94. %int_1 = OpConstant %int 1
  95. %_ptr_Output_int = OpTypePointer Output %int
  96. %outparm = OpVariable %_ptr_Output_int Output
  97. %main = OpFunction %void None %3
  98. %4 = OpLabel
  99. %9 = OpIAdd %int %int_4 %int_3
  100. %6 = OpSGreaterThan %bool %9 %int_3
  101. OpSelectionMerge %25 None
  102. ; CHECK: OpBranchConditional %true [[bb_taken:%\d+]] [[bb_not_taken:%\d+]]
  103. OpBranchConditional %6 %22 %23
  104. ; CHECK: [[bb_taken]] = OpLabel
  105. %22 = OpLabel
  106. ; CHECK: OpCopyObject %int %int_7
  107. %7 = OpCopyObject %int %9
  108. OpBranch %25
  109. ; CHECK: [[bb_not_taken]] = OpLabel
  110. %23 = OpLabel
  111. ; CHECK: [[id_not_evaluated:%\d+]] = OpCopyObject %int %int_4
  112. %8 = OpCopyObject %int %int_4
  113. OpBranch %25
  114. %25 = OpLabel
  115. ; %int_7 should have propagated to the first OpPhi operand. But the else branch
  116. ; is not executable (conditional is always true), so no values should be
  117. ; propagated there and the value of the OpPhi should always be %int_7.
  118. ; CHECK: OpPhi %int %int_7 [[bb_taken]] [[id_not_evaluated]] [[bb_not_taken]]
  119. %35 = OpPhi %int %7 %22 %8 %23
  120. ; Only the true path of the conditional is ever executed. The output of this
  121. ; function is always %int_7.
  122. ; CHECK: OpStore %outparm %int_7
  123. OpStore %outparm %35
  124. OpReturn
  125. OpFunctionEnd
  126. )";
  127. SinglePassRunAndMatch<CCPPass>(spv_asm, true);
  128. }
  129. TEST_F(CCPTest, SimplifySwitches) {
  130. const std::string spv_asm = R"(
  131. OpCapability Shader
  132. %1 = OpExtInstImport "GLSL.std.450"
  133. OpMemoryModel Logical GLSL450
  134. OpEntryPoint Fragment %main "main" %outparm
  135. OpExecutionMode %main OriginUpperLeft
  136. OpSource GLSL 450
  137. OpName %main "main"
  138. OpName %outparm "outparm"
  139. OpDecorate %outparm Location 0
  140. %void = OpTypeVoid
  141. %6 = OpTypeFunction %void
  142. %int = OpTypeInt 32 1
  143. %_ptr_Function_int = OpTypePointer Function %int
  144. %int_23 = OpConstant %int 23
  145. %int_42 = OpConstant %int 42
  146. %int_14 = OpConstant %int 14
  147. %int_15 = OpConstant %int 15
  148. %int_4 = OpConstant %int 4
  149. %_ptr_Output_int = OpTypePointer Output %int
  150. %outparm = OpVariable %_ptr_Output_int Output
  151. %main = OpFunction %void None %6
  152. %15 = OpLabel
  153. OpSelectionMerge %17 None
  154. OpSwitch %int_23 %17 10 %18 13 %19 23 %20
  155. %18 = OpLabel
  156. OpBranch %17
  157. %19 = OpLabel
  158. OpBranch %17
  159. %20 = OpLabel
  160. OpBranch %17
  161. %17 = OpLabel
  162. %24 = OpPhi %int %int_23 %15 %int_42 %18 %int_14 %19 %int_15 %20
  163. ; The switch will always jump to label %20, which carries the value %int_15.
  164. ; CHECK: OpIAdd %int %int_15 %int_4
  165. %22 = OpIAdd %int %24 %int_4
  166. ; Consequently, the return value will always be %int_19.
  167. ; CHECK: OpStore %outparm %int_19
  168. OpStore %outparm %22
  169. OpReturn
  170. OpFunctionEnd
  171. )";
  172. SinglePassRunAndMatch<CCPPass>(spv_asm, true);
  173. }
  174. TEST_F(CCPTest, SimplifySwitchesDefaultBranch) {
  175. const std::string spv_asm = R"(
  176. OpCapability Shader
  177. %1 = OpExtInstImport "GLSL.std.450"
  178. OpMemoryModel Logical GLSL450
  179. OpEntryPoint Fragment %main "main" %outparm
  180. OpExecutionMode %main OriginUpperLeft
  181. OpSource GLSL 450
  182. OpName %main "main"
  183. OpName %outparm "outparm"
  184. OpDecorate %outparm Location 0
  185. %void = OpTypeVoid
  186. %6 = OpTypeFunction %void
  187. %int = OpTypeInt 32 1
  188. %_ptr_Function_int = OpTypePointer Function %int
  189. %int_42 = OpConstant %int 42
  190. %int_4 = OpConstant %int 4
  191. %int_1 = OpConstant %int 1
  192. %_ptr_Output_int = OpTypePointer Output %int
  193. %outparm = OpVariable %_ptr_Output_int Output
  194. %main = OpFunction %void None %6
  195. %13 = OpLabel
  196. %15 = OpIAdd %int %int_42 %int_4
  197. OpSelectionMerge %16 None
  198. ; CHECK: OpSwitch %int_46 {{%\d+}} 10 {{%\d+}}
  199. OpSwitch %15 %17 10 %18
  200. %18 = OpLabel
  201. OpBranch %16
  202. %17 = OpLabel
  203. OpBranch %16
  204. %16 = OpLabel
  205. %22 = OpPhi %int %int_42 %18 %int_1 %17
  206. ; The switch will always jump to the default label %17. This carries the value
  207. ; %int_1.
  208. ; CHECK: OpIAdd %int %int_1 %int_4
  209. %20 = OpIAdd %int %22 %int_4
  210. ; Resulting in a return value of %int_5.
  211. ; CHECK: OpStore %outparm %int_5
  212. OpStore %outparm %20
  213. OpReturn
  214. OpFunctionEnd
  215. )";
  216. SinglePassRunAndMatch<CCPPass>(spv_asm, true);
  217. }
  218. TEST_F(CCPTest, SimplifyIntVector) {
  219. const std::string spv_asm = R"(
  220. OpCapability Shader
  221. %1 = OpExtInstImport "GLSL.std.450"
  222. OpMemoryModel Logical GLSL450
  223. OpEntryPoint Fragment %main "main" %OutColor
  224. OpExecutionMode %main OriginUpperLeft
  225. OpSource GLSL 450
  226. OpName %main "main"
  227. OpName %v "v"
  228. OpName %OutColor "OutColor"
  229. OpDecorate %OutColor Location 0
  230. %void = OpTypeVoid
  231. %3 = OpTypeFunction %void
  232. %int = OpTypeInt 32 1
  233. %v4int = OpTypeVector %int 4
  234. %_ptr_Function_v4int = OpTypePointer Function %v4int
  235. %int_1 = OpConstant %int 1
  236. %int_2 = OpConstant %int 2
  237. %int_3 = OpConstant %int 3
  238. %int_4 = OpConstant %int 4
  239. %14 = OpConstantComposite %v4int %int_1 %int_2 %int_3 %int_4
  240. %uint = OpTypeInt 32 0
  241. %uint_0 = OpConstant %uint 0
  242. %_ptr_Function_int = OpTypePointer Function %int
  243. %_ptr_Output_v4int = OpTypePointer Output %v4int
  244. %OutColor = OpVariable %_ptr_Output_v4int Output
  245. %main = OpFunction %void None %3
  246. %5 = OpLabel
  247. %v = OpVariable %_ptr_Function_v4int Function
  248. OpStore %v %14
  249. %18 = OpAccessChain %_ptr_Function_int %v %uint_0
  250. %19 = OpLoad %int %18
  251. ; The constant folder does not see through access chains. To get this, the
  252. ; vector would have to be scalarized.
  253. ; CHECK: [[result_id:%\d+]] = OpIAdd %int {{%\d+}} %int_1
  254. %20 = OpIAdd %int %19 %int_1
  255. %21 = OpAccessChain %_ptr_Function_int %v %uint_0
  256. ; CHECK: OpStore {{%\d+}} [[result_id]]
  257. OpStore %21 %20
  258. %24 = OpLoad %v4int %v
  259. OpStore %OutColor %24
  260. OpReturn
  261. OpFunctionEnd
  262. )";
  263. SinglePassRunAndMatch<CCPPass>(spv_asm, true);
  264. }
  265. TEST_F(CCPTest, BadSimplifyFloatVector) {
  266. const std::string spv_asm = R"(
  267. OpCapability Shader
  268. %1 = OpExtInstImport "GLSL.std.450"
  269. OpMemoryModel Logical GLSL450
  270. OpEntryPoint Fragment %main "main" %OutColor
  271. OpExecutionMode %main OriginUpperLeft
  272. OpSource GLSL 450
  273. OpName %main "main"
  274. OpName %v "v"
  275. OpName %OutColor "OutColor"
  276. OpDecorate %OutColor Location 0
  277. %void = OpTypeVoid
  278. %3 = OpTypeFunction %void
  279. %float = OpTypeFloat 32
  280. %v4float = OpTypeVector %float 4
  281. %_ptr_Function_v4float = OpTypePointer Function %v4float
  282. %float_1 = OpConstant %float 1
  283. %float_2 = OpConstant %float 2
  284. %float_3 = OpConstant %float 3
  285. %float_4 = OpConstant %float 4
  286. %14 = OpConstantComposite %v4float %float_1 %float_2 %float_3 %float_4
  287. %uint = OpTypeInt 32 0
  288. %uint_0 = OpConstant %uint 0
  289. %_ptr_Function_float = OpTypePointer Function %float
  290. %_ptr_Output_v4float = OpTypePointer Output %v4float
  291. %OutColor = OpVariable %_ptr_Output_v4float Output
  292. %main = OpFunction %void None %3
  293. %5 = OpLabel
  294. %v = OpVariable %_ptr_Function_v4float Function
  295. OpStore %v %14
  296. %18 = OpAccessChain %_ptr_Function_float %v %uint_0
  297. %19 = OpLoad %float %18
  298. ; NOTE: This test should start failing once floating point folding is
  299. ; implemented (https://github.com/KhronosGroup/SPIRV-Tools/issues/943).
  300. ; This should be checking that we are adding %float_1 + %float_1.
  301. ; CHECK: [[result_id:%\d+]] = OpFAdd %float {{%\d+}} %float_1
  302. %20 = OpFAdd %float %19 %float_1
  303. %21 = OpAccessChain %_ptr_Function_float %v %uint_0
  304. ; This should be checkint that we are storing %float_2 instead of result_it.
  305. ; CHECK: OpStore {{%\d+}} [[result_id]]
  306. OpStore %21 %20
  307. %24 = OpLoad %v4float %v
  308. OpStore %OutColor %24
  309. OpReturn
  310. OpFunctionEnd
  311. )";
  312. SinglePassRunAndMatch<CCPPass>(spv_asm, true);
  313. }
  314. TEST_F(CCPTest, NoLoadStorePropagation) {
  315. const std::string spv_asm = R"(
  316. OpCapability Shader
  317. %1 = OpExtInstImport "GLSL.std.450"
  318. OpMemoryModel Logical GLSL450
  319. OpEntryPoint Fragment %main "main" %outparm
  320. OpExecutionMode %main OriginUpperLeft
  321. OpSource GLSL 450
  322. OpName %main "main"
  323. OpName %x "x"
  324. OpName %outparm "outparm"
  325. OpDecorate %outparm Location 0
  326. %void = OpTypeVoid
  327. %3 = OpTypeFunction %void
  328. %int = OpTypeInt 32 1
  329. %_ptr_Function_int = OpTypePointer Function %int
  330. %int_23 = OpConstant %int 23
  331. %_ptr_Output_int = OpTypePointer Output %int
  332. %outparm = OpVariable %_ptr_Output_int Output
  333. %main = OpFunction %void None %3
  334. %5 = OpLabel
  335. %x = OpVariable %_ptr_Function_int Function
  336. OpStore %x %int_23
  337. ; int_23 should not propagate into this load.
  338. ; CHECK: [[load_id:%\d+]] = OpLoad %int %x
  339. %12 = OpLoad %int %x
  340. ; Nor into this copy operation.
  341. ; CHECK: [[copy_id:%\d+]] = OpCopyObject %int [[load_id]]
  342. %13 = OpCopyObject %int %12
  343. ; Likewise here.
  344. ; CHECK: OpStore %outparm [[copy_id]]
  345. OpStore %outparm %13
  346. OpReturn
  347. OpFunctionEnd
  348. )";
  349. SinglePassRunAndMatch<CCPPass>(spv_asm, true);
  350. }
  351. TEST_F(CCPTest, HandleAbortInstructions) {
  352. const std::string spv_asm = R"(
  353. OpCapability Shader
  354. %1 = OpExtInstImport "GLSL.std.450"
  355. OpMemoryModel Logical GLSL450
  356. OpEntryPoint Fragment %main "main"
  357. OpExecutionMode %main OriginUpperLeft
  358. OpSource HLSL 500
  359. OpName %main "main"
  360. %void = OpTypeVoid
  361. %3 = OpTypeFunction %void
  362. %int = OpTypeInt 32 1
  363. %bool = OpTypeBool
  364. ; CHECK: %true = OpConstantTrue %bool
  365. %int_3 = OpConstant %int 3
  366. %int_1 = OpConstant %int 1
  367. %main = OpFunction %void None %3
  368. %4 = OpLabel
  369. %9 = OpIAdd %int %int_3 %int_1
  370. %6 = OpSGreaterThan %bool %9 %int_3
  371. OpSelectionMerge %23 None
  372. ; CHECK: OpBranchConditional %true {{%\d+}} {{%\d+}}
  373. OpBranchConditional %6 %22 %23
  374. %22 = OpLabel
  375. OpKill
  376. %23 = OpLabel
  377. OpReturn
  378. OpFunctionEnd
  379. )";
  380. SinglePassRunAndMatch<CCPPass>(spv_asm, true);
  381. }
  382. TEST_F(CCPTest, SSAWebCycles) {
  383. // Test reduced from https://github.com/KhronosGroup/SPIRV-Tools/issues/1159
  384. // When there is a cycle in the SSA def-use web, the propagator was getting
  385. // into an infinite loop. SSA edges for Phi instructions should not be
  386. // added to the edges to simulate.
  387. const std::string spv_asm = R"(
  388. OpCapability Shader
  389. %1 = OpExtInstImport "GLSL.std.450"
  390. OpMemoryModel Logical GLSL450
  391. OpEntryPoint Fragment %main "main"
  392. OpExecutionMode %main OriginUpperLeft
  393. OpSource GLSL 450
  394. OpName %main "main"
  395. %void = OpTypeVoid
  396. %3 = OpTypeFunction %void
  397. %int = OpTypeInt 32 1
  398. %_ptr_Function_int = OpTypePointer Function %int
  399. %int_0 = OpConstant %int 0
  400. %int_4 = OpConstant %int 4
  401. %bool = OpTypeBool
  402. %int_1 = OpConstant %int 1
  403. %_ptr_Output_int = OpTypePointer Output %int
  404. %main = OpFunction %void None %3
  405. %5 = OpLabel
  406. OpBranch %11
  407. %11 = OpLabel
  408. %29 = OpPhi %int %int_0 %5 %22 %14
  409. %30 = OpPhi %int %int_0 %5 %25 %14
  410. OpLoopMerge %13 %14 None
  411. OpBranch %15
  412. %15 = OpLabel
  413. %19 = OpSLessThan %bool %30 %int_4
  414. ; CHECK: OpBranchConditional %true {{%\d+}} {{%\d+}}
  415. OpBranchConditional %19 %12 %13
  416. %12 = OpLabel
  417. ; CHECK: OpIAdd %int %int_0 %int_0
  418. %22 = OpIAdd %int %29 %30
  419. OpBranch %14
  420. %14 = OpLabel
  421. ; CHECK: OpPhi %int %int_0 {{%\d+}}
  422. %25 = OpPhi %int %30 %12
  423. OpBranch %11
  424. %13 = OpLabel
  425. OpReturn
  426. OpFunctionEnd
  427. )";
  428. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  429. SinglePassRunAndMatch<CCPPass>(spv_asm, true);
  430. }
  431. TEST_F(CCPTest, LoopInductionVariables) {
  432. // Test reduced from https://github.com/KhronosGroup/SPIRV-Tools/issues/1143
  433. // We are failing to properly consider the induction variable for this loop
  434. // as Varying.
  435. const std::string spv_asm = R"(
  436. OpCapability Shader
  437. %1 = OpExtInstImport "GLSL.std.450"
  438. OpMemoryModel Logical GLSL450
  439. OpEntryPoint Fragment %main "main"
  440. OpExecutionMode %main OriginUpperLeft
  441. OpSource GLSL 430
  442. OpName %main "main"
  443. %void = OpTypeVoid
  444. %5 = OpTypeFunction %void
  445. %int = OpTypeInt 32 1
  446. %_ptr_Function_int = OpTypePointer Function %int
  447. %int_0 = OpConstant %int 0
  448. %int_10 = OpConstant %int 10
  449. %bool = OpTypeBool
  450. %int_1 = OpConstant %int 1
  451. %main = OpFunction %void None %5
  452. %12 = OpLabel
  453. OpBranch %13
  454. %13 = OpLabel
  455. ; This Phi should not have all constant arguments:
  456. ; CHECK: [[phi_id:%\d+]] = OpPhi %int %int_0 {{%\d+}} {{%\d+}} {{%\d+}}
  457. %22 = OpPhi %int %int_0 %12 %21 %15
  458. OpLoopMerge %14 %15 None
  459. OpBranch %16
  460. %16 = OpLabel
  461. ; The Phi should never be considered to have the value %int_0.
  462. ; CHECK: [[branch_selector:%\d+]] = OpSLessThan %bool [[phi_id]] %int_10
  463. %18 = OpSLessThan %bool %22 %int_10
  464. ; This conditional was wrongly converted into an always-true jump due to the
  465. ; bad meet evaluation of %22.
  466. ; CHECK: OpBranchConditional [[branch_selector]] {{%\d+}} {{%\d+}}
  467. OpBranchConditional %18 %19 %14
  468. %19 = OpLabel
  469. OpBranch %15
  470. %15 = OpLabel
  471. ; CHECK: OpIAdd %int [[phi_id]] %int_1
  472. %21 = OpIAdd %int %22 %int_1
  473. OpBranch %13
  474. %14 = OpLabel
  475. OpReturn
  476. OpFunctionEnd
  477. )";
  478. SinglePassRunAndMatch<CCPPass>(spv_asm, true);
  479. }
  480. TEST_F(CCPTest, HandleCompositeWithUndef) {
  481. // Check to make sure that CCP does not crash when given a "constant" struct
  482. // with an undef. If at a later time CCP is enhanced to optimize this case,
  483. // it is not wrong.
  484. const std::string spv_asm = R"(
  485. OpCapability Shader
  486. %1 = OpExtInstImport "GLSL.std.450"
  487. OpMemoryModel Logical GLSL450
  488. OpEntryPoint Fragment %main "main"
  489. OpExecutionMode %main OriginUpperLeft
  490. OpSource HLSL 500
  491. OpName %main "main"
  492. %void = OpTypeVoid
  493. %4 = OpTypeFunction %void
  494. %int = OpTypeInt 32 1
  495. %bool = OpTypeBool
  496. %_struct_7 = OpTypeStruct %int %int
  497. %int_1 = OpConstant %int 1
  498. %9 = OpUndef %int
  499. %10 = OpConstantComposite %_struct_7 %int_1 %9
  500. %main = OpFunction %void None %4
  501. %11 = OpLabel
  502. %12 = OpCompositeExtract %int %10 0
  503. %13 = OpCopyObject %int %12
  504. OpReturn
  505. OpFunctionEnd
  506. )";
  507. auto res = SinglePassRunToBinary<CCPPass>(spv_asm, true);
  508. EXPECT_EQ(std::get<1>(res), Pass::Status::SuccessWithoutChange);
  509. }
  510. TEST_F(CCPTest, SkipSpecConstantInstrucitons) {
  511. const std::string spv_asm = R"(
  512. OpCapability Shader
  513. %1 = OpExtInstImport "GLSL.std.450"
  514. OpMemoryModel Logical GLSL450
  515. OpEntryPoint Fragment %main "main"
  516. OpExecutionMode %main OriginUpperLeft
  517. OpSource HLSL 500
  518. OpName %main "main"
  519. %void = OpTypeVoid
  520. %4 = OpTypeFunction %void
  521. %bool = OpTypeBool
  522. %10 = OpSpecConstantFalse %bool
  523. %main = OpFunction %void None %4
  524. %11 = OpLabel
  525. OpBranchConditional %10 %L1 %L2
  526. %L1 = OpLabel
  527. OpReturn
  528. %L2 = OpLabel
  529. OpReturn
  530. OpFunctionEnd
  531. )";
  532. auto res = SinglePassRunToBinary<CCPPass>(spv_asm, true);
  533. EXPECT_EQ(std::get<1>(res), Pass::Status::SuccessWithoutChange);
  534. }
  535. TEST_F(CCPTest, UpdateSubsequentPhisToVarying) {
  536. const std::string text = R"(
  537. OpCapability Shader
  538. OpMemoryModel Logical GLSL450
  539. OpEntryPoint Fragment %func "func" %in
  540. OpExecutionMode %func OriginUpperLeft
  541. %void = OpTypeVoid
  542. %bool = OpTypeBool
  543. %int = OpTypeInt 32 1
  544. %false = OpConstantFalse %bool
  545. %int0 = OpConstant %int 0
  546. %int1 = OpConstant %int 1
  547. %int6 = OpConstant %int 6
  548. %int_ptr_Input = OpTypePointer Input %int
  549. %in = OpVariable %int_ptr_Input Input
  550. %undef = OpUndef %int
  551. ; Although no constants are propagated in this function, the propagator
  552. ; generates a new %true value while visiting conditional statements.
  553. ; CHECK: %true = OpConstantTrue %bool
  554. %functy = OpTypeFunction %void
  555. %func = OpFunction %void None %functy
  556. %1 = OpLabel
  557. OpBranch %2
  558. %2 = OpLabel
  559. %outer_phi = OpPhi %int %int0 %1 %outer_add %15
  560. %cond1 = OpSLessThanEqual %bool %outer_phi %int6
  561. OpLoopMerge %3 %15 None
  562. OpBranchConditional %cond1 %4 %3
  563. %4 = OpLabel
  564. %ld = OpLoad %int %in
  565. %cond2 = OpSGreaterThanEqual %bool %int1 %ld
  566. OpSelectionMerge %10 None
  567. OpBranchConditional %cond2 %8 %9
  568. %8 = OpLabel
  569. OpBranch %10
  570. %9 = OpLabel
  571. OpBranch %10
  572. %10 = OpLabel
  573. %extra_phi = OpPhi %int %outer_phi %8 %outer_phi %9
  574. OpBranch %11
  575. %11 = OpLabel
  576. %inner_phi = OpPhi %int %int0 %10 %inner_add %13
  577. %cond3 = OpSLessThanEqual %bool %inner_phi %int6
  578. OpLoopMerge %14 %13 None
  579. OpBranchConditional %cond3 %12 %14
  580. %12 = OpLabel
  581. OpBranch %13
  582. %13 = OpLabel
  583. %inner_add = OpIAdd %int %inner_phi %int1
  584. OpBranch %11
  585. %14 = OpLabel
  586. OpBranch %15
  587. %15 = OpLabel
  588. %outer_add = OpIAdd %int %extra_phi %int1
  589. OpBranch %2
  590. %3 = OpLabel
  591. OpReturn
  592. OpFunctionEnd
  593. )";
  594. auto result = SinglePassRunAndMatch<CCPPass>(text, true);
  595. EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
  596. }
  597. TEST_F(CCPTest, UndefInPhi) {
  598. const std::string text = R"(
  599. ; CHECK: [[uint1:%\w+]] = OpConstant {{%\w+}} 1
  600. ; CHECK: [[phi:%\w+]] = OpPhi
  601. ; CHECK: OpIAdd {{%\w+}} [[phi]] [[uint1]]
  602. OpCapability Kernel
  603. OpCapability Linkage
  604. OpMemoryModel Logical OpenCL
  605. OpDecorate %1 LinkageAttributes "func" Export
  606. %void = OpTypeVoid
  607. %bool = OpTypeBool
  608. %uint = OpTypeInt 32 0
  609. %uint_0 = OpConstant %uint 0
  610. %uint_1 = OpConstant %uint 1
  611. %7 = OpUndef %uint
  612. %8 = OpTypeFunction %void %bool
  613. %1 = OpFunction %void None %8
  614. %9 = OpFunctionParameter %bool
  615. %10 = OpLabel
  616. OpBranchConditional %9 %11 %12
  617. %11 = OpLabel
  618. OpBranch %13
  619. %12 = OpLabel
  620. OpBranch %14
  621. %14 = OpLabel
  622. OpBranchConditional %9 %13 %15
  623. %15 = OpLabel
  624. OpBranch %13
  625. %13 = OpLabel
  626. %16 = OpPhi %uint %uint_0 %11 %7 %14 %uint_1 %15
  627. %17 = OpIAdd %uint %16 %uint_1
  628. OpReturn
  629. OpFunctionEnd
  630. )";
  631. SinglePassRunAndMatch<CCPPass>(text, true);
  632. }
  633. // Just test to make sure the constant fold rules are being used. Will rely on
  634. // the folding test for specific testing of specific rules.
  635. TEST_F(CCPTest, UseConstantFoldingRules) {
  636. const std::string text = R"(
  637. ; CHECK: [[float1:%\w+]] = OpConstant {{%\w+}} 1
  638. ; CHECK: OpReturnValue [[float1]]
  639. OpCapability Shader
  640. OpCapability Linkage
  641. OpMemoryModel Logical GLSL450
  642. OpDecorate %1 LinkageAttributes "func" Export
  643. %void = OpTypeVoid
  644. %bool = OpTypeBool
  645. %float = OpTypeFloat 32
  646. %float_0 = OpConstant %float 0
  647. %float_1 = OpConstant %float 1
  648. %8 = OpTypeFunction %float
  649. %1 = OpFunction %float None %8
  650. %10 = OpLabel
  651. %17 = OpFAdd %float %float_0 %float_1
  652. OpReturnValue %17
  653. OpFunctionEnd
  654. )";
  655. SinglePassRunAndMatch<CCPPass>(text, true);
  656. }
  657. // Test for #1300. Previously value for %5 would not settle during simulation.
  658. TEST_F(CCPTest, SettlePhiLatticeValue) {
  659. const std::string text = R"(
  660. OpCapability Kernel
  661. OpCapability Linkage
  662. OpMemoryModel Logical OpenCL
  663. OpDecorate %func LinkageAttributes "func" Export
  664. %void = OpTypeVoid
  665. %bool = OpTypeBool
  666. %true = OpConstantTrue %bool
  667. %false = OpConstantFalse %bool
  668. %functy = OpTypeFunction %void
  669. %func = OpFunction %void None %functy
  670. %1 = OpLabel
  671. OpBranchConditional %true %2 %3
  672. %3 = OpLabel
  673. OpBranch %2
  674. %2 = OpLabel
  675. %5 = OpPhi %bool %true %1 %false %3
  676. OpReturn
  677. OpFunctionEnd
  678. )";
  679. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  680. SinglePassRunToBinary<CCPPass>(text, true);
  681. }
  682. TEST_F(CCPTest, NullBranchCondition) {
  683. const std::string text = R"(
  684. ; CHECK: [[int1:%\w+]] = OpConstant {{%\w+}} 1
  685. ; CHECK: [[int2:%\w+]] = OpConstant {{%\w+}} 2
  686. ; CHECK: OpIAdd {{%\w+}} [[int1]] [[int2]]
  687. OpCapability Shader
  688. OpMemoryModel Logical GLSL450
  689. OpEntryPoint Fragment %func "func"
  690. OpExecutionMode %func OriginUpperLeft
  691. %void = OpTypeVoid
  692. %bool = OpTypeBool
  693. %int = OpTypeInt 32 1
  694. %null = OpConstantNull %bool
  695. %int_1 = OpConstant %int 1
  696. %int_2 = OpConstant %int 2
  697. %functy = OpTypeFunction %void
  698. %func = OpFunction %void None %functy
  699. %1 = OpLabel
  700. OpSelectionMerge %2 None
  701. OpBranchConditional %null %2 %3
  702. %3 = OpLabel
  703. OpBranch %2
  704. %2 = OpLabel
  705. %phi = OpPhi %int %int_1 %1 %int_2 %3
  706. %add = OpIAdd %int %int_1 %phi
  707. OpReturn
  708. OpFunctionEnd
  709. )";
  710. SinglePassRunAndMatch<CCPPass>(text, true);
  711. }
  712. TEST_F(CCPTest, UndefBranchCondition) {
  713. const std::string text = R"(
  714. ; CHECK: [[int1:%\w+]] = OpConstant {{%\w+}} 1
  715. ; CHECK: [[phi:%\w+]] = OpPhi
  716. ; CHECK: OpIAdd {{%\w+}} [[int1]] [[phi]]
  717. OpCapability Shader
  718. OpMemoryModel Logical GLSL450
  719. OpEntryPoint Fragment %func "func"
  720. OpExecutionMode %func OriginUpperLeft
  721. %void = OpTypeVoid
  722. %bool = OpTypeBool
  723. %int = OpTypeInt 32 1
  724. %undef = OpUndef %bool
  725. %int_1 = OpConstant %int 1
  726. %int_2 = OpConstant %int 2
  727. %functy = OpTypeFunction %void
  728. %func = OpFunction %void None %functy
  729. %1 = OpLabel
  730. OpSelectionMerge %2 None
  731. OpBranchConditional %undef %2 %3
  732. %3 = OpLabel
  733. OpBranch %2
  734. %2 = OpLabel
  735. %phi = OpPhi %int %int_1 %1 %int_2 %3
  736. %add = OpIAdd %int %int_1 %phi
  737. OpReturn
  738. OpFunctionEnd
  739. )";
  740. SinglePassRunAndMatch<CCPPass>(text, true);
  741. }
  742. TEST_F(CCPTest, NullSwitchCondition) {
  743. const std::string text = R"(
  744. ; CHECK: [[int1:%\w+]] = OpConstant {{%\w+}} 1
  745. ; CHECK: [[int2:%\w+]] = OpConstant {{%\w+}} 2
  746. ; CHECK: OpIAdd {{%\w+}} [[int1]] [[int2]]
  747. OpCapability Shader
  748. OpMemoryModel Logical GLSL450
  749. OpEntryPoint Fragment %func "func"
  750. OpExecutionMode %func OriginUpperLeft
  751. %void = OpTypeVoid
  752. %int = OpTypeInt 32 1
  753. %null = OpConstantNull %int
  754. %int_1 = OpConstant %int 1
  755. %int_2 = OpConstant %int 2
  756. %functy = OpTypeFunction %void
  757. %func = OpFunction %void None %functy
  758. %1 = OpLabel
  759. OpSelectionMerge %2 None
  760. OpSwitch %null %2 0 %3
  761. %3 = OpLabel
  762. OpBranch %2
  763. %2 = OpLabel
  764. %phi = OpPhi %int %int_1 %1 %int_2 %3
  765. %add = OpIAdd %int %int_1 %phi
  766. OpReturn
  767. OpFunctionEnd
  768. )";
  769. SinglePassRunAndMatch<CCPPass>(text, true);
  770. }
  771. TEST_F(CCPTest, UndefSwitchCondition) {
  772. const std::string text = R"(
  773. ; CHECK: [[int1:%\w+]] = OpConstant {{%\w+}} 1
  774. ; CHECK: [[phi:%\w+]] = OpPhi
  775. ; CHECK: OpIAdd {{%\w+}} [[int1]] [[phi]]
  776. OpCapability Shader
  777. OpMemoryModel Logical GLSL450
  778. OpEntryPoint Fragment %func "func"
  779. OpExecutionMode %func OriginUpperLeft
  780. %void = OpTypeVoid
  781. %int = OpTypeInt 32 1
  782. %undef = OpUndef %int
  783. %int_1 = OpConstant %int 1
  784. %int_2 = OpConstant %int 2
  785. %functy = OpTypeFunction %void
  786. %func = OpFunction %void None %functy
  787. %1 = OpLabel
  788. OpSelectionMerge %2 None
  789. OpSwitch %undef %2 0 %3
  790. %3 = OpLabel
  791. OpBranch %2
  792. %2 = OpLabel
  793. %phi = OpPhi %int %int_1 %1 %int_2 %3
  794. %add = OpIAdd %int %int_1 %phi
  795. OpReturn
  796. OpFunctionEnd
  797. )";
  798. SinglePassRunAndMatch<CCPPass>(text, true);
  799. }
  800. // Test for #1361.
  801. TEST_F(CCPTest, CompositeConstructOfGlobalValue) {
  802. const std::string text = R"(
  803. ; CHECK: [[phi:%\w+]] = OpPhi
  804. ; CHECK-NEXT: OpCompositeExtract {{%\w+}} [[phi]] 0
  805. OpCapability Shader
  806. OpMemoryModel Logical GLSL450
  807. OpEntryPoint Fragment %func "func" %in
  808. OpExecutionMode %func OriginUpperLeft
  809. %void = OpTypeVoid
  810. %int = OpTypeInt 32 1
  811. %bool = OpTypeBool
  812. %functy = OpTypeFunction %void
  813. %ptr_int_Input = OpTypePointer Input %int
  814. %in = OpVariable %ptr_int_Input Input
  815. %struct = OpTypeStruct %ptr_int_Input %ptr_int_Input
  816. %struct_null = OpConstantNull %struct
  817. %func = OpFunction %void None %functy
  818. %1 = OpLabel
  819. OpBranch %2
  820. %2 = OpLabel
  821. %phi = OpPhi %struct %struct_null %1 %5 %4
  822. %extract = OpCompositeExtract %ptr_int_Input %phi 0
  823. OpLoopMerge %3 %4 None
  824. OpBranch %4
  825. %4 = OpLabel
  826. %5 = OpCompositeConstruct %struct %in %in
  827. OpBranch %2
  828. %3 = OpLabel
  829. OpReturn
  830. OpFunctionEnd
  831. )";
  832. SinglePassRunAndMatch<CCPPass>(text, true);
  833. }
  834. TEST_F(CCPTest, FoldWithDecoration) {
  835. const std::string text = R"(
  836. ; CHECK: OpCapability
  837. ; CHECK-NOT: OpDecorate
  838. ; CHECK: OpFunctionEnd
  839. OpCapability Shader
  840. %1 = OpExtInstImport "GLSL.std.450"
  841. OpMemoryModel Logical GLSL450
  842. OpEntryPoint Fragment %2 "main"
  843. OpExecutionMode %2 OriginUpperLeft
  844. OpSource ESSL 310
  845. OpDecorate %3 RelaxedPrecision
  846. %void = OpTypeVoid
  847. %5 = OpTypeFunction %void
  848. %float = OpTypeFloat 32
  849. %v3float = OpTypeVector %float 3
  850. %float_0 = OpConstant %float 0
  851. %v4float = OpTypeVector %float 4
  852. %10 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
  853. %2 = OpFunction %void None %5
  854. %11 = OpLabel
  855. %3 = OpVectorShuffle %v3float %10 %10 0 1 2
  856. OpReturn
  857. OpFunctionEnd
  858. )";
  859. SinglePassRunAndMatch<CCPPass>(text, true);
  860. }
  861. TEST_F(CCPTest, DebugSimpleFoldConstant) {
  862. const std::string text = R"(
  863. OpCapability Shader
  864. OpCapability Linkage
  865. %ext = OpExtInstImport "OpenCL.DebugInfo.100"
  866. OpMemoryModel Logical GLSL450
  867. %file_name = OpString "test"
  868. %float_name = OpString "float"
  869. %main_name = OpString "main"
  870. %f_name = OpString "f"
  871. OpDecorate %1 LinkageAttributes "func" Export
  872. %void = OpTypeVoid
  873. %bool = OpTypeBool
  874. %float = OpTypeFloat 32
  875. %float_0 = OpConstant %float 0
  876. ; CHECK: [[float1:%\w+]] = OpConstant {{%\w+}} 1
  877. %float_1 = OpConstant %float 1
  878. %uint = OpTypeInt 32 0
  879. %uint_32 = OpConstant %uint 32
  880. %8 = OpTypeFunction %float
  881. %null_expr = OpExtInst %void %ext DebugExpression
  882. %src = OpExtInst %void %ext DebugSource %file_name
  883. %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
  884. %dbg_tf = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
  885. %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_tf
  886. %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 0 0 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %1
  887. %dbg_f = OpExtInst %void %ext DebugLocalVariable %f_name %dbg_tf %src 0 0 %dbg_main FlagIsLocal
  888. %1 = OpFunction %float None %8
  889. %10 = OpLabel
  890. ; CHECK: OpExtInst %void [[ext:%\w+]] DebugScope
  891. ; CHECK: OpLine [[file:%\w+]] 1 0
  892. ; CHECK: OpExtInst %void [[ext]] DebugValue {{%\w+}} %float_1
  893. %s0 = OpExtInst %void %ext DebugScope %dbg_main
  894. OpLine %file_name 1 0
  895. %17 = OpFAdd %float %float_0 %float_1
  896. %val = OpExtInst %void %ext DebugValue %dbg_f %17 %null_expr
  897. ; CHECK: OpLine [[file]] 2 0
  898. ; CHECK: OpReturnValue [[float1]]
  899. OpLine %file_name 2 0
  900. OpReturnValue %17
  901. OpFunctionEnd
  902. )";
  903. SinglePassRunAndMatch<CCPPass>(text, true);
  904. }
  905. TEST_F(CCPTest, DebugFoldMultipleForSingleConstant) {
  906. const std::string text = R"(
  907. OpCapability Shader
  908. %1 = OpExtInstImport "GLSL.std.450"
  909. %ext = OpExtInstImport "OpenCL.DebugInfo.100"
  910. OpMemoryModel Logical GLSL450
  911. OpEntryPoint Fragment %main "main" %outparm
  912. OpExecutionMode %main OriginUpperLeft
  913. OpSource GLSL 450
  914. %file_name = OpString "test"
  915. %float_name = OpString "float"
  916. %main_name = OpString "main"
  917. %f_name = OpString "f"
  918. OpName %main "main"
  919. OpName %outparm "outparm"
  920. OpDecorate %outparm Location 0
  921. %void = OpTypeVoid
  922. %3 = OpTypeFunction %void
  923. %int = OpTypeInt 32 1
  924. %bool = OpTypeBool
  925. %_ptr_Function_int = OpTypePointer Function %int
  926. %int_4 = OpConstant %int 4
  927. %int_3 = OpConstant %int 3
  928. %int_1 = OpConstant %int 1
  929. %uint = OpTypeInt 32 0
  930. %uint_32 = OpConstant %uint 32
  931. %_ptr_Output_int = OpTypePointer Output %int
  932. %outparm = OpVariable %_ptr_Output_int Output
  933. %null_expr = OpExtInst %void %ext DebugExpression
  934. %src = OpExtInst %void %ext DebugSource %file_name
  935. %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
  936. %dbg_tf = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
  937. %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_tf
  938. %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 0 0 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
  939. %bb0 = OpExtInst %void %ext DebugLexicalBlock %src 0 0 %dbg_main
  940. %bb1 = OpExtInst %void %ext DebugLexicalBlock %src 1 0 %dbg_main
  941. %bb2 = OpExtInst %void %ext DebugLexicalBlock %src 2 0 %dbg_main
  942. %bb3 = OpExtInst %void %ext DebugLexicalBlock %src 3 0 %dbg_main
  943. %dbg_f0 = OpExtInst %void %ext DebugLocalVariable %f_name %dbg_tf %src 0 0 %dbg_main FlagIsLocal
  944. %dbg_f1 = OpExtInst %void %ext DebugLocalVariable %f_name %dbg_tf %src 1 0 %dbg_main FlagIsLocal
  945. %dbg_f2 = OpExtInst %void %ext DebugLocalVariable %f_name %dbg_tf %src 2 0 %dbg_main FlagIsLocal
  946. %main = OpFunction %void None %3
  947. %4 = OpLabel
  948. ; CHECK: OpExtInst %void [[ext:%\w+]] DebugScope
  949. ; CHECK: OpLine [[file:%\w+]] 1 0
  950. ; CHECK: OpIAdd %int %int_4 %int_3
  951. ; CHECK: OpExtInst %void [[ext]] DebugValue {{%\w+}} %int_7
  952. %s0 = OpExtInst %void %ext DebugScope %bb0
  953. OpLine %file_name 1 0
  954. %9 = OpIAdd %int %int_4 %int_3
  955. %val0 = OpExtInst %void %ext DebugValue %dbg_f0 %9 %null_expr
  956. ; CHECK: OpLine [[file]] 2 0
  957. ; CHECK: OpSGreaterThan %bool %int_7 %int_3
  958. ; CHECK: OpExtInst %void [[ext]] DebugValue {{%\w+}} %true
  959. OpLine %file_name 2 0
  960. %6 = OpSGreaterThan %bool %9 %int_3
  961. %val1 = OpExtInst %void %ext DebugValue %dbg_f1 %6 %null_expr
  962. OpSelectionMerge %25 None
  963. OpBranchConditional %6 %22 %23
  964. %22 = OpLabel
  965. %s1 = OpExtInst %void %ext DebugScope %bb1
  966. %7 = OpCopyObject %int %9
  967. %val2 = OpExtInst %void %ext DebugValue %dbg_f2 %7 %null_expr
  968. OpBranch %25
  969. %23 = OpLabel
  970. %s2 = OpExtInst %void %ext DebugScope %bb2
  971. %8 = OpCopyObject %int %int_4
  972. OpBranch %25
  973. %25 = OpLabel
  974. %s3 = OpExtInst %void %ext DebugScope %bb3
  975. %35 = OpPhi %int %7 %22 %8 %23
  976. OpStore %outparm %35
  977. OpReturn
  978. OpFunctionEnd
  979. )";
  980. SinglePassRunAndMatch<CCPPass>(text, true);
  981. }
  982. // Test from https://github.com/KhronosGroup/SPIRV-Tools/issues/3636
  983. TEST_F(CCPTest, CCPNoChangeFailure) {
  984. const std::string text = R"(
  985. OpCapability Shader
  986. %1 = OpExtInstImport "GLSL.std.450"
  987. OpMemoryModel Logical GLSL450
  988. OpEntryPoint Fragment %4 "main"
  989. OpExecutionMode %4 OriginUpperLeft
  990. OpSource ESSL 320
  991. %2 = OpTypeVoid
  992. %3 = OpTypeFunction %2
  993. %6 = OpTypeInt 32 1
  994. %7 = OpConstant %6 2
  995. %13 = OpConstant %6 4
  996. %21 = OpConstant %6 1
  997. %10 = OpTypeBool
  998. %17 = OpTypePointer Function %6
  999. ; CCP is generating two new constants during propagation that end up being
  1000. ; dead because they cannot be replaced anywhere in the IR. CCP was wrongly
  1001. ; considering the IR to be unmodified because of this.
  1002. ; CHECK: %true = OpConstantTrue %bool
  1003. ; CHECK: %int_3 = OpConstant %int 3
  1004. %4 = OpFunction %2 None %3
  1005. %11 = OpLabel
  1006. OpBranch %5
  1007. %5 = OpLabel
  1008. %23 = OpPhi %6 %7 %11 %20 %15
  1009. %9 = OpSLessThan %10 %23 %13
  1010. OpLoopMerge %8 %15 None
  1011. OpBranchConditional %9 %15 %8
  1012. %15 = OpLabel
  1013. %20 = OpIAdd %6 %23 %21
  1014. OpBranch %5
  1015. %8 = OpLabel
  1016. OpReturn
  1017. OpFunctionEnd
  1018. )";
  1019. auto result = SinglePassRunAndMatch<CCPPass>(text, true);
  1020. EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
  1021. }
  1022. // Test from https://github.com/KhronosGroup/SPIRV-Tools/issues/3738
  1023. // Similar to the previous one but more than one constant is generated in a
  1024. // single call to the instruction folder.
  1025. TEST_F(CCPTest, CCPNoChangeFailureSeveralConstantsDuringFolding) {
  1026. const std::string text = R"(
  1027. OpCapability Shader
  1028. %1 = OpExtInstImport "GLSL.std.450"
  1029. OpMemoryModel Logical GLSL450
  1030. OpEntryPoint Fragment %2 "main"
  1031. OpExecutionMode %2 OriginUpperLeft
  1032. %void = OpTypeVoid
  1033. %4 = OpTypeFunction %void
  1034. %float = OpTypeFloat 32
  1035. %v3float = OpTypeVector %float 3
  1036. %uint = OpTypeInt 32 0
  1037. %uint_0 = OpConstant %uint 0
  1038. %bool = OpTypeBool
  1039. %v3bool = OpTypeVector %bool 3
  1040. %float_0 = OpConstant %float 0
  1041. %12 = OpConstantComposite %v3float %float_0 %float_0 %float_0
  1042. %float_0_300000012 = OpConstant %float 0.300000012
  1043. %14 = OpConstantComposite %v3float %float_0_300000012 %float_0_300000012 %float_0_300000012
  1044. ; CCP is generating several constants during a single instruction evaluation.
  1045. ; When folding %19, it generates the constants %true and %24. They are dead
  1046. ; because they cannot be replaced anywhere in the IR. CCP was wrongly
  1047. ; considering the IR to be unmodified because of this.
  1048. ;
  1049. ; CHECK: %true = OpConstantTrue %bool
  1050. ; CHECK: %24 = OpConstantComposite %v3bool %true %true %true
  1051. ; CHECK: %float_1 = OpConstant %float 1
  1052. ; CHECK: %float_0_699999988 = OpConstant %float 0.699999988
  1053. %2 = OpFunction %void None %4
  1054. %15 = OpLabel
  1055. OpBranch %16
  1056. %16 = OpLabel
  1057. %17 = OpPhi %v3float %12 %15 %14 %18
  1058. %19 = OpFOrdLessThan %v3bool %17 %14
  1059. %20 = OpAll %bool %19
  1060. OpLoopMerge %21 %18 None
  1061. OpBranchConditional %20 %18 %21
  1062. %18 = OpLabel
  1063. OpBranch %16
  1064. %21 = OpLabel
  1065. %22 = OpExtInst %v3float %1 FMix %12 %17 %14
  1066. OpReturn
  1067. OpFunctionEnd
  1068. )";
  1069. auto result = SinglePassRunAndMatch<CCPPass>(text, true);
  1070. EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
  1071. }
  1072. // Test from https://github.com/KhronosGroup/SPIRV-Tools/issues/3991
  1073. // Similar to the previous one but constants are created even when no
  1074. // instruction are ever folded during propagation.
  1075. TEST_F(CCPTest, CCPNoChangeFailureWithUnfoldableInstr) {
  1076. const std::string text = R"(
  1077. OpCapability Shader
  1078. %1 = OpExtInstImport "GLSL.std.450"
  1079. OpMemoryModel Logical GLSL450
  1080. OpEntryPoint Fragment %2 "main"
  1081. OpExecutionMode %2 OriginUpperLeft
  1082. %void = OpTypeVoid
  1083. %4 = OpTypeFunction %void
  1084. %float = OpTypeFloat 32
  1085. %v3float = OpTypeVector %float 3
  1086. %uint = OpTypeInt 32 0
  1087. %uint_0 = OpConstant %uint 0
  1088. %bool = OpTypeBool
  1089. %float_0 = OpConstant %float 0
  1090. %11 = OpConstantComposite %v3float %float_0 %float_0 %float_0
  1091. %float_0_300000012 = OpConstant %float 0.300000012
  1092. %13 = OpConstantComposite %v3float %float_0_300000012 %float_0_300000012 %float_0_300000012
  1093. ; CCP generates two constants when trying to fold an instruction, which it
  1094. ; ultimately fails to fold. The instruction folder in CCP was only
  1095. ; checking for newly added constants if the instruction folds successfully.
  1096. ;
  1097. ; CHECK: %float_1 = OpConstant %float 1
  1098. ; CHECK: %float_0_699999988 = OpConstant %float 0.69999998
  1099. %2 = OpFunction %void None %4
  1100. %14 = OpLabel
  1101. %15 = OpBitcast %uint %float_0_300000012
  1102. %16 = OpUGreaterThan %bool %15 %uint_0
  1103. OpBranch %17
  1104. %17 = OpLabel
  1105. %18 = OpPhi %v3float %11 %14 %13 %19
  1106. OpLoopMerge %20 %19 None
  1107. OpBranchConditional %16 %19 %20
  1108. %19 = OpLabel
  1109. OpBranch %17
  1110. %20 = OpLabel
  1111. %21 = OpExtInst %v3float %1 FMix %11 %18 %13
  1112. OpReturn
  1113. OpFunctionEnd
  1114. )";
  1115. auto result = SinglePassRunAndMatch<CCPPass>(text, true);
  1116. EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
  1117. }
  1118. } // namespace
  1119. } // namespace opt
  1120. } // namespace spvtools