gpu.c 184 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849
  1. /*
  2. * Copyright 2010-2011 INRIA Saclay
  3. * Copyright 2012-2013 Ecole Normale Superieure
  4. * Copyright 2015-2016 Sven Verdoolaege
  5. *
  6. * Use of this software is governed by the MIT license
  7. *
  8. * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
  9. * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
  10. * 91893 Orsay, France
  11. * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
  12. */
  13. #include <assert.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <isl/polynomial.h>
  17. #include <isl/union_set.h>
  18. #include <isl/aff.h>
  19. #include <isl/ilp.h>
  20. #include <isl/flow.h>
  21. #include <isl/schedule.h>
  22. #include <isl/schedule_node.h>
  23. #include <isl/options.h>
  24. #include <isl/ast_build.h>
  25. #include "cpu.h"
  26. #include "gpu.h"
  27. #include "gpu_array_tile.h"
  28. #include "gpu_group.h"
  29. #include "gpu_hybrid.h"
  30. #include "gpu_tree.h"
  31. #include "hybrid.h"
  32. #include "schedule.h"
  33. #include "ppcg_options.h"
  34. #include "print.h"
  35. #include "util.h"
  36. struct gpu_array_info;
  37. /* Return the name of the outer array (of structs) accessed by "access".
  38. */
  39. static const char *get_outer_array_name(__isl_keep isl_map *access)
  40. {
  41. isl_space *space;
  42. const char *name;
  43. space = isl_space_range(isl_map_get_space(access));
  44. while (space && isl_space_is_wrapping(space))
  45. space = isl_space_domain(isl_space_unwrap(space));
  46. name = isl_space_get_tuple_name(space, isl_dim_set);
  47. isl_space_free(space);
  48. return name;
  49. }
  50. /* Collect all references to the given array and store pointers to them
  51. * in array->refs.
  52. */
  53. void collect_references(struct gpu_prog *prog,
  54. struct gpu_array_info *array)
  55. {
  56. int i;
  57. int n;
  58. n = 0;
  59. for (i = 0; i < prog->n_stmts; ++i) {
  60. struct gpu_stmt *stmt = &prog->stmts[i];
  61. struct gpu_stmt_access *access;
  62. for (access = stmt->accesses; access; access = access->next) {
  63. const char *name;
  64. name = get_outer_array_name(access->access);
  65. if (name && !strcmp(array->name, name))
  66. n++;
  67. }
  68. }
  69. array->n_ref = n;
  70. array->refs = isl_alloc_array(prog->ctx, struct gpu_stmt_access *, n);
  71. assert(array->refs);
  72. n = 0;
  73. for (i = 0; i < prog->n_stmts; ++i) {
  74. struct gpu_stmt *stmt = &prog->stmts[i];
  75. struct gpu_stmt_access *access;
  76. for (access = stmt->accesses; access; access = access->next) {
  77. const char *name;
  78. name = get_outer_array_name(access->access);
  79. if (!name || strcmp(array->name, name))
  80. continue;
  81. array->refs[n++] = access;
  82. }
  83. }
  84. }
  85. /* Compute and return the extent of "array", taking into account the set of
  86. * accessed elements.
  87. *
  88. * In particular, the extent in the outer dimension is taken
  89. * from "accessed", while the extents in the remaining dimensions
  90. * are taken from array->extent.
  91. *
  92. * The extent in the outer dimension cannot be taken from array->extent
  93. * because that may be unbounded. Furthermore, even if it is bounded,
  94. * it may be larger than the piece of the array that is being accessed.
  95. */
  96. static __isl_give isl_set *compute_extent(struct pet_array *array,
  97. __isl_keep isl_set *accessed)
  98. {
  99. int n_index;
  100. isl_id *id;
  101. isl_set *outer;
  102. isl_set *extent;
  103. extent = isl_set_copy(array->extent);
  104. n_index = isl_set_dim(accessed, isl_dim_set);
  105. if (n_index == 0)
  106. return extent;
  107. extent = isl_set_project_out(extent, isl_dim_set, 0, 1);
  108. outer = isl_set_copy(accessed);
  109. outer = isl_set_project_out(outer, isl_dim_set, 1, n_index - 1);
  110. extent = isl_set_flat_product(outer, extent);
  111. id = isl_set_get_tuple_id(accessed);
  112. extent = isl_set_set_tuple_id(extent, id);
  113. return extent;
  114. }
  115. /* Is the array "array" being extracted a read-only scalar?
  116. *
  117. * That is, is "array" a scalar that is never possibly written to.
  118. * An array containing structures is never considered to be a scalar.
  119. */
  120. static int is_read_only_scalar(struct gpu_array_info *array,
  121. struct gpu_prog *prog)
  122. {
  123. isl_set *space;
  124. isl_union_map *write;
  125. int empty;
  126. if (array->has_compound_element)
  127. return 0;
  128. if (array->n_index != 0)
  129. return 0;
  130. write = isl_union_map_copy(prog->may_write);
  131. space = isl_set_universe(isl_space_copy(array->space));
  132. write = isl_union_map_intersect_range(write,
  133. isl_union_set_from_set(space));
  134. empty = isl_union_map_is_empty(write);
  135. isl_union_map_free(write);
  136. return empty;
  137. }
  138. /* Is "array" only accessed as individual, fixed elements?
  139. * That is, does each access to "array" access a single, fixed element?
  140. */
  141. isl_bool only_fixed_element_accessed(struct gpu_array_info *array)
  142. {
  143. int i;
  144. for (i = 0; i < array->n_ref; ++i)
  145. if (!array->refs[i]->fixed_element)
  146. return isl_bool_false;
  147. return isl_bool_true;
  148. }
  149. /* Compute bounds on the host array "pa" based on the corresponding
  150. * accessed elements in "arrays"
  151. * and collect all references to the array.
  152. * Store the results in "info".
  153. *
  154. * If the array is zero-dimensional and does not contain structures,
  155. * i.e., if the array is a scalar, we check whether it is read-only.
  156. * We also check whether the array is accessed at all.
  157. */
  158. static int extract_array_info(struct gpu_prog *prog,
  159. struct gpu_array_info *info, struct pet_array *pa,
  160. __isl_keep isl_union_set *arrays)
  161. {
  162. int empty;
  163. const char *name;
  164. int n_index;
  165. isl_multi_pw_aff *bounds;
  166. isl_set *accessed, *extent;
  167. n_index = isl_set_dim(pa->extent, isl_dim_set);
  168. name = isl_set_get_tuple_name(pa->extent);
  169. info->space = isl_set_get_space(pa->extent);
  170. info->name = strdup(name);
  171. info->n_index = n_index;
  172. info->linearize = prog->scop->options->linearize_device_arrays;
  173. info->type = strdup(pa->element_type);
  174. info->size = pa->element_size;
  175. info->local = pa->declared && !pa->exposed;
  176. info->has_compound_element = pa->element_is_record;
  177. info->read_only_scalar = is_read_only_scalar(info, prog);
  178. info->declared_extent = isl_set_copy(pa->extent);
  179. accessed = isl_union_set_extract_set(arrays,
  180. isl_space_copy(info->space));
  181. empty = isl_set_is_empty(accessed);
  182. extent = compute_extent(pa, accessed);
  183. isl_set_free(accessed);
  184. info->extent = extent;
  185. if (empty < 0)
  186. return -1;
  187. info->accessed = !empty;
  188. bounds = ppcg_size_from_extent(isl_set_copy(extent));
  189. bounds = isl_multi_pw_aff_gist(bounds, isl_set_copy(prog->context));
  190. if (!bounds)
  191. return -1;
  192. if (!isl_multi_pw_aff_is_cst(bounds))
  193. info->linearize = 1;
  194. info->bound = bounds;
  195. collect_references(prog, info);
  196. info->only_fixed_element = only_fixed_element_accessed(info);
  197. return 0;
  198. }
  199. /* Remove independence from the order constraints "order" on array "array".
  200. * Since the pairs of iterations in the filter relation of an independence
  201. * are guaranteed to be completely independent by the user, there is
  202. * no need to ensure that live ranges are ordered along those pairs.
  203. * We make an exception for local variables, though, as the independence
  204. * guarantee does not apply to those.
  205. *
  206. * The order constraints are used in two places.
  207. * Those on scalars are used in check_scalar_live_ranges to check if
  208. * we need to force the scalar to be private. Any non-local scalar
  209. * should not be forced scalar if it only appears in independent loops.
  210. * Those on non-scalars are added to the coincidence constraints
  211. * in compute_schedule because we do not support any array expansion.
  212. * Accesses to non-local arrays should not prevent a loop from being
  213. * considered coincident so we should indeed remove those constraints
  214. * from the order constraints.
  215. */
  216. static __isl_give isl_union_map *remove_independences(struct gpu_prog *prog,
  217. struct gpu_array_info *array, __isl_take isl_union_map *order)
  218. {
  219. // We do not have independence information in Polly. Hence, make this
  220. // function a no-op.
  221. return order;
  222. int i;
  223. for (i = 0; i < prog->scop->pet->n_independence; ++i) {
  224. struct pet_independence *pi = prog->scop->pet->independences[i];
  225. if (isl_union_set_contains(pi->local, array->space))
  226. continue;
  227. order = isl_union_map_subtract(order,
  228. isl_union_map_copy(pi->filter));
  229. }
  230. return order;
  231. }
  232. /* For each array in "prog", store the (untagged) order dependences
  233. * derived from the array in array->dep_order.
  234. * In particular, consider all references that access the given array
  235. * and take the order dependences that have one of these references
  236. * as source. (Since an order dependence relates two references to
  237. * the same array, the target of these order dependences will also
  238. * be one of these references.)
  239. * Additionally, store the union of these array->dep_order relations
  240. * for all arrays that cannot be mapped to private memory in prog->array_order.
  241. */
  242. void collect_order_dependences(struct gpu_prog *prog)
  243. {
  244. int i;
  245. isl_space *space;
  246. isl_union_map *accesses;
  247. space = isl_union_map_get_space(prog->read);
  248. prog->array_order = isl_union_map_empty(space);
  249. accesses = isl_union_map_copy(prog->scop->tagged_reads);
  250. accesses = isl_union_map_union(accesses,
  251. isl_union_map_copy(prog->scop->tagged_may_writes));
  252. accesses = isl_union_map_universe(accesses);
  253. accesses = isl_union_map_apply_range(accesses,
  254. isl_union_map_copy(prog->to_outer));
  255. for (i = 0; i < prog->n_array; ++i) {
  256. struct gpu_array_info *array = &prog->array[i];
  257. isl_set *set;
  258. isl_union_set *uset;
  259. isl_union_map *order;
  260. set = isl_set_universe(isl_space_copy(array->space));
  261. uset = isl_union_set_from_set(set);
  262. uset = isl_union_map_domain(
  263. isl_union_map_intersect_range(isl_union_map_copy(accesses),
  264. uset));
  265. order = isl_union_map_copy(prog->scop->tagged_dep_order);
  266. order = isl_union_map_intersect_domain(order, uset);
  267. order = isl_union_map_zip(order);
  268. order = isl_union_set_unwrap(isl_union_map_domain(order));
  269. order = remove_independences(prog, array, order);
  270. array->dep_order = order;
  271. if (gpu_array_can_be_private(array))
  272. continue;
  273. prog->array_order = isl_union_map_union(prog->array_order,
  274. isl_union_map_copy(array->dep_order));
  275. }
  276. isl_union_map_free(accesses);
  277. }
  278. /* Construct a gpu_array_info for each array referenced by prog->scop and
  279. * collect them in prog->array.
  280. *
  281. * The sizes are based on the extents and the set of possibly accessed
  282. * elements by "prog".
  283. * If there are any member accesses involved, then they are first mapped
  284. * to the outer arrays of structs.
  285. * Only extract gpu_array_info entries for these outer arrays.
  286. *
  287. * If we are allowing live range reordering, then also set
  288. * the dep_order field. Otherwise leave it NULL.
  289. */
  290. static int collect_array_info(struct gpu_prog *prog)
  291. {
  292. int i;
  293. int r = 0;
  294. isl_union_set *arrays;
  295. arrays = isl_union_map_range(isl_union_map_copy(prog->read));
  296. arrays = isl_union_set_union(arrays,
  297. isl_union_map_range(isl_union_map_copy(prog->may_write)));
  298. arrays = isl_union_set_apply(arrays,
  299. isl_union_map_copy(prog->to_outer));
  300. arrays = isl_union_set_coalesce(arrays);
  301. prog->n_array = prog->scop->pet->n_array;
  302. prog->array = isl_calloc_array(prog->ctx,
  303. struct gpu_array_info, prog->n_array);
  304. assert(prog->array);
  305. prog->n_array = 0;
  306. for (i = 0; i < prog->scop->pet->n_array; ++i) {
  307. isl_bool field;
  308. field = isl_set_is_wrapping(prog->scop->pet->arrays[i]->extent);
  309. if (field < 0)
  310. break;
  311. if (field)
  312. continue;
  313. if (extract_array_info(prog, &prog->array[prog->n_array++],
  314. prog->scop->pet->arrays[i], arrays) < 0)
  315. r = -1;
  316. }
  317. if (i < prog->scop->pet->n_array)
  318. r = -1;
  319. isl_union_set_free(arrays);
  320. if (prog->scop->options->live_range_reordering)
  321. collect_order_dependences(prog);
  322. return r;
  323. }
  324. static void free_array_info(struct gpu_prog *prog)
  325. {
  326. int i;
  327. for (i = 0; i < prog->n_array; ++i) {
  328. free(prog->array[i].type);
  329. free(prog->array[i].name);
  330. isl_multi_pw_aff_free(prog->array[i].bound);
  331. isl_ast_expr_free(prog->array[i].bound_expr);
  332. isl_space_free(prog->array[i].space);
  333. isl_set_free(prog->array[i].declared_extent);
  334. isl_set_free(prog->array[i].extent);
  335. isl_ast_expr_free(prog->array[i].declared_size);
  336. free(prog->array[i].refs);
  337. isl_union_map_free(prog->array[i].dep_order);
  338. }
  339. free(prog->array);
  340. }
  341. /* Check if a gpu array is a scalar. A scalar is a value that is not stored
  342. * as an array or through a pointer reference, but as a single data element.
  343. * At the moment, scalars are represented as zero-dimensional arrays.
  344. * Note that the single data element may be an entire structure.
  345. */
  346. int gpu_array_is_scalar(struct gpu_array_info *array)
  347. {
  348. return array->n_index == 0;
  349. }
  350. /* Can "array" be mapped to private memory?
  351. * That is, is it only accessed as individual elements with
  352. * constant index expressions?
  353. */
  354. isl_bool gpu_array_can_be_private(struct gpu_array_info *array)
  355. {
  356. if (!array)
  357. return isl_bool_error;
  358. return array->only_fixed_element;
  359. }
  360. /* Is "array" a read-only scalar?
  361. */
  362. int gpu_array_is_read_only_scalar(struct gpu_array_info *array)
  363. {
  364. return array->read_only_scalar;
  365. }
  366. /* Does "array" need to be allocated on the device?
  367. * If it is a read-only scalar, then it will be passed as an argument
  368. * to the kernel and therefore does not require any allocation.
  369. * If this device memory is not accessed at all, then it does not
  370. * need to be allocated either.
  371. */
  372. int gpu_array_requires_device_allocation(struct gpu_array_info *array)
  373. {
  374. if (gpu_array_is_read_only_scalar(array))
  375. return 0;
  376. if (!array->global)
  377. return 0;
  378. return 1;
  379. }
  380. /* Return the set of parameter values for which the array has a positive
  381. * size in all dimensions.
  382. * If the sizes are only valid for some parameter values, then those
  383. * constraints are also taken into account.
  384. */
  385. __isl_give isl_set *gpu_array_positive_size_guard(struct gpu_array_info *array)
  386. {
  387. int i;
  388. isl_space *space;
  389. isl_set *guard;
  390. if (!array)
  391. return NULL;
  392. space = isl_space_params(isl_space_copy(array->space));
  393. guard = isl_set_universe(space);
  394. for (i = 0; i < array->n_index; ++i) {
  395. isl_pw_aff *bound;
  396. isl_set *guard_i, *zero;
  397. bound = isl_multi_pw_aff_get_pw_aff(array->bound, i);
  398. guard_i = isl_pw_aff_nonneg_set(isl_pw_aff_copy(bound));
  399. zero = isl_pw_aff_zero_set(bound);
  400. guard_i = isl_set_subtract(guard_i, zero);
  401. guard = isl_set_intersect(guard, guard_i);
  402. }
  403. return guard;
  404. }
  405. /* Internal data structure for extract_size_of_type.
  406. * "type" specifies the name of the space that we want to extract.
  407. * "res" is used to store the subset of that space.
  408. */
  409. struct ppcg_extract_size_data {
  410. const char *type;
  411. isl_set *res;
  412. };
  413. /* This function is called for each set in a union_set.
  414. * If the name of the set matches data->type, we store the
  415. * set in data->res.
  416. */
  417. static isl_stat extract_size_of_type(__isl_take isl_set *size, void *user)
  418. {
  419. struct ppcg_extract_size_data *data = user;
  420. const char *name;
  421. name = isl_set_get_tuple_name(size);
  422. if (name && !strcmp(name, data->type)) {
  423. data->res = size;
  424. return isl_stat_error;
  425. }
  426. isl_set_free(size);
  427. return isl_stat_ok;
  428. }
  429. /* Given a union map { kernel[i] -> *[...] },
  430. * return the range in the space called "type" for the kernel with
  431. * sequence number "id".
  432. */
  433. static __isl_give isl_set *extract_sizes(__isl_keep isl_union_map *sizes,
  434. const char *type, int id)
  435. {
  436. isl_space *space;
  437. isl_set *dom;
  438. isl_union_set *local_sizes;
  439. struct ppcg_extract_size_data data = { type, NULL };
  440. if (!sizes)
  441. return NULL;
  442. space = isl_union_map_get_space(sizes);
  443. space = isl_space_set_from_params(space);
  444. space = isl_space_add_dims(space, isl_dim_set, 1);
  445. space = isl_space_set_tuple_name(space, isl_dim_set, "kernel");
  446. dom = isl_set_universe(space);
  447. dom = isl_set_fix_si(dom, isl_dim_set, 0, id);
  448. local_sizes = isl_union_set_apply(isl_union_set_from_set(dom),
  449. isl_union_map_copy(sizes));
  450. isl_union_set_foreach_set(local_sizes, &extract_size_of_type, &data);
  451. isl_union_set_free(local_sizes);
  452. return data.res;
  453. }
  454. /* Given a singleton set, extract the first (at most *len) elements
  455. * of the single integer tuple into *sizes and update *len if needed.
  456. */
  457. static void read_sizes_from_set(__isl_take isl_set *set, int *sizes, int *len)
  458. {
  459. int i;
  460. int dim;
  461. if (!set)
  462. return;
  463. dim = isl_set_dim(set, isl_dim_set);
  464. if (dim < *len)
  465. *len = dim;
  466. for (i = 0; i < *len; ++i) {
  467. isl_val *v;
  468. v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, i);
  469. assert(v);
  470. sizes[i] = isl_val_get_num_si(v);
  471. isl_val_free(v);
  472. }
  473. isl_set_free(set);
  474. }
  475. /* Add the map { kernel[id] -> type[sizes] } to gen->used_sizes,
  476. * if the option debug->dump_sizes is set.
  477. */
  478. static void set_used_sizes(struct gpu_gen *gen, const char *type, int id,
  479. int *sizes, int len)
  480. {
  481. int i;
  482. isl_space *space;
  483. isl_map *map;
  484. if (!gen->options->debug->dump_sizes)
  485. return;
  486. space = isl_union_map_get_space(gen->used_sizes);
  487. space = isl_space_set_from_params(space);
  488. space = isl_space_add_dims(space, isl_dim_set, 1);
  489. space = isl_space_set_tuple_name(space, isl_dim_set, "kernel");
  490. space = isl_space_from_domain(space);
  491. space = isl_space_add_dims(space, isl_dim_out, len);
  492. space = isl_space_set_tuple_name(space, isl_dim_out, type);
  493. map = isl_map_universe(space);
  494. map = isl_map_fix_si(map, isl_dim_in, 0, id);
  495. for (i = 0; i < len; ++i)
  496. map = isl_map_fix_si(map, isl_dim_out, i, sizes[i]);
  497. gen->used_sizes = isl_union_map_add_map(gen->used_sizes, map);
  498. }
  499. /* Extract user specified "tile" sizes from the "sizes" command line option,
  500. * defaulting to option->tile_size in each dimension.
  501. * *tile_len contains the maximum number of tile sizes needed.
  502. * Update *tile_len to the number of specified tile sizes, if any, and
  503. * return a pointer to the tile sizes (or NULL on error).
  504. * Add the effectively used sizes to gen->used_sizes.
  505. */
  506. static int *read_tile_sizes(struct gpu_gen *gen, int *tile_len)
  507. {
  508. int n;
  509. int *tile_size;
  510. isl_set *size;
  511. tile_size = isl_alloc_array(gen->ctx, int, *tile_len);
  512. if (!tile_size)
  513. return NULL;
  514. for (n = 0; n < *tile_len; ++n)
  515. tile_size[n] = gen->options->tile_size;
  516. size = extract_sizes(gen->sizes, "tile", gen->kernel_id);
  517. read_sizes_from_set(size, tile_size, tile_len);
  518. set_used_sizes(gen, "tile", gen->kernel_id, tile_size, *tile_len);
  519. return tile_size;
  520. }
  521. /* Extract user specified "block" sizes from the "sizes" command line option,
  522. * after filling in some potentially useful defaults.
  523. */
  524. static void read_block_sizes(struct ppcg_kernel *kernel,
  525. __isl_keep isl_union_map *sizes)
  526. {
  527. isl_set *size;
  528. if (kernel->n_block > 3)
  529. kernel->n_block = 3;
  530. switch (kernel->n_block) {
  531. case 1:
  532. kernel->block_dim[0] = 512;
  533. break;
  534. case 2:
  535. kernel->block_dim[0] = 32;
  536. kernel->block_dim[1] = 16;
  537. break;
  538. default:
  539. kernel->block_dim[0] = 32;
  540. kernel->block_dim[1] = 4;
  541. kernel->block_dim[2] = 4;
  542. break;
  543. }
  544. size = extract_sizes(sizes, "block", kernel->id);
  545. read_sizes_from_set(size, kernel->block_dim, &kernel->n_block);
  546. }
  547. /* Extract user specified "grid" sizes from the "sizes" command line option,
  548. * after filling in some potentially useful defaults.
  549. */
  550. static void read_grid_sizes(struct ppcg_kernel *kernel,
  551. __isl_keep isl_union_map *sizes)
  552. {
  553. isl_set *size;
  554. if (kernel->n_grid > 2)
  555. kernel->n_grid = 2;
  556. switch (kernel->n_grid) {
  557. case 1:
  558. kernel->grid_dim[0] = 32768;
  559. break;
  560. default:
  561. kernel->grid_dim[0] = 256;
  562. kernel->grid_dim[1] = 256;
  563. break;
  564. }
  565. size = extract_sizes(sizes, "grid", kernel->id);
  566. read_sizes_from_set(size, kernel->grid_dim, &kernel->n_grid);
  567. }
  568. /* Extract user specified grid and block sizes from the gen->sizes
  569. * command line option after filling in some potentially useful defaults.
  570. * Store the extracted sizes in "kernel".
  571. * Add the effectively used sizes to gen->used_sizes.
  572. */
  573. static void read_grid_and_block_sizes(struct ppcg_kernel *kernel,
  574. struct gpu_gen *gen)
  575. {
  576. read_block_sizes(kernel, gen->sizes);
  577. read_grid_sizes(kernel, gen->sizes);
  578. set_used_sizes(gen, "block", kernel->id,
  579. kernel->block_dim, kernel->n_block);
  580. set_used_sizes(gen, "grid", kernel->id,
  581. kernel->grid_dim, kernel->n_grid);
  582. }
  583. static void *free_stmts(struct gpu_stmt *stmts, int n)
  584. {
  585. int i;
  586. if (!stmts)
  587. return NULL;
  588. for (i = 0; i < n; ++i) {
  589. struct gpu_stmt_access *access, *next;
  590. for (access = stmts[i].accesses; access; access = next) {
  591. next = access->next;
  592. isl_id_free(access->ref_id);
  593. isl_map_free(access->access);
  594. isl_map_free(access->tagged_access);
  595. free(access);
  596. }
  597. isl_id_free(stmts[i].id);
  598. }
  599. free(stmts);
  600. return NULL;
  601. }
  602. /* Add parameters p[i] with identifiers "ids" to "set",
  603. * with bounds to 0 <= p[i] < size[i].
  604. */
  605. __isl_give isl_set *add_bounded_parameters(__isl_take isl_set *set,
  606. int *size, __isl_keep isl_id_list *ids)
  607. {
  608. int i, len;
  609. unsigned nparam;
  610. len = isl_id_list_n_id(ids);
  611. nparam = isl_set_dim(set, isl_dim_param);
  612. set = isl_set_add_dims(set, isl_dim_param, len);
  613. for (i = 0; i < len; ++i) {
  614. isl_id *id;
  615. id = isl_id_list_get_id(ids, i);
  616. set = isl_set_set_dim_id(set, isl_dim_param, nparam + i, id);
  617. set = isl_set_lower_bound_si(set, isl_dim_param, nparam + i, 0);
  618. set = isl_set_upper_bound_si(set, isl_dim_param,
  619. nparam + i, size[i] - 1);
  620. }
  621. return set;
  622. }
  623. /* Add "len" parameters p[i] with identifiers "ids" and intersect "set"
  624. * with
  625. *
  626. * { : 0 <= p[i] < size[i] }
  627. *
  628. * or an overapproximation.
  629. */
  630. static __isl_give isl_set *add_bounded_parameters_dynamic(
  631. __isl_take isl_set *set, __isl_keep isl_multi_pw_aff *size,
  632. __isl_keep isl_id_list *ids)
  633. {
  634. int i, len;
  635. unsigned nparam;
  636. isl_space *space;
  637. isl_local_space *ls;
  638. len = isl_multi_pw_aff_dim(size, isl_dim_out);
  639. nparam = isl_set_dim(set, isl_dim_param);
  640. set = isl_set_add_dims(set, isl_dim_param, len);
  641. for (i = 0; i < len; ++i) {
  642. isl_id *id;
  643. id = isl_id_list_get_id(ids, i);
  644. set = isl_set_set_dim_id(set, isl_dim_param, nparam + i, id);
  645. }
  646. space = isl_space_params(isl_set_get_space(set));
  647. ls = isl_local_space_from_space(space);
  648. for (i = 0; i < len; ++i) {
  649. isl_pw_aff *param, *size_i, *zero;
  650. isl_set *bound;
  651. param = isl_pw_aff_var_on_domain(isl_local_space_copy(ls),
  652. isl_dim_param, nparam + i);
  653. size_i = isl_multi_pw_aff_get_pw_aff(size, i);
  654. bound = isl_pw_aff_lt_set(isl_pw_aff_copy(param), size_i);
  655. bound = isl_set_from_basic_set(isl_set_simple_hull(bound));
  656. set = isl_set_intersect_params(set, bound);
  657. zero = isl_pw_aff_zero_on_domain(isl_local_space_copy(ls));
  658. bound = isl_pw_aff_ge_set(param, zero);
  659. set = isl_set_intersect_params(set, bound);
  660. }
  661. isl_local_space_free(ls);
  662. return set;
  663. }
  664. /* Return the union of all tagged access relations in the group.
  665. */
  666. static __isl_give isl_union_map *group_tagged_access_relation(
  667. struct gpu_array_ref_group *group)
  668. {
  669. int i;
  670. isl_union_map *access;
  671. access = isl_union_map_empty(isl_map_get_space(group->access));
  672. for (i = 0; i < group->n_ref; ++i) {
  673. isl_map *map_i;
  674. map_i = isl_map_copy(group->refs[i]->tagged_access);
  675. access = isl_union_map_union(access,
  676. isl_union_map_from_map(map_i));
  677. }
  678. return access;
  679. }
  680. /* Return the extent of "array", recomputed from the bounds.
  681. * The recomputed extent may be simpler than the original extent.
  682. */
  683. static __isl_give isl_set *array_extent(struct gpu_array_info *array)
  684. {
  685. int i;
  686. isl_id *id;
  687. isl_space *space;
  688. isl_local_space *ls;
  689. isl_set *extent;
  690. id = isl_set_get_tuple_id(array->extent);
  691. space = isl_set_get_space(array->extent);
  692. extent = isl_set_universe(isl_space_copy(space));
  693. ls = isl_local_space_from_space(space);
  694. for (i = 0; i < array->n_index; ++i) {
  695. isl_pw_aff *bound;
  696. isl_aff *aff;
  697. isl_pw_aff *index;
  698. isl_set *lt;
  699. extent = isl_set_lower_bound_si(extent, isl_dim_set, i, 0);
  700. aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
  701. isl_dim_set, i);
  702. index = isl_pw_aff_from_aff(aff);
  703. bound = isl_multi_pw_aff_get_pw_aff(array->bound, i);
  704. bound = isl_pw_aff_from_range(bound);
  705. bound = isl_pw_aff_add_dims(bound, isl_dim_in, array->n_index);
  706. bound = isl_pw_aff_set_tuple_id(bound, isl_dim_in,
  707. isl_id_copy(id));
  708. lt = isl_pw_aff_lt_set(index, bound);
  709. extent = isl_set_intersect(extent, lt);
  710. }
  711. isl_local_space_free(ls);
  712. isl_id_free(id);
  713. return extent;
  714. }
  715. /* Return a map from the first group->shared_tile->depth dimensions
  716. * of the computed schedule to the array tile in
  717. * global memory that corresponds to the shared memory copy.
  718. *
  719. * In particular, return a map
  720. *
  721. * { D[i] -> A[a] }
  722. *
  723. * with constraints
  724. *
  725. * tile_offset(i) <= a <= tile_offset(i) + tile_size - 1 (1)
  726. *
  727. * and
  728. *
  729. * 0 <= a <= array_size - 1 (2)
  730. *
  731. * Note that if some stride has been detected (i.e., when
  732. * group->shared_tile->bound[i].shift is set), then a in (1) refers
  733. * to the shifted and scaled down version.
  734. *
  735. * Constraints (1) are obtained by mapping the size constraints on the
  736. * shared/private memory tile back to the access relation.
  737. * Constraints (2) are obtained from the (recomputed) extent.
  738. */
  739. static __isl_give isl_map *group_tile(struct gpu_array_ref_group *group)
  740. {
  741. int i;
  742. int n_index = group->array->n_index;
  743. isl_map *tile;
  744. isl_space *space;
  745. isl_set *local;
  746. isl_set *extent;
  747. space = isl_multi_aff_get_space(group->shared_tile->tiling);
  748. space = isl_space_range(space);
  749. local = isl_set_universe(space);
  750. for (i = 0; i < n_index; ++i) {
  751. isl_val *bound;
  752. local = isl_set_lower_bound_si(local, isl_dim_set, i, 0);
  753. bound = isl_val_copy(group->shared_tile->bound[i].size);
  754. bound = isl_val_sub_ui(bound, 1);
  755. local = isl_set_upper_bound_val(local, isl_dim_set, i, bound);
  756. }
  757. local = isl_set_preimage_multi_aff(local,
  758. isl_multi_aff_copy(group->shared_tile->tiling));
  759. tile = isl_set_unwrap(local);
  760. extent = array_extent(group->array);
  761. tile = isl_map_intersect_range(tile, extent);
  762. return tile;
  763. }
  764. /* Given a mapping "iterator_map" from the AST schedule to a domain,
  765. * return the corresponding mapping from the AST schedule to
  766. * to the outer kernel->copy_schedule_dim dimensions of
  767. * the schedule computed by PPCG for this kernel.
  768. *
  769. * Note that kernel->copy_schedule_dim is at least as large as
  770. * the largest depth of any array reference group associated to the kernel.
  771. * This is needed as the returned schedule is used to extract a mapping
  772. * to the outer tile->depth dimensions in transform_index.
  773. */
  774. static __isl_give isl_pw_multi_aff *compute_sched_to_copy(
  775. struct ppcg_kernel *kernel, __isl_take isl_pw_multi_aff *iterator_map)
  776. {
  777. isl_union_pw_multi_aff *upma;
  778. isl_pw_multi_aff *pma;
  779. isl_space *space;
  780. space = isl_space_range(isl_pw_multi_aff_get_space(iterator_map));
  781. space = isl_space_from_domain(space);
  782. space = isl_space_add_dims(space, isl_dim_out,
  783. kernel->copy_schedule_dim);
  784. upma = isl_union_pw_multi_aff_copy(kernel->copy_schedule);
  785. pma = isl_union_pw_multi_aff_extract_pw_multi_aff(upma, space);
  786. isl_union_pw_multi_aff_free(upma);
  787. return isl_pw_multi_aff_pullback_pw_multi_aff(pma, iterator_map);
  788. }
  789. /* If max_shared_memory is not set to infinity (-1), then make
  790. * sure that the total amount of shared memory required by the
  791. * array reference groups mapped to shared memory by "kernel"
  792. * is no larger than this maximum.
  793. *
  794. * We apply a greedy approach and discard (keep in global memory)
  795. * those groups that would result in a total memory size that
  796. * is larger than the maximum.
  797. *
  798. * This function should be called after any function that may
  799. * affect the decision on whether to place a reference group
  800. * in private, shared or global memory.
  801. */
  802. static void check_shared_memory_bound(struct ppcg_kernel *kernel)
  803. {
  804. int i, j;
  805. isl_val *left, *size;
  806. if (kernel->options->max_shared_memory < 0)
  807. return;
  808. left = isl_val_int_from_si(kernel->ctx,
  809. kernel->options->max_shared_memory);
  810. for (i = 0; i < kernel->n_array; ++i) {
  811. struct gpu_local_array_info *local = &kernel->array[i];
  812. for (j = 0; j < local->n_group; ++j) {
  813. struct gpu_array_ref_group *group;
  814. enum ppcg_group_access_type type;
  815. group = local->groups[j];
  816. type = gpu_array_ref_group_type(group);
  817. if (type != ppcg_access_shared)
  818. continue;
  819. size = gpu_array_tile_size(group->shared_tile);
  820. size = isl_val_mul_ui(size, local->array->size);
  821. if (isl_val_le(size, left)) {
  822. left = isl_val_sub(left, size);
  823. continue;
  824. }
  825. isl_val_free(size);
  826. group->shared_tile =
  827. gpu_array_tile_free(group->shared_tile);
  828. }
  829. }
  830. isl_val_free(left);
  831. }
  832. /* Mark all arrays of "kernel" that have an array reference group
  833. * that is not mapped to private or shared memory as
  834. * accessing the corresponding global device memory.
  835. */
  836. static void mark_global_arrays(struct ppcg_kernel *kernel)
  837. {
  838. int i, j;
  839. for (i = 0; i < kernel->n_array; ++i) {
  840. struct gpu_local_array_info *local = &kernel->array[i];
  841. if (local->global)
  842. continue;
  843. for (j = 0; j < local->n_group; ++j) {
  844. if (gpu_array_ref_group_tile(local->groups[j]))
  845. continue;
  846. local->global = 1;
  847. local->array->global = 1;
  848. break;
  849. }
  850. }
  851. }
  852. /* Compute a tiling for all the array reference groups in "kernel".
  853. */
  854. static void compute_group_tilings(struct ppcg_kernel *kernel)
  855. {
  856. int i, j;
  857. for (i = 0; i < kernel->n_array; ++i) {
  858. struct gpu_local_array_info *array = &kernel->array[i];
  859. for (j = 0; j < array->n_group; ++j)
  860. gpu_array_ref_group_compute_tiling(array->groups[j]);
  861. }
  862. }
  863. /* Compute the effective grid size as a list of the sizes in each dimension.
  864. *
  865. * The grid size specified by the user or set by default
  866. * in read_grid_sizes() and applied by the block filter,
  867. * may be too large for the given code in the sense that
  868. * it may contain blocks that don't need to execute anything.
  869. * We therefore don't return this grid size, but instead the
  870. * smallest grid size that ensures that all blocks that actually
  871. * execute code are included in the grid.
  872. *
  873. * We first extract a description of the grid, i.e., the possible values
  874. * of the block ids, from the domain elements in "domain" and
  875. * kernel->block_filter.
  876. * The block ids are parameters in kernel->block_filter.
  877. * We simply need to change them into set dimensions.
  878. *
  879. * Then, for each block dimension, we compute the maximal value of the block id
  880. * and add one.
  881. */
  882. static __isl_give isl_multi_pw_aff *extract_grid_size(
  883. struct ppcg_kernel *kernel, __isl_take isl_union_set *domain)
  884. {
  885. int i;
  886. isl_set *grid;
  887. isl_set *context;
  888. isl_multi_pw_aff *size;
  889. domain = isl_union_set_intersect(domain,
  890. isl_union_set_copy(kernel->block_filter));
  891. grid = isl_union_set_params(domain);
  892. grid = isl_set_from_params(grid);
  893. grid = isl_set_add_dims(grid, isl_dim_set, kernel->n_grid);
  894. for (i = 0; i < kernel->n_grid; ++i) {
  895. int pos;
  896. isl_id *id;
  897. id = isl_id_list_get_id(kernel->block_ids, i);
  898. pos = isl_set_find_dim_by_id(grid, isl_dim_param, id);
  899. isl_id_free(id);
  900. assert(pos >= 0);
  901. grid = isl_set_equate(grid, isl_dim_param, pos, isl_dim_set, i);
  902. grid = isl_set_project_out(grid, isl_dim_param, pos, 1);
  903. }
  904. grid = isl_set_coalesce(grid);
  905. size = ppcg_size_from_extent(grid);
  906. context = isl_set_params(isl_set_copy(kernel->context));
  907. return isl_multi_pw_aff_gist(size, context);
  908. }
  909. /* Compute the size of a fixed bounding box around the origin and "set",
  910. * where "set" is assumed to contain only non-negative elements,
  911. * and store the results in "size".
  912. * In particular, compute the maximal value of "set" in each direction
  913. * and add one.
  914. */
  915. static void extract_fixed_size(__isl_take isl_set *set, int *size)
  916. {
  917. int i, n;
  918. isl_local_space *ls;
  919. isl_aff *obj;
  920. n = isl_set_dim(set, isl_dim_set);
  921. ls = isl_local_space_from_space(isl_set_get_space(set));
  922. obj = isl_aff_zero_on_domain(ls);
  923. for (i = 0; i < n; ++i) {
  924. isl_val *max;
  925. obj = isl_aff_set_coefficient_si(obj, isl_dim_in, i, 1);
  926. max = isl_set_max_val(set, obj);
  927. size[i] = isl_val_get_num_si(max) + 1;
  928. isl_val_free(max);
  929. obj = isl_aff_set_coefficient_si(obj, isl_dim_in, i, 0);
  930. }
  931. isl_aff_free(obj);
  932. isl_set_free(set);
  933. }
  934. /* Compute the effective block size as a list of the sizes in each dimension
  935. * and store the sizes in kernel->block_dim.
  936. *
  937. * The block size specified by the user or set by default
  938. * in read_block_sizes() and applied by the thread filter,
  939. * may be too large for the given code in the sense that
  940. * it may contain threads that don't need to execute anything.
  941. * We therefore update this block size in kernel->block_dim
  942. * to the smallest block size that ensures that all threads
  943. * that actually execute code are included in the block.
  944. *
  945. * The set of possible values of the thread ids is obtained from
  946. * the domain elements "domain" and kernel->thread_filter.
  947. * The current implementation eliminates all parameters, ensuring
  948. * that the size is a fixed constant in each dimension.
  949. * In principle we could also compute parametric sizes.
  950. * We would have to make sure to project out all b%d and t%d parameters,
  951. * however.
  952. */
  953. static isl_stat extract_block_size(struct ppcg_kernel *kernel,
  954. __isl_take isl_union_set *domain)
  955. {
  956. int i;
  957. int nparam;
  958. isl_set *block;
  959. domain = isl_union_set_intersect(domain,
  960. isl_union_set_copy(kernel->thread_filter));
  961. block = isl_union_set_params(domain);
  962. block = isl_set_from_params(block);
  963. block = isl_set_add_dims(block, isl_dim_set, kernel->n_block);
  964. for (i = 0; i < kernel->n_block; ++i) {
  965. int pos;
  966. isl_id *id;
  967. if (!block)
  968. return isl_stat_error;
  969. id = isl_id_list_get_id(kernel->thread_ids, i);
  970. pos = isl_set_find_dim_by_id(block, isl_dim_param, id);
  971. isl_id_free(id);
  972. if (pos < 0)
  973. isl_die(isl_set_get_ctx(block), isl_error_internal,
  974. "missing constraints on thread identifier",
  975. block = isl_set_free(block));
  976. block = isl_set_equate(block, isl_dim_param, pos,
  977. isl_dim_set, i);
  978. }
  979. nparam = isl_set_dim(block, isl_dim_param);
  980. block = isl_set_project_out(block, isl_dim_param, 0, nparam);
  981. if (!block)
  982. return isl_stat_error;
  983. extract_fixed_size(block, kernel->block_dim);
  984. return isl_stat_ok;
  985. }
  986. struct ppcg_kernel *ppcg_kernel_free(struct ppcg_kernel *kernel)
  987. {
  988. int i, j;
  989. if (!kernel)
  990. return NULL;
  991. isl_id_list_free(kernel->block_ids);
  992. isl_id_list_free(kernel->thread_ids);
  993. isl_multi_pw_aff_free(kernel->grid_size);
  994. isl_ast_expr_free(kernel->grid_size_expr);
  995. isl_set_free(kernel->context);
  996. isl_union_set_free(kernel->core);
  997. isl_union_set_free(kernel->arrays);
  998. isl_union_pw_multi_aff_free(kernel->contraction);
  999. isl_union_set_free(kernel->expanded_domain);
  1000. isl_space_free(kernel->space);
  1001. isl_ast_node_free(kernel->tree);
  1002. isl_union_set_free(kernel->block_filter);
  1003. isl_union_set_free(kernel->thread_filter);
  1004. isl_union_pw_multi_aff_free(kernel->copy_schedule);
  1005. isl_union_set_free(kernel->sync_writes);
  1006. for (i = 0; i < kernel->n_array; ++i) {
  1007. struct gpu_local_array_info *array = &kernel->array[i];
  1008. for (j = 0; j < array->n_group; ++j)
  1009. gpu_array_ref_group_free(array->groups[j]);
  1010. free(array->groups);
  1011. isl_multi_pw_aff_free(array->bound);
  1012. isl_ast_expr_free(array->bound_expr);
  1013. }
  1014. free(kernel->array);
  1015. for (i = 0; i < kernel->n_var; ++i) {
  1016. free(kernel->var[i].name);
  1017. isl_vec_free(kernel->var[i].size);
  1018. }
  1019. free(kernel->var);
  1020. free(kernel);
  1021. return NULL;
  1022. }
  1023. /* Wrapper around ppcg_kernel_free for use as a isl_id_set_free_user callback.
  1024. */
  1025. static void ppcg_kernel_free_wrap(void *user)
  1026. {
  1027. struct ppcg_kernel *kernel = user;
  1028. ppcg_kernel_free(kernel);
  1029. }
  1030. static void create_kernel_var(isl_ctx *ctx, struct gpu_array_ref_group *group,
  1031. struct ppcg_kernel_var *var)
  1032. {
  1033. int j;
  1034. struct gpu_array_tile *tile;
  1035. isl_printer *p;
  1036. var->array = group->array;
  1037. var->type = gpu_array_ref_group_type(group);
  1038. tile = gpu_array_ref_group_tile(group);
  1039. p = isl_printer_to_str(ctx);
  1040. p = gpu_array_ref_group_print_name(group, p);
  1041. var->name = isl_printer_get_str(p);
  1042. isl_printer_free(p);
  1043. var->size = isl_vec_alloc(ctx, group->array->n_index);
  1044. for (j = 0; j < group->array->n_index; ++j)
  1045. var->size = isl_vec_set_element_val(var->size, j,
  1046. isl_val_copy(tile->bound[j].size));
  1047. }
  1048. static int create_kernel_vars(struct ppcg_kernel *kernel)
  1049. {
  1050. int i, j, n;
  1051. n = 0;
  1052. for (i = 0; i < kernel->n_array; ++i) {
  1053. struct gpu_local_array_info *array = &kernel->array[i];
  1054. for (j = 0; j < array->n_group; ++j) {
  1055. struct gpu_array_ref_group *group = array->groups[j];
  1056. enum ppcg_group_access_type type;
  1057. type = gpu_array_ref_group_type(group);
  1058. if (type != ppcg_access_global)
  1059. ++n;
  1060. }
  1061. }
  1062. kernel->n_var = n;
  1063. kernel->var = isl_calloc_array(kernel->ctx, struct ppcg_kernel_var, n);
  1064. if (!kernel->var)
  1065. return -1;
  1066. n = 0;
  1067. for (i = 0; i < kernel->n_array; ++i) {
  1068. struct gpu_local_array_info *array = &kernel->array[i];
  1069. for (j = 0; j < array->n_group; ++j) {
  1070. struct gpu_array_ref_group *group = array->groups[j];
  1071. enum ppcg_group_access_type type;
  1072. type = gpu_array_ref_group_type(group);
  1073. if (type == ppcg_access_global)
  1074. continue;
  1075. create_kernel_var(kernel->ctx, group, &kernel->var[n]);
  1076. ++n;
  1077. }
  1078. }
  1079. return 0;
  1080. }
  1081. /* Replace "pa" by the zero function defined over the universe domain
  1082. * in the space of "pa".
  1083. */
  1084. static __isl_give isl_pw_aff *set_universally_zero(__isl_take isl_pw_aff *pa)
  1085. {
  1086. isl_space *space;
  1087. isl_aff *zero;
  1088. space = isl_space_domain(isl_pw_aff_get_space(pa));
  1089. isl_pw_aff_free(pa);
  1090. zero = isl_aff_zero_on_domain(isl_local_space_from_space(space));
  1091. return isl_pw_aff_from_aff(zero);
  1092. }
  1093. /* The sizes of the arrays on the host that have been computed by
  1094. * extract_array_info may depend on the parameters. Use the extra
  1095. * constraints on the parameters that are valid at "host_domain"
  1096. * to simplify these expressions and store the results in kernel->array.
  1097. *
  1098. * We only need these localized bounds for arrays that are accessed
  1099. * by the current kernel. If we have found at least one reference group
  1100. * then the array is accessed by the kernel.
  1101. *
  1102. * The resulting sizes may be functions that are nowhere defined
  1103. * in case the access function cannot possibly access anything inside
  1104. * the kernel for some reason. If so, they are replaced by the zero
  1105. * function. Since the access function cannot actually access anything,
  1106. * there is no harm in printing the array sizes as zero.
  1107. */
  1108. static void localize_bounds(struct ppcg_kernel *kernel,
  1109. __isl_keep isl_set *host_domain)
  1110. {
  1111. int i, j;
  1112. isl_set *context;
  1113. context = isl_set_copy(host_domain);
  1114. context = isl_set_params(context);
  1115. for (i = 0; i < kernel->n_array; ++i) {
  1116. struct gpu_local_array_info *local = &kernel->array[i];
  1117. isl_multi_pw_aff *bound;
  1118. int n_index;
  1119. if (local->n_group == 0)
  1120. continue;
  1121. n_index = local->array->n_index;
  1122. bound = isl_multi_pw_aff_copy(local->array->bound);
  1123. for (j = 0; j < n_index; ++j) {
  1124. isl_pw_aff *pwaff;
  1125. int empty;
  1126. pwaff = isl_multi_pw_aff_get_pw_aff(bound, j);
  1127. pwaff = isl_pw_aff_gist(pwaff, isl_set_copy(context));
  1128. empty = isl_pw_aff_is_empty(pwaff);
  1129. if (empty < 0)
  1130. pwaff = isl_pw_aff_free(pwaff);
  1131. else if (empty)
  1132. pwaff = set_universally_zero(pwaff);
  1133. bound = isl_multi_pw_aff_set_pw_aff(bound, j, pwaff);
  1134. }
  1135. local->n_index = n_index;
  1136. local->bound = bound;
  1137. }
  1138. isl_set_free(context);
  1139. }
  1140. /* Create the array of gpu_local_array_info structures "array"
  1141. * inside "kernel". The number of elements in this array is
  1142. * the same as the number of arrays in "prog".
  1143. * Initialize the "array" field of each local array to point
  1144. * to the corresponding array in "prog".
  1145. */
  1146. static struct ppcg_kernel *ppcg_kernel_create_local_arrays(
  1147. struct ppcg_kernel *kernel, struct gpu_prog *prog)
  1148. {
  1149. int i;
  1150. isl_ctx *ctx;
  1151. ctx = isl_set_get_ctx(prog->context);
  1152. kernel->array = isl_calloc_array(ctx,
  1153. struct gpu_local_array_info, prog->n_array);
  1154. if (!kernel->array)
  1155. return ppcg_kernel_free(kernel);
  1156. kernel->n_array = prog->n_array;
  1157. for (i = 0; i < prog->n_array; ++i)
  1158. kernel->array[i].array = &prog->array[i];
  1159. return kernel;
  1160. }
  1161. /* Does "kernel" need to be passed an argument corresponding to array "i"?
  1162. *
  1163. * The argument is only needed if the kernel accesses this device memory.
  1164. */
  1165. int ppcg_kernel_requires_array_argument(struct ppcg_kernel *kernel, int i)
  1166. {
  1167. return kernel->array[i].global;
  1168. }
  1169. /* Find the element in gen->stmt that has the given "id".
  1170. * Return NULL if no such gpu_stmt can be found.
  1171. */
  1172. static struct gpu_stmt *find_stmt(struct gpu_prog *prog, __isl_keep isl_id *id)
  1173. {
  1174. int i;
  1175. for (i = 0; i < prog->n_stmts; ++i) {
  1176. if (id == prog->stmts[i].id)
  1177. break;
  1178. }
  1179. return i < prog->n_stmts ? &prog->stmts[i] : NULL;
  1180. }
  1181. void ppcg_kernel_stmt_free(void *user)
  1182. {
  1183. struct ppcg_kernel_stmt *stmt = user;
  1184. if (!stmt)
  1185. return;
  1186. switch (stmt->type) {
  1187. case ppcg_kernel_copy:
  1188. isl_ast_expr_free(stmt->u.c.index);
  1189. isl_ast_expr_free(stmt->u.c.local_index);
  1190. break;
  1191. case ppcg_kernel_domain:
  1192. isl_id_to_ast_expr_free(stmt->u.d.ref2expr);
  1193. break;
  1194. case ppcg_kernel_sync:
  1195. break;
  1196. }
  1197. free(stmt);
  1198. }
  1199. /* Return the gpu_stmt_access in the list "accesses" that corresponds
  1200. * to "ref_id".
  1201. */
  1202. static struct gpu_stmt_access *find_access(struct gpu_stmt_access *accesses,
  1203. __isl_keep isl_id *ref_id)
  1204. {
  1205. struct gpu_stmt_access *access;
  1206. for (access = accesses; access; access = access->next)
  1207. if (access->ref_id == ref_id)
  1208. return access;
  1209. return NULL;
  1210. }
  1211. /* Return the index of the array called "name" in the list of arrays.
  1212. */
  1213. static int find_array_index(struct ppcg_kernel *kernel, const char *name)
  1214. {
  1215. int i;
  1216. for (i = 0; i < kernel->n_array; ++i)
  1217. if (!strcmp(name, kernel->array[i].array->name))
  1218. return i;
  1219. return -1;
  1220. }
  1221. /* Internal data structure for the index and AST expression transformation
  1222. * callbacks for pet_stmt_build_ast_exprs.
  1223. *
  1224. * "kernel" is the kernel for which are computing AST expressions and
  1225. * may be NULL if we are not inside a kernel.
  1226. * "accesses" is the list of gpu_stmt_access in the statement.
  1227. * "iterator_map" expresses the statement iterators in terms of
  1228. * the AST loop iterators.
  1229. * "sched2copy" expresses the outer copy_schedule_dim dimensions of
  1230. * the kernel schedule in terms of the AST loop iterators and
  1231. * may be NULL if we are not inside a kernel.
  1232. *
  1233. * The following fields are set in transform_index and used in transform_expr.
  1234. * "array" is the array that is being accessed.
  1235. * "global" is set if the global array is accessed (rather than
  1236. * shared/private memory).
  1237. * "local_array" refers to information on the array specialized
  1238. * to the current kernel.
  1239. */
  1240. struct ppcg_transform_data {
  1241. struct ppcg_options *options;
  1242. struct ppcg_kernel *kernel;
  1243. struct gpu_stmt_access *accesses;
  1244. isl_pw_multi_aff *iterator_map;
  1245. isl_pw_multi_aff *sched2copy;
  1246. struct gpu_array_info *array;
  1247. int global;
  1248. struct gpu_local_array_info *local_array;
  1249. };
  1250. /* Return a pointer to the gpu_array_ref_group in "local"
  1251. * that contains the reference "access".
  1252. * Return NULL if no such group can be found.
  1253. */
  1254. static struct gpu_array_ref_group *find_ref_group(
  1255. struct gpu_local_array_info *local, struct gpu_stmt_access *access)
  1256. {
  1257. int i, j;
  1258. for (i = 0; i < local->n_group; ++i) {
  1259. struct gpu_array_ref_group *group = local->groups[i];
  1260. for (j = 0; j < group->n_ref; ++j)
  1261. if (group->refs[j] == access)
  1262. return group;
  1263. }
  1264. return NULL;
  1265. }
  1266. /* Given an index expression "index" of the form
  1267. *
  1268. * L -> F(A),
  1269. *
  1270. * with F(A) either A or some subfield of A and L the AST loop iterators,
  1271. * and a tiling "tiling" of the form
  1272. *
  1273. * [L -> A] -> T
  1274. *
  1275. * apply the tiling to the outer array in the index expression to obtain
  1276. *
  1277. * L -> T(A)
  1278. *
  1279. * If F(A) is some subfield of A, then separate the member access
  1280. * into the base index expression and the field index expression,
  1281. * apply the tiling to the base index expression and combine the result
  1282. * with the field index expression.
  1283. *
  1284. * If F(A) is A, then modify index to keep track of the iterators
  1285. *
  1286. * L -> [L -> A]
  1287. *
  1288. * and combine the result with the tiling to obtain a tiled index expression
  1289. * in terms of the AST loop iterators
  1290. *
  1291. * L -> T
  1292. */
  1293. static __isl_give isl_multi_pw_aff *tile_outer(
  1294. __isl_take isl_multi_pw_aff *index, __isl_take isl_multi_pw_aff *tiling)
  1295. {
  1296. isl_bool is_wrapping;
  1297. isl_space *space;
  1298. isl_multi_pw_aff *mpa;
  1299. is_wrapping = isl_multi_pw_aff_range_is_wrapping(index);
  1300. if (is_wrapping < 0)
  1301. goto error;
  1302. if (is_wrapping) {
  1303. isl_multi_pw_aff *field;
  1304. field = isl_multi_pw_aff_copy(index);
  1305. field = isl_multi_pw_aff_range_factor_range(field);
  1306. index = isl_multi_pw_aff_range_factor_domain(index);
  1307. index = tile_outer(index, tiling);
  1308. return isl_multi_pw_aff_range_product(index, field);
  1309. }
  1310. space = isl_space_domain(isl_multi_pw_aff_get_space(index));
  1311. space = isl_space_map_from_set(space);
  1312. mpa = isl_multi_pw_aff_identity(space);
  1313. index = isl_multi_pw_aff_range_product(mpa, index);
  1314. index = isl_multi_pw_aff_pullback_multi_pw_aff(tiling, index);
  1315. return index;
  1316. error:
  1317. isl_multi_pw_aff_free(index);
  1318. isl_multi_pw_aff_free(tiling);
  1319. return NULL;
  1320. }
  1321. /* Index transformation callback for pet_stmt_build_ast_exprs.
  1322. *
  1323. * "index" expresses the array indices in terms of statement iterators
  1324. *
  1325. * We first reformulate "index" in terms of the AST loop iterators.
  1326. * Then we check if we are accessing the global array or
  1327. * a shared/private copy. In particular, if we are not inside a kernel
  1328. * then we must be accessing a global array.
  1329. * In the former case, we simply return
  1330. * the updated index. If "index" is an affine expression rather
  1331. * than an array access, then we also return the updated index here.
  1332. *
  1333. * If no reference groups have been computed for the array,
  1334. * then we can only be accessing the global array.
  1335. *
  1336. * Otherwise, we apply the tiling to the index.
  1337. * This tiling is of the form
  1338. *
  1339. * [D -> A] -> T
  1340. *
  1341. * where D corresponds to the outer tile->depth dimensions of
  1342. * the kernel schedule.
  1343. * The index is of the form
  1344. *
  1345. * L -> A
  1346. *
  1347. * We update the tiling to refer to the AST loop iterators
  1348. *
  1349. * [L -> A] -> T
  1350. *
  1351. * and combine it with the index to obtain a tiled index expression in terms
  1352. * of the AST loop iterators
  1353. *
  1354. * L -> T
  1355. *
  1356. * Note that while the tiling applies directly to an outer array.
  1357. * the index may refer to some subfield of this outer array.
  1358. * In such cases, the result will refer to the same subfield of the tile.
  1359. * That is, an index expression of the form L -> F(A) will be transformed
  1360. * into an index expression of the form L -> F(T).
  1361. */
  1362. static __isl_give isl_multi_pw_aff *transform_index(
  1363. __isl_take isl_multi_pw_aff *index, __isl_keep isl_id *ref_id,
  1364. void *user)
  1365. {
  1366. struct ppcg_transform_data *data = user;
  1367. struct gpu_stmt_access *access;
  1368. struct gpu_array_ref_group *group;
  1369. struct gpu_array_tile *tile;
  1370. isl_pw_multi_aff *iterator_map;
  1371. int i;
  1372. int dim;
  1373. const char *name;
  1374. isl_space *space;
  1375. isl_multi_pw_aff *tiling;
  1376. isl_pw_multi_aff *pma;
  1377. isl_pw_multi_aff *sched2depth;
  1378. data->array = NULL;
  1379. iterator_map = isl_pw_multi_aff_copy(data->iterator_map);
  1380. index = isl_multi_pw_aff_pullback_pw_multi_aff(index, iterator_map);
  1381. if (!data->kernel)
  1382. return index;
  1383. access = find_access(data->accesses, ref_id);
  1384. if (!access)
  1385. return index;
  1386. if (!isl_map_has_tuple_name(access->access, isl_dim_out))
  1387. return index;
  1388. name = get_outer_array_name(access->access);
  1389. i = find_array_index(data->kernel, name);
  1390. if (i < 0)
  1391. isl_die(isl_multi_pw_aff_get_ctx(index), isl_error_internal,
  1392. "cannot find array",
  1393. return isl_multi_pw_aff_free(index));
  1394. data->local_array = &data->kernel->array[i];
  1395. data->array = data->local_array->array;
  1396. group = find_ref_group(data->local_array, access);
  1397. if (!group) {
  1398. data->global = 1;
  1399. return index;
  1400. }
  1401. tile = gpu_array_ref_group_tile(group);
  1402. data->global = !tile;
  1403. if (!tile)
  1404. return index;
  1405. space = isl_space_domain(isl_multi_aff_get_space(tile->tiling));
  1406. space = isl_space_range(isl_space_unwrap(space));
  1407. space = isl_space_map_from_set(space);
  1408. pma = isl_pw_multi_aff_identity(space);
  1409. sched2depth = isl_pw_multi_aff_copy(data->sched2copy);
  1410. dim = isl_pw_multi_aff_dim(sched2depth, isl_dim_out);
  1411. sched2depth = isl_pw_multi_aff_drop_dims(sched2depth, isl_dim_out,
  1412. tile->depth, dim - tile->depth);
  1413. pma = isl_pw_multi_aff_product(sched2depth, pma);
  1414. tiling = isl_multi_pw_aff_from_multi_aff(
  1415. isl_multi_aff_copy(tile->tiling));
  1416. tiling = isl_multi_pw_aff_pullback_pw_multi_aff(tiling, pma);
  1417. index = tile_outer(index, tiling);
  1418. return index;
  1419. }
  1420. /* Dereference "expr" by adding an index [0].
  1421. * The original "expr" is assumed not to have any indices.
  1422. *
  1423. * If "expr" is a member access, then the dereferencing needs
  1424. * to be applied to the structure argument of this member access.
  1425. */
  1426. static __isl_give isl_ast_expr *dereference(__isl_take isl_ast_expr *expr)
  1427. {
  1428. isl_ctx *ctx;
  1429. isl_ast_expr *arg0, *res;
  1430. isl_ast_expr_list *list;
  1431. arg0 = isl_ast_expr_get_op_arg(expr, 0);
  1432. if (!arg0)
  1433. return isl_ast_expr_free(expr);
  1434. if (isl_ast_expr_get_type(arg0) == isl_ast_expr_op &&
  1435. isl_ast_expr_get_op_type(arg0) == isl_ast_op_member) {
  1436. isl_ast_expr *arg;
  1437. arg = isl_ast_expr_get_op_arg(arg0, 0);
  1438. arg = dereference(arg);
  1439. arg0 = isl_ast_expr_set_op_arg(arg0, 0, arg);
  1440. expr = isl_ast_expr_set_op_arg(expr, 0, arg0);
  1441. return expr;
  1442. }
  1443. isl_ast_expr_free(arg0);
  1444. ctx = isl_ast_expr_get_ctx(expr);
  1445. res = isl_ast_expr_from_val(isl_val_zero(ctx));
  1446. list = isl_ast_expr_list_from_ast_expr(res);
  1447. res = isl_ast_expr_get_op_arg(expr, 0);
  1448. res = isl_ast_expr_access(res, list);
  1449. isl_ast_expr_free(expr);
  1450. return res;
  1451. }
  1452. /* Linearize the index expression "expr" based on the array bounds
  1453. * of "array".
  1454. *
  1455. * That is, transform expression
  1456. *
  1457. * A[i_0][i_1]...[i_n]
  1458. *
  1459. * to
  1460. *
  1461. * A[(..((i_0 * b_1 + i_1) ... ) * b_n + i_n]
  1462. *
  1463. * where b_0, b_1, ..., b_n are the bounds on the array.
  1464. *
  1465. * If the base of "expr" is a member access, then the linearization needs
  1466. * to be applied to the structure argument of this member access.
  1467. *
  1468. * In the base case, if "expr" has no arguments (other than the name of
  1469. * the array), then we are passing an entire array to a function.
  1470. * In this case, there is nothing to linearize.
  1471. * Note that at this point an expression with no arguments can
  1472. * only be an entire array because the scalar case and
  1473. * the case of single struct are handled by the caller.
  1474. *
  1475. * If the number of specified index expressions in "expr"
  1476. * is smaller than the dimension of the accessed array,
  1477. * then the missing i_j also do not appear in the linearized expression.
  1478. * Furthermore, since such an expression does not refer to a single
  1479. * element while the default linearized expression would refer to
  1480. * a single element, we return the expression
  1481. *
  1482. * A + (..((i_0 * b_1 + i_1) ... ) * b_l + i_l)
  1483. *
  1484. * instead. Note that because of the special case handling above,
  1485. * we can assume here that there is at least one index expression.
  1486. */
  1487. __isl_give isl_ast_expr *gpu_local_array_info_linearize_index(
  1488. struct gpu_local_array_info *array, __isl_take isl_ast_expr *expr)
  1489. {
  1490. int i, n;
  1491. isl_ast_expr *arg0;
  1492. isl_ast_expr *res;
  1493. isl_ast_expr_list *list;
  1494. arg0 = isl_ast_expr_get_op_arg(expr, 0);
  1495. if (isl_ast_expr_get_type(arg0) == isl_ast_expr_op &&
  1496. isl_ast_expr_get_op_type(arg0) == isl_ast_op_member) {
  1497. isl_ast_expr *arg;
  1498. arg = isl_ast_expr_get_op_arg(arg0, 0);
  1499. arg = gpu_local_array_info_linearize_index(array, arg);
  1500. arg0 = isl_ast_expr_set_op_arg(arg0, 0, arg);
  1501. expr = isl_ast_expr_set_op_arg(expr, 0, arg0);
  1502. return expr;
  1503. }
  1504. isl_ast_expr_free(arg0);
  1505. if (isl_ast_expr_get_op_n_arg(expr) == 1)
  1506. return expr;
  1507. n = isl_ast_expr_get_op_n_arg(expr);
  1508. res = isl_ast_expr_get_op_arg(expr, 1);
  1509. for (i = 1; i < array->n_index; ++i) {
  1510. isl_ast_expr *expr_i;
  1511. expr_i = isl_ast_expr_get_op_arg(array->bound_expr, 1 + i);
  1512. res = isl_ast_expr_mul(res, expr_i);
  1513. if (i + 1 >= n)
  1514. continue;
  1515. expr_i = isl_ast_expr_get_op_arg(expr, i + 1);
  1516. res = isl_ast_expr_add(res, expr_i);
  1517. }
  1518. if (1 + array->n_index > n) {
  1519. res = isl_ast_expr_add(isl_ast_expr_get_op_arg(expr, 0), res);
  1520. } else {
  1521. list = isl_ast_expr_list_from_ast_expr(res);
  1522. res = isl_ast_expr_get_op_arg(expr, 0);
  1523. res = isl_ast_expr_access(res, list);
  1524. }
  1525. isl_ast_expr_free(expr);
  1526. return res;
  1527. }
  1528. /* AST expression transformation callback for pet_stmt_build_ast_exprs.
  1529. *
  1530. * If the AST expression refers to an array that is not accessed
  1531. * at all, then this means the value of the expression is not used,
  1532. * so we might as well print zero (NULL pointer) instead.
  1533. *
  1534. * If the AST expression refers to a global scalar that is not
  1535. * a read-only scalar, then its address was passed to the kernel and
  1536. * we need to dereference it.
  1537. *
  1538. * If the AST expression refers to an access to a global array,
  1539. * then we linearize the access exploiting the bounds in data->local_array.
  1540. */
  1541. static __isl_give isl_ast_expr *transform_expr(__isl_take isl_ast_expr *expr,
  1542. __isl_keep isl_id *id, void *user)
  1543. {
  1544. struct ppcg_transform_data *data = user;
  1545. if (!data->array)
  1546. return expr;
  1547. if (!data->array->accessed) {
  1548. isl_ctx *ctx;
  1549. ctx = isl_ast_expr_get_ctx(expr);
  1550. isl_ast_expr_free(expr);
  1551. return isl_ast_expr_from_val(isl_val_zero(ctx));
  1552. }
  1553. if (gpu_array_is_read_only_scalar(data->array))
  1554. return expr;
  1555. if (!data->global)
  1556. return expr;
  1557. if (data->array->n_index == 0)
  1558. return dereference(expr);
  1559. if (!data->array->linearize)
  1560. return expr;
  1561. return gpu_local_array_info_linearize_index(data->local_array, expr);
  1562. }
  1563. /* This function is called for each instance of a user statement
  1564. * in the kernel "kernel", identified by "gpu_stmt".
  1565. * "kernel" may be NULL if we are not inside a kernel.
  1566. *
  1567. * We attach a struct ppcg_kernel_stmt to the "node", containing
  1568. * a computed AST expression for each access, through an annotation
  1569. * with name "user".
  1570. * These AST expressions are computed from iterator_map,
  1571. * which expresses the domain
  1572. * elements in terms of the generated loops, and sched2copy,
  1573. * which expresses the outer copy_schedule_dim dimensions of
  1574. * the kernel schedule computed by PPCG in terms of the generated loops.
  1575. */
  1576. static __isl_give isl_ast_node *create_domain_leaf(
  1577. struct ppcg_kernel *kernel, __isl_take isl_ast_node *node,
  1578. __isl_keep isl_ast_build *build, struct gpu_stmt *gpu_stmt,
  1579. struct gpu_gen *gen)
  1580. {
  1581. struct ppcg_transform_data data;
  1582. struct ppcg_kernel_stmt *stmt;
  1583. isl_ctx *ctx;
  1584. isl_id *id;
  1585. isl_pw_multi_aff *sched2copy;
  1586. isl_map *map;
  1587. isl_pw_multi_aff *iterator_map;
  1588. isl_union_map *schedule;
  1589. if (!node)
  1590. return NULL;
  1591. ctx = isl_ast_node_get_ctx(node);
  1592. stmt = isl_calloc_type(ctx, struct ppcg_kernel_stmt);
  1593. if (!stmt)
  1594. return isl_ast_node_free(node);
  1595. schedule = isl_ast_build_get_schedule(build);
  1596. map = isl_map_reverse(isl_map_from_union_map(schedule));
  1597. iterator_map = isl_pw_multi_aff_from_map(map);
  1598. if (kernel)
  1599. sched2copy = compute_sched_to_copy(kernel,
  1600. isl_pw_multi_aff_copy(iterator_map));
  1601. else
  1602. sched2copy = NULL;
  1603. stmt->type = ppcg_kernel_domain;
  1604. stmt->u.d.stmt = gpu_stmt;
  1605. data.kernel = kernel;
  1606. data.accesses = stmt->u.d.stmt->accesses;
  1607. data.iterator_map = iterator_map;
  1608. data.sched2copy = sched2copy;
  1609. stmt->u.d.ref2expr = gen->build_ast_expr(stmt->u.d.stmt->stmt,
  1610. build, &transform_index, &data,
  1611. &transform_expr, &data);
  1612. isl_pw_multi_aff_free(iterator_map);
  1613. isl_pw_multi_aff_free(sched2copy);
  1614. id = isl_id_alloc(ctx, "user", stmt);
  1615. id = isl_id_set_free_user(id, &ppcg_kernel_stmt_free);
  1616. return isl_ast_node_set_annotation(node, id);
  1617. }
  1618. /* This function is called for each statement node in the AST
  1619. * for copying to or from shared/private memory.
  1620. * Attach a pointer to a ppcg_kernel_stmt representing the copy
  1621. * statement to the node.
  1622. * The statement name is "read" or "write", depending on whether we are
  1623. * reading from global memory or writing to global memory.
  1624. *
  1625. * The schedule is of the form
  1626. *
  1627. * type[D -> A] -> L
  1628. *
  1629. * where D corresponds to the outer tile->depth dimensions of
  1630. * the kernel schedule, A to the global array and L to the outer
  1631. * generated AST schedule.
  1632. * We compute the inverse and strip off the type, resulting in
  1633. *
  1634. * L -> [D -> A]
  1635. *
  1636. * We combine this mapping with on the one hand the projection
  1637. *
  1638. * [D -> A] -> A
  1639. *
  1640. * and on the other hand the group tiling
  1641. *
  1642. * [D -> A] -> T
  1643. *
  1644. * resulting in
  1645. *
  1646. * L -> A and L -> T
  1647. *
  1648. * and store the corresponding expressions in stmt->index and stmt->local_index,
  1649. * where stmt points to the ppcg_kernel_stmt that is attached to the node.
  1650. * stmt->index is linearized if the global memory array is linearized.
  1651. */
  1652. static __isl_give isl_ast_node *create_access_leaf(struct ppcg_kernel *kernel,
  1653. struct gpu_array_ref_group *group, __isl_take isl_ast_node *node,
  1654. __isl_keep isl_ast_build *build)
  1655. {
  1656. struct ppcg_kernel_stmt *stmt;
  1657. struct gpu_array_tile *tile;
  1658. isl_id *id;
  1659. isl_ast_expr *expr;
  1660. isl_space *space;
  1661. isl_map *access;
  1662. isl_pw_multi_aff *pma, *pma2;
  1663. const char *type;
  1664. stmt = isl_calloc_type(kernel->ctx, struct ppcg_kernel_stmt);
  1665. if (!stmt)
  1666. return isl_ast_node_free(node);
  1667. access = isl_map_from_union_map(isl_ast_build_get_schedule(build));
  1668. type = isl_map_get_tuple_name(access, isl_dim_in);
  1669. stmt->u.c.read = !strcmp(type, "read");
  1670. access = isl_map_reverse(access);
  1671. pma = isl_pw_multi_aff_from_map(access);
  1672. pma = isl_pw_multi_aff_reset_tuple_id(pma, isl_dim_out);
  1673. space = isl_space_range(isl_pw_multi_aff_get_space(pma));
  1674. space = isl_space_unwrap(space);
  1675. pma2 = isl_pw_multi_aff_range_map(space);
  1676. pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(pma2,
  1677. isl_pw_multi_aff_copy(pma));
  1678. expr = isl_ast_build_access_from_pw_multi_aff(build, pma2);
  1679. if (group->array->linearize)
  1680. expr = gpu_local_array_info_linearize_index(group->local_array,
  1681. expr);
  1682. stmt->u.c.index = expr;
  1683. tile = gpu_array_ref_group_tile(group);
  1684. pma2 = isl_pw_multi_aff_from_multi_aff(
  1685. isl_multi_aff_copy(tile->tiling));
  1686. pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(pma2, pma);
  1687. expr = isl_ast_build_access_from_pw_multi_aff(build, pma2);
  1688. stmt->u.c.local_index = expr;
  1689. stmt->u.c.array = group->array;
  1690. stmt->u.c.local_array = group->local_array;
  1691. stmt->type = ppcg_kernel_copy;
  1692. id = isl_id_alloc(kernel->ctx, "copy", stmt);
  1693. id = isl_id_set_free_user(id, &ppcg_kernel_stmt_free);
  1694. return isl_ast_node_set_annotation(node, id);
  1695. }
  1696. /* Create a synchronization ppcg_kernel_stmt and
  1697. * attach it to the node "node" representing the synchronization.
  1698. */
  1699. static __isl_give isl_ast_node *create_sync_leaf(
  1700. struct ppcg_kernel *kernel, __isl_take isl_ast_node *node,
  1701. __isl_keep isl_ast_build *build)
  1702. {
  1703. struct ppcg_kernel_stmt *stmt;
  1704. isl_id *id;
  1705. stmt = isl_calloc_type(kernel->ctx, struct ppcg_kernel_stmt);
  1706. if (!stmt)
  1707. return isl_ast_node_free(node);
  1708. stmt->type = ppcg_kernel_sync;
  1709. id = isl_id_alloc(kernel->ctx, "sync", stmt);
  1710. id = isl_id_set_free_user(id, &ppcg_kernel_stmt_free);
  1711. return isl_ast_node_set_annotation(node, id);
  1712. }
  1713. /* Build AST expressions for the device array sizes of all arrays in "prog"
  1714. * that require allocation on the device using "build", as well as
  1715. * for the original array sizes of all arrays that need to be declared
  1716. * on the host.
  1717. * "node" is freed in case of error.
  1718. */
  1719. static __isl_give isl_ast_node *build_array_bounds(
  1720. __isl_take isl_ast_node *node, struct gpu_prog *prog,
  1721. __isl_keep isl_ast_build *build)
  1722. {
  1723. int i;
  1724. for (i = 0; i < prog->n_array; ++i) {
  1725. struct gpu_array_info *array = &prog->array[i];
  1726. isl_multi_pw_aff *size;
  1727. isl_ast_expr *expr;
  1728. if (!gpu_array_requires_device_allocation(array))
  1729. continue;
  1730. size = isl_multi_pw_aff_copy(array->bound);
  1731. expr = ppcg_build_size_expr(size, build);
  1732. array->bound_expr = expr;
  1733. if (!expr)
  1734. return isl_ast_node_free(node);
  1735. }
  1736. for (i = 0; i < prog->n_array; ++i) {
  1737. struct gpu_array_info *array = &prog->array[i];
  1738. isl_set *extent;
  1739. isl_multi_pw_aff *size;
  1740. isl_ast_expr *expr;
  1741. if (!array->declare_local)
  1742. continue;
  1743. extent = isl_set_copy(array->declared_extent);
  1744. size = ppcg_size_from_extent(extent);
  1745. expr = ppcg_build_size_expr(size, build);
  1746. array->declared_size = expr;
  1747. if (!expr)
  1748. return isl_ast_node_free(node);
  1749. }
  1750. return node;
  1751. }
  1752. /* Internal data structure for at_domain.
  1753. *
  1754. * "prog" represents the entire scop.
  1755. * "kernel" points to the kernel to which the current schedule node
  1756. * belongs. It is set by before_mark and reset by after_mark.
  1757. * It may be NULL if we are outside any kernel.
  1758. */
  1759. struct ppcg_at_domain_data {
  1760. struct gpu_prog *prog;
  1761. struct gpu_gen *gen;
  1762. struct ppcg_kernel *kernel;
  1763. };
  1764. /* This function is called for each instance of a user statement
  1765. * in the kernel. This may be one of the original user statements
  1766. * or a statement introduced by PPCG.
  1767. *
  1768. * We first check if the statement id corresponds to a gpu statement,
  1769. * which indicates the statement is an original user statement. Any statement
  1770. * that is not an original user statement has been introduced by PPCG and
  1771. * requires special handling.
  1772. *
  1773. * If the user statement is one of the original user statements, then we call
  1774. * create_domain_leaf. If it is "init_device", then we call
  1775. * build_array_bounds. Otherwise, we check if it is a copy or synchronization
  1776. * statement and call the appropriate functions. Statements that copy an array
  1777. * to/from the device do not need any further treatment.
  1778. * Neither does "clear_device".
  1779. */
  1780. static __isl_give isl_ast_node *at_domain(__isl_take isl_ast_node *node,
  1781. __isl_keep isl_ast_build *build, void *user)
  1782. {
  1783. struct ppcg_at_domain_data *data = user;
  1784. struct gpu_stmt *gpu_stmt;
  1785. isl_ast_expr *expr, *arg;
  1786. isl_id *id;
  1787. int is_sync;
  1788. const char *name;
  1789. void *p;
  1790. expr = isl_ast_node_user_get_expr(node);
  1791. arg = isl_ast_expr_get_op_arg(expr, 0);
  1792. id = isl_ast_expr_get_id(arg);
  1793. name = isl_id_get_name(id);
  1794. p = isl_id_get_user(id);
  1795. isl_ast_expr_free(expr);
  1796. isl_ast_expr_free(arg);
  1797. gpu_stmt = find_stmt(data->prog, id);
  1798. is_sync = gpu_tree_id_is_sync(id, data->kernel);
  1799. isl_id_free(id);
  1800. if (gpu_stmt)
  1801. return create_domain_leaf(data->kernel, node, build, gpu_stmt,
  1802. data->gen);
  1803. if (!prefixcmp(name, "to_device_") || !prefixcmp(name, "from_device_"))
  1804. return node;
  1805. if (!strcmp(name, "init_device"))
  1806. return build_array_bounds(node, data->prog, build);
  1807. if (!strcmp(name, "clear_device"))
  1808. return node;
  1809. if (is_sync < 0)
  1810. return isl_ast_node_free(node);
  1811. if (!strcmp(name, "read") || !strcmp(name, "write")) {
  1812. struct gpu_array_ref_group *group = p;
  1813. return create_access_leaf(data->kernel, group, node, build);
  1814. }
  1815. if (!is_sync)
  1816. isl_die(data->prog->ctx, isl_error_internal,
  1817. "unknown statement type",
  1818. return isl_ast_node_free(node));
  1819. return create_sync_leaf(data->kernel, node, build);
  1820. }
  1821. /* Given a set of wrapped references "ref", return the corresponding
  1822. * access relations based on the tagged access relations "tagged".
  1823. *
  1824. * The elements of "ref" are of the form
  1825. *
  1826. * [D -> R]
  1827. *
  1828. * with D an iteration domains and R a reference.
  1829. * The elements of "tagged" are of the form
  1830. *
  1831. * [D -> R] -> A
  1832. *
  1833. * with A an array.
  1834. *
  1835. * Extend "tagged" to include the iteration domain in the range, i.e.,
  1836. *
  1837. * [D -> R] -> [D -> A]
  1838. *
  1839. * apply the result to "ref" and then unwrap the resulting set
  1840. * to obtain relations of the form
  1841. *
  1842. * D -> A
  1843. */
  1844. static __isl_give isl_union_map *wrapped_reference_to_access(
  1845. __isl_take isl_union_set *ref, __isl_take isl_union_map *tagged)
  1846. {
  1847. isl_union_map *tag2access;
  1848. tag2access = isl_union_map_copy(tagged);
  1849. tag2access = isl_union_map_universe(tag2access);
  1850. tag2access = isl_union_set_unwrap(isl_union_map_domain(tag2access));
  1851. tag2access = isl_union_map_domain_map(tag2access);
  1852. tag2access = isl_union_map_range_product(tag2access, tagged);
  1853. ref = isl_union_set_coalesce(ref);
  1854. ref = isl_union_set_apply(ref, tag2access);
  1855. return isl_union_set_unwrap(ref);
  1856. }
  1857. /* Given an access relation "access" from one or more array reference groups,
  1858. * remove those reads if ("read" is 1) or writes (if "read" is 0)
  1859. * that are only needed to communicate data within
  1860. * the same iteration of "sched".
  1861. * The domain of "sched" corresponds to the original statement instances,
  1862. * i.e., those that appear in the domains of the access relations.
  1863. * "tagged" contains all tagged access relations to all
  1864. * the array reference groups accessed by "access" from statement
  1865. * instances scheduled by "sched".
  1866. *
  1867. * If the access is a read then it is either an element of
  1868. *
  1869. * live_in union (range flow)
  1870. *
  1871. * where live_in and flow may be overapproximations, or
  1872. * it reads an uninitialized value (that is not live-in because
  1873. * there is an intermediate kill) or it reads a value that was
  1874. * written within the same (compound) statement instance.
  1875. * If the access is a write then it is either an element of
  1876. *
  1877. * live_out union (domain flow)
  1878. *
  1879. * or it writes a value that is never read (and is not live-out
  1880. * because of an intermediate kill) or only
  1881. * within the same (compound) statement instance.
  1882. * In both cases, the access relation is also a subset of
  1883. * the group access relation.
  1884. *
  1885. * The cases where an uninitialized value is read or a value is written
  1886. * that is never read or where the dataflow occurs within a statement
  1887. * instance are also considered local and may also be removed.
  1888. *
  1889. * Essentially, we compute the intersection of "access" with either
  1890. *
  1891. * live_in union (range non-local-flow)
  1892. *
  1893. * or
  1894. *
  1895. * live_out union (domain non-local-flow)
  1896. *
  1897. * We first construct a relation "local"
  1898. *
  1899. * [[D -> R] -> [D' -> R']]
  1900. *
  1901. * of pairs of domain iterations accessing the reference group
  1902. * and references in the group that are coscheduled by "sched".
  1903. *
  1904. * If this relation does not intersect the dataflow dependences,
  1905. * then there is nothing we can possibly remove, unless the dataflow
  1906. * dependences themselves only relate a subset of the accesses.
  1907. * In particular, the accesses may not be involved in any dataflow
  1908. * dependences, either because they are uninitialized reads/dead writes
  1909. * or because the dataflow occurs inside a statement instance.
  1910. *
  1911. * Since the computation below may break up the access relation
  1912. * into smaller pieces, we only perform the intersection with
  1913. * the non-local dependent accesses if the local pairs
  1914. * intersect the dataflow dependences. Otherwise, we intersect
  1915. * with the universe of the non-local dependent accesses.
  1916. * This should at least remove accesses from statements that
  1917. * do not participate in any dependences.
  1918. *
  1919. * In particular, we remove the "local" dataflow dependences from
  1920. * the set of all dataflow dependences, or at least those
  1921. * that may contribute to a domain/range that intersects
  1922. * the domain of "access".
  1923. * Note that if the potential dataflow dependences are an overapproximation
  1924. * of the actual dataflow dependences, then the result remains an
  1925. * overapproximation of the non-local dataflow dependences.
  1926. * Copying to/from global memory is only needed for the references
  1927. * in the domain/range of the result or for accesses that are live out/in
  1928. * for the entire scop.
  1929. *
  1930. * We therefore map the domain/range of the "external" relation
  1931. * to the corresponding access relation and take the union with
  1932. * the live out/in relation.
  1933. */
  1934. static __isl_give isl_union_map *remove_local_accesses(
  1935. struct gpu_prog *prog, __isl_take isl_union_map *tagged,
  1936. __isl_take isl_union_map *access, __isl_take isl_union_map *sched,
  1937. int read)
  1938. {
  1939. int empty;
  1940. isl_union_pw_multi_aff *tagger;
  1941. isl_union_set *domain, *access_domain;
  1942. isl_union_map *local, *external, *universe;
  1943. isl_union_set *tag_set;
  1944. if (isl_union_map_is_empty(access)) {
  1945. isl_union_map_free(sched);
  1946. isl_union_map_free(tagged);
  1947. return access;
  1948. }
  1949. tagger = isl_union_pw_multi_aff_copy(prog->scop->tagger);
  1950. domain = isl_union_map_domain(isl_union_map_copy(tagged));
  1951. tagger = isl_union_pw_multi_aff_intersect_domain(tagger,
  1952. isl_union_set_copy(domain));
  1953. sched = isl_union_map_preimage_domain_union_pw_multi_aff(sched, tagger);
  1954. local = isl_union_map_apply_range(sched,
  1955. isl_union_map_reverse(isl_union_map_copy(sched)));
  1956. local = isl_union_map_intersect(local,
  1957. isl_union_map_copy(prog->scop->tagged_dep_flow));
  1958. empty = isl_union_map_is_empty(local);
  1959. external = isl_union_map_copy(prog->scop->tagged_dep_flow);
  1960. universe = isl_union_map_universe(isl_union_map_copy(access));
  1961. access_domain = isl_union_map_domain(universe);
  1962. domain = isl_union_set_universe(domain);
  1963. universe = isl_union_set_unwrap(domain);
  1964. universe = isl_union_map_intersect_domain(universe, access_domain);
  1965. domain = isl_union_map_wrap(universe);
  1966. if (read)
  1967. external = isl_union_map_intersect_range(external, domain);
  1968. else
  1969. external = isl_union_map_intersect_domain(external, domain);
  1970. external = isl_union_map_intersect_params(external,
  1971. isl_set_copy(prog->scop->context));
  1972. external = isl_union_map_subtract(external, local);
  1973. if (read) {
  1974. tag_set = isl_union_map_range(external);
  1975. external = wrapped_reference_to_access(tag_set, tagged);
  1976. external = isl_union_map_union(external,
  1977. isl_union_map_copy(prog->scop->live_in));
  1978. } else {
  1979. tag_set = isl_union_map_domain(external);
  1980. external = wrapped_reference_to_access(tag_set, tagged);
  1981. external = isl_union_map_union(external,
  1982. isl_union_map_copy(prog->scop->live_out));
  1983. }
  1984. if (empty < 0)
  1985. external = isl_union_map_free(external);
  1986. else if (empty)
  1987. external = isl_union_map_universe(external);
  1988. access = isl_union_map_intersect(access, external);
  1989. return access;
  1990. }
  1991. /* Given an access relation "access" from "group", remove those reads
  1992. * if ("read" is 1) or writes (if "read" is 0) that are only needed to
  1993. * communicate data within the same iteration of the schedule "prefix"
  1994. * at the position where the copying of the group is inserted.
  1995. * That is, the output dimension of "prefix"
  1996. * is equal to tile->depth.
  1997. * The domain of "prefix" corresponds to the original statement instances,
  1998. * i.e., those that appear in the domains of the access relations.
  1999. *
  2000. * Extract the tagged access relation of "group" and
  2001. * then call remove_local_accesses.
  2002. */
  2003. static __isl_give isl_union_map *remove_local_accesses_group(
  2004. struct ppcg_kernel *kernel, struct gpu_array_ref_group *group,
  2005. __isl_take isl_union_map *access, __isl_keep isl_union_map *prefix,
  2006. int read)
  2007. {
  2008. isl_union_map *sched, *tagged;
  2009. if (isl_union_map_is_empty(access))
  2010. return access;
  2011. tagged = group_tagged_access_relation(group);
  2012. sched = isl_union_map_copy(prefix);
  2013. return remove_local_accesses(kernel->prog, tagged, access, sched, read);
  2014. }
  2015. /* Build an access AST expression for the effective grid size using "build".
  2016. * Store the result in kernel->grid_size_expr.
  2017. */
  2018. static isl_stat build_grid_size(struct ppcg_kernel *kernel,
  2019. __isl_keep isl_ast_build *build)
  2020. {
  2021. isl_multi_pw_aff *size;
  2022. size = isl_multi_pw_aff_copy(kernel->grid_size);
  2023. size = isl_multi_pw_aff_set_tuple_name(size, isl_dim_out, "grid");
  2024. kernel->grid_size_expr = ppcg_build_size_expr(size, build);
  2025. if (!kernel->grid_size_expr)
  2026. return isl_stat_error;
  2027. return isl_stat_ok;
  2028. }
  2029. /* Build access AST expressions for the localized array sizes using "build".
  2030. * Store the result in local->bound_expr.
  2031. * Only do this for arrays for which localized bounds have been computed.
  2032. */
  2033. static isl_stat build_local_array_sizes(struct ppcg_kernel *kernel,
  2034. __isl_keep isl_ast_build *build)
  2035. {
  2036. int i;
  2037. for (i = 0; i < kernel->n_array; ++i) {
  2038. struct gpu_local_array_info *local = &kernel->array[i];
  2039. isl_multi_pw_aff *size;
  2040. if (local->n_group == 0)
  2041. continue;
  2042. size = isl_multi_pw_aff_copy(local->bound);
  2043. local->bound_expr = ppcg_build_size_expr(size, build);
  2044. if (!local->bound_expr)
  2045. return isl_stat_error;
  2046. }
  2047. return isl_stat_ok;
  2048. }
  2049. /* Build access AST expressions for the effective grid size and
  2050. * the localized array sizes using "build".
  2051. */
  2052. static isl_stat build_grid_and_local_array_sizes(struct ppcg_kernel *kernel,
  2053. __isl_keep isl_ast_build *build)
  2054. {
  2055. if (build_grid_size(kernel, build) < 0)
  2056. return isl_stat_error;
  2057. if (build_local_array_sizes(kernel, build) < 0)
  2058. return isl_stat_error;
  2059. return isl_stat_ok;
  2060. }
  2061. /* This function is called before the AST generator starts traversing
  2062. * the schedule subtree of a node with mark "mark".
  2063. *
  2064. * If the mark is called "kernel", store the kernel pointer in data->kernel
  2065. * for use in at_domain and build AST expressions for the grid size and
  2066. * the localized array sizes.
  2067. */
  2068. static isl_stat before_mark(__isl_keep isl_id *mark,
  2069. __isl_keep isl_ast_build *build, void *user)
  2070. {
  2071. struct ppcg_at_domain_data *data = user;
  2072. if (!mark)
  2073. return isl_stat_error;
  2074. if (!strcmp(isl_id_get_name(mark), "kernel")) {
  2075. data->kernel = isl_id_get_user(mark);
  2076. if (build_grid_and_local_array_sizes(data->kernel, build) < 0)
  2077. return isl_stat_error;
  2078. }
  2079. return isl_stat_ok;
  2080. }
  2081. /* This function is called after the AST generator has finished traversing
  2082. * the schedule subtree of a mark node. "node" points to the corresponding
  2083. * mark AST node.
  2084. *
  2085. * If the mark is called "kernel", then replace "node" by a user node
  2086. * that "calls" the kernel, representing the launch of the kernel.
  2087. * The original "node" is stored inside the kernel object so that
  2088. * it can be used to print the device code.
  2089. * Note that this assumes that a kernel is only launched once.
  2090. * Also clear data->kernel.
  2091. */
  2092. static __isl_give isl_ast_node *after_mark(__isl_take isl_ast_node *node,
  2093. __isl_keep isl_ast_build *build, void *user)
  2094. {
  2095. isl_ctx *ctx;
  2096. isl_id *id;
  2097. isl_ast_expr *expr;
  2098. isl_ast_expr_list *list;
  2099. struct ppcg_kernel *kernel;
  2100. struct ppcg_at_domain_data *data = user;
  2101. ctx = isl_ast_node_get_ctx(node);
  2102. id = isl_ast_node_mark_get_id(node);
  2103. if (!id)
  2104. return isl_ast_node_free(node);
  2105. if (strcmp(isl_id_get_name(id), "kernel") || !data->kernel) {
  2106. isl_id_free(id);
  2107. return node;
  2108. }
  2109. kernel = data->kernel;
  2110. data->kernel = NULL;
  2111. kernel->space = isl_ast_build_get_schedule_space(build);
  2112. kernel->tree = isl_ast_node_mark_get_node(node);
  2113. isl_ast_node_free(node);
  2114. expr = isl_ast_expr_from_id(isl_id_copy(id));
  2115. list = isl_ast_expr_list_alloc(ctx, 0);
  2116. expr = isl_ast_expr_call(expr, list);
  2117. node = isl_ast_node_alloc_user(expr);
  2118. node = isl_ast_node_set_annotation(node, id);
  2119. return node;
  2120. }
  2121. static isl_bool update_depth(__isl_keep isl_schedule_node *node, void *user)
  2122. {
  2123. int *depth = user;
  2124. int node_depth;
  2125. if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
  2126. return isl_bool_true;
  2127. node_depth = isl_schedule_node_get_schedule_depth(node);
  2128. if (node_depth > *depth)
  2129. *depth = node_depth;
  2130. return isl_bool_false;
  2131. }
  2132. /* Use isl to generate code for both the host and the device
  2133. * from "schedule".
  2134. * The device code is marked by "kernel" mark nodes in the schedule tree,
  2135. * containing a pointer to a ppcg_kernel object.
  2136. * The returned AST only contains the AST for the host code.
  2137. * The ASTs for the device code are embedded in ppcg_kernel objects
  2138. * attached to the leaf nodes that call "kernel".
  2139. */
  2140. __isl_give isl_ast_node *generate_code(struct gpu_gen *gen,
  2141. __isl_take isl_schedule *schedule)
  2142. {
  2143. struct ppcg_at_domain_data data;
  2144. isl_ast_build *build;
  2145. isl_ast_node *tree;
  2146. isl_id_list *iterators;
  2147. int depth;
  2148. data.prog = gen->prog;
  2149. data.gen = gen;
  2150. data.kernel = NULL;
  2151. depth = 0;
  2152. if (isl_schedule_foreach_schedule_node_top_down(schedule, &update_depth,
  2153. &depth) < 0)
  2154. return NULL;
  2155. build = isl_ast_build_alloc(gen->prog->ctx);
  2156. iterators = ppcg_scop_generate_names(gen->prog->scop, depth, "c");
  2157. build = isl_ast_build_set_iterators(build, iterators);
  2158. build = isl_ast_build_set_at_each_domain(build, &at_domain, &data);
  2159. build = isl_ast_build_set_before_each_mark(build, &before_mark, &data);
  2160. build = isl_ast_build_set_after_each_mark(build, &after_mark, &data);
  2161. if (gen->prog->scop->options->debug->dump_final_schedule)
  2162. isl_schedule_dump(schedule);
  2163. tree = isl_ast_build_node_from_schedule(build, schedule);
  2164. isl_ast_build_free(build);
  2165. return tree;
  2166. }
  2167. __isl_give isl_union_map *extract_sizes_from_str(isl_ctx *ctx, const char *str)
  2168. {
  2169. if (!str)
  2170. return NULL;
  2171. return isl_union_map_read_from_str(ctx, str);
  2172. }
  2173. /* Can "node" be tiled and then mapped to block and thread identifiers?
  2174. * That is, is it permutable with at least one coincident dimension?
  2175. */
  2176. static int is_permutable(__isl_keep isl_schedule_node *node)
  2177. {
  2178. if (!node)
  2179. return -1;
  2180. if (isl_schedule_node_get_type(node) != isl_schedule_node_band)
  2181. return 0;
  2182. if (!isl_schedule_node_band_get_permutable(node))
  2183. return 0;
  2184. if (isl_schedule_node_band_n_member(node) < 1)
  2185. return 0;
  2186. if (!isl_schedule_node_band_member_get_coincident(node, 0))
  2187. return 0;
  2188. return 1;
  2189. }
  2190. /* A isl_schedule_foreach_schedule_node_top_down callback
  2191. * for setting *any_permutable and aborting the search
  2192. * if "node" is a permutable band with coincident dimensions.
  2193. * Otherwise, continue searching.
  2194. */
  2195. static isl_bool set_permutable(__isl_keep isl_schedule_node *node, void *user)
  2196. {
  2197. int *any_permutable = user;
  2198. int permutable;
  2199. permutable = is_permutable(node);
  2200. if (permutable < 0)
  2201. return isl_bool_error;
  2202. if (!permutable)
  2203. return isl_bool_true;
  2204. *any_permutable = 1;
  2205. return isl_bool_error;
  2206. }
  2207. /* Does the subtree rooted at "node" have any suitably permutable band nodes?
  2208. * That is, does it have any nodes that are permutable and that
  2209. * have a least one coincident dimension?
  2210. */
  2211. static int subtree_has_permutable_bands(__isl_keep isl_schedule_node *node)
  2212. {
  2213. int any_parallelism = 0;
  2214. if (isl_schedule_node_foreach_descendant_top_down(node, &set_permutable,
  2215. &any_parallelism) < 0 &&
  2216. !any_parallelism)
  2217. return -1;
  2218. return any_parallelism;
  2219. }
  2220. /* Does "schedule" contain any permutable band with at least one coincident
  2221. * member?
  2222. */
  2223. int has_any_permutable_node(__isl_keep isl_schedule *schedule)
  2224. {
  2225. isl_schedule_node *root;
  2226. int any_permutable;
  2227. root = isl_schedule_get_root(schedule);
  2228. any_permutable = subtree_has_permutable_bands(root);
  2229. isl_schedule_node_free(root);
  2230. return any_permutable;
  2231. }
  2232. /* Is "node" a candidate for mapping to block and thread identifiers?
  2233. * In particular, is it permutable with at least one coincident dimension?
  2234. * Alternatively, does the subtree rooted at "node" not contain
  2235. * any such permutable node? Filter nodes are skipped in this case,
  2236. * because a band node will be inserted in front of the returned
  2237. * node and this is not possible for filter nodes that are children
  2238. * of set or sequence nodes.
  2239. */
  2240. static int is_candidate(__isl_keep isl_schedule_node *node)
  2241. {
  2242. int permutable;
  2243. if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf)
  2244. return 1;
  2245. permutable = is_permutable(node);
  2246. if (permutable < 0 || permutable)
  2247. return permutable;
  2248. if (isl_schedule_node_get_type(node) == isl_schedule_node_filter)
  2249. return 0;
  2250. permutable = subtree_has_permutable_bands(node);
  2251. if (permutable < 0)
  2252. return -1;
  2253. return !permutable;
  2254. }
  2255. /* Is "node" the outermost node in its branch that can be tiled
  2256. * and then mapped to block and thread identifiers?
  2257. * If there are no such nodes in the subtree at "node" and
  2258. * if "node" is not a filter node, then it is accepted too.
  2259. */
  2260. static int is_outer_tilable(__isl_keep isl_schedule_node *node)
  2261. {
  2262. int tilable;
  2263. isl_schedule_node *ancestor;
  2264. tilable = is_candidate(node);
  2265. if (tilable < 0)
  2266. return -1;
  2267. if (!tilable)
  2268. return 0;
  2269. tilable = 0;
  2270. ancestor = isl_schedule_node_copy(node);
  2271. while (isl_schedule_node_has_parent(ancestor)) {
  2272. ancestor = isl_schedule_node_parent(ancestor);
  2273. tilable = is_candidate(ancestor);
  2274. if (tilable < 0 || tilable)
  2275. break;
  2276. }
  2277. isl_schedule_node_free(ancestor);
  2278. return tilable < 0 ? -1 : !tilable;
  2279. }
  2280. /* Collect the references to all writes in "group".
  2281. * Each reference is represented by a universe set in a space
  2282. *
  2283. * [S[i,j] -> R[]]
  2284. *
  2285. * with S[i,j] the statement instance space and R[] the array reference.
  2286. */
  2287. static __isl_give isl_union_set *group_tagged_writes(
  2288. struct gpu_array_ref_group *group)
  2289. {
  2290. int i;
  2291. isl_space *space;
  2292. isl_union_set *writes;
  2293. space = isl_map_get_space(group->access);
  2294. writes = isl_union_set_empty(space);
  2295. for (i = 0; i < group->n_ref; ++i) {
  2296. isl_space *space;
  2297. isl_set *writes_i;
  2298. if (!group->refs[i]->write)
  2299. continue;
  2300. space = isl_map_get_space(group->refs[i]->tagged_access);
  2301. space = isl_space_domain(space);
  2302. writes_i = isl_set_universe(space);
  2303. writes = isl_union_set_add_set(writes, writes_i);
  2304. }
  2305. return writes;
  2306. }
  2307. /* Is there any write access in "group" that requires synchronization
  2308. * on a write to global memory?
  2309. * We currently take into account all writes that would require
  2310. * synchronization at the thread level depth, but if the copying
  2311. * for this group is performed at an outer level, then we do not
  2312. * actually need to take into account dependences at intermediate levels.
  2313. */
  2314. static int any_sync_writes_in_group(struct ppcg_kernel *kernel,
  2315. struct gpu_array_ref_group *group)
  2316. {
  2317. isl_union_set *writes;
  2318. int empty, disjoint;
  2319. empty = isl_union_set_is_empty(kernel->sync_writes);
  2320. if (empty < 0)
  2321. return -1;
  2322. if (empty)
  2323. return 0;
  2324. writes = group_tagged_writes(group);
  2325. disjoint = isl_union_set_is_disjoint(kernel->sync_writes, writes);
  2326. isl_union_set_free(writes);
  2327. return disjoint < 0 ? -1 : !disjoint;
  2328. }
  2329. /* Collect the references to all writes in "kernel" that write directly
  2330. * to global or shared memory, i.e., that are not mapped to private memory.
  2331. * Each reference is represented by a universe set in a space
  2332. *
  2333. * [S[i,j] -> R[]]
  2334. *
  2335. * with S[i,j] the statement instance space and R[] the array reference.
  2336. */
  2337. static __isl_give isl_union_set *collect_non_private_tagged_writes(
  2338. struct ppcg_kernel *kernel)
  2339. {
  2340. isl_union_set *writes;
  2341. int i, j;
  2342. writes = isl_union_set_empty(isl_union_set_get_space(kernel->arrays));
  2343. for (i = 0; i < kernel->n_array; ++i) {
  2344. struct gpu_local_array_info *array = &kernel->array[i];
  2345. for (j = 0; j < array->n_group; ++j) {
  2346. struct gpu_array_ref_group *group = array->groups[j];
  2347. enum ppcg_group_access_type type;
  2348. isl_union_set *writes_ij;
  2349. if (!group->write)
  2350. continue;
  2351. type = gpu_array_ref_group_type(group);
  2352. if (type == ppcg_access_private)
  2353. continue;
  2354. writes_ij = group_tagged_writes(group);
  2355. writes = isl_union_set_union(writes, writes_ij);
  2356. }
  2357. }
  2358. return writes;
  2359. }
  2360. /* Are there any direct writes to global memory that require
  2361. * synchronization?
  2362. */
  2363. static int any_global_or_shared_sync_writes(struct ppcg_kernel *kernel)
  2364. {
  2365. isl_union_set *writes;
  2366. int empty, disjoint;
  2367. empty = isl_union_set_is_empty(kernel->sync_writes);
  2368. if (empty < 0)
  2369. return -1;
  2370. if (empty)
  2371. return 0;
  2372. writes = collect_non_private_tagged_writes(kernel);
  2373. disjoint = isl_union_set_is_disjoint(kernel->sync_writes, writes);
  2374. isl_union_set_free(writes);
  2375. return disjoint < 0 ? -1 : !disjoint;
  2376. }
  2377. /* Construct an isl_multi_val for use as tile sizes for tiling "node"
  2378. * from the elements in "tile_size".
  2379. */
  2380. static __isl_give isl_multi_val *construct_band_tiles_sizes(
  2381. __isl_keep isl_schedule_node *node, int *tile_size)
  2382. {
  2383. isl_space *space;
  2384. if (!node)
  2385. return NULL;
  2386. space = isl_schedule_node_band_get_space(node);
  2387. return ppcg_multi_val_from_int_list(space, tile_size);
  2388. }
  2389. /* Replace the partial schedule S of the band node "node" by
  2390. *
  2391. * floor(S/f)
  2392. *
  2393. * or
  2394. *
  2395. * f * floor(S/f)
  2396. *
  2397. * if scale_tile_loops is set, with f the integers in "factor".
  2398. * The list that "factor" points to is assumed to contain at least
  2399. * as many elements as the number of members in the band.
  2400. */
  2401. static __isl_give isl_schedule_node *snap_band_to_sizes(
  2402. __isl_take isl_schedule_node *node, int *factor,
  2403. struct ppcg_options *options)
  2404. {
  2405. isl_multi_val *mv;
  2406. mv = construct_band_tiles_sizes(node, factor);
  2407. node = isl_schedule_node_band_scale_down(node, isl_multi_val_copy(mv));
  2408. if (options->scale_tile_loops)
  2409. node = isl_schedule_node_band_scale(node,
  2410. isl_multi_val_copy(mv));
  2411. isl_multi_val_free(mv);
  2412. return node;
  2413. }
  2414. /* Tile "band" with tile size specified by "sizes".
  2415. *
  2416. * Since the tile loops will be mapped to block ids, we forcibly
  2417. * turn off tile loop scaling. We may want to enable tile loop scaling
  2418. * at some later point, but then we would have to support the detection
  2419. * of strides during the mapping to block ids.
  2420. * Similarly, since the point loops will be mapped to thread ids,
  2421. * we forcibly shift the point loops so that they start at zero.
  2422. */
  2423. static __isl_give isl_schedule_node *tile_band(
  2424. __isl_take isl_schedule_node *node, __isl_take isl_multi_val *sizes)
  2425. {
  2426. isl_ctx *ctx = isl_schedule_node_get_ctx(node);
  2427. int scale_tile;
  2428. int shift_point;
  2429. scale_tile = isl_options_get_tile_scale_tile_loops(ctx);
  2430. isl_options_set_tile_scale_tile_loops(ctx, 0);
  2431. shift_point = isl_options_get_tile_shift_point_loops(ctx);
  2432. isl_options_set_tile_shift_point_loops(ctx, 1);
  2433. node = isl_schedule_node_band_tile(node, sizes);
  2434. isl_options_set_tile_scale_tile_loops(ctx, scale_tile);
  2435. isl_options_set_tile_shift_point_loops(ctx, shift_point);
  2436. return node;
  2437. }
  2438. /* Extract the set of parameter values and outer schedule dimensions
  2439. * for which any statement instance
  2440. * in the kernel inserted at "node" needs to be executed.
  2441. * Intersect the set of parameter values derived from the host schedule
  2442. * relation with the context of "prog".
  2443. */
  2444. static __isl_give isl_set *extract_context(__isl_keep isl_schedule_node *node,
  2445. struct gpu_prog *prog)
  2446. {
  2447. isl_union_map *schedule;
  2448. isl_union_set *schedule_domain;
  2449. isl_set *context;
  2450. int empty;
  2451. schedule = isl_schedule_node_get_prefix_schedule_relation(node);
  2452. schedule_domain = isl_union_map_range(schedule);
  2453. empty = isl_union_set_is_empty(schedule_domain);
  2454. if (empty < 0) {
  2455. isl_union_set_free(schedule_domain);
  2456. return NULL;
  2457. }
  2458. if (empty) {
  2459. int depth;
  2460. isl_space *space;
  2461. space = isl_union_set_get_space(schedule_domain);
  2462. isl_union_set_free(schedule_domain);
  2463. space = isl_space_set_from_params(space);
  2464. depth = isl_schedule_node_get_schedule_depth(node);
  2465. space = isl_space_add_dims(space, isl_dim_set, depth);
  2466. context = isl_set_empty(space);
  2467. } else {
  2468. context = isl_set_from_union_set(schedule_domain);
  2469. }
  2470. context = isl_set_intersect_params(context,
  2471. isl_set_copy(prog->context));
  2472. return context;
  2473. }
  2474. /* Return the set of outer array elements accessed by
  2475. * by the statement instances in "domain" in "prog".
  2476. * The instances in "domain" are those that appear
  2477. * in the domains of the access relations in "prog".
  2478. */
  2479. static __isl_give isl_union_set *accessed_by_domain(
  2480. __isl_take isl_union_set *domain, struct gpu_prog *prog)
  2481. {
  2482. isl_union_map *access;
  2483. isl_union_set *arrays;
  2484. access = isl_union_map_union(isl_union_map_copy(prog->read),
  2485. isl_union_map_copy(prog->may_write));
  2486. access = isl_union_map_intersect_domain(access, domain);
  2487. arrays = isl_union_map_range(access);
  2488. arrays = isl_union_set_apply(arrays,
  2489. isl_union_map_copy(prog->to_outer));
  2490. return arrays;
  2491. }
  2492. /* Return the number of outer band members of the band node "node"
  2493. * that are marked coincident.
  2494. */
  2495. static int n_outer_coincidence(__isl_keep isl_schedule_node *node)
  2496. {
  2497. int i, n;
  2498. n = isl_schedule_node_band_n_member(node);
  2499. for (i = 0; i < n; ++i)
  2500. if (!isl_schedule_node_band_member_get_coincident(node, i))
  2501. break;
  2502. return i;
  2503. }
  2504. /* If the band node "node" has more than "n" members, then split off
  2505. * the first "n" of them.
  2506. */
  2507. static __isl_give isl_schedule_node *split_band(
  2508. __isl_take isl_schedule_node *node, int n)
  2509. {
  2510. int dim;
  2511. dim = isl_schedule_node_band_n_member(node);
  2512. if (n < dim)
  2513. node = isl_schedule_node_band_split(node, n);
  2514. return node;
  2515. }
  2516. /* Scale a band node that may have been split by split_band.
  2517. * "sizes" are the scaling factors for the original node.
  2518. * "node" either points to the original band node, or the outer
  2519. * of the two pieces after splitting.
  2520. *
  2521. * If the number of elements in "node" is smaller than the number of
  2522. * elements in "sizes", then some splitting has occurred and we split
  2523. * "sizes" in the same way.
  2524. */
  2525. static __isl_give isl_schedule_node *scale_band(
  2526. __isl_take isl_schedule_node *node, __isl_take isl_multi_val *sizes)
  2527. {
  2528. int n, dim;
  2529. n = isl_multi_val_dim(sizes, isl_dim_set);
  2530. dim = isl_schedule_node_band_n_member(node);
  2531. if (n > dim) {
  2532. isl_multi_val *sizes2;
  2533. sizes2 = isl_multi_val_copy(sizes);
  2534. sizes = isl_multi_val_drop_dims(sizes,
  2535. isl_dim_set, dim, n - dim);
  2536. sizes2 = isl_multi_val_drop_dims(sizes2, isl_dim_set, 0, dim);
  2537. node = isl_schedule_node_child(node, 0);
  2538. node = isl_schedule_node_band_scale(node, sizes2);
  2539. node = isl_schedule_node_parent(node);
  2540. }
  2541. return isl_schedule_node_band_scale(node, sizes);
  2542. }
  2543. /* Return an isl_multi_aff, with as elements the parameters in "space"
  2544. * that have the names specified by the elements in "names".
  2545. * If (some of) these parameters do not already appear in "space",
  2546. * then they are added first.
  2547. */
  2548. static __isl_give isl_multi_aff *parameter_vector(__isl_take isl_space *space,
  2549. __isl_keep isl_id_list *names)
  2550. {
  2551. int i, n;
  2552. isl_local_space *ls;
  2553. isl_multi_aff *ma;
  2554. if (!names)
  2555. space = isl_space_free(space);
  2556. n = isl_id_list_n_id(names);
  2557. for (i = 0; i < n; ++i) {
  2558. int pos;
  2559. isl_id *id;
  2560. id = isl_id_list_get_id(names, i);
  2561. pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
  2562. if (pos >= 0) {
  2563. isl_id_free(id);
  2564. continue;
  2565. }
  2566. pos = isl_space_dim(space, isl_dim_param);
  2567. space = isl_space_add_dims(space, isl_dim_param, 1);
  2568. space = isl_space_set_dim_id(space, isl_dim_param, pos, id);
  2569. }
  2570. ma = isl_multi_aff_zero(isl_space_copy(space));
  2571. ls = isl_local_space_from_space(isl_space_domain(space));
  2572. for (i = 0; i < n; ++i) {
  2573. int pos;
  2574. isl_id *id;
  2575. isl_aff *aff;
  2576. id = isl_id_list_get_id(names, i);
  2577. pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
  2578. isl_id_free(id);
  2579. aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
  2580. isl_dim_param, pos);
  2581. ma = isl_multi_aff_set_aff(ma, i, aff);
  2582. }
  2583. isl_local_space_free(ls);
  2584. return ma;
  2585. }
  2586. /* Return constraints on the domain elements that equate a sequence of
  2587. * parameters called "names", to the partial schedule
  2588. * of "node" modulo the integers in "size".
  2589. * The number of elements in the array "size" should be equal
  2590. * to the number of elements in "names".
  2591. * The number of members of the band node "node" should be smaller
  2592. * than or equal to this number. If it is smaller, then the first
  2593. * elements of "names" are equated to zero.
  2594. */
  2595. static __isl_give isl_union_set *set_schedule_modulo(
  2596. __isl_keep isl_schedule_node *node, __isl_keep isl_id_list *names,
  2597. int *size)
  2598. {
  2599. int n, n_zero;
  2600. isl_space *space;
  2601. isl_multi_aff *ma;
  2602. isl_multi_union_pw_aff *mupa, *mupa2;
  2603. isl_multi_val *mv;
  2604. isl_union_set *domain;
  2605. if (!node)
  2606. return NULL;
  2607. n = isl_id_list_n_id(names);
  2608. if (n == 0)
  2609. return isl_schedule_node_get_universe_domain(node);
  2610. n_zero = n - isl_schedule_node_band_n_member(node);
  2611. mupa = isl_schedule_node_band_get_partial_schedule(node);
  2612. mv = construct_band_tiles_sizes(node, size + n_zero);
  2613. mupa = isl_multi_union_pw_aff_mod_multi_val(mupa, mv);
  2614. space = isl_multi_union_pw_aff_get_space(mupa);
  2615. space = isl_space_params(space);
  2616. space = isl_space_set_from_params(space);
  2617. space = isl_space_add_dims(space, isl_dim_set, n_zero);
  2618. ma = isl_multi_aff_zero(space);
  2619. domain = isl_schedule_node_get_universe_domain(node);
  2620. mupa2 = isl_multi_union_pw_aff_multi_aff_on_domain(
  2621. isl_union_set_copy(domain), ma);
  2622. mupa = isl_multi_union_pw_aff_range_product(mupa2, mupa);
  2623. space = isl_multi_union_pw_aff_get_space(mupa);
  2624. ma = parameter_vector(space, names);
  2625. mupa2 = isl_multi_union_pw_aff_multi_aff_on_domain(domain, ma);
  2626. mupa = isl_multi_union_pw_aff_sub(mupa, mupa2);
  2627. return isl_multi_union_pw_aff_zero_union_set(mupa);
  2628. }
  2629. /* Insert a context node at "node" introducing the block and thread
  2630. * identifiers along with their bounds, which are stored in kernel->grid_size
  2631. * and kernel->block_dim.
  2632. * Note that the bounds on the block identifiers may implicitly impose
  2633. * constraints on the parameters. A guard needs to be inserted
  2634. * in the schedule tree to ensure that those bounds hold at "node".
  2635. * This guard is inserted in insert_guard.
  2636. */
  2637. static __isl_give isl_schedule_node *insert_context(struct ppcg_kernel *kernel,
  2638. __isl_take isl_schedule_node *node)
  2639. {
  2640. isl_set *context;
  2641. context = isl_set_universe(isl_set_get_space(kernel->context));
  2642. context = add_bounded_parameters_dynamic(context,
  2643. kernel->grid_size, kernel->block_ids);
  2644. context = add_bounded_parameters(context,
  2645. kernel->block_dim, kernel->thread_ids);
  2646. node = isl_schedule_node_insert_context(node, context);
  2647. return node;
  2648. }
  2649. /* Insert a guard that eliminates kernel launches where the kernel
  2650. * obviously does not have any work to do.
  2651. *
  2652. * In particular, eliminate kernel launches where there are obviously
  2653. * zero blocks.
  2654. * Use the same block size constraints that are used to create the context
  2655. * to ensure that all constraints implicit in the constructed context
  2656. * are imposed by the guard.
  2657. *
  2658. * Additionally, add other constraints that are valid
  2659. * for each executed instance ("context"), as long as this does not result
  2660. * in a disjunction.
  2661. */
  2662. static __isl_give isl_schedule_node *insert_guard(
  2663. __isl_take isl_schedule_node *node, __isl_keep isl_set *context,
  2664. __isl_keep isl_multi_pw_aff *size, struct ppcg_scop *scop)
  2665. {
  2666. unsigned nparam, n;
  2667. isl_set *guard;
  2668. isl_id_list *ids;
  2669. guard = isl_set_copy(context);
  2670. guard = isl_set_compute_divs(guard);
  2671. guard = isl_set_from_basic_set(isl_set_simple_hull(guard));
  2672. nparam = isl_set_dim(guard, isl_dim_param);
  2673. n = isl_multi_pw_aff_dim(size, isl_dim_out);
  2674. ids = ppcg_scop_generate_names(scop, n, "__ppcg_tmp");
  2675. guard = add_bounded_parameters_dynamic(guard, size, ids);
  2676. isl_id_list_free(ids);
  2677. guard = isl_set_project_out(guard, isl_dim_param, nparam, n);
  2678. node = isl_schedule_node_insert_guard(node, guard);
  2679. return node;
  2680. }
  2681. /* Does any array reference group mapping require the band that is mapped
  2682. * to threads to be unrolled?
  2683. */
  2684. static int kernel_requires_unroll(struct ppcg_kernel *kernel)
  2685. {
  2686. int i, j;
  2687. for (i = 0; i < kernel->n_array; ++i) {
  2688. struct gpu_local_array_info *array = &kernel->array[i];
  2689. for (j = 0; j < array->n_group; ++j) {
  2690. struct gpu_array_ref_group *group = array->groups[j];
  2691. if (gpu_array_ref_group_requires_unroll(group))
  2692. return 1;
  2693. }
  2694. }
  2695. return 0;
  2696. }
  2697. /* Mark the given band node "node" for unrolling by the AST generator and
  2698. * then sink it to the leaves of the schedule tree.
  2699. * All dimensions of "node" are assumed to be coincident, such that this
  2700. * sinking is a valid operation.
  2701. */
  2702. static __isl_give isl_schedule_node *unroll(__isl_take isl_schedule_node *node)
  2703. {
  2704. node = ppcg_set_schedule_node_type(node, isl_ast_loop_unroll);
  2705. node = isl_schedule_node_band_sink(node);
  2706. return node;
  2707. }
  2708. /* Insert a synchronization node in the schedule tree of "node"
  2709. * after the core computation of "kernel" at the level of the band
  2710. * that is mapped to threads, except if that level is equal to
  2711. * that of the band that is mapped to blocks or if there are no writes
  2712. * to global or shared memory in the core computation that require
  2713. * synchronization.
  2714. * If there are any writes to shared memory and the shared memory
  2715. * copying is performed at the same level, then synchronization
  2716. * is needed between the core and the copying anyway, so we might
  2717. * as well add it here. If the copying is performed at a higher
  2718. * level, then different iterations of intermediate schedule dimensions
  2719. * may have a different mapping from between shared memory elements and
  2720. * threads, such that synchronization is required after the core.
  2721. * "node" is assumed to point to the kernel node.
  2722. *
  2723. * If the shared and the thread mark point to the same node, then make
  2724. * sure the synchronization is inserted outside of the shared mark.
  2725. */
  2726. static __isl_give isl_schedule_node *add_sync(struct ppcg_kernel *kernel,
  2727. __isl_take isl_schedule_node *node)
  2728. {
  2729. int depth;
  2730. int need_sync;
  2731. need_sync = any_global_or_shared_sync_writes(kernel);
  2732. if (need_sync < 0)
  2733. return isl_schedule_node_free(node);
  2734. if (!need_sync)
  2735. return node;
  2736. node = gpu_tree_move_down_to_thread(node, kernel->core);
  2737. depth = isl_schedule_node_get_schedule_depth(node);
  2738. node = gpu_tree_move_up_to_kernel(node);
  2739. if (depth == isl_schedule_node_get_schedule_depth(node))
  2740. return node;
  2741. node = gpu_tree_move_down_to_depth(node, depth, kernel->core);
  2742. node = gpu_tree_ensure_following_sync(node, kernel);
  2743. node = gpu_tree_move_up_to_kernel(node);
  2744. return node;
  2745. }
  2746. /* Return a read ("read" is 1) or write access relation for "group"
  2747. * with those accesses removed that are only needed to communicate data
  2748. * within the subtree of the schedule rooted at "node".
  2749. * Furthermore, include the prefix schedule at "node".
  2750. * That is, return a relation of the form
  2751. *
  2752. * S -> [D -> A]
  2753. *
  2754. * with D the outer schedule dimensions at "node".
  2755. */
  2756. static __isl_give isl_union_map *anchored_non_local_accesses(
  2757. struct ppcg_kernel *kernel, struct gpu_array_ref_group *group,
  2758. __isl_take isl_schedule_node *node, int read)
  2759. {
  2760. isl_union_map *access;
  2761. isl_union_map *prefix;
  2762. prefix = isl_schedule_node_get_prefix_schedule_relation(node);
  2763. prefix = isl_union_map_preimage_domain_union_pw_multi_aff(prefix,
  2764. isl_union_pw_multi_aff_copy(kernel->contraction));
  2765. access = gpu_array_ref_group_access_relation(group, read, !read);
  2766. access = remove_local_accesses_group(kernel, group, access, prefix,
  2767. read);
  2768. access = isl_union_map_range_product(prefix, access);
  2769. return access;
  2770. }
  2771. /* Given an array reference group "group", create a mapping
  2772. *
  2773. * read[D -> A] -> [D -> A]
  2774. *
  2775. * if "read" is set or
  2776. *
  2777. * write[D -> A] -> [D -> A]
  2778. *
  2779. * if "read" is not set.
  2780. * D corresponds to the outer tile->depth dimensions of
  2781. * the kernel schedule.
  2782. */
  2783. static __isl_give isl_multi_aff *create_from_access(isl_ctx *ctx,
  2784. struct gpu_array_ref_group *group, int read)
  2785. {
  2786. struct gpu_array_tile *tile;
  2787. isl_space *space;
  2788. isl_id *id;
  2789. tile = gpu_array_ref_group_tile(group);
  2790. space = isl_space_copy(group->array->space);
  2791. space = isl_space_from_range(space);
  2792. space = isl_space_add_dims(space, isl_dim_in, tile->depth);
  2793. space = isl_space_wrap(space);
  2794. space = isl_space_map_from_set(space);
  2795. id = isl_id_alloc(ctx, read ? "read" : "write", group);
  2796. space = isl_space_set_tuple_id(space, isl_dim_in, id);
  2797. return isl_multi_aff_identity(space);
  2798. }
  2799. /* If any writes in "group" require synchronization, then make sure
  2800. * that there is a synchronization node for "kernel" after the node
  2801. * following "node" in a sequence.
  2802. *
  2803. * If "shared" is set and no synchronization is needed for
  2804. * the writes to global memory, then add synchronization before
  2805. * the kernel to protect shared memory from being overwritten
  2806. * by the next iteration of the core computation.
  2807. * No additional synchronization is needed to protect against
  2808. * the next copy into shared memory because each element of
  2809. * the shared memory tile is always copied by the same thread.
  2810. */
  2811. static __isl_give isl_schedule_node *add_group_write_sync(
  2812. __isl_take isl_schedule_node *node, struct ppcg_kernel *kernel,
  2813. struct gpu_array_ref_group *group, int shared)
  2814. {
  2815. int need_sync;
  2816. need_sync = any_sync_writes_in_group(kernel, group);
  2817. if (need_sync < 0)
  2818. return isl_schedule_node_free(node);
  2819. if (need_sync) {
  2820. node = isl_schedule_node_parent(node);
  2821. node = isl_schedule_node_next_sibling(node);
  2822. node = isl_schedule_node_child(node, 0);
  2823. node = gpu_tree_ensure_following_sync(node, kernel);
  2824. } else if (shared) {
  2825. struct gpu_array_tile *tile;
  2826. tile = gpu_array_ref_group_tile(group);
  2827. node = isl_schedule_node_parent(node);
  2828. node = isl_schedule_node_parent(node);
  2829. node = gpu_tree_move_down_to_depth(node, tile->depth,
  2830. kernel->core);
  2831. node = gpu_tree_move_left_to_sync(node, kernel);
  2832. }
  2833. return node;
  2834. }
  2835. /* Add copy statements to the schedule tree of "node"
  2836. * for reading from global memory to private memory (if "read" is set) or
  2837. * for writing back from private memory to global memory
  2838. * (if "read" is not set) for the array reference group "group" that
  2839. * is mapped to private memory.
  2840. * On input, "node" points to the kernel node, and it is moved
  2841. * back there on output.
  2842. *
  2843. * The copies are performed in the order of the array elements.
  2844. * The copy statement instances include a reference to the outer
  2845. * tile->depth dimensions of the kernel schedule for ease of
  2846. * combining them with the group tiling.
  2847. *
  2848. * That is, the extra schedule is of the form
  2849. *
  2850. * type[D -> A] -> A
  2851. *
  2852. * where D corresponds to the outer tile->depth dimensions of
  2853. * the kernel schedule and A to the global array.
  2854. * This schedule is unrolled because registers are not addressable.
  2855. *
  2856. * The copying is inserted in the schedule tree through an extension
  2857. * of the form
  2858. *
  2859. * D -> type[D -> A]
  2860. *
  2861. * where the extra domain elements type[D -> A] are those accessed
  2862. * by the group.
  2863. * A filter is inserted on type[D -> A] to ensure that the element
  2864. * is read/written by the same thread that needs the element.
  2865. * This filter is obtained by applying
  2866. *
  2867. * S -> type[D -> A]
  2868. *
  2869. * to the thread filter for the core statements.
  2870. *
  2871. * The extension is inserted before the core computation in case of a read
  2872. * and after the core computation in case of a write.
  2873. * In the latter case, we also make sure that there is a synchronization
  2874. * node after the write to global memory, unless this write is performed
  2875. * at the outer level of the kernel.
  2876. * In principle, this synchronization could be inserted higher
  2877. * in the schedule tree depending on where the corresponding reads
  2878. * from global memory are performed.
  2879. */
  2880. static __isl_give isl_schedule_node *add_copies_group_private(
  2881. struct ppcg_kernel *kernel, struct gpu_array_ref_group *group,
  2882. __isl_take isl_schedule_node *node, int read)
  2883. {
  2884. struct gpu_array_tile *tile;
  2885. isl_union_map *access;
  2886. isl_union_set *domain;
  2887. isl_space *space;
  2888. isl_multi_aff *from_access;
  2889. isl_multi_pw_aff *mpa;
  2890. isl_multi_union_pw_aff *mupa;
  2891. isl_union_pw_multi_aff *contraction;
  2892. isl_schedule_node *graft;
  2893. isl_union_set *filter;
  2894. int kernel_depth;
  2895. int empty;
  2896. kernel_depth = isl_schedule_node_get_schedule_depth(node);
  2897. tile = gpu_array_ref_group_tile(group);
  2898. node = gpu_tree_move_down_to_depth(node, tile->depth, kernel->core);
  2899. access = anchored_non_local_accesses(kernel, group, node, read);
  2900. empty = isl_union_map_is_empty(access);
  2901. if (empty < 0 || empty) {
  2902. isl_union_map_free(access);
  2903. if (empty < 0)
  2904. return isl_schedule_node_free(node);
  2905. return gpu_tree_move_up_to_kernel(node);
  2906. }
  2907. group->array->global = 1;
  2908. group->local_array->global = 1;
  2909. from_access = create_from_access(kernel->ctx, group, read);
  2910. space = isl_space_domain(isl_multi_aff_get_space(from_access));
  2911. access = isl_union_map_preimage_range_multi_aff(access, from_access);
  2912. filter = isl_union_set_copy(kernel->thread_filter);
  2913. contraction = isl_union_pw_multi_aff_copy(kernel->contraction);
  2914. filter = isl_union_set_preimage_union_pw_multi_aff(filter, contraction);
  2915. filter = isl_union_set_apply(filter, isl_union_map_copy(access));
  2916. filter = isl_union_set_detect_equalities(filter);
  2917. filter = isl_union_set_coalesce(filter);
  2918. domain = isl_union_map_range(access);
  2919. access = isl_union_set_wrapped_domain_map(domain);
  2920. access = isl_union_map_reverse(access);
  2921. access = isl_union_map_coalesce(access);
  2922. graft = isl_schedule_node_from_extension(access);
  2923. space = isl_space_map_from_set(space);
  2924. mpa = isl_multi_pw_aff_identity(space);
  2925. mpa = isl_multi_pw_aff_range_factor_range(mpa);
  2926. mupa = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
  2927. graft = isl_schedule_node_child(graft, 0);
  2928. graft = isl_schedule_node_insert_partial_schedule(graft, mupa);
  2929. graft = unroll(graft);
  2930. graft = isl_schedule_node_insert_filter(graft, filter);
  2931. graft = isl_schedule_node_parent(graft);
  2932. if (read)
  2933. node = isl_schedule_node_graft_before(node, graft);
  2934. else {
  2935. node = isl_schedule_node_graft_after(node, graft);
  2936. if (kernel_depth < tile->depth)
  2937. node = add_group_write_sync(node, kernel, group, 0);
  2938. }
  2939. node = gpu_tree_move_up_to_kernel(node);
  2940. return node;
  2941. }
  2942. /* Add copy statements to the schedule tree of "node"
  2943. * for reading from global memory to shared memory (if "read" is set) or
  2944. * for writing back from shared memory to global memory
  2945. * (if "read" is not set) for the array reference group "group" that
  2946. * is mapped to shared memory.
  2947. * On input, "node" points to the kernel node, and it is moved
  2948. * back there on output.
  2949. *
  2950. * The copies are performed in the order of the corresponding shared
  2951. * memory tile.
  2952. * The copy statement instances include a reference to the outer
  2953. * tile->depth dimensions of the kernel schedule for ease of
  2954. * combining them with the group tiling.
  2955. *
  2956. * If we are performing a read from global memory to shared memory and
  2957. * if the array involved is not a scalar, then we copy
  2958. * the entire tile to shared memory. This may result in some extra
  2959. * elements getting copied, but it should lead to simpler code
  2960. * (which means that fewer registers may be needed) and less divergence.
  2961. *
  2962. * Otherwise, we only copy the elements that will be read or have been written
  2963. * in the kernel.
  2964. *
  2965. * That is, the extra schedule is of the form
  2966. *
  2967. * type[D -> A] -> T
  2968. *
  2969. * where D corresponds to the outer tile->depth dimensions of
  2970. * the kernel schedule, A to the global array and T is the corresponding
  2971. * shared memory tile.
  2972. *
  2973. * The copying is inserted in the schedule tree through an extension
  2974. * of the form
  2975. *
  2976. * D -> type[D -> A]
  2977. *
  2978. * where the extra domain elements type[D -> A] are those accessed
  2979. * by the group. In the case of read from a non-scalar, this set
  2980. * is replaced by the entire shared memory tile.
  2981. *
  2982. * If the "unroll_copy_shared" option is set, then the AST generator
  2983. * is instructed to unroll the copying code.
  2984. *
  2985. * A filter is inserted on type[D -> A] to map the copy instances
  2986. * to the threads. In particular, the thread identifiers are
  2987. * equated to the position inside the shared memory tile (T)
  2988. * modulo the block size.
  2989. * We try to align the innermost tile dimension with the innermost
  2990. * thread identifier (x) as a heuristic to improve coalescing.
  2991. * In particular, if the dimension of the tile is greater than
  2992. * the dimension of the block, then the schedule mapping to the tile
  2993. * is broken up into two pieces and the filter is applied to the inner part.
  2994. * If, on the other hand, the dimension of the tile is smaller than
  2995. * the dimension of the block, then the initial thread identifiers
  2996. * are equated to zero and the remaining thread identifiers are
  2997. * matched to the memory tile.
  2998. *
  2999. * The extension is inserted before the core computation in case of a read
  3000. * and after the core computation in case of a write.
  3001. * In the case of a read, we first need to make sure there is some
  3002. * synchronization before the core computation such that we can put the read
  3003. * from global memory to shared memory before that synchronization.
  3004. * This ensures that all threads have finished copying into shared memory
  3005. * before the shared memory is used.
  3006. * We also need to make sure that there is a synchronization node after
  3007. * the core computation to ensure that the next load into shared memory
  3008. * only happens after all data has been used. There is no need for
  3009. * this synchronization if we are at the outer level since then there
  3010. * won't be a next load.
  3011. * In the case of a write, we need to make sure there is some synchronization
  3012. * after the core computation such taht we can put the write from shared
  3013. * memory to global memory after that synchronization.
  3014. * Unless we are at the outer level, we also need a synchronization node
  3015. * after the write to ensure the data is saved to global memory
  3016. * before the next iteration write to the same shared memory.
  3017. * It also makes sure the data has arrived in global memory before
  3018. * it is read in a subsequent iteration.
  3019. */
  3020. static __isl_give isl_schedule_node *add_copies_group_shared(
  3021. struct ppcg_kernel *kernel, struct gpu_array_ref_group *group,
  3022. __isl_take isl_schedule_node *node, int read)
  3023. {
  3024. struct gpu_array_tile *tile;
  3025. isl_union_map *access;
  3026. isl_union_set *domain;
  3027. isl_multi_aff *ma;
  3028. isl_multi_aff *from_access;
  3029. isl_multi_pw_aff *mpa;
  3030. isl_multi_union_pw_aff *mupa;
  3031. isl_schedule_node *graft;
  3032. isl_union_set *filter;
  3033. int skip;
  3034. int kernel_depth;
  3035. int empty;
  3036. tile = gpu_array_ref_group_tile(group);
  3037. kernel_depth = isl_schedule_node_get_schedule_depth(node);
  3038. node = gpu_tree_move_down_to_depth(node, tile->depth, kernel->core);
  3039. access = anchored_non_local_accesses(kernel, group, node, read);
  3040. empty = isl_union_map_is_empty(access);
  3041. if (empty < 0 || empty) {
  3042. isl_union_map_free(access);
  3043. if (empty < 0)
  3044. return isl_schedule_node_free(node);
  3045. return gpu_tree_move_up_to_kernel(node);
  3046. }
  3047. group->array->global = 1;
  3048. group->local_array->global = 1;
  3049. from_access = create_from_access(kernel->ctx, group, read);
  3050. ma = isl_multi_aff_copy(tile->tiling);
  3051. ma = isl_multi_aff_pullback_multi_aff(ma,
  3052. isl_multi_aff_copy(from_access));
  3053. mpa = isl_multi_pw_aff_from_multi_aff(ma);
  3054. mupa = isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
  3055. domain = isl_union_map_range(access);
  3056. if (read && !gpu_array_is_scalar(group->array)) {
  3057. isl_map *map;
  3058. isl_union_set_free(domain);
  3059. map = group_tile(group);
  3060. domain = isl_union_set_from_set(isl_map_wrap(map));
  3061. }
  3062. domain = isl_union_set_preimage_multi_aff(domain, from_access);
  3063. access = isl_union_set_wrapped_domain_map(domain);
  3064. access = isl_union_map_reverse(access);
  3065. access = isl_union_map_coalesce(access);
  3066. graft = isl_schedule_node_from_extension(access);
  3067. graft = isl_schedule_node_child(graft, 0);
  3068. graft = isl_schedule_node_insert_partial_schedule(graft, mupa);
  3069. if (kernel->options->unroll_copy_shared)
  3070. graft = ppcg_set_schedule_node_type(graft, isl_ast_loop_unroll);
  3071. if (tile->n > kernel->n_block && kernel->n_block > 0) {
  3072. graft = isl_schedule_node_band_split(graft,
  3073. tile->n - kernel->n_block);
  3074. graft = isl_schedule_node_child(graft, 0);
  3075. }
  3076. if (tile->n < kernel->n_block)
  3077. skip = kernel->n_block - tile->n;
  3078. else
  3079. skip = 0;
  3080. filter = set_schedule_modulo(graft, kernel->thread_ids,
  3081. kernel->block_dim);
  3082. if (!kernel->options->wrap)
  3083. graft = snap_band_to_sizes(graft, kernel->block_dim + skip,
  3084. kernel->options);
  3085. if (tile->n > kernel->n_block && kernel->n_block > 0)
  3086. graft = isl_schedule_node_parent(graft);
  3087. graft = isl_schedule_node_insert_filter(graft, filter);
  3088. while (graft && isl_schedule_node_has_parent(graft))
  3089. graft = isl_schedule_node_parent(graft);
  3090. if (read) {
  3091. if (kernel_depth < tile->depth)
  3092. node = gpu_tree_ensure_sync_after_core(node, kernel);
  3093. node = gpu_tree_move_left_to_sync(node, kernel);
  3094. node = isl_schedule_node_graft_before(node, graft);
  3095. } else {
  3096. node = gpu_tree_move_right_to_sync(node, kernel);
  3097. node = isl_schedule_node_graft_after(node, graft);
  3098. if (kernel_depth < tile->depth)
  3099. node = add_group_write_sync(node, kernel, group, 1);
  3100. }
  3101. node = gpu_tree_move_up_to_kernel(node);
  3102. return node;
  3103. }
  3104. /* Check whether the array reference group "group" is mapped to
  3105. * private or shared memory and, if so,
  3106. * add copy statements to the schedule tree of "node"
  3107. * for reading from global memory to private or shared memory
  3108. * (if "read" is set) or for writing back from private or shared memory
  3109. * to global memory (if "read" is not set) for this group.
  3110. * On input, "node" points to the kernel node, and it is moved
  3111. * back there on output.
  3112. */
  3113. static __isl_give isl_schedule_node *add_copies_group(
  3114. struct ppcg_kernel *kernel, struct gpu_array_ref_group *group,
  3115. __isl_take isl_schedule_node *node, int read)
  3116. {
  3117. enum ppcg_group_access_type type;
  3118. type = gpu_array_ref_group_type(group);
  3119. if (type == ppcg_access_private)
  3120. return add_copies_group_private(kernel, group, node, read);
  3121. if (type == ppcg_access_shared)
  3122. return add_copies_group_shared(kernel, group, node, read);
  3123. return node;
  3124. }
  3125. /* For each array reference group that is mapped to private or shared memory,
  3126. * add copy statements to the schedule tree of "node"
  3127. * for reading from global memory to private or shared memory
  3128. * and for writing back.
  3129. * On input, "node" points to the kernel node, and it is moved
  3130. * back there on output.
  3131. */
  3132. static __isl_give isl_schedule_node *add_copies(struct ppcg_kernel *kernel,
  3133. __isl_take isl_schedule_node *node)
  3134. {
  3135. int i, j;
  3136. for (i = 0; i < kernel->n_array; ++i) {
  3137. struct gpu_local_array_info *array = &kernel->array[i];
  3138. for (j = 0; j < array->n_group; ++j) {
  3139. struct gpu_array_ref_group *group = array->groups[j];
  3140. node = add_copies_group(kernel, group, node, 1);
  3141. if (!node)
  3142. return NULL;
  3143. node = add_copies_group(kernel, group, node, 0);
  3144. if (!node)
  3145. return NULL;
  3146. }
  3147. }
  3148. return node;
  3149. }
  3150. /* Mark all dimensions in the current band node atomic.
  3151. */
  3152. static __isl_give isl_schedule_node *atomic(__isl_take isl_schedule_node *node)
  3153. {
  3154. return ppcg_set_schedule_node_type(node, isl_ast_loop_atomic);
  3155. }
  3156. /* Mark "node" atomic, if it is a band node.
  3157. * Do the same for all ancestors.
  3158. * Return a pointer to "node" (in the updated schedule tree).
  3159. */
  3160. static __isl_give isl_schedule_node *atomic_ancestors(
  3161. __isl_take isl_schedule_node *node)
  3162. {
  3163. int pos;
  3164. if (!node)
  3165. return NULL;
  3166. if (!isl_schedule_node_has_parent(node))
  3167. return node;
  3168. pos = isl_schedule_node_get_child_position(node);
  3169. node = isl_schedule_node_parent(node);
  3170. if (isl_schedule_node_get_type(node) == isl_schedule_node_band)
  3171. node = atomic(node);
  3172. node = atomic_ancestors(node);
  3173. node = isl_schedule_node_child(node, pos);
  3174. return node;
  3175. }
  3176. /* Collect all write references that require synchronization.
  3177. * "node" is assumed to point to the kernel node.
  3178. * Each reference is represented by a universe set in a space
  3179. *
  3180. * [S[i,j] -> R[]]
  3181. *
  3182. * with S[i,j] the statement instance space and R[] the array reference.
  3183. *
  3184. * This function should be called before block and thread filters are added.
  3185. *
  3186. * Synchronization is needed after a write if there is a subsequent read
  3187. * within the same block that may not be performed by the same thread.
  3188. * There should not be any dependences between different blocks,
  3189. * so we start with the flow dependences within the same kernel invocation
  3190. * and we subtract from these those dependences that are mapped
  3191. * to the same iteration of the bands where synchronization is inserted.
  3192. * We do not remove pairs of instances that are known to map to
  3193. * the same thread across different iterations of the intermediate
  3194. * bands because the read may be performed by a different thread
  3195. * than the one that needs the value if shared memory is involved.
  3196. *
  3197. * We also consider all pairs of possible writes that access the same
  3198. * memory location and that may be mapped to the same block but not
  3199. * to the same iteration of the intermediate bands.
  3200. * In theory, it would be possible for one thread to still be in
  3201. * a previous iteration of a loop in these bands.
  3202. * A write to global memory in this delayed thread could then overwrite
  3203. * a write from another thread that has already moved on to
  3204. * the next iteration.
  3205. *
  3206. * After computing the above writes paired off with reads or writes
  3207. * that depend on them, we project onto the domain writes.
  3208. * Sychronization is needed after writes to global memory
  3209. * through these references.
  3210. */
  3211. static __isl_give isl_union_set *compute_sync_writes(
  3212. struct ppcg_kernel *kernel, __isl_keep isl_schedule_node *node)
  3213. {
  3214. isl_union_map *local;
  3215. isl_union_map *may_writes, *shared_access;
  3216. isl_union_map *kernel_prefix, *thread_prefix;
  3217. isl_union_map *equal;
  3218. isl_union_set *wrap;
  3219. isl_union_set *domain;
  3220. isl_union_pw_multi_aff *contraction;
  3221. kernel_prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
  3222. node = isl_schedule_node_copy(node);
  3223. node = gpu_tree_move_down_to_thread(node, kernel->core);
  3224. thread_prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
  3225. isl_schedule_node_free(node);
  3226. contraction = kernel->contraction;
  3227. kernel_prefix = isl_union_map_preimage_domain_union_pw_multi_aff(
  3228. kernel_prefix, isl_union_pw_multi_aff_copy(contraction));
  3229. thread_prefix = isl_union_map_preimage_domain_union_pw_multi_aff(
  3230. thread_prefix, isl_union_pw_multi_aff_copy(contraction));
  3231. domain = isl_union_set_copy(kernel->expanded_domain);
  3232. domain = isl_union_set_universe(domain);
  3233. may_writes = isl_union_map_copy(kernel->prog->scop->tagged_may_writes);
  3234. may_writes = isl_union_map_curry(may_writes);
  3235. may_writes = isl_union_map_intersect_domain(may_writes, domain);
  3236. may_writes = isl_union_map_uncurry(may_writes);
  3237. shared_access = isl_union_map_copy(may_writes);
  3238. shared_access = isl_union_map_apply_range(shared_access,
  3239. isl_union_map_reverse(may_writes));
  3240. local = isl_union_map_copy(kernel->prog->scop->tagged_dep_flow);
  3241. local = isl_union_map_union(local, shared_access);
  3242. local = isl_union_map_zip(local);
  3243. equal = isl_union_map_apply_range(kernel_prefix,
  3244. isl_union_map_reverse(isl_union_map_copy(kernel_prefix)));
  3245. wrap = isl_union_map_wrap(equal);
  3246. local = isl_union_map_intersect_domain(local, wrap);
  3247. equal = isl_union_map_apply_range(thread_prefix,
  3248. isl_union_map_reverse(isl_union_map_copy(thread_prefix)));
  3249. wrap = isl_union_map_wrap(equal);
  3250. local = isl_union_map_subtract_domain(local, wrap);
  3251. local = isl_union_map_zip(local);
  3252. local = isl_union_map_universe(local);
  3253. return isl_union_map_domain(local);
  3254. }
  3255. /* Group the domain elements into a single space, named kernelX,
  3256. * with X the kernel sequence number "kernel_id".
  3257. */
  3258. static __isl_give isl_schedule_node *group_statements(
  3259. __isl_take isl_schedule_node *node, int kernel_id)
  3260. {
  3261. char buffer[20];
  3262. isl_id *id;
  3263. if (!node)
  3264. return NULL;
  3265. snprintf(buffer, sizeof(buffer), "kernel%d", kernel_id);
  3266. id = isl_id_alloc(isl_schedule_node_get_ctx(node), buffer, NULL);
  3267. return isl_schedule_node_group(node, id);
  3268. }
  3269. /* Create a ppcg_kernel representing the domain instances that reach "node"
  3270. * and insert a mark node pointing to the ppcg_kernel before "node".
  3271. * The band that "node" points to is the band that needs to be mapped
  3272. * to block identifiers. The band that needs to be mapped to thread
  3273. * identifiers should be marked by a "thread" mark by the caller.
  3274. * The linear branch between the current node and the "thread" mark
  3275. * may also have a "shared" mark. If present, the mapping to shared
  3276. * memory is computed at that point.
  3277. * Both marks are removed by this function.
  3278. * If "scale" is set, then the band that "node" points to is scaled
  3279. * by "sizes".
  3280. *
  3281. * Mark all outer band nodes as atomic to ensure each kernel is only
  3282. * scheduled once.
  3283. * If the domain elements that reach "node" live in more than one space,
  3284. * then group the domain elements into a single space, named kernelX,
  3285. * with X the kernel sequence number.
  3286. *
  3287. * Insert a guard node governing the kernel node to ensure that
  3288. * no kernels with zero blocks are launched.
  3289. *
  3290. * Insert a context node describing the block and thread
  3291. * identifiers inside the kernel mark.
  3292. * The context node needs to be inserted after the effective block size
  3293. * has been determined such that the bounds on the thread identifiers
  3294. * would reflect the effective block size.
  3295. * Insert a filter node inside the context node mapping the statement
  3296. * instances to block identifiers. In particular, the block identifiers
  3297. * are equated to the partial schedule of band that was marked for mapping
  3298. * to blocks modulo the grid size.
  3299. * Insert a filter node inside the "thread" mark mapping the statement
  3300. * instances to thread identifiers. In particular, the thread identifiers
  3301. * are equated to the partial schedule of band that was marked for mapping
  3302. * to threads modulo the block size.
  3303. *
  3304. * Compute array reference groups for all arrays, set the local
  3305. * array bounds based on the set of domain instances that reach
  3306. * the kernel node, check the total amount of shared memory used
  3307. * and compute all group tilings.
  3308. * The array reference groups are computed after the block filter
  3309. * has been inserted because it affects the mapping to shared or
  3310. * private memory. This computation also requires the thread filter
  3311. * (in the ppcg_kernel object), but this thread filter should not
  3312. * have been added to the schedule tree yet since the computation
  3313. * requires the schedule of the band that needs to be mapped to
  3314. * threads before the privatization is applied.
  3315. *
  3316. * If any array reference group requires the band mapped to threads
  3317. * to be unrolled, then we perform the required unrolling.
  3318. *
  3319. * We save a copy of the schedule that may influence the mappings
  3320. * to shared or private memory in kernel->copy_schedule.
  3321. *
  3322. * Finally, we add synchronization and copy statements to the schedule tree,
  3323. * remove the "thread" mark and create representations for the local
  3324. * variables in the kernel.
  3325. *
  3326. * We keep a copy of the isl_id that points to the kernel to ensure
  3327. * that the kernel does not get destroyed if the schedule node
  3328. * is freed due to some error condition.
  3329. */
  3330. __isl_give isl_schedule_node *gpu_create_kernel(struct gpu_gen *gen,
  3331. __isl_take isl_schedule_node *node, int scale,
  3332. __isl_keep isl_multi_val *sizes)
  3333. {
  3334. struct ppcg_kernel *kernel;
  3335. isl_id *id;
  3336. isl_schedule_node *node_thread;
  3337. isl_union_map *host_schedule;
  3338. isl_union_pw_multi_aff *contraction;
  3339. isl_set *host_domain;
  3340. isl_union_set *domain, *expanded;
  3341. int single_statement;
  3342. node = gpu_tree_insert_shared_before_thread(node);
  3343. if (!node)
  3344. return NULL;
  3345. kernel = isl_calloc_type(gen->ctx, struct ppcg_kernel);
  3346. kernel = ppcg_kernel_create_local_arrays(kernel, gen->prog);
  3347. if (!kernel)
  3348. return isl_schedule_node_free(node);
  3349. domain = isl_schedule_node_get_domain(node);
  3350. single_statement = isl_union_set_n_set(domain) == 1;
  3351. kernel->ctx = gen->ctx;
  3352. kernel->prog = gen->prog;
  3353. kernel->options = gen->options;
  3354. kernel->context = extract_context(node, gen->prog);
  3355. kernel->core = isl_union_set_universe(isl_union_set_copy(domain));
  3356. contraction = isl_schedule_node_get_subtree_contraction(node);
  3357. kernel->contraction = isl_union_pw_multi_aff_copy(contraction);
  3358. expanded = isl_union_set_copy(domain);
  3359. expanded = isl_union_set_preimage_union_pw_multi_aff(expanded,
  3360. contraction);
  3361. kernel->expanded_domain = isl_union_set_copy(expanded);
  3362. kernel->arrays = accessed_by_domain(expanded, gen->prog);
  3363. kernel->n_grid = n_outer_coincidence(node);
  3364. node_thread = isl_schedule_node_copy(node);
  3365. node_thread = gpu_tree_move_down_to_thread(node_thread, kernel->core);
  3366. node_thread = isl_schedule_node_child(node_thread, 0);
  3367. kernel->n_block = n_outer_coincidence(node_thread);
  3368. isl_schedule_node_free(node_thread);
  3369. kernel->id = gen->kernel_id++;
  3370. read_grid_and_block_sizes(kernel, gen);
  3371. kernel->sync_writes = compute_sync_writes(kernel, node);
  3372. host_schedule = isl_schedule_node_get_prefix_schedule_union_map(node);
  3373. host_domain = isl_set_from_union_set(isl_union_map_range(
  3374. host_schedule));
  3375. node = atomic_ancestors(node);
  3376. id = isl_id_alloc(gen->ctx, "kernel", kernel);
  3377. id = isl_id_set_free_user(id, &ppcg_kernel_free_wrap);
  3378. node = isl_schedule_node_insert_mark(node, isl_id_copy(id));
  3379. if (!single_statement)
  3380. node = group_statements(node, kernel->id);
  3381. node = isl_schedule_node_child(node, 0);
  3382. node = split_band(node, kernel->n_grid);
  3383. kernel->block_ids = ppcg_scop_generate_names(gen->prog->scop,
  3384. kernel->n_grid, "b");
  3385. kernel->block_filter = set_schedule_modulo(node, kernel->block_ids,
  3386. kernel->grid_dim);
  3387. kernel->grid_size = extract_grid_size(kernel,
  3388. isl_union_set_copy(domain));
  3389. if (!kernel->options->wrap)
  3390. node = snap_band_to_sizes(node, kernel->grid_dim,
  3391. kernel->options);
  3392. if (scale)
  3393. node = scale_band(node, isl_multi_val_copy(sizes));
  3394. node = isl_schedule_node_parent(node);
  3395. if (!single_statement)
  3396. node = isl_schedule_node_parent(node);
  3397. node = insert_guard(node, kernel->context, kernel->grid_size,
  3398. gen->prog->scop);
  3399. node = gpu_tree_move_down_to_thread(node, kernel->core);
  3400. node = isl_schedule_node_child(node, 0);
  3401. node = split_band(node, kernel->n_block);
  3402. kernel->thread_ids = ppcg_scop_generate_names(gen->prog->scop,
  3403. kernel->n_block, "t");
  3404. kernel->thread_filter = set_schedule_modulo(node, kernel->thread_ids,
  3405. kernel->block_dim);
  3406. if (extract_block_size(kernel, domain) < 0)
  3407. node = isl_schedule_node_free(node);
  3408. node = gpu_tree_move_up_to_kernel(node);
  3409. node = isl_schedule_node_child(node, 0);
  3410. node = insert_context(kernel, node);
  3411. node = isl_schedule_node_child(node, 0);
  3412. node = isl_schedule_node_insert_filter(node,
  3413. isl_union_set_copy(kernel->block_filter));
  3414. node = gpu_tree_move_up_to_kernel(node);
  3415. if (gpu_group_references(kernel, node) < 0)
  3416. node = isl_schedule_node_free(node);
  3417. localize_bounds(kernel, host_domain);
  3418. isl_set_free(host_domain);
  3419. check_shared_memory_bound(kernel);
  3420. mark_global_arrays(kernel);
  3421. compute_group_tilings(kernel);
  3422. node = gpu_tree_move_down_to_thread(node, kernel->core);
  3423. node = isl_schedule_node_child(node, 0);
  3424. if (!kernel->options->wrap)
  3425. node = snap_band_to_sizes(node, kernel->block_dim,
  3426. kernel->options);
  3427. node = isl_schedule_node_insert_filter(node,
  3428. isl_union_set_copy(kernel->thread_filter));
  3429. if (kernel_requires_unroll(kernel)) {
  3430. node = isl_schedule_node_child(node, 0);
  3431. node = unroll(node);
  3432. }
  3433. node = gpu_tree_move_up_to_thread(node);
  3434. kernel->copy_schedule_dim = isl_schedule_node_get_schedule_depth(node);
  3435. kernel->copy_schedule =
  3436. isl_schedule_node_get_prefix_schedule_union_pw_multi_aff(node);
  3437. contraction = isl_union_pw_multi_aff_copy(kernel->contraction);
  3438. kernel->copy_schedule =
  3439. isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
  3440. kernel->copy_schedule, contraction);
  3441. node = gpu_tree_move_up_to_kernel(node);
  3442. node = add_sync(kernel, node);
  3443. node = add_copies(kernel, node);
  3444. node = gpu_tree_move_down_to_shared(node, kernel->core);
  3445. node = isl_schedule_node_delete(node);
  3446. node = gpu_tree_move_down_to_thread(node, kernel->core);
  3447. node = isl_schedule_node_delete(node);
  3448. node = gpu_tree_move_up_to_kernel(node);
  3449. if (create_kernel_vars(kernel) < 0)
  3450. node = isl_schedule_node_free(node);
  3451. if (!single_statement)
  3452. node = isl_schedule_node_parent(node);
  3453. node = isl_schedule_node_parent(node);
  3454. isl_id_free(id);
  3455. return node;
  3456. }
  3457. /* Insert a zero-dimensional permutable band at "node".
  3458. */
  3459. static __isl_give isl_schedule_node *insert_empty_permutable_band(
  3460. __isl_take isl_schedule_node *node)
  3461. {
  3462. isl_space *space;
  3463. isl_schedule *schedule;
  3464. isl_union_set *domain;
  3465. isl_multi_union_pw_aff *mupa;
  3466. schedule = isl_schedule_node_get_schedule(node);
  3467. domain = isl_schedule_get_domain(schedule);
  3468. space = isl_union_set_get_space(domain);
  3469. isl_union_set_free(domain);
  3470. isl_schedule_free(schedule);
  3471. space = isl_space_set_from_params(space);
  3472. mupa = isl_multi_union_pw_aff_zero(space);
  3473. node = isl_schedule_node_insert_partial_schedule(node, mupa);
  3474. node = isl_schedule_node_band_set_permutable(node, 1);
  3475. return node;
  3476. }
  3477. /* See if hybrid tiling can be performed on "node" and its parent.
  3478. * If so, apply hybrid tiling and return the updated schedule tree.
  3479. * If not, return the original schedule tree.
  3480. * Return NULL on error.
  3481. *
  3482. * First check if "node", together with its parent, meets
  3483. * the basic requirements for hybrid tiling.
  3484. * If so, compute the relative dependence distances of "node"
  3485. * with respect to its parent and check if they are sufficiently bounded.
  3486. * If so, apply hybrid tiling using user specified tile sizes.
  3487. *
  3488. * The tile sizes are read before the dependence distance bounds are
  3489. * computed, because the user may have specified fewer dimensions
  3490. * than are available. In this case, the remaining schedule dimensions
  3491. * are split off and the dependence distances should be computed
  3492. * after these dimensions have been split off.
  3493. */
  3494. static __isl_give isl_schedule_node *try_hybrid_tile(struct gpu_gen *gen,
  3495. __isl_take isl_schedule_node *node)
  3496. {
  3497. int tile_len;
  3498. int *tile_size;
  3499. isl_bool ok;
  3500. isl_schedule_node *orig = node;
  3501. ppcg_ht_bounds *bounds;
  3502. ok = ppcg_ht_parent_has_input_pattern(node);
  3503. if (ok < 0)
  3504. return isl_schedule_node_free(node);
  3505. if (!ok)
  3506. return orig;
  3507. tile_len = 1 + isl_schedule_node_band_n_member(node);
  3508. tile_size = read_tile_sizes(gen, &tile_len);
  3509. if (!tile_size)
  3510. return isl_schedule_node_free(node);
  3511. node = isl_schedule_node_copy(node);
  3512. node = split_band(node, tile_len - 1);
  3513. node = isl_schedule_node_parent(node);
  3514. bounds = ppcg_ht_compute_bounds(gen->prog->scop, node);
  3515. node = isl_schedule_node_child(node, 0);
  3516. ok = ppcg_ht_bounds_is_valid(bounds);
  3517. if (ok >= 0 && ok)
  3518. node = gpu_hybrid_tile(gen, node, bounds, tile_size);
  3519. else
  3520. ppcg_ht_bounds_free(bounds);
  3521. free(tile_size);
  3522. if (ok >= 0 && !ok) {
  3523. isl_schedule_node_free(node);
  3524. return orig;
  3525. }
  3526. isl_schedule_node_free(orig);
  3527. if (ok < 0)
  3528. return isl_schedule_node_free(node);
  3529. return node;
  3530. }
  3531. /* If "node" is the outermost permutable band that can be mapped to block and
  3532. * thread identifiers in its branch (or the root of a subtree with
  3533. * no such outer bands),
  3534. * then mark the band as such, attaching a ppcg_kernel to the mark.
  3535. *
  3536. * If hybrid tiling is allowed, then first try and apply it
  3537. * to "node" and its parent.
  3538. *
  3539. * If "node" is the root of a subtree without permutable bands,
  3540. * then insert a zero-dimensional permutable band such that
  3541. * we can assume that "node" always points to a band node.
  3542. * This includes the case where "node" already points to a band node,
  3543. * but one without any coincident dimension. In this case,
  3544. * the extra node ensures that this original node does not get tiled.
  3545. *
  3546. * Tile "node" using user specified tile sizes, after splitting the band
  3547. * if the number of specified tile sizes is smaller than the dimension
  3548. * of the band. Mark the point band of this tiling as the band that
  3549. * needs to be mapped to threads and instruct the AST generator to unroll
  3550. * the band if the "unroll_gpu_tile" option is set.
  3551. * Create a kernel representing the domain instances that reach "node" and
  3552. * insert a mark node pointing to the ppcg_kernel before the band node.
  3553. */
  3554. static __isl_give isl_schedule_node *mark_outer_permutable(
  3555. __isl_take isl_schedule_node *node, void *user)
  3556. {
  3557. struct gpu_gen *gen = user;
  3558. int outer;
  3559. int scale;
  3560. int tile_len;
  3561. int *tile_size;
  3562. isl_id *id;
  3563. isl_multi_val *sizes;
  3564. outer = is_outer_tilable(node);
  3565. if (outer < 0)
  3566. return isl_schedule_node_free(node);
  3567. if (!outer)
  3568. return node;
  3569. if (gen->options->hybrid) {
  3570. isl_schedule_node *saved = isl_schedule_node_copy(node);
  3571. node = try_hybrid_tile(gen, node);
  3572. isl_schedule_node_free(saved);
  3573. if (node != saved)
  3574. return node;
  3575. }
  3576. if (isl_schedule_node_get_type(node) != isl_schedule_node_band ||
  3577. !isl_schedule_node_band_member_get_coincident(node, 0))
  3578. node = insert_empty_permutable_band(node);
  3579. tile_len = isl_schedule_node_band_n_member(node);
  3580. tile_size = read_tile_sizes(gen, &tile_len);
  3581. if (!tile_size)
  3582. return isl_schedule_node_free(node);
  3583. if (tile_len < isl_schedule_node_band_n_member(node))
  3584. node = isl_schedule_node_band_split(node, tile_len);
  3585. sizes = construct_band_tiles_sizes(node, tile_size);
  3586. node = tile_band(node, isl_multi_val_copy(sizes));
  3587. node = isl_schedule_node_child(node, 0);
  3588. if (gen->options->unroll_gpu_tile)
  3589. node = ppcg_set_schedule_node_type(node, isl_ast_loop_unroll);
  3590. id = isl_id_alloc(gen->ctx, "thread", NULL);
  3591. node = isl_schedule_node_insert_mark(node, id);
  3592. node = isl_schedule_node_parent(node);
  3593. scale = gen->options->scale_tile_loops;
  3594. node = gpu_create_kernel(gen, node, scale, sizes);
  3595. isl_multi_val_free(sizes);
  3596. free(tile_size);
  3597. return node;
  3598. }
  3599. /* Given a set or sequence node, return the union the filters of either all
  3600. * (if "only_initial" is not set) or the initial (if "only_initial" is set)
  3601. * direct subtrees that do not contain any suitably permutable bands
  3602. * (according to subtree_has_permutable_bands).
  3603. */
  3604. static __isl_give isl_union_set *get_non_parallel_subtree_filters(
  3605. __isl_keep isl_schedule_node *node, int only_initial)
  3606. {
  3607. isl_space *space;
  3608. isl_union_set *filter;
  3609. int i, n;
  3610. n = isl_schedule_node_n_children(node);
  3611. if (n < 0)
  3612. return NULL;
  3613. node = isl_schedule_node_copy(node);
  3614. node = isl_schedule_node_child(node, 0);
  3615. filter = isl_schedule_node_filter_get_filter(node);
  3616. node = isl_schedule_node_parent(node);
  3617. space = isl_union_set_get_space(filter);
  3618. isl_union_set_free(filter);
  3619. filter = isl_union_set_empty(space);
  3620. for (i = 0; i < n; ++i) {
  3621. int parallelism;
  3622. node = isl_schedule_node_child(node, i);
  3623. parallelism = subtree_has_permutable_bands(node);
  3624. if (parallelism < 0) {
  3625. filter = isl_union_set_free(filter);
  3626. } else if (!parallelism) {
  3627. isl_union_set *filter_i;
  3628. filter_i = isl_schedule_node_filter_get_filter(node);
  3629. filter = isl_union_set_union(filter, filter_i);
  3630. } else if (only_initial)
  3631. break;
  3632. node = isl_schedule_node_parent(node);
  3633. }
  3634. isl_schedule_node_free(node);
  3635. return filter;
  3636. }
  3637. /* Given a set or sequence node, return the union of the filters of
  3638. * the direct subtrees that do not contain any suitably permutable bands
  3639. * (according to subtree_has_permutable_bands).
  3640. */
  3641. static __isl_give isl_union_set *get_all_non_parallel_subtree_filters(
  3642. __isl_keep isl_schedule_node *node)
  3643. {
  3644. return get_non_parallel_subtree_filters(node, 0);
  3645. }
  3646. /* Given a set or sequence node, return the union of the filters of
  3647. * the initial direct subtrees that do not contain any suitably permutable
  3648. * bands (according to subtree_has_permutable_bands).
  3649. */
  3650. static __isl_give isl_union_set *get_initial_non_parallel_subtree_filters(
  3651. __isl_keep isl_schedule_node *node)
  3652. {
  3653. return get_non_parallel_subtree_filters(node, 1);
  3654. }
  3655. /* Mark all variables that are accessed by the statement instances in "domain"
  3656. * and that are local to "prog" as requiring a declaration in the host code.
  3657. * The statement instances in "domain" correspond to (a subset of)
  3658. * the active instances at "node".
  3659. * "node" is not modified by this function, except that NULL is returned
  3660. * in case of error.
  3661. */
  3662. static __isl_give isl_schedule_node *declare_accessed_local_variables(
  3663. __isl_take isl_schedule_node *node, struct gpu_prog *prog,
  3664. __isl_keep isl_union_set *domain)
  3665. {
  3666. isl_union_pw_multi_aff *contraction;
  3667. isl_union_set *arrays;
  3668. int i;
  3669. if (!ppcg_scop_any_hidden_declarations(prog->scop))
  3670. return node;
  3671. contraction = isl_schedule_node_get_subtree_contraction(node);
  3672. domain = isl_union_set_copy(domain);
  3673. domain = isl_union_set_preimage_union_pw_multi_aff(domain, contraction);
  3674. arrays = accessed_by_domain(domain, prog);
  3675. for (i = 0; i < prog->n_array; ++i) {
  3676. isl_space *space;
  3677. isl_set *set;
  3678. int empty;
  3679. if (!prog->array[i].local)
  3680. continue;
  3681. space = isl_set_get_space(prog->array[i].extent);
  3682. set = isl_union_set_extract_set(arrays, space);
  3683. empty = isl_set_plain_is_empty(set);
  3684. isl_set_free(set);
  3685. if (empty < 0)
  3686. goto error;
  3687. if (!empty)
  3688. prog->array[i].declare_local = 1;
  3689. }
  3690. isl_union_set_free(arrays);
  3691. return node;
  3692. error:
  3693. isl_union_set_free(arrays);
  3694. return isl_schedule_node_free(node);
  3695. }
  3696. /* If "node" points to a set node, then separate its children
  3697. * into subtrees that have suitably permutable bands and
  3698. * those that do not.
  3699. * Adjust the schedule tree in order to execute the second group
  3700. * after the first group and return a pointer to the first group,
  3701. * assuming there are any such subtrees.
  3702. * If "node" points to a sequence node, then separate the initial
  3703. * children that do not have suitably permutable bands and
  3704. * return a pointer to the subsequence of children that do have such bands,
  3705. * assuming there are any such subtrees.
  3706. *
  3707. * In both cases, mark all local variables in "prog" that are accessed by
  3708. * the group without permutable bands as requiring a declaration on the host.
  3709. */
  3710. static __isl_give isl_schedule_node *isolate_permutable_subtrees(
  3711. __isl_take isl_schedule_node *node, struct gpu_prog *prog)
  3712. {
  3713. isl_union_set *filter;
  3714. enum isl_schedule_node_type type;
  3715. if (!node)
  3716. return NULL;
  3717. type = isl_schedule_node_get_type(node);
  3718. if (type == isl_schedule_node_set) {
  3719. filter = get_all_non_parallel_subtree_filters(node);
  3720. node = declare_accessed_local_variables(node, prog, filter);
  3721. node = isl_schedule_node_order_after(node, filter);
  3722. } else if (type == isl_schedule_node_sequence) {
  3723. filter = get_initial_non_parallel_subtree_filters(node);
  3724. node = declare_accessed_local_variables(node, prog, filter);
  3725. node = isl_schedule_node_order_before(node, filter);
  3726. }
  3727. return node;
  3728. }
  3729. /* Replace any reference to an array element in the range of "copy"
  3730. * by a reference to all array elements (defined by the extent of the array).
  3731. */
  3732. static __isl_give isl_union_map *approximate_copy_out(
  3733. __isl_take isl_union_map *copy, struct gpu_prog *prog)
  3734. {
  3735. int i;
  3736. isl_union_map *res;
  3737. res = isl_union_map_empty(isl_union_map_get_space(copy));
  3738. for (i = 0; i < prog->n_array; ++i) {
  3739. isl_space *space;
  3740. isl_set *set;
  3741. isl_union_map *copy_i;
  3742. isl_union_set *extent, *domain;
  3743. space = isl_space_copy(prog->array[i].space);
  3744. extent = isl_union_set_from_set(isl_set_universe(space));
  3745. copy_i = isl_union_map_copy(copy);
  3746. copy_i = isl_union_map_intersect_range(copy_i, extent);
  3747. set = isl_set_copy(prog->array[i].extent);
  3748. extent = isl_union_set_from_set(set);
  3749. domain = isl_union_map_domain(copy_i);
  3750. copy_i = isl_union_map_from_domain_and_range(domain, extent);
  3751. res = isl_union_map_union(res, copy_i);
  3752. }
  3753. isl_union_map_free(copy);
  3754. return res;
  3755. }
  3756. /* Insert "kernel" marks that point to a ppcg_kernel structure
  3757. * in front of all outermost tilable band that (by construction)
  3758. * have at least one parallel loop.
  3759. */
  3760. static __isl_give isl_schedule_node *mark_kernels(struct gpu_gen *gen,
  3761. __isl_take isl_schedule_node *node)
  3762. {
  3763. return isl_schedule_node_map_descendant_bottom_up(node,
  3764. &mark_outer_permutable, gen);
  3765. }
  3766. /* Construct schedule constraints from the dependences in prog->scop and
  3767. * the array order dependences in prog->array_order.
  3768. *
  3769. * If live range reordering is allowed, then we need to make sure
  3770. * that live ranges on arrays are not run in parallel since doing
  3771. * so would require array expansion. We therefore add the array
  3772. * order dependences to the coincidence dependences. Non-zero array
  3773. * order dependences will then prevent a schedule dimension from being
  3774. * considered parallel.
  3775. * Live ranges derived from scalars are allowed to be run in parallel
  3776. * since we force the scalars to be mapped to private memory in
  3777. * check_scalar_live_ranges.
  3778. * If live range reordering is allowed, then the false dependences
  3779. * are not added to the validity constraints as that would prevent
  3780. * reordering. Instead, the external false dependences that enforce that reads
  3781. * from potentially live-in data precede any later write and
  3782. * that writes of potentially live-out data follow any other earlier write
  3783. * are added to the validity and the coincidence constraints.
  3784. * The false dependences are still added to the proximity constraints
  3785. * for consistency with the case where live range reordering is not allowed.
  3786. * The coincidence constraints then consist of flow dependences,
  3787. * external false dependences and array order dependences.
  3788. * The independences can be filtered out from the first two sets.
  3789. * They have already been filtered out from the array order dependences
  3790. * on a per array basis in collect_order_dependences.
  3791. * There is no need for a per array handling of the other two sets
  3792. * as there should be no flow or external false dependence on local
  3793. * variables that can be filtered out.
  3794. */
  3795. static __isl_give isl_schedule_constraints *construct_schedule_constraints(
  3796. struct gpu_prog *prog)
  3797. {
  3798. isl_union_set *domain;
  3799. isl_union_map *dep_raw, *dep;
  3800. isl_union_map *validity, *proximity, *coincidence;
  3801. isl_schedule_constraints *sc;
  3802. domain = isl_union_set_copy(prog->scop->domain);
  3803. sc = isl_schedule_constraints_on_domain(domain);
  3804. sc = isl_schedule_constraints_set_context(sc,
  3805. isl_set_copy(prog->scop->context));
  3806. if (prog->scop->options->live_range_reordering) {
  3807. sc = isl_schedule_constraints_set_conditional_validity(sc,
  3808. isl_union_map_copy(prog->scop->tagged_dep_flow),
  3809. isl_union_map_copy(prog->scop->tagged_dep_order));
  3810. proximity = isl_union_map_copy(prog->scop->dep_flow);
  3811. validity = isl_union_map_copy(proximity);
  3812. validity = isl_union_map_union(validity,
  3813. isl_union_map_copy(prog->scop->dep_forced));
  3814. proximity = isl_union_map_union(proximity,
  3815. isl_union_map_copy(prog->scop->dep_false));
  3816. coincidence = isl_union_map_copy(validity);
  3817. coincidence = isl_union_map_subtract(coincidence,
  3818. isl_union_map_copy(prog->scop->independence));
  3819. coincidence = isl_union_map_union(coincidence,
  3820. isl_union_map_copy(prog->array_order));
  3821. } else {
  3822. dep_raw = isl_union_map_copy(prog->scop->dep_flow);
  3823. dep = isl_union_map_copy(prog->scop->dep_false);
  3824. dep = isl_union_map_union(dep, dep_raw);
  3825. dep = isl_union_map_coalesce(dep);
  3826. proximity = isl_union_map_copy(dep);
  3827. coincidence = isl_union_map_copy(dep);
  3828. validity = dep;
  3829. }
  3830. sc = isl_schedule_constraints_set_validity(sc, validity);
  3831. sc = isl_schedule_constraints_set_coincidence(sc, coincidence);
  3832. sc = isl_schedule_constraints_set_proximity(sc, proximity);
  3833. if (prog->scop->options->debug->dump_schedule_constraints)
  3834. isl_schedule_constraints_dump(sc);
  3835. return sc;
  3836. }
  3837. /* Compute an appropriate schedule based on the accesses in
  3838. * gen->read and gen->write.
  3839. *
  3840. * We derive schedule constraints from the dependences in gen->prog->scop
  3841. * and then use isl to compute a schedule that has a parallel loop
  3842. * in each tilable band.
  3843. * During the schedule construction, some statement instances
  3844. * may be grouped first based on the input schedule.
  3845. */
  3846. static __isl_give isl_schedule *compute_schedule(struct gpu_gen *gen)
  3847. {
  3848. isl_schedule_constraints *sc;
  3849. isl_schedule *schedule;
  3850. sc = construct_schedule_constraints(gen->prog);
  3851. schedule = gen->prog->scop->schedule;
  3852. schedule = ppcg_compute_schedule(sc, schedule, gen->options);
  3853. return schedule;
  3854. }
  3855. /* If the band node "node" has exactly one member then mark it permutable.
  3856. */
  3857. static __isl_give isl_schedule_node *band_set_permutable(
  3858. __isl_take isl_schedule_node *node,
  3859. __isl_keep isl_schedule_constraints *sc)
  3860. {
  3861. if (isl_schedule_node_band_n_member(node) == 1)
  3862. node = isl_schedule_node_band_set_permutable(node, 1);
  3863. return node;
  3864. }
  3865. /* Return the coincidence constraints between pairs of instances
  3866. * that are scheduled together by the ancestors of "node".
  3867. * That is, select those coincidence constraints that relate
  3868. * pairs of instances that have the same value for the prefix schedule.
  3869. * If the schedule depth is zero, then the prefix schedule does not
  3870. * contain any information, so we intersect domain and range
  3871. * of the schedule constraints with the reaching domain elements instead.
  3872. */
  3873. static __isl_give isl_union_map *get_local_coincidence(
  3874. __isl_keep isl_schedule_node *node,
  3875. __isl_keep isl_schedule_constraints *sc)
  3876. {
  3877. isl_union_map *coincidence;
  3878. isl_multi_union_pw_aff *prefix;
  3879. isl_union_pw_multi_aff *contraction;
  3880. coincidence = isl_schedule_constraints_get_coincidence(sc);
  3881. contraction = isl_schedule_node_get_subtree_contraction(node);
  3882. if (isl_schedule_node_get_schedule_depth(node) == 0) {
  3883. isl_union_set *domain;
  3884. domain = isl_schedule_node_get_domain(node);
  3885. domain = isl_union_set_preimage_union_pw_multi_aff(domain,
  3886. contraction);
  3887. coincidence = isl_union_map_intersect_domain(coincidence,
  3888. isl_union_set_copy(domain));
  3889. coincidence = isl_union_map_intersect_range(coincidence,
  3890. domain);
  3891. return coincidence;
  3892. }
  3893. prefix = isl_schedule_node_get_prefix_schedule_multi_union_pw_aff(node);
  3894. prefix = isl_multi_union_pw_aff_pullback_union_pw_multi_aff(prefix,
  3895. contraction);
  3896. return isl_union_map_eq_at_multi_union_pw_aff(coincidence, prefix);
  3897. }
  3898. /* For each member in the band node "node", determine whether
  3899. * it is coincident with respect to the outer nodes and mark
  3900. * it accordingly.
  3901. *
  3902. * That is, for each coincidence constraint between pairs
  3903. * of instances that are scheduled together by the outer nodes,
  3904. * check that domain and range are assigned the same value
  3905. * by the band member. This test is performed by checking
  3906. * that imposing the same value for the band member does not
  3907. * remove any elements from the set of coincidence constraints.
  3908. */
  3909. static __isl_give isl_schedule_node *band_set_coincident(
  3910. __isl_take isl_schedule_node *node,
  3911. __isl_keep isl_schedule_constraints *sc)
  3912. {
  3913. isl_union_map *coincidence;
  3914. isl_union_pw_multi_aff *contraction;
  3915. isl_multi_union_pw_aff *partial;
  3916. int i, n;
  3917. coincidence = get_local_coincidence(node, sc);
  3918. partial = isl_schedule_node_band_get_partial_schedule(node);
  3919. contraction = isl_schedule_node_get_subtree_contraction(node);
  3920. partial = isl_multi_union_pw_aff_pullback_union_pw_multi_aff(partial,
  3921. contraction);
  3922. n = isl_schedule_node_band_n_member(node);
  3923. for (i = 0; i < n; ++i) {
  3924. isl_union_map *coincidence_i;
  3925. isl_union_pw_aff *upa;
  3926. isl_multi_union_pw_aff *partial_i;
  3927. int subset;
  3928. upa = isl_multi_union_pw_aff_get_union_pw_aff(partial, i);
  3929. partial_i = isl_multi_union_pw_aff_from_union_pw_aff(upa);
  3930. coincidence_i = isl_union_map_copy(coincidence);
  3931. coincidence_i = isl_union_map_eq_at_multi_union_pw_aff(
  3932. coincidence_i, partial_i);
  3933. subset = isl_union_map_is_subset(coincidence, coincidence_i);
  3934. isl_union_map_free(coincidence_i);
  3935. if (subset < 0)
  3936. break;
  3937. node = isl_schedule_node_band_member_set_coincident(node, i,
  3938. subset);
  3939. }
  3940. if (i < n)
  3941. node = isl_schedule_node_free(node);
  3942. isl_multi_union_pw_aff_free(partial);
  3943. isl_union_map_free(coincidence);
  3944. return node;
  3945. }
  3946. /* If "node" is a band, then set its properties.
  3947. *
  3948. * In particular, if the band has exactly one member, then mark it permutable.
  3949. * Mark the band member coincident based on the coincidence constraints
  3950. * of "sc".
  3951. */
  3952. static __isl_give isl_schedule_node *set_band_properties(
  3953. __isl_take isl_schedule_node *node, void *user)
  3954. {
  3955. isl_schedule_constraints *sc = user;
  3956. if (isl_schedule_node_get_type(node) != isl_schedule_node_band)
  3957. return node;
  3958. if (isl_schedule_node_band_n_member(node) == 0)
  3959. return node;
  3960. node = band_set_permutable(node, sc);
  3961. node = band_set_coincident(node, sc);
  3962. return node;
  3963. }
  3964. /* Return the original schedule with all bands marked permutable and
  3965. * all band members marked coincident based on the coincidence constraints.
  3966. * The bands are explicitly marked permutable so that they will be considered
  3967. * by mark_outer_permutable.
  3968. */
  3969. static __isl_give isl_schedule *determine_properties_original_schedule(
  3970. struct gpu_gen *gen)
  3971. {
  3972. isl_schedule *schedule;
  3973. isl_schedule_constraints *sc;
  3974. schedule = isl_schedule_copy(gen->prog->scop->schedule);
  3975. sc = construct_schedule_constraints(gen->prog);
  3976. schedule = isl_schedule_map_schedule_node_bottom_up(schedule,
  3977. &set_band_properties, sc);
  3978. isl_schedule_constraints_free(sc);
  3979. return schedule;
  3980. }
  3981. /* Compute a schedule or determine the properties of the original schedule
  3982. * depending on the value of the "reschedule" option.
  3983. */
  3984. static __isl_give isl_schedule *compute_or_set_properties(void *user)
  3985. {
  3986. struct gpu_gen *gen = user;
  3987. if (gen->options->reschedule)
  3988. return compute_schedule(gen);
  3989. else
  3990. return determine_properties_original_schedule(gen);
  3991. }
  3992. /* Obtain a schedule for the scop, by reading it from
  3993. * a file, by computing one or by determining the properties
  3994. * of the original schedule.
  3995. */
  3996. __isl_give isl_schedule *get_schedule(struct gpu_gen *gen)
  3997. {
  3998. return ppcg_get_schedule(gen->ctx, gen->options,
  3999. &compute_or_set_properties, gen);
  4000. }
  4001. /* Construct the string "<a>_<b>".
  4002. */
  4003. static char *concat(isl_ctx *ctx, const char *a, const char *b)
  4004. {
  4005. isl_printer *p;
  4006. char *s;
  4007. p = isl_printer_to_str(ctx);
  4008. p = isl_printer_print_str(p, a);
  4009. p = isl_printer_print_str(p, "_");
  4010. p = isl_printer_print_str(p, b);
  4011. s = isl_printer_get_str(p);
  4012. isl_printer_free(p);
  4013. return s;
  4014. }
  4015. /* For each array in "prog" of which an element appears in "accessed" and
  4016. * that is not a read only scalar, create a zero-dimensional universe set
  4017. * of which the tuple id has name "<prefix>_<name of array>" and a user
  4018. * pointer pointing to the array (gpu_array_info).
  4019. *
  4020. * If the array is local to "prog", then make sure it will be declared
  4021. * in the host code.
  4022. *
  4023. * Return the list of these universe sets.
  4024. */
  4025. static __isl_give isl_union_set_list *create_copy_filters(struct gpu_prog *prog,
  4026. const char *prefix, __isl_take isl_union_set *accessed)
  4027. {
  4028. int i;
  4029. isl_ctx *ctx;
  4030. isl_union_set_list *filters;
  4031. ctx = prog->ctx;
  4032. filters = isl_union_set_list_alloc(ctx, 0);
  4033. for (i = 0; i < prog->n_array; ++i) {
  4034. struct gpu_array_info *array = &prog->array[i];
  4035. isl_space *space;
  4036. isl_set *accessed_i;
  4037. int empty;
  4038. char *name;
  4039. isl_id *id;
  4040. isl_union_set *uset;
  4041. if (gpu_array_is_read_only_scalar(array))
  4042. continue;
  4043. space = isl_space_copy(array->space);
  4044. accessed_i = isl_union_set_extract_set(accessed, space);
  4045. empty = isl_set_plain_is_empty(accessed_i);
  4046. isl_set_free(accessed_i);
  4047. if (empty < 0) {
  4048. filters = isl_union_set_list_free(filters);
  4049. break;
  4050. }
  4051. if (empty)
  4052. continue;
  4053. array->global = 1;
  4054. if (array->local)
  4055. array->declare_local = 1;
  4056. name = concat(ctx, prefix, array->name);
  4057. id = name ? isl_id_alloc(ctx, name, array) : NULL;
  4058. free(name);
  4059. space = isl_space_set_alloc(ctx, 0, 0);
  4060. space = isl_space_set_tuple_id(space, isl_dim_set, id);
  4061. uset = isl_union_set_from_set(isl_set_universe(space));
  4062. filters = isl_union_set_list_add(filters, uset);
  4063. }
  4064. isl_union_set_free(accessed);
  4065. return filters;
  4066. }
  4067. /* Make sure that code for the statements in "filters" that
  4068. * copy arrays to or from the device is only generated when
  4069. * the size of the corresponding array is positive.
  4070. * That is, add a set node underneath "graft" with "filters" as children
  4071. * and for each child add a guard that the selects the parameter
  4072. * values for which the corresponding array has a positive size.
  4073. * The array is available in the user pointer of the statement identifier.
  4074. * "depth" is the schedule depth of the position where "graft"
  4075. * will be added.
  4076. */
  4077. static __isl_give isl_schedule_node *insert_positive_size_guards(
  4078. __isl_take isl_schedule_node *graft,
  4079. __isl_take isl_union_set_list *filters, int depth)
  4080. {
  4081. int i, n;
  4082. graft = isl_schedule_node_child(graft, 0);
  4083. graft = isl_schedule_node_insert_set(graft, filters);
  4084. n = isl_schedule_node_n_children(graft);
  4085. for (i = 0; i < n; ++i) {
  4086. isl_union_set *filter;
  4087. isl_set *domain, *guard;
  4088. isl_id *id;
  4089. struct gpu_array_info *array;
  4090. graft = isl_schedule_node_child(graft, i);
  4091. filter = isl_schedule_node_filter_get_filter(graft);
  4092. domain = isl_set_from_union_set(filter);
  4093. id = isl_set_get_tuple_id(domain);
  4094. array = isl_id_get_user(id);
  4095. isl_id_free(id);
  4096. isl_set_free(domain);
  4097. guard = gpu_array_positive_size_guard(array);
  4098. guard = isl_set_from_params(guard);
  4099. guard = isl_set_add_dims(guard, isl_dim_set, depth);
  4100. graft = isl_schedule_node_child(graft, 0);
  4101. graft = isl_schedule_node_insert_guard(graft, guard);
  4102. graft = isl_schedule_node_parent(graft);
  4103. graft = isl_schedule_node_parent(graft);
  4104. }
  4105. graft = isl_schedule_node_parent(graft);
  4106. return graft;
  4107. }
  4108. /* Create a graft for copying arrays to or from the device,
  4109. * whenever the size of the array is strictly positive.
  4110. * Each statement is called "<prefix>_<name of array>" and
  4111. * the identifier has a user pointer pointing to the array.
  4112. * The graft will be added at the position specified by "node".
  4113. * "copy" contains the array elements that need to be copied.
  4114. * Only arrays of which some elements need to be copied
  4115. * will have a corresponding statement in the graph.
  4116. * Note though that each such statement will copy the entire array.
  4117. */
  4118. static __isl_give isl_schedule_node *create_copy_device(struct gpu_prog *prog,
  4119. __isl_keep isl_schedule_node *node, const char *prefix,
  4120. __isl_take isl_union_set *copy)
  4121. {
  4122. int depth;
  4123. isl_ctx *ctx;
  4124. isl_space *space;
  4125. isl_union_set *all, *domain;
  4126. isl_union_set_list *filters;
  4127. isl_union_map *extension;
  4128. isl_schedule_node *graft;
  4129. ctx = prog->ctx;
  4130. depth = isl_schedule_node_get_schedule_depth(node);
  4131. filters = create_copy_filters(prog, prefix, copy);
  4132. all = isl_union_set_list_union(isl_union_set_list_copy(filters));
  4133. space = depth < 0 ? NULL : isl_space_set_alloc(ctx, 0, depth);
  4134. domain = isl_union_set_from_set(isl_set_universe(space));
  4135. extension = isl_union_map_from_domain_and_range(domain, all);
  4136. graft = isl_schedule_node_from_extension(extension);
  4137. if (!filters)
  4138. return isl_schedule_node_free(graft);
  4139. if (isl_union_set_list_n_union_set(filters) == 0) {
  4140. isl_union_set_list_free(filters);
  4141. return graft;
  4142. }
  4143. return insert_positive_size_guards(graft, filters, depth);
  4144. }
  4145. /* Return (the universe spaces of) the arrays that are declared
  4146. * inside the scop corresponding to "prog" and for which all
  4147. * potential writes inside the scop form a subset of "domain".
  4148. */
  4149. static __isl_give isl_union_set *extract_local_accesses(struct gpu_prog *prog,
  4150. __isl_keep isl_union_set *domain)
  4151. {
  4152. int i;
  4153. isl_union_set *local;
  4154. local = isl_union_set_empty(isl_union_set_get_space(domain));
  4155. for (i = 0; i < prog->n_array; ++i) {
  4156. isl_set *set;
  4157. isl_union_map *to_outer;
  4158. isl_union_map *may_write;
  4159. isl_union_set *write_domain;
  4160. isl_union_set *fields;
  4161. int subset;
  4162. if (!prog->array[i].local)
  4163. continue;
  4164. set = isl_set_universe(isl_space_copy(prog->array[i].space));
  4165. to_outer = isl_union_map_copy(prog->to_outer);
  4166. to_outer = isl_union_map_intersect_range(to_outer,
  4167. isl_union_set_from_set(isl_set_copy(set)));
  4168. fields = isl_union_map_domain(to_outer);
  4169. may_write = isl_union_map_copy(prog->may_write);
  4170. may_write = isl_union_map_intersect_range(may_write, fields);
  4171. write_domain = isl_union_map_domain(may_write);
  4172. subset = isl_union_set_is_subset(write_domain, domain);
  4173. isl_union_set_free(write_domain);
  4174. if (subset < 0) {
  4175. isl_set_free(set);
  4176. return isl_union_set_free(local);
  4177. } else if (subset) {
  4178. local = isl_union_set_add_set(local, set);
  4179. } else {
  4180. isl_set_free(set);
  4181. }
  4182. }
  4183. return local;
  4184. }
  4185. /* Internal data structure for node_may_persist.
  4186. *
  4187. * "tagger" maps tagged iteration domains to the corresponding untagged
  4188. * iteration domain.
  4189. *
  4190. * "may_persist_flow" is the set of all tagged dataflow dependences
  4191. * with those dependences removed that either precede or follow
  4192. * the kernel launch in a sequence.
  4193. * "inner_band_flow" is the set of all tagged dataflow dependences
  4194. * that are local to a given iteration of the outer band nodes
  4195. * with respect to the current node.
  4196. * "local_flow" is equal to "inner_band_flow", except that the domain
  4197. * and the range have been intersected with intermediate filters
  4198. * on children of sets or sequences.
  4199. */
  4200. struct ppcg_may_persist_data {
  4201. isl_union_pw_multi_aff *tagger;
  4202. isl_union_map *local_flow;
  4203. isl_union_map *inner_band_flow;
  4204. isl_union_map *may_persist_flow;
  4205. };
  4206. /* Update the information in "data" based on the band ancestor "node".
  4207. *
  4208. * In particular, we restrict the dependences in data->local_flow
  4209. * to those dependence where the source and the sink occur in
  4210. * the same iteration of the given band node.
  4211. * We also update data->inner_band_flow to the new value of
  4212. * data->local_flow.
  4213. */
  4214. static int update_may_persist_at_band(__isl_keep isl_schedule_node *node,
  4215. struct ppcg_may_persist_data *data)
  4216. {
  4217. isl_multi_union_pw_aff *partial;
  4218. isl_union_pw_multi_aff *contraction;
  4219. isl_union_map *flow;
  4220. if (isl_schedule_node_band_n_member(node) == 0)
  4221. return 0;
  4222. partial = isl_schedule_node_band_get_partial_schedule(node);
  4223. contraction = isl_schedule_node_get_subtree_contraction(node);
  4224. partial = isl_multi_union_pw_aff_pullback_union_pw_multi_aff(partial,
  4225. contraction);
  4226. partial = isl_multi_union_pw_aff_pullback_union_pw_multi_aff(partial,
  4227. isl_union_pw_multi_aff_copy(data->tagger));
  4228. flow = data->local_flow;
  4229. flow = isl_union_map_eq_at_multi_union_pw_aff(flow, partial);
  4230. data->local_flow = flow;
  4231. isl_union_map_free(data->inner_band_flow);
  4232. data->inner_band_flow = isl_union_map_copy(data->local_flow);
  4233. return 0;
  4234. }
  4235. /* Given a set of local reaching domain elements "domain",
  4236. * expand them to the corresponding leaf domain elements using "contraction"
  4237. * and insert the array references tags using data->tagger.
  4238. */
  4239. static __isl_give isl_union_set *expand_and_tag(
  4240. __isl_take isl_union_set *domain,
  4241. __isl_take isl_union_pw_multi_aff *contraction,
  4242. struct ppcg_may_persist_data *data)
  4243. {
  4244. domain = isl_union_set_preimage_union_pw_multi_aff(domain,
  4245. contraction);
  4246. domain = isl_union_set_preimage_union_pw_multi_aff(domain,
  4247. isl_union_pw_multi_aff_copy(data->tagger));
  4248. return domain;
  4249. }
  4250. /* Given a filter node that is the child of a set or sequence node,
  4251. * restrict data->local_flow to refer only to those elements
  4252. * in the filter of the node.
  4253. * "contraction" maps the leaf domain elements of the schedule tree
  4254. * to the corresponding domain elements at (the parent of) "node".
  4255. */
  4256. static int filter_flow(__isl_keep isl_schedule_node *node,
  4257. struct ppcg_may_persist_data *data,
  4258. __isl_take isl_union_pw_multi_aff *contraction)
  4259. {
  4260. isl_union_set *filter;
  4261. isl_union_map *flow;
  4262. flow = data->local_flow;
  4263. filter = isl_schedule_node_filter_get_filter(node);
  4264. filter = expand_and_tag(filter, contraction, data);
  4265. flow = isl_union_map_intersect_domain(flow, isl_union_set_copy(filter));
  4266. flow = isl_union_map_intersect_range(flow, filter);
  4267. data->local_flow = flow;
  4268. return 0;
  4269. }
  4270. /* Given a filter node "node", collect the filters on all preceding siblings
  4271. * (which are also filter nodes), add them to "filters" and return the result.
  4272. */
  4273. static __isl_give isl_union_set *add_previous_filters(
  4274. __isl_take isl_union_set *filters, __isl_keep isl_schedule_node *node)
  4275. {
  4276. isl_schedule_node *sibling;
  4277. sibling = isl_schedule_node_copy(node);
  4278. while (sibling && isl_schedule_node_has_previous_sibling(sibling)) {
  4279. isl_union_set *filter;
  4280. sibling = isl_schedule_node_previous_sibling(sibling);
  4281. filter = isl_schedule_node_filter_get_filter(sibling);
  4282. filters = isl_union_set_union(filters, filter);
  4283. }
  4284. isl_schedule_node_free(sibling);
  4285. if (!sibling)
  4286. return isl_union_set_free(filters);
  4287. return filters;
  4288. }
  4289. /* Given a filter node "node", collect the filters on all following siblings
  4290. * (which are also filter nodes), add them to "filters" and return the result.
  4291. */
  4292. static __isl_give isl_union_set *add_next_filters(
  4293. __isl_take isl_union_set *filters, __isl_keep isl_schedule_node *node)
  4294. {
  4295. isl_schedule_node *sibling;
  4296. sibling = isl_schedule_node_copy(node);
  4297. while (sibling && isl_schedule_node_has_next_sibling(sibling)) {
  4298. isl_union_set *filter;
  4299. sibling = isl_schedule_node_next_sibling(sibling);
  4300. filter = isl_schedule_node_filter_get_filter(sibling);
  4301. filters = isl_union_set_union(filters, filter);
  4302. }
  4303. isl_schedule_node_free(sibling);
  4304. if (!sibling)
  4305. return isl_union_set_free(filters);
  4306. return filters;
  4307. }
  4308. /* Remove those flow dependences from data->may_persist_flow
  4309. * that flow between elements of "domain" within the same iteration
  4310. * of all outer band nodes.
  4311. * "contraction" maps the leaf domain elements of the schedule tree
  4312. * to the corresponding elements "domain".
  4313. */
  4314. static void remove_external_flow(struct ppcg_may_persist_data *data,
  4315. __isl_take isl_union_set *domain,
  4316. __isl_keep isl_union_pw_multi_aff *contraction)
  4317. {
  4318. isl_union_map *flow;
  4319. contraction = isl_union_pw_multi_aff_copy(contraction);
  4320. domain = expand_and_tag(domain, contraction, data);
  4321. flow = isl_union_map_copy(data->local_flow);
  4322. flow = isl_union_map_intersect_domain(flow, isl_union_set_copy(domain));
  4323. flow = isl_union_map_intersect_range(flow, domain);
  4324. data->may_persist_flow = isl_union_map_subtract(data->may_persist_flow,
  4325. flow);
  4326. }
  4327. /* Update the information in "data" based on the filter ancestor "node".
  4328. * We only need to modify anything if the filter is the child
  4329. * of a set or sequence node.
  4330. *
  4331. * In the case of a sequence, we remove the dependences between
  4332. * statement instances that are both executed either before or
  4333. * after the subtree that will be mapped to a kernel, within
  4334. * the same iteration of outer bands.
  4335. *
  4336. * In both cases, we restrict data->local_flow to the current child.
  4337. */
  4338. static int update_may_persist_at_filter(__isl_keep isl_schedule_node *node,
  4339. struct ppcg_may_persist_data *data)
  4340. {
  4341. enum isl_schedule_node_type type;
  4342. isl_schedule_node *parent;
  4343. isl_space *space;
  4344. isl_union_pw_multi_aff *contraction;
  4345. isl_union_set *before, *after, *filter;
  4346. type = isl_schedule_node_get_parent_type(node);
  4347. if (type != isl_schedule_node_sequence && type != isl_schedule_node_set)
  4348. return 0;
  4349. parent = isl_schedule_node_copy(node);
  4350. parent = isl_schedule_node_parent(parent);
  4351. contraction = isl_schedule_node_get_subtree_contraction(parent);
  4352. isl_schedule_node_free(parent);
  4353. if (type == isl_schedule_node_set)
  4354. return filter_flow(node, data, contraction);
  4355. filter = isl_schedule_node_filter_get_filter(node);
  4356. space = isl_union_set_get_space(filter);
  4357. isl_union_set_free(filter);
  4358. before = isl_union_set_empty(space);
  4359. after = isl_union_set_copy(before);
  4360. before = add_previous_filters(before, node);
  4361. after = add_next_filters(after, node);
  4362. remove_external_flow(data, before, contraction);
  4363. remove_external_flow(data, after, contraction);
  4364. return filter_flow(node, data, contraction);
  4365. }
  4366. /* Update the information in "data" based on the ancestor "node".
  4367. */
  4368. static isl_stat update_may_persist_at(__isl_keep isl_schedule_node *node,
  4369. void *user)
  4370. {
  4371. struct ppcg_may_persist_data *data = user;
  4372. switch (isl_schedule_node_get_type(node)) {
  4373. case isl_schedule_node_error:
  4374. return isl_stat_error;
  4375. case isl_schedule_node_context:
  4376. case isl_schedule_node_domain:
  4377. case isl_schedule_node_expansion:
  4378. case isl_schedule_node_extension:
  4379. case isl_schedule_node_guard:
  4380. case isl_schedule_node_leaf:
  4381. case isl_schedule_node_mark:
  4382. case isl_schedule_node_sequence:
  4383. case isl_schedule_node_set:
  4384. break;
  4385. case isl_schedule_node_band:
  4386. if (update_may_persist_at_band(node, data) < 0)
  4387. return isl_stat_error;
  4388. break;
  4389. case isl_schedule_node_filter:
  4390. if (update_may_persist_at_filter(node, data) < 0)
  4391. return isl_stat_error;
  4392. break;
  4393. }
  4394. return isl_stat_ok;
  4395. }
  4396. /* Determine the set of array elements that may need to be perserved
  4397. * by a kernel constructed from the subtree at "node".
  4398. * This includes the set of array elements that may need to be preserved
  4399. * by the entire scop (prog->may_persist) and the elements for which
  4400. * there is a potential flow dependence that may cross a kernel launch.
  4401. *
  4402. * To determine the second set, we start from all flow dependences.
  4403. * From this set of dependences, we remove those that cannot possibly
  4404. * require data to be preserved by a kernel launch.
  4405. * In particular, we consider the following sets of dependences.
  4406. * - dependences of which the write occurs inside the kernel.
  4407. * If the data is needed outside the kernel, then it will
  4408. * be copied out immediately after the kernel launch, so there
  4409. * is no need for any special care.
  4410. * - dependences of which the read occurs inside the kernel and the
  4411. * corresponding write occurs inside the same iteration of the
  4412. * outer band nodes. This means that the data is needed in
  4413. * the first kernel launch after the write, which is already
  4414. * taken care of by the standard copy-in. That is, the data
  4415. * do not need to be preserved by any intermediate call to
  4416. * the same kernel.
  4417. * - dependences of which the write and the read either both occur
  4418. * before the kernel launch or both occur after the kernel launch,
  4419. * within the same iteration of the outer band nodes with respect
  4420. * to the sequence that determines the ordering of the dependence
  4421. * and the kernel launch. Such flow dependences cannot cross
  4422. * any kernel launch.
  4423. *
  4424. * For the remaining (tagged) dependences, we take the domain
  4425. * (i.e., the tagged writes) and apply the tagged access relation
  4426. * to obtain the accessed data elements.
  4427. * These are then combined with the elements that may need to be
  4428. * preserved by the entire scop.
  4429. */
  4430. static __isl_give isl_union_set *node_may_persist(
  4431. __isl_keep isl_schedule_node *node, struct gpu_prog *prog)
  4432. {
  4433. struct ppcg_may_persist_data data;
  4434. isl_union_pw_multi_aff *contraction;
  4435. isl_union_set *domain;
  4436. isl_union_set *persist;
  4437. isl_union_map *flow, *local_flow;
  4438. data.tagger = prog->scop->tagger;
  4439. flow = isl_union_map_copy(prog->scop->tagged_dep_flow);
  4440. data.local_flow = isl_union_map_copy(flow);
  4441. data.inner_band_flow = isl_union_map_copy(flow);
  4442. data.may_persist_flow = flow;
  4443. if (isl_schedule_node_foreach_ancestor_top_down(node,
  4444. &update_may_persist_at, &data) < 0)
  4445. data.may_persist_flow =
  4446. isl_union_map_free(data.may_persist_flow);
  4447. flow = data.may_persist_flow;
  4448. isl_union_map_free(data.local_flow);
  4449. domain = isl_schedule_node_get_domain(node);
  4450. contraction = isl_schedule_node_get_subtree_contraction(node);
  4451. domain = isl_union_set_preimage_union_pw_multi_aff(domain,
  4452. contraction);
  4453. domain = isl_union_set_preimage_union_pw_multi_aff(domain,
  4454. isl_union_pw_multi_aff_copy(data.tagger));
  4455. flow = isl_union_map_subtract_domain(flow, isl_union_set_copy(domain));
  4456. local_flow = data.inner_band_flow;
  4457. local_flow = isl_union_map_intersect_range(local_flow, domain);
  4458. flow = isl_union_map_subtract(flow, local_flow);
  4459. persist = isl_union_map_domain(flow);
  4460. persist = isl_union_set_apply(persist,
  4461. isl_union_map_copy(prog->scop->tagged_may_writes));
  4462. persist = isl_union_set_union(persist,
  4463. isl_union_set_copy(prog->may_persist));
  4464. return persist;
  4465. }
  4466. /* Add nodes for copying outer arrays in and out of the device
  4467. * before and after the subtree "node", which contains one or more kernels.
  4468. * "domain" contains the original statement instances, i.e.,
  4469. * those that correspond to the domains of the access relations in "prog".
  4470. * In particular, the domain has not been contracted in any way.
  4471. * "prefix" contains the prefix schedule at that point, in terms
  4472. * of the same original statement instances.
  4473. *
  4474. * We first compute the sets of outer array elements that need
  4475. * to be copied in and out and then graft in the nodes for
  4476. * performing this copying.
  4477. *
  4478. * In particular, for each array that is possibly written anywhere in
  4479. * the subtree "node" and that may be used after "node"
  4480. * or that may be visible outside the corresponding scop,
  4481. * we copy out its entire extent.
  4482. *
  4483. * Any array elements that is read without first being written inside
  4484. * the subtree "node" needs to be copied in.
  4485. * Furthermore, if there are any array elements that
  4486. * are copied out, but that may not be written inside "node, then
  4487. * they also need to be copied in to ensure that the value after execution
  4488. * is the same as the value before execution, at least for those array
  4489. * elements that may have their values preserved by the scop or that
  4490. * may be written before "node" and read after "node".
  4491. * In case the array elements are structures, we need to take into
  4492. * account that all members of the structures need to be written
  4493. * by "node" before we can avoid copying the data structure in.
  4494. *
  4495. * Note that the may_write relation is intersected with the domain,
  4496. * which has been intersected with the context.
  4497. * This helps in those cases where the arrays are declared with a fixed size,
  4498. * while the accesses are parametric and the context assigns a fixed value
  4499. * to the parameters.
  4500. *
  4501. * If an element from a local array is read without first being written,
  4502. * then there is no point in copying it in since it cannot have been
  4503. * written prior to the scop. Warn about the uninitialized read instead.
  4504. */
  4505. static __isl_give isl_schedule_node *add_to_from_device(
  4506. __isl_take isl_schedule_node *node, __isl_take isl_union_set *domain,
  4507. __isl_take isl_union_map *prefix, struct gpu_prog *prog)
  4508. {
  4509. isl_union_set *local;
  4510. isl_union_set *may_persist;
  4511. isl_union_map *may_write, *must_write, *copy_out, *not_written;
  4512. isl_union_map *read, *copy_in;
  4513. isl_union_map *tagged;
  4514. isl_union_map *local_uninitialized;
  4515. isl_schedule_node *graft;
  4516. tagged = isl_union_map_copy(prog->scop->tagged_reads);
  4517. tagged = isl_union_map_union(tagged,
  4518. isl_union_map_copy(prog->scop->tagged_may_writes));
  4519. may_write = isl_union_map_copy(prog->may_write);
  4520. may_write = isl_union_map_intersect_domain(may_write,
  4521. isl_union_set_copy(domain));
  4522. may_write = remove_local_accesses(prog,
  4523. isl_union_map_copy(tagged), may_write,
  4524. isl_union_map_copy(prefix), 0);
  4525. may_write = isl_union_map_apply_range(may_write,
  4526. isl_union_map_copy(prog->to_outer));
  4527. may_write = isl_union_map_apply_domain(may_write,
  4528. isl_union_map_copy(prefix));
  4529. may_write = approximate_copy_out(may_write, prog);
  4530. copy_out = isl_union_map_copy(may_write);
  4531. may_write = isl_union_map_apply_range(may_write,
  4532. isl_union_map_copy(prog->to_inner));
  4533. must_write = isl_union_map_copy(prog->must_write);
  4534. must_write = isl_union_map_apply_domain(must_write,
  4535. isl_union_map_copy(prefix));
  4536. may_persist = node_may_persist(node, prog);
  4537. may_write = isl_union_map_intersect_range(may_write, may_persist);
  4538. not_written = isl_union_map_subtract(may_write, must_write);
  4539. local = extract_local_accesses(prog, domain);
  4540. read = isl_union_map_copy(prog->read);
  4541. read = isl_union_map_intersect_domain(read, domain);
  4542. read = remove_local_accesses(prog, tagged, read,
  4543. isl_union_map_copy(prefix), 1);
  4544. local = isl_union_set_apply(local, isl_union_map_copy(prog->to_inner));
  4545. local_uninitialized = isl_union_map_copy(prog->scop->live_in);
  4546. local_uninitialized = isl_union_map_intersect_range(local_uninitialized,
  4547. local);
  4548. local_uninitialized = isl_union_map_intersect(local_uninitialized,
  4549. isl_union_map_copy(read));
  4550. if (!isl_union_map_is_empty(local_uninitialized)) {
  4551. fprintf(stderr,
  4552. "possibly uninitialized reads (not copied in):\n");
  4553. isl_union_map_dump(local_uninitialized);
  4554. }
  4555. read = isl_union_map_subtract(read, local_uninitialized);
  4556. read = isl_union_map_apply_domain(read, prefix);
  4557. copy_in = isl_union_map_union(read, not_written);
  4558. copy_in = isl_union_map_apply_range(copy_in,
  4559. isl_union_map_copy(prog->to_outer));
  4560. graft = create_copy_device(prog, node, "to_device",
  4561. isl_union_map_range(copy_in));
  4562. node = isl_schedule_node_graft_before(node, graft);
  4563. graft = create_copy_device(prog, node, "from_device",
  4564. isl_union_map_range(copy_out));
  4565. node = isl_schedule_node_graft_after(node, graft);
  4566. return node;
  4567. }
  4568. /* Add nodes for initializing ("init_device") and clearing ("clear_device")
  4569. * the device before and after "node".
  4570. */
  4571. static __isl_give isl_schedule_node *add_init_clear_device(
  4572. __isl_take isl_schedule_node *node)
  4573. {
  4574. isl_ctx *ctx;
  4575. isl_space *space;
  4576. isl_union_set *domain;
  4577. isl_schedule_node *graft;
  4578. ctx = isl_schedule_node_get_ctx(node);
  4579. space = isl_space_set_alloc(ctx, 0, 0);
  4580. space = isl_space_set_tuple_name(space, isl_dim_set, "init_device");
  4581. domain = isl_union_set_from_set(isl_set_universe(space));
  4582. graft = isl_schedule_node_from_domain(domain);
  4583. node = isl_schedule_node_graft_before(node, graft);
  4584. space = isl_space_set_alloc(ctx, 0, 0);
  4585. space = isl_space_set_tuple_name(space, isl_dim_set, "clear_device");
  4586. domain = isl_union_set_from_set(isl_set_universe(space));
  4587. graft = isl_schedule_node_from_domain(domain);
  4588. node = isl_schedule_node_graft_after(node, graft);
  4589. return node;
  4590. }
  4591. /* Update "schedule" for mapping to a GPU device.
  4592. *
  4593. * In particular, insert a context node, create kernels for
  4594. * each outermost tilable band and introduce nodes for copying arrays
  4595. * in and out of the device and for initializing and clearing the device.
  4596. * If the child of the initial root points to a set node,
  4597. * then children of this node that do not contain any tilable bands
  4598. * are separated from the other children and are not mapped to
  4599. * the device.
  4600. *
  4601. * The GPU code is generated in a context where at least one
  4602. * statement instance is executed. The corresponding guard is inserted
  4603. * around the entire schedule.
  4604. */
  4605. __isl_give isl_schedule *map_to_device(struct gpu_gen *gen,
  4606. __isl_take isl_schedule *schedule, int to_from_device)
  4607. {
  4608. isl_schedule_node *node;
  4609. isl_set *context;
  4610. isl_set *guard;
  4611. isl_union_set *domain;
  4612. isl_union_map *prefix;
  4613. isl_union_pw_multi_aff *contraction;
  4614. struct gpu_prog *prog;
  4615. context = isl_set_copy(gen->prog->context);
  4616. context = isl_set_from_params(context);
  4617. schedule = isl_schedule_insert_context(schedule, context);
  4618. prog = gen->prog;
  4619. guard = isl_union_set_params(isl_union_set_copy(prog->scop->domain));
  4620. prog->context = isl_set_intersect(prog->context, isl_set_copy(guard));
  4621. guard = isl_set_from_params(guard);
  4622. node = isl_schedule_get_root(schedule);
  4623. isl_schedule_free(schedule);
  4624. node = isl_schedule_node_child(node, 0);
  4625. node = isl_schedule_node_child(node, 0);
  4626. node = isolate_permutable_subtrees(node, gen->prog);
  4627. domain = isl_schedule_node_get_domain(node);
  4628. contraction = isl_schedule_node_get_subtree_contraction(node);
  4629. domain = isl_union_set_preimage_union_pw_multi_aff(domain,
  4630. isl_union_pw_multi_aff_copy(contraction));
  4631. prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
  4632. prefix = isl_union_map_preimage_domain_union_pw_multi_aff(prefix,
  4633. contraction);
  4634. node = mark_kernels(gen, node);
  4635. if (to_from_device) {
  4636. node = add_to_from_device(node, domain, prefix, gen->prog);
  4637. } else {
  4638. isl_union_set_free(domain);
  4639. isl_union_map_free(prefix);
  4640. }
  4641. node = isl_schedule_node_root(node);
  4642. node = isl_schedule_node_child(node, 0);
  4643. node = isl_schedule_node_child(node, 0);
  4644. node = isl_schedule_node_insert_guard(node, guard);
  4645. node = isl_schedule_node_child(node, 0);
  4646. node = add_init_clear_device(node);
  4647. schedule = isl_schedule_node_get_schedule(node);
  4648. isl_schedule_node_free(node);
  4649. return schedule;
  4650. }
  4651. /* Internal data structure for extract_access.
  4652. * "next_access" points to the end of a linked list that is extended
  4653. * by extract_access.
  4654. * "single_expression" is set if the access expressions belong to
  4655. * an expression statement (i.e., a statement without internal control).
  4656. * "any_to_outer" maps all intermediate arrays to their outer arrays.
  4657. */
  4658. struct ppcg_extract_access_data {
  4659. struct gpu_stmt_access **next_access;
  4660. int single_expression;
  4661. isl_union_map *any_to_outer;
  4662. };
  4663. /* Given a tagged access relation to a single array "tagged", extract it
  4664. * as a map, taking into account that the input may be empty.
  4665. * If the access relation is empty, then it does not contain
  4666. * any space information, so we try to recover it from the index
  4667. * expression.
  4668. * The space of the index expression is of the form I -> A,
  4669. * with I the statement instances and A the array, or [I -> F] -> A,
  4670. * with F the filters corresponding to arguments.
  4671. * We first drop F, if present, obtaining I -> A.
  4672. * Then we construct I -> R, with R the reference tag,
  4673. * combine the two into I -> [R -> A] and uncurry to obtain
  4674. * the final result [I -> R] -> A.
  4675. * Note that the index expression may have a lower dimension
  4676. * than that of the array, but this dimension is not used
  4677. * if the access relation is empty.
  4678. */
  4679. static __isl_give isl_map *extract_single_tagged_access(
  4680. __isl_take isl_union_map *tagged, __isl_keep pet_expr *expr)
  4681. {
  4682. int empty;
  4683. isl_id *id;
  4684. isl_space *space, *space2;
  4685. isl_multi_pw_aff *index;
  4686. empty = isl_union_map_is_empty(tagged);
  4687. if (empty < 0)
  4688. goto error;
  4689. if (!empty)
  4690. return isl_map_from_union_map(tagged);
  4691. isl_union_map_free(tagged);
  4692. index = pet_expr_access_get_index(expr);
  4693. space = isl_multi_pw_aff_get_space(index);
  4694. isl_multi_pw_aff_free(index);
  4695. if (isl_space_domain_is_wrapping(space))
  4696. space = isl_space_domain_factor_domain(space);
  4697. space2 = isl_space_copy(space);
  4698. space2 = isl_space_from_domain(isl_space_domain(space));
  4699. id = pet_expr_access_get_ref_id(expr);
  4700. space2 = isl_space_set_tuple_id(space2, isl_dim_out, id);
  4701. space = isl_space_range_product(space2, space);
  4702. space = isl_space_uncurry(space);
  4703. return isl_map_empty(space);
  4704. error:
  4705. isl_union_map_free(tagged);
  4706. return NULL;
  4707. }
  4708. /* Does the index expression "index" of "expr" represent an access
  4709. * to a single element?
  4710. * That is, is "index" completely specified?
  4711. *
  4712. * If "expr" accesses elements from different spaces (i.e., fields
  4713. * of a structure), then it does not access a single element.
  4714. * Otherwise, if the single space of the access matches the space
  4715. * of "index", then the index expression is completely specified
  4716. * (no pointer to a lower-dimensional slice of the accessed array)
  4717. * and a single element is being accessed.
  4718. */
  4719. static isl_bool complete_index(__isl_keep pet_expr *expr,
  4720. __isl_keep isl_multi_pw_aff *index)
  4721. {
  4722. isl_union_map *read, *write, *all;
  4723. isl_map *map;
  4724. isl_space *space1, *space2;
  4725. isl_bool complete;
  4726. read = pet_expr_access_get_may_read(expr);
  4727. write = pet_expr_access_get_may_write(expr);
  4728. all = isl_union_map_union(read, write);
  4729. if (!all)
  4730. return isl_bool_error;
  4731. if (isl_union_map_n_map(all) != 1) {
  4732. isl_union_map_free(all);
  4733. return isl_bool_false;
  4734. }
  4735. map = isl_map_from_union_map(all);
  4736. space1 = isl_map_get_space(map);
  4737. isl_map_free(map);
  4738. space2 = isl_multi_pw_aff_get_space(index);
  4739. complete = isl_space_tuple_is_equal(space1, isl_dim_out,
  4740. space2, isl_dim_out);
  4741. isl_space_free(space1);
  4742. isl_space_free(space2);
  4743. return complete;
  4744. }
  4745. /* Does "expr" access a single, fixed element (independently of the statement
  4746. * instance)?
  4747. * That is, does it have a completely specified constant index expression?
  4748. *
  4749. * Note that it is not sufficient for the index expression to be
  4750. * piecewise constant. isl_multi_pw_aff_is_cst can therefore not be used.
  4751. */
  4752. static isl_bool accesses_fixed_element(__isl_keep pet_expr *expr)
  4753. {
  4754. int i, n;
  4755. isl_multi_pw_aff *index;
  4756. isl_bool fixed = isl_bool_true;
  4757. index = pet_expr_access_get_index(expr);
  4758. if (index < 0)
  4759. return isl_bool_error;
  4760. n = isl_multi_pw_aff_dim(index, isl_dim_out);
  4761. for (i = 0; i < n; ++i) {
  4762. isl_pw_aff *pa;
  4763. pa = isl_multi_pw_aff_get_pw_aff(index, 0);
  4764. fixed = isl_pw_aff_n_piece(pa) == 1;
  4765. if (fixed)
  4766. fixed = isl_pw_aff_is_cst(pa);
  4767. isl_pw_aff_free(pa);
  4768. if (fixed < 0 || !fixed)
  4769. break;
  4770. }
  4771. if (fixed >= 0 && fixed)
  4772. fixed = complete_index(expr, index);
  4773. isl_multi_pw_aff_free(index);
  4774. return fixed;
  4775. }
  4776. /* Extract a gpu_stmt_access from "expr", append it to the list
  4777. * that ends in *data->next_access and update the end of the list.
  4778. * If the access expression performs a write, then it is considered
  4779. * exact only if it appears in a single expression statement and
  4780. * if its may access relation is equal to its must access relation.
  4781. *
  4782. * The combined set of may accesses may be a union if member accesses
  4783. * are involved, but the entire set is derived from a single reference and
  4784. * therefore from a single index expression. These accesses therefore
  4785. * all map to the same outer array.
  4786. */
  4787. static int extract_access(__isl_keep pet_expr *expr, void *user)
  4788. {
  4789. struct ppcg_extract_access_data *data = user;
  4790. isl_union_map *tagged;
  4791. struct gpu_stmt_access *access;
  4792. isl_ctx *ctx = pet_expr_get_ctx(expr);
  4793. isl_multi_pw_aff *index;
  4794. access = isl_alloc_type(ctx, struct gpu_stmt_access);
  4795. assert(access);
  4796. access->next = NULL;
  4797. access->read = pet_expr_access_is_read(expr);
  4798. access->write = pet_expr_access_is_write(expr);
  4799. tagged = pet_expr_access_get_tagged_may_read(expr);
  4800. tagged = isl_union_map_union(tagged,
  4801. pet_expr_access_get_tagged_may_write(expr));
  4802. tagged = isl_union_map_apply_range(tagged,
  4803. isl_union_map_copy(data->any_to_outer));
  4804. if (!access->write) {
  4805. access->exact_write = 1;
  4806. } else if (!data->single_expression) {
  4807. access->exact_write = 0;
  4808. } else {
  4809. isl_union_map *must, *may;
  4810. may = isl_union_map_copy(tagged);
  4811. may = isl_union_map_domain_factor_domain(may);
  4812. must = pet_expr_access_get_must_write(expr);
  4813. access->exact_write = isl_union_map_is_equal(must, may);
  4814. isl_union_map_free(must);
  4815. isl_union_map_free(may);
  4816. }
  4817. index = pet_expr_access_get_index(expr);
  4818. access->n_index = isl_multi_pw_aff_dim(index, isl_dim_out);
  4819. isl_multi_pw_aff_free(index);
  4820. access->ref_id = pet_expr_access_get_ref_id(expr);
  4821. access->tagged_access = extract_single_tagged_access(tagged, expr);
  4822. access->access = isl_map_copy(access->tagged_access);
  4823. access->access = isl_map_domain_factor_domain(access->access);
  4824. access->fixed_element = accesses_fixed_element(expr);
  4825. *data->next_access = access;
  4826. data->next_access = &(*data->next_access)->next;
  4827. if (!access->access || access->fixed_element < 0)
  4828. return -1;
  4829. return 0;
  4830. }
  4831. /* Construct a linked list of gpu_stmt_access objects,
  4832. * one for each access expression in the statement body.
  4833. * "any_to_outer" maps all intermediate arrays to their outer arrays.
  4834. */
  4835. static int pet_stmt_extract_accesses(struct gpu_stmt *stmt,
  4836. __isl_keep isl_union_map *any_to_outer)
  4837. {
  4838. struct ppcg_extract_access_data data;
  4839. stmt->accesses = NULL;
  4840. data.next_access = &stmt->accesses;
  4841. data.single_expression =
  4842. pet_tree_get_type(stmt->stmt->body) == pet_tree_expr;
  4843. data.any_to_outer = any_to_outer;
  4844. return pet_tree_foreach_access_expr(stmt->stmt->body,
  4845. &extract_access, &data);
  4846. }
  4847. /* Has statement "stmt" been killed from "scop"?
  4848. * That is, is the instance set of "scop" free from any
  4849. * instances of "stmt"?
  4850. */
  4851. static isl_bool is_stmt_killed(struct ppcg_scop *scop, struct pet_stmt *stmt)
  4852. {
  4853. isl_space *space;
  4854. isl_set *left;
  4855. isl_bool empty;
  4856. if (!scop || !stmt)
  4857. return isl_bool_error;
  4858. space = isl_set_get_space(stmt->domain);
  4859. left = isl_union_set_extract_set(scop->domain, space);
  4860. empty = isl_set_plain_is_empty(left);
  4861. isl_set_free(left);
  4862. return empty;
  4863. }
  4864. /* Return an array of gpu_stmt representing the statements in "scop".
  4865. * Do not collect array accesses for statements that have been killed.
  4866. */
  4867. static struct gpu_stmt *extract_stmts(isl_ctx *ctx, struct ppcg_scop *scop,
  4868. __isl_keep isl_union_map *any_to_outer)
  4869. {
  4870. int i;
  4871. struct gpu_stmt *stmts;
  4872. stmts = isl_calloc_array(ctx, struct gpu_stmt, scop->pet->n_stmt);
  4873. if (!stmts)
  4874. return NULL;
  4875. for (i = 0; i < scop->pet->n_stmt; ++i) {
  4876. struct gpu_stmt *s = &stmts[i];
  4877. isl_bool killed;
  4878. s->id = isl_set_get_tuple_id(scop->pet->stmts[i]->domain);
  4879. s->stmt = scop->pet->stmts[i];
  4880. killed = is_stmt_killed(scop, scop->pet->stmts[i]);
  4881. if (killed < 0)
  4882. return free_stmts(stmts, i + 1);
  4883. if (killed)
  4884. continue;
  4885. if (pet_stmt_extract_accesses(s, any_to_outer) < 0)
  4886. return free_stmts(stmts, i + 1);
  4887. }
  4888. return stmts;
  4889. }
  4890. /* Generate CUDA code for "scop" and print it to "p".
  4891. * After generating an AST for the transformed scop as explained below,
  4892. * we call "gen->print" to print the AST in the desired output format
  4893. * to "p".
  4894. *
  4895. * If it turns out that it does not make sense to generate GPU code,
  4896. * then we generate CPU code instead.
  4897. *
  4898. * The declarations of the arrays that are visible outside of the scop
  4899. * are printed outside of the code generated from the schedule,
  4900. * because the generated code may involve a guard around the entire code.
  4901. *
  4902. * We first compute a schedule that respects the dependences
  4903. * of the original program and select the outermost bands
  4904. * of tilable dimensions that have at least one parallel loop.
  4905. * If the --load-schedule is specified, then the loaded schedule
  4906. * is used instead of a computed schedule.
  4907. *
  4908. * Each of these bands B is then tiled according to "tile" sizes, resulting
  4909. * in two nested bands, with a kernel marker on top
  4910. *
  4911. * K
  4912. * |
  4913. * T
  4914. * |
  4915. * P
  4916. *
  4917. * We then split off at most 2 parallel dimensions from the T band and
  4918. * at most 3 parallel dimension from the P band
  4919. *
  4920. * K
  4921. * |
  4922. * T
  4923. * T1
  4924. * |
  4925. * T2
  4926. * |
  4927. * P1
  4928. * |
  4929. * P2
  4930. *
  4931. * A filter is introduced in front of T1 that maps the domain instances
  4932. * to block identifiers. Similarly, a filter is introduced in front of P1
  4933. * that maps the domain instances to thread identifiers.
  4934. *
  4935. * For each iteration of the T2 band and for each array, we compute
  4936. * the array elements accessed by that iteration, construct a rectangular
  4937. * box around it and shift it to the origin. The result is used
  4938. * as shared memory for the array.
  4939. *
  4940. * Copying and synchronization statements are added to this schedule tree.
  4941. * In principle, these are added in front of the P1 band, but some of
  4942. * them may get hoisted up to higher levels.
  4943. *
  4944. * The entire AST is then generated from the single resulting schedule tree.
  4945. * During the generation the subtrees at kernel nodes (K) are saved
  4946. * aside and replaced by kernel calls. The result is printed as host code
  4947. * while the saved subtrees are printed as device code.
  4948. */
  4949. static __isl_give isl_printer *generate(__isl_take isl_printer *p,
  4950. struct gpu_gen *gen, struct ppcg_scop *scop,
  4951. struct ppcg_options *options)
  4952. {
  4953. struct gpu_prog *prog;
  4954. isl_ctx *ctx;
  4955. isl_schedule *schedule;
  4956. int any_permutable;
  4957. if (!scop)
  4958. return isl_printer_free(p);
  4959. ctx = isl_printer_get_ctx(p);
  4960. prog = gpu_prog_alloc(ctx, scop);
  4961. if (!prog)
  4962. return isl_printer_free(p);
  4963. gen->prog = prog;
  4964. schedule = get_schedule(gen);
  4965. any_permutable = has_any_permutable_node(schedule);
  4966. if (any_permutable < 0 || !any_permutable) {
  4967. if (any_permutable < 0)
  4968. p = isl_printer_free(p);
  4969. else
  4970. p = print_cpu(p, scop, options);
  4971. isl_schedule_free(schedule);
  4972. } else {
  4973. const int create_to_from_device = 1;
  4974. schedule = map_to_device(gen, schedule, create_to_from_device);
  4975. gen->tree = generate_code(gen, schedule);
  4976. p = ppcg_set_macro_names(p);
  4977. p = ppcg_print_exposed_declarations(p, prog->scop);
  4978. p = gen->print(p, gen->prog, gen->tree, &gen->types,
  4979. gen->print_user);
  4980. isl_ast_node_free(gen->tree);
  4981. }
  4982. gpu_prog_free(prog);
  4983. return p;
  4984. }
  4985. /* Wrapper around generate for use as a ppcg_transform callback.
  4986. */
  4987. static __isl_give isl_printer *generate_wrap(__isl_take isl_printer *p,
  4988. struct ppcg_scop *scop, void *user)
  4989. {
  4990. struct gpu_gen *gen = user;
  4991. return generate(p, gen, scop, gen->options);
  4992. }
  4993. /* Transform the code in the file called "input" by replacing
  4994. * all scops by corresponding GPU code and write the results to "out".
  4995. */
  4996. int generate_gpu(isl_ctx *ctx, const char *input, FILE *out,
  4997. struct ppcg_options *options,
  4998. __isl_give isl_printer *(*print)(__isl_take isl_printer *p,
  4999. struct gpu_prog *prog, __isl_keep isl_ast_node *tree,
  5000. struct gpu_types *types, void *user), void *user)
  5001. {
  5002. struct gpu_gen gen;
  5003. int r;
  5004. int i;
  5005. gen.ctx = ctx;
  5006. gen.sizes = extract_sizes_from_str(ctx, options->sizes);
  5007. gen.options = options;
  5008. gen.kernel_id = 0;
  5009. gen.print = print;
  5010. gen.print_user = user;
  5011. gen.types.n = 0;
  5012. gen.types.name = NULL;
  5013. if (options->debug->dump_sizes) {
  5014. isl_space *space = isl_space_params_alloc(ctx, 0);
  5015. gen.used_sizes = isl_union_map_empty(space);
  5016. }
  5017. r = ppcg_transform(ctx, input, out, options, &generate_wrap, &gen);
  5018. if (options->debug->dump_sizes) {
  5019. isl_union_map_dump(gen.used_sizes);
  5020. isl_union_map_free(gen.used_sizes);
  5021. }
  5022. isl_union_map_free(gen.sizes);
  5023. for (i = 0; i < gen.types.n; ++i)
  5024. free(gen.types.name[i]);
  5025. free(gen.types.name);
  5026. return r;
  5027. }
  5028. /* Compute the set of inner array elements that may have their values
  5029. * preserved by "prog". In particular, collect the array elements of
  5030. * arrays that are not local to "prog" and remove those elements that
  5031. * are definitely killed or definitely written by "prog".
  5032. */
  5033. __isl_give isl_union_set *compute_may_persist(struct gpu_prog *prog)
  5034. {
  5035. int i;
  5036. isl_union_set *may_persist, *killed;
  5037. isl_union_map *must_kill;
  5038. may_persist = isl_union_set_empty(isl_set_get_space(prog->context));
  5039. for (i = 0; i < prog->n_array; ++i) {
  5040. isl_set *extent;
  5041. if (prog->array[i].local)
  5042. continue;
  5043. extent = isl_set_copy(prog->array[i].extent);
  5044. may_persist = isl_union_set_add_set(may_persist, extent);
  5045. }
  5046. may_persist = isl_union_set_intersect_params(may_persist,
  5047. isl_set_copy(prog->context));
  5048. may_persist = isl_union_set_apply(may_persist,
  5049. isl_union_map_copy(prog->to_inner));
  5050. must_kill = isl_union_map_copy(prog->tagged_must_kill);
  5051. killed = isl_union_map_range(must_kill);
  5052. must_kill = isl_union_map_copy(prog->must_write);
  5053. killed = isl_union_set_union(killed, isl_union_map_range(must_kill));
  5054. may_persist = isl_union_set_subtract(may_persist, killed);
  5055. return may_persist;
  5056. }
  5057. struct gpu_prog *gpu_prog_alloc(isl_ctx *ctx, struct ppcg_scop *scop)
  5058. {
  5059. struct gpu_prog *prog;
  5060. isl_space *space;
  5061. isl_map *id;
  5062. if (!scop)
  5063. return NULL;
  5064. prog = isl_calloc_type(ctx, struct gpu_prog);
  5065. assert(prog);
  5066. prog->ctx = ctx;
  5067. prog->scop = scop;
  5068. prog->context = isl_set_copy(scop->context);
  5069. prog->n_stmts = scop->pet->n_stmt;
  5070. prog->any_to_outer = pet_scop_compute_outer_to_any(scop->pet);
  5071. prog->any_to_outer = isl_union_map_reverse(prog->any_to_outer);
  5072. space = isl_union_map_get_space(prog->any_to_outer);
  5073. space = isl_space_set_from_params(space);
  5074. space = isl_space_add_dims(space, isl_dim_set, 1);
  5075. space = isl_space_map_from_set(space);
  5076. id = isl_map_identity(space);
  5077. prog->any_to_outer = isl_union_map_add_map(prog->any_to_outer, id);
  5078. prog->stmts = extract_stmts(ctx, scop, prog->any_to_outer);
  5079. prog->read = isl_union_map_copy(scop->reads);
  5080. prog->may_write = isl_union_map_copy(scop->may_writes);
  5081. prog->must_write = isl_union_map_copy(scop->must_writes);
  5082. prog->tagged_must_kill = isl_union_map_copy(scop->tagged_must_kills);
  5083. prog->to_inner = pet_scop_compute_outer_to_inner(scop->pet);
  5084. prog->to_outer = isl_union_map_copy(prog->to_inner);
  5085. prog->to_outer = isl_union_map_reverse(prog->to_outer);
  5086. if (!prog->stmts)
  5087. return gpu_prog_free(prog);
  5088. if (collect_array_info(prog) < 0)
  5089. return gpu_prog_free(prog);
  5090. prog->may_persist = compute_may_persist(prog);
  5091. return prog;
  5092. }
  5093. void *gpu_prog_free(struct gpu_prog *prog)
  5094. {
  5095. if (!prog)
  5096. return NULL;
  5097. free_array_info(prog);
  5098. free_stmts(prog->stmts, prog->n_stmts);
  5099. isl_union_map_free(prog->any_to_outer);
  5100. isl_union_map_free(prog->to_outer);
  5101. isl_union_map_free(prog->to_inner);
  5102. isl_union_map_free(prog->read);
  5103. isl_union_map_free(prog->may_write);
  5104. isl_union_map_free(prog->must_write);
  5105. isl_union_map_free(prog->tagged_must_kill);
  5106. isl_union_map_free(prog->array_order);
  5107. isl_union_set_free(prog->may_persist);
  5108. isl_set_free(prog->context);
  5109. free(prog);
  5110. return NULL;
  5111. }