ccp_test.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  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, FoldConstantCompositeInstrucitonsWithSpecConst) {
  536. const std::string spv_asm = R"(
  537. OpCapability Shader
  538. OpMemoryModel Logical GLSL450
  539. OpEntryPoint Fragment %1 "main"
  540. OpExecutionMode %1 OriginUpperLeft
  541. %void = OpTypeVoid
  542. %4 = OpTypeFunction %void
  543. %bool = OpTypeBool
  544. %v3bool = OpTypeVector %bool 3
  545. %_struct_8 = OpTypeStruct %v3bool
  546. %true = OpConstantTrue %bool
  547. ; CHECK: [[spec_const:%\w+]] = OpSpecConstantComposite %v3bool
  548. %11 = OpSpecConstantComposite %v3bool %true %true %true
  549. %12 = OpConstantComposite %_struct_8 %11
  550. ; CHECK: OpFunction
  551. %1 = OpFunction %void None %4
  552. %29 = OpLabel
  553. %31 = OpCompositeExtract %v3bool %12 0
  554. ; CHECK: OpCompositeExtract %bool [[spec_const]] 0
  555. %32 = OpCompositeExtract %bool %31 0
  556. OpReturn
  557. OpFunctionEnd
  558. )";
  559. auto result = SinglePassRunAndMatch<CCPPass>(spv_asm, true);
  560. EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
  561. }
  562. TEST_F(CCPTest, UpdateSubsequentPhisToVarying) {
  563. const std::string text = R"(
  564. OpCapability Shader
  565. OpMemoryModel Logical GLSL450
  566. OpEntryPoint Fragment %func "func" %in
  567. OpExecutionMode %func OriginUpperLeft
  568. %void = OpTypeVoid
  569. %bool = OpTypeBool
  570. %int = OpTypeInt 32 1
  571. %false = OpConstantFalse %bool
  572. %int0 = OpConstant %int 0
  573. %int1 = OpConstant %int 1
  574. %int6 = OpConstant %int 6
  575. %int_ptr_Input = OpTypePointer Input %int
  576. %in = OpVariable %int_ptr_Input Input
  577. %undef = OpUndef %int
  578. ; Although no constants are propagated in this function, the propagator
  579. ; generates a new %true value while visiting conditional statements.
  580. ; CHECK: %true = OpConstantTrue %bool
  581. %functy = OpTypeFunction %void
  582. %func = OpFunction %void None %functy
  583. %1 = OpLabel
  584. OpBranch %2
  585. %2 = OpLabel
  586. %outer_phi = OpPhi %int %int0 %1 %outer_add %15
  587. %cond1 = OpSLessThanEqual %bool %outer_phi %int6
  588. OpLoopMerge %3 %15 None
  589. OpBranchConditional %cond1 %4 %3
  590. %4 = OpLabel
  591. %ld = OpLoad %int %in
  592. %cond2 = OpSGreaterThanEqual %bool %int1 %ld
  593. OpSelectionMerge %10 None
  594. OpBranchConditional %cond2 %8 %9
  595. %8 = OpLabel
  596. OpBranch %10
  597. %9 = OpLabel
  598. OpBranch %10
  599. %10 = OpLabel
  600. %extra_phi = OpPhi %int %outer_phi %8 %outer_phi %9
  601. OpBranch %11
  602. %11 = OpLabel
  603. %inner_phi = OpPhi %int %int0 %10 %inner_add %13
  604. %cond3 = OpSLessThanEqual %bool %inner_phi %int6
  605. OpLoopMerge %14 %13 None
  606. OpBranchConditional %cond3 %12 %14
  607. %12 = OpLabel
  608. OpBranch %13
  609. %13 = OpLabel
  610. %inner_add = OpIAdd %int %inner_phi %int1
  611. OpBranch %11
  612. %14 = OpLabel
  613. OpBranch %15
  614. %15 = OpLabel
  615. %outer_add = OpIAdd %int %extra_phi %int1
  616. OpBranch %2
  617. %3 = OpLabel
  618. OpReturn
  619. OpFunctionEnd
  620. )";
  621. auto result = SinglePassRunAndMatch<CCPPass>(text, true);
  622. EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
  623. }
  624. TEST_F(CCPTest, UndefInPhi) {
  625. const std::string text = R"(
  626. ; CHECK: [[uint1:%\w+]] = OpConstant {{%\w+}} 1
  627. ; CHECK: [[phi:%\w+]] = OpPhi
  628. ; CHECK: OpIAdd {{%\w+}} [[phi]] [[uint1]]
  629. OpCapability Kernel
  630. OpCapability Linkage
  631. OpMemoryModel Logical OpenCL
  632. OpDecorate %1 LinkageAttributes "func" Export
  633. %void = OpTypeVoid
  634. %bool = OpTypeBool
  635. %uint = OpTypeInt 32 0
  636. %uint_0 = OpConstant %uint 0
  637. %uint_1 = OpConstant %uint 1
  638. %7 = OpUndef %uint
  639. %8 = OpTypeFunction %void %bool
  640. %1 = OpFunction %void None %8
  641. %9 = OpFunctionParameter %bool
  642. %10 = OpLabel
  643. OpBranchConditional %9 %11 %12
  644. %11 = OpLabel
  645. OpBranch %13
  646. %12 = OpLabel
  647. OpBranch %14
  648. %14 = OpLabel
  649. OpBranchConditional %9 %13 %15
  650. %15 = OpLabel
  651. OpBranch %13
  652. %13 = OpLabel
  653. %16 = OpPhi %uint %uint_0 %11 %7 %14 %uint_1 %15
  654. %17 = OpIAdd %uint %16 %uint_1
  655. OpReturn
  656. OpFunctionEnd
  657. )";
  658. SinglePassRunAndMatch<CCPPass>(text, true);
  659. }
  660. // Just test to make sure the constant fold rules are being used. Will rely on
  661. // the folding test for specific testing of specific rules.
  662. TEST_F(CCPTest, UseConstantFoldingRules) {
  663. const std::string text = R"(
  664. ; CHECK: [[float1:%\w+]] = OpConstant {{%\w+}} 1
  665. ; CHECK: OpReturnValue [[float1]]
  666. OpCapability Shader
  667. OpCapability Linkage
  668. OpMemoryModel Logical GLSL450
  669. OpDecorate %1 LinkageAttributes "func" Export
  670. %void = OpTypeVoid
  671. %bool = OpTypeBool
  672. %float = OpTypeFloat 32
  673. %float_0 = OpConstant %float 0
  674. %float_1 = OpConstant %float 1
  675. %8 = OpTypeFunction %float
  676. %1 = OpFunction %float None %8
  677. %10 = OpLabel
  678. %17 = OpFAdd %float %float_0 %float_1
  679. OpReturnValue %17
  680. OpFunctionEnd
  681. )";
  682. SinglePassRunAndMatch<CCPPass>(text, true);
  683. }
  684. // Test for #1300. Previously value for %5 would not settle during simulation.
  685. TEST_F(CCPTest, SettlePhiLatticeValue) {
  686. const std::string text = R"(
  687. OpCapability Kernel
  688. OpCapability Linkage
  689. OpMemoryModel Logical OpenCL
  690. OpDecorate %func LinkageAttributes "func" Export
  691. %void = OpTypeVoid
  692. %bool = OpTypeBool
  693. %true = OpConstantTrue %bool
  694. %false = OpConstantFalse %bool
  695. %functy = OpTypeFunction %void
  696. %func = OpFunction %void None %functy
  697. %1 = OpLabel
  698. OpBranchConditional %true %2 %3
  699. %3 = OpLabel
  700. OpBranch %2
  701. %2 = OpLabel
  702. %5 = OpPhi %bool %true %1 %false %3
  703. OpReturn
  704. OpFunctionEnd
  705. )";
  706. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  707. SinglePassRunToBinary<CCPPass>(text, true);
  708. }
  709. TEST_F(CCPTest, NullBranchCondition) {
  710. const std::string text = R"(
  711. ; CHECK: [[int1:%\w+]] = OpConstant {{%\w+}} 1
  712. ; CHECK: [[int2:%\w+]] = OpConstant {{%\w+}} 2
  713. ; CHECK: OpIAdd {{%\w+}} [[int1]] [[int2]]
  714. OpCapability Shader
  715. OpMemoryModel Logical GLSL450
  716. OpEntryPoint Fragment %func "func"
  717. OpExecutionMode %func OriginUpperLeft
  718. %void = OpTypeVoid
  719. %bool = OpTypeBool
  720. %int = OpTypeInt 32 1
  721. %null = OpConstantNull %bool
  722. %int_1 = OpConstant %int 1
  723. %int_2 = OpConstant %int 2
  724. %functy = OpTypeFunction %void
  725. %func = OpFunction %void None %functy
  726. %1 = OpLabel
  727. OpSelectionMerge %2 None
  728. OpBranchConditional %null %2 %3
  729. %3 = OpLabel
  730. OpBranch %2
  731. %2 = OpLabel
  732. %phi = OpPhi %int %int_1 %1 %int_2 %3
  733. %add = OpIAdd %int %int_1 %phi
  734. OpReturn
  735. OpFunctionEnd
  736. )";
  737. SinglePassRunAndMatch<CCPPass>(text, true);
  738. }
  739. TEST_F(CCPTest, UndefBranchCondition) {
  740. const std::string text = R"(
  741. ; CHECK: [[int1:%\w+]] = OpConstant {{%\w+}} 1
  742. ; CHECK: [[phi:%\w+]] = OpPhi
  743. ; CHECK: OpIAdd {{%\w+}} [[int1]] [[phi]]
  744. OpCapability Shader
  745. OpMemoryModel Logical GLSL450
  746. OpEntryPoint Fragment %func "func"
  747. OpExecutionMode %func OriginUpperLeft
  748. %void = OpTypeVoid
  749. %bool = OpTypeBool
  750. %int = OpTypeInt 32 1
  751. %undef = OpUndef %bool
  752. %int_1 = OpConstant %int 1
  753. %int_2 = OpConstant %int 2
  754. %functy = OpTypeFunction %void
  755. %func = OpFunction %void None %functy
  756. %1 = OpLabel
  757. OpSelectionMerge %2 None
  758. OpBranchConditional %undef %2 %3
  759. %3 = OpLabel
  760. OpBranch %2
  761. %2 = OpLabel
  762. %phi = OpPhi %int %int_1 %1 %int_2 %3
  763. %add = OpIAdd %int %int_1 %phi
  764. OpReturn
  765. OpFunctionEnd
  766. )";
  767. SinglePassRunAndMatch<CCPPass>(text, true);
  768. }
  769. TEST_F(CCPTest, NullSwitchCondition) {
  770. const std::string text = R"(
  771. ; CHECK: [[int1:%\w+]] = OpConstant {{%\w+}} 1
  772. ; CHECK: [[int2:%\w+]] = OpConstant {{%\w+}} 2
  773. ; CHECK: OpIAdd {{%\w+}} [[int1]] [[int2]]
  774. OpCapability Shader
  775. OpMemoryModel Logical GLSL450
  776. OpEntryPoint Fragment %func "func"
  777. OpExecutionMode %func OriginUpperLeft
  778. %void = OpTypeVoid
  779. %int = OpTypeInt 32 1
  780. %null = OpConstantNull %int
  781. %int_1 = OpConstant %int 1
  782. %int_2 = OpConstant %int 2
  783. %functy = OpTypeFunction %void
  784. %func = OpFunction %void None %functy
  785. %1 = OpLabel
  786. OpSelectionMerge %2 None
  787. OpSwitch %null %2 0 %3
  788. %3 = OpLabel
  789. OpBranch %2
  790. %2 = OpLabel
  791. %phi = OpPhi %int %int_1 %1 %int_2 %3
  792. %add = OpIAdd %int %int_1 %phi
  793. OpReturn
  794. OpFunctionEnd
  795. )";
  796. SinglePassRunAndMatch<CCPPass>(text, true);
  797. }
  798. TEST_F(CCPTest, UndefSwitchCondition) {
  799. const std::string text = R"(
  800. ; CHECK: [[int1:%\w+]] = OpConstant {{%\w+}} 1
  801. ; CHECK: [[phi:%\w+]] = OpPhi
  802. ; CHECK: OpIAdd {{%\w+}} [[int1]] [[phi]]
  803. OpCapability Shader
  804. OpMemoryModel Logical GLSL450
  805. OpEntryPoint Fragment %func "func"
  806. OpExecutionMode %func OriginUpperLeft
  807. %void = OpTypeVoid
  808. %int = OpTypeInt 32 1
  809. %undef = OpUndef %int
  810. %int_1 = OpConstant %int 1
  811. %int_2 = OpConstant %int 2
  812. %functy = OpTypeFunction %void
  813. %func = OpFunction %void None %functy
  814. %1 = OpLabel
  815. OpSelectionMerge %2 None
  816. OpSwitch %undef %2 0 %3
  817. %3 = OpLabel
  818. OpBranch %2
  819. %2 = OpLabel
  820. %phi = OpPhi %int %int_1 %1 %int_2 %3
  821. %add = OpIAdd %int %int_1 %phi
  822. OpReturn
  823. OpFunctionEnd
  824. )";
  825. SinglePassRunAndMatch<CCPPass>(text, true);
  826. }
  827. // Test for #1361.
  828. TEST_F(CCPTest, CompositeConstructOfGlobalValue) {
  829. const std::string text = R"(
  830. ; CHECK: [[phi:%\w+]] = OpPhi
  831. ; CHECK-NEXT: OpCompositeExtract {{%\w+}} [[phi]] 0
  832. OpCapability Shader
  833. OpMemoryModel Logical GLSL450
  834. OpEntryPoint Fragment %func "func" %in
  835. OpExecutionMode %func OriginUpperLeft
  836. %void = OpTypeVoid
  837. %int = OpTypeInt 32 1
  838. %bool = OpTypeBool
  839. %functy = OpTypeFunction %void
  840. %ptr_int_Input = OpTypePointer Input %int
  841. %in = OpVariable %ptr_int_Input Input
  842. %struct = OpTypeStruct %ptr_int_Input %ptr_int_Input
  843. %struct_null = OpConstantNull %struct
  844. %func = OpFunction %void None %functy
  845. %1 = OpLabel
  846. OpBranch %2
  847. %2 = OpLabel
  848. %phi = OpPhi %struct %struct_null %1 %5 %4
  849. %extract = OpCompositeExtract %ptr_int_Input %phi 0
  850. OpLoopMerge %3 %4 None
  851. OpBranch %4
  852. %4 = OpLabel
  853. %5 = OpCompositeConstruct %struct %in %in
  854. OpBranch %2
  855. %3 = OpLabel
  856. OpReturn
  857. OpFunctionEnd
  858. )";
  859. SinglePassRunAndMatch<CCPPass>(text, true);
  860. }
  861. TEST_F(CCPTest, FoldWithDecoration) {
  862. const std::string text = R"(
  863. ; CHECK: OpCapability
  864. ; CHECK-NOT: OpDecorate
  865. ; CHECK: OpFunctionEnd
  866. OpCapability Shader
  867. %1 = OpExtInstImport "GLSL.std.450"
  868. OpMemoryModel Logical GLSL450
  869. OpEntryPoint Fragment %2 "main"
  870. OpExecutionMode %2 OriginUpperLeft
  871. OpSource ESSL 310
  872. OpDecorate %3 RelaxedPrecision
  873. %void = OpTypeVoid
  874. %5 = OpTypeFunction %void
  875. %float = OpTypeFloat 32
  876. %v3float = OpTypeVector %float 3
  877. %float_0 = OpConstant %float 0
  878. %v4float = OpTypeVector %float 4
  879. %10 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
  880. %2 = OpFunction %void None %5
  881. %11 = OpLabel
  882. %3 = OpVectorShuffle %v3float %10 %10 0 1 2
  883. OpReturn
  884. OpFunctionEnd
  885. )";
  886. SinglePassRunAndMatch<CCPPass>(text, true);
  887. }
  888. TEST_F(CCPTest, DebugSimpleFoldConstant) {
  889. const std::string text = R"(
  890. OpCapability Shader
  891. OpCapability Linkage
  892. %ext = OpExtInstImport "OpenCL.DebugInfo.100"
  893. OpMemoryModel Logical GLSL450
  894. %file_name = OpString "test"
  895. %float_name = OpString "float"
  896. %main_name = OpString "main"
  897. %f_name = OpString "f"
  898. OpDecorate %1 LinkageAttributes "func" Export
  899. %void = OpTypeVoid
  900. %bool = OpTypeBool
  901. %float = OpTypeFloat 32
  902. %float_0 = OpConstant %float 0
  903. ; CHECK: [[float1:%\w+]] = OpConstant {{%\w+}} 1
  904. %float_1 = OpConstant %float 1
  905. %uint = OpTypeInt 32 0
  906. %uint_32 = OpConstant %uint 32
  907. %8 = OpTypeFunction %float
  908. %null_expr = OpExtInst %void %ext DebugExpression
  909. %src = OpExtInst %void %ext DebugSource %file_name
  910. %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
  911. %dbg_tf = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
  912. %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_tf
  913. %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 0 0 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %1
  914. %dbg_f = OpExtInst %void %ext DebugLocalVariable %f_name %dbg_tf %src 0 0 %dbg_main FlagIsLocal
  915. %1 = OpFunction %float None %8
  916. %10 = OpLabel
  917. ; CHECK: OpExtInst %void [[ext:%\w+]] DebugScope
  918. ; CHECK: OpLine [[file:%\w+]] 1 0
  919. ; CHECK: OpExtInst %void [[ext]] DebugValue {{%\w+}} %float_1
  920. %s0 = OpExtInst %void %ext DebugScope %dbg_main
  921. OpLine %file_name 1 0
  922. %17 = OpFAdd %float %float_0 %float_1
  923. %val = OpExtInst %void %ext DebugValue %dbg_f %17 %null_expr
  924. ; CHECK: OpLine [[file]] 2 0
  925. ; CHECK: OpReturnValue [[float1]]
  926. OpLine %file_name 2 0
  927. OpReturnValue %17
  928. OpFunctionEnd
  929. )";
  930. SinglePassRunAndMatch<CCPPass>(text, true);
  931. }
  932. TEST_F(CCPTest, DebugFoldMultipleForSingleConstant) {
  933. const std::string text = R"(
  934. OpCapability Shader
  935. %1 = OpExtInstImport "GLSL.std.450"
  936. %ext = OpExtInstImport "OpenCL.DebugInfo.100"
  937. OpMemoryModel Logical GLSL450
  938. OpEntryPoint Fragment %main "main" %outparm
  939. OpExecutionMode %main OriginUpperLeft
  940. OpSource GLSL 450
  941. %file_name = OpString "test"
  942. %float_name = OpString "float"
  943. %main_name = OpString "main"
  944. %f_name = OpString "f"
  945. OpName %main "main"
  946. OpName %outparm "outparm"
  947. OpDecorate %outparm Location 0
  948. %void = OpTypeVoid
  949. %3 = OpTypeFunction %void
  950. %int = OpTypeInt 32 1
  951. %bool = OpTypeBool
  952. %_ptr_Function_int = OpTypePointer Function %int
  953. %int_4 = OpConstant %int 4
  954. %int_3 = OpConstant %int 3
  955. %int_1 = OpConstant %int 1
  956. %uint = OpTypeInt 32 0
  957. %uint_32 = OpConstant %uint 32
  958. %_ptr_Output_int = OpTypePointer Output %int
  959. %outparm = OpVariable %_ptr_Output_int Output
  960. %null_expr = OpExtInst %void %ext DebugExpression
  961. %src = OpExtInst %void %ext DebugSource %file_name
  962. %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
  963. %dbg_tf = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
  964. %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_tf
  965. %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 0 0 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
  966. %bb0 = OpExtInst %void %ext DebugLexicalBlock %src 0 0 %dbg_main
  967. %bb1 = OpExtInst %void %ext DebugLexicalBlock %src 1 0 %dbg_main
  968. %bb2 = OpExtInst %void %ext DebugLexicalBlock %src 2 0 %dbg_main
  969. %bb3 = OpExtInst %void %ext DebugLexicalBlock %src 3 0 %dbg_main
  970. %dbg_f0 = OpExtInst %void %ext DebugLocalVariable %f_name %dbg_tf %src 0 0 %dbg_main FlagIsLocal
  971. %dbg_f1 = OpExtInst %void %ext DebugLocalVariable %f_name %dbg_tf %src 1 0 %dbg_main FlagIsLocal
  972. %dbg_f2 = OpExtInst %void %ext DebugLocalVariable %f_name %dbg_tf %src 2 0 %dbg_main FlagIsLocal
  973. %main = OpFunction %void None %3
  974. %4 = OpLabel
  975. ; CHECK: OpExtInst %void [[ext:%\w+]] DebugScope
  976. ; CHECK: OpLine [[file:%\w+]] 1 0
  977. ; CHECK: OpIAdd %int %int_4 %int_3
  978. ; CHECK: OpExtInst %void [[ext]] DebugValue {{%\w+}} %int_7
  979. %s0 = OpExtInst %void %ext DebugScope %bb0
  980. OpLine %file_name 1 0
  981. %9 = OpIAdd %int %int_4 %int_3
  982. %val0 = OpExtInst %void %ext DebugValue %dbg_f0 %9 %null_expr
  983. ; CHECK: OpLine [[file]] 2 0
  984. ; CHECK: OpSGreaterThan %bool %int_7 %int_3
  985. ; CHECK: OpExtInst %void [[ext]] DebugValue {{%\w+}} %true
  986. OpLine %file_name 2 0
  987. %6 = OpSGreaterThan %bool %9 %int_3
  988. %val1 = OpExtInst %void %ext DebugValue %dbg_f1 %6 %null_expr
  989. OpSelectionMerge %25 None
  990. OpBranchConditional %6 %22 %23
  991. %22 = OpLabel
  992. %s1 = OpExtInst %void %ext DebugScope %bb1
  993. %7 = OpCopyObject %int %9
  994. %val2 = OpExtInst %void %ext DebugValue %dbg_f2 %7 %null_expr
  995. OpBranch %25
  996. %23 = OpLabel
  997. %s2 = OpExtInst %void %ext DebugScope %bb2
  998. %8 = OpCopyObject %int %int_4
  999. OpBranch %25
  1000. %25 = OpLabel
  1001. %s3 = OpExtInst %void %ext DebugScope %bb3
  1002. %35 = OpPhi %int %7 %22 %8 %23
  1003. OpStore %outparm %35
  1004. OpReturn
  1005. OpFunctionEnd
  1006. )";
  1007. SinglePassRunAndMatch<CCPPass>(text, true);
  1008. }
  1009. // Test from https://github.com/KhronosGroup/SPIRV-Tools/issues/3636
  1010. TEST_F(CCPTest, CCPNoChangeFailure) {
  1011. const std::string text = R"(
  1012. OpCapability Shader
  1013. %1 = OpExtInstImport "GLSL.std.450"
  1014. OpMemoryModel Logical GLSL450
  1015. OpEntryPoint Fragment %4 "main"
  1016. OpExecutionMode %4 OriginUpperLeft
  1017. OpSource ESSL 320
  1018. %2 = OpTypeVoid
  1019. %3 = OpTypeFunction %2
  1020. %6 = OpTypeInt 32 1
  1021. %7 = OpConstant %6 2
  1022. %13 = OpConstant %6 4
  1023. %21 = OpConstant %6 1
  1024. %10 = OpTypeBool
  1025. %17 = OpTypePointer Function %6
  1026. ; CCP is generating two new constants during propagation that end up being
  1027. ; dead because they cannot be replaced anywhere in the IR. CCP was wrongly
  1028. ; considering the IR to be unmodified because of this.
  1029. ; CHECK: %true = OpConstantTrue %bool
  1030. ; CHECK: %int_3 = OpConstant %int 3
  1031. %4 = OpFunction %2 None %3
  1032. %11 = OpLabel
  1033. OpBranch %5
  1034. %5 = OpLabel
  1035. %23 = OpPhi %6 %7 %11 %20 %15
  1036. %9 = OpSLessThan %10 %23 %13
  1037. OpLoopMerge %8 %15 None
  1038. OpBranchConditional %9 %15 %8
  1039. %15 = OpLabel
  1040. %20 = OpIAdd %6 %23 %21
  1041. OpBranch %5
  1042. %8 = OpLabel
  1043. OpReturn
  1044. OpFunctionEnd
  1045. )";
  1046. auto result = SinglePassRunAndMatch<CCPPass>(text, true);
  1047. EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
  1048. }
  1049. // Test from https://github.com/KhronosGroup/SPIRV-Tools/issues/3738
  1050. // Similar to the previous one but more than one constant is generated in a
  1051. // single call to the instruction folder.
  1052. TEST_F(CCPTest, CCPNoChangeFailureSeveralConstantsDuringFolding) {
  1053. const std::string text = R"(
  1054. OpCapability Shader
  1055. %1 = OpExtInstImport "GLSL.std.450"
  1056. OpMemoryModel Logical GLSL450
  1057. OpEntryPoint Fragment %2 "main"
  1058. OpExecutionMode %2 OriginUpperLeft
  1059. %void = OpTypeVoid
  1060. %4 = OpTypeFunction %void
  1061. %float = OpTypeFloat 32
  1062. %v3float = OpTypeVector %float 3
  1063. %uint = OpTypeInt 32 0
  1064. %uint_0 = OpConstant %uint 0
  1065. %bool = OpTypeBool
  1066. %v3bool = OpTypeVector %bool 3
  1067. %float_0 = OpConstant %float 0
  1068. %12 = OpConstantComposite %v3float %float_0 %float_0 %float_0
  1069. %float_0_300000012 = OpConstant %float 0.300000012
  1070. %14 = OpConstantComposite %v3float %float_0_300000012 %float_0_300000012 %float_0_300000012
  1071. ; CCP is generating several constants during a single instruction evaluation.
  1072. ; When folding %19, it generates the constants %true and %24. They are dead
  1073. ; because they cannot be replaced anywhere in the IR. CCP was wrongly
  1074. ; considering the IR to be unmodified because of this.
  1075. ;
  1076. ; CHECK: %true = OpConstantTrue %bool
  1077. ; CHECK: %24 = OpConstantComposite %v3bool %true %true %true
  1078. ; CHECK: %float_1 = OpConstant %float 1
  1079. ; CHECK: %float_0_699999988 = OpConstant %float 0.699999988
  1080. %2 = OpFunction %void None %4
  1081. %15 = OpLabel
  1082. OpBranch %16
  1083. %16 = OpLabel
  1084. %17 = OpPhi %v3float %12 %15 %14 %18
  1085. %19 = OpFOrdLessThan %v3bool %17 %14
  1086. %20 = OpAll %bool %19
  1087. OpLoopMerge %21 %18 None
  1088. OpBranchConditional %20 %18 %21
  1089. %18 = OpLabel
  1090. OpBranch %16
  1091. %21 = OpLabel
  1092. %22 = OpExtInst %v3float %1 FMix %12 %17 %14
  1093. OpReturn
  1094. OpFunctionEnd
  1095. )";
  1096. auto result = SinglePassRunAndMatch<CCPPass>(text, true);
  1097. EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
  1098. }
  1099. // Test from https://github.com/KhronosGroup/SPIRV-Tools/issues/3991
  1100. // Similar to the previous one but constants are created even when no
  1101. // instruction are ever folded during propagation.
  1102. TEST_F(CCPTest, CCPNoChangeFailureWithUnfoldableInstr) {
  1103. const std::string text = R"(
  1104. OpCapability Shader
  1105. %1 = OpExtInstImport "GLSL.std.450"
  1106. OpMemoryModel Logical GLSL450
  1107. OpEntryPoint Fragment %2 "main"
  1108. OpExecutionMode %2 OriginUpperLeft
  1109. %void = OpTypeVoid
  1110. %4 = OpTypeFunction %void
  1111. %float = OpTypeFloat 32
  1112. %v3float = OpTypeVector %float 3
  1113. %uint = OpTypeInt 32 0
  1114. %uint_0 = OpConstant %uint 0
  1115. %bool = OpTypeBool
  1116. %float_0 = OpConstant %float 0
  1117. %11 = OpConstantComposite %v3float %float_0 %float_0 %float_0
  1118. %float_0_300000012 = OpConstant %float 0.300000012
  1119. %13 = OpConstantComposite %v3float %float_0_300000012 %float_0_300000012 %float_0_300000012
  1120. ; CCP generates two constants when trying to fold an instruction, which it
  1121. ; ultimately fails to fold. The instruction folder in CCP was only
  1122. ; checking for newly added constants if the instruction folds successfully.
  1123. ;
  1124. ; CHECK: %float_1 = OpConstant %float 1
  1125. ; CHECK: %float_0_699999988 = OpConstant %float 0.69999998
  1126. %2 = OpFunction %void None %4
  1127. %14 = OpLabel
  1128. %15 = OpBitcast %uint %float_0_300000012
  1129. %16 = OpUGreaterThan %bool %15 %uint_0
  1130. OpBranch %17
  1131. %17 = OpLabel
  1132. %18 = OpPhi %v3float %11 %14 %13 %19
  1133. OpLoopMerge %20 %19 None
  1134. OpBranchConditional %16 %19 %20
  1135. %19 = OpLabel
  1136. OpBranch %17
  1137. %20 = OpLabel
  1138. %21 = OpExtInst %v3float %1 FMix %11 %18 %13
  1139. OpReturn
  1140. OpFunctionEnd
  1141. )";
  1142. auto result = SinglePassRunAndMatch<CCPPass>(text, true);
  1143. EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
  1144. }
  1145. TEST_F(CCPTest, FunctionDeclaration) {
  1146. // Make sure the pass works with a function declaration that is called.
  1147. const std::string text = R"(OpCapability Addresses
  1148. OpCapability Linkage
  1149. OpCapability Kernel
  1150. OpCapability Int8
  1151. %1 = OpExtInstImport "OpenCL.std"
  1152. OpMemoryModel Physical64 OpenCL
  1153. OpEntryPoint Kernel %2 "_Z23julia__1166_kernel_77094Bool"
  1154. OpExecutionMode %2 ContractionOff
  1155. OpSource Unknown 0
  1156. OpDecorate %3 LinkageAttributes "julia_error_7712" Import
  1157. %void = OpTypeVoid
  1158. %5 = OpTypeFunction %void
  1159. %3 = OpFunction %void None %5
  1160. OpFunctionEnd
  1161. %2 = OpFunction %void None %5
  1162. %6 = OpLabel
  1163. %7 = OpFunctionCall %void %3
  1164. OpReturn
  1165. OpFunctionEnd
  1166. )";
  1167. SinglePassRunAndCheck<CCPPass>(text, text, false);
  1168. }
  1169. // Test from https://github.com/KhronosGroup/SPIRV-Tools/issues/4462.
  1170. // The test was causing a lateral movement in the constant lattice, which was
  1171. // not being detected as varying by CCP. In this test, FClamp is evaluated
  1172. // twice. On the first evaluation, if computes FClamp(0.5, 0.5, -1) which
  1173. // returns -1. On the second evaluation, it computes FClamp(0.5, 0.5, VARYING)
  1174. // which returns 0.5.
  1175. //
  1176. // Both fold() computations are correct given the semantics of FClamp() but
  1177. // this causes a lateral transition in the constant lattice which was not being
  1178. // considered VARYING by CCP.
  1179. TEST_F(CCPTest, LateralLatticeTransition) {
  1180. const std::string text = R"(OpCapability Shader
  1181. %1 = OpExtInstImport "GLSL.std.450"
  1182. OpMemoryModel Logical GLSL450
  1183. OpEntryPoint Fragment %main "main" %gl_FragCoord %outColor
  1184. OpExecutionMode %main OriginUpperLeft
  1185. OpSource ESSL 310
  1186. OpName %main "main"
  1187. OpName %gl_FragCoord "gl_FragCoord"
  1188. OpName %outColor "outColor"
  1189. OpDecorate %gl_FragCoord BuiltIn FragCoord
  1190. OpDecorate %outColor Location 0
  1191. %void = OpTypeVoid
  1192. %6 = OpTypeFunction %void
  1193. %float = OpTypeFloat 32
  1194. %float_0_5 = OpConstant %float 0.5
  1195. %v4float = OpTypeVector %float 4
  1196. %_ptr_Input_v4float = OpTypePointer Input %v4float
  1197. %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
  1198. %uint = OpTypeInt 32 0
  1199. %uint_0 = OpConstant %uint 0
  1200. %_ptr_Input_float = OpTypePointer Input %float
  1201. %float_0 = OpConstant %float 0
  1202. %bool = OpTypeBool
  1203. %float_n1 = OpConstant %float -1
  1204. %float_1 = OpConstant %float 1
  1205. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1206. %outColor = OpVariable %_ptr_Output_v4float Output
  1207. ; This constant is created during the first evaluation of the CompositeConstruct
  1208. ; CHECK: [[new_constant:%\d+]] = OpConstantComposite %v4float %float_n1 %float_0_5 %float_0 %float_1
  1209. %main = OpFunction %void None %6
  1210. %19 = OpLabel
  1211. %20 = OpAccessChain %_ptr_Input_float %gl_FragCoord %uint_0
  1212. %21 = OpLoad %float %20
  1213. %22 = OpFOrdLessThan %bool %21 %float_0
  1214. OpSelectionMerge %23 None
  1215. OpBranchConditional %22 %24 %25
  1216. %24 = OpLabel
  1217. OpBranch %23
  1218. %25 = OpLabel
  1219. OpBranch %26
  1220. %26 = OpLabel
  1221. OpBranch %23
  1222. %23 = OpLabel
  1223. %27 = OpPhi %float %float_n1 %24 %float_0_5 %26
  1224. %28 = OpExtInst %float %1 FClamp %float_0_5 %float_0_5 %27
  1225. ; On first evaluation, the result from FClamp will return 0.5.
  1226. ; But on second evaluation, FClamp should return VARYING. Check
  1227. ; that CCP is not keeping the first result.
  1228. ; CHECK-NOT: %29 = OpCompositeConstruct %v4float %float_0_5 %float_0_5 %float_0 %float_1
  1229. %29 = OpCompositeConstruct %v4float %28 %float_0_5 %float_0 %float_1
  1230. ; CHECK-NOT: OpCopyObject %v4float [[new_constant]]
  1231. %42 = OpCopyObject %v4float %29
  1232. ; CHECK-NOT: OpStore %outColor [[new_constant]]
  1233. OpStore %outColor %42
  1234. OpReturn
  1235. OpFunctionEnd
  1236. )";
  1237. auto result = SinglePassRunAndMatch<CCPPass>(text, true);
  1238. EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
  1239. }
  1240. } // namespace
  1241. } // namespace opt
  1242. } // namespace spvtools