dependence_analysis.cpp 139 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205
  1. // Copyright (c) 2018 Google LLC.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <memory>
  15. #include <set>
  16. #include <string>
  17. #include <unordered_set>
  18. #include <utility>
  19. #include <vector>
  20. #include "gmock/gmock.h"
  21. #include "source/opt/iterator.h"
  22. #include "source/opt/loop_dependence.h"
  23. #include "source/opt/loop_descriptor.h"
  24. #include "source/opt/pass.h"
  25. #include "source/opt/tree_iterator.h"
  26. #include "test/opt//assembly_builder.h"
  27. #include "test/opt//function_utils.h"
  28. #include "test/opt//pass_fixture.h"
  29. #include "test/opt//pass_utils.h"
  30. namespace spvtools {
  31. namespace opt {
  32. namespace {
  33. using DependencyAnalysis = ::testing::Test;
  34. /*
  35. Generated from the following GLSL fragment shader
  36. with --eliminate-local-multi-store
  37. #version 440 core
  38. void main(){
  39. int[10] arr;
  40. int[10] arr2;
  41. int a = 2;
  42. for (int i = 0; i < 10; i++) {
  43. arr[a] = arr[3];
  44. arr[a*2] = arr[a+3];
  45. arr[6] = arr2[6];
  46. arr[a+5] = arr2[7];
  47. }
  48. }
  49. */
  50. TEST(DependencyAnalysis, ZIV) {
  51. const std::string text = R"( OpCapability Shader
  52. %1 = OpExtInstImport "GLSL.std.450"
  53. OpMemoryModel Logical GLSL450
  54. OpEntryPoint Fragment %4 "main"
  55. OpExecutionMode %4 OriginUpperLeft
  56. OpSource GLSL 440
  57. OpName %4 "main"
  58. OpName %25 "arr"
  59. OpName %39 "arr2"
  60. %2 = OpTypeVoid
  61. %3 = OpTypeFunction %2
  62. %6 = OpTypeInt 32 1
  63. %7 = OpTypePointer Function %6
  64. %9 = OpConstant %6 2
  65. %11 = OpConstant %6 0
  66. %18 = OpConstant %6 10
  67. %19 = OpTypeBool
  68. %21 = OpTypeInt 32 0
  69. %22 = OpConstant %21 10
  70. %23 = OpTypeArray %6 %22
  71. %24 = OpTypePointer Function %23
  72. %27 = OpConstant %6 3
  73. %38 = OpConstant %6 6
  74. %44 = OpConstant %6 5
  75. %46 = OpConstant %6 7
  76. %51 = OpConstant %6 1
  77. %4 = OpFunction %2 None %3
  78. %5 = OpLabel
  79. %25 = OpVariable %24 Function
  80. %39 = OpVariable %24 Function
  81. OpBranch %12
  82. %12 = OpLabel
  83. %53 = OpPhi %6 %11 %5 %52 %15
  84. OpLoopMerge %14 %15 None
  85. OpBranch %16
  86. %16 = OpLabel
  87. %20 = OpSLessThan %19 %53 %18
  88. OpBranchConditional %20 %13 %14
  89. %13 = OpLabel
  90. %28 = OpAccessChain %7 %25 %27
  91. %29 = OpLoad %6 %28
  92. %30 = OpAccessChain %7 %25 %9
  93. OpStore %30 %29
  94. %32 = OpIMul %6 %9 %9
  95. %34 = OpIAdd %6 %9 %27
  96. %35 = OpAccessChain %7 %25 %34
  97. %36 = OpLoad %6 %35
  98. %37 = OpAccessChain %7 %25 %32
  99. OpStore %37 %36
  100. %40 = OpAccessChain %7 %39 %38
  101. %41 = OpLoad %6 %40
  102. %42 = OpAccessChain %7 %25 %38
  103. OpStore %42 %41
  104. %45 = OpIAdd %6 %9 %44
  105. %47 = OpAccessChain %7 %39 %46
  106. %48 = OpLoad %6 %47
  107. %49 = OpAccessChain %7 %25 %45
  108. OpStore %49 %48
  109. OpBranch %15
  110. %15 = OpLabel
  111. %52 = OpIAdd %6 %53 %51
  112. OpBranch %12
  113. %14 = OpLabel
  114. OpReturn
  115. OpFunctionEnd
  116. )";
  117. std::unique_ptr<IRContext> context =
  118. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  119. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  120. Module* module = context->module();
  121. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  122. << text << std::endl;
  123. const Function* f = spvtest::GetFunction(module, 4);
  124. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  125. Loop* loop = &ld.GetLoopByIndex(0);
  126. std::vector<const Loop*> loops{loop};
  127. LoopDependenceAnalysis analysis{context.get(), loops};
  128. const Instruction* store[4];
  129. int stores_found = 0;
  130. for (const Instruction& inst : *spvtest::GetBasicBlock(f, 13)) {
  131. if (inst.opcode() == SpvOp::SpvOpStore) {
  132. store[stores_found] = &inst;
  133. ++stores_found;
  134. }
  135. }
  136. for (int i = 0; i < 4; ++i) {
  137. EXPECT_TRUE(store[i]);
  138. }
  139. // 29 -> 30 tests looking through constants.
  140. {
  141. DistanceVector distance_vector{loops.size()};
  142. EXPECT_TRUE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(29),
  143. store[0], &distance_vector));
  144. }
  145. // 36 -> 37 tests looking through additions.
  146. {
  147. DistanceVector distance_vector{loops.size()};
  148. EXPECT_TRUE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(36),
  149. store[1], &distance_vector));
  150. }
  151. // 41 -> 42 tests looking at same index across two different arrays.
  152. {
  153. DistanceVector distance_vector{loops.size()};
  154. EXPECT_TRUE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(41),
  155. store[2], &distance_vector));
  156. }
  157. // 48 -> 49 tests looking through additions for same index in two different
  158. // arrays.
  159. {
  160. DistanceVector distance_vector{loops.size()};
  161. EXPECT_TRUE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(48),
  162. store[3], &distance_vector));
  163. }
  164. }
  165. /*
  166. Generated from the following GLSL fragment shader
  167. with --eliminate-local-multi-store
  168. #version 440 core
  169. layout(location = 0) in vec4 c;
  170. void main(){
  171. int[10] arr;
  172. int[10] arr2;
  173. int[10] arr3;
  174. int[10] arr4;
  175. int[10] arr5;
  176. int N = int(c.x);
  177. for (int i = 0; i < N; i++) {
  178. arr[2*N] = arr[N];
  179. arr2[2*N+1] = arr2[N];
  180. arr3[2*N] = arr3[N-1];
  181. arr4[N] = arr5[N];
  182. }
  183. }
  184. */
  185. TEST(DependencyAnalysis, SymbolicZIV) {
  186. const std::string text = R"( OpCapability Shader
  187. %1 = OpExtInstImport "GLSL.std.450"
  188. OpMemoryModel Logical GLSL450
  189. OpEntryPoint Fragment %4 "main" %12
  190. OpExecutionMode %4 OriginUpperLeft
  191. OpSource GLSL 440
  192. OpName %4 "main"
  193. OpName %12 "c"
  194. OpName %33 "arr"
  195. OpName %41 "arr2"
  196. OpName %50 "arr3"
  197. OpName %58 "arr4"
  198. OpName %60 "arr5"
  199. OpDecorate %12 Location 0
  200. %2 = OpTypeVoid
  201. %3 = OpTypeFunction %2
  202. %6 = OpTypeInt 32 1
  203. %7 = OpTypePointer Function %6
  204. %9 = OpTypeFloat 32
  205. %10 = OpTypeVector %9 4
  206. %11 = OpTypePointer Input %10
  207. %12 = OpVariable %11 Input
  208. %13 = OpTypeInt 32 0
  209. %14 = OpConstant %13 0
  210. %15 = OpTypePointer Input %9
  211. %20 = OpConstant %6 0
  212. %28 = OpTypeBool
  213. %30 = OpConstant %13 10
  214. %31 = OpTypeArray %6 %30
  215. %32 = OpTypePointer Function %31
  216. %34 = OpConstant %6 2
  217. %44 = OpConstant %6 1
  218. %4 = OpFunction %2 None %3
  219. %5 = OpLabel
  220. %33 = OpVariable %32 Function
  221. %41 = OpVariable %32 Function
  222. %50 = OpVariable %32 Function
  223. %58 = OpVariable %32 Function
  224. %60 = OpVariable %32 Function
  225. %16 = OpAccessChain %15 %12 %14
  226. %17 = OpLoad %9 %16
  227. %18 = OpConvertFToS %6 %17
  228. OpBranch %21
  229. %21 = OpLabel
  230. %67 = OpPhi %6 %20 %5 %66 %24
  231. OpLoopMerge %23 %24 None
  232. OpBranch %25
  233. %25 = OpLabel
  234. %29 = OpSLessThan %28 %67 %18
  235. OpBranchConditional %29 %22 %23
  236. %22 = OpLabel
  237. %36 = OpIMul %6 %34 %18
  238. %38 = OpAccessChain %7 %33 %18
  239. %39 = OpLoad %6 %38
  240. %40 = OpAccessChain %7 %33 %36
  241. OpStore %40 %39
  242. %43 = OpIMul %6 %34 %18
  243. %45 = OpIAdd %6 %43 %44
  244. %47 = OpAccessChain %7 %41 %18
  245. %48 = OpLoad %6 %47
  246. %49 = OpAccessChain %7 %41 %45
  247. OpStore %49 %48
  248. %52 = OpIMul %6 %34 %18
  249. %54 = OpISub %6 %18 %44
  250. %55 = OpAccessChain %7 %50 %54
  251. %56 = OpLoad %6 %55
  252. %57 = OpAccessChain %7 %50 %52
  253. OpStore %57 %56
  254. %62 = OpAccessChain %7 %60 %18
  255. %63 = OpLoad %6 %62
  256. %64 = OpAccessChain %7 %58 %18
  257. OpStore %64 %63
  258. OpBranch %24
  259. %24 = OpLabel
  260. %66 = OpIAdd %6 %67 %44
  261. OpBranch %21
  262. %23 = OpLabel
  263. OpReturn
  264. OpFunctionEnd
  265. )";
  266. std::unique_ptr<IRContext> context =
  267. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  268. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  269. Module* module = context->module();
  270. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  271. << text << std::endl;
  272. const Function* f = spvtest::GetFunction(module, 4);
  273. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  274. Loop* loop = &ld.GetLoopByIndex(0);
  275. std::vector<const Loop*> loops{loop};
  276. LoopDependenceAnalysis analysis{context.get(), loops};
  277. const Instruction* store[4];
  278. int stores_found = 0;
  279. for (const Instruction& inst : *spvtest::GetBasicBlock(f, 22)) {
  280. if (inst.opcode() == SpvOp::SpvOpStore) {
  281. store[stores_found] = &inst;
  282. ++stores_found;
  283. }
  284. }
  285. for (int i = 0; i < 4; ++i) {
  286. EXPECT_TRUE(store[i]);
  287. }
  288. // independent due to loop bounds (won't enter if N <= 0).
  289. // 39 -> 40 tests looking through symbols and multiplicaiton.
  290. {
  291. DistanceVector distance_vector{loops.size()};
  292. EXPECT_TRUE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(39),
  293. store[0], &distance_vector));
  294. }
  295. // 48 -> 49 tests looking through symbols and multiplication + addition.
  296. {
  297. DistanceVector distance_vector{loops.size()};
  298. EXPECT_TRUE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(48),
  299. store[1], &distance_vector));
  300. }
  301. // 56 -> 57 tests looking through symbols and arithmetic on load and store.
  302. {
  303. DistanceVector distance_vector{loops.size()};
  304. EXPECT_TRUE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(56),
  305. store[2], &distance_vector));
  306. }
  307. // independent as different arrays
  308. // 63 -> 64 tests looking through symbols and load/store from/to different
  309. // arrays.
  310. {
  311. DistanceVector distance_vector{loops.size()};
  312. EXPECT_TRUE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(63),
  313. store[3], &distance_vector));
  314. }
  315. }
  316. /*
  317. Generated from the following GLSL fragment shader
  318. with --eliminate-local-multi-store
  319. #version 440 core
  320. void a(){
  321. int[10] arr;
  322. int[11] arr2;
  323. int[20] arr3;
  324. int[20] arr4;
  325. int a = 2;
  326. for (int i = 0; i < 10; i++) {
  327. arr[i] = arr[i];
  328. arr2[i] = arr2[i+1];
  329. arr3[i] = arr3[i-1];
  330. arr4[2*i] = arr4[i];
  331. }
  332. }
  333. void b(){
  334. int[10] arr;
  335. int[11] arr2;
  336. int[20] arr3;
  337. int[20] arr4;
  338. int a = 2;
  339. for (int i = 10; i > 0; i--) {
  340. arr[i] = arr[i];
  341. arr2[i] = arr2[i+1];
  342. arr3[i] = arr3[i-1];
  343. arr4[2*i] = arr4[i];
  344. }
  345. }
  346. void main() {
  347. a();
  348. b();
  349. }
  350. */
  351. TEST(DependencyAnalysis, SIV) {
  352. const std::string text = R"( OpCapability Shader
  353. %1 = OpExtInstImport "GLSL.std.450"
  354. OpMemoryModel Logical GLSL450
  355. OpEntryPoint Fragment %4 "main"
  356. OpExecutionMode %4 OriginUpperLeft
  357. OpSource GLSL 440
  358. OpName %4 "main"
  359. OpName %6 "a("
  360. OpName %8 "b("
  361. OpName %12 "a"
  362. OpName %14 "i"
  363. OpName %29 "arr"
  364. OpName %38 "arr2"
  365. OpName %49 "arr3"
  366. OpName %56 "arr4"
  367. OpName %65 "a"
  368. OpName %66 "i"
  369. OpName %74 "arr"
  370. OpName %80 "arr2"
  371. OpName %87 "arr3"
  372. OpName %94 "arr4"
  373. %2 = OpTypeVoid
  374. %3 = OpTypeFunction %2
  375. %10 = OpTypeInt 32 1
  376. %11 = OpTypePointer Function %10
  377. %13 = OpConstant %10 2
  378. %15 = OpConstant %10 0
  379. %22 = OpConstant %10 10
  380. %23 = OpTypeBool
  381. %25 = OpTypeInt 32 0
  382. %26 = OpConstant %25 10
  383. %27 = OpTypeArray %10 %26
  384. %28 = OpTypePointer Function %27
  385. %35 = OpConstant %25 11
  386. %36 = OpTypeArray %10 %35
  387. %37 = OpTypePointer Function %36
  388. %41 = OpConstant %10 1
  389. %46 = OpConstant %25 20
  390. %47 = OpTypeArray %10 %46
  391. %48 = OpTypePointer Function %47
  392. %4 = OpFunction %2 None %3
  393. %5 = OpLabel
  394. %103 = OpFunctionCall %2 %6
  395. %104 = OpFunctionCall %2 %8
  396. OpReturn
  397. OpFunctionEnd
  398. %6 = OpFunction %2 None %3
  399. %7 = OpLabel
  400. %12 = OpVariable %11 Function
  401. %14 = OpVariable %11 Function
  402. %29 = OpVariable %28 Function
  403. %38 = OpVariable %37 Function
  404. %49 = OpVariable %48 Function
  405. %56 = OpVariable %48 Function
  406. OpStore %12 %13
  407. OpStore %14 %15
  408. OpBranch %16
  409. %16 = OpLabel
  410. %105 = OpPhi %10 %15 %7 %64 %19
  411. OpLoopMerge %18 %19 None
  412. OpBranch %20
  413. %20 = OpLabel
  414. %24 = OpSLessThan %23 %105 %22
  415. OpBranchConditional %24 %17 %18
  416. %17 = OpLabel
  417. %32 = OpAccessChain %11 %29 %105
  418. %33 = OpLoad %10 %32
  419. %34 = OpAccessChain %11 %29 %105
  420. OpStore %34 %33
  421. %42 = OpIAdd %10 %105 %41
  422. %43 = OpAccessChain %11 %38 %42
  423. %44 = OpLoad %10 %43
  424. %45 = OpAccessChain %11 %38 %105
  425. OpStore %45 %44
  426. %52 = OpISub %10 %105 %41
  427. %53 = OpAccessChain %11 %49 %52
  428. %54 = OpLoad %10 %53
  429. %55 = OpAccessChain %11 %49 %105
  430. OpStore %55 %54
  431. %58 = OpIMul %10 %13 %105
  432. %60 = OpAccessChain %11 %56 %105
  433. %61 = OpLoad %10 %60
  434. %62 = OpAccessChain %11 %56 %58
  435. OpStore %62 %61
  436. OpBranch %19
  437. %19 = OpLabel
  438. %64 = OpIAdd %10 %105 %41
  439. OpStore %14 %64
  440. OpBranch %16
  441. %18 = OpLabel
  442. OpReturn
  443. OpFunctionEnd
  444. %8 = OpFunction %2 None %3
  445. %9 = OpLabel
  446. %65 = OpVariable %11 Function
  447. %66 = OpVariable %11 Function
  448. %74 = OpVariable %28 Function
  449. %80 = OpVariable %37 Function
  450. %87 = OpVariable %48 Function
  451. %94 = OpVariable %48 Function
  452. OpStore %65 %13
  453. OpStore %66 %22
  454. OpBranch %67
  455. %67 = OpLabel
  456. %106 = OpPhi %10 %22 %9 %102 %70
  457. OpLoopMerge %69 %70 None
  458. OpBranch %71
  459. %71 = OpLabel
  460. %73 = OpSGreaterThan %23 %106 %15
  461. OpBranchConditional %73 %68 %69
  462. %68 = OpLabel
  463. %77 = OpAccessChain %11 %74 %106
  464. %78 = OpLoad %10 %77
  465. %79 = OpAccessChain %11 %74 %106
  466. OpStore %79 %78
  467. %83 = OpIAdd %10 %106 %41
  468. %84 = OpAccessChain %11 %80 %83
  469. %85 = OpLoad %10 %84
  470. %86 = OpAccessChain %11 %80 %106
  471. OpStore %86 %85
  472. %90 = OpISub %10 %106 %41
  473. %91 = OpAccessChain %11 %87 %90
  474. %92 = OpLoad %10 %91
  475. %93 = OpAccessChain %11 %87 %106
  476. OpStore %93 %92
  477. %96 = OpIMul %10 %13 %106
  478. %98 = OpAccessChain %11 %94 %106
  479. %99 = OpLoad %10 %98
  480. %100 = OpAccessChain %11 %94 %96
  481. OpStore %100 %99
  482. OpBranch %70
  483. %70 = OpLabel
  484. %102 = OpISub %10 %106 %41
  485. OpStore %66 %102
  486. OpBranch %67
  487. %69 = OpLabel
  488. OpReturn
  489. OpFunctionEnd
  490. )";
  491. std::unique_ptr<IRContext> context =
  492. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  493. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  494. Module* module = context->module();
  495. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  496. << text << std::endl;
  497. // For the loop in function a.
  498. {
  499. const Function* f = spvtest::GetFunction(module, 6);
  500. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  501. Loop* loop = &ld.GetLoopByIndex(0);
  502. std::vector<const Loop*> loops{loop};
  503. LoopDependenceAnalysis analysis{context.get(), loops};
  504. const Instruction* store[4];
  505. int stores_found = 0;
  506. for (const Instruction& inst : *spvtest::GetBasicBlock(f, 17)) {
  507. if (inst.opcode() == SpvOp::SpvOpStore) {
  508. store[stores_found] = &inst;
  509. ++stores_found;
  510. }
  511. }
  512. for (int i = 0; i < 4; ++i) {
  513. EXPECT_TRUE(store[i]);
  514. }
  515. // = dependence
  516. // 33 -> 34 tests looking at SIV in same array.
  517. {
  518. DistanceVector distance_vector{loops.size()};
  519. EXPECT_FALSE(analysis.GetDependence(
  520. context->get_def_use_mgr()->GetDef(33), store[0], &distance_vector));
  521. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  522. DistanceEntry::DependenceInformation::DISTANCE);
  523. EXPECT_EQ(distance_vector.GetEntries()[0].direction,
  524. DistanceEntry::Directions::EQ);
  525. EXPECT_EQ(distance_vector.GetEntries()[0].distance, 0);
  526. }
  527. // > -1 dependence
  528. // 44 -> 45 tests looking at SIV in same array with addition.
  529. {
  530. DistanceVector distance_vector{loops.size()};
  531. EXPECT_FALSE(analysis.GetDependence(
  532. context->get_def_use_mgr()->GetDef(44), store[1], &distance_vector));
  533. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  534. DistanceEntry::DependenceInformation::DISTANCE);
  535. EXPECT_EQ(distance_vector.GetEntries()[0].direction,
  536. DistanceEntry::Directions::GT);
  537. EXPECT_EQ(distance_vector.GetEntries()[0].distance, -1);
  538. }
  539. // < 1 dependence
  540. // 54 -> 55 tests looking at SIV in same array with subtraction.
  541. {
  542. DistanceVector distance_vector{loops.size()};
  543. EXPECT_FALSE(analysis.GetDependence(
  544. context->get_def_use_mgr()->GetDef(54), store[2], &distance_vector));
  545. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  546. DistanceEntry::DependenceInformation::DISTANCE);
  547. EXPECT_EQ(distance_vector.GetEntries()[0].direction,
  548. DistanceEntry::Directions::LT);
  549. EXPECT_EQ(distance_vector.GetEntries()[0].distance, 1);
  550. }
  551. // <=> dependence
  552. // 61 -> 62 tests looking at SIV in same array with multiplication.
  553. {
  554. DistanceVector distance_vector{loops.size()};
  555. EXPECT_FALSE(analysis.GetDependence(
  556. context->get_def_use_mgr()->GetDef(61), store[3], &distance_vector));
  557. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  558. DistanceEntry::DependenceInformation::UNKNOWN);
  559. EXPECT_EQ(distance_vector.GetEntries()[0].direction,
  560. DistanceEntry::Directions::ALL);
  561. }
  562. }
  563. // For the loop in function b.
  564. {
  565. const Function* f = spvtest::GetFunction(module, 8);
  566. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  567. Loop* loop = &ld.GetLoopByIndex(0);
  568. std::vector<const Loop*> loops{loop};
  569. LoopDependenceAnalysis analysis{context.get(), loops};
  570. const Instruction* store[4];
  571. int stores_found = 0;
  572. for (const Instruction& inst : *spvtest::GetBasicBlock(f, 68)) {
  573. if (inst.opcode() == SpvOp::SpvOpStore) {
  574. store[stores_found] = &inst;
  575. ++stores_found;
  576. }
  577. }
  578. for (int i = 0; i < 4; ++i) {
  579. EXPECT_TRUE(store[i]);
  580. }
  581. // = dependence
  582. // 78 -> 79 tests looking at SIV in same array.
  583. {
  584. DistanceVector distance_vector{loops.size()};
  585. EXPECT_FALSE(analysis.GetDependence(
  586. context->get_def_use_mgr()->GetDef(78), store[0], &distance_vector));
  587. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  588. DistanceEntry::DependenceInformation::DISTANCE);
  589. EXPECT_EQ(distance_vector.GetEntries()[0].direction,
  590. DistanceEntry::Directions::EQ);
  591. EXPECT_EQ(distance_vector.GetEntries()[0].distance, 0);
  592. }
  593. // < 1 dependence
  594. // 85 -> 86 tests looking at SIV in same array with addition.
  595. {
  596. DistanceVector distance_vector{loops.size()};
  597. EXPECT_FALSE(analysis.GetDependence(
  598. context->get_def_use_mgr()->GetDef(85), store[1], &distance_vector));
  599. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  600. DistanceEntry::DependenceInformation::DISTANCE);
  601. EXPECT_EQ(distance_vector.GetEntries()[0].direction,
  602. DistanceEntry::Directions::LT);
  603. EXPECT_EQ(distance_vector.GetEntries()[0].distance, 1);
  604. }
  605. // > -1 dependence
  606. // 92 -> 93 tests looking at SIV in same array with subtraction.
  607. {
  608. DistanceVector distance_vector{loops.size()};
  609. EXPECT_FALSE(analysis.GetDependence(
  610. context->get_def_use_mgr()->GetDef(92), store[2], &distance_vector));
  611. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  612. DistanceEntry::DependenceInformation::DISTANCE);
  613. EXPECT_EQ(distance_vector.GetEntries()[0].direction,
  614. DistanceEntry::Directions::GT);
  615. EXPECT_EQ(distance_vector.GetEntries()[0].distance, -1);
  616. }
  617. // <=> dependence
  618. // 99 -> 100 tests looking at SIV in same array with multiplication.
  619. {
  620. DistanceVector distance_vector{loops.size()};
  621. EXPECT_FALSE(analysis.GetDependence(
  622. context->get_def_use_mgr()->GetDef(99), store[3], &distance_vector));
  623. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  624. DistanceEntry::DependenceInformation::UNKNOWN);
  625. EXPECT_EQ(distance_vector.GetEntries()[0].direction,
  626. DistanceEntry::Directions::ALL);
  627. }
  628. }
  629. }
  630. /*
  631. Generated from the following GLSL fragment shader
  632. with --eliminate-local-multi-store
  633. #version 440 core
  634. layout(location = 0) in vec4 c;
  635. void a() {
  636. int[13] arr;
  637. int[15] arr2;
  638. int[18] arr3;
  639. int[18] arr4;
  640. int N = int(c.x);
  641. int C = 2;
  642. int a = 2;
  643. for (int i = 0; i < N; i++) { // Bounds are N - 1
  644. arr[i+2*N] = arr[i+N]; // |distance| = N
  645. arr2[i+N] = arr2[i+2*N] + C; // |distance| = N
  646. arr3[2*i+2*N+1] = arr3[2*i+N+1]; // |distance| = N
  647. arr4[a*i+N+1] = arr4[a*i+2*N+1]; // |distance| = N
  648. }
  649. }
  650. void b() {
  651. int[13] arr;
  652. int[15] arr2;
  653. int[18] arr3;
  654. int[18] arr4;
  655. int N = int(c.x);
  656. int C = 2;
  657. int a = 2;
  658. for (int i = N; i > 0; i--) { // Bounds are N - 1
  659. arr[i+2*N] = arr[i+N]; // |distance| = N
  660. arr2[i+N] = arr2[i+2*N] + C; // |distance| = N
  661. arr3[2*i+2*N+1] = arr3[2*i+N+1]; // |distance| = N
  662. arr4[a*i+N+1] = arr4[a*i+2*N+1]; // |distance| = N
  663. }
  664. }
  665. void main(){
  666. a();
  667. b();
  668. }*/
  669. TEST(DependencyAnalysis, SymbolicSIV) {
  670. const std::string text = R"( OpCapability Shader
  671. %1 = OpExtInstImport "GLSL.std.450"
  672. OpMemoryModel Logical GLSL450
  673. OpEntryPoint Fragment %4 "main" %16
  674. OpExecutionMode %4 OriginUpperLeft
  675. OpSource GLSL 440
  676. OpName %4 "main"
  677. OpName %6 "a("
  678. OpName %8 "b("
  679. OpName %12 "N"
  680. OpName %16 "c"
  681. OpName %23 "C"
  682. OpName %25 "a"
  683. OpName %26 "i"
  684. OpName %40 "arr"
  685. OpName %54 "arr2"
  686. OpName %70 "arr3"
  687. OpName %86 "arr4"
  688. OpName %105 "N"
  689. OpName %109 "C"
  690. OpName %110 "a"
  691. OpName %111 "i"
  692. OpName %120 "arr"
  693. OpName %131 "arr2"
  694. OpName %144 "arr3"
  695. OpName %159 "arr4"
  696. OpDecorate %16 Location 0
  697. %2 = OpTypeVoid
  698. %3 = OpTypeFunction %2
  699. %10 = OpTypeInt 32 1
  700. %11 = OpTypePointer Function %10
  701. %13 = OpTypeFloat 32
  702. %14 = OpTypeVector %13 4
  703. %15 = OpTypePointer Input %14
  704. %16 = OpVariable %15 Input
  705. %17 = OpTypeInt 32 0
  706. %18 = OpConstant %17 0
  707. %19 = OpTypePointer Input %13
  708. %24 = OpConstant %10 2
  709. %27 = OpConstant %10 0
  710. %35 = OpTypeBool
  711. %37 = OpConstant %17 13
  712. %38 = OpTypeArray %10 %37
  713. %39 = OpTypePointer Function %38
  714. %51 = OpConstant %17 15
  715. %52 = OpTypeArray %10 %51
  716. %53 = OpTypePointer Function %52
  717. %67 = OpConstant %17 18
  718. %68 = OpTypeArray %10 %67
  719. %69 = OpTypePointer Function %68
  720. %76 = OpConstant %10 1
  721. %4 = OpFunction %2 None %3
  722. %5 = OpLabel
  723. %178 = OpFunctionCall %2 %6
  724. %179 = OpFunctionCall %2 %8
  725. OpReturn
  726. OpFunctionEnd
  727. %6 = OpFunction %2 None %3
  728. %7 = OpLabel
  729. %12 = OpVariable %11 Function
  730. %23 = OpVariable %11 Function
  731. %25 = OpVariable %11 Function
  732. %26 = OpVariable %11 Function
  733. %40 = OpVariable %39 Function
  734. %54 = OpVariable %53 Function
  735. %70 = OpVariable %69 Function
  736. %86 = OpVariable %69 Function
  737. %20 = OpAccessChain %19 %16 %18
  738. %21 = OpLoad %13 %20
  739. %22 = OpConvertFToS %10 %21
  740. OpStore %12 %22
  741. OpStore %23 %24
  742. OpStore %25 %24
  743. OpStore %26 %27
  744. OpBranch %28
  745. %28 = OpLabel
  746. %180 = OpPhi %10 %27 %7 %104 %31
  747. OpLoopMerge %30 %31 None
  748. OpBranch %32
  749. %32 = OpLabel
  750. %36 = OpSLessThan %35 %180 %22
  751. OpBranchConditional %36 %29 %30
  752. %29 = OpLabel
  753. %43 = OpIMul %10 %24 %22
  754. %44 = OpIAdd %10 %180 %43
  755. %47 = OpIAdd %10 %180 %22
  756. %48 = OpAccessChain %11 %40 %47
  757. %49 = OpLoad %10 %48
  758. %50 = OpAccessChain %11 %40 %44
  759. OpStore %50 %49
  760. %57 = OpIAdd %10 %180 %22
  761. %60 = OpIMul %10 %24 %22
  762. %61 = OpIAdd %10 %180 %60
  763. %62 = OpAccessChain %11 %54 %61
  764. %63 = OpLoad %10 %62
  765. %65 = OpIAdd %10 %63 %24
  766. %66 = OpAccessChain %11 %54 %57
  767. OpStore %66 %65
  768. %72 = OpIMul %10 %24 %180
  769. %74 = OpIMul %10 %24 %22
  770. %75 = OpIAdd %10 %72 %74
  771. %77 = OpIAdd %10 %75 %76
  772. %79 = OpIMul %10 %24 %180
  773. %81 = OpIAdd %10 %79 %22
  774. %82 = OpIAdd %10 %81 %76
  775. %83 = OpAccessChain %11 %70 %82
  776. %84 = OpLoad %10 %83
  777. %85 = OpAccessChain %11 %70 %77
  778. OpStore %85 %84
  779. %89 = OpIMul %10 %24 %180
  780. %91 = OpIAdd %10 %89 %22
  781. %92 = OpIAdd %10 %91 %76
  782. %95 = OpIMul %10 %24 %180
  783. %97 = OpIMul %10 %24 %22
  784. %98 = OpIAdd %10 %95 %97
  785. %99 = OpIAdd %10 %98 %76
  786. %100 = OpAccessChain %11 %86 %99
  787. %101 = OpLoad %10 %100
  788. %102 = OpAccessChain %11 %86 %92
  789. OpStore %102 %101
  790. OpBranch %31
  791. %31 = OpLabel
  792. %104 = OpIAdd %10 %180 %76
  793. OpStore %26 %104
  794. OpBranch %28
  795. %30 = OpLabel
  796. OpReturn
  797. OpFunctionEnd
  798. %8 = OpFunction %2 None %3
  799. %9 = OpLabel
  800. %105 = OpVariable %11 Function
  801. %109 = OpVariable %11 Function
  802. %110 = OpVariable %11 Function
  803. %111 = OpVariable %11 Function
  804. %120 = OpVariable %39 Function
  805. %131 = OpVariable %53 Function
  806. %144 = OpVariable %69 Function
  807. %159 = OpVariable %69 Function
  808. %106 = OpAccessChain %19 %16 %18
  809. %107 = OpLoad %13 %106
  810. %108 = OpConvertFToS %10 %107
  811. OpStore %105 %108
  812. OpStore %109 %24
  813. OpStore %110 %24
  814. OpStore %111 %108
  815. OpBranch %113
  816. %113 = OpLabel
  817. %181 = OpPhi %10 %108 %9 %177 %116
  818. OpLoopMerge %115 %116 None
  819. OpBranch %117
  820. %117 = OpLabel
  821. %119 = OpSGreaterThan %35 %181 %27
  822. OpBranchConditional %119 %114 %115
  823. %114 = OpLabel
  824. %123 = OpIMul %10 %24 %108
  825. %124 = OpIAdd %10 %181 %123
  826. %127 = OpIAdd %10 %181 %108
  827. %128 = OpAccessChain %11 %120 %127
  828. %129 = OpLoad %10 %128
  829. %130 = OpAccessChain %11 %120 %124
  830. OpStore %130 %129
  831. %134 = OpIAdd %10 %181 %108
  832. %137 = OpIMul %10 %24 %108
  833. %138 = OpIAdd %10 %181 %137
  834. %139 = OpAccessChain %11 %131 %138
  835. %140 = OpLoad %10 %139
  836. %142 = OpIAdd %10 %140 %24
  837. %143 = OpAccessChain %11 %131 %134
  838. OpStore %143 %142
  839. %146 = OpIMul %10 %24 %181
  840. %148 = OpIMul %10 %24 %108
  841. %149 = OpIAdd %10 %146 %148
  842. %150 = OpIAdd %10 %149 %76
  843. %152 = OpIMul %10 %24 %181
  844. %154 = OpIAdd %10 %152 %108
  845. %155 = OpIAdd %10 %154 %76
  846. %156 = OpAccessChain %11 %144 %155
  847. %157 = OpLoad %10 %156
  848. %158 = OpAccessChain %11 %144 %150
  849. OpStore %158 %157
  850. %162 = OpIMul %10 %24 %181
  851. %164 = OpIAdd %10 %162 %108
  852. %165 = OpIAdd %10 %164 %76
  853. %168 = OpIMul %10 %24 %181
  854. %170 = OpIMul %10 %24 %108
  855. %171 = OpIAdd %10 %168 %170
  856. %172 = OpIAdd %10 %171 %76
  857. %173 = OpAccessChain %11 %159 %172
  858. %174 = OpLoad %10 %173
  859. %175 = OpAccessChain %11 %159 %165
  860. OpStore %175 %174
  861. OpBranch %116
  862. %116 = OpLabel
  863. %177 = OpISub %10 %181 %76
  864. OpStore %111 %177
  865. OpBranch %113
  866. %115 = OpLabel
  867. OpReturn
  868. OpFunctionEnd
  869. )";
  870. std::unique_ptr<IRContext> context =
  871. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  872. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  873. Module* module = context->module();
  874. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  875. << text << std::endl;
  876. // For the loop in function a.
  877. {
  878. const Function* f = spvtest::GetFunction(module, 6);
  879. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  880. Loop* loop = &ld.GetLoopByIndex(0);
  881. std::vector<const Loop*> loops{loop};
  882. LoopDependenceAnalysis analysis{context.get(), loops};
  883. const Instruction* store[4];
  884. int stores_found = 0;
  885. for (const Instruction& inst : *spvtest::GetBasicBlock(f, 29)) {
  886. if (inst.opcode() == SpvOp::SpvOpStore) {
  887. store[stores_found] = &inst;
  888. ++stores_found;
  889. }
  890. }
  891. for (int i = 0; i < 4; ++i) {
  892. EXPECT_TRUE(store[i]);
  893. }
  894. // independent due to loop bounds (won't enter when N <= 0)
  895. // 49 -> 50 tests looking through SIV and symbols with multiplication
  896. {
  897. DistanceVector distance_vector{loops.size()};
  898. // Independent but not yet supported.
  899. EXPECT_FALSE(analysis.GetDependence(
  900. context->get_def_use_mgr()->GetDef(49), store[0], &distance_vector));
  901. }
  902. // 63 -> 66 tests looking through SIV and symbols with multiplication and +
  903. // C
  904. {
  905. DistanceVector distance_vector{loops.size()};
  906. // Independent.
  907. EXPECT_TRUE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(63),
  908. store[1], &distance_vector));
  909. }
  910. // 84 -> 85 tests looking through arithmetic on SIV and symbols
  911. {
  912. DistanceVector distance_vector{loops.size()};
  913. // Independent but not yet supported.
  914. EXPECT_FALSE(analysis.GetDependence(
  915. context->get_def_use_mgr()->GetDef(84), store[2], &distance_vector));
  916. }
  917. // 101 -> 102 tests looking through symbol arithmetic on SIV and symbols
  918. {
  919. DistanceVector distance_vector{loops.size()};
  920. // Independent.
  921. EXPECT_TRUE(analysis.GetDependence(
  922. context->get_def_use_mgr()->GetDef(101), store[3], &distance_vector));
  923. }
  924. }
  925. // For the loop in function b.
  926. {
  927. const Function* f = spvtest::GetFunction(module, 8);
  928. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  929. Loop* loop = &ld.GetLoopByIndex(0);
  930. std::vector<const Loop*> loops{loop};
  931. LoopDependenceAnalysis analysis{context.get(), loops};
  932. const Instruction* store[4];
  933. int stores_found = 0;
  934. for (const Instruction& inst : *spvtest::GetBasicBlock(f, 114)) {
  935. if (inst.opcode() == SpvOp::SpvOpStore) {
  936. store[stores_found] = &inst;
  937. ++stores_found;
  938. }
  939. }
  940. for (int i = 0; i < 4; ++i) {
  941. EXPECT_TRUE(store[i]);
  942. }
  943. // independent due to loop bounds (won't enter when N <= 0).
  944. // 129 -> 130 tests looking through SIV and symbols with multiplication.
  945. {
  946. DistanceVector distance_vector{loops.size()};
  947. // Independent but not yet supported.
  948. EXPECT_FALSE(analysis.GetDependence(
  949. context->get_def_use_mgr()->GetDef(129), store[0], &distance_vector));
  950. }
  951. // 140 -> 143 tests looking through SIV and symbols with multiplication and
  952. // + C.
  953. {
  954. DistanceVector distance_vector{loops.size()};
  955. // Independent.
  956. EXPECT_TRUE(analysis.GetDependence(
  957. context->get_def_use_mgr()->GetDef(140), store[1], &distance_vector));
  958. }
  959. // 157 -> 158 tests looking through arithmetic on SIV and symbols.
  960. {
  961. DistanceVector distance_vector{loops.size()};
  962. // Independent but not yet supported.
  963. EXPECT_FALSE(analysis.GetDependence(
  964. context->get_def_use_mgr()->GetDef(157), store[2], &distance_vector));
  965. }
  966. // 174 -> 175 tests looking through symbol arithmetic on SIV and symbols.
  967. {
  968. DistanceVector distance_vector{loops.size()};
  969. // Independent.
  970. EXPECT_TRUE(analysis.GetDependence(
  971. context->get_def_use_mgr()->GetDef(174), store[3], &distance_vector));
  972. }
  973. }
  974. }
  975. /*
  976. Generated from the following GLSL fragment shader
  977. with --eliminate-local-multi-store
  978. #version 440 core
  979. void a() {
  980. int[6] arr;
  981. int N = 5;
  982. for (int i = 1; i < N; i++) {
  983. arr[i] = arr[N-i];
  984. }
  985. }
  986. void b() {
  987. int[6] arr;
  988. int N = 5;
  989. for (int i = 1; i < N; i++) {
  990. arr[N-i] = arr[i];
  991. }
  992. }
  993. void c() {
  994. int[11] arr;
  995. int N = 10;
  996. for (int i = 1; i < N; i++) {
  997. arr[i] = arr[N-i+1];
  998. }
  999. }
  1000. void d() {
  1001. int[11] arr;
  1002. int N = 10;
  1003. for (int i = 1; i < N; i++) {
  1004. arr[N-i+1] = arr[i];
  1005. }
  1006. }
  1007. void e() {
  1008. int[6] arr;
  1009. int N = 5;
  1010. for (int i = N; i > 0; i--) {
  1011. arr[i] = arr[N-i];
  1012. }
  1013. }
  1014. void f() {
  1015. int[6] arr;
  1016. int N = 5;
  1017. for (int i = N; i > 0; i--) {
  1018. arr[N-i] = arr[i];
  1019. }
  1020. }
  1021. void g() {
  1022. int[11] arr;
  1023. int N = 10;
  1024. for (int i = N; i > 0; i--) {
  1025. arr[i] = arr[N-i+1];
  1026. }
  1027. }
  1028. void h() {
  1029. int[11] arr;
  1030. int N = 10;
  1031. for (int i = N; i > 0; i--) {
  1032. arr[N-i+1] = arr[i];
  1033. }
  1034. }
  1035. void main(){
  1036. a();
  1037. b();
  1038. c();
  1039. d();
  1040. e();
  1041. f();
  1042. g();
  1043. h();
  1044. }
  1045. */
  1046. TEST(DependencyAnalysis, Crossing) {
  1047. const std::string text = R"( OpCapability Shader
  1048. %1 = OpExtInstImport "GLSL.std.450"
  1049. OpMemoryModel Logical GLSL450
  1050. OpEntryPoint Fragment %4 "main"
  1051. OpExecutionMode %4 OriginUpperLeft
  1052. OpSource GLSL 440
  1053. OpName %4 "main"
  1054. OpName %6 "a("
  1055. OpName %8 "b("
  1056. OpName %10 "c("
  1057. OpName %12 "d("
  1058. OpName %14 "e("
  1059. OpName %16 "f("
  1060. OpName %18 "g("
  1061. OpName %20 "h("
  1062. OpName %24 "N"
  1063. OpName %26 "i"
  1064. OpName %41 "arr"
  1065. OpName %51 "N"
  1066. OpName %52 "i"
  1067. OpName %61 "arr"
  1068. OpName %71 "N"
  1069. OpName %73 "i"
  1070. OpName %85 "arr"
  1071. OpName %96 "N"
  1072. OpName %97 "i"
  1073. OpName %106 "arr"
  1074. OpName %117 "N"
  1075. OpName %118 "i"
  1076. OpName %128 "arr"
  1077. OpName %138 "N"
  1078. OpName %139 "i"
  1079. OpName %148 "arr"
  1080. OpName %158 "N"
  1081. OpName %159 "i"
  1082. OpName %168 "arr"
  1083. OpName %179 "N"
  1084. OpName %180 "i"
  1085. OpName %189 "arr"
  1086. %2 = OpTypeVoid
  1087. %3 = OpTypeFunction %2
  1088. %22 = OpTypeInt 32 1
  1089. %23 = OpTypePointer Function %22
  1090. %25 = OpConstant %22 5
  1091. %27 = OpConstant %22 1
  1092. %35 = OpTypeBool
  1093. %37 = OpTypeInt 32 0
  1094. %38 = OpConstant %37 6
  1095. %39 = OpTypeArray %22 %38
  1096. %40 = OpTypePointer Function %39
  1097. %72 = OpConstant %22 10
  1098. %82 = OpConstant %37 11
  1099. %83 = OpTypeArray %22 %82
  1100. %84 = OpTypePointer Function %83
  1101. %126 = OpConstant %22 0
  1102. %4 = OpFunction %2 None %3
  1103. %5 = OpLabel
  1104. %200 = OpFunctionCall %2 %6
  1105. %201 = OpFunctionCall %2 %8
  1106. %202 = OpFunctionCall %2 %10
  1107. %203 = OpFunctionCall %2 %12
  1108. %204 = OpFunctionCall %2 %14
  1109. %205 = OpFunctionCall %2 %16
  1110. %206 = OpFunctionCall %2 %18
  1111. %207 = OpFunctionCall %2 %20
  1112. OpReturn
  1113. OpFunctionEnd
  1114. %6 = OpFunction %2 None %3
  1115. %7 = OpLabel
  1116. %24 = OpVariable %23 Function
  1117. %26 = OpVariable %23 Function
  1118. %41 = OpVariable %40 Function
  1119. OpStore %24 %25
  1120. OpStore %26 %27
  1121. OpBranch %28
  1122. %28 = OpLabel
  1123. %208 = OpPhi %22 %27 %7 %50 %31
  1124. OpLoopMerge %30 %31 None
  1125. OpBranch %32
  1126. %32 = OpLabel
  1127. %36 = OpSLessThan %35 %208 %25
  1128. OpBranchConditional %36 %29 %30
  1129. %29 = OpLabel
  1130. %45 = OpISub %22 %25 %208
  1131. %46 = OpAccessChain %23 %41 %45
  1132. %47 = OpLoad %22 %46
  1133. %48 = OpAccessChain %23 %41 %208
  1134. OpStore %48 %47
  1135. OpBranch %31
  1136. %31 = OpLabel
  1137. %50 = OpIAdd %22 %208 %27
  1138. OpStore %26 %50
  1139. OpBranch %28
  1140. %30 = OpLabel
  1141. OpReturn
  1142. OpFunctionEnd
  1143. %8 = OpFunction %2 None %3
  1144. %9 = OpLabel
  1145. %51 = OpVariable %23 Function
  1146. %52 = OpVariable %23 Function
  1147. %61 = OpVariable %40 Function
  1148. OpStore %51 %25
  1149. OpStore %52 %27
  1150. OpBranch %53
  1151. %53 = OpLabel
  1152. %209 = OpPhi %22 %27 %9 %70 %56
  1153. OpLoopMerge %55 %56 None
  1154. OpBranch %57
  1155. %57 = OpLabel
  1156. %60 = OpSLessThan %35 %209 %25
  1157. OpBranchConditional %60 %54 %55
  1158. %54 = OpLabel
  1159. %64 = OpISub %22 %25 %209
  1160. %66 = OpAccessChain %23 %61 %209
  1161. %67 = OpLoad %22 %66
  1162. %68 = OpAccessChain %23 %61 %64
  1163. OpStore %68 %67
  1164. OpBranch %56
  1165. %56 = OpLabel
  1166. %70 = OpIAdd %22 %209 %27
  1167. OpStore %52 %70
  1168. OpBranch %53
  1169. %55 = OpLabel
  1170. OpReturn
  1171. OpFunctionEnd
  1172. %10 = OpFunction %2 None %3
  1173. %11 = OpLabel
  1174. %71 = OpVariable %23 Function
  1175. %73 = OpVariable %23 Function
  1176. %85 = OpVariable %84 Function
  1177. OpStore %71 %72
  1178. OpStore %73 %27
  1179. OpBranch %74
  1180. %74 = OpLabel
  1181. %210 = OpPhi %22 %27 %11 %95 %77
  1182. OpLoopMerge %76 %77 None
  1183. OpBranch %78
  1184. %78 = OpLabel
  1185. %81 = OpSLessThan %35 %210 %72
  1186. OpBranchConditional %81 %75 %76
  1187. %75 = OpLabel
  1188. %89 = OpISub %22 %72 %210
  1189. %90 = OpIAdd %22 %89 %27
  1190. %91 = OpAccessChain %23 %85 %90
  1191. %92 = OpLoad %22 %91
  1192. %93 = OpAccessChain %23 %85 %210
  1193. OpStore %93 %92
  1194. OpBranch %77
  1195. %77 = OpLabel
  1196. %95 = OpIAdd %22 %210 %27
  1197. OpStore %73 %95
  1198. OpBranch %74
  1199. %76 = OpLabel
  1200. OpReturn
  1201. OpFunctionEnd
  1202. %12 = OpFunction %2 None %3
  1203. %13 = OpLabel
  1204. %96 = OpVariable %23 Function
  1205. %97 = OpVariable %23 Function
  1206. %106 = OpVariable %84 Function
  1207. OpStore %96 %72
  1208. OpStore %97 %27
  1209. OpBranch %98
  1210. %98 = OpLabel
  1211. %211 = OpPhi %22 %27 %13 %116 %101
  1212. OpLoopMerge %100 %101 None
  1213. OpBranch %102
  1214. %102 = OpLabel
  1215. %105 = OpSLessThan %35 %211 %72
  1216. OpBranchConditional %105 %99 %100
  1217. %99 = OpLabel
  1218. %109 = OpISub %22 %72 %211
  1219. %110 = OpIAdd %22 %109 %27
  1220. %112 = OpAccessChain %23 %106 %211
  1221. %113 = OpLoad %22 %112
  1222. %114 = OpAccessChain %23 %106 %110
  1223. OpStore %114 %113
  1224. OpBranch %101
  1225. %101 = OpLabel
  1226. %116 = OpIAdd %22 %211 %27
  1227. OpStore %97 %116
  1228. OpBranch %98
  1229. %100 = OpLabel
  1230. OpReturn
  1231. OpFunctionEnd
  1232. %14 = OpFunction %2 None %3
  1233. %15 = OpLabel
  1234. %117 = OpVariable %23 Function
  1235. %118 = OpVariable %23 Function
  1236. %128 = OpVariable %40 Function
  1237. OpStore %117 %25
  1238. OpStore %118 %25
  1239. OpBranch %120
  1240. %120 = OpLabel
  1241. %212 = OpPhi %22 %25 %15 %137 %123
  1242. OpLoopMerge %122 %123 None
  1243. OpBranch %124
  1244. %124 = OpLabel
  1245. %127 = OpSGreaterThan %35 %212 %126
  1246. OpBranchConditional %127 %121 %122
  1247. %121 = OpLabel
  1248. %132 = OpISub %22 %25 %212
  1249. %133 = OpAccessChain %23 %128 %132
  1250. %134 = OpLoad %22 %133
  1251. %135 = OpAccessChain %23 %128 %212
  1252. OpStore %135 %134
  1253. OpBranch %123
  1254. %123 = OpLabel
  1255. %137 = OpISub %22 %212 %27
  1256. OpStore %118 %137
  1257. OpBranch %120
  1258. %122 = OpLabel
  1259. OpReturn
  1260. OpFunctionEnd
  1261. %16 = OpFunction %2 None %3
  1262. %17 = OpLabel
  1263. %138 = OpVariable %23 Function
  1264. %139 = OpVariable %23 Function
  1265. %148 = OpVariable %40 Function
  1266. OpStore %138 %25
  1267. OpStore %139 %25
  1268. OpBranch %141
  1269. %141 = OpLabel
  1270. %213 = OpPhi %22 %25 %17 %157 %144
  1271. OpLoopMerge %143 %144 None
  1272. OpBranch %145
  1273. %145 = OpLabel
  1274. %147 = OpSGreaterThan %35 %213 %126
  1275. OpBranchConditional %147 %142 %143
  1276. %142 = OpLabel
  1277. %151 = OpISub %22 %25 %213
  1278. %153 = OpAccessChain %23 %148 %213
  1279. %154 = OpLoad %22 %153
  1280. %155 = OpAccessChain %23 %148 %151
  1281. OpStore %155 %154
  1282. OpBranch %144
  1283. %144 = OpLabel
  1284. %157 = OpISub %22 %213 %27
  1285. OpStore %139 %157
  1286. OpBranch %141
  1287. %143 = OpLabel
  1288. OpReturn
  1289. OpFunctionEnd
  1290. %18 = OpFunction %2 None %3
  1291. %19 = OpLabel
  1292. %158 = OpVariable %23 Function
  1293. %159 = OpVariable %23 Function
  1294. %168 = OpVariable %84 Function
  1295. OpStore %158 %72
  1296. OpStore %159 %72
  1297. OpBranch %161
  1298. %161 = OpLabel
  1299. %214 = OpPhi %22 %72 %19 %178 %164
  1300. OpLoopMerge %163 %164 None
  1301. OpBranch %165
  1302. %165 = OpLabel
  1303. %167 = OpSGreaterThan %35 %214 %126
  1304. OpBranchConditional %167 %162 %163
  1305. %162 = OpLabel
  1306. %172 = OpISub %22 %72 %214
  1307. %173 = OpIAdd %22 %172 %27
  1308. %174 = OpAccessChain %23 %168 %173
  1309. %175 = OpLoad %22 %174
  1310. %176 = OpAccessChain %23 %168 %214
  1311. OpStore %176 %175
  1312. OpBranch %164
  1313. %164 = OpLabel
  1314. %178 = OpISub %22 %214 %27
  1315. OpStore %159 %178
  1316. OpBranch %161
  1317. %163 = OpLabel
  1318. OpReturn
  1319. OpFunctionEnd
  1320. %20 = OpFunction %2 None %3
  1321. %21 = OpLabel
  1322. %179 = OpVariable %23 Function
  1323. %180 = OpVariable %23 Function
  1324. %189 = OpVariable %84 Function
  1325. OpStore %179 %72
  1326. OpStore %180 %72
  1327. OpBranch %182
  1328. %182 = OpLabel
  1329. %215 = OpPhi %22 %72 %21 %199 %185
  1330. OpLoopMerge %184 %185 None
  1331. OpBranch %186
  1332. %186 = OpLabel
  1333. %188 = OpSGreaterThan %35 %215 %126
  1334. OpBranchConditional %188 %183 %184
  1335. %183 = OpLabel
  1336. %192 = OpISub %22 %72 %215
  1337. %193 = OpIAdd %22 %192 %27
  1338. %195 = OpAccessChain %23 %189 %215
  1339. %196 = OpLoad %22 %195
  1340. %197 = OpAccessChain %23 %189 %193
  1341. OpStore %197 %196
  1342. OpBranch %185
  1343. %185 = OpLabel
  1344. %199 = OpISub %22 %215 %27
  1345. OpStore %180 %199
  1346. OpBranch %182
  1347. %184 = OpLabel
  1348. OpReturn
  1349. OpFunctionEnd
  1350. )";
  1351. std::unique_ptr<IRContext> context =
  1352. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  1353. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1354. Module* module = context->module();
  1355. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  1356. << text << std::endl;
  1357. // First two tests can be split into two loops.
  1358. // Tests even crossing subscripts from low to high indexes.
  1359. // 47 -> 48
  1360. {
  1361. const Function* f = spvtest::GetFunction(module, 6);
  1362. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  1363. Loop* loop = &ld.GetLoopByIndex(0);
  1364. std::vector<const Loop*> loops{loop};
  1365. LoopDependenceAnalysis analysis{context.get(), loops};
  1366. const Instruction* store = nullptr;
  1367. for (const Instruction& inst : *spvtest::GetBasicBlock(f, 29)) {
  1368. if (inst.opcode() == SpvOp::SpvOpStore) {
  1369. store = &inst;
  1370. }
  1371. }
  1372. DistanceVector distance_vector{loops.size()};
  1373. EXPECT_FALSE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(47),
  1374. store, &distance_vector));
  1375. }
  1376. // Tests even crossing subscripts from high to low indexes.
  1377. // 67 -> 68
  1378. {
  1379. const Function* f = spvtest::GetFunction(module, 8);
  1380. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  1381. Loop* loop = &ld.GetLoopByIndex(0);
  1382. std::vector<const Loop*> loops{loop};
  1383. LoopDependenceAnalysis analysis{context.get(), loops};
  1384. const Instruction* store = nullptr;
  1385. for (const Instruction& inst : *spvtest::GetBasicBlock(f, 54)) {
  1386. if (inst.opcode() == SpvOp::SpvOpStore) {
  1387. store = &inst;
  1388. }
  1389. }
  1390. DistanceVector distance_vector{loops.size()};
  1391. EXPECT_FALSE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(67),
  1392. store, &distance_vector));
  1393. }
  1394. // Next two tests can have an end peeled, then be split.
  1395. // Tests uneven crossing subscripts from low to high indexes.
  1396. // 92 -> 93
  1397. {
  1398. const Function* f = spvtest::GetFunction(module, 10);
  1399. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  1400. Loop* loop = &ld.GetLoopByIndex(0);
  1401. std::vector<const Loop*> loops{loop};
  1402. LoopDependenceAnalysis analysis{context.get(), loops};
  1403. const Instruction* store = nullptr;
  1404. for (const Instruction& inst : *spvtest::GetBasicBlock(f, 75)) {
  1405. if (inst.opcode() == SpvOp::SpvOpStore) {
  1406. store = &inst;
  1407. }
  1408. }
  1409. DistanceVector distance_vector{loops.size()};
  1410. EXPECT_FALSE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(92),
  1411. store, &distance_vector));
  1412. }
  1413. // Tests uneven crossing subscripts from high to low indexes.
  1414. // 113 -> 114
  1415. {
  1416. const Function* f = spvtest::GetFunction(module, 12);
  1417. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  1418. Loop* loop = &ld.GetLoopByIndex(0);
  1419. std::vector<const Loop*> loops{loop};
  1420. LoopDependenceAnalysis analysis{context.get(), loops};
  1421. const Instruction* store = nullptr;
  1422. for (const Instruction& inst : *spvtest::GetBasicBlock(f, 99)) {
  1423. if (inst.opcode() == SpvOp::SpvOpStore) {
  1424. store = &inst;
  1425. }
  1426. }
  1427. DistanceVector distance_vector{loops.size()};
  1428. EXPECT_FALSE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(113),
  1429. store, &distance_vector));
  1430. }
  1431. // First two tests can be split into two loops.
  1432. // Tests even crossing subscripts from low to high indexes.
  1433. // 134 -> 135
  1434. {
  1435. const Function* f = spvtest::GetFunction(module, 14);
  1436. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  1437. Loop* loop = &ld.GetLoopByIndex(0);
  1438. std::vector<const Loop*> loops{loop};
  1439. LoopDependenceAnalysis analysis{context.get(), loops};
  1440. const Instruction* store = nullptr;
  1441. for (const Instruction& inst : *spvtest::GetBasicBlock(f, 121)) {
  1442. if (inst.opcode() == SpvOp::SpvOpStore) {
  1443. store = &inst;
  1444. }
  1445. }
  1446. DistanceVector distance_vector{loops.size()};
  1447. EXPECT_FALSE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(134),
  1448. store, &distance_vector));
  1449. }
  1450. // Tests even crossing subscripts from high to low indexes.
  1451. // 154 -> 155
  1452. {
  1453. const Function* f = spvtest::GetFunction(module, 16);
  1454. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  1455. Loop* loop = &ld.GetLoopByIndex(0);
  1456. std::vector<const Loop*> loops{loop};
  1457. LoopDependenceAnalysis analysis{context.get(), loops};
  1458. const Instruction* store = nullptr;
  1459. for (const Instruction& inst : *spvtest::GetBasicBlock(f, 142)) {
  1460. if (inst.opcode() == SpvOp::SpvOpStore) {
  1461. store = &inst;
  1462. }
  1463. }
  1464. DistanceVector distance_vector{loops.size()};
  1465. EXPECT_FALSE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(154),
  1466. store, &distance_vector));
  1467. }
  1468. // Next two tests can have an end peeled, then be split.
  1469. // Tests uneven crossing subscripts from low to high indexes.
  1470. // 175 -> 176
  1471. {
  1472. const Function* f = spvtest::GetFunction(module, 18);
  1473. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  1474. Loop* loop = &ld.GetLoopByIndex(0);
  1475. std::vector<const Loop*> loops{loop};
  1476. LoopDependenceAnalysis analysis{context.get(), loops};
  1477. const Instruction* store = nullptr;
  1478. for (const Instruction& inst : *spvtest::GetBasicBlock(f, 162)) {
  1479. if (inst.opcode() == SpvOp::SpvOpStore) {
  1480. store = &inst;
  1481. }
  1482. }
  1483. DistanceVector distance_vector{loops.size()};
  1484. EXPECT_FALSE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(175),
  1485. store, &distance_vector));
  1486. }
  1487. // Tests uneven crossing subscripts from high to low indexes.
  1488. // 196 -> 197
  1489. {
  1490. const Function* f = spvtest::GetFunction(module, 20);
  1491. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  1492. Loop* loop = &ld.GetLoopByIndex(0);
  1493. std::vector<const Loop*> loops{loop};
  1494. LoopDependenceAnalysis analysis{context.get(), loops};
  1495. const Instruction* store = nullptr;
  1496. for (const Instruction& inst : *spvtest::GetBasicBlock(f, 183)) {
  1497. if (inst.opcode() == SpvOp::SpvOpStore) {
  1498. store = &inst;
  1499. }
  1500. }
  1501. DistanceVector distance_vector{loops.size()};
  1502. EXPECT_FALSE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(196),
  1503. store, &distance_vector));
  1504. }
  1505. }
  1506. /*
  1507. Generated from the following GLSL fragment shader
  1508. with --eliminate-local-multi-store
  1509. #version 440 core
  1510. void a() {
  1511. int[10] arr;
  1512. for (int i = 0; i < 10; i++) {
  1513. arr[0] = arr[i]; // peel first
  1514. arr[i] = arr[0]; // peel first
  1515. arr[9] = arr[i]; // peel last
  1516. arr[i] = arr[9]; // peel last
  1517. }
  1518. }
  1519. void b() {
  1520. int[11] arr;
  1521. for (int i = 0; i <= 10; i++) {
  1522. arr[0] = arr[i]; // peel first
  1523. arr[i] = arr[0]; // peel first
  1524. arr[10] = arr[i]; // peel last
  1525. arr[i] = arr[10]; // peel last
  1526. }
  1527. }
  1528. void c() {
  1529. int[11] arr;
  1530. for (int i = 10; i > 0; i--) {
  1531. arr[10] = arr[i]; // peel first
  1532. arr[i] = arr[10]; // peel first
  1533. arr[1] = arr[i]; // peel last
  1534. arr[i] = arr[1]; // peel last
  1535. }
  1536. }
  1537. void d() {
  1538. int[11] arr;
  1539. for (int i = 10; i >= 0; i--) {
  1540. arr[10] = arr[i]; // peel first
  1541. arr[i] = arr[10]; // peel first
  1542. arr[0] = arr[i]; // peel last
  1543. arr[i] = arr[0]; // peel last
  1544. }
  1545. }
  1546. void main(){
  1547. a();
  1548. b();
  1549. c();
  1550. d();
  1551. }
  1552. */
  1553. TEST(DependencyAnalysis, WeakZeroSIV) {
  1554. const std::string text = R"( OpCapability Shader
  1555. %1 = OpExtInstImport "GLSL.std.450"
  1556. OpMemoryModel Logical GLSL450
  1557. OpEntryPoint Fragment %4 "main"
  1558. OpExecutionMode %4 OriginUpperLeft
  1559. OpSource GLSL 440
  1560. OpName %4 "main"
  1561. OpName %6 "a("
  1562. OpName %8 "b("
  1563. OpName %10 "c("
  1564. OpName %12 "d("
  1565. OpName %16 "i"
  1566. OpName %31 "arr"
  1567. OpName %52 "i"
  1568. OpName %63 "arr"
  1569. OpName %82 "i"
  1570. OpName %90 "arr"
  1571. OpName %109 "i"
  1572. OpName %117 "arr"
  1573. %2 = OpTypeVoid
  1574. %3 = OpTypeFunction %2
  1575. %14 = OpTypeInt 32 1
  1576. %15 = OpTypePointer Function %14
  1577. %17 = OpConstant %14 0
  1578. %24 = OpConstant %14 10
  1579. %25 = OpTypeBool
  1580. %27 = OpTypeInt 32 0
  1581. %28 = OpConstant %27 10
  1582. %29 = OpTypeArray %14 %28
  1583. %30 = OpTypePointer Function %29
  1584. %40 = OpConstant %14 9
  1585. %50 = OpConstant %14 1
  1586. %60 = OpConstant %27 11
  1587. %61 = OpTypeArray %14 %60
  1588. %62 = OpTypePointer Function %61
  1589. %4 = OpFunction %2 None %3
  1590. %5 = OpLabel
  1591. %136 = OpFunctionCall %2 %6
  1592. %137 = OpFunctionCall %2 %8
  1593. %138 = OpFunctionCall %2 %10
  1594. %139 = OpFunctionCall %2 %12
  1595. OpReturn
  1596. OpFunctionEnd
  1597. %6 = OpFunction %2 None %3
  1598. %7 = OpLabel
  1599. %16 = OpVariable %15 Function
  1600. %31 = OpVariable %30 Function
  1601. OpStore %16 %17
  1602. OpBranch %18
  1603. %18 = OpLabel
  1604. %140 = OpPhi %14 %17 %7 %51 %21
  1605. OpLoopMerge %20 %21 None
  1606. OpBranch %22
  1607. %22 = OpLabel
  1608. %26 = OpSLessThan %25 %140 %24
  1609. OpBranchConditional %26 %19 %20
  1610. %19 = OpLabel
  1611. %33 = OpAccessChain %15 %31 %140
  1612. %34 = OpLoad %14 %33
  1613. %35 = OpAccessChain %15 %31 %17
  1614. OpStore %35 %34
  1615. %37 = OpAccessChain %15 %31 %17
  1616. %38 = OpLoad %14 %37
  1617. %39 = OpAccessChain %15 %31 %140
  1618. OpStore %39 %38
  1619. %42 = OpAccessChain %15 %31 %140
  1620. %43 = OpLoad %14 %42
  1621. %44 = OpAccessChain %15 %31 %40
  1622. OpStore %44 %43
  1623. %46 = OpAccessChain %15 %31 %40
  1624. %47 = OpLoad %14 %46
  1625. %48 = OpAccessChain %15 %31 %140
  1626. OpStore %48 %47
  1627. OpBranch %21
  1628. %21 = OpLabel
  1629. %51 = OpIAdd %14 %140 %50
  1630. OpStore %16 %51
  1631. OpBranch %18
  1632. %20 = OpLabel
  1633. OpReturn
  1634. OpFunctionEnd
  1635. %8 = OpFunction %2 None %3
  1636. %9 = OpLabel
  1637. %52 = OpVariable %15 Function
  1638. %63 = OpVariable %62 Function
  1639. OpStore %52 %17
  1640. OpBranch %53
  1641. %53 = OpLabel
  1642. %141 = OpPhi %14 %17 %9 %81 %56
  1643. OpLoopMerge %55 %56 None
  1644. OpBranch %57
  1645. %57 = OpLabel
  1646. %59 = OpSLessThanEqual %25 %141 %24
  1647. OpBranchConditional %59 %54 %55
  1648. %54 = OpLabel
  1649. %65 = OpAccessChain %15 %63 %141
  1650. %66 = OpLoad %14 %65
  1651. %67 = OpAccessChain %15 %63 %17
  1652. OpStore %67 %66
  1653. %69 = OpAccessChain %15 %63 %17
  1654. %70 = OpLoad %14 %69
  1655. %71 = OpAccessChain %15 %63 %141
  1656. OpStore %71 %70
  1657. %73 = OpAccessChain %15 %63 %141
  1658. %74 = OpLoad %14 %73
  1659. %75 = OpAccessChain %15 %63 %24
  1660. OpStore %75 %74
  1661. %77 = OpAccessChain %15 %63 %24
  1662. %78 = OpLoad %14 %77
  1663. %79 = OpAccessChain %15 %63 %141
  1664. OpStore %79 %78
  1665. OpBranch %56
  1666. %56 = OpLabel
  1667. %81 = OpIAdd %14 %141 %50
  1668. OpStore %52 %81
  1669. OpBranch %53
  1670. %55 = OpLabel
  1671. OpReturn
  1672. OpFunctionEnd
  1673. %10 = OpFunction %2 None %3
  1674. %11 = OpLabel
  1675. %82 = OpVariable %15 Function
  1676. %90 = OpVariable %62 Function
  1677. OpStore %82 %24
  1678. OpBranch %83
  1679. %83 = OpLabel
  1680. %142 = OpPhi %14 %24 %11 %108 %86
  1681. OpLoopMerge %85 %86 None
  1682. OpBranch %87
  1683. %87 = OpLabel
  1684. %89 = OpSGreaterThan %25 %142 %17
  1685. OpBranchConditional %89 %84 %85
  1686. %84 = OpLabel
  1687. %92 = OpAccessChain %15 %90 %142
  1688. %93 = OpLoad %14 %92
  1689. %94 = OpAccessChain %15 %90 %24
  1690. OpStore %94 %93
  1691. %96 = OpAccessChain %15 %90 %24
  1692. %97 = OpLoad %14 %96
  1693. %98 = OpAccessChain %15 %90 %142
  1694. OpStore %98 %97
  1695. %100 = OpAccessChain %15 %90 %142
  1696. %101 = OpLoad %14 %100
  1697. %102 = OpAccessChain %15 %90 %50
  1698. OpStore %102 %101
  1699. %104 = OpAccessChain %15 %90 %50
  1700. %105 = OpLoad %14 %104
  1701. %106 = OpAccessChain %15 %90 %142
  1702. OpStore %106 %105
  1703. OpBranch %86
  1704. %86 = OpLabel
  1705. %108 = OpISub %14 %142 %50
  1706. OpStore %82 %108
  1707. OpBranch %83
  1708. %85 = OpLabel
  1709. OpReturn
  1710. OpFunctionEnd
  1711. %12 = OpFunction %2 None %3
  1712. %13 = OpLabel
  1713. %109 = OpVariable %15 Function
  1714. %117 = OpVariable %62 Function
  1715. OpStore %109 %24
  1716. OpBranch %110
  1717. %110 = OpLabel
  1718. %143 = OpPhi %14 %24 %13 %135 %113
  1719. OpLoopMerge %112 %113 None
  1720. OpBranch %114
  1721. %114 = OpLabel
  1722. %116 = OpSGreaterThanEqual %25 %143 %17
  1723. OpBranchConditional %116 %111 %112
  1724. %111 = OpLabel
  1725. %119 = OpAccessChain %15 %117 %143
  1726. %120 = OpLoad %14 %119
  1727. %121 = OpAccessChain %15 %117 %24
  1728. OpStore %121 %120
  1729. %123 = OpAccessChain %15 %117 %24
  1730. %124 = OpLoad %14 %123
  1731. %125 = OpAccessChain %15 %117 %143
  1732. OpStore %125 %124
  1733. %127 = OpAccessChain %15 %117 %143
  1734. %128 = OpLoad %14 %127
  1735. %129 = OpAccessChain %15 %117 %17
  1736. OpStore %129 %128
  1737. %131 = OpAccessChain %15 %117 %17
  1738. %132 = OpLoad %14 %131
  1739. %133 = OpAccessChain %15 %117 %143
  1740. OpStore %133 %132
  1741. OpBranch %113
  1742. %113 = OpLabel
  1743. %135 = OpISub %14 %143 %50
  1744. OpStore %109 %135
  1745. OpBranch %110
  1746. %112 = OpLabel
  1747. OpReturn
  1748. OpFunctionEnd
  1749. )";
  1750. std::unique_ptr<IRContext> context =
  1751. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  1752. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1753. Module* module = context->module();
  1754. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  1755. << text << std::endl;
  1756. // For the loop in function a
  1757. {
  1758. const Function* f = spvtest::GetFunction(module, 6);
  1759. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  1760. Loop* loop = &ld.GetLoopByIndex(0);
  1761. std::vector<const Loop*> loops{loop};
  1762. LoopDependenceAnalysis analysis{context.get(), loops};
  1763. const Instruction* store[4];
  1764. int stores_found = 0;
  1765. for (const Instruction& inst : *spvtest::GetBasicBlock(f, 19)) {
  1766. if (inst.opcode() == SpvOp::SpvOpStore) {
  1767. store[stores_found] = &inst;
  1768. ++stores_found;
  1769. }
  1770. }
  1771. for (int i = 0; i < 4; ++i) {
  1772. EXPECT_TRUE(store[i]);
  1773. }
  1774. // Tests identifying peel first with weak zero with destination as zero
  1775. // index.
  1776. // 34 -> 35
  1777. {
  1778. DistanceVector distance_vector{loops.size()};
  1779. EXPECT_FALSE(analysis.GetDependence(
  1780. context->get_def_use_mgr()->GetDef(34), store[0], &distance_vector));
  1781. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  1782. DistanceEntry::DependenceInformation::PEEL);
  1783. EXPECT_TRUE(distance_vector.GetEntries()[0].peel_first);
  1784. }
  1785. // Tests identifying peel first with weak zero with source as zero index.
  1786. // 38 -> 39
  1787. {
  1788. DistanceVector distance_vector{loops.size()};
  1789. EXPECT_FALSE(analysis.GetDependence(
  1790. context->get_def_use_mgr()->GetDef(38), store[1], &distance_vector));
  1791. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  1792. DistanceEntry::DependenceInformation::PEEL);
  1793. EXPECT_TRUE(distance_vector.GetEntries()[0].peel_first);
  1794. }
  1795. // Tests identifying peel first with weak zero with destination as zero
  1796. // index.
  1797. // 43 -> 44
  1798. {
  1799. DistanceVector distance_vector{loops.size()};
  1800. EXPECT_FALSE(analysis.GetDependence(
  1801. context->get_def_use_mgr()->GetDef(43), store[2], &distance_vector));
  1802. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  1803. DistanceEntry::DependenceInformation::PEEL);
  1804. EXPECT_TRUE(distance_vector.GetEntries()[0].peel_last);
  1805. }
  1806. // Tests identifying peel first with weak zero with source as zero index.
  1807. // 47 -> 48
  1808. {
  1809. DistanceVector distance_vector{loops.size()};
  1810. EXPECT_FALSE(analysis.GetDependence(
  1811. context->get_def_use_mgr()->GetDef(47), store[3], &distance_vector));
  1812. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  1813. DistanceEntry::DependenceInformation::PEEL);
  1814. EXPECT_TRUE(distance_vector.GetEntries()[0].peel_last);
  1815. }
  1816. }
  1817. // For the loop in function b
  1818. {
  1819. const Function* f = spvtest::GetFunction(module, 8);
  1820. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  1821. Loop* loop = &ld.GetLoopByIndex(0);
  1822. std::vector<const Loop*> loops{loop};
  1823. LoopDependenceAnalysis analysis{context.get(), loops};
  1824. const Instruction* store[4];
  1825. int stores_found = 0;
  1826. for (const Instruction& inst : *spvtest::GetBasicBlock(f, 54)) {
  1827. if (inst.opcode() == SpvOp::SpvOpStore) {
  1828. store[stores_found] = &inst;
  1829. ++stores_found;
  1830. }
  1831. }
  1832. for (int i = 0; i < 4; ++i) {
  1833. EXPECT_TRUE(store[i]);
  1834. }
  1835. // Tests identifying peel first with weak zero with destination as zero
  1836. // index.
  1837. // 66 -> 67
  1838. {
  1839. DistanceVector distance_vector{loops.size()};
  1840. EXPECT_FALSE(analysis.GetDependence(
  1841. context->get_def_use_mgr()->GetDef(66), store[0], &distance_vector));
  1842. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  1843. DistanceEntry::DependenceInformation::PEEL);
  1844. EXPECT_TRUE(distance_vector.GetEntries()[0].peel_first);
  1845. }
  1846. // Tests identifying peel first with weak zero with source as zero index.
  1847. // 70 -> 71
  1848. {
  1849. DistanceVector distance_vector{loops.size()};
  1850. EXPECT_FALSE(analysis.GetDependence(
  1851. context->get_def_use_mgr()->GetDef(70), store[1], &distance_vector));
  1852. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  1853. DistanceEntry::DependenceInformation::PEEL);
  1854. EXPECT_TRUE(distance_vector.GetEntries()[0].peel_first);
  1855. }
  1856. // Tests identifying peel first with weak zero with destination as zero
  1857. // index.
  1858. // 74 -> 75
  1859. {
  1860. DistanceVector distance_vector{loops.size()};
  1861. EXPECT_FALSE(analysis.GetDependence(
  1862. context->get_def_use_mgr()->GetDef(74), store[2], &distance_vector));
  1863. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  1864. DistanceEntry::DependenceInformation::PEEL);
  1865. EXPECT_TRUE(distance_vector.GetEntries()[0].peel_last);
  1866. }
  1867. // Tests identifying peel first with weak zero with source as zero index.
  1868. // 78 -> 79
  1869. {
  1870. DistanceVector distance_vector{loops.size()};
  1871. EXPECT_FALSE(analysis.GetDependence(
  1872. context->get_def_use_mgr()->GetDef(78), store[3], &distance_vector));
  1873. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  1874. DistanceEntry::DependenceInformation::PEEL);
  1875. EXPECT_TRUE(distance_vector.GetEntries()[0].peel_last);
  1876. }
  1877. }
  1878. // For the loop in function c
  1879. {
  1880. const Function* f = spvtest::GetFunction(module, 10);
  1881. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  1882. Loop* loop = &ld.GetLoopByIndex(0);
  1883. std::vector<const Loop*> loops{loop};
  1884. LoopDependenceAnalysis analysis{context.get(), loops};
  1885. const Instruction* store[4];
  1886. int stores_found = 0;
  1887. for (const Instruction& inst : *spvtest::GetBasicBlock(f, 84)) {
  1888. if (inst.opcode() == SpvOp::SpvOpStore) {
  1889. store[stores_found] = &inst;
  1890. ++stores_found;
  1891. }
  1892. }
  1893. for (int i = 0; i < 4; ++i) {
  1894. EXPECT_TRUE(store[i]);
  1895. }
  1896. // Tests identifying peel first with weak zero with destination as zero
  1897. // index.
  1898. // 93 -> 94
  1899. {
  1900. DistanceVector distance_vector{loops.size()};
  1901. EXPECT_FALSE(analysis.GetDependence(
  1902. context->get_def_use_mgr()->GetDef(93), store[0], &distance_vector));
  1903. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  1904. DistanceEntry::DependenceInformation::PEEL);
  1905. EXPECT_TRUE(distance_vector.GetEntries()[0].peel_first);
  1906. }
  1907. // Tests identifying peel first with weak zero with source as zero index.
  1908. // 97 -> 98
  1909. {
  1910. DistanceVector distance_vector{loops.size()};
  1911. EXPECT_FALSE(analysis.GetDependence(
  1912. context->get_def_use_mgr()->GetDef(97), store[1], &distance_vector));
  1913. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  1914. DistanceEntry::DependenceInformation::PEEL);
  1915. EXPECT_TRUE(distance_vector.GetEntries()[0].peel_first);
  1916. }
  1917. // Tests identifying peel first with weak zero with destination as zero
  1918. // index.
  1919. // 101 -> 102
  1920. {
  1921. DistanceVector distance_vector{loops.size()};
  1922. EXPECT_FALSE(analysis.GetDependence(
  1923. context->get_def_use_mgr()->GetDef(101), store[2], &distance_vector));
  1924. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  1925. DistanceEntry::DependenceInformation::PEEL);
  1926. EXPECT_TRUE(distance_vector.GetEntries()[0].peel_last);
  1927. }
  1928. // Tests identifying peel first with weak zero with source as zero index.
  1929. // 105 -> 106
  1930. {
  1931. DistanceVector distance_vector{loops.size()};
  1932. EXPECT_FALSE(analysis.GetDependence(
  1933. context->get_def_use_mgr()->GetDef(105), store[3], &distance_vector));
  1934. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  1935. DistanceEntry::DependenceInformation::PEEL);
  1936. EXPECT_TRUE(distance_vector.GetEntries()[0].peel_last);
  1937. }
  1938. }
  1939. // For the loop in function d
  1940. {
  1941. const Function* f = spvtest::GetFunction(module, 12);
  1942. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  1943. Loop* loop = &ld.GetLoopByIndex(0);
  1944. std::vector<const Loop*> loops{loop};
  1945. LoopDependenceAnalysis analysis{context.get(), loops};
  1946. const Instruction* store[4];
  1947. int stores_found = 0;
  1948. for (const Instruction& inst : *spvtest::GetBasicBlock(f, 111)) {
  1949. if (inst.opcode() == SpvOp::SpvOpStore) {
  1950. store[stores_found] = &inst;
  1951. ++stores_found;
  1952. }
  1953. }
  1954. for (int i = 0; i < 4; ++i) {
  1955. EXPECT_TRUE(store[i]);
  1956. }
  1957. // Tests identifying peel first with weak zero with destination as zero
  1958. // index.
  1959. // 120 -> 121
  1960. {
  1961. DistanceVector distance_vector{loops.size()};
  1962. EXPECT_FALSE(analysis.GetDependence(
  1963. context->get_def_use_mgr()->GetDef(120), store[0], &distance_vector));
  1964. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  1965. DistanceEntry::DependenceInformation::PEEL);
  1966. EXPECT_TRUE(distance_vector.GetEntries()[0].peel_first);
  1967. }
  1968. // Tests identifying peel first with weak zero with source as zero index.
  1969. // 124 -> 125
  1970. {
  1971. DistanceVector distance_vector{loops.size()};
  1972. EXPECT_FALSE(analysis.GetDependence(
  1973. context->get_def_use_mgr()->GetDef(124), store[1], &distance_vector));
  1974. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  1975. DistanceEntry::DependenceInformation::PEEL);
  1976. EXPECT_TRUE(distance_vector.GetEntries()[0].peel_first);
  1977. }
  1978. // Tests identifying peel first with weak zero with destination as zero
  1979. // index.
  1980. // 128 -> 129
  1981. {
  1982. DistanceVector distance_vector{loops.size()};
  1983. EXPECT_FALSE(analysis.GetDependence(
  1984. context->get_def_use_mgr()->GetDef(128), store[2], &distance_vector));
  1985. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  1986. DistanceEntry::DependenceInformation::PEEL);
  1987. EXPECT_TRUE(distance_vector.GetEntries()[0].peel_last);
  1988. }
  1989. // Tests identifying peel first with weak zero with source as zero index.
  1990. // 132 -> 133
  1991. {
  1992. DistanceVector distance_vector{loops.size()};
  1993. EXPECT_FALSE(analysis.GetDependence(
  1994. context->get_def_use_mgr()->GetDef(132), store[3], &distance_vector));
  1995. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  1996. DistanceEntry::DependenceInformation::PEEL);
  1997. EXPECT_TRUE(distance_vector.GetEntries()[0].peel_last);
  1998. }
  1999. }
  2000. }
  2001. /*
  2002. Generated from the following GLSL fragment shader
  2003. with --eliminate-local-multi-store
  2004. #version 440 core
  2005. void main(){
  2006. int[10][10] arr;
  2007. for (int i = 0; i < 10; i++) {
  2008. arr[i][i] = arr[i][i];
  2009. arr[0][i] = arr[1][i];
  2010. arr[1][i] = arr[0][i];
  2011. arr[i][0] = arr[i][1];
  2012. arr[i][1] = arr[i][0];
  2013. arr[0][1] = arr[1][0];
  2014. }
  2015. }
  2016. */
  2017. TEST(DependencyAnalysis, MultipleSubscriptZIVSIV) {
  2018. const std::string text = R"( OpCapability Shader
  2019. %1 = OpExtInstImport "GLSL.std.450"
  2020. OpMemoryModel Logical GLSL450
  2021. OpEntryPoint Fragment %4 "main"
  2022. OpExecutionMode %4 OriginUpperLeft
  2023. OpSource GLSL 440
  2024. OpName %4 "main"
  2025. OpName %8 "i"
  2026. OpName %24 "arr"
  2027. %2 = OpTypeVoid
  2028. %3 = OpTypeFunction %2
  2029. %6 = OpTypeInt 32 1
  2030. %7 = OpTypePointer Function %6
  2031. %9 = OpConstant %6 0
  2032. %16 = OpConstant %6 10
  2033. %17 = OpTypeBool
  2034. %19 = OpTypeInt 32 0
  2035. %20 = OpConstant %19 10
  2036. %21 = OpTypeArray %6 %20
  2037. %22 = OpTypeArray %21 %20
  2038. %23 = OpTypePointer Function %22
  2039. %33 = OpConstant %6 1
  2040. %4 = OpFunction %2 None %3
  2041. %5 = OpLabel
  2042. %8 = OpVariable %7 Function
  2043. %24 = OpVariable %23 Function
  2044. OpStore %8 %9
  2045. OpBranch %10
  2046. %10 = OpLabel
  2047. %58 = OpPhi %6 %9 %5 %57 %13
  2048. OpLoopMerge %12 %13 None
  2049. OpBranch %14
  2050. %14 = OpLabel
  2051. %18 = OpSLessThan %17 %58 %16
  2052. OpBranchConditional %18 %11 %12
  2053. %11 = OpLabel
  2054. %29 = OpAccessChain %7 %24 %58 %58
  2055. %30 = OpLoad %6 %29
  2056. %31 = OpAccessChain %7 %24 %58 %58
  2057. OpStore %31 %30
  2058. %35 = OpAccessChain %7 %24 %33 %58
  2059. %36 = OpLoad %6 %35
  2060. %37 = OpAccessChain %7 %24 %9 %58
  2061. OpStore %37 %36
  2062. %40 = OpAccessChain %7 %24 %9 %58
  2063. %41 = OpLoad %6 %40
  2064. %42 = OpAccessChain %7 %24 %33 %58
  2065. OpStore %42 %41
  2066. %45 = OpAccessChain %7 %24 %58 %33
  2067. %46 = OpLoad %6 %45
  2068. %47 = OpAccessChain %7 %24 %58 %9
  2069. OpStore %47 %46
  2070. %50 = OpAccessChain %7 %24 %58 %9
  2071. %51 = OpLoad %6 %50
  2072. %52 = OpAccessChain %7 %24 %58 %33
  2073. OpStore %52 %51
  2074. %53 = OpAccessChain %7 %24 %33 %9
  2075. %54 = OpLoad %6 %53
  2076. %55 = OpAccessChain %7 %24 %9 %33
  2077. OpStore %55 %54
  2078. OpBranch %13
  2079. %13 = OpLabel
  2080. %57 = OpIAdd %6 %58 %33
  2081. OpStore %8 %57
  2082. OpBranch %10
  2083. %12 = OpLabel
  2084. OpReturn
  2085. OpFunctionEnd
  2086. )";
  2087. std::unique_ptr<IRContext> context =
  2088. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  2089. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2090. Module* module = context->module();
  2091. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  2092. << text << std::endl;
  2093. const Function* f = spvtest::GetFunction(module, 4);
  2094. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  2095. Loop* loop = &ld.GetLoopByIndex(0);
  2096. std::vector<const Loop*> loops{loop};
  2097. LoopDependenceAnalysis analysis{context.get(), loops};
  2098. const Instruction* store[6];
  2099. int stores_found = 0;
  2100. for (const Instruction& inst : *spvtest::GetBasicBlock(f, 11)) {
  2101. if (inst.opcode() == SpvOp::SpvOpStore) {
  2102. store[stores_found] = &inst;
  2103. ++stores_found;
  2104. }
  2105. }
  2106. for (int i = 0; i < 6; ++i) {
  2107. EXPECT_TRUE(store[i]);
  2108. }
  2109. // 30 -> 31
  2110. {
  2111. DistanceVector distance_vector{loops.size()};
  2112. EXPECT_FALSE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(30),
  2113. store[0], &distance_vector));
  2114. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  2115. DistanceEntry::DependenceInformation::DISTANCE);
  2116. EXPECT_EQ(distance_vector.GetEntries()[0].direction,
  2117. DistanceEntry::Directions::EQ);
  2118. EXPECT_EQ(distance_vector.GetEntries()[0].distance, 0);
  2119. }
  2120. // 36 -> 37
  2121. {
  2122. DistanceVector distance_vector{loops.size()};
  2123. EXPECT_TRUE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(36),
  2124. store[1], &distance_vector));
  2125. }
  2126. // 41 -> 42
  2127. {
  2128. DistanceVector distance_vector{loops.size()};
  2129. EXPECT_TRUE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(41),
  2130. store[2], &distance_vector));
  2131. }
  2132. // 46 -> 47
  2133. {
  2134. DistanceVector distance_vector{loops.size()};
  2135. EXPECT_TRUE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(46),
  2136. store[3], &distance_vector));
  2137. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  2138. DistanceEntry::DependenceInformation::DISTANCE);
  2139. EXPECT_EQ(distance_vector.GetEntries()[0].direction,
  2140. DistanceEntry::Directions::EQ);
  2141. EXPECT_EQ(distance_vector.GetEntries()[0].distance, 0);
  2142. }
  2143. // 51 -> 52
  2144. {
  2145. DistanceVector distance_vector{loops.size()};
  2146. EXPECT_TRUE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(51),
  2147. store[4], &distance_vector));
  2148. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  2149. DistanceEntry::DependenceInformation::DISTANCE);
  2150. EXPECT_EQ(distance_vector.GetEntries()[0].direction,
  2151. DistanceEntry::Directions::EQ);
  2152. EXPECT_EQ(distance_vector.GetEntries()[0].distance, 0);
  2153. }
  2154. // 54 -> 55
  2155. {
  2156. DistanceVector distance_vector{loops.size()};
  2157. EXPECT_TRUE(analysis.GetDependence(context->get_def_use_mgr()->GetDef(54),
  2158. store[5], &distance_vector));
  2159. }
  2160. }
  2161. /*
  2162. Generated from the following GLSL fragment shader
  2163. with --eliminate-local-multi-store
  2164. #version 440 core
  2165. void a(){
  2166. int[10] arr;
  2167. for (int i = 0; i < 10; i++) {
  2168. for (int j = 0; j < 10; j++) {
  2169. arr[j] = arr[j];
  2170. }
  2171. }
  2172. }
  2173. void b(){
  2174. int[10] arr;
  2175. for (int i = 0; i < 10; i++) {
  2176. for (int j = 0; j < 10; j++) {
  2177. arr[i] = arr[i];
  2178. }
  2179. }
  2180. }
  2181. void main() {
  2182. a();
  2183. b();
  2184. }
  2185. */
  2186. TEST(DependencyAnalysis, IrrelevantSubscripts) {
  2187. const std::string text = R"( OpCapability Shader
  2188. %1 = OpExtInstImport "GLSL.std.450"
  2189. OpMemoryModel Logical GLSL450
  2190. OpEntryPoint Fragment %4 "main"
  2191. OpExecutionMode %4 OriginUpperLeft
  2192. OpSource GLSL 440
  2193. OpName %4 "main"
  2194. OpName %6 "a("
  2195. OpName %8 "b("
  2196. OpName %12 "i"
  2197. OpName %23 "j"
  2198. OpName %35 "arr"
  2199. OpName %46 "i"
  2200. OpName %54 "j"
  2201. OpName %62 "arr"
  2202. %2 = OpTypeVoid
  2203. %3 = OpTypeFunction %2
  2204. %10 = OpTypeInt 32 1
  2205. %11 = OpTypePointer Function %10
  2206. %13 = OpConstant %10 0
  2207. %20 = OpConstant %10 10
  2208. %21 = OpTypeBool
  2209. %31 = OpTypeInt 32 0
  2210. %32 = OpConstant %31 10
  2211. %33 = OpTypeArray %10 %32
  2212. %34 = OpTypePointer Function %33
  2213. %42 = OpConstant %10 1
  2214. %4 = OpFunction %2 None %3
  2215. %5 = OpLabel
  2216. %72 = OpFunctionCall %2 %6
  2217. %73 = OpFunctionCall %2 %8
  2218. OpReturn
  2219. OpFunctionEnd
  2220. %6 = OpFunction %2 None %3
  2221. %7 = OpLabel
  2222. %12 = OpVariable %11 Function
  2223. %23 = OpVariable %11 Function
  2224. %35 = OpVariable %34 Function
  2225. OpStore %12 %13
  2226. OpBranch %14
  2227. %14 = OpLabel
  2228. %74 = OpPhi %10 %13 %7 %45 %17
  2229. OpLoopMerge %16 %17 None
  2230. OpBranch %18
  2231. %18 = OpLabel
  2232. %22 = OpSLessThan %21 %74 %20
  2233. OpBranchConditional %22 %15 %16
  2234. %15 = OpLabel
  2235. OpStore %23 %13
  2236. OpBranch %24
  2237. %24 = OpLabel
  2238. %75 = OpPhi %10 %13 %15 %43 %27
  2239. OpLoopMerge %26 %27 None
  2240. OpBranch %28
  2241. %28 = OpLabel
  2242. %30 = OpSLessThan %21 %75 %20
  2243. OpBranchConditional %30 %25 %26
  2244. %25 = OpLabel
  2245. %38 = OpAccessChain %11 %35 %75
  2246. %39 = OpLoad %10 %38
  2247. %40 = OpAccessChain %11 %35 %75
  2248. OpStore %40 %39
  2249. OpBranch %27
  2250. %27 = OpLabel
  2251. %43 = OpIAdd %10 %75 %42
  2252. OpStore %23 %43
  2253. OpBranch %24
  2254. %26 = OpLabel
  2255. OpBranch %17
  2256. %17 = OpLabel
  2257. %45 = OpIAdd %10 %74 %42
  2258. OpStore %12 %45
  2259. OpBranch %14
  2260. %16 = OpLabel
  2261. OpReturn
  2262. OpFunctionEnd
  2263. %8 = OpFunction %2 None %3
  2264. %9 = OpLabel
  2265. %46 = OpVariable %11 Function
  2266. %54 = OpVariable %11 Function
  2267. %62 = OpVariable %34 Function
  2268. OpStore %46 %13
  2269. OpBranch %47
  2270. %47 = OpLabel
  2271. %77 = OpPhi %10 %13 %9 %71 %50
  2272. OpLoopMerge %49 %50 None
  2273. OpBranch %51
  2274. %51 = OpLabel
  2275. %53 = OpSLessThan %21 %77 %20
  2276. OpBranchConditional %53 %48 %49
  2277. %48 = OpLabel
  2278. OpStore %54 %13
  2279. OpBranch %55
  2280. %55 = OpLabel
  2281. %78 = OpPhi %10 %13 %48 %69 %58
  2282. OpLoopMerge %57 %58 None
  2283. OpBranch %59
  2284. %59 = OpLabel
  2285. %61 = OpSLessThan %21 %78 %20
  2286. OpBranchConditional %61 %56 %57
  2287. %56 = OpLabel
  2288. %65 = OpAccessChain %11 %62 %77
  2289. %66 = OpLoad %10 %65
  2290. %67 = OpAccessChain %11 %62 %77
  2291. OpStore %67 %66
  2292. OpBranch %58
  2293. %58 = OpLabel
  2294. %69 = OpIAdd %10 %78 %42
  2295. OpStore %54 %69
  2296. OpBranch %55
  2297. %57 = OpLabel
  2298. OpBranch %50
  2299. %50 = OpLabel
  2300. %71 = OpIAdd %10 %77 %42
  2301. OpStore %46 %71
  2302. OpBranch %47
  2303. %49 = OpLabel
  2304. OpReturn
  2305. OpFunctionEnd
  2306. )";
  2307. std::unique_ptr<IRContext> context =
  2308. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  2309. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2310. Module* module = context->module();
  2311. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  2312. << text << std::endl;
  2313. // For the loop in function a
  2314. {
  2315. const Function* f = spvtest::GetFunction(module, 6);
  2316. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  2317. std::vector<const Loop*> loops{&ld.GetLoopByIndex(1),
  2318. &ld.GetLoopByIndex(0)};
  2319. LoopDependenceAnalysis analysis{context.get(), loops};
  2320. const Instruction* store[1];
  2321. int stores_found = 0;
  2322. for (const Instruction& inst : *spvtest::GetBasicBlock(f, 25)) {
  2323. if (inst.opcode() == SpvOp::SpvOpStore) {
  2324. store[stores_found] = &inst;
  2325. ++stores_found;
  2326. }
  2327. }
  2328. for (int i = 0; i < 1; ++i) {
  2329. EXPECT_TRUE(store[i]);
  2330. }
  2331. // 39 -> 40
  2332. {
  2333. DistanceVector distance_vector{loops.size()};
  2334. analysis.SetDebugStream(std::cout);
  2335. EXPECT_FALSE(analysis.GetDependence(
  2336. context->get_def_use_mgr()->GetDef(39), store[0], &distance_vector));
  2337. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  2338. DistanceEntry::DependenceInformation::IRRELEVANT);
  2339. EXPECT_EQ(distance_vector.GetEntries()[1].dependence_information,
  2340. DistanceEntry::DependenceInformation::DISTANCE);
  2341. EXPECT_EQ(distance_vector.GetEntries()[1].distance, 0);
  2342. }
  2343. }
  2344. // For the loop in function b
  2345. {
  2346. const Function* f = spvtest::GetFunction(module, 8);
  2347. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  2348. std::vector<const Loop*> loops{&ld.GetLoopByIndex(1),
  2349. &ld.GetLoopByIndex(0)};
  2350. LoopDependenceAnalysis analysis{context.get(), loops};
  2351. const Instruction* store[1];
  2352. int stores_found = 0;
  2353. for (const Instruction& inst : *spvtest::GetBasicBlock(f, 56)) {
  2354. if (inst.opcode() == SpvOp::SpvOpStore) {
  2355. store[stores_found] = &inst;
  2356. ++stores_found;
  2357. }
  2358. }
  2359. for (int i = 0; i < 1; ++i) {
  2360. EXPECT_TRUE(store[i]);
  2361. }
  2362. // 66 -> 67
  2363. {
  2364. DistanceVector distance_vector{loops.size()};
  2365. EXPECT_FALSE(analysis.GetDependence(
  2366. context->get_def_use_mgr()->GetDef(66), store[0], &distance_vector));
  2367. EXPECT_EQ(distance_vector.GetEntries()[0].dependence_information,
  2368. DistanceEntry::DependenceInformation::DISTANCE);
  2369. EXPECT_EQ(distance_vector.GetEntries()[0].distance, 0);
  2370. EXPECT_EQ(distance_vector.GetEntries()[1].dependence_information,
  2371. DistanceEntry::DependenceInformation::IRRELEVANT);
  2372. }
  2373. }
  2374. }
  2375. void CheckDependenceAndDirection(const Instruction* source,
  2376. const Instruction* destination,
  2377. bool expected_dependence,
  2378. DistanceVector expected_distance,
  2379. LoopDependenceAnalysis* analysis) {
  2380. DistanceVector dv_entry(2);
  2381. EXPECT_EQ(expected_dependence,
  2382. analysis->GetDependence(source, destination, &dv_entry));
  2383. EXPECT_EQ(expected_distance, dv_entry);
  2384. }
  2385. /*
  2386. Generated from the following GLSL fragment shader
  2387. with --eliminate-local-multi-store
  2388. #version 440 core
  2389. layout(location = 0) in vec4 c;
  2390. void main(){
  2391. int[10] arr;
  2392. int a = 2;
  2393. int b = 3;
  2394. int N = int(c.x);
  2395. for (int i = 0; i < 10; i++) {
  2396. for (int j = 2; j < 10; j++) {
  2397. arr[i] = arr[j]; // 0
  2398. arr[j] = arr[i]; // 1
  2399. arr[j-2] = arr[i+3]; // 2
  2400. arr[j-a] = arr[i+b]; // 3
  2401. arr[2*i] = arr[4*j+3]; // 4, independent
  2402. arr[2*i] = arr[4*j]; // 5
  2403. arr[i+j] = arr[i+j]; // 6
  2404. arr[10*i+j] = arr[10*i+j]; // 7
  2405. arr[10*i+10*j] = arr[10*i+10*j+3]; // 8, independent
  2406. arr[10*i+10*j] = arr[10*i+N*j+3]; // 9, bail out because of N coefficient
  2407. arr[10*i+10*j] = arr[10*i+10*j+N]; // 10, bail out because of N constant
  2408. // term
  2409. arr[10*i+N*j] = arr[10*i+10*j+3]; // 11, bail out because of N coefficient
  2410. arr[10*i+10*j+N] = arr[10*i+10*j]; // 12, bail out because of N constant
  2411. // term
  2412. arr[10*i] = arr[5*j]; // 13, independent
  2413. arr[5*i] = arr[10*j]; // 14, independent
  2414. arr[9*i] = arr[3*j]; // 15, independent
  2415. arr[3*i] = arr[9*j]; // 16, independent
  2416. }
  2417. }
  2418. }
  2419. */
  2420. TEST(DependencyAnalysis, MIV) {
  2421. const std::string text = R"(
  2422. OpCapability Shader
  2423. %1 = OpExtInstImport "GLSL.std.450"
  2424. OpMemoryModel Logical GLSL450
  2425. OpEntryPoint Fragment %4 "main" %16
  2426. OpExecutionMode %4 OriginUpperLeft
  2427. OpSource GLSL 440
  2428. OpName %4 "main"
  2429. OpName %8 "a"
  2430. OpName %10 "b"
  2431. OpName %12 "N"
  2432. OpName %16 "c"
  2433. OpName %23 "i"
  2434. OpName %34 "j"
  2435. OpName %45 "arr"
  2436. OpDecorate %16 Location 0
  2437. %2 = OpTypeVoid
  2438. %3 = OpTypeFunction %2
  2439. %6 = OpTypeInt 32 1
  2440. %7 = OpTypePointer Function %6
  2441. %9 = OpConstant %6 2
  2442. %11 = OpConstant %6 3
  2443. %13 = OpTypeFloat 32
  2444. %14 = OpTypeVector %13 4
  2445. %15 = OpTypePointer Input %14
  2446. %16 = OpVariable %15 Input
  2447. %17 = OpTypeInt 32 0
  2448. %18 = OpConstant %17 0
  2449. %19 = OpTypePointer Input %13
  2450. %24 = OpConstant %6 0
  2451. %31 = OpConstant %6 10
  2452. %32 = OpTypeBool
  2453. %42 = OpConstant %17 10
  2454. %43 = OpTypeArray %6 %42
  2455. %44 = OpTypePointer Function %43
  2456. %74 = OpConstant %6 4
  2457. %184 = OpConstant %6 5
  2458. %197 = OpConstant %6 9
  2459. %213 = OpConstant %6 1
  2460. %218 = OpUndef %6
  2461. %4 = OpFunction %2 None %3
  2462. %5 = OpLabel
  2463. %8 = OpVariable %7 Function
  2464. %10 = OpVariable %7 Function
  2465. %12 = OpVariable %7 Function
  2466. %23 = OpVariable %7 Function
  2467. %34 = OpVariable %7 Function
  2468. %45 = OpVariable %44 Function
  2469. OpStore %8 %9
  2470. OpStore %10 %11
  2471. %20 = OpAccessChain %19 %16 %18
  2472. %21 = OpLoad %13 %20
  2473. %22 = OpConvertFToS %6 %21
  2474. OpStore %12 %22
  2475. OpStore %23 %24
  2476. OpBranch %25
  2477. %25 = OpLabel
  2478. %217 = OpPhi %6 %24 %5 %216 %28
  2479. %219 = OpPhi %6 %218 %5 %220 %28
  2480. OpLoopMerge %27 %28 None
  2481. OpBranch %29
  2482. %29 = OpLabel
  2483. %33 = OpSLessThan %32 %217 %31
  2484. OpBranchConditional %33 %26 %27
  2485. %26 = OpLabel
  2486. OpStore %34 %9
  2487. OpBranch %35
  2488. %35 = OpLabel
  2489. %220 = OpPhi %6 %9 %26 %214 %38
  2490. OpLoopMerge %37 %38 None
  2491. OpBranch %39
  2492. %39 = OpLabel
  2493. %41 = OpSLessThan %32 %220 %31
  2494. OpBranchConditional %41 %36 %37
  2495. %36 = OpLabel
  2496. %48 = OpAccessChain %7 %45 %220
  2497. %49 = OpLoad %6 %48
  2498. %50 = OpAccessChain %7 %45 %217
  2499. OpStore %50 %49
  2500. %53 = OpAccessChain %7 %45 %217
  2501. %54 = OpLoad %6 %53
  2502. %55 = OpAccessChain %7 %45 %220
  2503. OpStore %55 %54
  2504. %57 = OpISub %6 %220 %9
  2505. %59 = OpIAdd %6 %217 %11
  2506. %60 = OpAccessChain %7 %45 %59
  2507. %61 = OpLoad %6 %60
  2508. %62 = OpAccessChain %7 %45 %57
  2509. OpStore %62 %61
  2510. %65 = OpISub %6 %220 %9
  2511. %68 = OpIAdd %6 %217 %11
  2512. %69 = OpAccessChain %7 %45 %68
  2513. %70 = OpLoad %6 %69
  2514. %71 = OpAccessChain %7 %45 %65
  2515. OpStore %71 %70
  2516. %73 = OpIMul %6 %9 %217
  2517. %76 = OpIMul %6 %74 %220
  2518. %77 = OpIAdd %6 %76 %11
  2519. %78 = OpAccessChain %7 %45 %77
  2520. %79 = OpLoad %6 %78
  2521. %80 = OpAccessChain %7 %45 %73
  2522. OpStore %80 %79
  2523. %82 = OpIMul %6 %9 %217
  2524. %84 = OpIMul %6 %74 %220
  2525. %85 = OpAccessChain %7 %45 %84
  2526. %86 = OpLoad %6 %85
  2527. %87 = OpAccessChain %7 %45 %82
  2528. OpStore %87 %86
  2529. %90 = OpIAdd %6 %217 %220
  2530. %93 = OpIAdd %6 %217 %220
  2531. %94 = OpAccessChain %7 %45 %93
  2532. %95 = OpLoad %6 %94
  2533. %96 = OpAccessChain %7 %45 %90
  2534. OpStore %96 %95
  2535. %98 = OpIMul %6 %31 %217
  2536. %100 = OpIAdd %6 %98 %220
  2537. %102 = OpIMul %6 %31 %217
  2538. %104 = OpIAdd %6 %102 %220
  2539. %105 = OpAccessChain %7 %45 %104
  2540. %106 = OpLoad %6 %105
  2541. %107 = OpAccessChain %7 %45 %100
  2542. OpStore %107 %106
  2543. %109 = OpIMul %6 %31 %217
  2544. %111 = OpIMul %6 %31 %220
  2545. %112 = OpIAdd %6 %109 %111
  2546. %114 = OpIMul %6 %31 %217
  2547. %116 = OpIMul %6 %31 %220
  2548. %117 = OpIAdd %6 %114 %116
  2549. %118 = OpIAdd %6 %117 %11
  2550. %119 = OpAccessChain %7 %45 %118
  2551. %120 = OpLoad %6 %119
  2552. %121 = OpAccessChain %7 %45 %112
  2553. OpStore %121 %120
  2554. %123 = OpIMul %6 %31 %217
  2555. %125 = OpIMul %6 %31 %220
  2556. %126 = OpIAdd %6 %123 %125
  2557. %128 = OpIMul %6 %31 %217
  2558. %131 = OpIMul %6 %22 %220
  2559. %132 = OpIAdd %6 %128 %131
  2560. %133 = OpIAdd %6 %132 %11
  2561. %134 = OpAccessChain %7 %45 %133
  2562. %135 = OpLoad %6 %134
  2563. %136 = OpAccessChain %7 %45 %126
  2564. OpStore %136 %135
  2565. %138 = OpIMul %6 %31 %217
  2566. %140 = OpIMul %6 %31 %220
  2567. %141 = OpIAdd %6 %138 %140
  2568. %143 = OpIMul %6 %31 %217
  2569. %145 = OpIMul %6 %31 %220
  2570. %146 = OpIAdd %6 %143 %145
  2571. %148 = OpIAdd %6 %146 %22
  2572. %149 = OpAccessChain %7 %45 %148
  2573. %150 = OpLoad %6 %149
  2574. %151 = OpAccessChain %7 %45 %141
  2575. OpStore %151 %150
  2576. %153 = OpIMul %6 %31 %217
  2577. %156 = OpIMul %6 %22 %220
  2578. %157 = OpIAdd %6 %153 %156
  2579. %159 = OpIMul %6 %31 %217
  2580. %161 = OpIMul %6 %31 %220
  2581. %162 = OpIAdd %6 %159 %161
  2582. %163 = OpIAdd %6 %162 %11
  2583. %164 = OpAccessChain %7 %45 %163
  2584. %165 = OpLoad %6 %164
  2585. %166 = OpAccessChain %7 %45 %157
  2586. OpStore %166 %165
  2587. %168 = OpIMul %6 %31 %217
  2588. %170 = OpIMul %6 %31 %220
  2589. %171 = OpIAdd %6 %168 %170
  2590. %173 = OpIAdd %6 %171 %22
  2591. %175 = OpIMul %6 %31 %217
  2592. %177 = OpIMul %6 %31 %220
  2593. %178 = OpIAdd %6 %175 %177
  2594. %179 = OpAccessChain %7 %45 %178
  2595. %180 = OpLoad %6 %179
  2596. %181 = OpAccessChain %7 %45 %173
  2597. OpStore %181 %180
  2598. %183 = OpIMul %6 %31 %217
  2599. %186 = OpIMul %6 %184 %220
  2600. %187 = OpAccessChain %7 %45 %186
  2601. %188 = OpLoad %6 %187
  2602. %189 = OpAccessChain %7 %45 %183
  2603. OpStore %189 %188
  2604. %191 = OpIMul %6 %184 %217
  2605. %193 = OpIMul %6 %31 %220
  2606. %194 = OpAccessChain %7 %45 %193
  2607. %195 = OpLoad %6 %194
  2608. %196 = OpAccessChain %7 %45 %191
  2609. OpStore %196 %195
  2610. %199 = OpIMul %6 %197 %217
  2611. %201 = OpIMul %6 %11 %220
  2612. %202 = OpAccessChain %7 %45 %201
  2613. %203 = OpLoad %6 %202
  2614. %204 = OpAccessChain %7 %45 %199
  2615. OpStore %204 %203
  2616. %206 = OpIMul %6 %11 %217
  2617. %208 = OpIMul %6 %197 %220
  2618. %209 = OpAccessChain %7 %45 %208
  2619. %210 = OpLoad %6 %209
  2620. %211 = OpAccessChain %7 %45 %206
  2621. OpStore %211 %210
  2622. OpBranch %38
  2623. %38 = OpLabel
  2624. %214 = OpIAdd %6 %220 %213
  2625. OpStore %34 %214
  2626. OpBranch %35
  2627. %37 = OpLabel
  2628. OpBranch %28
  2629. %28 = OpLabel
  2630. %216 = OpIAdd %6 %217 %213
  2631. OpStore %23 %216
  2632. OpBranch %25
  2633. %27 = OpLabel
  2634. OpReturn
  2635. OpFunctionEnd
  2636. )";
  2637. std::unique_ptr<IRContext> context =
  2638. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  2639. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2640. Module* module = context->module();
  2641. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  2642. << text << std::endl;
  2643. const Function* f = spvtest::GetFunction(module, 4);
  2644. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  2645. std::vector<const Loop*> loops{&ld.GetLoopByIndex(0), &ld.GetLoopByIndex(1)};
  2646. LoopDependenceAnalysis analysis{context.get(), loops};
  2647. const int instructions_expected = 17;
  2648. const Instruction* store[instructions_expected];
  2649. const Instruction* load[instructions_expected];
  2650. int stores_found = 0;
  2651. int loads_found = 0;
  2652. int block_id = 36;
  2653. ASSERT_TRUE(spvtest::GetBasicBlock(f, block_id));
  2654. for (const Instruction& inst : *spvtest::GetBasicBlock(f, block_id)) {
  2655. if (inst.opcode() == SpvOp::SpvOpStore) {
  2656. store[stores_found] = &inst;
  2657. ++stores_found;
  2658. }
  2659. if (inst.opcode() == SpvOp::SpvOpLoad) {
  2660. load[loads_found] = &inst;
  2661. ++loads_found;
  2662. }
  2663. }
  2664. EXPECT_EQ(instructions_expected, stores_found);
  2665. EXPECT_EQ(instructions_expected, loads_found);
  2666. auto directions_all = DistanceEntry(DistanceEntry::Directions::ALL);
  2667. auto directions_none = DistanceEntry(DistanceEntry::Directions::NONE);
  2668. auto dependent = DistanceVector({directions_all, directions_all});
  2669. auto independent = DistanceVector({directions_none, directions_none});
  2670. CheckDependenceAndDirection(load[0], store[0], false, dependent, &analysis);
  2671. CheckDependenceAndDirection(load[1], store[1], false, dependent, &analysis);
  2672. CheckDependenceAndDirection(load[2], store[2], false, dependent, &analysis);
  2673. CheckDependenceAndDirection(load[3], store[3], false, dependent, &analysis);
  2674. CheckDependenceAndDirection(load[4], store[4], true, independent, &analysis);
  2675. CheckDependenceAndDirection(load[5], store[5], false, dependent, &analysis);
  2676. CheckDependenceAndDirection(load[6], store[6], false, dependent, &analysis);
  2677. CheckDependenceAndDirection(load[7], store[7], false, dependent, &analysis);
  2678. CheckDependenceAndDirection(load[8], store[8], true, independent, &analysis);
  2679. CheckDependenceAndDirection(load[9], store[9], false, dependent, &analysis);
  2680. CheckDependenceAndDirection(load[10], store[10], false, dependent, &analysis);
  2681. CheckDependenceAndDirection(load[11], store[11], false, dependent, &analysis);
  2682. CheckDependenceAndDirection(load[12], store[12], false, dependent, &analysis);
  2683. CheckDependenceAndDirection(load[13], store[13], true, independent,
  2684. &analysis);
  2685. CheckDependenceAndDirection(load[14], store[14], true, independent,
  2686. &analysis);
  2687. CheckDependenceAndDirection(load[15], store[15], true, independent,
  2688. &analysis);
  2689. CheckDependenceAndDirection(load[16], store[16], true, independent,
  2690. &analysis);
  2691. }
  2692. void PartitionSubscripts(const Instruction* instruction_0,
  2693. const Instruction* instruction_1,
  2694. LoopDependenceAnalysis* analysis,
  2695. std::vector<std::vector<int>> expected_ids) {
  2696. auto subscripts_0 = analysis->GetSubscripts(instruction_0);
  2697. auto subscripts_1 = analysis->GetSubscripts(instruction_1);
  2698. std::vector<std::set<std::pair<Instruction*, Instruction*>>>
  2699. expected_partition{};
  2700. for (const auto& partition : expected_ids) {
  2701. expected_partition.push_back(
  2702. std::set<std::pair<Instruction*, Instruction*>>{});
  2703. for (auto id : partition) {
  2704. expected_partition.back().insert({subscripts_0[id], subscripts_1[id]});
  2705. }
  2706. }
  2707. EXPECT_EQ(expected_partition,
  2708. analysis->PartitionSubscripts(subscripts_0, subscripts_1));
  2709. }
  2710. /*
  2711. Generated from the following GLSL fragment shader
  2712. with --eliminate-local-multi-store
  2713. #version 440 core
  2714. void main(){
  2715. int[10][10][10][10] arr;
  2716. for (int i = 0; i < 10; i++) {
  2717. for (int j = 0; j < 10; j++) {
  2718. for (int k = 0; k < 10; k++) {
  2719. for (int l = 0; l < 10; l++) {
  2720. arr[i][j][k][l] = arr[i][j][k][l]; // 0, all independent
  2721. arr[i][j][k][l] = arr[i][j][l][0]; // 1, last 2 coupled
  2722. arr[i][j][k][l] = arr[j][i][k][l]; // 2, first 2 coupled
  2723. arr[i][j][k][l] = arr[l][j][k][i]; // 3, first & last coupled
  2724. arr[i][j][k][l] = arr[i][k][j][l]; // 4, middle 2 coupled
  2725. arr[i+j][j][k][l] = arr[i][j][k][l]; // 5, first 2 coupled
  2726. arr[i+j+k][j][k][l] = arr[i][j][k][l]; // 6, first 3 coupled
  2727. arr[i+j+k+l][j][k][l] = arr[i][j][k][l]; // 7, all 4 coupled
  2728. arr[i][j][k][l] = arr[i][l][j][k]; // 8, last 3 coupled
  2729. arr[i][j-k][k][l] = arr[i][j][l][k]; // 9, last 3 coupled
  2730. arr[i][j][k][l] = arr[l][i][j][k]; // 10, all 4 coupled
  2731. arr[i][j][k][l] = arr[j][i][l][k]; // 11, 2 coupled partitions (i,j) &
  2732. (l&k)
  2733. arr[i][j][k][l] = arr[k][l][i][j]; // 12, 2 coupled partitions (i,k) &
  2734. (j&l)
  2735. }
  2736. }
  2737. }
  2738. }
  2739. }
  2740. */
  2741. TEST(DependencyAnalysis, SubscriptPartitioning) {
  2742. const std::string text = R"(
  2743. OpCapability Shader
  2744. %1 = OpExtInstImport "GLSL.std.450"
  2745. OpMemoryModel Logical GLSL450
  2746. OpEntryPoint Fragment %4 "main"
  2747. OpExecutionMode %4 OriginUpperLeft
  2748. OpSource GLSL 440
  2749. OpName %4 "main"
  2750. OpName %8 "i"
  2751. OpName %19 "j"
  2752. OpName %27 "k"
  2753. OpName %35 "l"
  2754. OpName %50 "arr"
  2755. %2 = OpTypeVoid
  2756. %3 = OpTypeFunction %2
  2757. %6 = OpTypeInt 32 1
  2758. %7 = OpTypePointer Function %6
  2759. %9 = OpConstant %6 0
  2760. %16 = OpConstant %6 10
  2761. %17 = OpTypeBool
  2762. %43 = OpTypeInt 32 0
  2763. %44 = OpConstant %43 10
  2764. %45 = OpTypeArray %6 %44
  2765. %46 = OpTypeArray %45 %44
  2766. %47 = OpTypeArray %46 %44
  2767. %48 = OpTypeArray %47 %44
  2768. %49 = OpTypePointer Function %48
  2769. %208 = OpConstant %6 1
  2770. %217 = OpUndef %6
  2771. %4 = OpFunction %2 None %3
  2772. %5 = OpLabel
  2773. %8 = OpVariable %7 Function
  2774. %19 = OpVariable %7 Function
  2775. %27 = OpVariable %7 Function
  2776. %35 = OpVariable %7 Function
  2777. %50 = OpVariable %49 Function
  2778. OpStore %8 %9
  2779. OpBranch %10
  2780. %10 = OpLabel
  2781. %216 = OpPhi %6 %9 %5 %215 %13
  2782. %218 = OpPhi %6 %217 %5 %221 %13
  2783. %219 = OpPhi %6 %217 %5 %222 %13
  2784. %220 = OpPhi %6 %217 %5 %223 %13
  2785. OpLoopMerge %12 %13 None
  2786. OpBranch %14
  2787. %14 = OpLabel
  2788. %18 = OpSLessThan %17 %216 %16
  2789. OpBranchConditional %18 %11 %12
  2790. %11 = OpLabel
  2791. OpStore %19 %9
  2792. OpBranch %20
  2793. %20 = OpLabel
  2794. %221 = OpPhi %6 %9 %11 %213 %23
  2795. %222 = OpPhi %6 %219 %11 %224 %23
  2796. %223 = OpPhi %6 %220 %11 %225 %23
  2797. OpLoopMerge %22 %23 None
  2798. OpBranch %24
  2799. %24 = OpLabel
  2800. %26 = OpSLessThan %17 %221 %16
  2801. OpBranchConditional %26 %21 %22
  2802. %21 = OpLabel
  2803. OpStore %27 %9
  2804. OpBranch %28
  2805. %28 = OpLabel
  2806. %224 = OpPhi %6 %9 %21 %211 %31
  2807. %225 = OpPhi %6 %223 %21 %226 %31
  2808. OpLoopMerge %30 %31 None
  2809. OpBranch %32
  2810. %32 = OpLabel
  2811. %34 = OpSLessThan %17 %224 %16
  2812. OpBranchConditional %34 %29 %30
  2813. %29 = OpLabel
  2814. OpStore %35 %9
  2815. OpBranch %36
  2816. %36 = OpLabel
  2817. %226 = OpPhi %6 %9 %29 %209 %39
  2818. OpLoopMerge %38 %39 None
  2819. OpBranch %40
  2820. %40 = OpLabel
  2821. %42 = OpSLessThan %17 %226 %16
  2822. OpBranchConditional %42 %37 %38
  2823. %37 = OpLabel
  2824. %59 = OpAccessChain %7 %50 %216 %221 %224 %226
  2825. %60 = OpLoad %6 %59
  2826. %61 = OpAccessChain %7 %50 %216 %221 %224 %226
  2827. OpStore %61 %60
  2828. %69 = OpAccessChain %7 %50 %216 %221 %226 %9
  2829. %70 = OpLoad %6 %69
  2830. %71 = OpAccessChain %7 %50 %216 %221 %224 %226
  2831. OpStore %71 %70
  2832. %80 = OpAccessChain %7 %50 %221 %216 %224 %226
  2833. %81 = OpLoad %6 %80
  2834. %82 = OpAccessChain %7 %50 %216 %221 %224 %226
  2835. OpStore %82 %81
  2836. %91 = OpAccessChain %7 %50 %226 %221 %224 %216
  2837. %92 = OpLoad %6 %91
  2838. %93 = OpAccessChain %7 %50 %216 %221 %224 %226
  2839. OpStore %93 %92
  2840. %102 = OpAccessChain %7 %50 %216 %224 %221 %226
  2841. %103 = OpLoad %6 %102
  2842. %104 = OpAccessChain %7 %50 %216 %221 %224 %226
  2843. OpStore %104 %103
  2844. %107 = OpIAdd %6 %216 %221
  2845. %115 = OpAccessChain %7 %50 %216 %221 %224 %226
  2846. %116 = OpLoad %6 %115
  2847. %117 = OpAccessChain %7 %50 %107 %221 %224 %226
  2848. OpStore %117 %116
  2849. %120 = OpIAdd %6 %216 %221
  2850. %122 = OpIAdd %6 %120 %224
  2851. %130 = OpAccessChain %7 %50 %216 %221 %224 %226
  2852. %131 = OpLoad %6 %130
  2853. %132 = OpAccessChain %7 %50 %122 %221 %224 %226
  2854. OpStore %132 %131
  2855. %135 = OpIAdd %6 %216 %221
  2856. %137 = OpIAdd %6 %135 %224
  2857. %139 = OpIAdd %6 %137 %226
  2858. %147 = OpAccessChain %7 %50 %216 %221 %224 %226
  2859. %148 = OpLoad %6 %147
  2860. %149 = OpAccessChain %7 %50 %139 %221 %224 %226
  2861. OpStore %149 %148
  2862. %158 = OpAccessChain %7 %50 %216 %226 %221 %224
  2863. %159 = OpLoad %6 %158
  2864. %160 = OpAccessChain %7 %50 %216 %221 %224 %226
  2865. OpStore %160 %159
  2866. %164 = OpISub %6 %221 %224
  2867. %171 = OpAccessChain %7 %50 %216 %221 %226 %224
  2868. %172 = OpLoad %6 %171
  2869. %173 = OpAccessChain %7 %50 %216 %164 %224 %226
  2870. OpStore %173 %172
  2871. %182 = OpAccessChain %7 %50 %226 %216 %221 %224
  2872. %183 = OpLoad %6 %182
  2873. %184 = OpAccessChain %7 %50 %216 %221 %224 %226
  2874. OpStore %184 %183
  2875. %193 = OpAccessChain %7 %50 %221 %216 %226 %224
  2876. %194 = OpLoad %6 %193
  2877. %195 = OpAccessChain %7 %50 %216 %221 %224 %226
  2878. OpStore %195 %194
  2879. %204 = OpAccessChain %7 %50 %224 %226 %216 %221
  2880. %205 = OpLoad %6 %204
  2881. %206 = OpAccessChain %7 %50 %216 %221 %224 %226
  2882. OpStore %206 %205
  2883. OpBranch %39
  2884. %39 = OpLabel
  2885. %209 = OpIAdd %6 %226 %208
  2886. OpStore %35 %209
  2887. OpBranch %36
  2888. %38 = OpLabel
  2889. OpBranch %31
  2890. %31 = OpLabel
  2891. %211 = OpIAdd %6 %224 %208
  2892. OpStore %27 %211
  2893. OpBranch %28
  2894. %30 = OpLabel
  2895. OpBranch %23
  2896. %23 = OpLabel
  2897. %213 = OpIAdd %6 %221 %208
  2898. OpStore %19 %213
  2899. OpBranch %20
  2900. %22 = OpLabel
  2901. OpBranch %13
  2902. %13 = OpLabel
  2903. %215 = OpIAdd %6 %216 %208
  2904. OpStore %8 %215
  2905. OpBranch %10
  2906. %12 = OpLabel
  2907. OpReturn
  2908. OpFunctionEnd
  2909. )";
  2910. std::unique_ptr<IRContext> context =
  2911. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  2912. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2913. Module* module = context->module();
  2914. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  2915. << text << std::endl;
  2916. const Function* f = spvtest::GetFunction(module, 4);
  2917. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  2918. std::vector<const Loop*> loop_nest{
  2919. &ld.GetLoopByIndex(0), &ld.GetLoopByIndex(1), &ld.GetLoopByIndex(2),
  2920. &ld.GetLoopByIndex(3)};
  2921. LoopDependenceAnalysis analysis{context.get(), loop_nest};
  2922. const int instructions_expected = 13;
  2923. const Instruction* store[instructions_expected];
  2924. const Instruction* load[instructions_expected];
  2925. int stores_found = 0;
  2926. int loads_found = 0;
  2927. int block_id = 37;
  2928. ASSERT_TRUE(spvtest::GetBasicBlock(f, block_id));
  2929. for (const Instruction& inst : *spvtest::GetBasicBlock(f, block_id)) {
  2930. if (inst.opcode() == SpvOp::SpvOpStore) {
  2931. store[stores_found] = &inst;
  2932. ++stores_found;
  2933. }
  2934. if (inst.opcode() == SpvOp::SpvOpLoad) {
  2935. load[loads_found] = &inst;
  2936. ++loads_found;
  2937. }
  2938. }
  2939. EXPECT_EQ(instructions_expected, stores_found);
  2940. EXPECT_EQ(instructions_expected, loads_found);
  2941. PartitionSubscripts(load[0], store[0], &analysis, {{0}, {1}, {2}, {3}});
  2942. PartitionSubscripts(load[1], store[1], &analysis, {{0}, {1}, {2, 3}});
  2943. PartitionSubscripts(load[2], store[2], &analysis, {{0, 1}, {2}, {3}});
  2944. PartitionSubscripts(load[3], store[3], &analysis, {{0, 3}, {1}, {2}});
  2945. PartitionSubscripts(load[4], store[4], &analysis, {{0}, {1, 2}, {3}});
  2946. PartitionSubscripts(load[5], store[5], &analysis, {{0, 1}, {2}, {3}});
  2947. PartitionSubscripts(load[6], store[6], &analysis, {{0, 1, 2}, {3}});
  2948. PartitionSubscripts(load[7], store[7], &analysis, {{0, 1, 2, 3}});
  2949. PartitionSubscripts(load[8], store[8], &analysis, {{0}, {1, 2, 3}});
  2950. PartitionSubscripts(load[9], store[9], &analysis, {{0}, {1, 2, 3}});
  2951. PartitionSubscripts(load[10], store[10], &analysis, {{0, 1, 2, 3}});
  2952. PartitionSubscripts(load[11], store[11], &analysis, {{0, 1}, {2, 3}});
  2953. PartitionSubscripts(load[12], store[12], &analysis, {{0, 2}, {1, 3}});
  2954. }
  2955. /*
  2956. Generated from the following GLSL fragment shader
  2957. with --eliminate-local-multi-store
  2958. #version 440 core
  2959. void a() {
  2960. int[10][10] arr;
  2961. for (int i = 0; i < 10; ++i) {
  2962. for (int j = 0; j < 10; ++j) {
  2963. // Dependent, distance vector (1, -1)
  2964. arr[i+1][i+j] = arr[i][i+j];
  2965. }
  2966. }
  2967. }
  2968. void b() {
  2969. int[10][10] arr;
  2970. for (int i = 0; i < 10; ++i) {
  2971. // Independent
  2972. arr[i+1][i+2] = arr[i][i] + 2;
  2973. }
  2974. }
  2975. void c() {
  2976. int[10][10] arr;
  2977. for (int i = 0; i < 10; ++i) {
  2978. // Dependence point (1,2)
  2979. arr[i][i] = arr[1][i-1] + 2;
  2980. }
  2981. }
  2982. void d() {
  2983. int[10][10][10] arr;
  2984. for (int i = 0; i < 10; ++i) {
  2985. for (int j = 0; j < 10; ++j) {
  2986. for (int k = 0; k < 10; ++k) {
  2987. // Dependent, distance vector (1,1,-1)
  2988. arr[j-i][i+1][j+k] = arr[j-i][i][j+k];
  2989. }
  2990. }
  2991. }
  2992. }
  2993. void e() {
  2994. int[10][10] arr;
  2995. for (int i = 0; i < 10; ++i) {
  2996. for (int j = 0; j < 10; ++j) {
  2997. // Independent with GCD after propagation
  2998. arr[i][2*j+i] = arr[i][2*j-i+5];
  2999. }
  3000. }
  3001. }
  3002. void main(){
  3003. a();
  3004. b();
  3005. c();
  3006. d();
  3007. e();
  3008. }
  3009. */
  3010. TEST(DependencyAnalysis, Delta) {
  3011. const std::string text = R"(
  3012. OpCapability Shader
  3013. %1 = OpExtInstImport "GLSL.std.450"
  3014. OpMemoryModel Logical GLSL450
  3015. OpEntryPoint Fragment %4 "main"
  3016. OpExecutionMode %4 OriginUpperLeft
  3017. OpSource GLSL 440
  3018. OpName %4 "main"
  3019. OpName %6 "a("
  3020. OpName %8 "b("
  3021. OpName %10 "c("
  3022. OpName %12 "d("
  3023. OpName %14 "e("
  3024. OpName %18 "i"
  3025. OpName %29 "j"
  3026. OpName %42 "arr"
  3027. OpName %60 "i"
  3028. OpName %68 "arr"
  3029. OpName %82 "i"
  3030. OpName %90 "arr"
  3031. OpName %101 "i"
  3032. OpName %109 "j"
  3033. OpName %117 "k"
  3034. OpName %127 "arr"
  3035. OpName %152 "i"
  3036. OpName %160 "j"
  3037. OpName %168 "arr"
  3038. %2 = OpTypeVoid
  3039. %3 = OpTypeFunction %2
  3040. %16 = OpTypeInt 32 1
  3041. %17 = OpTypePointer Function %16
  3042. %19 = OpConstant %16 0
  3043. %26 = OpConstant %16 10
  3044. %27 = OpTypeBool
  3045. %37 = OpTypeInt 32 0
  3046. %38 = OpConstant %37 10
  3047. %39 = OpTypeArray %16 %38
  3048. %40 = OpTypeArray %39 %38
  3049. %41 = OpTypePointer Function %40
  3050. %44 = OpConstant %16 1
  3051. %72 = OpConstant %16 2
  3052. %125 = OpTypeArray %40 %38
  3053. %126 = OpTypePointer Function %125
  3054. %179 = OpConstant %16 5
  3055. %4 = OpFunction %2 None %3
  3056. %5 = OpLabel
  3057. %188 = OpFunctionCall %2 %6
  3058. %189 = OpFunctionCall %2 %8
  3059. %190 = OpFunctionCall %2 %10
  3060. %191 = OpFunctionCall %2 %12
  3061. %192 = OpFunctionCall %2 %14
  3062. OpReturn
  3063. OpFunctionEnd
  3064. %6 = OpFunction %2 None %3
  3065. %7 = OpLabel
  3066. %18 = OpVariable %17 Function
  3067. %29 = OpVariable %17 Function
  3068. %42 = OpVariable %41 Function
  3069. OpStore %18 %19
  3070. OpBranch %20
  3071. %20 = OpLabel
  3072. %193 = OpPhi %16 %19 %7 %59 %23
  3073. OpLoopMerge %22 %23 None
  3074. OpBranch %24
  3075. %24 = OpLabel
  3076. %28 = OpSLessThan %27 %193 %26
  3077. OpBranchConditional %28 %21 %22
  3078. %21 = OpLabel
  3079. OpStore %29 %19
  3080. OpBranch %30
  3081. %30 = OpLabel
  3082. %194 = OpPhi %16 %19 %21 %57 %33
  3083. OpLoopMerge %32 %33 None
  3084. OpBranch %34
  3085. %34 = OpLabel
  3086. %36 = OpSLessThan %27 %194 %26
  3087. OpBranchConditional %36 %31 %32
  3088. %31 = OpLabel
  3089. %45 = OpIAdd %16 %193 %44
  3090. %48 = OpIAdd %16 %193 %194
  3091. %52 = OpIAdd %16 %193 %194
  3092. %53 = OpAccessChain %17 %42 %193 %52
  3093. %54 = OpLoad %16 %53
  3094. %55 = OpAccessChain %17 %42 %45 %48
  3095. OpStore %55 %54
  3096. OpBranch %33
  3097. %33 = OpLabel
  3098. %57 = OpIAdd %16 %194 %44
  3099. OpStore %29 %57
  3100. OpBranch %30
  3101. %32 = OpLabel
  3102. OpBranch %23
  3103. %23 = OpLabel
  3104. %59 = OpIAdd %16 %193 %44
  3105. OpStore %18 %59
  3106. OpBranch %20
  3107. %22 = OpLabel
  3108. OpReturn
  3109. OpFunctionEnd
  3110. %8 = OpFunction %2 None %3
  3111. %9 = OpLabel
  3112. %60 = OpVariable %17 Function
  3113. %68 = OpVariable %41 Function
  3114. OpStore %60 %19
  3115. OpBranch %61
  3116. %61 = OpLabel
  3117. %196 = OpPhi %16 %19 %9 %81 %64
  3118. OpLoopMerge %63 %64 None
  3119. OpBranch %65
  3120. %65 = OpLabel
  3121. %67 = OpSLessThan %27 %196 %26
  3122. OpBranchConditional %67 %62 %63
  3123. %62 = OpLabel
  3124. %70 = OpIAdd %16 %196 %44
  3125. %73 = OpIAdd %16 %196 %72
  3126. %76 = OpAccessChain %17 %68 %196 %196
  3127. %77 = OpLoad %16 %76
  3128. %78 = OpIAdd %16 %77 %72
  3129. %79 = OpAccessChain %17 %68 %70 %73
  3130. OpStore %79 %78
  3131. OpBranch %64
  3132. %64 = OpLabel
  3133. %81 = OpIAdd %16 %196 %44
  3134. OpStore %60 %81
  3135. OpBranch %61
  3136. %63 = OpLabel
  3137. OpReturn
  3138. OpFunctionEnd
  3139. %10 = OpFunction %2 None %3
  3140. %11 = OpLabel
  3141. %82 = OpVariable %17 Function
  3142. %90 = OpVariable %41 Function
  3143. OpStore %82 %19
  3144. OpBranch %83
  3145. %83 = OpLabel
  3146. %197 = OpPhi %16 %19 %11 %100 %86
  3147. OpLoopMerge %85 %86 None
  3148. OpBranch %87
  3149. %87 = OpLabel
  3150. %89 = OpSLessThan %27 %197 %26
  3151. OpBranchConditional %89 %84 %85
  3152. %84 = OpLabel
  3153. %94 = OpISub %16 %197 %44
  3154. %95 = OpAccessChain %17 %90 %44 %94
  3155. %96 = OpLoad %16 %95
  3156. %97 = OpIAdd %16 %96 %72
  3157. %98 = OpAccessChain %17 %90 %197 %197
  3158. OpStore %98 %97
  3159. OpBranch %86
  3160. %86 = OpLabel
  3161. %100 = OpIAdd %16 %197 %44
  3162. OpStore %82 %100
  3163. OpBranch %83
  3164. %85 = OpLabel
  3165. OpReturn
  3166. OpFunctionEnd
  3167. %12 = OpFunction %2 None %3
  3168. %13 = OpLabel
  3169. %101 = OpVariable %17 Function
  3170. %109 = OpVariable %17 Function
  3171. %117 = OpVariable %17 Function
  3172. %127 = OpVariable %126 Function
  3173. OpStore %101 %19
  3174. OpBranch %102
  3175. %102 = OpLabel
  3176. %198 = OpPhi %16 %19 %13 %151 %105
  3177. OpLoopMerge %104 %105 None
  3178. OpBranch %106
  3179. %106 = OpLabel
  3180. %108 = OpSLessThan %27 %198 %26
  3181. OpBranchConditional %108 %103 %104
  3182. %103 = OpLabel
  3183. OpStore %109 %19
  3184. OpBranch %110
  3185. %110 = OpLabel
  3186. %199 = OpPhi %16 %19 %103 %149 %113
  3187. OpLoopMerge %112 %113 None
  3188. OpBranch %114
  3189. %114 = OpLabel
  3190. %116 = OpSLessThan %27 %199 %26
  3191. OpBranchConditional %116 %111 %112
  3192. %111 = OpLabel
  3193. OpStore %117 %19
  3194. OpBranch %118
  3195. %118 = OpLabel
  3196. %201 = OpPhi %16 %19 %111 %147 %121
  3197. OpLoopMerge %120 %121 None
  3198. OpBranch %122
  3199. %122 = OpLabel
  3200. %124 = OpSLessThan %27 %201 %26
  3201. OpBranchConditional %124 %119 %120
  3202. %119 = OpLabel
  3203. %130 = OpISub %16 %199 %198
  3204. %132 = OpIAdd %16 %198 %44
  3205. %135 = OpIAdd %16 %199 %201
  3206. %138 = OpISub %16 %199 %198
  3207. %142 = OpIAdd %16 %199 %201
  3208. %143 = OpAccessChain %17 %127 %138 %198 %142
  3209. %144 = OpLoad %16 %143
  3210. %145 = OpAccessChain %17 %127 %130 %132 %135
  3211. OpStore %145 %144
  3212. OpBranch %121
  3213. %121 = OpLabel
  3214. %147 = OpIAdd %16 %201 %44
  3215. OpStore %117 %147
  3216. OpBranch %118
  3217. %120 = OpLabel
  3218. OpBranch %113
  3219. %113 = OpLabel
  3220. %149 = OpIAdd %16 %199 %44
  3221. OpStore %109 %149
  3222. OpBranch %110
  3223. %112 = OpLabel
  3224. OpBranch %105
  3225. %105 = OpLabel
  3226. %151 = OpIAdd %16 %198 %44
  3227. OpStore %101 %151
  3228. OpBranch %102
  3229. %104 = OpLabel
  3230. OpReturn
  3231. OpFunctionEnd
  3232. %14 = OpFunction %2 None %3
  3233. %15 = OpLabel
  3234. %152 = OpVariable %17 Function
  3235. %160 = OpVariable %17 Function
  3236. %168 = OpVariable %41 Function
  3237. OpStore %152 %19
  3238. OpBranch %153
  3239. %153 = OpLabel
  3240. %204 = OpPhi %16 %19 %15 %187 %156
  3241. OpLoopMerge %155 %156 None
  3242. OpBranch %157
  3243. %157 = OpLabel
  3244. %159 = OpSLessThan %27 %204 %26
  3245. OpBranchConditional %159 %154 %155
  3246. %154 = OpLabel
  3247. OpStore %160 %19
  3248. OpBranch %161
  3249. %161 = OpLabel
  3250. %205 = OpPhi %16 %19 %154 %185 %164
  3251. OpLoopMerge %163 %164 None
  3252. OpBranch %165
  3253. %165 = OpLabel
  3254. %167 = OpSLessThan %27 %205 %26
  3255. OpBranchConditional %167 %162 %163
  3256. %162 = OpLabel
  3257. %171 = OpIMul %16 %72 %205
  3258. %173 = OpIAdd %16 %171 %204
  3259. %176 = OpIMul %16 %72 %205
  3260. %178 = OpISub %16 %176 %204
  3261. %180 = OpIAdd %16 %178 %179
  3262. %181 = OpAccessChain %17 %168 %204 %180
  3263. %182 = OpLoad %16 %181
  3264. %183 = OpAccessChain %17 %168 %204 %173
  3265. OpStore %183 %182
  3266. OpBranch %164
  3267. %164 = OpLabel
  3268. %185 = OpIAdd %16 %205 %44
  3269. OpStore %160 %185
  3270. OpBranch %161
  3271. %163 = OpLabel
  3272. OpBranch %156
  3273. %156 = OpLabel
  3274. %187 = OpIAdd %16 %204 %44
  3275. OpStore %152 %187
  3276. OpBranch %153
  3277. %155 = OpLabel
  3278. OpReturn
  3279. OpFunctionEnd
  3280. )";
  3281. std::unique_ptr<IRContext> context =
  3282. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  3283. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  3284. ASSERT_NE(nullptr, context);
  3285. Module* module = context->module();
  3286. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  3287. << text << std::endl;
  3288. {
  3289. const Function* f = spvtest::GetFunction(module, 6);
  3290. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  3291. const Instruction* store = nullptr;
  3292. const Instruction* load = nullptr;
  3293. int block_id = 31;
  3294. ASSERT_TRUE(spvtest::GetBasicBlock(f, block_id));
  3295. for (const Instruction& inst : *spvtest::GetBasicBlock(f, block_id)) {
  3296. if (inst.opcode() == SpvOp::SpvOpStore) {
  3297. store = &inst;
  3298. }
  3299. if (inst.opcode() == SpvOp::SpvOpLoad) {
  3300. load = &inst;
  3301. }
  3302. }
  3303. EXPECT_NE(nullptr, store);
  3304. EXPECT_NE(nullptr, load);
  3305. std::vector<const Loop*> loop_nest{&ld.GetLoopByIndex(0),
  3306. &ld.GetLoopByIndex(1)};
  3307. LoopDependenceAnalysis analysis{context.get(), loop_nest};
  3308. DistanceVector dv_entry(loop_nest.size());
  3309. std::vector<DistanceEntry> expected_entries{
  3310. DistanceEntry(DistanceEntry::Directions::LT, 1),
  3311. DistanceEntry(DistanceEntry::Directions::LT, 1)};
  3312. DistanceVector expected_distance_vector(expected_entries);
  3313. auto is_independent = analysis.GetDependence(load, store, &dv_entry);
  3314. EXPECT_FALSE(is_independent);
  3315. EXPECT_EQ(expected_distance_vector, dv_entry);
  3316. }
  3317. {
  3318. const Function* f = spvtest::GetFunction(module, 8);
  3319. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  3320. const Instruction* store = nullptr;
  3321. const Instruction* load = nullptr;
  3322. int block_id = 62;
  3323. ASSERT_TRUE(spvtest::GetBasicBlock(f, block_id));
  3324. for (const Instruction& inst : *spvtest::GetBasicBlock(f, block_id)) {
  3325. if (inst.opcode() == SpvOp::SpvOpStore) {
  3326. store = &inst;
  3327. }
  3328. if (inst.opcode() == SpvOp::SpvOpLoad) {
  3329. load = &inst;
  3330. }
  3331. }
  3332. EXPECT_NE(nullptr, store);
  3333. EXPECT_NE(nullptr, load);
  3334. std::vector<const Loop*> loop_nest{&ld.GetLoopByIndex(0)};
  3335. LoopDependenceAnalysis analysis{context.get(), loop_nest};
  3336. DistanceVector dv_entry(loop_nest.size());
  3337. auto is_independent = analysis.GetDependence(load, store, &dv_entry);
  3338. EXPECT_TRUE(is_independent);
  3339. }
  3340. {
  3341. const Function* f = spvtest::GetFunction(module, 10);
  3342. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  3343. const Instruction* store = nullptr;
  3344. const Instruction* load = nullptr;
  3345. int block_id = 84;
  3346. ASSERT_TRUE(spvtest::GetBasicBlock(f, block_id));
  3347. for (const Instruction& inst : *spvtest::GetBasicBlock(f, block_id)) {
  3348. if (inst.opcode() == SpvOp::SpvOpStore) {
  3349. store = &inst;
  3350. }
  3351. if (inst.opcode() == SpvOp::SpvOpLoad) {
  3352. load = &inst;
  3353. }
  3354. }
  3355. EXPECT_NE(nullptr, store);
  3356. EXPECT_NE(nullptr, load);
  3357. std::vector<const Loop*> loop_nest{&ld.GetLoopByIndex(0)};
  3358. LoopDependenceAnalysis analysis{context.get(), loop_nest};
  3359. DistanceVector dv_entry(loop_nest.size());
  3360. auto is_independent = analysis.GetDependence(load, store, &dv_entry);
  3361. DistanceVector expected_distance_vector({DistanceEntry(1, 2)});
  3362. EXPECT_FALSE(is_independent);
  3363. EXPECT_EQ(expected_distance_vector, dv_entry);
  3364. }
  3365. {
  3366. const Function* f = spvtest::GetFunction(module, 12);
  3367. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  3368. const Instruction* store = nullptr;
  3369. const Instruction* load = nullptr;
  3370. int block_id = 119;
  3371. ASSERT_TRUE(spvtest::GetBasicBlock(f, block_id));
  3372. for (const Instruction& inst : *spvtest::GetBasicBlock(f, block_id)) {
  3373. if (inst.opcode() == SpvOp::SpvOpStore) {
  3374. store = &inst;
  3375. }
  3376. if (inst.opcode() == SpvOp::SpvOpLoad) {
  3377. load = &inst;
  3378. }
  3379. }
  3380. EXPECT_NE(nullptr, store);
  3381. EXPECT_NE(nullptr, load);
  3382. std::vector<const Loop*> loop_nest{
  3383. &ld.GetLoopByIndex(0), &ld.GetLoopByIndex(1), &ld.GetLoopByIndex(2)};
  3384. LoopDependenceAnalysis analysis{context.get(), loop_nest};
  3385. DistanceVector dv_entry(loop_nest.size());
  3386. std::vector<DistanceEntry> expected_entries{
  3387. DistanceEntry(DistanceEntry::Directions::LT, 1),
  3388. DistanceEntry(DistanceEntry::Directions::LT, 1),
  3389. DistanceEntry(DistanceEntry::Directions::GT, -1)};
  3390. DistanceVector expected_distance_vector(expected_entries);
  3391. auto is_independent = analysis.GetDependence(store, load, &dv_entry);
  3392. EXPECT_FALSE(is_independent);
  3393. EXPECT_EQ(expected_distance_vector, dv_entry);
  3394. }
  3395. {
  3396. const Function* f = spvtest::GetFunction(module, 14);
  3397. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  3398. const Instruction* store = nullptr;
  3399. const Instruction* load = nullptr;
  3400. int block_id = 162;
  3401. ASSERT_TRUE(spvtest::GetBasicBlock(f, block_id));
  3402. for (const Instruction& inst : *spvtest::GetBasicBlock(f, block_id)) {
  3403. if (inst.opcode() == SpvOp::SpvOpStore) {
  3404. store = &inst;
  3405. }
  3406. if (inst.opcode() == SpvOp::SpvOpLoad) {
  3407. load = &inst;
  3408. }
  3409. }
  3410. EXPECT_NE(nullptr, store);
  3411. EXPECT_NE(nullptr, load);
  3412. std::vector<const Loop*> loop_nest{&ld.GetLoopByIndex(0),
  3413. &ld.GetLoopByIndex(1)};
  3414. LoopDependenceAnalysis analysis{context.get(), loop_nest};
  3415. DistanceVector dv_entry(loop_nest.size());
  3416. auto is_independent = analysis.GetDependence(load, store, &dv_entry);
  3417. EXPECT_TRUE(is_independent);
  3418. }
  3419. }
  3420. TEST(DependencyAnalysis, ConstraintIntersection) {
  3421. LoopDependenceAnalysis analysis{nullptr, std::vector<const Loop*>{}};
  3422. auto scalar_evolution = analysis.GetScalarEvolution();
  3423. {
  3424. // One is none. Other should be returned
  3425. auto none = analysis.make_constraint<DependenceNone>();
  3426. auto x = scalar_evolution->CreateConstant(1);
  3427. auto y = scalar_evolution->CreateConstant(10);
  3428. auto point = analysis.make_constraint<DependencePoint>(x, y, nullptr);
  3429. auto ret_0 = analysis.IntersectConstraints(none, point, nullptr, nullptr);
  3430. auto ret_point_0 = ret_0->AsDependencePoint();
  3431. ASSERT_NE(nullptr, ret_point_0);
  3432. EXPECT_EQ(*x, *ret_point_0->GetSource());
  3433. EXPECT_EQ(*y, *ret_point_0->GetDestination());
  3434. auto ret_1 = analysis.IntersectConstraints(point, none, nullptr, nullptr);
  3435. auto ret_point_1 = ret_1->AsDependencePoint();
  3436. ASSERT_NE(nullptr, ret_point_1);
  3437. EXPECT_EQ(*x, *ret_point_1->GetSource());
  3438. EXPECT_EQ(*y, *ret_point_1->GetDestination());
  3439. }
  3440. {
  3441. // Both distances
  3442. auto x = scalar_evolution->CreateConstant(1);
  3443. auto y = scalar_evolution->CreateConstant(10);
  3444. auto distance_0 = analysis.make_constraint<DependenceDistance>(x, nullptr);
  3445. auto distance_1 = analysis.make_constraint<DependenceDistance>(y, nullptr);
  3446. // Equal distances
  3447. auto ret_0 =
  3448. analysis.IntersectConstraints(distance_1, distance_1, nullptr, nullptr);
  3449. auto ret_distance = ret_0->AsDependenceDistance();
  3450. ASSERT_NE(nullptr, ret_distance);
  3451. EXPECT_EQ(*y, *ret_distance->GetDistance());
  3452. // Non-equal distances
  3453. auto ret_1 =
  3454. analysis.IntersectConstraints(distance_0, distance_1, nullptr, nullptr);
  3455. EXPECT_NE(nullptr, ret_1->AsDependenceEmpty());
  3456. }
  3457. {
  3458. // Both points
  3459. auto x = scalar_evolution->CreateConstant(1);
  3460. auto y = scalar_evolution->CreateConstant(10);
  3461. auto point_0 = analysis.make_constraint<DependencePoint>(x, y, nullptr);
  3462. auto point_1 = analysis.make_constraint<DependencePoint>(x, y, nullptr);
  3463. auto point_2 = analysis.make_constraint<DependencePoint>(y, y, nullptr);
  3464. // Equal points
  3465. auto ret_0 =
  3466. analysis.IntersectConstraints(point_0, point_1, nullptr, nullptr);
  3467. auto ret_point_0 = ret_0->AsDependencePoint();
  3468. ASSERT_NE(nullptr, ret_point_0);
  3469. EXPECT_EQ(*x, *ret_point_0->GetSource());
  3470. EXPECT_EQ(*y, *ret_point_0->GetDestination());
  3471. // Non-equal points
  3472. auto ret_1 =
  3473. analysis.IntersectConstraints(point_0, point_2, nullptr, nullptr);
  3474. EXPECT_NE(nullptr, ret_1->AsDependenceEmpty());
  3475. }
  3476. {
  3477. // Both lines, parallel
  3478. auto a0 = scalar_evolution->CreateConstant(3);
  3479. auto b0 = scalar_evolution->CreateConstant(6);
  3480. auto c0 = scalar_evolution->CreateConstant(9);
  3481. auto a1 = scalar_evolution->CreateConstant(6);
  3482. auto b1 = scalar_evolution->CreateConstant(12);
  3483. auto c1 = scalar_evolution->CreateConstant(18);
  3484. auto line_0 = analysis.make_constraint<DependenceLine>(a0, b0, c0, nullptr);
  3485. auto line_1 = analysis.make_constraint<DependenceLine>(a1, b1, c1, nullptr);
  3486. // Same line, both ways
  3487. auto ret_0 =
  3488. analysis.IntersectConstraints(line_0, line_1, nullptr, nullptr);
  3489. auto ret_1 =
  3490. analysis.IntersectConstraints(line_1, line_0, nullptr, nullptr);
  3491. auto ret_line_0 = ret_0->AsDependenceLine();
  3492. auto ret_line_1 = ret_1->AsDependenceLine();
  3493. EXPECT_NE(nullptr, ret_line_0);
  3494. EXPECT_NE(nullptr, ret_line_1);
  3495. // Non-intersecting parallel lines
  3496. auto c2 = scalar_evolution->CreateConstant(12);
  3497. auto line_2 = analysis.make_constraint<DependenceLine>(a1, b1, c2, nullptr);
  3498. auto ret_2 =
  3499. analysis.IntersectConstraints(line_0, line_2, nullptr, nullptr);
  3500. auto ret_3 =
  3501. analysis.IntersectConstraints(line_2, line_0, nullptr, nullptr);
  3502. EXPECT_NE(nullptr, ret_2->AsDependenceEmpty());
  3503. EXPECT_NE(nullptr, ret_3->AsDependenceEmpty());
  3504. auto c3 = scalar_evolution->CreateConstant(20);
  3505. auto line_3 = analysis.make_constraint<DependenceLine>(a1, b1, c3, nullptr);
  3506. auto ret_4 =
  3507. analysis.IntersectConstraints(line_0, line_3, nullptr, nullptr);
  3508. auto ret_5 =
  3509. analysis.IntersectConstraints(line_3, line_0, nullptr, nullptr);
  3510. EXPECT_NE(nullptr, ret_4->AsDependenceEmpty());
  3511. EXPECT_NE(nullptr, ret_5->AsDependenceEmpty());
  3512. }
  3513. {
  3514. // Non-constant line
  3515. auto unknown = scalar_evolution->CreateCantComputeNode();
  3516. auto constant = scalar_evolution->CreateConstant(10);
  3517. auto line_0 = analysis.make_constraint<DependenceLine>(constant, constant,
  3518. constant, nullptr);
  3519. auto line_1 = analysis.make_constraint<DependenceLine>(unknown, unknown,
  3520. unknown, nullptr);
  3521. auto ret_0 =
  3522. analysis.IntersectConstraints(line_0, line_1, nullptr, nullptr);
  3523. auto ret_1 =
  3524. analysis.IntersectConstraints(line_1, line_0, nullptr, nullptr);
  3525. EXPECT_NE(nullptr, ret_0->AsDependenceNone());
  3526. EXPECT_NE(nullptr, ret_1->AsDependenceNone());
  3527. }
  3528. {
  3529. auto bound_0 = scalar_evolution->CreateConstant(0);
  3530. auto bound_1 = scalar_evolution->CreateConstant(20);
  3531. auto a0 = scalar_evolution->CreateConstant(1);
  3532. auto b0 = scalar_evolution->CreateConstant(2);
  3533. auto c0 = scalar_evolution->CreateConstant(6);
  3534. auto a1 = scalar_evolution->CreateConstant(-1);
  3535. auto b1 = scalar_evolution->CreateConstant(2);
  3536. auto c1 = scalar_evolution->CreateConstant(2);
  3537. auto line_0 = analysis.make_constraint<DependenceLine>(a0, b0, c0, nullptr);
  3538. auto line_1 = analysis.make_constraint<DependenceLine>(a1, b1, c1, nullptr);
  3539. // Intersecting lines, has integer solution, in bounds
  3540. auto ret_0 =
  3541. analysis.IntersectConstraints(line_0, line_1, bound_0, bound_1);
  3542. auto ret_1 =
  3543. analysis.IntersectConstraints(line_1, line_0, bound_0, bound_1);
  3544. auto ret_point_0 = ret_0->AsDependencePoint();
  3545. auto ret_point_1 = ret_1->AsDependencePoint();
  3546. EXPECT_NE(nullptr, ret_point_0);
  3547. EXPECT_NE(nullptr, ret_point_1);
  3548. auto const_2 = scalar_evolution->CreateConstant(2);
  3549. EXPECT_EQ(*const_2, *ret_point_0->GetSource());
  3550. EXPECT_EQ(*const_2, *ret_point_0->GetDestination());
  3551. EXPECT_EQ(*const_2, *ret_point_1->GetSource());
  3552. EXPECT_EQ(*const_2, *ret_point_1->GetDestination());
  3553. // Intersecting lines, has integer solution, out of bounds
  3554. auto ret_2 =
  3555. analysis.IntersectConstraints(line_0, line_1, bound_0, bound_0);
  3556. auto ret_3 =
  3557. analysis.IntersectConstraints(line_1, line_0, bound_0, bound_0);
  3558. EXPECT_NE(nullptr, ret_2->AsDependenceEmpty());
  3559. EXPECT_NE(nullptr, ret_3->AsDependenceEmpty());
  3560. auto a2 = scalar_evolution->CreateConstant(-4);
  3561. auto b2 = scalar_evolution->CreateConstant(1);
  3562. auto c2 = scalar_evolution->CreateConstant(0);
  3563. auto a3 = scalar_evolution->CreateConstant(4);
  3564. auto b3 = scalar_evolution->CreateConstant(1);
  3565. auto c3 = scalar_evolution->CreateConstant(4);
  3566. auto line_2 = analysis.make_constraint<DependenceLine>(a2, b2, c2, nullptr);
  3567. auto line_3 = analysis.make_constraint<DependenceLine>(a3, b3, c3, nullptr);
  3568. // Intersecting, no integer solution
  3569. auto ret_4 =
  3570. analysis.IntersectConstraints(line_2, line_3, bound_0, bound_1);
  3571. auto ret_5 =
  3572. analysis.IntersectConstraints(line_3, line_2, bound_0, bound_1);
  3573. EXPECT_NE(nullptr, ret_4->AsDependenceEmpty());
  3574. EXPECT_NE(nullptr, ret_5->AsDependenceEmpty());
  3575. auto unknown = scalar_evolution->CreateCantComputeNode();
  3576. // Non-constant bound
  3577. auto ret_6 =
  3578. analysis.IntersectConstraints(line_0, line_1, unknown, bound_1);
  3579. auto ret_7 =
  3580. analysis.IntersectConstraints(line_1, line_0, bound_0, unknown);
  3581. EXPECT_NE(nullptr, ret_6->AsDependenceNone());
  3582. EXPECT_NE(nullptr, ret_7->AsDependenceNone());
  3583. }
  3584. {
  3585. auto constant_0 = scalar_evolution->CreateConstant(0);
  3586. auto constant_1 = scalar_evolution->CreateConstant(1);
  3587. auto constant_neg_1 = scalar_evolution->CreateConstant(-1);
  3588. auto constant_2 = scalar_evolution->CreateConstant(2);
  3589. auto constant_neg_2 = scalar_evolution->CreateConstant(-2);
  3590. auto point_0_0 = analysis.make_constraint<DependencePoint>(
  3591. constant_0, constant_0, nullptr);
  3592. auto point_0_1 = analysis.make_constraint<DependencePoint>(
  3593. constant_0, constant_1, nullptr);
  3594. auto point_1_0 = analysis.make_constraint<DependencePoint>(
  3595. constant_1, constant_0, nullptr);
  3596. auto point_1_1 = analysis.make_constraint<DependencePoint>(
  3597. constant_1, constant_1, nullptr);
  3598. auto point_1_2 = analysis.make_constraint<DependencePoint>(
  3599. constant_1, constant_2, nullptr);
  3600. auto point_1_neg_1 = analysis.make_constraint<DependencePoint>(
  3601. constant_1, constant_neg_1, nullptr);
  3602. auto point_neg_1_1 = analysis.make_constraint<DependencePoint>(
  3603. constant_neg_1, constant_1, nullptr);
  3604. auto line_y_0 = analysis.make_constraint<DependenceLine>(
  3605. constant_0, constant_1, constant_0, nullptr);
  3606. auto line_y_1 = analysis.make_constraint<DependenceLine>(
  3607. constant_0, constant_1, constant_1, nullptr);
  3608. auto line_y_2 = analysis.make_constraint<DependenceLine>(
  3609. constant_0, constant_1, constant_2, nullptr);
  3610. // Parallel horizontal lines, y = 0 & y = 1, should return no intersection
  3611. auto ret =
  3612. analysis.IntersectConstraints(line_y_0, line_y_1, nullptr, nullptr);
  3613. EXPECT_NE(nullptr, ret->AsDependenceEmpty());
  3614. // Parallel horizontal lines, y = 1 & y = 2, should return no intersection
  3615. auto ret_y_12 =
  3616. analysis.IntersectConstraints(line_y_1, line_y_2, nullptr, nullptr);
  3617. EXPECT_NE(nullptr, ret_y_12->AsDependenceEmpty());
  3618. // Same horizontal lines, y = 0 & y = 0, should return the line
  3619. auto ret_y_same_0 =
  3620. analysis.IntersectConstraints(line_y_0, line_y_0, nullptr, nullptr);
  3621. EXPECT_NE(nullptr, ret_y_same_0->AsDependenceLine());
  3622. // Same horizontal lines, y = 1 & y = 1, should return the line
  3623. auto ret_y_same_1 =
  3624. analysis.IntersectConstraints(line_y_1, line_y_1, nullptr, nullptr);
  3625. EXPECT_NE(nullptr, ret_y_same_1->AsDependenceLine());
  3626. auto line_x_0 = analysis.make_constraint<DependenceLine>(
  3627. constant_1, constant_0, constant_0, nullptr);
  3628. auto line_x_1 = analysis.make_constraint<DependenceLine>(
  3629. constant_1, constant_0, constant_1, nullptr);
  3630. auto line_x_2 = analysis.make_constraint<DependenceLine>(
  3631. constant_1, constant_0, constant_2, nullptr);
  3632. auto line_2x_1 = analysis.make_constraint<DependenceLine>(
  3633. constant_2, constant_0, constant_1, nullptr);
  3634. auto line_2x_2 = analysis.make_constraint<DependenceLine>(
  3635. constant_2, constant_0, constant_2, nullptr);
  3636. // Parallel vertical lines, x = 0 & x = 1, should return no intersection
  3637. auto ret_x =
  3638. analysis.IntersectConstraints(line_x_0, line_x_1, nullptr, nullptr);
  3639. EXPECT_NE(nullptr, ret_x->AsDependenceEmpty());
  3640. // Parallel vertical lines, x = 1 & x = 2, should return no intersection
  3641. auto ret_x_12 =
  3642. analysis.IntersectConstraints(line_x_1, line_x_2, nullptr, nullptr);
  3643. EXPECT_NE(nullptr, ret_x_12->AsDependenceEmpty());
  3644. // Parallel vertical lines, 2x = 1 & 2x = 2, should return no intersection
  3645. auto ret_2x_2_2x_1 =
  3646. analysis.IntersectConstraints(line_2x_2, line_2x_1, nullptr, nullptr);
  3647. EXPECT_NE(nullptr, ret_2x_2_2x_1->AsDependenceEmpty());
  3648. // same line, 2x=2 & x = 1
  3649. auto ret_2x_2_x_1 =
  3650. analysis.IntersectConstraints(line_2x_2, line_x_1, nullptr, nullptr);
  3651. EXPECT_NE(nullptr, ret_2x_2_x_1->AsDependenceLine());
  3652. // Same vertical lines, x = 0 & x = 0, should return the line
  3653. auto ret_x_same_0 =
  3654. analysis.IntersectConstraints(line_x_0, line_x_0, nullptr, nullptr);
  3655. EXPECT_NE(nullptr, ret_x_same_0->AsDependenceLine());
  3656. // EXPECT_EQ(*line_x_0, *ret_x_same_0->AsDependenceLine());
  3657. // Same vertical lines, x = 1 & x = 1, should return the line
  3658. auto ret_x_same_1 =
  3659. analysis.IntersectConstraints(line_x_1, line_x_1, nullptr, nullptr);
  3660. EXPECT_NE(nullptr, ret_x_same_1->AsDependenceLine());
  3661. EXPECT_EQ(*line_x_1, *ret_x_same_1->AsDependenceLine());
  3662. // x=1 & y = 0, intersect at (1, 0)
  3663. auto ret_1_0 = analysis.IntersectConstraints(line_x_1, line_y_0,
  3664. constant_neg_1, constant_2);
  3665. auto ret_point_1_0 = ret_1_0->AsDependencePoint();
  3666. EXPECT_NE(nullptr, ret_point_1_0);
  3667. EXPECT_EQ(*point_1_0, *ret_point_1_0);
  3668. // x=1 & y = 1, intersect at (1, 1)
  3669. auto ret_1_1 = analysis.IntersectConstraints(line_x_1, line_y_1,
  3670. constant_neg_1, constant_2);
  3671. auto ret_point_1_1 = ret_1_1->AsDependencePoint();
  3672. EXPECT_NE(nullptr, ret_point_1_1);
  3673. EXPECT_EQ(*point_1_1, *ret_point_1_1);
  3674. // x=0 & y = 0, intersect at (0, 0)
  3675. auto ret_0_0 = analysis.IntersectConstraints(line_x_0, line_y_0,
  3676. constant_neg_1, constant_2);
  3677. auto ret_point_0_0 = ret_0_0->AsDependencePoint();
  3678. EXPECT_NE(nullptr, ret_point_0_0);
  3679. EXPECT_EQ(*point_0_0, *ret_point_0_0);
  3680. // x=0 & y = 1, intersect at (0, 1)
  3681. auto ret_0_1 = analysis.IntersectConstraints(line_x_0, line_y_1,
  3682. constant_neg_1, constant_2);
  3683. auto ret_point_0_1 = ret_0_1->AsDependencePoint();
  3684. EXPECT_NE(nullptr, ret_point_0_1);
  3685. EXPECT_EQ(*point_0_1, *ret_point_0_1);
  3686. // x = 1 & y = 2
  3687. auto ret_1_2 = analysis.IntersectConstraints(line_x_1, line_y_2,
  3688. constant_neg_1, constant_2);
  3689. auto ret_point_1_2 = ret_1_2->AsDependencePoint();
  3690. EXPECT_NE(nullptr, ret_point_1_2);
  3691. EXPECT_EQ(*point_1_2, *ret_point_1_2);
  3692. auto line_x_y_0 = analysis.make_constraint<DependenceLine>(
  3693. constant_1, constant_1, constant_0, nullptr);
  3694. auto line_x_y_1 = analysis.make_constraint<DependenceLine>(
  3695. constant_1, constant_1, constant_1, nullptr);
  3696. // x+y=0 & x=0, intersect (0, 0)
  3697. auto ret_xy_0_x_0 = analysis.IntersectConstraints(
  3698. line_x_y_0, line_x_0, constant_neg_1, constant_2);
  3699. EXPECT_NE(nullptr, ret_xy_0_x_0->AsDependencePoint());
  3700. EXPECT_EQ(*point_0_0, *ret_xy_0_x_0);
  3701. // x+y=0 & y=0, intersect (0, 0)
  3702. auto ret_xy_0_y_0 = analysis.IntersectConstraints(
  3703. line_x_y_0, line_y_0, constant_neg_1, constant_2);
  3704. EXPECT_NE(nullptr, ret_xy_0_y_0->AsDependencePoint());
  3705. EXPECT_EQ(*point_0_0, *ret_xy_0_y_0);
  3706. // x+y=0 & x=1, intersect (1, -1)
  3707. auto ret_xy_0_x_1 = analysis.IntersectConstraints(
  3708. line_x_y_0, line_x_1, constant_neg_2, constant_2);
  3709. EXPECT_NE(nullptr, ret_xy_0_x_1->AsDependencePoint());
  3710. EXPECT_EQ(*point_1_neg_1, *ret_xy_0_x_1);
  3711. // x+y=0 & y=1, intersect (-1, 1)
  3712. auto ret_xy_0_y_1 = analysis.IntersectConstraints(
  3713. line_x_y_0, line_y_1, constant_neg_2, constant_2);
  3714. EXPECT_NE(nullptr, ret_xy_0_y_1->AsDependencePoint());
  3715. EXPECT_EQ(*point_neg_1_1, *ret_xy_0_y_1);
  3716. // x=0 & x+y=0, intersect (0, 0)
  3717. auto ret_x_0_xy_0 = analysis.IntersectConstraints(
  3718. line_x_0, line_x_y_0, constant_neg_1, constant_2);
  3719. EXPECT_NE(nullptr, ret_x_0_xy_0->AsDependencePoint());
  3720. EXPECT_EQ(*point_0_0, *ret_x_0_xy_0);
  3721. // y=0 & x+y=0, intersect (0, 0)
  3722. auto ret_y_0_xy_0 = analysis.IntersectConstraints(
  3723. line_y_0, line_x_y_0, constant_neg_1, constant_2);
  3724. EXPECT_NE(nullptr, ret_y_0_xy_0->AsDependencePoint());
  3725. EXPECT_EQ(*point_0_0, *ret_y_0_xy_0);
  3726. // x=1 & x+y=0, intersect (1, -1)
  3727. auto ret_x_1_xy_0 = analysis.IntersectConstraints(
  3728. line_x_1, line_x_y_0, constant_neg_2, constant_2);
  3729. EXPECT_NE(nullptr, ret_x_1_xy_0->AsDependencePoint());
  3730. EXPECT_EQ(*point_1_neg_1, *ret_x_1_xy_0);
  3731. // y=1 & x+y=0, intersect (-1, 1)
  3732. auto ret_y_1_xy_0 = analysis.IntersectConstraints(
  3733. line_y_1, line_x_y_0, constant_neg_2, constant_2);
  3734. EXPECT_NE(nullptr, ret_y_1_xy_0->AsDependencePoint());
  3735. EXPECT_EQ(*point_neg_1_1, *ret_y_1_xy_0);
  3736. // x+y=1 & x=0, intersect (0, 1)
  3737. auto ret_xy_1_x_0 = analysis.IntersectConstraints(
  3738. line_x_y_1, line_x_0, constant_neg_1, constant_2);
  3739. EXPECT_NE(nullptr, ret_xy_1_x_0->AsDependencePoint());
  3740. EXPECT_EQ(*point_0_1, *ret_xy_1_x_0);
  3741. // x+y=1 & y=0, intersect (1, 0)
  3742. auto ret_xy_1_y_0 = analysis.IntersectConstraints(
  3743. line_x_y_1, line_y_0, constant_neg_1, constant_2);
  3744. EXPECT_NE(nullptr, ret_xy_1_y_0->AsDependencePoint());
  3745. EXPECT_EQ(*point_1_0, *ret_xy_1_y_0);
  3746. // x+y=1 & x=1, intersect (1, 0)
  3747. auto ret_xy_1_x_1 = analysis.IntersectConstraints(
  3748. line_x_y_1, line_x_1, constant_neg_1, constant_2);
  3749. EXPECT_NE(nullptr, ret_xy_1_x_1->AsDependencePoint());
  3750. EXPECT_EQ(*point_1_0, *ret_xy_1_x_1);
  3751. // x+y=1 & y=1, intersect (0, 1)
  3752. auto ret_xy_1_y_1 = analysis.IntersectConstraints(
  3753. line_x_y_1, line_y_1, constant_neg_1, constant_2);
  3754. EXPECT_NE(nullptr, ret_xy_1_y_1->AsDependencePoint());
  3755. EXPECT_EQ(*point_0_1, *ret_xy_1_y_1);
  3756. // x=0 & x+y=1, intersect (0, 1)
  3757. auto ret_x_0_xy_1 = analysis.IntersectConstraints(
  3758. line_x_0, line_x_y_1, constant_neg_1, constant_2);
  3759. EXPECT_NE(nullptr, ret_x_0_xy_1->AsDependencePoint());
  3760. EXPECT_EQ(*point_0_1, *ret_x_0_xy_1);
  3761. // y=0 & x+y=1, intersect (1, 0)
  3762. auto ret_y_0_xy_1 = analysis.IntersectConstraints(
  3763. line_y_0, line_x_y_1, constant_neg_1, constant_2);
  3764. EXPECT_NE(nullptr, ret_y_0_xy_1->AsDependencePoint());
  3765. EXPECT_EQ(*point_1_0, *ret_y_0_xy_1);
  3766. // x=1 & x+y=1, intersect (1, 0)
  3767. auto ret_x_1_xy_1 = analysis.IntersectConstraints(
  3768. line_x_1, line_x_y_1, constant_neg_2, constant_2);
  3769. EXPECT_NE(nullptr, ret_x_1_xy_1->AsDependencePoint());
  3770. EXPECT_EQ(*point_1_0, *ret_x_1_xy_1);
  3771. // y=1 & x+y=1, intersect (0, 1)
  3772. auto ret_y_1_xy_1 = analysis.IntersectConstraints(
  3773. line_y_1, line_x_y_1, constant_neg_2, constant_2);
  3774. EXPECT_NE(nullptr, ret_y_1_xy_1->AsDependencePoint());
  3775. EXPECT_EQ(*point_0_1, *ret_y_1_xy_1);
  3776. }
  3777. {
  3778. // Line and point
  3779. auto a = scalar_evolution->CreateConstant(3);
  3780. auto b = scalar_evolution->CreateConstant(10);
  3781. auto c = scalar_evolution->CreateConstant(16);
  3782. auto line = analysis.make_constraint<DependenceLine>(a, b, c, nullptr);
  3783. // Point on line
  3784. auto x = scalar_evolution->CreateConstant(2);
  3785. auto y = scalar_evolution->CreateConstant(1);
  3786. auto point_0 = analysis.make_constraint<DependencePoint>(x, y, nullptr);
  3787. auto ret_0 = analysis.IntersectConstraints(line, point_0, nullptr, nullptr);
  3788. auto ret_1 = analysis.IntersectConstraints(point_0, line, nullptr, nullptr);
  3789. auto ret_point_0 = ret_0->AsDependencePoint();
  3790. auto ret_point_1 = ret_1->AsDependencePoint();
  3791. ASSERT_NE(nullptr, ret_point_0);
  3792. ASSERT_NE(nullptr, ret_point_1);
  3793. EXPECT_EQ(*x, *ret_point_0->GetSource());
  3794. EXPECT_EQ(*y, *ret_point_0->GetDestination());
  3795. EXPECT_EQ(*x, *ret_point_1->GetSource());
  3796. EXPECT_EQ(*y, *ret_point_1->GetDestination());
  3797. // Point not on line
  3798. auto point_1 = analysis.make_constraint<DependencePoint>(a, a, nullptr);
  3799. auto ret_2 = analysis.IntersectConstraints(line, point_1, nullptr, nullptr);
  3800. auto ret_3 = analysis.IntersectConstraints(point_1, line, nullptr, nullptr);
  3801. EXPECT_NE(nullptr, ret_2->AsDependenceEmpty());
  3802. EXPECT_NE(nullptr, ret_3->AsDependenceEmpty());
  3803. // Non-constant
  3804. auto unknown = scalar_evolution->CreateCantComputeNode();
  3805. auto point_2 =
  3806. analysis.make_constraint<DependencePoint>(unknown, x, nullptr);
  3807. auto ret_4 = analysis.IntersectConstraints(line, point_2, nullptr, nullptr);
  3808. auto ret_5 = analysis.IntersectConstraints(point_2, line, nullptr, nullptr);
  3809. EXPECT_NE(nullptr, ret_4->AsDependenceNone());
  3810. EXPECT_NE(nullptr, ret_5->AsDependenceNone());
  3811. }
  3812. {
  3813. // Distance and point
  3814. auto d = scalar_evolution->CreateConstant(5);
  3815. auto distance = analysis.make_constraint<DependenceDistance>(d, nullptr);
  3816. // Point on line
  3817. auto x = scalar_evolution->CreateConstant(10);
  3818. auto point_0 = analysis.make_constraint<DependencePoint>(d, x, nullptr);
  3819. auto ret_0 =
  3820. analysis.IntersectConstraints(distance, point_0, nullptr, nullptr);
  3821. auto ret_1 =
  3822. analysis.IntersectConstraints(point_0, distance, nullptr, nullptr);
  3823. auto ret_point_0 = ret_0->AsDependencePoint();
  3824. auto ret_point_1 = ret_1->AsDependencePoint();
  3825. ASSERT_NE(nullptr, ret_point_0);
  3826. ASSERT_NE(nullptr, ret_point_1);
  3827. // Point not on line
  3828. auto point_1 = analysis.make_constraint<DependencePoint>(x, x, nullptr);
  3829. auto ret_2 =
  3830. analysis.IntersectConstraints(distance, point_1, nullptr, nullptr);
  3831. auto ret_3 =
  3832. analysis.IntersectConstraints(point_1, distance, nullptr, nullptr);
  3833. EXPECT_NE(nullptr, ret_2->AsDependenceEmpty());
  3834. EXPECT_NE(nullptr, ret_3->AsDependenceEmpty());
  3835. // Non-constant
  3836. auto unknown = scalar_evolution->CreateCantComputeNode();
  3837. auto unknown_distance =
  3838. analysis.make_constraint<DependenceDistance>(unknown, nullptr);
  3839. auto ret_4 = analysis.IntersectConstraints(unknown_distance, point_1,
  3840. nullptr, nullptr);
  3841. auto ret_5 = analysis.IntersectConstraints(point_1, unknown_distance,
  3842. nullptr, nullptr);
  3843. EXPECT_NE(nullptr, ret_4->AsDependenceNone());
  3844. EXPECT_NE(nullptr, ret_5->AsDependenceNone());
  3845. }
  3846. }
  3847. } // namespace
  3848. } // namespace opt
  3849. } // namespace spvtools