Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ac_nir_to_llvm.c 220KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869
  1. /*
  2. * Copyright © 2016 Bas Nieuwenhuizen
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. */
  23. #include "ac_nir_to_llvm.h"
  24. #include "ac_llvm_build.h"
  25. #include "ac_llvm_util.h"
  26. #include "ac_binary.h"
  27. #include "sid.h"
  28. #include "nir/nir.h"
  29. #include "../vulkan/radv_descriptor_set.h"
  30. #include "util/bitscan.h"
  31. #include <llvm-c/Transforms/Scalar.h>
  32. #include "ac_shader_abi.h"
  33. #include "ac_shader_info.h"
  34. #include "ac_exp_param.h"
  35. enum radeon_llvm_calling_convention {
  36. RADEON_LLVM_AMDGPU_VS = 87,
  37. RADEON_LLVM_AMDGPU_GS = 88,
  38. RADEON_LLVM_AMDGPU_PS = 89,
  39. RADEON_LLVM_AMDGPU_CS = 90,
  40. RADEON_LLVM_AMDGPU_HS = 93,
  41. };
  42. #define CONST_ADDR_SPACE 2
  43. #define LOCAL_ADDR_SPACE 3
  44. #define RADEON_LLVM_MAX_INPUTS (VARYING_SLOT_VAR31 + 1)
  45. #define RADEON_LLVM_MAX_OUTPUTS (VARYING_SLOT_VAR31 + 1)
  46. struct nir_to_llvm_context;
  47. struct ac_nir_context {
  48. struct ac_llvm_context ac;
  49. struct ac_shader_abi *abi;
  50. gl_shader_stage stage;
  51. struct hash_table *defs;
  52. struct hash_table *phis;
  53. struct hash_table *vars;
  54. LLVMValueRef main_function;
  55. LLVMBasicBlockRef continue_block;
  56. LLVMBasicBlockRef break_block;
  57. LLVMValueRef outputs[RADEON_LLVM_MAX_OUTPUTS * 4];
  58. int num_locals;
  59. LLVMValueRef *locals;
  60. struct nir_to_llvm_context *nctx; /* TODO get rid of this */
  61. };
  62. struct nir_to_llvm_context {
  63. struct ac_llvm_context ac;
  64. const struct ac_nir_compiler_options *options;
  65. struct ac_shader_variant_info *shader_info;
  66. struct ac_shader_abi abi;
  67. struct ac_nir_context *nir;
  68. unsigned max_workgroup_size;
  69. LLVMContextRef context;
  70. LLVMModuleRef module;
  71. LLVMBuilderRef builder;
  72. LLVMValueRef main_function;
  73. struct hash_table *defs;
  74. struct hash_table *phis;
  75. LLVMValueRef descriptor_sets[AC_UD_MAX_SETS];
  76. LLVMValueRef ring_offsets;
  77. LLVMValueRef push_constants;
  78. LLVMValueRef view_index;
  79. LLVMValueRef num_work_groups;
  80. LLVMValueRef workgroup_ids;
  81. LLVMValueRef local_invocation_ids;
  82. LLVMValueRef tg_size;
  83. LLVMValueRef vertex_buffers;
  84. LLVMValueRef rel_auto_id;
  85. LLVMValueRef vs_prim_id;
  86. LLVMValueRef ls_out_layout;
  87. LLVMValueRef es2gs_offset;
  88. LLVMValueRef tcs_offchip_layout;
  89. LLVMValueRef tcs_out_offsets;
  90. LLVMValueRef tcs_out_layout;
  91. LLVMValueRef tcs_in_layout;
  92. LLVMValueRef oc_lds;
  93. LLVMValueRef merged_wave_info;
  94. LLVMValueRef tess_factor_offset;
  95. LLVMValueRef tcs_patch_id;
  96. LLVMValueRef tcs_rel_ids;
  97. LLVMValueRef tes_rel_patch_id;
  98. LLVMValueRef tes_patch_id;
  99. LLVMValueRef tes_u;
  100. LLVMValueRef tes_v;
  101. LLVMValueRef gsvs_ring_stride;
  102. LLVMValueRef gsvs_num_entries;
  103. LLVMValueRef gs2vs_offset;
  104. LLVMValueRef gs_wave_id;
  105. LLVMValueRef gs_vtx_offset[6];
  106. LLVMValueRef gs_prim_id, gs_invocation_id;
  107. LLVMValueRef esgs_ring;
  108. LLVMValueRef gsvs_ring;
  109. LLVMValueRef hs_ring_tess_offchip;
  110. LLVMValueRef hs_ring_tess_factor;
  111. LLVMValueRef prim_mask;
  112. LLVMValueRef sample_pos_offset;
  113. LLVMValueRef persp_sample, persp_center, persp_centroid;
  114. LLVMValueRef linear_sample, linear_center, linear_centroid;
  115. LLVMTypeRef i1;
  116. LLVMTypeRef i8;
  117. LLVMTypeRef i16;
  118. LLVMTypeRef i32;
  119. LLVMTypeRef i64;
  120. LLVMTypeRef v2i32;
  121. LLVMTypeRef v3i32;
  122. LLVMTypeRef v4i32;
  123. LLVMTypeRef v8i32;
  124. LLVMTypeRef f64;
  125. LLVMTypeRef f32;
  126. LLVMTypeRef f16;
  127. LLVMTypeRef v2f32;
  128. LLVMTypeRef v4f32;
  129. LLVMTypeRef voidt;
  130. LLVMValueRef i1true;
  131. LLVMValueRef i1false;
  132. LLVMValueRef i32zero;
  133. LLVMValueRef i32one;
  134. LLVMValueRef f32zero;
  135. LLVMValueRef f32one;
  136. LLVMValueRef v4f32empty;
  137. unsigned uniform_md_kind;
  138. LLVMValueRef empty_md;
  139. gl_shader_stage stage;
  140. LLVMValueRef lds;
  141. LLVMValueRef inputs[RADEON_LLVM_MAX_INPUTS * 4];
  142. uint64_t input_mask;
  143. uint64_t output_mask;
  144. uint8_t num_output_clips;
  145. uint8_t num_output_culls;
  146. bool is_gs_copy_shader;
  147. LLVMValueRef gs_next_vertex;
  148. unsigned gs_max_out_vertices;
  149. unsigned tes_primitive_mode;
  150. uint64_t tess_outputs_written;
  151. uint64_t tess_patch_outputs_written;
  152. };
  153. static inline struct nir_to_llvm_context *
  154. nir_to_llvm_context_from_abi(struct ac_shader_abi *abi)
  155. {
  156. struct nir_to_llvm_context *ctx = NULL;
  157. return container_of(abi, ctx, abi);
  158. }
  159. static LLVMValueRef get_sampler_desc(struct ac_nir_context *ctx,
  160. const nir_deref_var *deref,
  161. enum ac_descriptor_type desc_type,
  162. bool image, bool write);
  163. static unsigned radeon_llvm_reg_index_soa(unsigned index, unsigned chan)
  164. {
  165. return (index * 4) + chan;
  166. }
  167. static unsigned shader_io_get_unique_index(gl_varying_slot slot)
  168. {
  169. /* handle patch indices separate */
  170. if (slot == VARYING_SLOT_TESS_LEVEL_OUTER)
  171. return 0;
  172. if (slot == VARYING_SLOT_TESS_LEVEL_INNER)
  173. return 1;
  174. if (slot >= VARYING_SLOT_PATCH0 && slot <= VARYING_SLOT_TESS_MAX)
  175. return 2 + (slot - VARYING_SLOT_PATCH0);
  176. if (slot == VARYING_SLOT_POS)
  177. return 0;
  178. if (slot == VARYING_SLOT_PSIZ)
  179. return 1;
  180. if (slot == VARYING_SLOT_CLIP_DIST0)
  181. return 2;
  182. /* 3 is reserved for clip dist as well */
  183. if (slot >= VARYING_SLOT_VAR0 && slot <= VARYING_SLOT_VAR31)
  184. return 4 + (slot - VARYING_SLOT_VAR0);
  185. unreachable("illegal slot in get unique index\n");
  186. }
  187. static void set_llvm_calling_convention(LLVMValueRef func,
  188. gl_shader_stage stage)
  189. {
  190. enum radeon_llvm_calling_convention calling_conv;
  191. switch (stage) {
  192. case MESA_SHADER_VERTEX:
  193. case MESA_SHADER_TESS_EVAL:
  194. calling_conv = RADEON_LLVM_AMDGPU_VS;
  195. break;
  196. case MESA_SHADER_GEOMETRY:
  197. calling_conv = RADEON_LLVM_AMDGPU_GS;
  198. break;
  199. case MESA_SHADER_TESS_CTRL:
  200. calling_conv = HAVE_LLVM >= 0x0500 ? RADEON_LLVM_AMDGPU_HS : RADEON_LLVM_AMDGPU_VS;
  201. break;
  202. case MESA_SHADER_FRAGMENT:
  203. calling_conv = RADEON_LLVM_AMDGPU_PS;
  204. break;
  205. case MESA_SHADER_COMPUTE:
  206. calling_conv = RADEON_LLVM_AMDGPU_CS;
  207. break;
  208. default:
  209. unreachable("Unhandle shader type");
  210. }
  211. LLVMSetFunctionCallConv(func, calling_conv);
  212. }
  213. #define MAX_ARGS 23
  214. struct arg_info {
  215. LLVMTypeRef types[MAX_ARGS];
  216. LLVMValueRef *assign[MAX_ARGS];
  217. unsigned array_params_mask;
  218. uint8_t count;
  219. uint8_t user_sgpr_count;
  220. uint8_t sgpr_count;
  221. uint8_t num_user_sgprs_used;
  222. uint8_t num_sgprs_used;
  223. uint8_t num_vgprs_used;
  224. };
  225. static inline void
  226. add_argument(struct arg_info *info,
  227. LLVMTypeRef type, LLVMValueRef *param_ptr)
  228. {
  229. assert(info->count < MAX_ARGS);
  230. info->assign[info->count] = param_ptr;
  231. info->types[info->count] = type;
  232. info->count++;
  233. }
  234. static inline void
  235. add_sgpr_argument(struct arg_info *info,
  236. LLVMTypeRef type, LLVMValueRef *param_ptr)
  237. {
  238. add_argument(info, type, param_ptr);
  239. info->num_sgprs_used += ac_get_type_size(type) / 4;
  240. info->sgpr_count++;
  241. }
  242. static inline void
  243. add_user_sgpr_argument(struct arg_info *info,
  244. LLVMTypeRef type,
  245. LLVMValueRef *param_ptr)
  246. {
  247. add_sgpr_argument(info, type, param_ptr);
  248. info->num_user_sgprs_used += ac_get_type_size(type) / 4;
  249. info->user_sgpr_count++;
  250. }
  251. static inline void
  252. add_vgpr_argument(struct arg_info *info,
  253. LLVMTypeRef type,
  254. LLVMValueRef *param_ptr)
  255. {
  256. add_argument(info, type, param_ptr);
  257. info->num_vgprs_used += ac_get_type_size(type) / 4;
  258. }
  259. static inline void
  260. add_user_sgpr_array_argument(struct arg_info *info,
  261. LLVMTypeRef type,
  262. LLVMValueRef *param_ptr)
  263. {
  264. info->array_params_mask |= (1 << info->count);
  265. add_user_sgpr_argument(info, type, param_ptr);
  266. }
  267. static void assign_arguments(LLVMValueRef main_function,
  268. struct arg_info *info)
  269. {
  270. unsigned i;
  271. for (i = 0; i < info->count; i++) {
  272. if (info->assign[i])
  273. *info->assign[i] = LLVMGetParam(main_function, i);
  274. }
  275. }
  276. static LLVMValueRef
  277. create_llvm_function(LLVMContextRef ctx, LLVMModuleRef module,
  278. LLVMBuilderRef builder, LLVMTypeRef *return_types,
  279. unsigned num_return_elems,
  280. struct arg_info *args,
  281. unsigned max_workgroup_size,
  282. bool unsafe_math)
  283. {
  284. LLVMTypeRef main_function_type, ret_type;
  285. LLVMBasicBlockRef main_function_body;
  286. if (num_return_elems)
  287. ret_type = LLVMStructTypeInContext(ctx, return_types,
  288. num_return_elems, true);
  289. else
  290. ret_type = LLVMVoidTypeInContext(ctx);
  291. /* Setup the function */
  292. main_function_type =
  293. LLVMFunctionType(ret_type, args->types, args->count, 0);
  294. LLVMValueRef main_function =
  295. LLVMAddFunction(module, "main", main_function_type);
  296. main_function_body =
  297. LLVMAppendBasicBlockInContext(ctx, main_function, "main_body");
  298. LLVMPositionBuilderAtEnd(builder, main_function_body);
  299. LLVMSetFunctionCallConv(main_function, RADEON_LLVM_AMDGPU_CS);
  300. for (unsigned i = 0; i < args->sgpr_count; ++i) {
  301. if (args->array_params_mask & (1 << i)) {
  302. LLVMValueRef P = LLVMGetParam(main_function, i);
  303. ac_add_function_attr(ctx, main_function, i + 1, AC_FUNC_ATTR_BYVAL);
  304. ac_add_attr_dereferenceable(P, UINT64_MAX);
  305. }
  306. else {
  307. ac_add_function_attr(ctx, main_function, i + 1, AC_FUNC_ATTR_INREG);
  308. }
  309. }
  310. if (max_workgroup_size) {
  311. ac_llvm_add_target_dep_function_attr(main_function,
  312. "amdgpu-max-work-group-size",
  313. max_workgroup_size);
  314. }
  315. if (unsafe_math) {
  316. /* These were copied from some LLVM test. */
  317. LLVMAddTargetDependentFunctionAttr(main_function,
  318. "less-precise-fpmad",
  319. "true");
  320. LLVMAddTargetDependentFunctionAttr(main_function,
  321. "no-infs-fp-math",
  322. "true");
  323. LLVMAddTargetDependentFunctionAttr(main_function,
  324. "no-nans-fp-math",
  325. "true");
  326. LLVMAddTargetDependentFunctionAttr(main_function,
  327. "unsafe-fp-math",
  328. "true");
  329. }
  330. return main_function;
  331. }
  332. static LLVMTypeRef const_array(LLVMTypeRef elem_type, int num_elements)
  333. {
  334. return LLVMPointerType(LLVMArrayType(elem_type, num_elements),
  335. CONST_ADDR_SPACE);
  336. }
  337. static int get_elem_bits(struct ac_llvm_context *ctx, LLVMTypeRef type)
  338. {
  339. if (LLVMGetTypeKind(type) == LLVMVectorTypeKind)
  340. type = LLVMGetElementType(type);
  341. if (LLVMGetTypeKind(type) == LLVMIntegerTypeKind)
  342. return LLVMGetIntTypeWidth(type);
  343. if (type == ctx->f16)
  344. return 16;
  345. if (type == ctx->f32)
  346. return 32;
  347. if (type == ctx->f64)
  348. return 64;
  349. unreachable("Unhandled type kind in get_elem_bits");
  350. }
  351. static LLVMValueRef unpack_param(struct ac_llvm_context *ctx,
  352. LLVMValueRef param, unsigned rshift,
  353. unsigned bitwidth)
  354. {
  355. LLVMValueRef value = param;
  356. if (rshift)
  357. value = LLVMBuildLShr(ctx->builder, value,
  358. LLVMConstInt(ctx->i32, rshift, false), "");
  359. if (rshift + bitwidth < 32) {
  360. unsigned mask = (1 << bitwidth) - 1;
  361. value = LLVMBuildAnd(ctx->builder, value,
  362. LLVMConstInt(ctx->i32, mask, false), "");
  363. }
  364. return value;
  365. }
  366. static LLVMValueRef get_rel_patch_id(struct nir_to_llvm_context *ctx)
  367. {
  368. switch (ctx->stage) {
  369. case MESA_SHADER_TESS_CTRL:
  370. return unpack_param(&ctx->ac, ctx->tcs_rel_ids, 0, 8);
  371. case MESA_SHADER_TESS_EVAL:
  372. return ctx->tes_rel_patch_id;
  373. break;
  374. default:
  375. unreachable("Illegal stage");
  376. }
  377. }
  378. /* Tessellation shaders pass outputs to the next shader using LDS.
  379. *
  380. * LS outputs = TCS inputs
  381. * TCS outputs = TES inputs
  382. *
  383. * The LDS layout is:
  384. * - TCS inputs for patch 0
  385. * - TCS inputs for patch 1
  386. * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
  387. * - ...
  388. * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
  389. * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
  390. * - TCS outputs for patch 1
  391. * - Per-patch TCS outputs for patch 1
  392. * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
  393. * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
  394. * - ...
  395. *
  396. * All three shaders VS(LS), TCS, TES share the same LDS space.
  397. */
  398. static LLVMValueRef
  399. get_tcs_in_patch_stride(struct nir_to_llvm_context *ctx)
  400. {
  401. if (ctx->stage == MESA_SHADER_VERTEX)
  402. return unpack_param(&ctx->ac, ctx->ls_out_layout, 0, 13);
  403. else if (ctx->stage == MESA_SHADER_TESS_CTRL)
  404. return unpack_param(&ctx->ac, ctx->tcs_in_layout, 0, 13);
  405. else {
  406. assert(0);
  407. return NULL;
  408. }
  409. }
  410. static LLVMValueRef
  411. get_tcs_out_patch_stride(struct nir_to_llvm_context *ctx)
  412. {
  413. return unpack_param(&ctx->ac, ctx->tcs_out_layout, 0, 13);
  414. }
  415. static LLVMValueRef
  416. get_tcs_out_patch0_offset(struct nir_to_llvm_context *ctx)
  417. {
  418. return LLVMBuildMul(ctx->builder,
  419. unpack_param(&ctx->ac, ctx->tcs_out_offsets, 0, 16),
  420. LLVMConstInt(ctx->i32, 4, false), "");
  421. }
  422. static LLVMValueRef
  423. get_tcs_out_patch0_patch_data_offset(struct nir_to_llvm_context *ctx)
  424. {
  425. return LLVMBuildMul(ctx->builder,
  426. unpack_param(&ctx->ac, ctx->tcs_out_offsets, 16, 16),
  427. LLVMConstInt(ctx->i32, 4, false), "");
  428. }
  429. static LLVMValueRef
  430. get_tcs_in_current_patch_offset(struct nir_to_llvm_context *ctx)
  431. {
  432. LLVMValueRef patch_stride = get_tcs_in_patch_stride(ctx);
  433. LLVMValueRef rel_patch_id = get_rel_patch_id(ctx);
  434. return LLVMBuildMul(ctx->builder, patch_stride, rel_patch_id, "");
  435. }
  436. static LLVMValueRef
  437. get_tcs_out_current_patch_offset(struct nir_to_llvm_context *ctx)
  438. {
  439. LLVMValueRef patch0_offset = get_tcs_out_patch0_offset(ctx);
  440. LLVMValueRef patch_stride = get_tcs_out_patch_stride(ctx);
  441. LLVMValueRef rel_patch_id = get_rel_patch_id(ctx);
  442. return LLVMBuildAdd(ctx->builder, patch0_offset,
  443. LLVMBuildMul(ctx->builder, patch_stride,
  444. rel_patch_id, ""),
  445. "");
  446. }
  447. static LLVMValueRef
  448. get_tcs_out_current_patch_data_offset(struct nir_to_llvm_context *ctx)
  449. {
  450. LLVMValueRef patch0_patch_data_offset =
  451. get_tcs_out_patch0_patch_data_offset(ctx);
  452. LLVMValueRef patch_stride = get_tcs_out_patch_stride(ctx);
  453. LLVMValueRef rel_patch_id = get_rel_patch_id(ctx);
  454. return LLVMBuildAdd(ctx->builder, patch0_patch_data_offset,
  455. LLVMBuildMul(ctx->builder, patch_stride,
  456. rel_patch_id, ""),
  457. "");
  458. }
  459. static void set_userdata_location(struct ac_userdata_info *ud_info, uint8_t *sgpr_idx, uint8_t num_sgprs)
  460. {
  461. ud_info->sgpr_idx = *sgpr_idx;
  462. ud_info->num_sgprs = num_sgprs;
  463. ud_info->indirect = false;
  464. ud_info->indirect_offset = 0;
  465. *sgpr_idx += num_sgprs;
  466. }
  467. static void set_userdata_location_shader(struct nir_to_llvm_context *ctx,
  468. int idx, uint8_t *sgpr_idx, uint8_t num_sgprs)
  469. {
  470. set_userdata_location(&ctx->shader_info->user_sgprs_locs.shader_data[idx], sgpr_idx, num_sgprs);
  471. }
  472. static void set_userdata_location_indirect(struct ac_userdata_info *ud_info, uint8_t sgpr_idx, uint8_t num_sgprs,
  473. uint32_t indirect_offset)
  474. {
  475. ud_info->sgpr_idx = sgpr_idx;
  476. ud_info->num_sgprs = num_sgprs;
  477. ud_info->indirect = true;
  478. ud_info->indirect_offset = indirect_offset;
  479. }
  480. static void declare_tess_lds(struct nir_to_llvm_context *ctx)
  481. {
  482. unsigned lds_size = ctx->options->chip_class >= CIK ? 65536 : 32768;
  483. ctx->lds = LLVMBuildIntToPtr(ctx->builder, ctx->i32zero,
  484. LLVMPointerType(LLVMArrayType(ctx->i32, lds_size / 4), LOCAL_ADDR_SPACE),
  485. "tess_lds");
  486. }
  487. struct user_sgpr_info {
  488. bool need_ring_offsets;
  489. uint8_t sgpr_count;
  490. bool indirect_all_descriptor_sets;
  491. };
  492. static void allocate_user_sgprs(struct nir_to_llvm_context *ctx,
  493. struct user_sgpr_info *user_sgpr_info)
  494. {
  495. memset(user_sgpr_info, 0, sizeof(struct user_sgpr_info));
  496. /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
  497. if (ctx->stage == MESA_SHADER_GEOMETRY ||
  498. ctx->stage == MESA_SHADER_VERTEX ||
  499. ctx->stage == MESA_SHADER_TESS_CTRL ||
  500. ctx->stage == MESA_SHADER_TESS_EVAL ||
  501. ctx->is_gs_copy_shader)
  502. user_sgpr_info->need_ring_offsets = true;
  503. if (ctx->stage == MESA_SHADER_FRAGMENT &&
  504. ctx->shader_info->info.ps.needs_sample_positions)
  505. user_sgpr_info->need_ring_offsets = true;
  506. /* 2 user sgprs will nearly always be allocated for scratch/rings */
  507. if (ctx->options->supports_spill || user_sgpr_info->need_ring_offsets) {
  508. user_sgpr_info->sgpr_count += 2;
  509. }
  510. switch (ctx->stage) {
  511. case MESA_SHADER_COMPUTE:
  512. user_sgpr_info->sgpr_count += ctx->shader_info->info.cs.grid_components_used;
  513. break;
  514. case MESA_SHADER_FRAGMENT:
  515. user_sgpr_info->sgpr_count += ctx->shader_info->info.ps.needs_sample_positions;
  516. break;
  517. case MESA_SHADER_VERTEX:
  518. if (!ctx->is_gs_copy_shader) {
  519. user_sgpr_info->sgpr_count += ctx->shader_info->info.vs.has_vertex_buffers ? 2 : 0;
  520. if (ctx->shader_info->info.vs.needs_draw_id) {
  521. user_sgpr_info->sgpr_count += 3;
  522. } else {
  523. user_sgpr_info->sgpr_count += 2;
  524. }
  525. }
  526. if (ctx->options->key.vs.as_ls)
  527. user_sgpr_info->sgpr_count++;
  528. break;
  529. case MESA_SHADER_TESS_CTRL:
  530. user_sgpr_info->sgpr_count += 4;
  531. break;
  532. case MESA_SHADER_TESS_EVAL:
  533. user_sgpr_info->sgpr_count += 1;
  534. break;
  535. case MESA_SHADER_GEOMETRY:
  536. user_sgpr_info->sgpr_count += 2;
  537. break;
  538. default:
  539. break;
  540. }
  541. if (ctx->shader_info->info.needs_push_constants)
  542. user_sgpr_info->sgpr_count += 2;
  543. uint32_t remaining_sgprs = 16 - user_sgpr_info->sgpr_count;
  544. if (remaining_sgprs / 2 < util_bitcount(ctx->shader_info->info.desc_set_used_mask)) {
  545. user_sgpr_info->sgpr_count += 2;
  546. user_sgpr_info->indirect_all_descriptor_sets = true;
  547. } else {
  548. user_sgpr_info->sgpr_count += util_bitcount(ctx->shader_info->info.desc_set_used_mask) * 2;
  549. }
  550. }
  551. static void
  552. radv_define_common_user_sgprs_phase1(struct nir_to_llvm_context *ctx,
  553. gl_shader_stage stage,
  554. bool has_previous_stage,
  555. gl_shader_stage previous_stage,
  556. const struct user_sgpr_info *user_sgpr_info,
  557. struct arg_info *args,
  558. LLVMValueRef *desc_sets)
  559. {
  560. unsigned num_sets = ctx->options->layout ? ctx->options->layout->num_sets : 0;
  561. unsigned stage_mask = 1 << stage;
  562. if (has_previous_stage)
  563. stage_mask |= 1 << previous_stage;
  564. /* 1 for each descriptor set */
  565. if (!user_sgpr_info->indirect_all_descriptor_sets) {
  566. for (unsigned i = 0; i < num_sets; ++i) {
  567. if (ctx->options->layout->set[i].layout->shader_stages & stage_mask) {
  568. add_user_sgpr_array_argument(args, const_array(ctx->i8, 1024 * 1024), &ctx->descriptor_sets[i]);
  569. }
  570. }
  571. } else
  572. add_user_sgpr_array_argument(args, const_array(const_array(ctx->i8, 1024 * 1024), 32), desc_sets);
  573. if (ctx->shader_info->info.needs_push_constants) {
  574. /* 1 for push constants and dynamic descriptors */
  575. add_user_sgpr_array_argument(args, const_array(ctx->i8, 1024 * 1024), &ctx->push_constants);
  576. }
  577. }
  578. static void
  579. radv_define_common_user_sgprs_phase2(struct nir_to_llvm_context *ctx,
  580. gl_shader_stage stage,
  581. bool has_previous_stage,
  582. gl_shader_stage previous_stage,
  583. const struct user_sgpr_info *user_sgpr_info,
  584. LLVMValueRef desc_sets,
  585. uint8_t *user_sgpr_idx)
  586. {
  587. unsigned num_sets = ctx->options->layout ? ctx->options->layout->num_sets : 0;
  588. unsigned stage_mask = 1 << stage;
  589. if (has_previous_stage)
  590. stage_mask |= 1 << previous_stage;
  591. if (!user_sgpr_info->indirect_all_descriptor_sets) {
  592. for (unsigned i = 0; i < num_sets; ++i) {
  593. if (ctx->options->layout->set[i].layout->shader_stages & stage_mask) {
  594. set_userdata_location(&ctx->shader_info->user_sgprs_locs.descriptor_sets[i], user_sgpr_idx, 2);
  595. } else
  596. ctx->descriptor_sets[i] = NULL;
  597. }
  598. } else {
  599. uint32_t desc_sgpr_idx = *user_sgpr_idx;
  600. set_userdata_location_shader(ctx, AC_UD_INDIRECT_DESCRIPTOR_SETS, user_sgpr_idx, 2);
  601. for (unsigned i = 0; i < num_sets; ++i) {
  602. if (ctx->options->layout->set[i].layout->shader_stages & stage_mask) {
  603. set_userdata_location_indirect(&ctx->shader_info->user_sgprs_locs.descriptor_sets[i], desc_sgpr_idx, 2, i * 8);
  604. ctx->descriptor_sets[i] = ac_build_load_to_sgpr(&ctx->ac, desc_sets, LLVMConstInt(ctx->i32, i, false));
  605. } else
  606. ctx->descriptor_sets[i] = NULL;
  607. }
  608. ctx->shader_info->need_indirect_descriptor_sets = true;
  609. }
  610. if (ctx->shader_info->info.needs_push_constants) {
  611. set_userdata_location_shader(ctx, AC_UD_PUSH_CONSTANTS, user_sgpr_idx, 2);
  612. }
  613. }
  614. static void
  615. radv_define_vs_user_sgprs_phase1(struct nir_to_llvm_context *ctx,
  616. gl_shader_stage stage,
  617. bool has_previous_stage,
  618. gl_shader_stage previous_stage,
  619. struct arg_info *args)
  620. {
  621. if (!ctx->is_gs_copy_shader && (stage == MESA_SHADER_VERTEX || (has_previous_stage && previous_stage == MESA_SHADER_VERTEX))) {
  622. if (ctx->shader_info->info.vs.has_vertex_buffers)
  623. add_user_sgpr_argument(args, const_array(ctx->v4i32, 16), &ctx->vertex_buffers); /* vertex buffers */
  624. add_user_sgpr_argument(args, ctx->i32, &ctx->abi.base_vertex); // base vertex
  625. add_user_sgpr_argument(args, ctx->i32, &ctx->abi.start_instance);// start instance
  626. if (ctx->shader_info->info.vs.needs_draw_id)
  627. add_user_sgpr_argument(args, ctx->i32, &ctx->abi.draw_id); // draw id
  628. }
  629. }
  630. static void
  631. radv_define_vs_user_sgprs_phase2(struct nir_to_llvm_context *ctx,
  632. gl_shader_stage stage,
  633. bool has_previous_stage,
  634. gl_shader_stage previous_stage,
  635. uint8_t *user_sgpr_idx)
  636. {
  637. if (!ctx->is_gs_copy_shader && (stage == MESA_SHADER_VERTEX || (has_previous_stage && previous_stage == MESA_SHADER_VERTEX))) {
  638. if (ctx->shader_info->info.vs.has_vertex_buffers) {
  639. set_userdata_location_shader(ctx, AC_UD_VS_VERTEX_BUFFERS, user_sgpr_idx, 2);
  640. }
  641. unsigned vs_num = 2;
  642. if (ctx->shader_info->info.vs.needs_draw_id)
  643. vs_num++;
  644. set_userdata_location_shader(ctx, AC_UD_VS_BASE_VERTEX_START_INSTANCE, user_sgpr_idx, vs_num);
  645. }
  646. }
  647. static void create_function(struct nir_to_llvm_context *ctx,
  648. gl_shader_stage stage,
  649. bool has_previous_stage,
  650. gl_shader_stage previous_stage)
  651. {
  652. uint8_t user_sgpr_idx;
  653. struct user_sgpr_info user_sgpr_info;
  654. struct arg_info args = {};
  655. LLVMValueRef desc_sets;
  656. allocate_user_sgprs(ctx, &user_sgpr_info);
  657. if (user_sgpr_info.need_ring_offsets && !ctx->options->supports_spill) {
  658. add_user_sgpr_argument(&args, const_array(ctx->v4i32, 16), &ctx->ring_offsets); /* address of rings */
  659. }
  660. switch (stage) {
  661. case MESA_SHADER_COMPUTE:
  662. radv_define_common_user_sgprs_phase1(ctx, stage, has_previous_stage, previous_stage, &user_sgpr_info, &args, &desc_sets);
  663. if (ctx->shader_info->info.cs.grid_components_used)
  664. add_user_sgpr_argument(&args, LLVMVectorType(ctx->i32, ctx->shader_info->info.cs.grid_components_used), &ctx->num_work_groups); /* grid size */
  665. add_sgpr_argument(&args, LLVMVectorType(ctx->i32, 3), &ctx->workgroup_ids);
  666. add_sgpr_argument(&args, ctx->i32, &ctx->tg_size);
  667. add_vgpr_argument(&args, LLVMVectorType(ctx->i32, 3), &ctx->local_invocation_ids);
  668. break;
  669. case MESA_SHADER_VERTEX:
  670. radv_define_common_user_sgprs_phase1(ctx, stage, has_previous_stage, previous_stage, &user_sgpr_info, &args, &desc_sets);
  671. radv_define_vs_user_sgprs_phase1(ctx, stage, has_previous_stage, previous_stage, &args);
  672. if (ctx->shader_info->info.needs_multiview_view_index || (!ctx->options->key.vs.as_es && !ctx->options->key.vs.as_ls && ctx->options->key.has_multiview_view_index))
  673. add_user_sgpr_argument(&args, ctx->i32, &ctx->view_index);
  674. if (ctx->options->key.vs.as_es)
  675. add_sgpr_argument(&args, ctx->i32, &ctx->es2gs_offset); // es2gs offset
  676. else if (ctx->options->key.vs.as_ls)
  677. add_user_sgpr_argument(&args, ctx->i32, &ctx->ls_out_layout); // ls out layout
  678. add_vgpr_argument(&args, ctx->i32, &ctx->abi.vertex_id); // vertex id
  679. if (!ctx->is_gs_copy_shader) {
  680. add_vgpr_argument(&args, ctx->i32, &ctx->rel_auto_id); // rel auto id
  681. add_vgpr_argument(&args, ctx->i32, &ctx->vs_prim_id); // vs prim id
  682. add_vgpr_argument(&args, ctx->i32, &ctx->abi.instance_id); // instance id
  683. }
  684. break;
  685. case MESA_SHADER_TESS_CTRL:
  686. if (has_previous_stage) {
  687. // First 6 system regs
  688. add_sgpr_argument(&args, ctx->i32, &ctx->oc_lds); // param oc lds
  689. add_sgpr_argument(&args, ctx->i32, &ctx->merged_wave_info); // merged wave info
  690. add_sgpr_argument(&args, ctx->i32, &ctx->tess_factor_offset); // tess factor offset
  691. add_sgpr_argument(&args, ctx->i32, NULL); // scratch offset
  692. add_sgpr_argument(&args, ctx->i32, NULL); // unknown
  693. add_sgpr_argument(&args, ctx->i32, NULL); // unknown
  694. radv_define_common_user_sgprs_phase1(ctx, stage, has_previous_stage, previous_stage, &user_sgpr_info, &args, &desc_sets);
  695. radv_define_vs_user_sgprs_phase1(ctx, stage, has_previous_stage, previous_stage, &args);
  696. add_user_sgpr_argument(&args, ctx->i32, &ctx->ls_out_layout); // ls out layout
  697. add_user_sgpr_argument(&args, ctx->i32, &ctx->tcs_offchip_layout); // tcs offchip layout
  698. add_user_sgpr_argument(&args, ctx->i32, &ctx->tcs_out_offsets); // tcs out offsets
  699. add_user_sgpr_argument(&args, ctx->i32, &ctx->tcs_out_layout); // tcs out layout
  700. add_user_sgpr_argument(&args, ctx->i32, &ctx->tcs_in_layout); // tcs in layout
  701. if (ctx->shader_info->info.needs_multiview_view_index)
  702. add_user_sgpr_argument(&args, ctx->i32, &ctx->view_index);
  703. add_vgpr_argument(&args, ctx->i32, &ctx->tcs_patch_id); // patch id
  704. add_vgpr_argument(&args, ctx->i32, &ctx->tcs_rel_ids); // rel ids;
  705. add_vgpr_argument(&args, ctx->i32, &ctx->abi.vertex_id); // vertex id
  706. add_vgpr_argument(&args, ctx->i32, &ctx->rel_auto_id); // rel auto id
  707. add_vgpr_argument(&args, ctx->i32, &ctx->vs_prim_id); // vs prim id
  708. add_vgpr_argument(&args, ctx->i32, &ctx->abi.instance_id); // instance id
  709. } else {
  710. radv_define_common_user_sgprs_phase1(ctx, stage, has_previous_stage, previous_stage, &user_sgpr_info, &args, &desc_sets);
  711. add_user_sgpr_argument(&args, ctx->i32, &ctx->tcs_offchip_layout); // tcs offchip layout
  712. add_user_sgpr_argument(&args, ctx->i32, &ctx->tcs_out_offsets); // tcs out offsets
  713. add_user_sgpr_argument(&args, ctx->i32, &ctx->tcs_out_layout); // tcs out layout
  714. add_user_sgpr_argument(&args, ctx->i32, &ctx->tcs_in_layout); // tcs in layout
  715. if (ctx->shader_info->info.needs_multiview_view_index)
  716. add_user_sgpr_argument(&args, ctx->i32, &ctx->view_index);
  717. add_sgpr_argument(&args, ctx->i32, &ctx->oc_lds); // param oc lds
  718. add_sgpr_argument(&args, ctx->i32, &ctx->tess_factor_offset); // tess factor offset
  719. add_vgpr_argument(&args, ctx->i32, &ctx->tcs_patch_id); // patch id
  720. add_vgpr_argument(&args, ctx->i32, &ctx->tcs_rel_ids); // rel ids;
  721. }
  722. break;
  723. case MESA_SHADER_TESS_EVAL:
  724. radv_define_common_user_sgprs_phase1(ctx, stage, has_previous_stage, previous_stage, &user_sgpr_info, &args, &desc_sets);
  725. add_user_sgpr_argument(&args, ctx->i32, &ctx->tcs_offchip_layout); // tcs offchip layout
  726. if (ctx->shader_info->info.needs_multiview_view_index || (!ctx->options->key.tes.as_es && ctx->options->key.has_multiview_view_index))
  727. add_user_sgpr_argument(&args, ctx->i32, &ctx->view_index);
  728. if (ctx->options->key.tes.as_es) {
  729. add_sgpr_argument(&args, ctx->i32, &ctx->oc_lds); // OC LDS
  730. add_sgpr_argument(&args, ctx->i32, NULL); //
  731. add_sgpr_argument(&args, ctx->i32, &ctx->es2gs_offset); // es2gs offset
  732. } else {
  733. add_sgpr_argument(&args, ctx->i32, NULL); //
  734. add_sgpr_argument(&args, ctx->i32, &ctx->oc_lds); // OC LDS
  735. }
  736. add_vgpr_argument(&args, ctx->f32, &ctx->tes_u); // tes_u
  737. add_vgpr_argument(&args, ctx->f32, &ctx->tes_v); // tes_v
  738. add_vgpr_argument(&args, ctx->i32, &ctx->tes_rel_patch_id); // tes rel patch id
  739. add_vgpr_argument(&args, ctx->i32, &ctx->tes_patch_id); // tes patch id
  740. break;
  741. case MESA_SHADER_GEOMETRY:
  742. radv_define_common_user_sgprs_phase1(ctx, stage, has_previous_stage, previous_stage, &user_sgpr_info, &args, &desc_sets);
  743. radv_define_vs_user_sgprs_phase1(ctx, stage, has_previous_stage, previous_stage, &args);
  744. add_user_sgpr_argument(&args, ctx->i32, &ctx->gsvs_ring_stride); // gsvs stride
  745. add_user_sgpr_argument(&args, ctx->i32, &ctx->gsvs_num_entries); // gsvs num entires
  746. if (ctx->shader_info->info.needs_multiview_view_index)
  747. add_user_sgpr_argument(&args, ctx->i32, &ctx->view_index);
  748. add_sgpr_argument(&args, ctx->i32, &ctx->gs2vs_offset); // gs2vs offset
  749. add_sgpr_argument(&args, ctx->i32, &ctx->gs_wave_id); // wave id
  750. add_vgpr_argument(&args, ctx->i32, &ctx->gs_vtx_offset[0]); // vtx0
  751. add_vgpr_argument(&args, ctx->i32, &ctx->gs_vtx_offset[1]); // vtx1
  752. add_vgpr_argument(&args, ctx->i32, &ctx->gs_prim_id); // prim id
  753. add_vgpr_argument(&args, ctx->i32, &ctx->gs_vtx_offset[2]);
  754. add_vgpr_argument(&args, ctx->i32, &ctx->gs_vtx_offset[3]);
  755. add_vgpr_argument(&args, ctx->i32, &ctx->gs_vtx_offset[4]);
  756. add_vgpr_argument(&args, ctx->i32, &ctx->gs_vtx_offset[5]);
  757. add_vgpr_argument(&args, ctx->i32, &ctx->gs_invocation_id);
  758. break;
  759. case MESA_SHADER_FRAGMENT:
  760. radv_define_common_user_sgprs_phase1(ctx, stage, has_previous_stage, previous_stage, &user_sgpr_info, &args, &desc_sets);
  761. if (ctx->shader_info->info.ps.needs_sample_positions)
  762. add_user_sgpr_argument(&args, ctx->i32, &ctx->sample_pos_offset); /* sample position offset */
  763. add_sgpr_argument(&args, ctx->i32, &ctx->prim_mask); /* prim mask */
  764. add_vgpr_argument(&args, ctx->v2i32, &ctx->persp_sample); /* persp sample */
  765. add_vgpr_argument(&args, ctx->v2i32, &ctx->persp_center); /* persp center */
  766. add_vgpr_argument(&args, ctx->v2i32, &ctx->persp_centroid); /* persp centroid */
  767. add_vgpr_argument(&args, ctx->v3i32, NULL); /* persp pull model */
  768. add_vgpr_argument(&args, ctx->v2i32, &ctx->linear_sample); /* linear sample */
  769. add_vgpr_argument(&args, ctx->v2i32, &ctx->linear_center); /* linear center */
  770. add_vgpr_argument(&args, ctx->v2i32, &ctx->linear_centroid); /* linear centroid */
  771. add_vgpr_argument(&args, ctx->f32, NULL); /* line stipple tex */
  772. add_vgpr_argument(&args, ctx->f32, &ctx->abi.frag_pos[0]); /* pos x float */
  773. add_vgpr_argument(&args, ctx->f32, &ctx->abi.frag_pos[1]); /* pos y float */
  774. add_vgpr_argument(&args, ctx->f32, &ctx->abi.frag_pos[2]); /* pos z float */
  775. add_vgpr_argument(&args, ctx->f32, &ctx->abi.frag_pos[3]); /* pos w float */
  776. add_vgpr_argument(&args, ctx->i32, &ctx->abi.front_face); /* front face */
  777. add_vgpr_argument(&args, ctx->i32, &ctx->abi.ancillary); /* ancillary */
  778. add_vgpr_argument(&args, ctx->i32, &ctx->abi.sample_coverage); /* sample coverage */
  779. add_vgpr_argument(&args, ctx->i32, NULL); /* fixed pt */
  780. break;
  781. default:
  782. unreachable("Shader stage not implemented");
  783. }
  784. ctx->main_function = create_llvm_function(
  785. ctx->context, ctx->module, ctx->builder, NULL, 0, &args,
  786. ctx->max_workgroup_size,
  787. ctx->options->unsafe_math);
  788. set_llvm_calling_convention(ctx->main_function, stage);
  789. ctx->shader_info->num_input_vgprs = 0;
  790. ctx->shader_info->num_input_sgprs = ctx->options->supports_spill ? 2 : 0;
  791. ctx->shader_info->num_input_sgprs += args.num_sgprs_used;
  792. if (ctx->stage != MESA_SHADER_FRAGMENT)
  793. ctx->shader_info->num_input_vgprs = args.num_vgprs_used;
  794. assign_arguments(ctx->main_function, &args);
  795. user_sgpr_idx = 0;
  796. if (ctx->options->supports_spill || user_sgpr_info.need_ring_offsets) {
  797. set_userdata_location_shader(ctx, AC_UD_SCRATCH_RING_OFFSETS, &user_sgpr_idx, 2);
  798. if (ctx->options->supports_spill) {
  799. ctx->ring_offsets = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.implicit.buffer.ptr",
  800. LLVMPointerType(ctx->i8, CONST_ADDR_SPACE),
  801. NULL, 0, AC_FUNC_ATTR_READNONE);
  802. ctx->ring_offsets = LLVMBuildBitCast(ctx->builder, ctx->ring_offsets,
  803. const_array(ctx->v4i32, 16), "");
  804. }
  805. }
  806. /* For merged shaders the user SGPRs start at 8, with 8 system SGPRs in front (including
  807. * the rw_buffers at s0/s1. With user SGPR0 = s8, lets restart the count from 0 */
  808. if (has_previous_stage)
  809. user_sgpr_idx = 0;
  810. radv_define_common_user_sgprs_phase2(ctx, stage, has_previous_stage, previous_stage, &user_sgpr_info, desc_sets, &user_sgpr_idx);
  811. switch (stage) {
  812. case MESA_SHADER_COMPUTE:
  813. if (ctx->shader_info->info.cs.grid_components_used) {
  814. set_userdata_location_shader(ctx, AC_UD_CS_GRID_SIZE, &user_sgpr_idx, ctx->shader_info->info.cs.grid_components_used);
  815. }
  816. break;
  817. case MESA_SHADER_VERTEX:
  818. radv_define_vs_user_sgprs_phase2(ctx, stage, has_previous_stage, previous_stage, &user_sgpr_idx);
  819. if (ctx->view_index)
  820. set_userdata_location_shader(ctx, AC_UD_VIEW_INDEX, &user_sgpr_idx, 1);
  821. if (ctx->options->key.vs.as_ls) {
  822. set_userdata_location_shader(ctx, AC_UD_VS_LS_TCS_IN_LAYOUT, &user_sgpr_idx, 1);
  823. }
  824. if (ctx->options->key.vs.as_ls)
  825. declare_tess_lds(ctx);
  826. break;
  827. case MESA_SHADER_TESS_CTRL:
  828. radv_define_vs_user_sgprs_phase2(ctx, stage, has_previous_stage, previous_stage, &user_sgpr_idx);
  829. if (has_previous_stage)
  830. set_userdata_location_shader(ctx, AC_UD_VS_LS_TCS_IN_LAYOUT, &user_sgpr_idx, 1);
  831. set_userdata_location_shader(ctx, AC_UD_TCS_OFFCHIP_LAYOUT, &user_sgpr_idx, 4);
  832. if (ctx->view_index)
  833. set_userdata_location_shader(ctx, AC_UD_VIEW_INDEX, &user_sgpr_idx, 1);
  834. declare_tess_lds(ctx);
  835. break;
  836. case MESA_SHADER_TESS_EVAL:
  837. set_userdata_location_shader(ctx, AC_UD_TES_OFFCHIP_LAYOUT, &user_sgpr_idx, 1);
  838. if (ctx->view_index)
  839. set_userdata_location_shader(ctx, AC_UD_VIEW_INDEX, &user_sgpr_idx, 1);
  840. break;
  841. case MESA_SHADER_GEOMETRY:
  842. radv_define_vs_user_sgprs_phase2(ctx, stage, has_previous_stage, previous_stage, &user_sgpr_idx);
  843. set_userdata_location_shader(ctx, AC_UD_GS_VS_RING_STRIDE_ENTRIES, &user_sgpr_idx, 2);
  844. if (ctx->view_index)
  845. set_userdata_location_shader(ctx, AC_UD_VIEW_INDEX, &user_sgpr_idx, 1);
  846. break;
  847. case MESA_SHADER_FRAGMENT:
  848. if (ctx->shader_info->info.ps.needs_sample_positions) {
  849. set_userdata_location_shader(ctx, AC_UD_PS_SAMPLE_POS_OFFSET, &user_sgpr_idx, 1);
  850. }
  851. break;
  852. default:
  853. unreachable("Shader stage not implemented");
  854. }
  855. ctx->shader_info->num_user_sgprs = user_sgpr_idx;
  856. }
  857. static void setup_types(struct nir_to_llvm_context *ctx)
  858. {
  859. LLVMValueRef args[4];
  860. ctx->voidt = LLVMVoidTypeInContext(ctx->context);
  861. ctx->i1 = LLVMIntTypeInContext(ctx->context, 1);
  862. ctx->i8 = LLVMIntTypeInContext(ctx->context, 8);
  863. ctx->i16 = LLVMIntTypeInContext(ctx->context, 16);
  864. ctx->i32 = LLVMIntTypeInContext(ctx->context, 32);
  865. ctx->i64 = LLVMIntTypeInContext(ctx->context, 64);
  866. ctx->v2i32 = LLVMVectorType(ctx->i32, 2);
  867. ctx->v3i32 = LLVMVectorType(ctx->i32, 3);
  868. ctx->v4i32 = LLVMVectorType(ctx->i32, 4);
  869. ctx->v8i32 = LLVMVectorType(ctx->i32, 8);
  870. ctx->f32 = LLVMFloatTypeInContext(ctx->context);
  871. ctx->f16 = LLVMHalfTypeInContext(ctx->context);
  872. ctx->f64 = LLVMDoubleTypeInContext(ctx->context);
  873. ctx->v2f32 = LLVMVectorType(ctx->f32, 2);
  874. ctx->v4f32 = LLVMVectorType(ctx->f32, 4);
  875. ctx->i1false = LLVMConstInt(ctx->i1, 0, false);
  876. ctx->i1true = LLVMConstInt(ctx->i1, 1, false);
  877. ctx->i32zero = LLVMConstInt(ctx->i32, 0, false);
  878. ctx->i32one = LLVMConstInt(ctx->i32, 1, false);
  879. ctx->f32zero = LLVMConstReal(ctx->f32, 0.0);
  880. ctx->f32one = LLVMConstReal(ctx->f32, 1.0);
  881. args[0] = ctx->f32zero;
  882. args[1] = ctx->f32zero;
  883. args[2] = ctx->f32zero;
  884. args[3] = ctx->f32one;
  885. ctx->v4f32empty = LLVMConstVector(args, 4);
  886. ctx->uniform_md_kind =
  887. LLVMGetMDKindIDInContext(ctx->context, "amdgpu.uniform", 14);
  888. ctx->empty_md = LLVMMDNodeInContext(ctx->context, NULL, 0);
  889. args[0] = LLVMConstReal(ctx->f32, 2.5);
  890. }
  891. static int get_llvm_num_components(LLVMValueRef value)
  892. {
  893. LLVMTypeRef type = LLVMTypeOf(value);
  894. unsigned num_components = LLVMGetTypeKind(type) == LLVMVectorTypeKind
  895. ? LLVMGetVectorSize(type)
  896. : 1;
  897. return num_components;
  898. }
  899. static LLVMValueRef llvm_extract_elem(struct ac_llvm_context *ac,
  900. LLVMValueRef value,
  901. int index)
  902. {
  903. int count = get_llvm_num_components(value);
  904. assert(index < count);
  905. if (count == 1)
  906. return value;
  907. return LLVMBuildExtractElement(ac->builder, value,
  908. LLVMConstInt(ac->i32, index, false), "");
  909. }
  910. static LLVMValueRef trim_vector(struct ac_llvm_context *ctx,
  911. LLVMValueRef value, unsigned count)
  912. {
  913. unsigned num_components = get_llvm_num_components(value);
  914. if (count == num_components)
  915. return value;
  916. LLVMValueRef masks[] = {
  917. LLVMConstInt(ctx->i32, 0, false), LLVMConstInt(ctx->i32, 1, false),
  918. LLVMConstInt(ctx->i32, 2, false), LLVMConstInt(ctx->i32, 3, false)};
  919. if (count == 1)
  920. return LLVMBuildExtractElement(ctx->builder, value, masks[0],
  921. "");
  922. LLVMValueRef swizzle = LLVMConstVector(masks, count);
  923. return LLVMBuildShuffleVector(ctx->builder, value, value, swizzle, "");
  924. }
  925. static void
  926. build_store_values_extended(struct ac_llvm_context *ac,
  927. LLVMValueRef *values,
  928. unsigned value_count,
  929. unsigned value_stride,
  930. LLVMValueRef vec)
  931. {
  932. LLVMBuilderRef builder = ac->builder;
  933. unsigned i;
  934. for (i = 0; i < value_count; i++) {
  935. LLVMValueRef ptr = values[i * value_stride];
  936. LLVMValueRef index = LLVMConstInt(ac->i32, i, false);
  937. LLVMValueRef value = LLVMBuildExtractElement(builder, vec, index, "");
  938. LLVMBuildStore(builder, value, ptr);
  939. }
  940. }
  941. static LLVMTypeRef get_def_type(struct ac_nir_context *ctx,
  942. const nir_ssa_def *def)
  943. {
  944. LLVMTypeRef type = LLVMIntTypeInContext(ctx->ac.context, def->bit_size);
  945. if (def->num_components > 1) {
  946. type = LLVMVectorType(type, def->num_components);
  947. }
  948. return type;
  949. }
  950. static LLVMValueRef get_src(struct ac_nir_context *nir, nir_src src)
  951. {
  952. assert(src.is_ssa);
  953. struct hash_entry *entry = _mesa_hash_table_search(nir->defs, src.ssa);
  954. return (LLVMValueRef)entry->data;
  955. }
  956. static LLVMBasicBlockRef get_block(struct ac_nir_context *nir,
  957. const struct nir_block *b)
  958. {
  959. struct hash_entry *entry = _mesa_hash_table_search(nir->defs, b);
  960. return (LLVMBasicBlockRef)entry->data;
  961. }
  962. static LLVMValueRef get_alu_src(struct ac_nir_context *ctx,
  963. nir_alu_src src,
  964. unsigned num_components)
  965. {
  966. LLVMValueRef value = get_src(ctx, src.src);
  967. bool need_swizzle = false;
  968. assert(value);
  969. LLVMTypeRef type = LLVMTypeOf(value);
  970. unsigned src_components = LLVMGetTypeKind(type) == LLVMVectorTypeKind
  971. ? LLVMGetVectorSize(type)
  972. : 1;
  973. for (unsigned i = 0; i < num_components; ++i) {
  974. assert(src.swizzle[i] < src_components);
  975. if (src.swizzle[i] != i)
  976. need_swizzle = true;
  977. }
  978. if (need_swizzle || num_components != src_components) {
  979. LLVMValueRef masks[] = {
  980. LLVMConstInt(ctx->ac.i32, src.swizzle[0], false),
  981. LLVMConstInt(ctx->ac.i32, src.swizzle[1], false),
  982. LLVMConstInt(ctx->ac.i32, src.swizzle[2], false),
  983. LLVMConstInt(ctx->ac.i32, src.swizzle[3], false)};
  984. if (src_components > 1 && num_components == 1) {
  985. value = LLVMBuildExtractElement(ctx->ac.builder, value,
  986. masks[0], "");
  987. } else if (src_components == 1 && num_components > 1) {
  988. LLVMValueRef values[] = {value, value, value, value};
  989. value = ac_build_gather_values(&ctx->ac, values, num_components);
  990. } else {
  991. LLVMValueRef swizzle = LLVMConstVector(masks, num_components);
  992. value = LLVMBuildShuffleVector(ctx->ac.builder, value, value,
  993. swizzle, "");
  994. }
  995. }
  996. assert(!src.negate);
  997. assert(!src.abs);
  998. return value;
  999. }
  1000. static LLVMValueRef emit_int_cmp(struct ac_llvm_context *ctx,
  1001. LLVMIntPredicate pred, LLVMValueRef src0,
  1002. LLVMValueRef src1)
  1003. {
  1004. LLVMValueRef result = LLVMBuildICmp(ctx->builder, pred, src0, src1, "");
  1005. return LLVMBuildSelect(ctx->builder, result,
  1006. LLVMConstInt(ctx->i32, 0xFFFFFFFF, false),
  1007. LLVMConstInt(ctx->i32, 0, false), "");
  1008. }
  1009. static LLVMValueRef emit_float_cmp(struct ac_llvm_context *ctx,
  1010. LLVMRealPredicate pred, LLVMValueRef src0,
  1011. LLVMValueRef src1)
  1012. {
  1013. LLVMValueRef result;
  1014. src0 = ac_to_float(ctx, src0);
  1015. src1 = ac_to_float(ctx, src1);
  1016. result = LLVMBuildFCmp(ctx->builder, pred, src0, src1, "");
  1017. return LLVMBuildSelect(ctx->builder, result,
  1018. LLVMConstInt(ctx->i32, 0xFFFFFFFF, false),
  1019. LLVMConstInt(ctx->i32, 0, false), "");
  1020. }
  1021. static LLVMValueRef emit_intrin_1f_param(struct ac_llvm_context *ctx,
  1022. const char *intrin,
  1023. LLVMTypeRef result_type,
  1024. LLVMValueRef src0)
  1025. {
  1026. char name[64];
  1027. LLVMValueRef params[] = {
  1028. ac_to_float(ctx, src0),
  1029. };
  1030. MAYBE_UNUSED const int length = snprintf(name, sizeof(name), "%s.f%d", intrin,
  1031. get_elem_bits(ctx, result_type));
  1032. assert(length < sizeof(name));
  1033. return ac_build_intrinsic(ctx, name, result_type, params, 1, AC_FUNC_ATTR_READNONE);
  1034. }
  1035. static LLVMValueRef emit_intrin_2f_param(struct ac_llvm_context *ctx,
  1036. const char *intrin,
  1037. LLVMTypeRef result_type,
  1038. LLVMValueRef src0, LLVMValueRef src1)
  1039. {
  1040. char name[64];
  1041. LLVMValueRef params[] = {
  1042. ac_to_float(ctx, src0),
  1043. ac_to_float(ctx, src1),
  1044. };
  1045. MAYBE_UNUSED const int length = snprintf(name, sizeof(name), "%s.f%d", intrin,
  1046. get_elem_bits(ctx, result_type));
  1047. assert(length < sizeof(name));
  1048. return ac_build_intrinsic(ctx, name, result_type, params, 2, AC_FUNC_ATTR_READNONE);
  1049. }
  1050. static LLVMValueRef emit_intrin_3f_param(struct ac_llvm_context *ctx,
  1051. const char *intrin,
  1052. LLVMTypeRef result_type,
  1053. LLVMValueRef src0, LLVMValueRef src1, LLVMValueRef src2)
  1054. {
  1055. char name[64];
  1056. LLVMValueRef params[] = {
  1057. ac_to_float(ctx, src0),
  1058. ac_to_float(ctx, src1),
  1059. ac_to_float(ctx, src2),
  1060. };
  1061. MAYBE_UNUSED const int length = snprintf(name, sizeof(name), "%s.f%d", intrin,
  1062. get_elem_bits(ctx, result_type));
  1063. assert(length < sizeof(name));
  1064. return ac_build_intrinsic(ctx, name, result_type, params, 3, AC_FUNC_ATTR_READNONE);
  1065. }
  1066. static LLVMValueRef emit_bcsel(struct ac_llvm_context *ctx,
  1067. LLVMValueRef src0, LLVMValueRef src1, LLVMValueRef src2)
  1068. {
  1069. LLVMValueRef v = LLVMBuildICmp(ctx->builder, LLVMIntNE, src0,
  1070. ctx->i32_0, "");
  1071. return LLVMBuildSelect(ctx->builder, v, src1, src2, "");
  1072. }
  1073. static LLVMValueRef emit_find_lsb(struct ac_llvm_context *ctx,
  1074. LLVMValueRef src0)
  1075. {
  1076. LLVMValueRef params[2] = {
  1077. src0,
  1078. /* The value of 1 means that ffs(x=0) = undef, so LLVM won't
  1079. * add special code to check for x=0. The reason is that
  1080. * the LLVM behavior for x=0 is different from what we
  1081. * need here.
  1082. *
  1083. * The hardware already implements the correct behavior.
  1084. */
  1085. LLVMConstInt(ctx->i1, 1, false),
  1086. };
  1087. LLVMValueRef lsb = ac_build_intrinsic(ctx, "llvm.cttz.i32", ctx->i32,
  1088. params, 2,
  1089. AC_FUNC_ATTR_READNONE);
  1090. /* TODO: We need an intrinsic to skip this conditional. */
  1091. /* Check for zero: */
  1092. return LLVMBuildSelect(ctx->builder, LLVMBuildICmp(ctx->builder,
  1093. LLVMIntEQ, src0,
  1094. ctx->i32_0, ""),
  1095. LLVMConstInt(ctx->i32, -1, 0), lsb, "");
  1096. }
  1097. static LLVMValueRef emit_ifind_msb(struct ac_llvm_context *ctx,
  1098. LLVMValueRef src0)
  1099. {
  1100. return ac_build_imsb(ctx, src0, ctx->i32);
  1101. }
  1102. static LLVMValueRef emit_ufind_msb(struct ac_llvm_context *ctx,
  1103. LLVMValueRef src0)
  1104. {
  1105. return ac_build_umsb(ctx, src0, ctx->i32);
  1106. }
  1107. static LLVMValueRef emit_minmax_int(struct ac_llvm_context *ctx,
  1108. LLVMIntPredicate pred,
  1109. LLVMValueRef src0, LLVMValueRef src1)
  1110. {
  1111. return LLVMBuildSelect(ctx->builder,
  1112. LLVMBuildICmp(ctx->builder, pred, src0, src1, ""),
  1113. src0,
  1114. src1, "");
  1115. }
  1116. static LLVMValueRef emit_iabs(struct ac_llvm_context *ctx,
  1117. LLVMValueRef src0)
  1118. {
  1119. return emit_minmax_int(ctx, LLVMIntSGT, src0,
  1120. LLVMBuildNeg(ctx->builder, src0, ""));
  1121. }
  1122. static LLVMValueRef emit_fsign(struct ac_llvm_context *ctx,
  1123. LLVMValueRef src0)
  1124. {
  1125. LLVMValueRef cmp, val;
  1126. cmp = LLVMBuildFCmp(ctx->builder, LLVMRealOGT, src0, ctx->f32_0, "");
  1127. val = LLVMBuildSelect(ctx->builder, cmp, ctx->f32_1, src0, "");
  1128. cmp = LLVMBuildFCmp(ctx->builder, LLVMRealOGE, val, ctx->f32_0, "");
  1129. val = LLVMBuildSelect(ctx->builder, cmp, val, LLVMConstReal(ctx->f32, -1.0), "");
  1130. return val;
  1131. }
  1132. static LLVMValueRef emit_isign(struct ac_llvm_context *ctx,
  1133. LLVMValueRef src0)
  1134. {
  1135. LLVMValueRef cmp, val;
  1136. cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGT, src0, ctx->i32_0, "");
  1137. val = LLVMBuildSelect(ctx->builder, cmp, ctx->i32_1, src0, "");
  1138. cmp = LLVMBuildICmp(ctx->builder, LLVMIntSGE, val, ctx->i32_0, "");
  1139. val = LLVMBuildSelect(ctx->builder, cmp, val, LLVMConstInt(ctx->i32, -1, true), "");
  1140. return val;
  1141. }
  1142. static LLVMValueRef emit_ffract(struct ac_llvm_context *ctx,
  1143. LLVMValueRef src0)
  1144. {
  1145. const char *intr = "llvm.floor.f32";
  1146. LLVMValueRef fsrc0 = ac_to_float(ctx, src0);
  1147. LLVMValueRef params[] = {
  1148. fsrc0,
  1149. };
  1150. LLVMValueRef floor = ac_build_intrinsic(ctx, intr,
  1151. ctx->f32, params, 1,
  1152. AC_FUNC_ATTR_READNONE);
  1153. return LLVMBuildFSub(ctx->builder, fsrc0, floor, "");
  1154. }
  1155. static LLVMValueRef emit_uint_carry(struct ac_llvm_context *ctx,
  1156. const char *intrin,
  1157. LLVMValueRef src0, LLVMValueRef src1)
  1158. {
  1159. LLVMTypeRef ret_type;
  1160. LLVMTypeRef types[] = { ctx->i32, ctx->i1 };
  1161. LLVMValueRef res;
  1162. LLVMValueRef params[] = { src0, src1 };
  1163. ret_type = LLVMStructTypeInContext(ctx->context, types,
  1164. 2, true);
  1165. res = ac_build_intrinsic(ctx, intrin, ret_type,
  1166. params, 2, AC_FUNC_ATTR_READNONE);
  1167. res = LLVMBuildExtractValue(ctx->builder, res, 1, "");
  1168. res = LLVMBuildZExt(ctx->builder, res, ctx->i32, "");
  1169. return res;
  1170. }
  1171. static LLVMValueRef emit_b2f(struct ac_llvm_context *ctx,
  1172. LLVMValueRef src0)
  1173. {
  1174. return LLVMBuildAnd(ctx->builder, src0, LLVMBuildBitCast(ctx->builder, LLVMConstReal(ctx->f32, 1.0), ctx->i32, ""), "");
  1175. }
  1176. static LLVMValueRef emit_f2b(struct ac_llvm_context *ctx,
  1177. LLVMValueRef src0)
  1178. {
  1179. src0 = ac_to_float(ctx, src0);
  1180. return LLVMBuildSExt(ctx->builder,
  1181. LLVMBuildFCmp(ctx->builder, LLVMRealUNE, src0, ctx->f32_0, ""),
  1182. ctx->i32, "");
  1183. }
  1184. static LLVMValueRef emit_b2i(struct ac_llvm_context *ctx,
  1185. LLVMValueRef src0)
  1186. {
  1187. return LLVMBuildAnd(ctx->builder, src0, ctx->i32_1, "");
  1188. }
  1189. static LLVMValueRef emit_i2b(struct ac_llvm_context *ctx,
  1190. LLVMValueRef src0)
  1191. {
  1192. return LLVMBuildSExt(ctx->builder,
  1193. LLVMBuildICmp(ctx->builder, LLVMIntNE, src0, ctx->i32_0, ""),
  1194. ctx->i32, "");
  1195. }
  1196. static LLVMValueRef emit_f2f16(struct nir_to_llvm_context *ctx,
  1197. LLVMValueRef src0)
  1198. {
  1199. LLVMValueRef result;
  1200. LLVMValueRef cond = NULL;
  1201. src0 = ac_to_float(&ctx->ac, src0);
  1202. result = LLVMBuildFPTrunc(ctx->builder, src0, ctx->f16, "");
  1203. if (ctx->options->chip_class >= VI) {
  1204. LLVMValueRef args[2];
  1205. /* Check if the result is a denormal - and flush to 0 if so. */
  1206. args[0] = result;
  1207. args[1] = LLVMConstInt(ctx->i32, N_SUBNORMAL | P_SUBNORMAL, false);
  1208. cond = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.class.f16", ctx->i1, args, 2, AC_FUNC_ATTR_READNONE);
  1209. }
  1210. /* need to convert back up to f32 */
  1211. result = LLVMBuildFPExt(ctx->builder, result, ctx->f32, "");
  1212. if (ctx->options->chip_class >= VI)
  1213. result = LLVMBuildSelect(ctx->builder, cond, ctx->f32zero, result, "");
  1214. else {
  1215. /* for SI/CIK */
  1216. /* 0x38800000 is smallest half float value (2^-14) in 32-bit float,
  1217. * so compare the result and flush to 0 if it's smaller.
  1218. */
  1219. LLVMValueRef temp, cond2;
  1220. temp = emit_intrin_1f_param(&ctx->ac, "llvm.fabs",
  1221. ctx->f32, result);
  1222. cond = LLVMBuildFCmp(ctx->builder, LLVMRealUGT,
  1223. LLVMBuildBitCast(ctx->builder, LLVMConstInt(ctx->i32, 0x38800000, false), ctx->f32, ""),
  1224. temp, "");
  1225. cond2 = LLVMBuildFCmp(ctx->builder, LLVMRealUNE,
  1226. temp, ctx->f32zero, "");
  1227. cond = LLVMBuildAnd(ctx->builder, cond, cond2, "");
  1228. result = LLVMBuildSelect(ctx->builder, cond, ctx->f32zero, result, "");
  1229. }
  1230. return result;
  1231. }
  1232. static LLVMValueRef emit_umul_high(struct ac_llvm_context *ctx,
  1233. LLVMValueRef src0, LLVMValueRef src1)
  1234. {
  1235. LLVMValueRef dst64, result;
  1236. src0 = LLVMBuildZExt(ctx->builder, src0, ctx->i64, "");
  1237. src1 = LLVMBuildZExt(ctx->builder, src1, ctx->i64, "");
  1238. dst64 = LLVMBuildMul(ctx->builder, src0, src1, "");
  1239. dst64 = LLVMBuildLShr(ctx->builder, dst64, LLVMConstInt(ctx->i64, 32, false), "");
  1240. result = LLVMBuildTrunc(ctx->builder, dst64, ctx->i32, "");
  1241. return result;
  1242. }
  1243. static LLVMValueRef emit_imul_high(struct ac_llvm_context *ctx,
  1244. LLVMValueRef src0, LLVMValueRef src1)
  1245. {
  1246. LLVMValueRef dst64, result;
  1247. src0 = LLVMBuildSExt(ctx->builder, src0, ctx->i64, "");
  1248. src1 = LLVMBuildSExt(ctx->builder, src1, ctx->i64, "");
  1249. dst64 = LLVMBuildMul(ctx->builder, src0, src1, "");
  1250. dst64 = LLVMBuildAShr(ctx->builder, dst64, LLVMConstInt(ctx->i64, 32, false), "");
  1251. result = LLVMBuildTrunc(ctx->builder, dst64, ctx->i32, "");
  1252. return result;
  1253. }
  1254. static LLVMValueRef emit_bitfield_extract(struct ac_llvm_context *ctx,
  1255. bool is_signed,
  1256. const LLVMValueRef srcs[3])
  1257. {
  1258. LLVMValueRef result;
  1259. LLVMValueRef icond = LLVMBuildICmp(ctx->builder, LLVMIntEQ, srcs[2], LLVMConstInt(ctx->i32, 32, false), "");
  1260. result = ac_build_bfe(ctx, srcs[0], srcs[1], srcs[2], is_signed);
  1261. result = LLVMBuildSelect(ctx->builder, icond, srcs[0], result, "");
  1262. return result;
  1263. }
  1264. static LLVMValueRef emit_bitfield_insert(struct ac_llvm_context *ctx,
  1265. LLVMValueRef src0, LLVMValueRef src1,
  1266. LLVMValueRef src2, LLVMValueRef src3)
  1267. {
  1268. LLVMValueRef bfi_args[3], result;
  1269. bfi_args[0] = LLVMBuildShl(ctx->builder,
  1270. LLVMBuildSub(ctx->builder,
  1271. LLVMBuildShl(ctx->builder,
  1272. ctx->i32_1,
  1273. src3, ""),
  1274. ctx->i32_1, ""),
  1275. src2, "");
  1276. bfi_args[1] = LLVMBuildShl(ctx->builder, src1, src2, "");
  1277. bfi_args[2] = src0;
  1278. LLVMValueRef icond = LLVMBuildICmp(ctx->builder, LLVMIntEQ, src3, LLVMConstInt(ctx->i32, 32, false), "");
  1279. /* Calculate:
  1280. * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
  1281. * Use the right-hand side, which the LLVM backend can convert to V_BFI.
  1282. */
  1283. result = LLVMBuildXor(ctx->builder, bfi_args[2],
  1284. LLVMBuildAnd(ctx->builder, bfi_args[0],
  1285. LLVMBuildXor(ctx->builder, bfi_args[1], bfi_args[2], ""), ""), "");
  1286. result = LLVMBuildSelect(ctx->builder, icond, src1, result, "");
  1287. return result;
  1288. }
  1289. static LLVMValueRef emit_pack_half_2x16(struct ac_llvm_context *ctx,
  1290. LLVMValueRef src0)
  1291. {
  1292. LLVMValueRef const16 = LLVMConstInt(ctx->i32, 16, false);
  1293. int i;
  1294. LLVMValueRef comp[2];
  1295. src0 = ac_to_float(ctx, src0);
  1296. comp[0] = LLVMBuildExtractElement(ctx->builder, src0, ctx->i32_0, "");
  1297. comp[1] = LLVMBuildExtractElement(ctx->builder, src0, ctx->i32_1, "");
  1298. for (i = 0; i < 2; i++) {
  1299. comp[i] = LLVMBuildFPTrunc(ctx->builder, comp[i], ctx->f16, "");
  1300. comp[i] = LLVMBuildBitCast(ctx->builder, comp[i], ctx->i16, "");
  1301. comp[i] = LLVMBuildZExt(ctx->builder, comp[i], ctx->i32, "");
  1302. }
  1303. comp[1] = LLVMBuildShl(ctx->builder, comp[1], const16, "");
  1304. comp[0] = LLVMBuildOr(ctx->builder, comp[0], comp[1], "");
  1305. return comp[0];
  1306. }
  1307. static LLVMValueRef emit_unpack_half_2x16(struct ac_llvm_context *ctx,
  1308. LLVMValueRef src0)
  1309. {
  1310. LLVMValueRef const16 = LLVMConstInt(ctx->i32, 16, false);
  1311. LLVMValueRef temps[2], result, val;
  1312. int i;
  1313. for (i = 0; i < 2; i++) {
  1314. val = i == 1 ? LLVMBuildLShr(ctx->builder, src0, const16, "") : src0;
  1315. val = LLVMBuildTrunc(ctx->builder, val, ctx->i16, "");
  1316. val = LLVMBuildBitCast(ctx->builder, val, ctx->f16, "");
  1317. temps[i] = LLVMBuildFPExt(ctx->builder, val, ctx->f32, "");
  1318. }
  1319. LLVMTypeRef v2f32 = LLVMVectorType(ctx->f32, 2);
  1320. result = LLVMBuildInsertElement(ctx->builder, LLVMGetUndef(v2f32), temps[0],
  1321. ctx->i32_0, "");
  1322. result = LLVMBuildInsertElement(ctx->builder, result, temps[1],
  1323. ctx->i32_1, "");
  1324. return result;
  1325. }
  1326. static LLVMValueRef emit_ddxy(struct ac_nir_context *ctx,
  1327. nir_op op,
  1328. LLVMValueRef src0)
  1329. {
  1330. unsigned mask;
  1331. int idx;
  1332. LLVMValueRef result;
  1333. if (op == nir_op_fddx_fine || op == nir_op_fddx)
  1334. mask = AC_TID_MASK_LEFT;
  1335. else if (op == nir_op_fddy_fine || op == nir_op_fddy)
  1336. mask = AC_TID_MASK_TOP;
  1337. else
  1338. mask = AC_TID_MASK_TOP_LEFT;
  1339. /* for DDX we want to next X pixel, DDY next Y pixel. */
  1340. if (op == nir_op_fddx_fine ||
  1341. op == nir_op_fddx_coarse ||
  1342. op == nir_op_fddx)
  1343. idx = 1;
  1344. else
  1345. idx = 2;
  1346. result = ac_build_ddxy(&ctx->ac, mask, idx, src0);
  1347. return result;
  1348. }
  1349. /*
  1350. * this takes an I,J coordinate pair,
  1351. * and works out the X and Y derivatives.
  1352. * it returns DDX(I), DDX(J), DDY(I), DDY(J).
  1353. */
  1354. static LLVMValueRef emit_ddxy_interp(
  1355. struct ac_nir_context *ctx,
  1356. LLVMValueRef interp_ij)
  1357. {
  1358. LLVMValueRef result[4], a;
  1359. unsigned i;
  1360. for (i = 0; i < 2; i++) {
  1361. a = LLVMBuildExtractElement(ctx->ac.builder, interp_ij,
  1362. LLVMConstInt(ctx->ac.i32, i, false), "");
  1363. result[i] = emit_ddxy(ctx, nir_op_fddx, a);
  1364. result[2+i] = emit_ddxy(ctx, nir_op_fddy, a);
  1365. }
  1366. return ac_build_gather_values(&ctx->ac, result, 4);
  1367. }
  1368. static void visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
  1369. {
  1370. LLVMValueRef src[4], result = NULL;
  1371. unsigned num_components = instr->dest.dest.ssa.num_components;
  1372. unsigned src_components;
  1373. LLVMTypeRef def_type = get_def_type(ctx, &instr->dest.dest.ssa);
  1374. assert(nir_op_infos[instr->op].num_inputs <= ARRAY_SIZE(src));
  1375. switch (instr->op) {
  1376. case nir_op_vec2:
  1377. case nir_op_vec3:
  1378. case nir_op_vec4:
  1379. src_components = 1;
  1380. break;
  1381. case nir_op_pack_half_2x16:
  1382. src_components = 2;
  1383. break;
  1384. case nir_op_unpack_half_2x16:
  1385. src_components = 1;
  1386. break;
  1387. default:
  1388. src_components = num_components;
  1389. break;
  1390. }
  1391. for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
  1392. src[i] = get_alu_src(ctx, instr->src[i], src_components);
  1393. switch (instr->op) {
  1394. case nir_op_fmov:
  1395. case nir_op_imov:
  1396. result = src[0];
  1397. break;
  1398. case nir_op_fneg:
  1399. src[0] = ac_to_float(&ctx->ac, src[0]);
  1400. result = LLVMBuildFNeg(ctx->ac.builder, src[0], "");
  1401. break;
  1402. case nir_op_ineg:
  1403. result = LLVMBuildNeg(ctx->ac.builder, src[0], "");
  1404. break;
  1405. case nir_op_inot:
  1406. result = LLVMBuildNot(ctx->ac.builder, src[0], "");
  1407. break;
  1408. case nir_op_iadd:
  1409. result = LLVMBuildAdd(ctx->ac.builder, src[0], src[1], "");
  1410. break;
  1411. case nir_op_fadd:
  1412. src[0] = ac_to_float(&ctx->ac, src[0]);
  1413. src[1] = ac_to_float(&ctx->ac, src[1]);
  1414. result = LLVMBuildFAdd(ctx->ac.builder, src[0], src[1], "");
  1415. break;
  1416. case nir_op_fsub:
  1417. src[0] = ac_to_float(&ctx->ac, src[0]);
  1418. src[1] = ac_to_float(&ctx->ac, src[1]);
  1419. result = LLVMBuildFSub(ctx->ac.builder, src[0], src[1], "");
  1420. break;
  1421. case nir_op_isub:
  1422. result = LLVMBuildSub(ctx->ac.builder, src[0], src[1], "");
  1423. break;
  1424. case nir_op_imul:
  1425. result = LLVMBuildMul(ctx->ac.builder, src[0], src[1], "");
  1426. break;
  1427. case nir_op_imod:
  1428. result = LLVMBuildSRem(ctx->ac.builder, src[0], src[1], "");
  1429. break;
  1430. case nir_op_umod:
  1431. result = LLVMBuildURem(ctx->ac.builder, src[0], src[1], "");
  1432. break;
  1433. case nir_op_fmod:
  1434. src[0] = ac_to_float(&ctx->ac, src[0]);
  1435. src[1] = ac_to_float(&ctx->ac, src[1]);
  1436. result = ac_build_fdiv(&ctx->ac, src[0], src[1]);
  1437. result = emit_intrin_1f_param(&ctx->ac, "llvm.floor",
  1438. ac_to_float_type(&ctx->ac, def_type), result);
  1439. result = LLVMBuildFMul(ctx->ac.builder, src[1] , result, "");
  1440. result = LLVMBuildFSub(ctx->ac.builder, src[0], result, "");
  1441. break;
  1442. case nir_op_frem:
  1443. src[0] = ac_to_float(&ctx->ac, src[0]);
  1444. src[1] = ac_to_float(&ctx->ac, src[1]);
  1445. result = LLVMBuildFRem(ctx->ac.builder, src[0], src[1], "");
  1446. break;
  1447. case nir_op_irem:
  1448. result = LLVMBuildSRem(ctx->ac.builder, src[0], src[1], "");
  1449. break;
  1450. case nir_op_idiv:
  1451. result = LLVMBuildSDiv(ctx->ac.builder, src[0], src[1], "");
  1452. break;
  1453. case nir_op_udiv:
  1454. result = LLVMBuildUDiv(ctx->ac.builder, src[0], src[1], "");
  1455. break;
  1456. case nir_op_fmul:
  1457. src[0] = ac_to_float(&ctx->ac, src[0]);
  1458. src[1] = ac_to_float(&ctx->ac, src[1]);
  1459. result = LLVMBuildFMul(ctx->ac.builder, src[0], src[1], "");
  1460. break;
  1461. case nir_op_fdiv:
  1462. src[0] = ac_to_float(&ctx->ac, src[0]);
  1463. src[1] = ac_to_float(&ctx->ac, src[1]);
  1464. result = ac_build_fdiv(&ctx->ac, src[0], src[1]);
  1465. break;
  1466. case nir_op_frcp:
  1467. src[0] = ac_to_float(&ctx->ac, src[0]);
  1468. result = ac_build_fdiv(&ctx->ac, ctx->ac.f32_1, src[0]);
  1469. break;
  1470. case nir_op_iand:
  1471. result = LLVMBuildAnd(ctx->ac.builder, src[0], src[1], "");
  1472. break;
  1473. case nir_op_ior:
  1474. result = LLVMBuildOr(ctx->ac.builder, src[0], src[1], "");
  1475. break;
  1476. case nir_op_ixor:
  1477. result = LLVMBuildXor(ctx->ac.builder, src[0], src[1], "");
  1478. break;
  1479. case nir_op_ishl:
  1480. result = LLVMBuildShl(ctx->ac.builder, src[0],
  1481. LLVMBuildZExt(ctx->ac.builder, src[1],
  1482. LLVMTypeOf(src[0]), ""),
  1483. "");
  1484. break;
  1485. case nir_op_ishr:
  1486. result = LLVMBuildAShr(ctx->ac.builder, src[0],
  1487. LLVMBuildZExt(ctx->ac.builder, src[1],
  1488. LLVMTypeOf(src[0]), ""),
  1489. "");
  1490. break;
  1491. case nir_op_ushr:
  1492. result = LLVMBuildLShr(ctx->ac.builder, src[0],
  1493. LLVMBuildZExt(ctx->ac.builder, src[1],
  1494. LLVMTypeOf(src[0]), ""),
  1495. "");
  1496. break;
  1497. case nir_op_ilt:
  1498. result = emit_int_cmp(&ctx->ac, LLVMIntSLT, src[0], src[1]);
  1499. break;
  1500. case nir_op_ine:
  1501. result = emit_int_cmp(&ctx->ac, LLVMIntNE, src[0], src[1]);
  1502. break;
  1503. case nir_op_ieq:
  1504. result = emit_int_cmp(&ctx->ac, LLVMIntEQ, src[0], src[1]);
  1505. break;
  1506. case nir_op_ige:
  1507. result = emit_int_cmp(&ctx->ac, LLVMIntSGE, src[0], src[1]);
  1508. break;
  1509. case nir_op_ult:
  1510. result = emit_int_cmp(&ctx->ac, LLVMIntULT, src[0], src[1]);
  1511. break;
  1512. case nir_op_uge:
  1513. result = emit_int_cmp(&ctx->ac, LLVMIntUGE, src[0], src[1]);
  1514. break;
  1515. case nir_op_feq:
  1516. result = emit_float_cmp(&ctx->ac, LLVMRealUEQ, src[0], src[1]);
  1517. break;
  1518. case nir_op_fne:
  1519. result = emit_float_cmp(&ctx->ac, LLVMRealUNE, src[0], src[1]);
  1520. break;
  1521. case nir_op_flt:
  1522. result = emit_float_cmp(&ctx->ac, LLVMRealULT, src[0], src[1]);
  1523. break;
  1524. case nir_op_fge:
  1525. result = emit_float_cmp(&ctx->ac, LLVMRealUGE, src[0], src[1]);
  1526. break;
  1527. case nir_op_fabs:
  1528. result = emit_intrin_1f_param(&ctx->ac, "llvm.fabs",
  1529. ac_to_float_type(&ctx->ac, def_type), src[0]);
  1530. break;
  1531. case nir_op_iabs:
  1532. result = emit_iabs(&ctx->ac, src[0]);
  1533. break;
  1534. case nir_op_imax:
  1535. result = emit_minmax_int(&ctx->ac, LLVMIntSGT, src[0], src[1]);
  1536. break;
  1537. case nir_op_imin:
  1538. result = emit_minmax_int(&ctx->ac, LLVMIntSLT, src[0], src[1]);
  1539. break;
  1540. case nir_op_umax:
  1541. result = emit_minmax_int(&ctx->ac, LLVMIntUGT, src[0], src[1]);
  1542. break;
  1543. case nir_op_umin:
  1544. result = emit_minmax_int(&ctx->ac, LLVMIntULT, src[0], src[1]);
  1545. break;
  1546. case nir_op_isign:
  1547. result = emit_isign(&ctx->ac, src[0]);
  1548. break;
  1549. case nir_op_fsign:
  1550. src[0] = ac_to_float(&ctx->ac, src[0]);
  1551. result = emit_fsign(&ctx->ac, src[0]);
  1552. break;
  1553. case nir_op_ffloor:
  1554. result = emit_intrin_1f_param(&ctx->ac, "llvm.floor",
  1555. ac_to_float_type(&ctx->ac, def_type), src[0]);
  1556. break;
  1557. case nir_op_ftrunc:
  1558. result = emit_intrin_1f_param(&ctx->ac, "llvm.trunc",
  1559. ac_to_float_type(&ctx->ac, def_type), src[0]);
  1560. break;
  1561. case nir_op_fceil:
  1562. result = emit_intrin_1f_param(&ctx->ac, "llvm.ceil",
  1563. ac_to_float_type(&ctx->ac, def_type), src[0]);
  1564. break;
  1565. case nir_op_fround_even:
  1566. result = emit_intrin_1f_param(&ctx->ac, "llvm.rint",
  1567. ac_to_float_type(&ctx->ac, def_type),src[0]);
  1568. break;
  1569. case nir_op_ffract:
  1570. result = emit_ffract(&ctx->ac, src[0]);
  1571. break;
  1572. case nir_op_fsin:
  1573. result = emit_intrin_1f_param(&ctx->ac, "llvm.sin",
  1574. ac_to_float_type(&ctx->ac, def_type), src[0]);
  1575. break;
  1576. case nir_op_fcos:
  1577. result = emit_intrin_1f_param(&ctx->ac, "llvm.cos",
  1578. ac_to_float_type(&ctx->ac, def_type), src[0]);
  1579. break;
  1580. case nir_op_fsqrt:
  1581. result = emit_intrin_1f_param(&ctx->ac, "llvm.sqrt",
  1582. ac_to_float_type(&ctx->ac, def_type), src[0]);
  1583. break;
  1584. case nir_op_fexp2:
  1585. result = emit_intrin_1f_param(&ctx->ac, "llvm.exp2",
  1586. ac_to_float_type(&ctx->ac, def_type), src[0]);
  1587. break;
  1588. case nir_op_flog2:
  1589. result = emit_intrin_1f_param(&ctx->ac, "llvm.log2",
  1590. ac_to_float_type(&ctx->ac, def_type), src[0]);
  1591. break;
  1592. case nir_op_frsq:
  1593. result = emit_intrin_1f_param(&ctx->ac, "llvm.sqrt",
  1594. ac_to_float_type(&ctx->ac, def_type), src[0]);
  1595. result = ac_build_fdiv(&ctx->ac, ctx->ac.f32_1, result);
  1596. break;
  1597. case nir_op_fpow:
  1598. result = emit_intrin_2f_param(&ctx->ac, "llvm.pow",
  1599. ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
  1600. break;
  1601. case nir_op_fmax:
  1602. result = emit_intrin_2f_param(&ctx->ac, "llvm.maxnum",
  1603. ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
  1604. if (instr->dest.dest.ssa.bit_size == 32)
  1605. result = emit_intrin_1f_param(&ctx->ac, "llvm.canonicalize",
  1606. ac_to_float_type(&ctx->ac, def_type),
  1607. result);
  1608. break;
  1609. case nir_op_fmin:
  1610. result = emit_intrin_2f_param(&ctx->ac, "llvm.minnum",
  1611. ac_to_float_type(&ctx->ac, def_type), src[0], src[1]);
  1612. if (instr->dest.dest.ssa.bit_size == 32)
  1613. result = emit_intrin_1f_param(&ctx->ac, "llvm.canonicalize",
  1614. ac_to_float_type(&ctx->ac, def_type),
  1615. result);
  1616. break;
  1617. case nir_op_ffma:
  1618. result = emit_intrin_3f_param(&ctx->ac, "llvm.fmuladd",
  1619. ac_to_float_type(&ctx->ac, def_type), src[0], src[1], src[2]);
  1620. break;
  1621. case nir_op_ibitfield_extract:
  1622. result = emit_bitfield_extract(&ctx->ac, true, src);
  1623. break;
  1624. case nir_op_ubitfield_extract:
  1625. result = emit_bitfield_extract(&ctx->ac, false, src);
  1626. break;
  1627. case nir_op_bitfield_insert:
  1628. result = emit_bitfield_insert(&ctx->ac, src[0], src[1], src[2], src[3]);
  1629. break;
  1630. case nir_op_bitfield_reverse:
  1631. result = ac_build_intrinsic(&ctx->ac, "llvm.bitreverse.i32", ctx->ac.i32, src, 1, AC_FUNC_ATTR_READNONE);
  1632. break;
  1633. case nir_op_bit_count:
  1634. result = ac_build_intrinsic(&ctx->ac, "llvm.ctpop.i32", ctx->ac.i32, src, 1, AC_FUNC_ATTR_READNONE);
  1635. break;
  1636. case nir_op_vec2:
  1637. case nir_op_vec3:
  1638. case nir_op_vec4:
  1639. for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
  1640. src[i] = ac_to_integer(&ctx->ac, src[i]);
  1641. result = ac_build_gather_values(&ctx->ac, src, num_components);
  1642. break;
  1643. case nir_op_f2i32:
  1644. case nir_op_f2i64:
  1645. src[0] = ac_to_float(&ctx->ac, src[0]);
  1646. result = LLVMBuildFPToSI(ctx->ac.builder, src[0], def_type, "");
  1647. break;
  1648. case nir_op_f2u32:
  1649. case nir_op_f2u64:
  1650. src[0] = ac_to_float(&ctx->ac, src[0]);
  1651. result = LLVMBuildFPToUI(ctx->ac.builder, src[0], def_type, "");
  1652. break;
  1653. case nir_op_i2f32:
  1654. case nir_op_i2f64:
  1655. src[0] = ac_to_integer(&ctx->ac, src[0]);
  1656. result = LLVMBuildSIToFP(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
  1657. break;
  1658. case nir_op_u2f32:
  1659. case nir_op_u2f64:
  1660. src[0] = ac_to_integer(&ctx->ac, src[0]);
  1661. result = LLVMBuildUIToFP(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
  1662. break;
  1663. case nir_op_f2f64:
  1664. result = LLVMBuildFPExt(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
  1665. break;
  1666. case nir_op_f2f32:
  1667. result = LLVMBuildFPTrunc(ctx->ac.builder, src[0], ac_to_float_type(&ctx->ac, def_type), "");
  1668. break;
  1669. case nir_op_u2u32:
  1670. case nir_op_u2u64:
  1671. src[0] = ac_to_integer(&ctx->ac, src[0]);
  1672. if (get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) < get_elem_bits(&ctx->ac, def_type))
  1673. result = LLVMBuildZExt(ctx->ac.builder, src[0], def_type, "");
  1674. else
  1675. result = LLVMBuildTrunc(ctx->ac.builder, src[0], def_type, "");
  1676. break;
  1677. case nir_op_i2i32:
  1678. case nir_op_i2i64:
  1679. src[0] = ac_to_integer(&ctx->ac, src[0]);
  1680. if (get_elem_bits(&ctx->ac, LLVMTypeOf(src[0])) < get_elem_bits(&ctx->ac, def_type))
  1681. result = LLVMBuildSExt(ctx->ac.builder, src[0], def_type, "");
  1682. else
  1683. result = LLVMBuildTrunc(ctx->ac.builder, src[0], def_type, "");
  1684. break;
  1685. case nir_op_bcsel:
  1686. result = emit_bcsel(&ctx->ac, src[0], src[1], src[2]);
  1687. break;
  1688. case nir_op_find_lsb:
  1689. src[0] = ac_to_integer(&ctx->ac, src[0]);
  1690. result = emit_find_lsb(&ctx->ac, src[0]);
  1691. break;
  1692. case nir_op_ufind_msb:
  1693. src[0] = ac_to_integer(&ctx->ac, src[0]);
  1694. result = emit_ufind_msb(&ctx->ac, src[0]);
  1695. break;
  1696. case nir_op_ifind_msb:
  1697. src[0] = ac_to_integer(&ctx->ac, src[0]);
  1698. result = emit_ifind_msb(&ctx->ac, src[0]);
  1699. break;
  1700. case nir_op_uadd_carry:
  1701. src[0] = ac_to_integer(&ctx->ac, src[0]);
  1702. src[1] = ac_to_integer(&ctx->ac, src[1]);
  1703. result = emit_uint_carry(&ctx->ac, "llvm.uadd.with.overflow.i32", src[0], src[1]);
  1704. break;
  1705. case nir_op_usub_borrow:
  1706. src[0] = ac_to_integer(&ctx->ac, src[0]);
  1707. src[1] = ac_to_integer(&ctx->ac, src[1]);
  1708. result = emit_uint_carry(&ctx->ac, "llvm.usub.with.overflow.i32", src[0], src[1]);
  1709. break;
  1710. case nir_op_b2f:
  1711. result = emit_b2f(&ctx->ac, src[0]);
  1712. break;
  1713. case nir_op_f2b:
  1714. result = emit_f2b(&ctx->ac, src[0]);
  1715. break;
  1716. case nir_op_b2i:
  1717. result = emit_b2i(&ctx->ac, src[0]);
  1718. break;
  1719. case nir_op_i2b:
  1720. src[0] = ac_to_integer(&ctx->ac, src[0]);
  1721. result = emit_i2b(&ctx->ac, src[0]);
  1722. break;
  1723. case nir_op_fquantize2f16:
  1724. result = emit_f2f16(ctx->nctx, src[0]);
  1725. break;
  1726. case nir_op_umul_high:
  1727. src[0] = ac_to_integer(&ctx->ac, src[0]);
  1728. src[1] = ac_to_integer(&ctx->ac, src[1]);
  1729. result = emit_umul_high(&ctx->ac, src[0], src[1]);
  1730. break;
  1731. case nir_op_imul_high:
  1732. src[0] = ac_to_integer(&ctx->ac, src[0]);
  1733. src[1] = ac_to_integer(&ctx->ac, src[1]);
  1734. result = emit_imul_high(&ctx->ac, src[0], src[1]);
  1735. break;
  1736. case nir_op_pack_half_2x16:
  1737. result = emit_pack_half_2x16(&ctx->ac, src[0]);
  1738. break;
  1739. case nir_op_unpack_half_2x16:
  1740. result = emit_unpack_half_2x16(&ctx->ac, src[0]);
  1741. break;
  1742. case nir_op_fddx:
  1743. case nir_op_fddy:
  1744. case nir_op_fddx_fine:
  1745. case nir_op_fddy_fine:
  1746. case nir_op_fddx_coarse:
  1747. case nir_op_fddy_coarse:
  1748. result = emit_ddxy(ctx, instr->op, src[0]);
  1749. break;
  1750. case nir_op_unpack_64_2x32_split_x: {
  1751. assert(instr->src[0].src.ssa->num_components == 1);
  1752. LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, src[0],
  1753. LLVMVectorType(ctx->ac.i32, 2),
  1754. "");
  1755. result = LLVMBuildExtractElement(ctx->ac.builder, tmp,
  1756. ctx->ac.i32_0, "");
  1757. break;
  1758. }
  1759. case nir_op_unpack_64_2x32_split_y: {
  1760. assert(instr->src[0].src.ssa->num_components == 1);
  1761. LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, src[0],
  1762. LLVMVectorType(ctx->ac.i32, 2),
  1763. "");
  1764. result = LLVMBuildExtractElement(ctx->ac.builder, tmp,
  1765. ctx->ac.i32_1, "");
  1766. break;
  1767. }
  1768. case nir_op_pack_64_2x32_split: {
  1769. LLVMValueRef tmp = LLVMGetUndef(LLVMVectorType(ctx->ac.i32, 2));
  1770. tmp = LLVMBuildInsertElement(ctx->ac.builder, tmp,
  1771. src[0], ctx->ac.i32_0, "");
  1772. tmp = LLVMBuildInsertElement(ctx->ac.builder, tmp,
  1773. src[1], ctx->ac.i32_1, "");
  1774. result = LLVMBuildBitCast(ctx->ac.builder, tmp, ctx->ac.i64, "");
  1775. break;
  1776. }
  1777. default:
  1778. fprintf(stderr, "Unknown NIR alu instr: ");
  1779. nir_print_instr(&instr->instr, stderr);
  1780. fprintf(stderr, "\n");
  1781. abort();
  1782. }
  1783. if (result) {
  1784. assert(instr->dest.dest.is_ssa);
  1785. result = ac_to_integer(&ctx->ac, result);
  1786. _mesa_hash_table_insert(ctx->defs, &instr->dest.dest.ssa,
  1787. result);
  1788. }
  1789. }
  1790. static void visit_load_const(struct ac_nir_context *ctx,
  1791. const nir_load_const_instr *instr)
  1792. {
  1793. LLVMValueRef values[4], value = NULL;
  1794. LLVMTypeRef element_type =
  1795. LLVMIntTypeInContext(ctx->ac.context, instr->def.bit_size);
  1796. for (unsigned i = 0; i < instr->def.num_components; ++i) {
  1797. switch (instr->def.bit_size) {
  1798. case 32:
  1799. values[i] = LLVMConstInt(element_type,
  1800. instr->value.u32[i], false);
  1801. break;
  1802. case 64:
  1803. values[i] = LLVMConstInt(element_type,
  1804. instr->value.u64[i], false);
  1805. break;
  1806. default:
  1807. fprintf(stderr,
  1808. "unsupported nir load_const bit_size: %d\n",
  1809. instr->def.bit_size);
  1810. abort();
  1811. }
  1812. }
  1813. if (instr->def.num_components > 1) {
  1814. value = LLVMConstVector(values, instr->def.num_components);
  1815. } else
  1816. value = values[0];
  1817. _mesa_hash_table_insert(ctx->defs, &instr->def, value);
  1818. }
  1819. static LLVMValueRef cast_ptr(struct nir_to_llvm_context *ctx, LLVMValueRef ptr,
  1820. LLVMTypeRef type)
  1821. {
  1822. int addr_space = LLVMGetPointerAddressSpace(LLVMTypeOf(ptr));
  1823. return LLVMBuildBitCast(ctx->builder, ptr,
  1824. LLVMPointerType(type, addr_space), "");
  1825. }
  1826. static LLVMValueRef
  1827. get_buffer_size(struct ac_nir_context *ctx, LLVMValueRef descriptor, bool in_elements)
  1828. {
  1829. LLVMValueRef size =
  1830. LLVMBuildExtractElement(ctx->ac.builder, descriptor,
  1831. LLVMConstInt(ctx->ac.i32, 2, false), "");
  1832. /* VI only */
  1833. if (ctx->ac.chip_class == VI && in_elements) {
  1834. /* On VI, the descriptor contains the size in bytes,
  1835. * but TXQ must return the size in elements.
  1836. * The stride is always non-zero for resources using TXQ.
  1837. */
  1838. LLVMValueRef stride =
  1839. LLVMBuildExtractElement(ctx->ac.builder, descriptor,
  1840. LLVMConstInt(ctx->ac.i32, 1, false), "");
  1841. stride = LLVMBuildLShr(ctx->ac.builder, stride,
  1842. LLVMConstInt(ctx->ac.i32, 16, false), "");
  1843. stride = LLVMBuildAnd(ctx->ac.builder, stride,
  1844. LLVMConstInt(ctx->ac.i32, 0x3fff, false), "");
  1845. size = LLVMBuildUDiv(ctx->ac.builder, size, stride, "");
  1846. }
  1847. return size;
  1848. }
  1849. /**
  1850. * Given the i32 or vNi32 \p type, generate the textual name (e.g. for use with
  1851. * intrinsic names).
  1852. */
  1853. static void build_int_type_name(
  1854. LLVMTypeRef type,
  1855. char *buf, unsigned bufsize)
  1856. {
  1857. assert(bufsize >= 6);
  1858. if (LLVMGetTypeKind(type) == LLVMVectorTypeKind)
  1859. snprintf(buf, bufsize, "v%ui32",
  1860. LLVMGetVectorSize(type));
  1861. else
  1862. strcpy(buf, "i32");
  1863. }
  1864. static LLVMValueRef radv_lower_gather4_integer(struct ac_llvm_context *ctx,
  1865. struct ac_image_args *args,
  1866. const nir_tex_instr *instr)
  1867. {
  1868. enum glsl_base_type stype = glsl_get_sampler_result_type(instr->texture->var->type);
  1869. LLVMValueRef coord = args->addr;
  1870. LLVMValueRef half_texel[2];
  1871. LLVMValueRef compare_cube_wa = NULL;
  1872. LLVMValueRef result;
  1873. int c;
  1874. unsigned coord_vgpr_index = (unsigned)args->offset + (unsigned)args->compare;
  1875. //TODO Rect
  1876. {
  1877. struct ac_image_args txq_args = { 0 };
  1878. txq_args.da = instr->is_array || instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
  1879. txq_args.opcode = ac_image_get_resinfo;
  1880. txq_args.dmask = 0xf;
  1881. txq_args.addr = ctx->i32_0;
  1882. txq_args.resource = args->resource;
  1883. LLVMValueRef size = ac_build_image_opcode(ctx, &txq_args);
  1884. for (c = 0; c < 2; c++) {
  1885. half_texel[c] = LLVMBuildExtractElement(ctx->builder, size,
  1886. LLVMConstInt(ctx->i32, c, false), "");
  1887. half_texel[c] = LLVMBuildUIToFP(ctx->builder, half_texel[c], ctx->f32, "");
  1888. half_texel[c] = ac_build_fdiv(ctx, ctx->f32_1, half_texel[c]);
  1889. half_texel[c] = LLVMBuildFMul(ctx->builder, half_texel[c],
  1890. LLVMConstReal(ctx->f32, -0.5), "");
  1891. }
  1892. }
  1893. LLVMValueRef orig_coords = args->addr;
  1894. for (c = 0; c < 2; c++) {
  1895. LLVMValueRef tmp;
  1896. LLVMValueRef index = LLVMConstInt(ctx->i32, coord_vgpr_index + c, 0);
  1897. tmp = LLVMBuildExtractElement(ctx->builder, coord, index, "");
  1898. tmp = LLVMBuildBitCast(ctx->builder, tmp, ctx->f32, "");
  1899. tmp = LLVMBuildFAdd(ctx->builder, tmp, half_texel[c], "");
  1900. tmp = LLVMBuildBitCast(ctx->builder, tmp, ctx->i32, "");
  1901. coord = LLVMBuildInsertElement(ctx->builder, coord, tmp, index, "");
  1902. }
  1903. /*
  1904. * Apparantly cube has issue with integer types that the workaround doesn't solve,
  1905. * so this tests if the format is 8_8_8_8 and an integer type do an alternate
  1906. * workaround by sampling using a scaled type and converting.
  1907. * This is taken from amdgpu-pro shaders.
  1908. */
  1909. /* NOTE this produces some ugly code compared to amdgpu-pro,
  1910. * LLVM ends up dumping SGPRs into VGPRs to deal with the compare/select,
  1911. * and then reads them back. -pro generates two selects,
  1912. * one s_cmp for the descriptor rewriting
  1913. * one v_cmp for the coordinate and result changes.
  1914. */
  1915. if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
  1916. LLVMValueRef tmp, tmp2;
  1917. /* workaround 8/8/8/8 uint/sint cube gather bug */
  1918. /* first detect it then change to a scaled read and f2i */
  1919. tmp = LLVMBuildExtractElement(ctx->builder, args->resource, ctx->i32_1, "");
  1920. tmp2 = tmp;
  1921. /* extract the DATA_FORMAT */
  1922. tmp = ac_build_bfe(ctx, tmp, LLVMConstInt(ctx->i32, 20, false),
  1923. LLVMConstInt(ctx->i32, 6, false), false);
  1924. /* is the DATA_FORMAT == 8_8_8_8 */
  1925. compare_cube_wa = LLVMBuildICmp(ctx->builder, LLVMIntEQ, tmp, LLVMConstInt(ctx->i32, V_008F14_IMG_DATA_FORMAT_8_8_8_8, false), "");
  1926. if (stype == GLSL_TYPE_UINT)
  1927. /* Create a NUM FORMAT - 0x2 or 0x4 - USCALED or UINT */
  1928. tmp = LLVMBuildSelect(ctx->builder, compare_cube_wa, LLVMConstInt(ctx->i32, 0x8000000, false),
  1929. LLVMConstInt(ctx->i32, 0x10000000, false), "");
  1930. else
  1931. /* Create a NUM FORMAT - 0x3 or 0x5 - SSCALED or SINT */
  1932. tmp = LLVMBuildSelect(ctx->builder, compare_cube_wa, LLVMConstInt(ctx->i32, 0xc000000, false),
  1933. LLVMConstInt(ctx->i32, 0x14000000, false), "");
  1934. /* replace the NUM FORMAT in the descriptor */
  1935. tmp2 = LLVMBuildAnd(ctx->builder, tmp2, LLVMConstInt(ctx->i32, C_008F14_NUM_FORMAT_GFX6, false), "");
  1936. tmp2 = LLVMBuildOr(ctx->builder, tmp2, tmp, "");
  1937. args->resource = LLVMBuildInsertElement(ctx->builder, args->resource, tmp2, ctx->i32_1, "");
  1938. /* don't modify the coordinates for this case */
  1939. coord = LLVMBuildSelect(ctx->builder, compare_cube_wa, orig_coords, coord, "");
  1940. }
  1941. args->addr = coord;
  1942. result = ac_build_image_opcode(ctx, args);
  1943. if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
  1944. LLVMValueRef tmp, tmp2;
  1945. /* if the cube workaround is in place, f2i the result. */
  1946. for (c = 0; c < 4; c++) {
  1947. tmp = LLVMBuildExtractElement(ctx->builder, result, LLVMConstInt(ctx->i32, c, false), "");
  1948. if (stype == GLSL_TYPE_UINT)
  1949. tmp2 = LLVMBuildFPToUI(ctx->builder, tmp, ctx->i32, "");
  1950. else
  1951. tmp2 = LLVMBuildFPToSI(ctx->builder, tmp, ctx->i32, "");
  1952. tmp = LLVMBuildBitCast(ctx->builder, tmp, ctx->i32, "");
  1953. tmp2 = LLVMBuildBitCast(ctx->builder, tmp2, ctx->i32, "");
  1954. tmp = LLVMBuildSelect(ctx->builder, compare_cube_wa, tmp2, tmp, "");
  1955. tmp = LLVMBuildBitCast(ctx->builder, tmp, ctx->f32, "");
  1956. result = LLVMBuildInsertElement(ctx->builder, result, tmp, LLVMConstInt(ctx->i32, c, false), "");
  1957. }
  1958. }
  1959. return result;
  1960. }
  1961. static LLVMValueRef build_tex_intrinsic(struct ac_nir_context *ctx,
  1962. const nir_tex_instr *instr,
  1963. bool lod_is_zero,
  1964. struct ac_image_args *args)
  1965. {
  1966. if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
  1967. return ac_build_buffer_load_format(&ctx->ac,
  1968. args->resource,
  1969. args->addr,
  1970. LLVMConstInt(ctx->ac.i32, 0, false),
  1971. true);
  1972. }
  1973. args->opcode = ac_image_sample;
  1974. args->compare = instr->is_shadow;
  1975. switch (instr->op) {
  1976. case nir_texop_txf:
  1977. case nir_texop_txf_ms:
  1978. case nir_texop_samples_identical:
  1979. args->opcode = instr->sampler_dim == GLSL_SAMPLER_DIM_MS ? ac_image_load : ac_image_load_mip;
  1980. args->compare = false;
  1981. args->offset = false;
  1982. break;
  1983. case nir_texop_txb:
  1984. args->bias = true;
  1985. break;
  1986. case nir_texop_txl:
  1987. if (lod_is_zero)
  1988. args->level_zero = true;
  1989. else
  1990. args->lod = true;
  1991. break;
  1992. case nir_texop_txs:
  1993. case nir_texop_query_levels:
  1994. args->opcode = ac_image_get_resinfo;
  1995. break;
  1996. case nir_texop_tex:
  1997. if (ctx->stage != MESA_SHADER_FRAGMENT)
  1998. args->level_zero = true;
  1999. break;
  2000. case nir_texop_txd:
  2001. args->deriv = true;
  2002. break;
  2003. case nir_texop_tg4:
  2004. args->opcode = ac_image_gather4;
  2005. args->level_zero = true;
  2006. break;
  2007. case nir_texop_lod:
  2008. args->opcode = ac_image_get_lod;
  2009. args->compare = false;
  2010. args->offset = false;
  2011. break;
  2012. default:
  2013. break;
  2014. }
  2015. if (instr->op == nir_texop_tg4 && ctx->ac.chip_class <= VI) {
  2016. enum glsl_base_type stype = glsl_get_sampler_result_type(instr->texture->var->type);
  2017. if (stype == GLSL_TYPE_UINT || stype == GLSL_TYPE_INT) {
  2018. return radv_lower_gather4_integer(&ctx->ac, args, instr);
  2019. }
  2020. }
  2021. return ac_build_image_opcode(&ctx->ac, args);
  2022. }
  2023. static LLVMValueRef visit_vulkan_resource_index(struct nir_to_llvm_context *ctx,
  2024. nir_intrinsic_instr *instr)
  2025. {
  2026. LLVMValueRef index = get_src(ctx->nir, instr->src[0]);
  2027. unsigned desc_set = nir_intrinsic_desc_set(instr);
  2028. unsigned binding = nir_intrinsic_binding(instr);
  2029. LLVMValueRef desc_ptr = ctx->descriptor_sets[desc_set];
  2030. struct radv_pipeline_layout *pipeline_layout = ctx->options->layout;
  2031. struct radv_descriptor_set_layout *layout = pipeline_layout->set[desc_set].layout;
  2032. unsigned base_offset = layout->binding[binding].offset;
  2033. LLVMValueRef offset, stride;
  2034. if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
  2035. layout->binding[binding].type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
  2036. unsigned idx = pipeline_layout->set[desc_set].dynamic_offset_start +
  2037. layout->binding[binding].dynamic_offset_offset;
  2038. desc_ptr = ctx->push_constants;
  2039. base_offset = pipeline_layout->push_constant_size + 16 * idx;
  2040. stride = LLVMConstInt(ctx->i32, 16, false);
  2041. } else
  2042. stride = LLVMConstInt(ctx->i32, layout->binding[binding].size, false);
  2043. offset = LLVMConstInt(ctx->i32, base_offset, false);
  2044. index = LLVMBuildMul(ctx->builder, index, stride, "");
  2045. offset = LLVMBuildAdd(ctx->builder, offset, index, "");
  2046. desc_ptr = ac_build_gep0(&ctx->ac, desc_ptr, offset);
  2047. desc_ptr = cast_ptr(ctx, desc_ptr, ctx->v4i32);
  2048. LLVMSetMetadata(desc_ptr, ctx->uniform_md_kind, ctx->empty_md);
  2049. return LLVMBuildLoad(ctx->builder, desc_ptr, "");
  2050. }
  2051. static LLVMValueRef visit_load_push_constant(struct nir_to_llvm_context *ctx,
  2052. nir_intrinsic_instr *instr)
  2053. {
  2054. LLVMValueRef ptr, addr;
  2055. addr = LLVMConstInt(ctx->i32, nir_intrinsic_base(instr), 0);
  2056. addr = LLVMBuildAdd(ctx->builder, addr, get_src(ctx->nir, instr->src[0]), "");
  2057. ptr = ac_build_gep0(&ctx->ac, ctx->push_constants, addr);
  2058. ptr = cast_ptr(ctx, ptr, get_def_type(ctx->nir, &instr->dest.ssa));
  2059. return LLVMBuildLoad(ctx->builder, ptr, "");
  2060. }
  2061. static LLVMValueRef visit_get_buffer_size(struct ac_nir_context *ctx,
  2062. const nir_intrinsic_instr *instr)
  2063. {
  2064. LLVMValueRef desc = get_src(ctx, instr->src[0]);
  2065. return get_buffer_size(ctx, desc, false);
  2066. }
  2067. static void visit_store_ssbo(struct ac_nir_context *ctx,
  2068. nir_intrinsic_instr *instr)
  2069. {
  2070. const char *store_name;
  2071. LLVMValueRef src_data = get_src(ctx, instr->src[0]);
  2072. LLVMTypeRef data_type = ctx->ac.f32;
  2073. int elem_size_mult = get_elem_bits(&ctx->ac, LLVMTypeOf(src_data)) / 32;
  2074. int components_32bit = elem_size_mult * instr->num_components;
  2075. unsigned writemask = nir_intrinsic_write_mask(instr);
  2076. LLVMValueRef base_data, base_offset;
  2077. LLVMValueRef params[6];
  2078. LLVMValueRef i1false = LLVMConstInt(ctx->ac.i1, 0, false);
  2079. params[1] = ctx->abi->load_ssbo(ctx->abi,
  2080. get_src(ctx, instr->src[1]), true);
  2081. params[2] = LLVMConstInt(ctx->ac.i32, 0, false); /* vindex */
  2082. params[4] = i1false; /* glc */
  2083. params[5] = i1false; /* slc */
  2084. if (components_32bit > 1)
  2085. data_type = LLVMVectorType(ctx->ac.f32, components_32bit);
  2086. base_data = ac_to_float(&ctx->ac, src_data);
  2087. base_data = trim_vector(&ctx->ac, base_data, instr->num_components);
  2088. base_data = LLVMBuildBitCast(ctx->ac.builder, base_data,
  2089. data_type, "");
  2090. base_offset = get_src(ctx, instr->src[2]); /* voffset */
  2091. while (writemask) {
  2092. int start, count;
  2093. LLVMValueRef data;
  2094. LLVMValueRef offset;
  2095. LLVMValueRef tmp;
  2096. u_bit_scan_consecutive_range(&writemask, &start, &count);
  2097. /* Due to an LLVM limitation, split 3-element writes
  2098. * into a 2-element and a 1-element write. */
  2099. if (count == 3) {
  2100. writemask |= 1 << (start + 2);
  2101. count = 2;
  2102. }
  2103. start *= elem_size_mult;
  2104. count *= elem_size_mult;
  2105. if (count > 4) {
  2106. writemask |= ((1u << (count - 4)) - 1u) << (start + 4);
  2107. count = 4;
  2108. }
  2109. if (count == 4) {
  2110. store_name = "llvm.amdgcn.buffer.store.v4f32";
  2111. data = base_data;
  2112. } else if (count == 2) {
  2113. LLVMTypeRef v2f32 = LLVMVectorType(ctx->ac.f32, 2);
  2114. tmp = LLVMBuildExtractElement(ctx->ac.builder,
  2115. base_data, LLVMConstInt(ctx->ac.i32, start, false), "");
  2116. data = LLVMBuildInsertElement(ctx->ac.builder, LLVMGetUndef(v2f32), tmp,
  2117. ctx->ac.i32_0, "");
  2118. tmp = LLVMBuildExtractElement(ctx->ac.builder,
  2119. base_data, LLVMConstInt(ctx->ac.i32, start + 1, false), "");
  2120. data = LLVMBuildInsertElement(ctx->ac.builder, data, tmp,
  2121. ctx->ac.i32_1, "");
  2122. store_name = "llvm.amdgcn.buffer.store.v2f32";
  2123. } else {
  2124. assert(count == 1);
  2125. if (get_llvm_num_components(base_data) > 1)
  2126. data = LLVMBuildExtractElement(ctx->ac.builder, base_data,
  2127. LLVMConstInt(ctx->ac.i32, start, false), "");
  2128. else
  2129. data = base_data;
  2130. store_name = "llvm.amdgcn.buffer.store.f32";
  2131. }
  2132. offset = base_offset;
  2133. if (start != 0) {
  2134. offset = LLVMBuildAdd(ctx->ac.builder, offset, LLVMConstInt(ctx->ac.i32, start * 4, false), "");
  2135. }
  2136. params[0] = data;
  2137. params[3] = offset;
  2138. ac_build_intrinsic(&ctx->ac, store_name,
  2139. ctx->ac.voidt, params, 6, 0);
  2140. }
  2141. }
  2142. static LLVMValueRef visit_atomic_ssbo(struct ac_nir_context *ctx,
  2143. const nir_intrinsic_instr *instr)
  2144. {
  2145. const char *name;
  2146. LLVMValueRef params[6];
  2147. int arg_count = 0;
  2148. if (instr->intrinsic == nir_intrinsic_ssbo_atomic_comp_swap) {
  2149. params[arg_count++] = llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[3]), 0);
  2150. }
  2151. params[arg_count++] = llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[2]), 0);
  2152. params[arg_count++] = ctx->abi->load_ssbo(ctx->abi,
  2153. get_src(ctx, instr->src[0]),
  2154. true);
  2155. params[arg_count++] = LLVMConstInt(ctx->ac.i32, 0, false); /* vindex */
  2156. params[arg_count++] = get_src(ctx, instr->src[1]); /* voffset */
  2157. params[arg_count++] = LLVMConstInt(ctx->ac.i1, 0, false); /* slc */
  2158. switch (instr->intrinsic) {
  2159. case nir_intrinsic_ssbo_atomic_add:
  2160. name = "llvm.amdgcn.buffer.atomic.add";
  2161. break;
  2162. case nir_intrinsic_ssbo_atomic_imin:
  2163. name = "llvm.amdgcn.buffer.atomic.smin";
  2164. break;
  2165. case nir_intrinsic_ssbo_atomic_umin:
  2166. name = "llvm.amdgcn.buffer.atomic.umin";
  2167. break;
  2168. case nir_intrinsic_ssbo_atomic_imax:
  2169. name = "llvm.amdgcn.buffer.atomic.smax";
  2170. break;
  2171. case nir_intrinsic_ssbo_atomic_umax:
  2172. name = "llvm.amdgcn.buffer.atomic.umax";
  2173. break;
  2174. case nir_intrinsic_ssbo_atomic_and:
  2175. name = "llvm.amdgcn.buffer.atomic.and";
  2176. break;
  2177. case nir_intrinsic_ssbo_atomic_or:
  2178. name = "llvm.amdgcn.buffer.atomic.or";
  2179. break;
  2180. case nir_intrinsic_ssbo_atomic_xor:
  2181. name = "llvm.amdgcn.buffer.atomic.xor";
  2182. break;
  2183. case nir_intrinsic_ssbo_atomic_exchange:
  2184. name = "llvm.amdgcn.buffer.atomic.swap";
  2185. break;
  2186. case nir_intrinsic_ssbo_atomic_comp_swap:
  2187. name = "llvm.amdgcn.buffer.atomic.cmpswap";
  2188. break;
  2189. default:
  2190. abort();
  2191. }
  2192. return ac_build_intrinsic(&ctx->ac, name, ctx->ac.i32, params, arg_count, 0);
  2193. }
  2194. static LLVMValueRef visit_load_buffer(struct ac_nir_context *ctx,
  2195. const nir_intrinsic_instr *instr)
  2196. {
  2197. LLVMValueRef results[2];
  2198. int load_components;
  2199. int num_components = instr->num_components;
  2200. if (instr->dest.ssa.bit_size == 64)
  2201. num_components *= 2;
  2202. for (int i = 0; i < num_components; i += load_components) {
  2203. load_components = MIN2(num_components - i, 4);
  2204. const char *load_name;
  2205. LLVMTypeRef data_type = ctx->ac.f32;
  2206. LLVMValueRef offset = LLVMConstInt(ctx->ac.i32, i * 4, false);
  2207. offset = LLVMBuildAdd(ctx->ac.builder, get_src(ctx, instr->src[1]), offset, "");
  2208. if (load_components == 3)
  2209. data_type = LLVMVectorType(ctx->ac.f32, 4);
  2210. else if (load_components > 1)
  2211. data_type = LLVMVectorType(ctx->ac.f32, load_components);
  2212. if (load_components >= 3)
  2213. load_name = "llvm.amdgcn.buffer.load.v4f32";
  2214. else if (load_components == 2)
  2215. load_name = "llvm.amdgcn.buffer.load.v2f32";
  2216. else if (load_components == 1)
  2217. load_name = "llvm.amdgcn.buffer.load.f32";
  2218. else
  2219. unreachable("unhandled number of components");
  2220. LLVMValueRef i1false = LLVMConstInt(ctx->ac.i1, 0, false);
  2221. LLVMValueRef params[] = {
  2222. ctx->abi->load_ssbo(ctx->abi,
  2223. get_src(ctx, instr->src[0]),
  2224. false),
  2225. LLVMConstInt(ctx->ac.i32, 0, false),
  2226. offset,
  2227. i1false,
  2228. i1false,
  2229. };
  2230. results[i] = ac_build_intrinsic(&ctx->ac, load_name, data_type, params, 5, 0);
  2231. }
  2232. LLVMValueRef ret = results[0];
  2233. if (num_components > 4 || num_components == 3) {
  2234. LLVMValueRef masks[] = {
  2235. LLVMConstInt(ctx->ac.i32, 0, false), LLVMConstInt(ctx->ac.i32, 1, false),
  2236. LLVMConstInt(ctx->ac.i32, 2, false), LLVMConstInt(ctx->ac.i32, 3, false),
  2237. LLVMConstInt(ctx->ac.i32, 4, false), LLVMConstInt(ctx->ac.i32, 5, false),
  2238. LLVMConstInt(ctx->ac.i32, 6, false), LLVMConstInt(ctx->ac.i32, 7, false)
  2239. };
  2240. LLVMValueRef swizzle = LLVMConstVector(masks, num_components);
  2241. ret = LLVMBuildShuffleVector(ctx->ac.builder, results[0],
  2242. results[num_components > 4 ? 1 : 0], swizzle, "");
  2243. }
  2244. return LLVMBuildBitCast(ctx->ac.builder, ret,
  2245. get_def_type(ctx, &instr->dest.ssa), "");
  2246. }
  2247. static LLVMValueRef visit_load_ubo_buffer(struct ac_nir_context *ctx,
  2248. const nir_intrinsic_instr *instr)
  2249. {
  2250. LLVMValueRef results[8], ret;
  2251. LLVMValueRef rsrc = get_src(ctx, instr->src[0]);
  2252. LLVMValueRef offset = get_src(ctx, instr->src[1]);
  2253. int num_components = instr->num_components;
  2254. if (ctx->abi->load_ubo)
  2255. rsrc = ctx->abi->load_ubo(ctx->abi, rsrc);
  2256. if (instr->dest.ssa.bit_size == 64)
  2257. num_components *= 2;
  2258. for (unsigned i = 0; i < num_components; ++i) {
  2259. LLVMValueRef params[] = {
  2260. rsrc,
  2261. LLVMBuildAdd(ctx->ac.builder, LLVMConstInt(ctx->ac.i32, 4 * i, 0),
  2262. offset, "")
  2263. };
  2264. results[i] = ac_build_intrinsic(&ctx->ac, "llvm.SI.load.const.v4i32", ctx->ac.f32,
  2265. params, 2,
  2266. AC_FUNC_ATTR_READNONE |
  2267. AC_FUNC_ATTR_LEGACY);
  2268. }
  2269. ret = ac_build_gather_values(&ctx->ac, results, instr->num_components);
  2270. return LLVMBuildBitCast(ctx->ac.builder, ret,
  2271. get_def_type(ctx, &instr->dest.ssa), "");
  2272. }
  2273. static void
  2274. get_deref_offset(struct ac_nir_context *ctx, nir_deref_var *deref,
  2275. bool vs_in, unsigned *vertex_index_out,
  2276. LLVMValueRef *vertex_index_ref,
  2277. unsigned *const_out, LLVMValueRef *indir_out)
  2278. {
  2279. unsigned const_offset = 0;
  2280. nir_deref *tail = &deref->deref;
  2281. LLVMValueRef offset = NULL;
  2282. if (vertex_index_out != NULL || vertex_index_ref != NULL) {
  2283. tail = tail->child;
  2284. nir_deref_array *deref_array = nir_deref_as_array(tail);
  2285. if (vertex_index_out)
  2286. *vertex_index_out = deref_array->base_offset;
  2287. if (vertex_index_ref) {
  2288. LLVMValueRef vtx = LLVMConstInt(ctx->ac.i32, deref_array->base_offset, false);
  2289. if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
  2290. vtx = LLVMBuildAdd(ctx->ac.builder, vtx, get_src(ctx, deref_array->indirect), "");
  2291. }
  2292. *vertex_index_ref = vtx;
  2293. }
  2294. }
  2295. if (deref->var->data.compact) {
  2296. assert(tail->child->deref_type == nir_deref_type_array);
  2297. assert(glsl_type_is_scalar(glsl_without_array(deref->var->type)));
  2298. nir_deref_array *deref_array = nir_deref_as_array(tail->child);
  2299. /* We always lower indirect dereferences for "compact" array vars. */
  2300. assert(deref_array->deref_array_type == nir_deref_array_type_direct);
  2301. const_offset = deref_array->base_offset;
  2302. goto out;
  2303. }
  2304. while (tail->child != NULL) {
  2305. const struct glsl_type *parent_type = tail->type;
  2306. tail = tail->child;
  2307. if (tail->deref_type == nir_deref_type_array) {
  2308. nir_deref_array *deref_array = nir_deref_as_array(tail);
  2309. LLVMValueRef index, stride, local_offset;
  2310. unsigned size = glsl_count_attribute_slots(tail->type, vs_in);
  2311. const_offset += size * deref_array->base_offset;
  2312. if (deref_array->deref_array_type == nir_deref_array_type_direct)
  2313. continue;
  2314. assert(deref_array->deref_array_type == nir_deref_array_type_indirect);
  2315. index = get_src(ctx, deref_array->indirect);
  2316. stride = LLVMConstInt(ctx->ac.i32, size, 0);
  2317. local_offset = LLVMBuildMul(ctx->ac.builder, stride, index, "");
  2318. if (offset)
  2319. offset = LLVMBuildAdd(ctx->ac.builder, offset, local_offset, "");
  2320. else
  2321. offset = local_offset;
  2322. } else if (tail->deref_type == nir_deref_type_struct) {
  2323. nir_deref_struct *deref_struct = nir_deref_as_struct(tail);
  2324. for (unsigned i = 0; i < deref_struct->index; i++) {
  2325. const struct glsl_type *ft = glsl_get_struct_field(parent_type, i);
  2326. const_offset += glsl_count_attribute_slots(ft, vs_in);
  2327. }
  2328. } else
  2329. unreachable("unsupported deref type");
  2330. }
  2331. out:
  2332. if (const_offset && offset)
  2333. offset = LLVMBuildAdd(ctx->ac.builder, offset,
  2334. LLVMConstInt(ctx->ac.i32, const_offset, 0),
  2335. "");
  2336. *const_out = const_offset;
  2337. *indir_out = offset;
  2338. }
  2339. static LLVMValueRef
  2340. lds_load(struct nir_to_llvm_context *ctx,
  2341. LLVMValueRef dw_addr)
  2342. {
  2343. LLVMValueRef value;
  2344. value = ac_build_load(&ctx->ac, ctx->lds, dw_addr);
  2345. return value;
  2346. }
  2347. static void
  2348. lds_store(struct nir_to_llvm_context *ctx,
  2349. LLVMValueRef dw_addr, LLVMValueRef value)
  2350. {
  2351. value = LLVMBuildBitCast(ctx->builder, value, ctx->i32, "");
  2352. ac_build_indexed_store(&ctx->ac, ctx->lds,
  2353. dw_addr, value);
  2354. }
  2355. /* The offchip buffer layout for TCS->TES is
  2356. *
  2357. * - attribute 0 of patch 0 vertex 0
  2358. * - attribute 0 of patch 0 vertex 1
  2359. * - attribute 0 of patch 0 vertex 2
  2360. * ...
  2361. * - attribute 0 of patch 1 vertex 0
  2362. * - attribute 0 of patch 1 vertex 1
  2363. * ...
  2364. * - attribute 1 of patch 0 vertex 0
  2365. * - attribute 1 of patch 0 vertex 1
  2366. * ...
  2367. * - per patch attribute 0 of patch 0
  2368. * - per patch attribute 0 of patch 1
  2369. * ...
  2370. *
  2371. * Note that every attribute has 4 components.
  2372. */
  2373. static LLVMValueRef get_tcs_tes_buffer_address(struct nir_to_llvm_context *ctx,
  2374. LLVMValueRef vertex_index,
  2375. LLVMValueRef param_index)
  2376. {
  2377. LLVMValueRef base_addr, vertices_per_patch, num_patches, total_vertices;
  2378. LLVMValueRef param_stride, constant16;
  2379. LLVMValueRef rel_patch_id = get_rel_patch_id(ctx);
  2380. vertices_per_patch = unpack_param(&ctx->ac, ctx->tcs_offchip_layout, 9, 6);
  2381. num_patches = unpack_param(&ctx->ac, ctx->tcs_offchip_layout, 0, 9);
  2382. total_vertices = LLVMBuildMul(ctx->builder, vertices_per_patch,
  2383. num_patches, "");
  2384. constant16 = LLVMConstInt(ctx->i32, 16, false);
  2385. if (vertex_index) {
  2386. base_addr = LLVMBuildMul(ctx->builder, rel_patch_id,
  2387. vertices_per_patch, "");
  2388. base_addr = LLVMBuildAdd(ctx->builder, base_addr,
  2389. vertex_index, "");
  2390. param_stride = total_vertices;
  2391. } else {
  2392. base_addr = rel_patch_id;
  2393. param_stride = num_patches;
  2394. }
  2395. base_addr = LLVMBuildAdd(ctx->builder, base_addr,
  2396. LLVMBuildMul(ctx->builder, param_index,
  2397. param_stride, ""), "");
  2398. base_addr = LLVMBuildMul(ctx->builder, base_addr, constant16, "");
  2399. if (!vertex_index) {
  2400. LLVMValueRef patch_data_offset =
  2401. unpack_param(&ctx->ac, ctx->tcs_offchip_layout, 16, 16);
  2402. base_addr = LLVMBuildAdd(ctx->builder, base_addr,
  2403. patch_data_offset, "");
  2404. }
  2405. return base_addr;
  2406. }
  2407. static LLVMValueRef get_tcs_tes_buffer_address_params(struct nir_to_llvm_context *ctx,
  2408. unsigned param,
  2409. unsigned const_index,
  2410. bool is_compact,
  2411. LLVMValueRef vertex_index,
  2412. LLVMValueRef indir_index)
  2413. {
  2414. LLVMValueRef param_index;
  2415. if (indir_index)
  2416. param_index = LLVMBuildAdd(ctx->builder, LLVMConstInt(ctx->i32, param, false),
  2417. indir_index, "");
  2418. else {
  2419. if (const_index && !is_compact)
  2420. param += const_index;
  2421. param_index = LLVMConstInt(ctx->i32, param, false);
  2422. }
  2423. return get_tcs_tes_buffer_address(ctx, vertex_index, param_index);
  2424. }
  2425. static void
  2426. mark_tess_output(struct nir_to_llvm_context *ctx,
  2427. bool is_patch, uint32_t param)
  2428. {
  2429. if (is_patch) {
  2430. ctx->tess_patch_outputs_written |= (1ull << param);
  2431. } else
  2432. ctx->tess_outputs_written |= (1ull << param);
  2433. }
  2434. static LLVMValueRef
  2435. get_dw_address(struct nir_to_llvm_context *ctx,
  2436. LLVMValueRef dw_addr,
  2437. unsigned param,
  2438. unsigned const_index,
  2439. bool compact_const_index,
  2440. LLVMValueRef vertex_index,
  2441. LLVMValueRef stride,
  2442. LLVMValueRef indir_index)
  2443. {
  2444. if (vertex_index) {
  2445. dw_addr = LLVMBuildAdd(ctx->builder, dw_addr,
  2446. LLVMBuildMul(ctx->builder,
  2447. vertex_index,
  2448. stride, ""), "");
  2449. }
  2450. if (indir_index)
  2451. dw_addr = LLVMBuildAdd(ctx->builder, dw_addr,
  2452. LLVMBuildMul(ctx->builder, indir_index,
  2453. LLVMConstInt(ctx->i32, 4, false), ""), "");
  2454. else if (const_index && !compact_const_index)
  2455. dw_addr = LLVMBuildAdd(ctx->builder, dw_addr,
  2456. LLVMConstInt(ctx->i32, const_index, false), "");
  2457. dw_addr = LLVMBuildAdd(ctx->builder, dw_addr,
  2458. LLVMConstInt(ctx->i32, param * 4, false), "");
  2459. if (const_index && compact_const_index)
  2460. dw_addr = LLVMBuildAdd(ctx->builder, dw_addr,
  2461. LLVMConstInt(ctx->i32, const_index, false), "");
  2462. return dw_addr;
  2463. }
  2464. static LLVMValueRef
  2465. load_tcs_input(struct nir_to_llvm_context *ctx,
  2466. nir_intrinsic_instr *instr)
  2467. {
  2468. LLVMValueRef dw_addr, stride;
  2469. unsigned const_index;
  2470. LLVMValueRef vertex_index;
  2471. LLVMValueRef indir_index;
  2472. unsigned param;
  2473. LLVMValueRef value[4], result;
  2474. const bool per_vertex = nir_is_per_vertex_io(instr->variables[0]->var, ctx->stage);
  2475. const bool is_compact = instr->variables[0]->var->data.compact;
  2476. param = shader_io_get_unique_index(instr->variables[0]->var->data.location);
  2477. get_deref_offset(ctx->nir, instr->variables[0],
  2478. false, NULL, per_vertex ? &vertex_index : NULL,
  2479. &const_index, &indir_index);
  2480. stride = unpack_param(&ctx->ac, ctx->tcs_in_layout, 13, 8);
  2481. dw_addr = get_tcs_in_current_patch_offset(ctx);
  2482. dw_addr = get_dw_address(ctx, dw_addr, param, const_index, is_compact, vertex_index, stride,
  2483. indir_index);
  2484. for (unsigned i = 0; i < instr->num_components; i++) {
  2485. value[i] = lds_load(ctx, dw_addr);
  2486. dw_addr = LLVMBuildAdd(ctx->builder, dw_addr,
  2487. ctx->i32one, "");
  2488. }
  2489. result = ac_build_gather_values(&ctx->ac, value, instr->num_components);
  2490. result = LLVMBuildBitCast(ctx->builder, result, get_def_type(ctx->nir, &instr->dest.ssa), "");
  2491. return result;
  2492. }
  2493. static LLVMValueRef
  2494. load_tcs_output(struct nir_to_llvm_context *ctx,
  2495. nir_intrinsic_instr *instr)
  2496. {
  2497. LLVMValueRef dw_addr;
  2498. LLVMValueRef stride = NULL;
  2499. LLVMValueRef value[4], result;
  2500. LLVMValueRef vertex_index = NULL;
  2501. LLVMValueRef indir_index = NULL;
  2502. unsigned const_index = 0;
  2503. unsigned param;
  2504. const bool per_vertex = nir_is_per_vertex_io(instr->variables[0]->var, ctx->stage);
  2505. const bool is_compact = instr->variables[0]->var->data.compact;
  2506. param = shader_io_get_unique_index(instr->variables[0]->var->data.location);
  2507. get_deref_offset(ctx->nir, instr->variables[0],
  2508. false, NULL, per_vertex ? &vertex_index : NULL,
  2509. &const_index, &indir_index);
  2510. if (!instr->variables[0]->var->data.patch) {
  2511. stride = unpack_param(&ctx->ac, ctx->tcs_out_layout, 13, 8);
  2512. dw_addr = get_tcs_out_current_patch_offset(ctx);
  2513. } else {
  2514. dw_addr = get_tcs_out_current_patch_data_offset(ctx);
  2515. }
  2516. dw_addr = get_dw_address(ctx, dw_addr, param, const_index, is_compact, vertex_index, stride,
  2517. indir_index);
  2518. for (unsigned i = 0; i < instr->num_components; i++) {
  2519. value[i] = lds_load(ctx, dw_addr);
  2520. dw_addr = LLVMBuildAdd(ctx->builder, dw_addr,
  2521. ctx->i32one, "");
  2522. }
  2523. result = ac_build_gather_values(&ctx->ac, value, instr->num_components);
  2524. result = LLVMBuildBitCast(ctx->builder, result, get_def_type(ctx->nir, &instr->dest.ssa), "");
  2525. return result;
  2526. }
  2527. static void
  2528. store_tcs_output(struct nir_to_llvm_context *ctx,
  2529. nir_intrinsic_instr *instr,
  2530. LLVMValueRef src,
  2531. unsigned writemask)
  2532. {
  2533. LLVMValueRef dw_addr;
  2534. LLVMValueRef stride = NULL;
  2535. LLVMValueRef buf_addr = NULL;
  2536. LLVMValueRef vertex_index = NULL;
  2537. LLVMValueRef indir_index = NULL;
  2538. unsigned const_index = 0;
  2539. unsigned param;
  2540. const bool per_vertex = nir_is_per_vertex_io(instr->variables[0]->var, ctx->stage);
  2541. const bool is_compact = instr->variables[0]->var->data.compact;
  2542. get_deref_offset(ctx->nir, instr->variables[0],
  2543. false, NULL, per_vertex ? &vertex_index : NULL,
  2544. &const_index, &indir_index);
  2545. param = shader_io_get_unique_index(instr->variables[0]->var->data.location);
  2546. if (instr->variables[0]->var->data.location == VARYING_SLOT_CLIP_DIST0 &&
  2547. is_compact && const_index > 3) {
  2548. const_index -= 3;
  2549. param++;
  2550. }
  2551. if (!instr->variables[0]->var->data.patch) {
  2552. stride = unpack_param(&ctx->ac, ctx->tcs_out_layout, 13, 8);
  2553. dw_addr = get_tcs_out_current_patch_offset(ctx);
  2554. } else {
  2555. dw_addr = get_tcs_out_current_patch_data_offset(ctx);
  2556. }
  2557. mark_tess_output(ctx, instr->variables[0]->var->data.patch, param);
  2558. dw_addr = get_dw_address(ctx, dw_addr, param, const_index, is_compact, vertex_index, stride,
  2559. indir_index);
  2560. buf_addr = get_tcs_tes_buffer_address_params(ctx, param, const_index, is_compact,
  2561. vertex_index, indir_index);
  2562. bool is_tess_factor = false;
  2563. if (instr->variables[0]->var->data.location == VARYING_SLOT_TESS_LEVEL_INNER ||
  2564. instr->variables[0]->var->data.location == VARYING_SLOT_TESS_LEVEL_OUTER)
  2565. is_tess_factor = true;
  2566. unsigned base = is_compact ? const_index : 0;
  2567. for (unsigned chan = 0; chan < 8; chan++) {
  2568. if (!(writemask & (1 << chan)))
  2569. continue;
  2570. LLVMValueRef value = llvm_extract_elem(&ctx->ac, src, chan);
  2571. lds_store(ctx, dw_addr, value);
  2572. if (!is_tess_factor && writemask != 0xF)
  2573. ac_build_buffer_store_dword(&ctx->ac, ctx->hs_ring_tess_offchip, value, 1,
  2574. buf_addr, ctx->oc_lds,
  2575. 4 * (base + chan), 1, 0, true, false);
  2576. dw_addr = LLVMBuildAdd(ctx->builder, dw_addr,
  2577. ctx->i32one, "");
  2578. }
  2579. if (writemask == 0xF) {
  2580. ac_build_buffer_store_dword(&ctx->ac, ctx->hs_ring_tess_offchip, src, 4,
  2581. buf_addr, ctx->oc_lds,
  2582. (base * 4), 1, 0, true, false);
  2583. }
  2584. }
  2585. static LLVMValueRef
  2586. load_tes_input(struct nir_to_llvm_context *ctx,
  2587. const nir_intrinsic_instr *instr)
  2588. {
  2589. LLVMValueRef buf_addr;
  2590. LLVMValueRef result;
  2591. LLVMValueRef vertex_index = NULL;
  2592. LLVMValueRef indir_index = NULL;
  2593. unsigned const_index = 0;
  2594. unsigned param;
  2595. const bool per_vertex = nir_is_per_vertex_io(instr->variables[0]->var, ctx->stage);
  2596. const bool is_compact = instr->variables[0]->var->data.compact;
  2597. get_deref_offset(ctx->nir, instr->variables[0],
  2598. false, NULL, per_vertex ? &vertex_index : NULL,
  2599. &const_index, &indir_index);
  2600. param = shader_io_get_unique_index(instr->variables[0]->var->data.location);
  2601. if (instr->variables[0]->var->data.location == VARYING_SLOT_CLIP_DIST0 &&
  2602. is_compact && const_index > 3) {
  2603. const_index -= 3;
  2604. param++;
  2605. }
  2606. buf_addr = get_tcs_tes_buffer_address_params(ctx, param, const_index,
  2607. is_compact, vertex_index, indir_index);
  2608. result = ac_build_buffer_load(&ctx->ac, ctx->hs_ring_tess_offchip, instr->num_components, NULL,
  2609. buf_addr, ctx->oc_lds, is_compact ? (4 * const_index) : 0, 1, 0, true, false);
  2610. result = trim_vector(&ctx->ac, result, instr->num_components);
  2611. result = LLVMBuildBitCast(ctx->builder, result, get_def_type(ctx->nir, &instr->dest.ssa), "");
  2612. return result;
  2613. }
  2614. static LLVMValueRef
  2615. load_gs_input(struct nir_to_llvm_context *ctx,
  2616. nir_intrinsic_instr *instr)
  2617. {
  2618. LLVMValueRef indir_index, vtx_offset;
  2619. unsigned const_index;
  2620. LLVMValueRef args[9];
  2621. unsigned param, vtx_offset_param;
  2622. LLVMValueRef value[4], result;
  2623. unsigned vertex_index;
  2624. get_deref_offset(ctx->nir, instr->variables[0],
  2625. false, &vertex_index, NULL,
  2626. &const_index, &indir_index);
  2627. vtx_offset_param = vertex_index;
  2628. assert(vtx_offset_param < 6);
  2629. vtx_offset = LLVMBuildMul(ctx->builder, ctx->gs_vtx_offset[vtx_offset_param],
  2630. LLVMConstInt(ctx->i32, 4, false), "");
  2631. param = shader_io_get_unique_index(instr->variables[0]->var->data.location);
  2632. for (unsigned i = 0; i < instr->num_components; i++) {
  2633. args[0] = ctx->esgs_ring;
  2634. args[1] = vtx_offset;
  2635. args[2] = LLVMConstInt(ctx->i32, (param * 4 + i + const_index) * 256, false);
  2636. args[3] = ctx->i32zero;
  2637. args[4] = ctx->i32one; /* OFFEN */
  2638. args[5] = ctx->i32zero; /* IDXEN */
  2639. args[6] = ctx->i32one; /* GLC */
  2640. args[7] = ctx->i32zero; /* SLC */
  2641. args[8] = ctx->i32zero; /* TFE */
  2642. value[i] = ac_build_intrinsic(&ctx->ac, "llvm.SI.buffer.load.dword.i32.i32",
  2643. ctx->i32, args, 9,
  2644. AC_FUNC_ATTR_READONLY |
  2645. AC_FUNC_ATTR_LEGACY);
  2646. }
  2647. result = ac_build_gather_values(&ctx->ac, value, instr->num_components);
  2648. return result;
  2649. }
  2650. static LLVMValueRef
  2651. build_gep_for_deref(struct ac_nir_context *ctx,
  2652. nir_deref_var *deref)
  2653. {
  2654. struct hash_entry *entry = _mesa_hash_table_search(ctx->vars, deref->var);
  2655. assert(entry->data);
  2656. LLVMValueRef val = entry->data;
  2657. nir_deref *tail = deref->deref.child;
  2658. while (tail != NULL) {
  2659. LLVMValueRef offset;
  2660. switch (tail->deref_type) {
  2661. case nir_deref_type_array: {
  2662. nir_deref_array *array = nir_deref_as_array(tail);
  2663. offset = LLVMConstInt(ctx->ac.i32, array->base_offset, 0);
  2664. if (array->deref_array_type ==
  2665. nir_deref_array_type_indirect) {
  2666. offset = LLVMBuildAdd(ctx->ac.builder, offset,
  2667. get_src(ctx,
  2668. array->indirect),
  2669. "");
  2670. }
  2671. break;
  2672. }
  2673. case nir_deref_type_struct: {
  2674. nir_deref_struct *deref_struct =
  2675. nir_deref_as_struct(tail);
  2676. offset = LLVMConstInt(ctx->ac.i32,
  2677. deref_struct->index, 0);
  2678. break;
  2679. }
  2680. default:
  2681. unreachable("bad deref type");
  2682. }
  2683. val = ac_build_gep0(&ctx->ac, val, offset);
  2684. tail = tail->child;
  2685. }
  2686. return val;
  2687. }
  2688. static LLVMValueRef visit_load_var(struct ac_nir_context *ctx,
  2689. nir_intrinsic_instr *instr)
  2690. {
  2691. LLVMValueRef values[8];
  2692. int idx = instr->variables[0]->var->data.driver_location;
  2693. int ve = instr->dest.ssa.num_components;
  2694. LLVMValueRef indir_index;
  2695. LLVMValueRef ret;
  2696. unsigned const_index;
  2697. bool vs_in = ctx->stage == MESA_SHADER_VERTEX &&
  2698. instr->variables[0]->var->data.mode == nir_var_shader_in;
  2699. get_deref_offset(ctx, instr->variables[0], vs_in, NULL, NULL,
  2700. &const_index, &indir_index);
  2701. if (instr->dest.ssa.bit_size == 64)
  2702. ve *= 2;
  2703. switch (instr->variables[0]->var->data.mode) {
  2704. case nir_var_shader_in:
  2705. if (ctx->stage == MESA_SHADER_TESS_CTRL)
  2706. return load_tcs_input(ctx->nctx, instr);
  2707. if (ctx->stage == MESA_SHADER_TESS_EVAL)
  2708. return load_tes_input(ctx->nctx, instr);
  2709. if (ctx->stage == MESA_SHADER_GEOMETRY) {
  2710. return load_gs_input(ctx->nctx, instr);
  2711. }
  2712. for (unsigned chan = 0; chan < ve; chan++) {
  2713. if (indir_index) {
  2714. unsigned count = glsl_count_attribute_slots(
  2715. instr->variables[0]->var->type,
  2716. ctx->stage == MESA_SHADER_VERTEX);
  2717. count -= chan / 4;
  2718. LLVMValueRef tmp_vec = ac_build_gather_values_extended(
  2719. &ctx->ac, ctx->abi->inputs + idx + chan, count,
  2720. 4, false, true);
  2721. values[chan] = LLVMBuildExtractElement(ctx->ac.builder,
  2722. tmp_vec,
  2723. indir_index, "");
  2724. } else
  2725. values[chan] = ctx->abi->inputs[idx + chan + const_index * 4];
  2726. }
  2727. break;
  2728. case nir_var_local:
  2729. for (unsigned chan = 0; chan < ve; chan++) {
  2730. if (indir_index) {
  2731. unsigned count = glsl_count_attribute_slots(
  2732. instr->variables[0]->var->type, false);
  2733. count -= chan / 4;
  2734. LLVMValueRef tmp_vec = ac_build_gather_values_extended(
  2735. &ctx->ac, ctx->locals + idx + chan, count,
  2736. 4, true, true);
  2737. values[chan] = LLVMBuildExtractElement(ctx->ac.builder,
  2738. tmp_vec,
  2739. indir_index, "");
  2740. } else {
  2741. values[chan] = LLVMBuildLoad(ctx->ac.builder, ctx->locals[idx + chan + const_index * 4], "");
  2742. }
  2743. }
  2744. break;
  2745. case nir_var_shared: {
  2746. LLVMValueRef address = build_gep_for_deref(ctx,
  2747. instr->variables[0]);
  2748. LLVMValueRef val = LLVMBuildLoad(ctx->ac.builder, address, "");
  2749. return LLVMBuildBitCast(ctx->ac.builder, val,
  2750. get_def_type(ctx, &instr->dest.ssa),
  2751. "");
  2752. }
  2753. case nir_var_shader_out:
  2754. if (ctx->stage == MESA_SHADER_TESS_CTRL)
  2755. return load_tcs_output(ctx->nctx, instr);
  2756. for (unsigned chan = 0; chan < ve; chan++) {
  2757. if (indir_index) {
  2758. unsigned count = glsl_count_attribute_slots(
  2759. instr->variables[0]->var->type, false);
  2760. count -= chan / 4;
  2761. LLVMValueRef tmp_vec = ac_build_gather_values_extended(
  2762. &ctx->ac, ctx->outputs + idx + chan, count,
  2763. 4, true, true);
  2764. values[chan] = LLVMBuildExtractElement(ctx->ac.builder,
  2765. tmp_vec,
  2766. indir_index, "");
  2767. } else {
  2768. values[chan] = LLVMBuildLoad(ctx->ac.builder,
  2769. ctx->outputs[idx + chan + const_index * 4],
  2770. "");
  2771. }
  2772. }
  2773. break;
  2774. default:
  2775. unreachable("unhandle variable mode");
  2776. }
  2777. ret = ac_build_gather_values(&ctx->ac, values, ve);
  2778. return LLVMBuildBitCast(ctx->ac.builder, ret, get_def_type(ctx, &instr->dest.ssa), "");
  2779. }
  2780. static void
  2781. visit_store_var(struct ac_nir_context *ctx,
  2782. nir_intrinsic_instr *instr)
  2783. {
  2784. LLVMValueRef temp_ptr, value;
  2785. int idx = instr->variables[0]->var->data.driver_location;
  2786. LLVMValueRef src = ac_to_float(&ctx->ac, get_src(ctx, instr->src[0]));
  2787. int writemask = instr->const_index[0];
  2788. LLVMValueRef indir_index;
  2789. unsigned const_index;
  2790. get_deref_offset(ctx, instr->variables[0], false,
  2791. NULL, NULL, &const_index, &indir_index);
  2792. if (get_elem_bits(&ctx->ac, LLVMTypeOf(src)) == 64) {
  2793. int old_writemask = writemask;
  2794. src = LLVMBuildBitCast(ctx->ac.builder, src,
  2795. LLVMVectorType(ctx->ac.f32, get_llvm_num_components(src) * 2),
  2796. "");
  2797. writemask = 0;
  2798. for (unsigned chan = 0; chan < 4; chan++) {
  2799. if (old_writemask & (1 << chan))
  2800. writemask |= 3u << (2 * chan);
  2801. }
  2802. }
  2803. switch (instr->variables[0]->var->data.mode) {
  2804. case nir_var_shader_out:
  2805. if (ctx->stage == MESA_SHADER_TESS_CTRL) {
  2806. store_tcs_output(ctx->nctx, instr, src, writemask);
  2807. return;
  2808. }
  2809. for (unsigned chan = 0; chan < 8; chan++) {
  2810. int stride = 4;
  2811. if (!(writemask & (1 << chan)))
  2812. continue;
  2813. value = llvm_extract_elem(&ctx->ac, src, chan);
  2814. if (instr->variables[0]->var->data.compact)
  2815. stride = 1;
  2816. if (indir_index) {
  2817. unsigned count = glsl_count_attribute_slots(
  2818. instr->variables[0]->var->type, false);
  2819. count -= chan / 4;
  2820. LLVMValueRef tmp_vec = ac_build_gather_values_extended(
  2821. &ctx->ac, ctx->outputs + idx + chan, count,
  2822. stride, true, true);
  2823. tmp_vec = LLVMBuildInsertElement(ctx->ac.builder, tmp_vec,
  2824. value, indir_index, "");
  2825. build_store_values_extended(&ctx->ac, ctx->outputs + idx + chan,
  2826. count, stride, tmp_vec);
  2827. } else {
  2828. temp_ptr = ctx->outputs[idx + chan + const_index * stride];
  2829. LLVMBuildStore(ctx->ac.builder, value, temp_ptr);
  2830. }
  2831. }
  2832. break;
  2833. case nir_var_local:
  2834. for (unsigned chan = 0; chan < 8; chan++) {
  2835. if (!(writemask & (1 << chan)))
  2836. continue;
  2837. value = llvm_extract_elem(&ctx->ac, src, chan);
  2838. if (indir_index) {
  2839. unsigned count = glsl_count_attribute_slots(
  2840. instr->variables[0]->var->type, false);
  2841. count -= chan / 4;
  2842. LLVMValueRef tmp_vec = ac_build_gather_values_extended(
  2843. &ctx->ac, ctx->locals + idx + chan, count,
  2844. 4, true, true);
  2845. tmp_vec = LLVMBuildInsertElement(ctx->ac.builder, tmp_vec,
  2846. value, indir_index, "");
  2847. build_store_values_extended(&ctx->ac, ctx->locals + idx + chan,
  2848. count, 4, tmp_vec);
  2849. } else {
  2850. temp_ptr = ctx->locals[idx + chan + const_index * 4];
  2851. LLVMBuildStore(ctx->ac.builder, value, temp_ptr);
  2852. }
  2853. }
  2854. break;
  2855. case nir_var_shared: {
  2856. int writemask = instr->const_index[0];
  2857. LLVMValueRef address = build_gep_for_deref(ctx,
  2858. instr->variables[0]);
  2859. LLVMValueRef val = get_src(ctx, instr->src[0]);
  2860. unsigned components =
  2861. glsl_get_vector_elements(
  2862. nir_deref_tail(&instr->variables[0]->deref)->type);
  2863. if (writemask == (1 << components) - 1) {
  2864. val = LLVMBuildBitCast(
  2865. ctx->ac.builder, val,
  2866. LLVMGetElementType(LLVMTypeOf(address)), "");
  2867. LLVMBuildStore(ctx->ac.builder, val, address);
  2868. } else {
  2869. for (unsigned chan = 0; chan < 4; chan++) {
  2870. if (!(writemask & (1 << chan)))
  2871. continue;
  2872. LLVMValueRef ptr =
  2873. LLVMBuildStructGEP(ctx->ac.builder,
  2874. address, chan, "");
  2875. LLVMValueRef src = llvm_extract_elem(&ctx->ac, val,
  2876. chan);
  2877. src = LLVMBuildBitCast(
  2878. ctx->ac.builder, src,
  2879. LLVMGetElementType(LLVMTypeOf(ptr)), "");
  2880. LLVMBuildStore(ctx->ac.builder, src, ptr);
  2881. }
  2882. }
  2883. break;
  2884. }
  2885. default:
  2886. break;
  2887. }
  2888. }
  2889. static int image_type_to_components_count(enum glsl_sampler_dim dim, bool array)
  2890. {
  2891. switch (dim) {
  2892. case GLSL_SAMPLER_DIM_BUF:
  2893. return 1;
  2894. case GLSL_SAMPLER_DIM_1D:
  2895. return array ? 2 : 1;
  2896. case GLSL_SAMPLER_DIM_2D:
  2897. return array ? 3 : 2;
  2898. case GLSL_SAMPLER_DIM_MS:
  2899. return array ? 4 : 3;
  2900. case GLSL_SAMPLER_DIM_3D:
  2901. case GLSL_SAMPLER_DIM_CUBE:
  2902. return 3;
  2903. case GLSL_SAMPLER_DIM_RECT:
  2904. case GLSL_SAMPLER_DIM_SUBPASS:
  2905. return 2;
  2906. case GLSL_SAMPLER_DIM_SUBPASS_MS:
  2907. return 3;
  2908. default:
  2909. break;
  2910. }
  2911. return 0;
  2912. }
  2913. /* Adjust the sample index according to FMASK.
  2914. *
  2915. * For uncompressed MSAA surfaces, FMASK should return 0x76543210,
  2916. * which is the identity mapping. Each nibble says which physical sample
  2917. * should be fetched to get that sample.
  2918. *
  2919. * For example, 0x11111100 means there are only 2 samples stored and
  2920. * the second sample covers 3/4 of the pixel. When reading samples 0
  2921. * and 1, return physical sample 0 (determined by the first two 0s
  2922. * in FMASK), otherwise return physical sample 1.
  2923. *
  2924. * The sample index should be adjusted as follows:
  2925. * sample_index = (fmask >> (sample_index * 4)) & 0xF;
  2926. */
  2927. static LLVMValueRef adjust_sample_index_using_fmask(struct ac_llvm_context *ctx,
  2928. LLVMValueRef coord_x, LLVMValueRef coord_y,
  2929. LLVMValueRef coord_z,
  2930. LLVMValueRef sample_index,
  2931. LLVMValueRef fmask_desc_ptr)
  2932. {
  2933. LLVMValueRef fmask_load_address[4];
  2934. LLVMValueRef res;
  2935. fmask_load_address[0] = coord_x;
  2936. fmask_load_address[1] = coord_y;
  2937. if (coord_z) {
  2938. fmask_load_address[2] = coord_z;
  2939. fmask_load_address[3] = LLVMGetUndef(ctx->i32);
  2940. }
  2941. struct ac_image_args args = {0};
  2942. args.opcode = ac_image_load;
  2943. args.da = coord_z ? true : false;
  2944. args.resource = fmask_desc_ptr;
  2945. args.dmask = 0xf;
  2946. args.addr = ac_build_gather_values(ctx, fmask_load_address, coord_z ? 4 : 2);
  2947. res = ac_build_image_opcode(ctx, &args);
  2948. res = ac_to_integer(ctx, res);
  2949. LLVMValueRef four = LLVMConstInt(ctx->i32, 4, false);
  2950. LLVMValueRef F = LLVMConstInt(ctx->i32, 0xf, false);
  2951. LLVMValueRef fmask = LLVMBuildExtractElement(ctx->builder,
  2952. res,
  2953. ctx->i32_0, "");
  2954. LLVMValueRef sample_index4 =
  2955. LLVMBuildMul(ctx->builder, sample_index, four, "");
  2956. LLVMValueRef shifted_fmask =
  2957. LLVMBuildLShr(ctx->builder, fmask, sample_index4, "");
  2958. LLVMValueRef final_sample =
  2959. LLVMBuildAnd(ctx->builder, shifted_fmask, F, "");
  2960. /* Don't rewrite the sample index if WORD1.DATA_FORMAT of the FMASK
  2961. * resource descriptor is 0 (invalid),
  2962. */
  2963. LLVMValueRef fmask_desc =
  2964. LLVMBuildBitCast(ctx->builder, fmask_desc_ptr,
  2965. ctx->v8i32, "");
  2966. LLVMValueRef fmask_word1 =
  2967. LLVMBuildExtractElement(ctx->builder, fmask_desc,
  2968. ctx->i32_1, "");
  2969. LLVMValueRef word1_is_nonzero =
  2970. LLVMBuildICmp(ctx->builder, LLVMIntNE,
  2971. fmask_word1, ctx->i32_0, "");
  2972. /* Replace the MSAA sample index. */
  2973. sample_index =
  2974. LLVMBuildSelect(ctx->builder, word1_is_nonzero,
  2975. final_sample, sample_index, "");
  2976. return sample_index;
  2977. }
  2978. static LLVMValueRef get_image_coords(struct ac_nir_context *ctx,
  2979. const nir_intrinsic_instr *instr)
  2980. {
  2981. const struct glsl_type *type = instr->variables[0]->var->type;
  2982. if(instr->variables[0]->deref.child)
  2983. type = instr->variables[0]->deref.child->type;
  2984. LLVMValueRef src0 = get_src(ctx, instr->src[0]);
  2985. LLVMValueRef coords[4];
  2986. LLVMValueRef masks[] = {
  2987. LLVMConstInt(ctx->ac.i32, 0, false), LLVMConstInt(ctx->ac.i32, 1, false),
  2988. LLVMConstInt(ctx->ac.i32, 2, false), LLVMConstInt(ctx->ac.i32, 3, false),
  2989. };
  2990. LLVMValueRef res;
  2991. LLVMValueRef sample_index = llvm_extract_elem(&ctx->ac, get_src(ctx, instr->src[1]), 0);
  2992. int count;
  2993. enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
  2994. bool is_array = glsl_sampler_type_is_array(type);
  2995. bool add_frag_pos = (dim == GLSL_SAMPLER_DIM_SUBPASS ||
  2996. dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
  2997. bool is_ms = (dim == GLSL_SAMPLER_DIM_MS ||
  2998. dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
  2999. bool gfx9_1d = ctx->ac.chip_class >= GFX9 && dim == GLSL_SAMPLER_DIM_1D;
  3000. count = image_type_to_components_count(dim, is_array);
  3001. if (is_ms) {
  3002. LLVMValueRef fmask_load_address[3];
  3003. int chan;
  3004. fmask_load_address[0] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[0], "");
  3005. fmask_load_address[1] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[1], "");
  3006. if (is_array)
  3007. fmask_load_address[2] = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[2], "");
  3008. else
  3009. fmask_load_address[2] = NULL;
  3010. if (add_frag_pos) {
  3011. for (chan = 0; chan < 2; ++chan)
  3012. fmask_load_address[chan] =
  3013. LLVMBuildAdd(ctx->ac.builder, fmask_load_address[chan],
  3014. LLVMBuildFPToUI(ctx->ac.builder, ctx->abi->frag_pos[chan],
  3015. ctx->ac.i32, ""), "");
  3016. fmask_load_address[2] = ac_to_integer(&ctx->ac, ctx->abi->inputs[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER, 0)]);
  3017. }
  3018. sample_index = adjust_sample_index_using_fmask(&ctx->ac,
  3019. fmask_load_address[0],
  3020. fmask_load_address[1],
  3021. fmask_load_address[2],
  3022. sample_index,
  3023. get_sampler_desc(ctx, instr->variables[0], AC_DESC_FMASK, true, false));
  3024. }
  3025. if (count == 1 && !gfx9_1d) {
  3026. if (instr->src[0].ssa->num_components)
  3027. res = LLVMBuildExtractElement(ctx->ac.builder, src0, masks[0], "");
  3028. else
  3029. res = src0;
  3030. } else {
  3031. int chan;
  3032. if (is_ms)
  3033. count--;
  3034. for (chan = 0; chan < count; ++chan) {
  3035. coords[chan] = llvm_extract_elem(&ctx->ac, src0, chan);
  3036. }
  3037. if (add_frag_pos) {
  3038. for (chan = 0; chan < 2; ++chan)
  3039. coords[chan] = LLVMBuildAdd(ctx->ac.builder, coords[chan], LLVMBuildFPToUI(ctx->ac.builder, ctx->abi->frag_pos[chan],
  3040. ctx->ac.i32, ""), "");
  3041. coords[2] = ac_to_integer(&ctx->ac, ctx->abi->inputs[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER, 0)]);
  3042. count++;
  3043. }
  3044. if (gfx9_1d) {
  3045. if (is_array) {
  3046. coords[2] = coords[1];
  3047. coords[1] = ctx->ac.i32_0;
  3048. } else
  3049. coords[1] = ctx->ac.i32_0;
  3050. count++;
  3051. }
  3052. if (is_ms) {
  3053. coords[count] = sample_index;
  3054. count++;
  3055. }
  3056. if (count == 3) {
  3057. coords[3] = LLVMGetUndef(ctx->ac.i32);
  3058. count = 4;
  3059. }
  3060. res = ac_build_gather_values(&ctx->ac, coords, count);
  3061. }
  3062. return res;
  3063. }
  3064. static LLVMValueRef visit_image_load(struct ac_nir_context *ctx,
  3065. const nir_intrinsic_instr *instr)
  3066. {
  3067. LLVMValueRef params[7];
  3068. LLVMValueRef res;
  3069. char intrinsic_name[64];
  3070. const nir_variable *var = instr->variables[0]->var;
  3071. const struct glsl_type *type = var->type;
  3072. LLVMValueRef i1false = LLVMConstInt(ctx->ac.i1, 0, false);
  3073. LLVMValueRef i1true = LLVMConstInt(ctx->ac.i1, 1, false);
  3074. if(instr->variables[0]->deref.child)
  3075. type = instr->variables[0]->deref.child->type;
  3076. type = glsl_without_array(type);
  3077. if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
  3078. params[0] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_BUFFER, true, false);
  3079. params[1] = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[0]),
  3080. ctx->ac.i32_0, ""); /* vindex */
  3081. params[2] = ctx->ac.i32_0; /* voffset */
  3082. params[3] = i1false; /* glc */
  3083. params[4] = i1false; /* slc */
  3084. res = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.buffer.load.format.v4f32", ctx->ac.v4f32,
  3085. params, 5, 0);
  3086. res = trim_vector(&ctx->ac, res, instr->dest.ssa.num_components);
  3087. res = ac_to_integer(&ctx->ac, res);
  3088. } else {
  3089. bool is_da = glsl_sampler_type_is_array(type) ||
  3090. glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE ||
  3091. glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_SUBPASS ||
  3092. glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_SUBPASS_MS;
  3093. LLVMValueRef da = is_da ? i1true : i1false;
  3094. LLVMValueRef glc = i1false;
  3095. LLVMValueRef slc = i1false;
  3096. params[0] = get_image_coords(ctx, instr);
  3097. params[1] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_IMAGE, true, false);
  3098. params[2] = LLVMConstInt(ctx->ac.i32, 15, false); /* dmask */
  3099. if (HAVE_LLVM <= 0x0309) {
  3100. params[3] = i1false; /* r128 */
  3101. params[4] = da;
  3102. params[5] = glc;
  3103. params[6] = slc;
  3104. } else {
  3105. LLVMValueRef lwe = i1false;
  3106. params[3] = glc;
  3107. params[4] = slc;
  3108. params[5] = lwe;
  3109. params[6] = da;
  3110. }
  3111. ac_get_image_intr_name("llvm.amdgcn.image.load",
  3112. ctx->ac.v4f32, /* vdata */
  3113. LLVMTypeOf(params[0]), /* coords */
  3114. LLVMTypeOf(params[1]), /* rsrc */
  3115. intrinsic_name, sizeof(intrinsic_name));
  3116. res = ac_build_intrinsic(&ctx->ac, intrinsic_name, ctx->ac.v4f32,
  3117. params, 7, AC_FUNC_ATTR_READONLY);
  3118. }
  3119. return ac_to_integer(&ctx->ac, res);
  3120. }
  3121. static void visit_image_store(struct ac_nir_context *ctx,
  3122. nir_intrinsic_instr *instr)
  3123. {
  3124. LLVMValueRef params[8];
  3125. char intrinsic_name[64];
  3126. const nir_variable *var = instr->variables[0]->var;
  3127. const struct glsl_type *type = glsl_without_array(var->type);
  3128. LLVMValueRef i1false = LLVMConstInt(ctx->ac.i1, 0, false);
  3129. LLVMValueRef i1true = LLVMConstInt(ctx->ac.i1, 1, false);
  3130. LLVMValueRef glc = i1false;
  3131. bool force_glc = ctx->ac.chip_class == SI;
  3132. if (force_glc)
  3133. glc = i1true;
  3134. if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
  3135. params[0] = ac_to_float(&ctx->ac, get_src(ctx, instr->src[2])); /* data */
  3136. params[1] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_BUFFER, true, true);
  3137. params[2] = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[0]),
  3138. ctx->ac.i32_0, ""); /* vindex */
  3139. params[3] = ctx->ac.i32_0; /* voffset */
  3140. params[4] = glc; /* glc */
  3141. params[5] = i1false; /* slc */
  3142. ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.buffer.store.format.v4f32", ctx->ac.voidt,
  3143. params, 6, 0);
  3144. } else {
  3145. bool is_da = glsl_sampler_type_is_array(type) ||
  3146. glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE;
  3147. LLVMValueRef da = is_da ? i1true : i1false;
  3148. LLVMValueRef slc = i1false;
  3149. params[0] = ac_to_float(&ctx->ac, get_src(ctx, instr->src[2]));
  3150. params[1] = get_image_coords(ctx, instr); /* coords */
  3151. params[2] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_IMAGE, true, true);
  3152. params[3] = LLVMConstInt(ctx->ac.i32, 15, false); /* dmask */
  3153. if (HAVE_LLVM <= 0x0309) {
  3154. params[4] = i1false; /* r128 */
  3155. params[5] = da;
  3156. params[6] = glc;
  3157. params[7] = slc;
  3158. } else {
  3159. LLVMValueRef lwe = i1false;
  3160. params[4] = glc;
  3161. params[5] = slc;
  3162. params[6] = lwe;
  3163. params[7] = da;
  3164. }
  3165. ac_get_image_intr_name("llvm.amdgcn.image.store",
  3166. LLVMTypeOf(params[0]), /* vdata */
  3167. LLVMTypeOf(params[1]), /* coords */
  3168. LLVMTypeOf(params[2]), /* rsrc */
  3169. intrinsic_name, sizeof(intrinsic_name));
  3170. ac_build_intrinsic(&ctx->ac, intrinsic_name, ctx->ac.voidt,
  3171. params, 8, 0);
  3172. }
  3173. }
  3174. static LLVMValueRef visit_image_atomic(struct ac_nir_context *ctx,
  3175. const nir_intrinsic_instr *instr)
  3176. {
  3177. LLVMValueRef params[7];
  3178. int param_count = 0;
  3179. const nir_variable *var = instr->variables[0]->var;
  3180. const char *atomic_name;
  3181. char intrinsic_name[41];
  3182. const struct glsl_type *type = glsl_without_array(var->type);
  3183. LLVMValueRef i1false = LLVMConstInt(ctx->ac.i1, 0, false);
  3184. LLVMValueRef i1true = LLVMConstInt(ctx->ac.i1, 1, false);
  3185. MAYBE_UNUSED int length;
  3186. switch (instr->intrinsic) {
  3187. case nir_intrinsic_image_atomic_add:
  3188. atomic_name = "add";
  3189. break;
  3190. case nir_intrinsic_image_atomic_min:
  3191. atomic_name = "smin";
  3192. break;
  3193. case nir_intrinsic_image_atomic_max:
  3194. atomic_name = "smax";
  3195. break;
  3196. case nir_intrinsic_image_atomic_and:
  3197. atomic_name = "and";
  3198. break;
  3199. case nir_intrinsic_image_atomic_or:
  3200. atomic_name = "or";
  3201. break;
  3202. case nir_intrinsic_image_atomic_xor:
  3203. atomic_name = "xor";
  3204. break;
  3205. case nir_intrinsic_image_atomic_exchange:
  3206. atomic_name = "swap";
  3207. break;
  3208. case nir_intrinsic_image_atomic_comp_swap:
  3209. atomic_name = "cmpswap";
  3210. break;
  3211. default:
  3212. abort();
  3213. }
  3214. if (instr->intrinsic == nir_intrinsic_image_atomic_comp_swap)
  3215. params[param_count++] = get_src(ctx, instr->src[3]);
  3216. params[param_count++] = get_src(ctx, instr->src[2]);
  3217. if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF) {
  3218. params[param_count++] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_BUFFER,
  3219. true, true);
  3220. params[param_count++] = LLVMBuildExtractElement(ctx->ac.builder, get_src(ctx, instr->src[0]),
  3221. ctx->ac.i32_0, ""); /* vindex */
  3222. params[param_count++] = ctx->ac.i32_0; /* voffset */
  3223. params[param_count++] = i1false; /* slc */
  3224. length = snprintf(intrinsic_name, sizeof(intrinsic_name),
  3225. "llvm.amdgcn.buffer.atomic.%s", atomic_name);
  3226. } else {
  3227. char coords_type[8];
  3228. bool da = glsl_sampler_type_is_array(type) ||
  3229. glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE;
  3230. LLVMValueRef coords = params[param_count++] = get_image_coords(ctx, instr);
  3231. params[param_count++] = get_sampler_desc(ctx, instr->variables[0], AC_DESC_IMAGE,
  3232. true, true);
  3233. params[param_count++] = i1false; /* r128 */
  3234. params[param_count++] = da ? i1true : i1false; /* da */
  3235. params[param_count++] = i1false; /* slc */
  3236. build_int_type_name(LLVMTypeOf(coords),
  3237. coords_type, sizeof(coords_type));
  3238. length = snprintf(intrinsic_name, sizeof(intrinsic_name),
  3239. "llvm.amdgcn.image.atomic.%s.%s", atomic_name, coords_type);
  3240. }
  3241. assert(length < sizeof(intrinsic_name));
  3242. return ac_build_intrinsic(&ctx->ac, intrinsic_name, ctx->ac.i32, params, param_count, 0);
  3243. }
  3244. static LLVMValueRef visit_image_size(struct ac_nir_context *ctx,
  3245. const nir_intrinsic_instr *instr)
  3246. {
  3247. LLVMValueRef res;
  3248. const nir_variable *var = instr->variables[0]->var;
  3249. const struct glsl_type *type = instr->variables[0]->var->type;
  3250. bool da = glsl_sampler_type_is_array(var->type) ||
  3251. glsl_get_sampler_dim(var->type) == GLSL_SAMPLER_DIM_CUBE;
  3252. if(instr->variables[0]->deref.child)
  3253. type = instr->variables[0]->deref.child->type;
  3254. if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_BUF)
  3255. return get_buffer_size(ctx,
  3256. get_sampler_desc(ctx, instr->variables[0],
  3257. AC_DESC_BUFFER, true, false), true);
  3258. struct ac_image_args args = { 0 };
  3259. args.da = da;
  3260. args.dmask = 0xf;
  3261. args.resource = get_sampler_desc(ctx, instr->variables[0], AC_DESC_IMAGE, true, false);
  3262. args.opcode = ac_image_get_resinfo;
  3263. args.addr = ctx->ac.i32_0;
  3264. res = ac_build_image_opcode(&ctx->ac, &args);
  3265. LLVMValueRef two = LLVMConstInt(ctx->ac.i32, 2, false);
  3266. if (glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_CUBE &&
  3267. glsl_sampler_type_is_array(type)) {
  3268. LLVMValueRef six = LLVMConstInt(ctx->ac.i32, 6, false);
  3269. LLVMValueRef z = LLVMBuildExtractElement(ctx->ac.builder, res, two, "");
  3270. z = LLVMBuildSDiv(ctx->ac.builder, z, six, "");
  3271. res = LLVMBuildInsertElement(ctx->ac.builder, res, z, two, "");
  3272. }
  3273. if (ctx->ac.chip_class >= GFX9 &&
  3274. glsl_get_sampler_dim(type) == GLSL_SAMPLER_DIM_1D &&
  3275. glsl_sampler_type_is_array(type)) {
  3276. LLVMValueRef layers = LLVMBuildExtractElement(ctx->ac.builder, res, two, "");
  3277. res = LLVMBuildInsertElement(ctx->ac.builder, res, layers,
  3278. ctx->ac.i32_1, "");
  3279. }
  3280. return res;
  3281. }
  3282. #define NOOP_WAITCNT 0xf7f
  3283. #define LGKM_CNT 0x07f
  3284. #define VM_CNT 0xf70
  3285. static void emit_waitcnt(struct nir_to_llvm_context *ctx,
  3286. unsigned simm16)
  3287. {
  3288. LLVMValueRef args[1] = {
  3289. LLVMConstInt(ctx->i32, simm16, false),
  3290. };
  3291. ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.s.waitcnt",
  3292. ctx->voidt, args, 1, 0);
  3293. }
  3294. static void emit_barrier(struct nir_to_llvm_context *ctx)
  3295. {
  3296. /* SI only (thanks to a hw bug workaround):
  3297. * The real barrier instruction isn’t needed, because an entire patch
  3298. * always fits into a single wave.
  3299. */
  3300. if (ctx->options->chip_class == SI &&
  3301. ctx->stage == MESA_SHADER_TESS_CTRL) {
  3302. emit_waitcnt(ctx, LGKM_CNT & VM_CNT);
  3303. return;
  3304. }
  3305. ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.s.barrier",
  3306. ctx->voidt, NULL, 0, AC_FUNC_ATTR_CONVERGENT);
  3307. }
  3308. static void emit_discard_if(struct ac_nir_context *ctx,
  3309. const nir_intrinsic_instr *instr)
  3310. {
  3311. LLVMValueRef cond;
  3312. cond = LLVMBuildICmp(ctx->ac.builder, LLVMIntNE,
  3313. get_src(ctx, instr->src[0]),
  3314. ctx->ac.i32_0, "");
  3315. cond = LLVMBuildSelect(ctx->ac.builder, cond,
  3316. LLVMConstReal(ctx->ac.f32, -1.0f),
  3317. ctx->ac.f32_0, "");
  3318. ac_build_kill(&ctx->ac, cond);
  3319. }
  3320. static LLVMValueRef
  3321. visit_load_local_invocation_index(struct nir_to_llvm_context *ctx)
  3322. {
  3323. LLVMValueRef result;
  3324. LLVMValueRef thread_id = ac_get_thread_id(&ctx->ac);
  3325. result = LLVMBuildAnd(ctx->builder, ctx->tg_size,
  3326. LLVMConstInt(ctx->i32, 0xfc0, false), "");
  3327. return LLVMBuildAdd(ctx->builder, result, thread_id, "");
  3328. }
  3329. static LLVMValueRef visit_var_atomic(struct nir_to_llvm_context *ctx,
  3330. const nir_intrinsic_instr *instr)
  3331. {
  3332. LLVMValueRef ptr, result;
  3333. LLVMValueRef src = get_src(ctx->nir, instr->src[0]);
  3334. ptr = build_gep_for_deref(ctx->nir, instr->variables[0]);
  3335. if (instr->intrinsic == nir_intrinsic_var_atomic_comp_swap) {
  3336. LLVMValueRef src1 = get_src(ctx->nir, instr->src[1]);
  3337. result = LLVMBuildAtomicCmpXchg(ctx->builder,
  3338. ptr, src, src1,
  3339. LLVMAtomicOrderingSequentiallyConsistent,
  3340. LLVMAtomicOrderingSequentiallyConsistent,
  3341. false);
  3342. } else {
  3343. LLVMAtomicRMWBinOp op;
  3344. switch (instr->intrinsic) {
  3345. case nir_intrinsic_var_atomic_add:
  3346. op = LLVMAtomicRMWBinOpAdd;
  3347. break;
  3348. case nir_intrinsic_var_atomic_umin:
  3349. op = LLVMAtomicRMWBinOpUMin;
  3350. break;
  3351. case nir_intrinsic_var_atomic_umax:
  3352. op = LLVMAtomicRMWBinOpUMax;
  3353. break;
  3354. case nir_intrinsic_var_atomic_imin:
  3355. op = LLVMAtomicRMWBinOpMin;
  3356. break;
  3357. case nir_intrinsic_var_atomic_imax:
  3358. op = LLVMAtomicRMWBinOpMax;
  3359. break;
  3360. case nir_intrinsic_var_atomic_and:
  3361. op = LLVMAtomicRMWBinOpAnd;
  3362. break;
  3363. case nir_intrinsic_var_atomic_or:
  3364. op = LLVMAtomicRMWBinOpOr;
  3365. break;
  3366. case nir_intrinsic_var_atomic_xor:
  3367. op = LLVMAtomicRMWBinOpXor;
  3368. break;
  3369. case nir_intrinsic_var_atomic_exchange:
  3370. op = LLVMAtomicRMWBinOpXchg;
  3371. break;
  3372. default:
  3373. return NULL;
  3374. }
  3375. result = LLVMBuildAtomicRMW(ctx->builder, op, ptr, ac_to_integer(&ctx->ac, src),
  3376. LLVMAtomicOrderingSequentiallyConsistent,
  3377. false);
  3378. }
  3379. return result;
  3380. }
  3381. #define INTERP_CENTER 0
  3382. #define INTERP_CENTROID 1
  3383. #define INTERP_SAMPLE 2
  3384. static LLVMValueRef lookup_interp_param(struct nir_to_llvm_context *ctx,
  3385. enum glsl_interp_mode interp, unsigned location)
  3386. {
  3387. switch (interp) {
  3388. case INTERP_MODE_FLAT:
  3389. default:
  3390. return NULL;
  3391. case INTERP_MODE_SMOOTH:
  3392. case INTERP_MODE_NONE:
  3393. if (location == INTERP_CENTER)
  3394. return ctx->persp_center;
  3395. else if (location == INTERP_CENTROID)
  3396. return ctx->persp_centroid;
  3397. else if (location == INTERP_SAMPLE)
  3398. return ctx->persp_sample;
  3399. break;
  3400. case INTERP_MODE_NOPERSPECTIVE:
  3401. if (location == INTERP_CENTER)
  3402. return ctx->linear_center;
  3403. else if (location == INTERP_CENTROID)
  3404. return ctx->linear_centroid;
  3405. else if (location == INTERP_SAMPLE)
  3406. return ctx->linear_sample;
  3407. break;
  3408. }
  3409. return NULL;
  3410. }
  3411. static LLVMValueRef load_sample_position(struct nir_to_llvm_context *ctx,
  3412. LLVMValueRef sample_id)
  3413. {
  3414. LLVMValueRef result;
  3415. LLVMValueRef ptr = ac_build_gep0(&ctx->ac, ctx->ring_offsets, LLVMConstInt(ctx->i32, RING_PS_SAMPLE_POSITIONS, false));
  3416. ptr = LLVMBuildBitCast(ctx->builder, ptr,
  3417. const_array(ctx->v2f32, 64), "");
  3418. sample_id = LLVMBuildAdd(ctx->builder, sample_id, ctx->sample_pos_offset, "");
  3419. result = ac_build_load_invariant(&ctx->ac, ptr, sample_id);
  3420. return result;
  3421. }
  3422. static LLVMValueRef load_sample_pos(struct ac_nir_context *ctx)
  3423. {
  3424. LLVMValueRef values[2];
  3425. values[0] = emit_ffract(&ctx->ac, ctx->abi->frag_pos[0]);
  3426. values[1] = emit_ffract(&ctx->ac, ctx->abi->frag_pos[1]);
  3427. return ac_build_gather_values(&ctx->ac, values, 2);
  3428. }
  3429. static LLVMValueRef visit_interp(struct nir_to_llvm_context *ctx,
  3430. const nir_intrinsic_instr *instr)
  3431. {
  3432. LLVMValueRef result[2];
  3433. LLVMValueRef interp_param, attr_number;
  3434. unsigned location;
  3435. unsigned chan;
  3436. LLVMValueRef src_c0 = NULL;
  3437. LLVMValueRef src_c1 = NULL;
  3438. LLVMValueRef src0 = NULL;
  3439. int input_index = instr->variables[0]->var->data.location - VARYING_SLOT_VAR0;
  3440. switch (instr->intrinsic) {
  3441. case nir_intrinsic_interp_var_at_centroid:
  3442. location = INTERP_CENTROID;
  3443. break;
  3444. case nir_intrinsic_interp_var_at_sample:
  3445. case nir_intrinsic_interp_var_at_offset:
  3446. location = INTERP_CENTER;
  3447. src0 = get_src(ctx->nir, instr->src[0]);
  3448. break;
  3449. default:
  3450. break;
  3451. }
  3452. if (instr->intrinsic == nir_intrinsic_interp_var_at_offset) {
  3453. src_c0 = ac_to_float(&ctx->ac, LLVMBuildExtractElement(ctx->builder, src0, ctx->i32zero, ""));
  3454. src_c1 = ac_to_float(&ctx->ac, LLVMBuildExtractElement(ctx->builder, src0, ctx->i32one, ""));
  3455. } else if (instr->intrinsic == nir_intrinsic_interp_var_at_sample) {
  3456. LLVMValueRef sample_position;
  3457. LLVMValueRef halfval = LLVMConstReal(ctx->f32, 0.5f);
  3458. /* fetch sample ID */
  3459. sample_position = load_sample_position(ctx, src0);
  3460. src_c0 = LLVMBuildExtractElement(ctx->builder, sample_position, ctx->i32zero, "");
  3461. src_c0 = LLVMBuildFSub(ctx->builder, src_c0, halfval, "");
  3462. src_c1 = LLVMBuildExtractElement(ctx->builder, sample_position, ctx->i32one, "");
  3463. src_c1 = LLVMBuildFSub(ctx->builder, src_c1, halfval, "");
  3464. }
  3465. interp_param = lookup_interp_param(ctx, instr->variables[0]->var->data.interpolation, location);
  3466. attr_number = LLVMConstInt(ctx->i32, input_index, false);
  3467. if (location == INTERP_CENTER) {
  3468. LLVMValueRef ij_out[2];
  3469. LLVMValueRef ddxy_out = emit_ddxy_interp(ctx->nir, interp_param);
  3470. /*
  3471. * take the I then J parameters, and the DDX/Y for it, and
  3472. * calculate the IJ inputs for the interpolator.
  3473. * temp1 = ddx * offset/sample.x + I;
  3474. * interp_param.I = ddy * offset/sample.y + temp1;
  3475. * temp1 = ddx * offset/sample.x + J;
  3476. * interp_param.J = ddy * offset/sample.y + temp1;
  3477. */
  3478. for (unsigned i = 0; i < 2; i++) {
  3479. LLVMValueRef ix_ll = LLVMConstInt(ctx->i32, i, false);
  3480. LLVMValueRef iy_ll = LLVMConstInt(ctx->i32, i + 2, false);
  3481. LLVMValueRef ddx_el = LLVMBuildExtractElement(ctx->builder,
  3482. ddxy_out, ix_ll, "");
  3483. LLVMValueRef ddy_el = LLVMBuildExtractElement(ctx->builder,
  3484. ddxy_out, iy_ll, "");
  3485. LLVMValueRef interp_el = LLVMBuildExtractElement(ctx->builder,
  3486. interp_param, ix_ll, "");
  3487. LLVMValueRef temp1, temp2;
  3488. interp_el = LLVMBuildBitCast(ctx->builder, interp_el,
  3489. ctx->f32, "");
  3490. temp1 = LLVMBuildFMul(ctx->builder, ddx_el, src_c0, "");
  3491. temp1 = LLVMBuildFAdd(ctx->builder, temp1, interp_el, "");
  3492. temp2 = LLVMBuildFMul(ctx->builder, ddy_el, src_c1, "");
  3493. temp2 = LLVMBuildFAdd(ctx->builder, temp2, temp1, "");
  3494. ij_out[i] = LLVMBuildBitCast(ctx->builder,
  3495. temp2, ctx->i32, "");
  3496. }
  3497. interp_param = ac_build_gather_values(&ctx->ac, ij_out, 2);
  3498. }
  3499. for (chan = 0; chan < 2; chan++) {
  3500. LLVMValueRef llvm_chan = LLVMConstInt(ctx->i32, chan, false);
  3501. if (interp_param) {
  3502. interp_param = LLVMBuildBitCast(ctx->builder,
  3503. interp_param, LLVMVectorType(ctx->f32, 2), "");
  3504. LLVMValueRef i = LLVMBuildExtractElement(
  3505. ctx->builder, interp_param, ctx->i32zero, "");
  3506. LLVMValueRef j = LLVMBuildExtractElement(
  3507. ctx->builder, interp_param, ctx->i32one, "");
  3508. result[chan] = ac_build_fs_interp(&ctx->ac,
  3509. llvm_chan, attr_number,
  3510. ctx->prim_mask, i, j);
  3511. } else {
  3512. result[chan] = ac_build_fs_interp_mov(&ctx->ac,
  3513. LLVMConstInt(ctx->i32, 2, false),
  3514. llvm_chan, attr_number,
  3515. ctx->prim_mask);
  3516. }
  3517. }
  3518. return ac_build_gather_values(&ctx->ac, result, 2);
  3519. }
  3520. static void
  3521. visit_emit_vertex(struct nir_to_llvm_context *ctx,
  3522. const nir_intrinsic_instr *instr)
  3523. {
  3524. LLVMValueRef gs_next_vertex;
  3525. LLVMValueRef can_emit, kill;
  3526. int idx;
  3527. assert(instr->const_index[0] == 0);
  3528. /* Write vertex attribute values to GSVS ring */
  3529. gs_next_vertex = LLVMBuildLoad(ctx->builder,
  3530. ctx->gs_next_vertex,
  3531. "");
  3532. /* If this thread has already emitted the declared maximum number of
  3533. * vertices, kill it: excessive vertex emissions are not supposed to
  3534. * have any effect, and GS threads have no externally observable
  3535. * effects other than emitting vertices.
  3536. */
  3537. can_emit = LLVMBuildICmp(ctx->builder, LLVMIntULT, gs_next_vertex,
  3538. LLVMConstInt(ctx->i32, ctx->gs_max_out_vertices, false), "");
  3539. kill = LLVMBuildSelect(ctx->builder, can_emit,
  3540. LLVMConstReal(ctx->f32, 1.0f),
  3541. LLVMConstReal(ctx->f32, -1.0f), "");
  3542. ac_build_kill(&ctx->ac, kill);
  3543. /* loop num outputs */
  3544. idx = 0;
  3545. for (unsigned i = 0; i < RADEON_LLVM_MAX_OUTPUTS; ++i) {
  3546. LLVMValueRef *out_ptr = &ctx->nir->outputs[i * 4];
  3547. int length = 4;
  3548. int slot = idx;
  3549. int slot_inc = 1;
  3550. if (!(ctx->output_mask & (1ull << i)))
  3551. continue;
  3552. if (i == VARYING_SLOT_CLIP_DIST0) {
  3553. /* pack clip and cull into a single set of slots */
  3554. length = ctx->num_output_clips + ctx->num_output_culls;
  3555. if (length > 4)
  3556. slot_inc = 2;
  3557. }
  3558. for (unsigned j = 0; j < length; j++) {
  3559. LLVMValueRef out_val = LLVMBuildLoad(ctx->builder,
  3560. out_ptr[j], "");
  3561. LLVMValueRef voffset = LLVMConstInt(ctx->i32, (slot * 4 + j) * ctx->gs_max_out_vertices, false);
  3562. voffset = LLVMBuildAdd(ctx->builder, voffset, gs_next_vertex, "");
  3563. voffset = LLVMBuildMul(ctx->builder, voffset, LLVMConstInt(ctx->i32, 4, false), "");
  3564. out_val = LLVMBuildBitCast(ctx->builder, out_val, ctx->i32, "");
  3565. ac_build_buffer_store_dword(&ctx->ac, ctx->gsvs_ring,
  3566. out_val, 1,
  3567. voffset, ctx->gs2vs_offset, 0,
  3568. 1, 1, true, true);
  3569. }
  3570. idx += slot_inc;
  3571. }
  3572. gs_next_vertex = LLVMBuildAdd(ctx->builder, gs_next_vertex,
  3573. ctx->i32one, "");
  3574. LLVMBuildStore(ctx->builder, gs_next_vertex, ctx->gs_next_vertex);
  3575. ac_build_sendmsg(&ctx->ac, AC_SENDMSG_GS_OP_EMIT | AC_SENDMSG_GS | (0 << 8), ctx->gs_wave_id);
  3576. }
  3577. static void
  3578. visit_end_primitive(struct nir_to_llvm_context *ctx,
  3579. const nir_intrinsic_instr *instr)
  3580. {
  3581. ac_build_sendmsg(&ctx->ac, AC_SENDMSG_GS_OP_CUT | AC_SENDMSG_GS | (0 << 8), ctx->gs_wave_id);
  3582. }
  3583. static LLVMValueRef
  3584. visit_load_tess_coord(struct nir_to_llvm_context *ctx,
  3585. const nir_intrinsic_instr *instr)
  3586. {
  3587. LLVMValueRef coord[4] = {
  3588. ctx->tes_u,
  3589. ctx->tes_v,
  3590. ctx->f32zero,
  3591. ctx->f32zero,
  3592. };
  3593. if (ctx->tes_primitive_mode == GL_TRIANGLES)
  3594. coord[2] = LLVMBuildFSub(ctx->builder, ctx->f32one,
  3595. LLVMBuildFAdd(ctx->builder, coord[0], coord[1], ""), "");
  3596. LLVMValueRef result = ac_build_gather_values(&ctx->ac, coord, instr->num_components);
  3597. return LLVMBuildBitCast(ctx->builder, result,
  3598. get_def_type(ctx->nir, &instr->dest.ssa), "");
  3599. }
  3600. static void visit_intrinsic(struct ac_nir_context *ctx,
  3601. nir_intrinsic_instr *instr)
  3602. {
  3603. LLVMValueRef result = NULL;
  3604. switch (instr->intrinsic) {
  3605. case nir_intrinsic_load_work_group_id: {
  3606. result = ctx->nctx->workgroup_ids;
  3607. break;
  3608. }
  3609. case nir_intrinsic_load_base_vertex: {
  3610. result = ctx->abi->base_vertex;
  3611. break;
  3612. }
  3613. case nir_intrinsic_load_vertex_id_zero_base: {
  3614. result = ctx->abi->vertex_id;
  3615. break;
  3616. }
  3617. case nir_intrinsic_load_local_invocation_id: {
  3618. result = ctx->nctx->local_invocation_ids;
  3619. break;
  3620. }
  3621. case nir_intrinsic_load_base_instance:
  3622. result = ctx->abi->start_instance;
  3623. break;
  3624. case nir_intrinsic_load_draw_id:
  3625. result = ctx->abi->draw_id;
  3626. break;
  3627. case nir_intrinsic_load_view_index:
  3628. result = ctx->nctx->view_index ? ctx->nctx->view_index : ctx->ac.i32_0;
  3629. break;
  3630. case nir_intrinsic_load_invocation_id:
  3631. if (ctx->stage == MESA_SHADER_TESS_CTRL)
  3632. result = unpack_param(&ctx->ac, ctx->nctx->tcs_rel_ids, 8, 5);
  3633. else
  3634. result = ctx->nctx->gs_invocation_id;
  3635. break;
  3636. case nir_intrinsic_load_primitive_id:
  3637. if (ctx->stage == MESA_SHADER_GEOMETRY) {
  3638. ctx->nctx->shader_info->gs.uses_prim_id = true;
  3639. result = ctx->nctx->gs_prim_id;
  3640. } else if (ctx->stage == MESA_SHADER_TESS_CTRL) {
  3641. ctx->nctx->shader_info->tcs.uses_prim_id = true;
  3642. result = ctx->nctx->tcs_patch_id;
  3643. } else if (ctx->stage == MESA_SHADER_TESS_EVAL) {
  3644. ctx->nctx->shader_info->tcs.uses_prim_id = true;
  3645. result = ctx->nctx->tes_patch_id;
  3646. } else
  3647. fprintf(stderr, "Unknown primitive id intrinsic: %d", ctx->stage);
  3648. break;
  3649. case nir_intrinsic_load_sample_id:
  3650. result = unpack_param(&ctx->ac, ctx->abi->ancillary, 8, 4);
  3651. break;
  3652. case nir_intrinsic_load_sample_pos:
  3653. result = load_sample_pos(ctx);
  3654. break;
  3655. case nir_intrinsic_load_sample_mask_in:
  3656. result = ctx->abi->sample_coverage;
  3657. break;
  3658. case nir_intrinsic_load_frag_coord: {
  3659. LLVMValueRef values[4] = {
  3660. ctx->abi->frag_pos[0],
  3661. ctx->abi->frag_pos[1],
  3662. ctx->abi->frag_pos[2],
  3663. ac_build_fdiv(&ctx->ac, ctx->ac.f32_1, ctx->abi->frag_pos[3])
  3664. };
  3665. result = ac_build_gather_values(&ctx->ac, values, 4);
  3666. break;
  3667. }
  3668. case nir_intrinsic_load_front_face:
  3669. result = ctx->abi->front_face;
  3670. break;
  3671. case nir_intrinsic_load_instance_id:
  3672. result = ctx->abi->instance_id;
  3673. break;
  3674. case nir_intrinsic_load_num_work_groups:
  3675. result = ctx->nctx->num_work_groups;
  3676. break;
  3677. case nir_intrinsic_load_local_invocation_index:
  3678. result = visit_load_local_invocation_index(ctx->nctx);
  3679. break;
  3680. case nir_intrinsic_load_push_constant:
  3681. result = visit_load_push_constant(ctx->nctx, instr);
  3682. break;
  3683. case nir_intrinsic_vulkan_resource_index:
  3684. result = visit_vulkan_resource_index(ctx->nctx, instr);
  3685. break;
  3686. case nir_intrinsic_store_ssbo:
  3687. visit_store_ssbo(ctx, instr);
  3688. break;
  3689. case nir_intrinsic_load_ssbo:
  3690. result = visit_load_buffer(ctx, instr);
  3691. break;
  3692. case nir_intrinsic_ssbo_atomic_add:
  3693. case nir_intrinsic_ssbo_atomic_imin:
  3694. case nir_intrinsic_ssbo_atomic_umin:
  3695. case nir_intrinsic_ssbo_atomic_imax:
  3696. case nir_intrinsic_ssbo_atomic_umax:
  3697. case nir_intrinsic_ssbo_atomic_and:
  3698. case nir_intrinsic_ssbo_atomic_or:
  3699. case nir_intrinsic_ssbo_atomic_xor:
  3700. case nir_intrinsic_ssbo_atomic_exchange:
  3701. case nir_intrinsic_ssbo_atomic_comp_swap:
  3702. result = visit_atomic_ssbo(ctx, instr);
  3703. break;
  3704. case nir_intrinsic_load_ubo:
  3705. result = visit_load_ubo_buffer(ctx, instr);
  3706. break;
  3707. case nir_intrinsic_get_buffer_size:
  3708. result = visit_get_buffer_size(ctx, instr);
  3709. break;
  3710. case nir_intrinsic_load_var:
  3711. result = visit_load_var(ctx, instr);
  3712. break;
  3713. case nir_intrinsic_store_var:
  3714. visit_store_var(ctx, instr);
  3715. break;
  3716. case nir_intrinsic_image_load:
  3717. result = visit_image_load(ctx, instr);
  3718. break;
  3719. case nir_intrinsic_image_store:
  3720. visit_image_store(ctx, instr);
  3721. break;
  3722. case nir_intrinsic_image_atomic_add:
  3723. case nir_intrinsic_image_atomic_min:
  3724. case nir_intrinsic_image_atomic_max:
  3725. case nir_intrinsic_image_atomic_and:
  3726. case nir_intrinsic_image_atomic_or:
  3727. case nir_intrinsic_image_atomic_xor:
  3728. case nir_intrinsic_image_atomic_exchange:
  3729. case nir_intrinsic_image_atomic_comp_swap:
  3730. result = visit_image_atomic(ctx, instr);
  3731. break;
  3732. case nir_intrinsic_image_size:
  3733. result = visit_image_size(ctx, instr);
  3734. break;
  3735. case nir_intrinsic_discard:
  3736. ac_build_intrinsic(&ctx->ac, "llvm.AMDGPU.kilp",
  3737. LLVMVoidTypeInContext(ctx->ac.context),
  3738. NULL, 0, AC_FUNC_ATTR_LEGACY);
  3739. break;
  3740. case nir_intrinsic_discard_if:
  3741. emit_discard_if(ctx, instr);
  3742. break;
  3743. case nir_intrinsic_memory_barrier:
  3744. emit_waitcnt(ctx->nctx, VM_CNT);
  3745. break;
  3746. case nir_intrinsic_barrier:
  3747. emit_barrier(ctx->nctx);
  3748. break;
  3749. case nir_intrinsic_var_atomic_add:
  3750. case nir_intrinsic_var_atomic_imin:
  3751. case nir_intrinsic_var_atomic_umin:
  3752. case nir_intrinsic_var_atomic_imax:
  3753. case nir_intrinsic_var_atomic_umax:
  3754. case nir_intrinsic_var_atomic_and:
  3755. case nir_intrinsic_var_atomic_or:
  3756. case nir_intrinsic_var_atomic_xor:
  3757. case nir_intrinsic_var_atomic_exchange:
  3758. case nir_intrinsic_var_atomic_comp_swap:
  3759. result = visit_var_atomic(ctx->nctx, instr);
  3760. break;
  3761. case nir_intrinsic_interp_var_at_centroid:
  3762. case nir_intrinsic_interp_var_at_sample:
  3763. case nir_intrinsic_interp_var_at_offset:
  3764. result = visit_interp(ctx->nctx, instr);
  3765. break;
  3766. case nir_intrinsic_emit_vertex:
  3767. visit_emit_vertex(ctx->nctx, instr);
  3768. break;
  3769. case nir_intrinsic_end_primitive:
  3770. visit_end_primitive(ctx->nctx, instr);
  3771. break;
  3772. case nir_intrinsic_load_tess_coord:
  3773. result = visit_load_tess_coord(ctx->nctx, instr);
  3774. break;
  3775. case nir_intrinsic_load_patch_vertices_in:
  3776. result = LLVMConstInt(ctx->ac.i32, ctx->nctx->options->key.tcs.input_vertices, false);
  3777. break;
  3778. default:
  3779. fprintf(stderr, "Unknown intrinsic: ");
  3780. nir_print_instr(&instr->instr, stderr);
  3781. fprintf(stderr, "\n");
  3782. break;
  3783. }
  3784. if (result) {
  3785. _mesa_hash_table_insert(ctx->defs, &instr->dest.ssa, result);
  3786. }
  3787. }
  3788. static LLVMValueRef radv_load_ssbo(struct ac_shader_abi *abi,
  3789. LLVMValueRef buffer, bool write)
  3790. {
  3791. struct nir_to_llvm_context *ctx = nir_to_llvm_context_from_abi(abi);
  3792. if (write && ctx->stage == MESA_SHADER_FRAGMENT)
  3793. ctx->shader_info->fs.writes_memory = true;
  3794. return buffer;
  3795. }
  3796. static LLVMValueRef radv_get_sampler_desc(struct ac_shader_abi *abi,
  3797. unsigned descriptor_set,
  3798. unsigned base_index,
  3799. unsigned constant_index,
  3800. LLVMValueRef index,
  3801. enum ac_descriptor_type desc_type,
  3802. bool image, bool write)
  3803. {
  3804. struct nir_to_llvm_context *ctx = nir_to_llvm_context_from_abi(abi);
  3805. LLVMValueRef list = ctx->descriptor_sets[descriptor_set];
  3806. struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
  3807. struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
  3808. unsigned offset = binding->offset;
  3809. unsigned stride = binding->size;
  3810. unsigned type_size;
  3811. LLVMBuilderRef builder = ctx->builder;
  3812. LLVMTypeRef type;
  3813. assert(base_index < layout->binding_count);
  3814. if (write && ctx->stage == MESA_SHADER_FRAGMENT)
  3815. ctx->shader_info->fs.writes_memory = true;
  3816. switch (desc_type) {
  3817. case AC_DESC_IMAGE:
  3818. type = ctx->v8i32;
  3819. type_size = 32;
  3820. break;
  3821. case AC_DESC_FMASK:
  3822. type = ctx->v8i32;
  3823. offset += 32;
  3824. type_size = 32;
  3825. break;
  3826. case AC_DESC_SAMPLER:
  3827. type = ctx->v4i32;
  3828. if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
  3829. offset += 64;
  3830. type_size = 16;
  3831. break;
  3832. case AC_DESC_BUFFER:
  3833. type = ctx->v4i32;
  3834. type_size = 16;
  3835. break;
  3836. default:
  3837. unreachable("invalid desc_type\n");
  3838. }
  3839. offset += constant_index * stride;
  3840. if (desc_type == AC_DESC_SAMPLER && binding->immutable_samplers_offset &&
  3841. (!index || binding->immutable_samplers_equal)) {
  3842. if (binding->immutable_samplers_equal)
  3843. constant_index = 0;
  3844. const uint32_t *samplers = radv_immutable_samplers(layout, binding);
  3845. LLVMValueRef constants[] = {
  3846. LLVMConstInt(ctx->i32, samplers[constant_index * 4 + 0], 0),
  3847. LLVMConstInt(ctx->i32, samplers[constant_index * 4 + 1], 0),
  3848. LLVMConstInt(ctx->i32, samplers[constant_index * 4 + 2], 0),
  3849. LLVMConstInt(ctx->i32, samplers[constant_index * 4 + 3], 0),
  3850. };
  3851. return ac_build_gather_values(&ctx->ac, constants, 4);
  3852. }
  3853. assert(stride % type_size == 0);
  3854. if (!index)
  3855. index = ctx->i32zero;
  3856. index = LLVMBuildMul(builder, index, LLVMConstInt(ctx->i32, stride / type_size, 0), "");
  3857. list = ac_build_gep0(&ctx->ac, list, LLVMConstInt(ctx->i32, offset, 0));
  3858. list = LLVMBuildPointerCast(builder, list, const_array(type, 0), "");
  3859. return ac_build_load_to_sgpr(&ctx->ac, list, index);
  3860. }
  3861. static LLVMValueRef get_sampler_desc(struct ac_nir_context *ctx,
  3862. const nir_deref_var *deref,
  3863. enum ac_descriptor_type desc_type,
  3864. bool image, bool write)
  3865. {
  3866. LLVMValueRef index = NULL;
  3867. unsigned constant_index = 0;
  3868. const nir_deref *tail = &deref->deref;
  3869. while (tail->child) {
  3870. const nir_deref_array *child = nir_deref_as_array(tail->child);
  3871. unsigned array_size = glsl_get_aoa_size(tail->child->type);
  3872. if (!array_size)
  3873. array_size = 1;
  3874. assert(child->deref_array_type != nir_deref_array_type_wildcard);
  3875. if (child->deref_array_type == nir_deref_array_type_indirect) {
  3876. LLVMValueRef indirect = get_src(ctx, child->indirect);
  3877. indirect = LLVMBuildMul(ctx->ac.builder, indirect,
  3878. LLVMConstInt(ctx->ac.i32, array_size, false), "");
  3879. if (!index)
  3880. index = indirect;
  3881. else
  3882. index = LLVMBuildAdd(ctx->ac.builder, index, indirect, "");
  3883. }
  3884. constant_index += child->base_offset * array_size;
  3885. tail = &child->deref;
  3886. }
  3887. return ctx->abi->load_sampler_desc(ctx->abi,
  3888. deref->var->data.descriptor_set,
  3889. deref->var->data.binding,
  3890. constant_index, index,
  3891. desc_type, image, write);
  3892. }
  3893. static void set_tex_fetch_args(struct ac_llvm_context *ctx,
  3894. struct ac_image_args *args,
  3895. const nir_tex_instr *instr,
  3896. nir_texop op,
  3897. LLVMValueRef res_ptr, LLVMValueRef samp_ptr,
  3898. LLVMValueRef *param, unsigned count,
  3899. unsigned dmask)
  3900. {
  3901. unsigned is_rect = 0;
  3902. bool da = instr->is_array || instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE;
  3903. if (op == nir_texop_lod)
  3904. da = false;
  3905. /* Pad to power of two vector */
  3906. while (count < util_next_power_of_two(count))
  3907. param[count++] = LLVMGetUndef(ctx->i32);
  3908. if (count > 1)
  3909. args->addr = ac_build_gather_values(ctx, param, count);
  3910. else
  3911. args->addr = param[0];
  3912. args->resource = res_ptr;
  3913. args->sampler = samp_ptr;
  3914. if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF && op == nir_texop_txf) {
  3915. args->addr = param[0];
  3916. return;
  3917. }
  3918. args->dmask = dmask;
  3919. args->unorm = is_rect;
  3920. args->da = da;
  3921. }
  3922. /* Disable anisotropic filtering if BASE_LEVEL == LAST_LEVEL.
  3923. *
  3924. * SI-CI:
  3925. * If BASE_LEVEL == LAST_LEVEL, the shader must disable anisotropic
  3926. * filtering manually. The driver sets img7 to a mask clearing
  3927. * MAX_ANISO_RATIO if BASE_LEVEL == LAST_LEVEL. The shader must do:
  3928. * s_and_b32 samp0, samp0, img7
  3929. *
  3930. * VI:
  3931. * The ANISO_OVERRIDE sampler field enables this fix in TA.
  3932. */
  3933. static LLVMValueRef sici_fix_sampler_aniso(struct ac_nir_context *ctx,
  3934. LLVMValueRef res, LLVMValueRef samp)
  3935. {
  3936. LLVMBuilderRef builder = ctx->ac.builder;
  3937. LLVMValueRef img7, samp0;
  3938. if (ctx->ac.chip_class >= VI)
  3939. return samp;
  3940. img7 = LLVMBuildExtractElement(builder, res,
  3941. LLVMConstInt(ctx->ac.i32, 7, 0), "");
  3942. samp0 = LLVMBuildExtractElement(builder, samp,
  3943. LLVMConstInt(ctx->ac.i32, 0, 0), "");
  3944. samp0 = LLVMBuildAnd(builder, samp0, img7, "");
  3945. return LLVMBuildInsertElement(builder, samp, samp0,
  3946. LLVMConstInt(ctx->ac.i32, 0, 0), "");
  3947. }
  3948. static void tex_fetch_ptrs(struct ac_nir_context *ctx,
  3949. nir_tex_instr *instr,
  3950. LLVMValueRef *res_ptr, LLVMValueRef *samp_ptr,
  3951. LLVMValueRef *fmask_ptr)
  3952. {
  3953. if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
  3954. *res_ptr = get_sampler_desc(ctx, instr->texture, AC_DESC_BUFFER, false, false);
  3955. else
  3956. *res_ptr = get_sampler_desc(ctx, instr->texture, AC_DESC_IMAGE, false, false);
  3957. if (samp_ptr) {
  3958. if (instr->sampler)
  3959. *samp_ptr = get_sampler_desc(ctx, instr->sampler, AC_DESC_SAMPLER, false, false);
  3960. else
  3961. *samp_ptr = get_sampler_desc(ctx, instr->texture, AC_DESC_SAMPLER, false, false);
  3962. if (instr->sampler_dim < GLSL_SAMPLER_DIM_RECT)
  3963. *samp_ptr = sici_fix_sampler_aniso(ctx, *res_ptr, *samp_ptr);
  3964. }
  3965. if (fmask_ptr && !instr->sampler && (instr->op == nir_texop_txf_ms ||
  3966. instr->op == nir_texop_samples_identical))
  3967. *fmask_ptr = get_sampler_desc(ctx, instr->texture, AC_DESC_FMASK, false, false);
  3968. }
  3969. static LLVMValueRef apply_round_slice(struct ac_llvm_context *ctx,
  3970. LLVMValueRef coord)
  3971. {
  3972. coord = ac_to_float(ctx, coord);
  3973. coord = ac_build_intrinsic(ctx, "llvm.rint.f32", ctx->f32, &coord, 1, 0);
  3974. coord = ac_to_integer(ctx, coord);
  3975. return coord;
  3976. }
  3977. static void visit_tex(struct ac_nir_context *ctx, nir_tex_instr *instr)
  3978. {
  3979. LLVMValueRef result = NULL;
  3980. struct ac_image_args args = { 0 };
  3981. unsigned dmask = 0xf;
  3982. LLVMValueRef address[16];
  3983. LLVMValueRef coords[5];
  3984. LLVMValueRef coord = NULL, lod = NULL, comparator = NULL;
  3985. LLVMValueRef bias = NULL, offsets = NULL;
  3986. LLVMValueRef res_ptr, samp_ptr, fmask_ptr = NULL, sample_index = NULL;
  3987. LLVMValueRef ddx = NULL, ddy = NULL;
  3988. LLVMValueRef derivs[6];
  3989. unsigned chan, count = 0;
  3990. unsigned const_src = 0, num_deriv_comp = 0;
  3991. bool lod_is_zero = false;
  3992. tex_fetch_ptrs(ctx, instr, &res_ptr, &samp_ptr, &fmask_ptr);
  3993. for (unsigned i = 0; i < instr->num_srcs; i++) {
  3994. switch (instr->src[i].src_type) {
  3995. case nir_tex_src_coord:
  3996. coord = get_src(ctx, instr->src[i].src);
  3997. break;
  3998. case nir_tex_src_projector:
  3999. break;
  4000. case nir_tex_src_comparator:
  4001. comparator = get_src(ctx, instr->src[i].src);
  4002. break;
  4003. case nir_tex_src_offset:
  4004. offsets = get_src(ctx, instr->src[i].src);
  4005. const_src = i;
  4006. break;
  4007. case nir_tex_src_bias:
  4008. bias = get_src(ctx, instr->src[i].src);
  4009. break;
  4010. case nir_tex_src_lod: {
  4011. nir_const_value *val = nir_src_as_const_value(instr->src[i].src);
  4012. if (val && val->i32[0] == 0)
  4013. lod_is_zero = true;
  4014. lod = get_src(ctx, instr->src[i].src);
  4015. break;
  4016. }
  4017. case nir_tex_src_ms_index:
  4018. sample_index = get_src(ctx, instr->src[i].src);
  4019. break;
  4020. case nir_tex_src_ms_mcs:
  4021. break;
  4022. case nir_tex_src_ddx:
  4023. ddx = get_src(ctx, instr->src[i].src);
  4024. num_deriv_comp = instr->src[i].src.ssa->num_components;
  4025. break;
  4026. case nir_tex_src_ddy:
  4027. ddy = get_src(ctx, instr->src[i].src);
  4028. break;
  4029. case nir_tex_src_texture_offset:
  4030. case nir_tex_src_sampler_offset:
  4031. case nir_tex_src_plane:
  4032. default:
  4033. break;
  4034. }
  4035. }
  4036. if (instr->op == nir_texop_txs && instr->sampler_dim == GLSL_SAMPLER_DIM_BUF) {
  4037. result = get_buffer_size(ctx, res_ptr, true);
  4038. goto write_result;
  4039. }
  4040. if (instr->op == nir_texop_texture_samples) {
  4041. LLVMValueRef res, samples, is_msaa;
  4042. res = LLVMBuildBitCast(ctx->ac.builder, res_ptr, ctx->ac.v8i32, "");
  4043. samples = LLVMBuildExtractElement(ctx->ac.builder, res,
  4044. LLVMConstInt(ctx->ac.i32, 3, false), "");
  4045. is_msaa = LLVMBuildLShr(ctx->ac.builder, samples,
  4046. LLVMConstInt(ctx->ac.i32, 28, false), "");
  4047. is_msaa = LLVMBuildAnd(ctx->ac.builder, is_msaa,
  4048. LLVMConstInt(ctx->ac.i32, 0xe, false), "");
  4049. is_msaa = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ, is_msaa,
  4050. LLVMConstInt(ctx->ac.i32, 0xe, false), "");
  4051. samples = LLVMBuildLShr(ctx->ac.builder, samples,
  4052. LLVMConstInt(ctx->ac.i32, 16, false), "");
  4053. samples = LLVMBuildAnd(ctx->ac.builder, samples,
  4054. LLVMConstInt(ctx->ac.i32, 0xf, false), "");
  4055. samples = LLVMBuildShl(ctx->ac.builder, ctx->ac.i32_1,
  4056. samples, "");
  4057. samples = LLVMBuildSelect(ctx->ac.builder, is_msaa, samples,
  4058. ctx->ac.i32_1, "");
  4059. result = samples;
  4060. goto write_result;
  4061. }
  4062. if (coord)
  4063. for (chan = 0; chan < instr->coord_components; chan++)
  4064. coords[chan] = llvm_extract_elem(&ctx->ac, coord, chan);
  4065. if (offsets && instr->op != nir_texop_txf) {
  4066. LLVMValueRef offset[3], pack;
  4067. for (chan = 0; chan < 3; ++chan)
  4068. offset[chan] = ctx->ac.i32_0;
  4069. args.offset = true;
  4070. for (chan = 0; chan < get_llvm_num_components(offsets); chan++) {
  4071. offset[chan] = llvm_extract_elem(&ctx->ac, offsets, chan);
  4072. offset[chan] = LLVMBuildAnd(ctx->ac.builder, offset[chan],
  4073. LLVMConstInt(ctx->ac.i32, 0x3f, false), "");
  4074. if (chan)
  4075. offset[chan] = LLVMBuildShl(ctx->ac.builder, offset[chan],
  4076. LLVMConstInt(ctx->ac.i32, chan * 8, false), "");
  4077. }
  4078. pack = LLVMBuildOr(ctx->ac.builder, offset[0], offset[1], "");
  4079. pack = LLVMBuildOr(ctx->ac.builder, pack, offset[2], "");
  4080. address[count++] = pack;
  4081. }
  4082. /* pack LOD bias value */
  4083. if (instr->op == nir_texop_txb && bias) {
  4084. address[count++] = bias;
  4085. }
  4086. /* Pack depth comparison value */
  4087. if (instr->is_shadow && comparator) {
  4088. LLVMValueRef z = ac_to_float(&ctx->ac,
  4089. llvm_extract_elem(&ctx->ac, comparator, 0));
  4090. /* TC-compatible HTILE promotes Z16 and Z24 to Z32_FLOAT,
  4091. * so the depth comparison value isn't clamped for Z16 and
  4092. * Z24 anymore. Do it manually here.
  4093. *
  4094. * It's unnecessary if the original texture format was
  4095. * Z32_FLOAT, but we don't know that here.
  4096. */
  4097. if (ctx->ac.chip_class == VI)
  4098. z = ac_build_clamp(&ctx->ac, z);
  4099. address[count++] = z;
  4100. }
  4101. /* pack derivatives */
  4102. if (ddx || ddy) {
  4103. int num_src_deriv_channels, num_dest_deriv_channels;
  4104. switch (instr->sampler_dim) {
  4105. case GLSL_SAMPLER_DIM_3D:
  4106. case GLSL_SAMPLER_DIM_CUBE:
  4107. num_deriv_comp = 3;
  4108. num_src_deriv_channels = 3;
  4109. num_dest_deriv_channels = 3;
  4110. break;
  4111. case GLSL_SAMPLER_DIM_2D:
  4112. default:
  4113. num_src_deriv_channels = 2;
  4114. num_dest_deriv_channels = 2;
  4115. num_deriv_comp = 2;
  4116. break;
  4117. case GLSL_SAMPLER_DIM_1D:
  4118. num_src_deriv_channels = 1;
  4119. if (ctx->ac.chip_class >= GFX9) {
  4120. num_dest_deriv_channels = 2;
  4121. num_deriv_comp = 2;
  4122. } else {
  4123. num_dest_deriv_channels = 1;
  4124. num_deriv_comp = 1;
  4125. }
  4126. break;
  4127. }
  4128. for (unsigned i = 0; i < num_src_deriv_channels; i++) {
  4129. derivs[i] = ac_to_float(&ctx->ac, llvm_extract_elem(&ctx->ac, ddx, i));
  4130. derivs[num_dest_deriv_channels + i] = ac_to_float(&ctx->ac, llvm_extract_elem(&ctx->ac, ddy, i));
  4131. }
  4132. for (unsigned i = num_src_deriv_channels; i < num_dest_deriv_channels; i++) {
  4133. derivs[i] = ctx->ac.f32_0;
  4134. derivs[num_dest_deriv_channels + i] = ctx->ac.f32_0;
  4135. }
  4136. }
  4137. if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE && coord) {
  4138. for (chan = 0; chan < instr->coord_components; chan++)
  4139. coords[chan] = ac_to_float(&ctx->ac, coords[chan]);
  4140. if (instr->coord_components == 3)
  4141. coords[3] = LLVMGetUndef(ctx->ac.f32);
  4142. ac_prepare_cube_coords(&ctx->ac,
  4143. instr->op == nir_texop_txd, instr->is_array,
  4144. instr->op == nir_texop_lod, coords, derivs);
  4145. if (num_deriv_comp)
  4146. num_deriv_comp--;
  4147. }
  4148. if (ddx || ddy) {
  4149. for (unsigned i = 0; i < num_deriv_comp * 2; i++)
  4150. address[count++] = derivs[i];
  4151. }
  4152. /* Pack texture coordinates */
  4153. if (coord) {
  4154. address[count++] = coords[0];
  4155. if (instr->coord_components > 1) {
  4156. if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D && instr->is_array && instr->op != nir_texop_txf) {
  4157. coords[1] = apply_round_slice(&ctx->ac, coords[1]);
  4158. }
  4159. address[count++] = coords[1];
  4160. }
  4161. if (instr->coord_components > 2) {
  4162. /* This seems like a bit of a hack - but it passes Vulkan CTS with it */
  4163. if (instr->sampler_dim != GLSL_SAMPLER_DIM_3D &&
  4164. instr->sampler_dim != GLSL_SAMPLER_DIM_CUBE &&
  4165. instr->op != nir_texop_txf) {
  4166. coords[2] = apply_round_slice(&ctx->ac, coords[2]);
  4167. }
  4168. address[count++] = coords[2];
  4169. }
  4170. if (ctx->ac.chip_class >= GFX9) {
  4171. LLVMValueRef filler;
  4172. if (instr->op == nir_texop_txf)
  4173. filler = ctx->ac.i32_0;
  4174. else
  4175. filler = LLVMConstReal(ctx->ac.f32, 0.5);
  4176. if (instr->sampler_dim == GLSL_SAMPLER_DIM_1D) {
  4177. if (instr->is_array) {
  4178. address[count] = address[count - 1];
  4179. address[count - 1] = filler;
  4180. count++;
  4181. } else
  4182. address[count++] = filler;
  4183. }
  4184. }
  4185. }
  4186. /* Pack LOD */
  4187. if (lod && ((instr->op == nir_texop_txl && !lod_is_zero) ||
  4188. instr->op == nir_texop_txf)) {
  4189. address[count++] = lod;
  4190. } else if (instr->op == nir_texop_txf_ms && sample_index) {
  4191. address[count++] = sample_index;
  4192. } else if(instr->op == nir_texop_txs) {
  4193. count = 0;
  4194. if (lod)
  4195. address[count++] = lod;
  4196. else
  4197. address[count++] = ctx->ac.i32_0;
  4198. }
  4199. for (chan = 0; chan < count; chan++) {
  4200. address[chan] = LLVMBuildBitCast(ctx->ac.builder,
  4201. address[chan], ctx->ac.i32, "");
  4202. }
  4203. if (instr->op == nir_texop_samples_identical) {
  4204. LLVMValueRef txf_address[4];
  4205. struct ac_image_args txf_args = { 0 };
  4206. unsigned txf_count = count;
  4207. memcpy(txf_address, address, sizeof(txf_address));
  4208. if (!instr->is_array)
  4209. txf_address[2] = ctx->ac.i32_0;
  4210. txf_address[3] = ctx->ac.i32_0;
  4211. set_tex_fetch_args(&ctx->ac, &txf_args, instr, nir_texop_txf,
  4212. fmask_ptr, NULL,
  4213. txf_address, txf_count, 0xf);
  4214. result = build_tex_intrinsic(ctx, instr, false, &txf_args);
  4215. result = LLVMBuildExtractElement(ctx->ac.builder, result, ctx->ac.i32_0, "");
  4216. result = emit_int_cmp(&ctx->ac, LLVMIntEQ, result, ctx->ac.i32_0);
  4217. goto write_result;
  4218. }
  4219. if (instr->sampler_dim == GLSL_SAMPLER_DIM_MS &&
  4220. instr->op != nir_texop_txs) {
  4221. unsigned sample_chan = instr->is_array ? 3 : 2;
  4222. address[sample_chan] = adjust_sample_index_using_fmask(&ctx->ac,
  4223. address[0],
  4224. address[1],
  4225. instr->is_array ? address[2] : NULL,
  4226. address[sample_chan],
  4227. fmask_ptr);
  4228. }
  4229. if (offsets && instr->op == nir_texop_txf) {
  4230. nir_const_value *const_offset =
  4231. nir_src_as_const_value(instr->src[const_src].src);
  4232. int num_offsets = instr->src[const_src].src.ssa->num_components;
  4233. assert(const_offset);
  4234. num_offsets = MIN2(num_offsets, instr->coord_components);
  4235. if (num_offsets > 2)
  4236. address[2] = LLVMBuildAdd(ctx->ac.builder,
  4237. address[2], LLVMConstInt(ctx->ac.i32, const_offset->i32[2], false), "");
  4238. if (num_offsets > 1)
  4239. address[1] = LLVMBuildAdd(ctx->ac.builder,
  4240. address[1], LLVMConstInt(ctx->ac.i32, const_offset->i32[1], false), "");
  4241. address[0] = LLVMBuildAdd(ctx->ac.builder,
  4242. address[0], LLVMConstInt(ctx->ac.i32, const_offset->i32[0], false), "");
  4243. }
  4244. /* TODO TG4 support */
  4245. if (instr->op == nir_texop_tg4) {
  4246. if (instr->is_shadow)
  4247. dmask = 1;
  4248. else
  4249. dmask = 1 << instr->component;
  4250. }
  4251. set_tex_fetch_args(&ctx->ac, &args, instr, instr->op,
  4252. res_ptr, samp_ptr, address, count, dmask);
  4253. result = build_tex_intrinsic(ctx, instr, lod_is_zero, &args);
  4254. if (instr->op == nir_texop_query_levels)
  4255. result = LLVMBuildExtractElement(ctx->ac.builder, result, LLVMConstInt(ctx->ac.i32, 3, false), "");
  4256. else if (instr->is_shadow && instr->is_new_style_shadow &&
  4257. instr->op != nir_texop_txs && instr->op != nir_texop_lod &&
  4258. instr->op != nir_texop_tg4)
  4259. result = LLVMBuildExtractElement(ctx->ac.builder, result, ctx->ac.i32_0, "");
  4260. else if (instr->op == nir_texop_txs &&
  4261. instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
  4262. instr->is_array) {
  4263. LLVMValueRef two = LLVMConstInt(ctx->ac.i32, 2, false);
  4264. LLVMValueRef six = LLVMConstInt(ctx->ac.i32, 6, false);
  4265. LLVMValueRef z = LLVMBuildExtractElement(ctx->ac.builder, result, two, "");
  4266. z = LLVMBuildSDiv(ctx->ac.builder, z, six, "");
  4267. result = LLVMBuildInsertElement(ctx->ac.builder, result, z, two, "");
  4268. } else if (ctx->ac.chip_class >= GFX9 &&
  4269. instr->op == nir_texop_txs &&
  4270. instr->sampler_dim == GLSL_SAMPLER_DIM_1D &&
  4271. instr->is_array) {
  4272. LLVMValueRef two = LLVMConstInt(ctx->ac.i32, 2, false);
  4273. LLVMValueRef layers = LLVMBuildExtractElement(ctx->ac.builder, result, two, "");
  4274. result = LLVMBuildInsertElement(ctx->ac.builder, result, layers,
  4275. ctx->ac.i32_1, "");
  4276. } else if (instr->dest.ssa.num_components != 4)
  4277. result = trim_vector(&ctx->ac, result, instr->dest.ssa.num_components);
  4278. write_result:
  4279. if (result) {
  4280. assert(instr->dest.is_ssa);
  4281. result = ac_to_integer(&ctx->ac, result);
  4282. _mesa_hash_table_insert(ctx->defs, &instr->dest.ssa, result);
  4283. }
  4284. }
  4285. static void visit_phi(struct ac_nir_context *ctx, nir_phi_instr *instr)
  4286. {
  4287. LLVMTypeRef type = get_def_type(ctx, &instr->dest.ssa);
  4288. LLVMValueRef result = LLVMBuildPhi(ctx->ac.builder, type, "");
  4289. _mesa_hash_table_insert(ctx->defs, &instr->dest.ssa, result);
  4290. _mesa_hash_table_insert(ctx->phis, instr, result);
  4291. }
  4292. static void visit_post_phi(struct ac_nir_context *ctx,
  4293. nir_phi_instr *instr,
  4294. LLVMValueRef llvm_phi)
  4295. {
  4296. nir_foreach_phi_src(src, instr) {
  4297. LLVMBasicBlockRef block = get_block(ctx, src->pred);
  4298. LLVMValueRef llvm_src = get_src(ctx, src->src);
  4299. LLVMAddIncoming(llvm_phi, &llvm_src, &block, 1);
  4300. }
  4301. }
  4302. static void phi_post_pass(struct ac_nir_context *ctx)
  4303. {
  4304. struct hash_entry *entry;
  4305. hash_table_foreach(ctx->phis, entry) {
  4306. visit_post_phi(ctx, (nir_phi_instr*)entry->key,
  4307. (LLVMValueRef)entry->data);
  4308. }
  4309. }
  4310. static void visit_ssa_undef(struct ac_nir_context *ctx,
  4311. const nir_ssa_undef_instr *instr)
  4312. {
  4313. unsigned num_components = instr->def.num_components;
  4314. LLVMValueRef undef;
  4315. if (num_components == 1)
  4316. undef = LLVMGetUndef(ctx->ac.i32);
  4317. else {
  4318. undef = LLVMGetUndef(LLVMVectorType(ctx->ac.i32, num_components));
  4319. }
  4320. _mesa_hash_table_insert(ctx->defs, &instr->def, undef);
  4321. }
  4322. static void visit_jump(struct ac_nir_context *ctx,
  4323. const nir_jump_instr *instr)
  4324. {
  4325. switch (instr->type) {
  4326. case nir_jump_break:
  4327. LLVMBuildBr(ctx->ac.builder, ctx->break_block);
  4328. LLVMClearInsertionPosition(ctx->ac.builder);
  4329. break;
  4330. case nir_jump_continue:
  4331. LLVMBuildBr(ctx->ac.builder, ctx->continue_block);
  4332. LLVMClearInsertionPosition(ctx->ac.builder);
  4333. break;
  4334. default:
  4335. fprintf(stderr, "Unknown NIR jump instr: ");
  4336. nir_print_instr(&instr->instr, stderr);
  4337. fprintf(stderr, "\n");
  4338. abort();
  4339. }
  4340. }
  4341. static void visit_cf_list(struct ac_nir_context *ctx,
  4342. struct exec_list *list);
  4343. static void visit_block(struct ac_nir_context *ctx, nir_block *block)
  4344. {
  4345. LLVMBasicBlockRef llvm_block = LLVMGetInsertBlock(ctx->ac.builder);
  4346. nir_foreach_instr(instr, block)
  4347. {
  4348. switch (instr->type) {
  4349. case nir_instr_type_alu:
  4350. visit_alu(ctx, nir_instr_as_alu(instr));
  4351. break;
  4352. case nir_instr_type_load_const:
  4353. visit_load_const(ctx, nir_instr_as_load_const(instr));
  4354. break;
  4355. case nir_instr_type_intrinsic:
  4356. visit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
  4357. break;
  4358. case nir_instr_type_tex:
  4359. visit_tex(ctx, nir_instr_as_tex(instr));
  4360. break;
  4361. case nir_instr_type_phi:
  4362. visit_phi(ctx, nir_instr_as_phi(instr));
  4363. break;
  4364. case nir_instr_type_ssa_undef:
  4365. visit_ssa_undef(ctx, nir_instr_as_ssa_undef(instr));
  4366. break;
  4367. case nir_instr_type_jump:
  4368. visit_jump(ctx, nir_instr_as_jump(instr));
  4369. break;
  4370. default:
  4371. fprintf(stderr, "Unknown NIR instr type: ");
  4372. nir_print_instr(instr, stderr);
  4373. fprintf(stderr, "\n");
  4374. abort();
  4375. }
  4376. }
  4377. _mesa_hash_table_insert(ctx->defs, block, llvm_block);
  4378. }
  4379. static void visit_if(struct ac_nir_context *ctx, nir_if *if_stmt)
  4380. {
  4381. LLVMValueRef value = get_src(ctx, if_stmt->condition);
  4382. LLVMValueRef fn = LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx->ac.builder));
  4383. LLVMBasicBlockRef merge_block =
  4384. LLVMAppendBasicBlockInContext(ctx->ac.context, fn, "");
  4385. LLVMBasicBlockRef if_block =
  4386. LLVMAppendBasicBlockInContext(ctx->ac.context, fn, "");
  4387. LLVMBasicBlockRef else_block = merge_block;
  4388. if (!exec_list_is_empty(&if_stmt->else_list))
  4389. else_block = LLVMAppendBasicBlockInContext(
  4390. ctx->ac.context, fn, "");
  4391. LLVMValueRef cond = LLVMBuildICmp(ctx->ac.builder, LLVMIntNE, value,
  4392. LLVMConstInt(ctx->ac.i32, 0, false), "");
  4393. LLVMBuildCondBr(ctx->ac.builder, cond, if_block, else_block);
  4394. LLVMPositionBuilderAtEnd(ctx->ac.builder, if_block);
  4395. visit_cf_list(ctx, &if_stmt->then_list);
  4396. if (LLVMGetInsertBlock(ctx->ac.builder))
  4397. LLVMBuildBr(ctx->ac.builder, merge_block);
  4398. if (!exec_list_is_empty(&if_stmt->else_list)) {
  4399. LLVMPositionBuilderAtEnd(ctx->ac.builder, else_block);
  4400. visit_cf_list(ctx, &if_stmt->else_list);
  4401. if (LLVMGetInsertBlock(ctx->ac.builder))
  4402. LLVMBuildBr(ctx->ac.builder, merge_block);
  4403. }
  4404. LLVMPositionBuilderAtEnd(ctx->ac.builder, merge_block);
  4405. }
  4406. static void visit_loop(struct ac_nir_context *ctx, nir_loop *loop)
  4407. {
  4408. LLVMValueRef fn = LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx->ac.builder));
  4409. LLVMBasicBlockRef continue_parent = ctx->continue_block;
  4410. LLVMBasicBlockRef break_parent = ctx->break_block;
  4411. ctx->continue_block =
  4412. LLVMAppendBasicBlockInContext(ctx->ac.context, fn, "");
  4413. ctx->break_block =
  4414. LLVMAppendBasicBlockInContext(ctx->ac.context, fn, "");
  4415. LLVMBuildBr(ctx->ac.builder, ctx->continue_block);
  4416. LLVMPositionBuilderAtEnd(ctx->ac.builder, ctx->continue_block);
  4417. visit_cf_list(ctx, &loop->body);
  4418. if (LLVMGetInsertBlock(ctx->ac.builder))
  4419. LLVMBuildBr(ctx->ac.builder, ctx->continue_block);
  4420. LLVMPositionBuilderAtEnd(ctx->ac.builder, ctx->break_block);
  4421. ctx->continue_block = continue_parent;
  4422. ctx->break_block = break_parent;
  4423. }
  4424. static void visit_cf_list(struct ac_nir_context *ctx,
  4425. struct exec_list *list)
  4426. {
  4427. foreach_list_typed(nir_cf_node, node, node, list)
  4428. {
  4429. switch (node->type) {
  4430. case nir_cf_node_block:
  4431. visit_block(ctx, nir_cf_node_as_block(node));
  4432. break;
  4433. case nir_cf_node_if:
  4434. visit_if(ctx, nir_cf_node_as_if(node));
  4435. break;
  4436. case nir_cf_node_loop:
  4437. visit_loop(ctx, nir_cf_node_as_loop(node));
  4438. break;
  4439. default:
  4440. assert(0);
  4441. }
  4442. }
  4443. }
  4444. static void
  4445. handle_vs_input_decl(struct nir_to_llvm_context *ctx,
  4446. struct nir_variable *variable)
  4447. {
  4448. LLVMValueRef t_list_ptr = ctx->vertex_buffers;
  4449. LLVMValueRef t_offset;
  4450. LLVMValueRef t_list;
  4451. LLVMValueRef input;
  4452. LLVMValueRef buffer_index;
  4453. int index = variable->data.location - VERT_ATTRIB_GENERIC0;
  4454. int idx = variable->data.location;
  4455. unsigned attrib_count = glsl_count_attribute_slots(variable->type, true);
  4456. variable->data.driver_location = idx * 4;
  4457. if (ctx->options->key.vs.instance_rate_inputs & (1u << index)) {
  4458. buffer_index = LLVMBuildAdd(ctx->builder, ctx->abi.instance_id,
  4459. ctx->abi.start_instance, "");
  4460. ctx->shader_info->vs.vgpr_comp_cnt = MAX2(3,
  4461. ctx->shader_info->vs.vgpr_comp_cnt);
  4462. } else
  4463. buffer_index = LLVMBuildAdd(ctx->builder, ctx->abi.vertex_id,
  4464. ctx->abi.base_vertex, "");
  4465. for (unsigned i = 0; i < attrib_count; ++i, ++idx) {
  4466. t_offset = LLVMConstInt(ctx->i32, index + i, false);
  4467. t_list = ac_build_load_to_sgpr(&ctx->ac, t_list_ptr, t_offset);
  4468. input = ac_build_buffer_load_format(&ctx->ac, t_list,
  4469. buffer_index,
  4470. LLVMConstInt(ctx->i32, 0, false),
  4471. true);
  4472. for (unsigned chan = 0; chan < 4; chan++) {
  4473. LLVMValueRef llvm_chan = LLVMConstInt(ctx->i32, chan, false);
  4474. ctx->inputs[radeon_llvm_reg_index_soa(idx, chan)] =
  4475. ac_to_integer(&ctx->ac, LLVMBuildExtractElement(ctx->builder,
  4476. input, llvm_chan, ""));
  4477. }
  4478. }
  4479. }
  4480. static void interp_fs_input(struct nir_to_llvm_context *ctx,
  4481. unsigned attr,
  4482. LLVMValueRef interp_param,
  4483. LLVMValueRef prim_mask,
  4484. LLVMValueRef result[4])
  4485. {
  4486. LLVMValueRef attr_number;
  4487. unsigned chan;
  4488. LLVMValueRef i, j;
  4489. bool interp = interp_param != NULL;
  4490. attr_number = LLVMConstInt(ctx->i32, attr, false);
  4491. /* fs.constant returns the param from the middle vertex, so it's not
  4492. * really useful for flat shading. It's meant to be used for custom
  4493. * interpolation (but the intrinsic can't fetch from the other two
  4494. * vertices).
  4495. *
  4496. * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
  4497. * to do the right thing. The only reason we use fs.constant is that
  4498. * fs.interp cannot be used on integers, because they can be equal
  4499. * to NaN.
  4500. */
  4501. if (interp) {
  4502. interp_param = LLVMBuildBitCast(ctx->builder, interp_param,
  4503. LLVMVectorType(ctx->f32, 2), "");
  4504. i = LLVMBuildExtractElement(ctx->builder, interp_param,
  4505. ctx->i32zero, "");
  4506. j = LLVMBuildExtractElement(ctx->builder, interp_param,
  4507. ctx->i32one, "");
  4508. }
  4509. for (chan = 0; chan < 4; chan++) {
  4510. LLVMValueRef llvm_chan = LLVMConstInt(ctx->i32, chan, false);
  4511. if (interp) {
  4512. result[chan] = ac_build_fs_interp(&ctx->ac,
  4513. llvm_chan,
  4514. attr_number,
  4515. prim_mask, i, j);
  4516. } else {
  4517. result[chan] = ac_build_fs_interp_mov(&ctx->ac,
  4518. LLVMConstInt(ctx->i32, 2, false),
  4519. llvm_chan,
  4520. attr_number,
  4521. prim_mask);
  4522. }
  4523. }
  4524. }
  4525. static void
  4526. handle_fs_input_decl(struct nir_to_llvm_context *ctx,
  4527. struct nir_variable *variable)
  4528. {
  4529. int idx = variable->data.location;
  4530. unsigned attrib_count = glsl_count_attribute_slots(variable->type, false);
  4531. LLVMValueRef interp;
  4532. variable->data.driver_location = idx * 4;
  4533. ctx->input_mask |= ((1ull << attrib_count) - 1) << variable->data.location;
  4534. if (glsl_get_base_type(glsl_without_array(variable->type)) == GLSL_TYPE_FLOAT) {
  4535. unsigned interp_type;
  4536. if (variable->data.sample) {
  4537. interp_type = INTERP_SAMPLE;
  4538. ctx->shader_info->info.ps.force_persample = true;
  4539. } else if (variable->data.centroid)
  4540. interp_type = INTERP_CENTROID;
  4541. else
  4542. interp_type = INTERP_CENTER;
  4543. interp = lookup_interp_param(ctx, variable->data.interpolation, interp_type);
  4544. } else
  4545. interp = NULL;
  4546. for (unsigned i = 0; i < attrib_count; ++i)
  4547. ctx->inputs[radeon_llvm_reg_index_soa(idx + i, 0)] = interp;
  4548. }
  4549. static void
  4550. handle_vs_inputs(struct nir_to_llvm_context *ctx,
  4551. struct nir_shader *nir) {
  4552. nir_foreach_variable(variable, &nir->inputs)
  4553. handle_vs_input_decl(ctx, variable);
  4554. }
  4555. static void
  4556. prepare_interp_optimize(struct nir_to_llvm_context *ctx,
  4557. struct nir_shader *nir)
  4558. {
  4559. if (!ctx->options->key.fs.multisample)
  4560. return;
  4561. bool uses_center = false;
  4562. bool uses_centroid = false;
  4563. nir_foreach_variable(variable, &nir->inputs) {
  4564. if (glsl_get_base_type(glsl_without_array(variable->type)) != GLSL_TYPE_FLOAT ||
  4565. variable->data.sample)
  4566. continue;
  4567. if (variable->data.centroid)
  4568. uses_centroid = true;
  4569. else
  4570. uses_center = true;
  4571. }
  4572. if (uses_center && uses_centroid) {
  4573. LLVMValueRef sel = LLVMBuildICmp(ctx->builder, LLVMIntSLT, ctx->prim_mask, ctx->ac.i32_0, "");
  4574. ctx->persp_centroid = LLVMBuildSelect(ctx->builder, sel, ctx->persp_center, ctx->persp_centroid, "");
  4575. ctx->linear_centroid = LLVMBuildSelect(ctx->builder, sel, ctx->linear_center, ctx->linear_centroid, "");
  4576. }
  4577. }
  4578. static void
  4579. handle_fs_inputs(struct nir_to_llvm_context *ctx,
  4580. struct nir_shader *nir)
  4581. {
  4582. prepare_interp_optimize(ctx, nir);
  4583. nir_foreach_variable(variable, &nir->inputs)
  4584. handle_fs_input_decl(ctx, variable);
  4585. unsigned index = 0;
  4586. if (ctx->shader_info->info.ps.uses_input_attachments ||
  4587. ctx->shader_info->info.needs_multiview_view_index)
  4588. ctx->input_mask |= 1ull << VARYING_SLOT_LAYER;
  4589. for (unsigned i = 0; i < RADEON_LLVM_MAX_INPUTS; ++i) {
  4590. LLVMValueRef interp_param;
  4591. LLVMValueRef *inputs = ctx->inputs +radeon_llvm_reg_index_soa(i, 0);
  4592. if (!(ctx->input_mask & (1ull << i)))
  4593. continue;
  4594. if (i >= VARYING_SLOT_VAR0 || i == VARYING_SLOT_PNTC ||
  4595. i == VARYING_SLOT_PRIMITIVE_ID || i == VARYING_SLOT_LAYER) {
  4596. interp_param = *inputs;
  4597. interp_fs_input(ctx, index, interp_param, ctx->prim_mask,
  4598. inputs);
  4599. if (!interp_param)
  4600. ctx->shader_info->fs.flat_shaded_mask |= 1u << index;
  4601. ++index;
  4602. } else if (i == VARYING_SLOT_POS) {
  4603. for(int i = 0; i < 3; ++i)
  4604. inputs[i] = ctx->abi.frag_pos[i];
  4605. inputs[3] = ac_build_fdiv(&ctx->ac, ctx->f32one,
  4606. ctx->abi.frag_pos[3]);
  4607. }
  4608. }
  4609. ctx->shader_info->fs.num_interp = index;
  4610. if (ctx->input_mask & (1 << VARYING_SLOT_PNTC))
  4611. ctx->shader_info->fs.has_pcoord = true;
  4612. if (ctx->input_mask & (1 << VARYING_SLOT_PRIMITIVE_ID))
  4613. ctx->shader_info->fs.prim_id_input = true;
  4614. if (ctx->input_mask & (1 << VARYING_SLOT_LAYER))
  4615. ctx->shader_info->fs.layer_input = true;
  4616. ctx->shader_info->fs.input_mask = ctx->input_mask >> VARYING_SLOT_VAR0;
  4617. if (ctx->shader_info->info.needs_multiview_view_index)
  4618. ctx->view_index = ctx->inputs[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER, 0)];
  4619. }
  4620. static LLVMValueRef
  4621. ac_build_alloca(struct ac_llvm_context *ac,
  4622. LLVMTypeRef type,
  4623. const char *name)
  4624. {
  4625. LLVMBuilderRef builder = ac->builder;
  4626. LLVMBasicBlockRef current_block = LLVMGetInsertBlock(builder);
  4627. LLVMValueRef function = LLVMGetBasicBlockParent(current_block);
  4628. LLVMBasicBlockRef first_block = LLVMGetEntryBasicBlock(function);
  4629. LLVMValueRef first_instr = LLVMGetFirstInstruction(first_block);
  4630. LLVMBuilderRef first_builder = LLVMCreateBuilderInContext(ac->context);
  4631. LLVMValueRef res;
  4632. if (first_instr) {
  4633. LLVMPositionBuilderBefore(first_builder, first_instr);
  4634. } else {
  4635. LLVMPositionBuilderAtEnd(first_builder, first_block);
  4636. }
  4637. res = LLVMBuildAlloca(first_builder, type, name);
  4638. LLVMBuildStore(builder, LLVMConstNull(type), res);
  4639. LLVMDisposeBuilder(first_builder);
  4640. return res;
  4641. }
  4642. static LLVMValueRef si_build_alloca_undef(struct ac_llvm_context *ac,
  4643. LLVMTypeRef type,
  4644. const char *name)
  4645. {
  4646. LLVMValueRef ptr = ac_build_alloca(ac, type, name);
  4647. LLVMBuildStore(ac->builder, LLVMGetUndef(type), ptr);
  4648. return ptr;
  4649. }
  4650. static void
  4651. scan_shader_output_decl(struct nir_to_llvm_context *ctx,
  4652. struct nir_variable *variable,
  4653. struct nir_shader *shader,
  4654. gl_shader_stage stage)
  4655. {
  4656. int idx = variable->data.location + variable->data.index;
  4657. unsigned attrib_count = glsl_count_attribute_slots(variable->type, false);
  4658. uint64_t mask_attribs;
  4659. variable->data.driver_location = idx * 4;
  4660. /* tess ctrl has it's own load/store paths for outputs */
  4661. if (stage == MESA_SHADER_TESS_CTRL)
  4662. return;
  4663. mask_attribs = ((1ull << attrib_count) - 1) << idx;
  4664. if (stage == MESA_SHADER_VERTEX ||
  4665. stage == MESA_SHADER_TESS_EVAL ||
  4666. stage == MESA_SHADER_GEOMETRY) {
  4667. if (idx == VARYING_SLOT_CLIP_DIST0) {
  4668. int length = shader->info.clip_distance_array_size +
  4669. shader->info.cull_distance_array_size;
  4670. if (stage == MESA_SHADER_VERTEX) {
  4671. ctx->shader_info->vs.outinfo.clip_dist_mask = (1 << shader->info.clip_distance_array_size) - 1;
  4672. ctx->shader_info->vs.outinfo.cull_dist_mask = (1 << shader->info.cull_distance_array_size) - 1;
  4673. }
  4674. if (stage == MESA_SHADER_TESS_EVAL) {
  4675. ctx->shader_info->tes.outinfo.clip_dist_mask = (1 << shader->info.clip_distance_array_size) - 1;
  4676. ctx->shader_info->tes.outinfo.cull_dist_mask = (1 << shader->info.cull_distance_array_size) - 1;
  4677. }
  4678. if (length > 4)
  4679. attrib_count = 2;
  4680. else
  4681. attrib_count = 1;
  4682. mask_attribs = 1ull << idx;
  4683. }
  4684. }
  4685. ctx->output_mask |= mask_attribs;
  4686. }
  4687. static void
  4688. handle_shader_output_decl(struct ac_nir_context *ctx,
  4689. struct nir_shader *nir,
  4690. struct nir_variable *variable)
  4691. {
  4692. unsigned output_loc = variable->data.driver_location / 4;
  4693. unsigned attrib_count = glsl_count_attribute_slots(variable->type, false);
  4694. /* tess ctrl has it's own load/store paths for outputs */
  4695. if (ctx->stage == MESA_SHADER_TESS_CTRL)
  4696. return;
  4697. if (ctx->stage == MESA_SHADER_VERTEX ||
  4698. ctx->stage == MESA_SHADER_TESS_EVAL ||
  4699. ctx->stage == MESA_SHADER_GEOMETRY) {
  4700. int idx = variable->data.location + variable->data.index;
  4701. if (idx == VARYING_SLOT_CLIP_DIST0) {
  4702. int length = nir->info.clip_distance_array_size +
  4703. nir->info.cull_distance_array_size;
  4704. if (length > 4)
  4705. attrib_count = 2;
  4706. else
  4707. attrib_count = 1;
  4708. }
  4709. }
  4710. for (unsigned i = 0; i < attrib_count; ++i) {
  4711. for (unsigned chan = 0; chan < 4; chan++) {
  4712. ctx->outputs[radeon_llvm_reg_index_soa(output_loc + i, chan)] =
  4713. si_build_alloca_undef(&ctx->ac, ctx->ac.f32, "");
  4714. }
  4715. }
  4716. }
  4717. static LLVMTypeRef
  4718. glsl_base_to_llvm_type(struct nir_to_llvm_context *ctx,
  4719. enum glsl_base_type type)
  4720. {
  4721. switch (type) {
  4722. case GLSL_TYPE_INT:
  4723. case GLSL_TYPE_UINT:
  4724. case GLSL_TYPE_BOOL:
  4725. case GLSL_TYPE_SUBROUTINE:
  4726. return ctx->i32;
  4727. case GLSL_TYPE_FLOAT: /* TODO handle mediump */
  4728. return ctx->f32;
  4729. case GLSL_TYPE_INT64:
  4730. case GLSL_TYPE_UINT64:
  4731. return ctx->i64;
  4732. case GLSL_TYPE_DOUBLE:
  4733. return ctx->f64;
  4734. default:
  4735. unreachable("unknown GLSL type");
  4736. }
  4737. }
  4738. static LLVMTypeRef
  4739. glsl_to_llvm_type(struct nir_to_llvm_context *ctx,
  4740. const struct glsl_type *type)
  4741. {
  4742. if (glsl_type_is_scalar(type)) {
  4743. return glsl_base_to_llvm_type(ctx, glsl_get_base_type(type));
  4744. }
  4745. if (glsl_type_is_vector(type)) {
  4746. return LLVMVectorType(
  4747. glsl_base_to_llvm_type(ctx, glsl_get_base_type(type)),
  4748. glsl_get_vector_elements(type));
  4749. }
  4750. if (glsl_type_is_matrix(type)) {
  4751. return LLVMArrayType(
  4752. glsl_to_llvm_type(ctx, glsl_get_column_type(type)),
  4753. glsl_get_matrix_columns(type));
  4754. }
  4755. if (glsl_type_is_array(type)) {
  4756. return LLVMArrayType(
  4757. glsl_to_llvm_type(ctx, glsl_get_array_element(type)),
  4758. glsl_get_length(type));
  4759. }
  4760. assert(glsl_type_is_struct(type));
  4761. LLVMTypeRef member_types[glsl_get_length(type)];
  4762. for (unsigned i = 0; i < glsl_get_length(type); i++) {
  4763. member_types[i] =
  4764. glsl_to_llvm_type(ctx,
  4765. glsl_get_struct_field(type, i));
  4766. }
  4767. return LLVMStructTypeInContext(ctx->context, member_types,
  4768. glsl_get_length(type), false);
  4769. }
  4770. static void
  4771. setup_locals(struct ac_nir_context *ctx,
  4772. struct nir_function *func)
  4773. {
  4774. int i, j;
  4775. ctx->num_locals = 0;
  4776. nir_foreach_variable(variable, &func->impl->locals) {
  4777. unsigned attrib_count = glsl_count_attribute_slots(variable->type, false);
  4778. variable->data.driver_location = ctx->num_locals * 4;
  4779. ctx->num_locals += attrib_count;
  4780. }
  4781. ctx->locals = malloc(4 * ctx->num_locals * sizeof(LLVMValueRef));
  4782. if (!ctx->locals)
  4783. return;
  4784. for (i = 0; i < ctx->num_locals; i++) {
  4785. for (j = 0; j < 4; j++) {
  4786. ctx->locals[i * 4 + j] =
  4787. si_build_alloca_undef(&ctx->ac, ctx->ac.f32, "temp");
  4788. }
  4789. }
  4790. }
  4791. static void
  4792. setup_shared(struct ac_nir_context *ctx,
  4793. struct nir_shader *nir)
  4794. {
  4795. nir_foreach_variable(variable, &nir->shared) {
  4796. LLVMValueRef shared =
  4797. LLVMAddGlobalInAddressSpace(
  4798. ctx->ac.module, glsl_to_llvm_type(ctx->nctx, variable->type),
  4799. variable->name ? variable->name : "",
  4800. LOCAL_ADDR_SPACE);
  4801. _mesa_hash_table_insert(ctx->vars, variable, shared);
  4802. }
  4803. }
  4804. static LLVMValueRef
  4805. emit_float_saturate(struct ac_llvm_context *ctx, LLVMValueRef v, float lo, float hi)
  4806. {
  4807. v = ac_to_float(ctx, v);
  4808. v = emit_intrin_2f_param(ctx, "llvm.maxnum", ctx->f32, v, LLVMConstReal(ctx->f32, lo));
  4809. return emit_intrin_2f_param(ctx, "llvm.minnum", ctx->f32, v, LLVMConstReal(ctx->f32, hi));
  4810. }
  4811. static LLVMValueRef emit_pack_int16(struct nir_to_llvm_context *ctx,
  4812. LLVMValueRef src0, LLVMValueRef src1)
  4813. {
  4814. LLVMValueRef const16 = LLVMConstInt(ctx->i32, 16, false);
  4815. LLVMValueRef comp[2];
  4816. comp[0] = LLVMBuildAnd(ctx->builder, src0, LLVMConstInt(ctx-> i32, 65535, 0), "");
  4817. comp[1] = LLVMBuildAnd(ctx->builder, src1, LLVMConstInt(ctx-> i32, 65535, 0), "");
  4818. comp[1] = LLVMBuildShl(ctx->builder, comp[1], const16, "");
  4819. return LLVMBuildOr(ctx->builder, comp[0], comp[1], "");
  4820. }
  4821. /* Initialize arguments for the shader export intrinsic */
  4822. static void
  4823. si_llvm_init_export_args(struct nir_to_llvm_context *ctx,
  4824. LLVMValueRef *values,
  4825. unsigned target,
  4826. struct ac_export_args *args)
  4827. {
  4828. /* Default is 0xf. Adjusted below depending on the format. */
  4829. args->enabled_channels = 0xf;
  4830. /* Specify whether the EXEC mask represents the valid mask */
  4831. args->valid_mask = 0;
  4832. /* Specify whether this is the last export */
  4833. args->done = 0;
  4834. /* Specify the target we are exporting */
  4835. args->target = target;
  4836. args->compr = false;
  4837. args->out[0] = LLVMGetUndef(ctx->f32);
  4838. args->out[1] = LLVMGetUndef(ctx->f32);
  4839. args->out[2] = LLVMGetUndef(ctx->f32);
  4840. args->out[3] = LLVMGetUndef(ctx->f32);
  4841. if (!values)
  4842. return;
  4843. if (ctx->stage == MESA_SHADER_FRAGMENT && target >= V_008DFC_SQ_EXP_MRT) {
  4844. LLVMValueRef val[4];
  4845. unsigned index = target - V_008DFC_SQ_EXP_MRT;
  4846. unsigned col_format = (ctx->options->key.fs.col_format >> (4 * index)) & 0xf;
  4847. bool is_int8 = (ctx->options->key.fs.is_int8 >> index) & 1;
  4848. bool is_int10 = (ctx->options->key.fs.is_int10 >> index) & 1;
  4849. switch(col_format) {
  4850. case V_028714_SPI_SHADER_ZERO:
  4851. args->enabled_channels = 0; /* writemask */
  4852. args->target = V_008DFC_SQ_EXP_NULL;
  4853. break;
  4854. case V_028714_SPI_SHADER_32_R:
  4855. args->enabled_channels = 1;
  4856. args->out[0] = values[0];
  4857. break;
  4858. case V_028714_SPI_SHADER_32_GR:
  4859. args->enabled_channels = 0x3;
  4860. args->out[0] = values[0];
  4861. args->out[1] = values[1];
  4862. break;
  4863. case V_028714_SPI_SHADER_32_AR:
  4864. args->enabled_channels = 0x9;
  4865. args->out[0] = values[0];
  4866. args->out[3] = values[3];
  4867. break;
  4868. case V_028714_SPI_SHADER_FP16_ABGR:
  4869. args->compr = 1;
  4870. for (unsigned chan = 0; chan < 2; chan++) {
  4871. LLVMValueRef pack_args[2] = {
  4872. values[2 * chan],
  4873. values[2 * chan + 1]
  4874. };
  4875. LLVMValueRef packed;
  4876. packed = ac_build_cvt_pkrtz_f16(&ctx->ac, pack_args);
  4877. args->out[chan] = packed;
  4878. }
  4879. break;
  4880. case V_028714_SPI_SHADER_UNORM16_ABGR:
  4881. for (unsigned chan = 0; chan < 4; chan++) {
  4882. val[chan] = ac_build_clamp(&ctx->ac, values[chan]);
  4883. val[chan] = LLVMBuildFMul(ctx->builder, val[chan],
  4884. LLVMConstReal(ctx->f32, 65535), "");
  4885. val[chan] = LLVMBuildFAdd(ctx->builder, val[chan],
  4886. LLVMConstReal(ctx->f32, 0.5), "");
  4887. val[chan] = LLVMBuildFPToUI(ctx->builder, val[chan],
  4888. ctx->i32, "");
  4889. }
  4890. args->compr = 1;
  4891. args->out[0] = emit_pack_int16(ctx, val[0], val[1]);
  4892. args->out[1] = emit_pack_int16(ctx, val[2], val[3]);
  4893. break;
  4894. case V_028714_SPI_SHADER_SNORM16_ABGR:
  4895. for (unsigned chan = 0; chan < 4; chan++) {
  4896. val[chan] = emit_float_saturate(&ctx->ac, values[chan], -1, 1);
  4897. val[chan] = LLVMBuildFMul(ctx->builder, val[chan],
  4898. LLVMConstReal(ctx->f32, 32767), "");
  4899. /* If positive, add 0.5, else add -0.5. */
  4900. val[chan] = LLVMBuildFAdd(ctx->builder, val[chan],
  4901. LLVMBuildSelect(ctx->builder,
  4902. LLVMBuildFCmp(ctx->builder, LLVMRealOGE,
  4903. val[chan], ctx->f32zero, ""),
  4904. LLVMConstReal(ctx->f32, 0.5),
  4905. LLVMConstReal(ctx->f32, -0.5), ""), "");
  4906. val[chan] = LLVMBuildFPToSI(ctx->builder, val[chan], ctx->i32, "");
  4907. }
  4908. args->compr = 1;
  4909. args->out[0] = emit_pack_int16(ctx, val[0], val[1]);
  4910. args->out[1] = emit_pack_int16(ctx, val[2], val[3]);
  4911. break;
  4912. case V_028714_SPI_SHADER_UINT16_ABGR: {
  4913. LLVMValueRef max_rgb = LLVMConstInt(ctx->i32,
  4914. is_int8 ? 255 : is_int10 ? 1023 : 65535, 0);
  4915. LLVMValueRef max_alpha = !is_int10 ? max_rgb : LLVMConstInt(ctx->i32, 3, 0);
  4916. for (unsigned chan = 0; chan < 4; chan++) {
  4917. val[chan] = ac_to_integer(&ctx->ac, values[chan]);
  4918. val[chan] = emit_minmax_int(&ctx->ac, LLVMIntULT, val[chan], chan == 3 ? max_alpha : max_rgb);
  4919. }
  4920. args->compr = 1;
  4921. args->out[0] = emit_pack_int16(ctx, val[0], val[1]);
  4922. args->out[1] = emit_pack_int16(ctx, val[2], val[3]);
  4923. break;
  4924. }
  4925. case V_028714_SPI_SHADER_SINT16_ABGR: {
  4926. LLVMValueRef max_rgb = LLVMConstInt(ctx->i32,
  4927. is_int8 ? 127 : is_int10 ? 511 : 32767, 0);
  4928. LLVMValueRef min_rgb = LLVMConstInt(ctx->i32,
  4929. is_int8 ? -128 : is_int10 ? -512 : -32768, 0);
  4930. LLVMValueRef max_alpha = !is_int10 ? max_rgb : ctx->i32one;
  4931. LLVMValueRef min_alpha = !is_int10 ? min_rgb : LLVMConstInt(ctx->i32, -2, 0);
  4932. /* Clamp. */
  4933. for (unsigned chan = 0; chan < 4; chan++) {
  4934. val[chan] = ac_to_integer(&ctx->ac, values[chan]);
  4935. val[chan] = emit_minmax_int(&ctx->ac, LLVMIntSLT, val[chan], chan == 3 ? max_alpha : max_rgb);
  4936. val[chan] = emit_minmax_int(&ctx->ac, LLVMIntSGT, val[chan], chan == 3 ? min_alpha : min_rgb);
  4937. }
  4938. args->compr = 1;
  4939. args->out[0] = emit_pack_int16(ctx, val[0], val[1]);
  4940. args->out[1] = emit_pack_int16(ctx, val[2], val[3]);
  4941. break;
  4942. }
  4943. default:
  4944. case V_028714_SPI_SHADER_32_ABGR:
  4945. memcpy(&args->out[0], values, sizeof(values[0]) * 4);
  4946. break;
  4947. }
  4948. } else
  4949. memcpy(&args->out[0], values, sizeof(values[0]) * 4);
  4950. for (unsigned i = 0; i < 4; ++i)
  4951. args->out[i] = ac_to_float(&ctx->ac, args->out[i]);
  4952. }
  4953. static void
  4954. handle_vs_outputs_post(struct nir_to_llvm_context *ctx,
  4955. bool export_prim_id,
  4956. struct ac_vs_output_info *outinfo)
  4957. {
  4958. uint32_t param_count = 0;
  4959. unsigned target;
  4960. unsigned pos_idx, num_pos_exports = 0;
  4961. struct ac_export_args args, pos_args[4] = {};
  4962. LLVMValueRef psize_value = NULL, layer_value = NULL, viewport_index_value = NULL;
  4963. int i;
  4964. if (ctx->options->key.has_multiview_view_index) {
  4965. LLVMValueRef* tmp_out = &ctx->nir->outputs[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER, 0)];
  4966. if(!*tmp_out) {
  4967. for(unsigned i = 0; i < 4; ++i)
  4968. ctx->nir->outputs[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER, i)] =
  4969. si_build_alloca_undef(&ctx->ac, ctx->ac.f32, "");
  4970. }
  4971. LLVMBuildStore(ctx->builder, ac_to_float(&ctx->ac, ctx->view_index), *tmp_out);
  4972. ctx->output_mask |= 1ull << VARYING_SLOT_LAYER;
  4973. }
  4974. memset(outinfo->vs_output_param_offset, AC_EXP_PARAM_UNDEFINED,
  4975. sizeof(outinfo->vs_output_param_offset));
  4976. if (ctx->output_mask & (1ull << VARYING_SLOT_CLIP_DIST0)) {
  4977. LLVMValueRef slots[8];
  4978. unsigned j;
  4979. if (outinfo->cull_dist_mask)
  4980. outinfo->cull_dist_mask <<= ctx->num_output_clips;
  4981. i = VARYING_SLOT_CLIP_DIST0;
  4982. for (j = 0; j < ctx->num_output_clips + ctx->num_output_culls; j++)
  4983. slots[j] = ac_to_float(&ctx->ac, LLVMBuildLoad(ctx->builder,
  4984. ctx->nir->outputs[radeon_llvm_reg_index_soa(i, j)], ""));
  4985. for (i = ctx->num_output_clips + ctx->num_output_culls; i < 8; i++)
  4986. slots[i] = LLVMGetUndef(ctx->f32);
  4987. if (ctx->num_output_clips + ctx->num_output_culls > 4) {
  4988. target = V_008DFC_SQ_EXP_POS + 3;
  4989. si_llvm_init_export_args(ctx, &slots[4], target, &args);
  4990. memcpy(&pos_args[target - V_008DFC_SQ_EXP_POS],
  4991. &args, sizeof(args));
  4992. }
  4993. target = V_008DFC_SQ_EXP_POS + 2;
  4994. si_llvm_init_export_args(ctx, &slots[0], target, &args);
  4995. memcpy(&pos_args[target - V_008DFC_SQ_EXP_POS],
  4996. &args, sizeof(args));
  4997. }
  4998. LLVMValueRef pos_values[4] = {ctx->f32zero, ctx->f32zero, ctx->f32zero, ctx->f32one};
  4999. if (ctx->output_mask & (1ull << VARYING_SLOT_POS)) {
  5000. for (unsigned j = 0; j < 4; j++)
  5001. pos_values[j] = LLVMBuildLoad(ctx->builder,
  5002. ctx->nir->outputs[radeon_llvm_reg_index_soa(VARYING_SLOT_POS, j)], "");
  5003. }
  5004. si_llvm_init_export_args(ctx, pos_values, V_008DFC_SQ_EXP_POS, &pos_args[0]);
  5005. if (ctx->output_mask & (1ull << VARYING_SLOT_PSIZ)) {
  5006. outinfo->writes_pointsize = true;
  5007. psize_value = LLVMBuildLoad(ctx->builder,
  5008. ctx->nir->outputs[radeon_llvm_reg_index_soa(VARYING_SLOT_PSIZ, 0)], "");
  5009. }
  5010. if (ctx->output_mask & (1ull << VARYING_SLOT_LAYER)) {
  5011. outinfo->writes_layer = true;
  5012. layer_value = LLVMBuildLoad(ctx->builder,
  5013. ctx->nir->outputs[radeon_llvm_reg_index_soa(VARYING_SLOT_LAYER, 0)], "");
  5014. }
  5015. if (ctx->output_mask & (1ull << VARYING_SLOT_VIEWPORT)) {
  5016. outinfo->writes_viewport_index = true;
  5017. viewport_index_value = LLVMBuildLoad(ctx->builder,
  5018. ctx->nir->outputs[radeon_llvm_reg_index_soa(VARYING_SLOT_VIEWPORT, 0)], "");
  5019. }
  5020. if (outinfo->writes_pointsize ||
  5021. outinfo->writes_layer ||
  5022. outinfo->writes_viewport_index) {
  5023. pos_args[1].enabled_channels = ((outinfo->writes_pointsize == true ? 1 : 0) |
  5024. (outinfo->writes_layer == true ? 4 : 0));
  5025. pos_args[1].valid_mask = 0;
  5026. pos_args[1].done = 0;
  5027. pos_args[1].target = V_008DFC_SQ_EXP_POS + 1;
  5028. pos_args[1].compr = 0;
  5029. pos_args[1].out[0] = ctx->f32zero; /* X */
  5030. pos_args[1].out[1] = ctx->f32zero; /* Y */
  5031. pos_args[1].out[2] = ctx->f32zero; /* Z */
  5032. pos_args[1].out[3] = ctx->f32zero; /* W */
  5033. if (outinfo->writes_pointsize == true)
  5034. pos_args[1].out[0] = psize_value;
  5035. if (outinfo->writes_layer == true)
  5036. pos_args[1].out[2] = layer_value;
  5037. if (outinfo->writes_viewport_index == true) {
  5038. if (ctx->options->chip_class >= GFX9) {
  5039. /* GFX9 has the layer in out.z[10:0] and the viewport
  5040. * index in out.z[19:16].
  5041. */
  5042. LLVMValueRef v = viewport_index_value;
  5043. v = ac_to_integer(&ctx->ac, v);
  5044. v = LLVMBuildShl(ctx->builder, v,
  5045. LLVMConstInt(ctx->i32, 16, false),
  5046. "");
  5047. v = LLVMBuildOr(ctx->builder, v,
  5048. ac_to_integer(&ctx->ac, pos_args[1].out[2]), "");
  5049. pos_args[1].out[2] = ac_to_float(&ctx->ac, v);
  5050. pos_args[1].enabled_channels |= 1 << 2;
  5051. } else {
  5052. pos_args[1].out[3] = viewport_index_value;
  5053. pos_args[1].enabled_channels |= 1 << 3;
  5054. }
  5055. }
  5056. }
  5057. for (i = 0; i < 4; i++) {
  5058. if (pos_args[i].out[0])
  5059. num_pos_exports++;
  5060. }
  5061. pos_idx = 0;
  5062. for (i = 0; i < 4; i++) {
  5063. if (!pos_args[i].out[0])
  5064. continue;
  5065. /* Specify the target we are exporting */
  5066. pos_args[i].target = V_008DFC_SQ_EXP_POS + pos_idx++;
  5067. if (pos_idx == num_pos_exports)
  5068. pos_args[i].done = 1;
  5069. ac_build_export(&ctx->ac, &pos_args[i]);
  5070. }
  5071. for (unsigned i = 0; i < RADEON_LLVM_MAX_OUTPUTS; ++i) {
  5072. LLVMValueRef values[4];
  5073. if (!(ctx->output_mask & (1ull << i)))
  5074. continue;
  5075. for (unsigned j = 0; j < 4; j++)
  5076. values[j] = ac_to_float(&ctx->ac, LLVMBuildLoad(ctx->builder,
  5077. ctx->nir->outputs[radeon_llvm_reg_index_soa(i, j)], ""));
  5078. if (i == VARYING_SLOT_LAYER) {
  5079. target = V_008DFC_SQ_EXP_PARAM + param_count;
  5080. outinfo->vs_output_param_offset[VARYING_SLOT_LAYER] = param_count;
  5081. param_count++;
  5082. } else if (i == VARYING_SLOT_PRIMITIVE_ID) {
  5083. target = V_008DFC_SQ_EXP_PARAM + param_count;
  5084. outinfo->vs_output_param_offset[VARYING_SLOT_PRIMITIVE_ID] = param_count;
  5085. param_count++;
  5086. } else if (i >= VARYING_SLOT_VAR0) {
  5087. outinfo->export_mask |= 1u << (i - VARYING_SLOT_VAR0);
  5088. target = V_008DFC_SQ_EXP_PARAM + param_count;
  5089. outinfo->vs_output_param_offset[i] = param_count;
  5090. param_count++;
  5091. } else
  5092. continue;
  5093. si_llvm_init_export_args(ctx, values, target, &args);
  5094. if (target >= V_008DFC_SQ_EXP_POS &&
  5095. target <= (V_008DFC_SQ_EXP_POS + 3)) {
  5096. memcpy(&pos_args[target - V_008DFC_SQ_EXP_POS],
  5097. &args, sizeof(args));
  5098. } else {
  5099. ac_build_export(&ctx->ac, &args);
  5100. }
  5101. }
  5102. if (export_prim_id) {
  5103. LLVMValueRef values[4];
  5104. target = V_008DFC_SQ_EXP_PARAM + param_count;
  5105. outinfo->vs_output_param_offset[VARYING_SLOT_PRIMITIVE_ID] = param_count;
  5106. param_count++;
  5107. values[0] = ctx->vs_prim_id;
  5108. ctx->shader_info->vs.vgpr_comp_cnt = MAX2(2,
  5109. ctx->shader_info->vs.vgpr_comp_cnt);
  5110. for (unsigned j = 1; j < 4; j++)
  5111. values[j] = ctx->f32zero;
  5112. si_llvm_init_export_args(ctx, values, target, &args);
  5113. ac_build_export(&ctx->ac, &args);
  5114. outinfo->export_prim_id = true;
  5115. }
  5116. outinfo->pos_exports = num_pos_exports;
  5117. outinfo->param_exports = param_count;
  5118. }
  5119. static void
  5120. handle_es_outputs_post(struct nir_to_llvm_context *ctx,
  5121. struct ac_es_output_info *outinfo)
  5122. {
  5123. int j;
  5124. uint64_t max_output_written = 0;
  5125. for (unsigned i = 0; i < RADEON_LLVM_MAX_OUTPUTS; ++i) {
  5126. LLVMValueRef *out_ptr = &ctx->nir->outputs[i * 4];
  5127. int param_index;
  5128. int length = 4;
  5129. if (!(ctx->output_mask & (1ull << i)))
  5130. continue;
  5131. if (i == VARYING_SLOT_CLIP_DIST0)
  5132. length = ctx->num_output_clips + ctx->num_output_culls;
  5133. param_index = shader_io_get_unique_index(i);
  5134. max_output_written = MAX2(param_index + (length > 4), max_output_written);
  5135. for (j = 0; j < length; j++) {
  5136. LLVMValueRef out_val = LLVMBuildLoad(ctx->builder, out_ptr[j], "");
  5137. out_val = LLVMBuildBitCast(ctx->builder, out_val, ctx->i32, "");
  5138. ac_build_buffer_store_dword(&ctx->ac,
  5139. ctx->esgs_ring,
  5140. out_val, 1,
  5141. NULL, ctx->es2gs_offset,
  5142. (4 * param_index + j) * 4,
  5143. 1, 1, true, true);
  5144. }
  5145. }
  5146. outinfo->esgs_itemsize = (max_output_written + 1) * 16;
  5147. }
  5148. static void
  5149. handle_ls_outputs_post(struct nir_to_llvm_context *ctx)
  5150. {
  5151. LLVMValueRef vertex_id = ctx->rel_auto_id;
  5152. LLVMValueRef vertex_dw_stride = unpack_param(&ctx->ac, ctx->ls_out_layout, 13, 8);
  5153. LLVMValueRef base_dw_addr = LLVMBuildMul(ctx->builder, vertex_id,
  5154. vertex_dw_stride, "");
  5155. for (unsigned i = 0; i < RADEON_LLVM_MAX_OUTPUTS; ++i) {
  5156. LLVMValueRef *out_ptr = &ctx->nir->outputs[i * 4];
  5157. int length = 4;
  5158. if (!(ctx->output_mask & (1ull << i)))
  5159. continue;
  5160. if (i == VARYING_SLOT_CLIP_DIST0)
  5161. length = ctx->num_output_clips + ctx->num_output_culls;
  5162. int param = shader_io_get_unique_index(i);
  5163. mark_tess_output(ctx, false, param);
  5164. if (length > 4)
  5165. mark_tess_output(ctx, false, param + 1);
  5166. LLVMValueRef dw_addr = LLVMBuildAdd(ctx->builder, base_dw_addr,
  5167. LLVMConstInt(ctx->i32, param * 4, false),
  5168. "");
  5169. for (unsigned j = 0; j < length; j++) {
  5170. lds_store(ctx, dw_addr,
  5171. LLVMBuildLoad(ctx->builder, out_ptr[j], ""));
  5172. dw_addr = LLVMBuildAdd(ctx->builder, dw_addr, ctx->i32one, "");
  5173. }
  5174. }
  5175. }
  5176. struct ac_build_if_state
  5177. {
  5178. struct nir_to_llvm_context *ctx;
  5179. LLVMValueRef condition;
  5180. LLVMBasicBlockRef entry_block;
  5181. LLVMBasicBlockRef true_block;
  5182. LLVMBasicBlockRef false_block;
  5183. LLVMBasicBlockRef merge_block;
  5184. };
  5185. static LLVMBasicBlockRef
  5186. ac_build_insert_new_block(struct nir_to_llvm_context *ctx, const char *name)
  5187. {
  5188. LLVMBasicBlockRef current_block;
  5189. LLVMBasicBlockRef next_block;
  5190. LLVMBasicBlockRef new_block;
  5191. /* get current basic block */
  5192. current_block = LLVMGetInsertBlock(ctx->builder);
  5193. /* chqeck if there's another block after this one */
  5194. next_block = LLVMGetNextBasicBlock(current_block);
  5195. if (next_block) {
  5196. /* insert the new block before the next block */
  5197. new_block = LLVMInsertBasicBlockInContext(ctx->context, next_block, name);
  5198. }
  5199. else {
  5200. /* append new block after current block */
  5201. LLVMValueRef function = LLVMGetBasicBlockParent(current_block);
  5202. new_block = LLVMAppendBasicBlockInContext(ctx->context, function, name);
  5203. }
  5204. return new_block;
  5205. }
  5206. static void
  5207. ac_nir_build_if(struct ac_build_if_state *ifthen,
  5208. struct nir_to_llvm_context *ctx,
  5209. LLVMValueRef condition)
  5210. {
  5211. LLVMBasicBlockRef block = LLVMGetInsertBlock(ctx->builder);
  5212. memset(ifthen, 0, sizeof *ifthen);
  5213. ifthen->ctx = ctx;
  5214. ifthen->condition = condition;
  5215. ifthen->entry_block = block;
  5216. /* create endif/merge basic block for the phi functions */
  5217. ifthen->merge_block = ac_build_insert_new_block(ctx, "endif-block");
  5218. /* create/insert true_block before merge_block */
  5219. ifthen->true_block =
  5220. LLVMInsertBasicBlockInContext(ctx->context,
  5221. ifthen->merge_block,
  5222. "if-true-block");
  5223. /* successive code goes into the true block */
  5224. LLVMPositionBuilderAtEnd(ctx->builder, ifthen->true_block);
  5225. }
  5226. /**
  5227. * End a conditional.
  5228. */
  5229. static void
  5230. ac_nir_build_endif(struct ac_build_if_state *ifthen)
  5231. {
  5232. LLVMBuilderRef builder = ifthen->ctx->builder;
  5233. /* Insert branch to the merge block from current block */
  5234. LLVMBuildBr(builder, ifthen->merge_block);
  5235. /*
  5236. * Now patch in the various branch instructions.
  5237. */
  5238. /* Insert the conditional branch instruction at the end of entry_block */
  5239. LLVMPositionBuilderAtEnd(builder, ifthen->entry_block);
  5240. if (ifthen->false_block) {
  5241. /* we have an else clause */
  5242. LLVMBuildCondBr(builder, ifthen->condition,
  5243. ifthen->true_block, ifthen->false_block);
  5244. }
  5245. else {
  5246. /* no else clause */
  5247. LLVMBuildCondBr(builder, ifthen->condition,
  5248. ifthen->true_block, ifthen->merge_block);
  5249. }
  5250. /* Resume building code at end of the ifthen->merge_block */
  5251. LLVMPositionBuilderAtEnd(builder, ifthen->merge_block);
  5252. }
  5253. static void
  5254. write_tess_factors(struct nir_to_llvm_context *ctx)
  5255. {
  5256. unsigned stride, outer_comps, inner_comps;
  5257. struct ac_build_if_state if_ctx, inner_if_ctx;
  5258. LLVMValueRef invocation_id = unpack_param(&ctx->ac, ctx->tcs_rel_ids, 8, 5);
  5259. LLVMValueRef rel_patch_id = unpack_param(&ctx->ac, ctx->tcs_rel_ids, 0, 8);
  5260. unsigned tess_inner_index, tess_outer_index;
  5261. LLVMValueRef lds_base, lds_inner, lds_outer, byteoffset, buffer;
  5262. LLVMValueRef out[6], vec0, vec1, tf_base, inner[4], outer[4];
  5263. int i;
  5264. emit_barrier(ctx);
  5265. switch (ctx->options->key.tcs.primitive_mode) {
  5266. case GL_ISOLINES:
  5267. stride = 2;
  5268. outer_comps = 2;
  5269. inner_comps = 0;
  5270. break;
  5271. case GL_TRIANGLES:
  5272. stride = 4;
  5273. outer_comps = 3;
  5274. inner_comps = 1;
  5275. break;
  5276. case GL_QUADS:
  5277. stride = 6;
  5278. outer_comps = 4;
  5279. inner_comps = 2;
  5280. break;
  5281. default:
  5282. return;
  5283. }
  5284. ac_nir_build_if(&if_ctx, ctx,
  5285. LLVMBuildICmp(ctx->builder, LLVMIntEQ,
  5286. invocation_id, ctx->i32zero, ""));
  5287. tess_inner_index = shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER);
  5288. tess_outer_index = shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER);
  5289. mark_tess_output(ctx, true, tess_inner_index);
  5290. mark_tess_output(ctx, true, tess_outer_index);
  5291. lds_base = get_tcs_out_current_patch_data_offset(ctx);
  5292. lds_inner = LLVMBuildAdd(ctx->builder, lds_base,
  5293. LLVMConstInt(ctx->i32, tess_inner_index * 4, false), "");
  5294. lds_outer = LLVMBuildAdd(ctx->builder, lds_base,
  5295. LLVMConstInt(ctx->i32, tess_outer_index * 4, false), "");
  5296. for (i = 0; i < 4; i++) {
  5297. inner[i] = LLVMGetUndef(ctx->i32);
  5298. outer[i] = LLVMGetUndef(ctx->i32);
  5299. }
  5300. // LINES reverseal
  5301. if (ctx->options->key.tcs.primitive_mode == GL_ISOLINES) {
  5302. outer[0] = out[1] = lds_load(ctx, lds_outer);
  5303. lds_outer = LLVMBuildAdd(ctx->builder, lds_outer,
  5304. LLVMConstInt(ctx->i32, 1, false), "");
  5305. outer[1] = out[0] = lds_load(ctx, lds_outer);
  5306. } else {
  5307. for (i = 0; i < outer_comps; i++) {
  5308. outer[i] = out[i] =
  5309. lds_load(ctx, lds_outer);
  5310. lds_outer = LLVMBuildAdd(ctx->builder, lds_outer,
  5311. LLVMConstInt(ctx->i32, 1, false), "");
  5312. }
  5313. for (i = 0; i < inner_comps; i++) {
  5314. inner[i] = out[outer_comps+i] =
  5315. lds_load(ctx, lds_inner);
  5316. lds_inner = LLVMBuildAdd(ctx->builder, lds_inner,
  5317. LLVMConstInt(ctx->i32, 1, false), "");
  5318. }
  5319. }
  5320. /* Convert the outputs to vectors for stores. */
  5321. vec0 = ac_build_gather_values(&ctx->ac, out, MIN2(stride, 4));
  5322. vec1 = NULL;
  5323. if (stride > 4)
  5324. vec1 = ac_build_gather_values(&ctx->ac, out + 4, stride - 4);
  5325. buffer = ctx->hs_ring_tess_factor;
  5326. tf_base = ctx->tess_factor_offset;
  5327. byteoffset = LLVMBuildMul(ctx->builder, rel_patch_id,
  5328. LLVMConstInt(ctx->i32, 4 * stride, false), "");
  5329. unsigned tf_offset = 0;
  5330. if (ctx->options->chip_class <= VI) {
  5331. ac_nir_build_if(&inner_if_ctx, ctx,
  5332. LLVMBuildICmp(ctx->builder, LLVMIntEQ,
  5333. rel_patch_id, ctx->i32zero, ""));
  5334. /* Store the dynamic HS control word. */
  5335. ac_build_buffer_store_dword(&ctx->ac, buffer,
  5336. LLVMConstInt(ctx->i32, 0x80000000, false),
  5337. 1, ctx->i32zero, tf_base,
  5338. 0, 1, 0, true, false);
  5339. tf_offset += 4;
  5340. ac_nir_build_endif(&inner_if_ctx);
  5341. }
  5342. /* Store the tessellation factors. */
  5343. ac_build_buffer_store_dword(&ctx->ac, buffer, vec0,
  5344. MIN2(stride, 4), byteoffset, tf_base,
  5345. tf_offset, 1, 0, true, false);
  5346. if (vec1)
  5347. ac_build_buffer_store_dword(&ctx->ac, buffer, vec1,
  5348. stride - 4, byteoffset, tf_base,
  5349. 16 + tf_offset, 1, 0, true, false);
  5350. //TODO store to offchip for TES to read - only if TES reads them
  5351. if (1) {
  5352. LLVMValueRef inner_vec, outer_vec, tf_outer_offset;
  5353. LLVMValueRef tf_inner_offset;
  5354. unsigned param_outer, param_inner;
  5355. param_outer = shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER);
  5356. tf_outer_offset = get_tcs_tes_buffer_address(ctx, NULL,
  5357. LLVMConstInt(ctx->i32, param_outer, 0));
  5358. outer_vec = ac_build_gather_values(&ctx->ac, outer,
  5359. util_next_power_of_two(outer_comps));
  5360. ac_build_buffer_store_dword(&ctx->ac, ctx->hs_ring_tess_offchip, outer_vec,
  5361. outer_comps, tf_outer_offset,
  5362. ctx->oc_lds, 0, 1, 0, true, false);
  5363. if (inner_comps) {
  5364. param_inner = shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER);
  5365. tf_inner_offset = get_tcs_tes_buffer_address(ctx, NULL,
  5366. LLVMConstInt(ctx->i32, param_inner, 0));
  5367. inner_vec = inner_comps == 1 ? inner[0] :
  5368. ac_build_gather_values(&ctx->ac, inner, inner_comps);
  5369. ac_build_buffer_store_dword(&ctx->ac, ctx->hs_ring_tess_offchip, inner_vec,
  5370. inner_comps, tf_inner_offset,
  5371. ctx->oc_lds, 0, 1, 0, true, false);
  5372. }
  5373. }
  5374. ac_nir_build_endif(&if_ctx);
  5375. }
  5376. static void
  5377. handle_tcs_outputs_post(struct nir_to_llvm_context *ctx)
  5378. {
  5379. write_tess_factors(ctx);
  5380. }
  5381. static bool
  5382. si_export_mrt_color(struct nir_to_llvm_context *ctx,
  5383. LLVMValueRef *color, unsigned param, bool is_last,
  5384. struct ac_export_args *args)
  5385. {
  5386. /* Export */
  5387. si_llvm_init_export_args(ctx, color, param,
  5388. args);
  5389. if (is_last) {
  5390. args->valid_mask = 1; /* whether the EXEC mask is valid */
  5391. args->done = 1; /* DONE bit */
  5392. } else if (!args->enabled_channels)
  5393. return false; /* unnecessary NULL export */
  5394. return true;
  5395. }
  5396. static void
  5397. si_export_mrt_z(struct nir_to_llvm_context *ctx,
  5398. LLVMValueRef depth, LLVMValueRef stencil,
  5399. LLVMValueRef samplemask)
  5400. {
  5401. struct ac_export_args args;
  5402. args.enabled_channels = 0;
  5403. args.valid_mask = 1;
  5404. args.done = 1;
  5405. args.target = V_008DFC_SQ_EXP_MRTZ;
  5406. args.compr = false;
  5407. args.out[0] = LLVMGetUndef(ctx->f32); /* R, depth */
  5408. args.out[1] = LLVMGetUndef(ctx->f32); /* G, stencil test val[0:7], stencil op val[8:15] */
  5409. args.out[2] = LLVMGetUndef(ctx->f32); /* B, sample mask */
  5410. args.out[3] = LLVMGetUndef(ctx->f32); /* A, alpha to mask */
  5411. if (depth) {
  5412. args.out[0] = depth;
  5413. args.enabled_channels |= 0x1;
  5414. }
  5415. if (stencil) {
  5416. args.out[1] = stencil;
  5417. args.enabled_channels |= 0x2;
  5418. }
  5419. if (samplemask) {
  5420. args.out[2] = samplemask;
  5421. args.enabled_channels |= 0x4;
  5422. }
  5423. /* SI (except OLAND and HAINAN) has a bug that it only looks
  5424. * at the X writemask component. */
  5425. if (ctx->options->chip_class == SI &&
  5426. ctx->options->family != CHIP_OLAND &&
  5427. ctx->options->family != CHIP_HAINAN)
  5428. args.enabled_channels |= 0x1;
  5429. ac_build_export(&ctx->ac, &args);
  5430. }
  5431. static void
  5432. handle_fs_outputs_post(struct nir_to_llvm_context *ctx)
  5433. {
  5434. unsigned index = 0;
  5435. LLVMValueRef depth = NULL, stencil = NULL, samplemask = NULL;
  5436. struct ac_export_args color_args[8];
  5437. for (unsigned i = 0; i < RADEON_LLVM_MAX_OUTPUTS; ++i) {
  5438. LLVMValueRef values[4];
  5439. if (!(ctx->output_mask & (1ull << i)))
  5440. continue;
  5441. if (i == FRAG_RESULT_DEPTH) {
  5442. ctx->shader_info->fs.writes_z = true;
  5443. depth = ac_to_float(&ctx->ac, LLVMBuildLoad(ctx->builder,
  5444. ctx->nir->outputs[radeon_llvm_reg_index_soa(i, 0)], ""));
  5445. } else if (i == FRAG_RESULT_STENCIL) {
  5446. ctx->shader_info->fs.writes_stencil = true;
  5447. stencil = ac_to_float(&ctx->ac, LLVMBuildLoad(ctx->builder,
  5448. ctx->nir->outputs[radeon_llvm_reg_index_soa(i, 0)], ""));
  5449. } else if (i == FRAG_RESULT_SAMPLE_MASK) {
  5450. ctx->shader_info->fs.writes_sample_mask = true;
  5451. samplemask = ac_to_float(&ctx->ac, LLVMBuildLoad(ctx->builder,
  5452. ctx->nir->outputs[radeon_llvm_reg_index_soa(i, 0)], ""));
  5453. } else {
  5454. bool last = false;
  5455. for (unsigned j = 0; j < 4; j++)
  5456. values[j] = ac_to_float(&ctx->ac, LLVMBuildLoad(ctx->builder,
  5457. ctx->nir->outputs[radeon_llvm_reg_index_soa(i, j)], ""));
  5458. if (!ctx->shader_info->fs.writes_z && !ctx->shader_info->fs.writes_stencil && !ctx->shader_info->fs.writes_sample_mask)
  5459. last = ctx->output_mask <= ((1ull << (i + 1)) - 1);
  5460. bool ret = si_export_mrt_color(ctx, values, V_008DFC_SQ_EXP_MRT + (i - FRAG_RESULT_DATA0), last, &color_args[index]);
  5461. if (ret)
  5462. index++;
  5463. }
  5464. }
  5465. for (unsigned i = 0; i < index; i++)
  5466. ac_build_export(&ctx->ac, &color_args[i]);
  5467. if (depth || stencil || samplemask)
  5468. si_export_mrt_z(ctx, depth, stencil, samplemask);
  5469. else if (!index) {
  5470. si_export_mrt_color(ctx, NULL, V_008DFC_SQ_EXP_NULL, true, &color_args[0]);
  5471. ac_build_export(&ctx->ac, &color_args[0]);
  5472. }
  5473. ctx->shader_info->fs.output_mask = index ? ((1ull << index) - 1) : 0;
  5474. }
  5475. static void
  5476. emit_gs_epilogue(struct nir_to_llvm_context *ctx)
  5477. {
  5478. ac_build_sendmsg(&ctx->ac, AC_SENDMSG_GS_OP_NOP | AC_SENDMSG_GS_DONE, ctx->gs_wave_id);
  5479. }
  5480. static void
  5481. handle_shader_outputs_post(struct ac_shader_abi *abi, unsigned max_outputs,
  5482. LLVMValueRef *addrs)
  5483. {
  5484. struct nir_to_llvm_context *ctx = nir_to_llvm_context_from_abi(abi);
  5485. switch (ctx->stage) {
  5486. case MESA_SHADER_VERTEX:
  5487. if (ctx->options->key.vs.as_ls)
  5488. handle_ls_outputs_post(ctx);
  5489. else if (ctx->options->key.vs.as_es)
  5490. handle_es_outputs_post(ctx, &ctx->shader_info->vs.es_info);
  5491. else
  5492. handle_vs_outputs_post(ctx, ctx->options->key.vs.export_prim_id,
  5493. &ctx->shader_info->vs.outinfo);
  5494. break;
  5495. case MESA_SHADER_FRAGMENT:
  5496. handle_fs_outputs_post(ctx);
  5497. break;
  5498. case MESA_SHADER_GEOMETRY:
  5499. emit_gs_epilogue(ctx);
  5500. break;
  5501. case MESA_SHADER_TESS_CTRL:
  5502. handle_tcs_outputs_post(ctx);
  5503. break;
  5504. case MESA_SHADER_TESS_EVAL:
  5505. if (ctx->options->key.tes.as_es)
  5506. handle_es_outputs_post(ctx, &ctx->shader_info->tes.es_info);
  5507. else
  5508. handle_vs_outputs_post(ctx, ctx->options->key.tes.export_prim_id,
  5509. &ctx->shader_info->tes.outinfo);
  5510. break;
  5511. default:
  5512. break;
  5513. }
  5514. }
  5515. static void ac_llvm_finalize_module(struct nir_to_llvm_context * ctx)
  5516. {
  5517. LLVMPassManagerRef passmgr;
  5518. /* Create the pass manager */
  5519. passmgr = LLVMCreateFunctionPassManagerForModule(
  5520. ctx->module);
  5521. /* This pass should eliminate all the load and store instructions */
  5522. LLVMAddPromoteMemoryToRegisterPass(passmgr);
  5523. /* Add some optimization passes */
  5524. LLVMAddScalarReplAggregatesPass(passmgr);
  5525. LLVMAddLICMPass(passmgr);
  5526. LLVMAddAggressiveDCEPass(passmgr);
  5527. LLVMAddCFGSimplificationPass(passmgr);
  5528. LLVMAddInstructionCombiningPass(passmgr);
  5529. /* Run the pass */
  5530. LLVMInitializeFunctionPassManager(passmgr);
  5531. LLVMRunFunctionPassManager(passmgr, ctx->main_function);
  5532. LLVMFinalizeFunctionPassManager(passmgr);
  5533. LLVMDisposeBuilder(ctx->builder);
  5534. LLVMDisposePassManager(passmgr);
  5535. }
  5536. static void
  5537. ac_nir_eliminate_const_vs_outputs(struct nir_to_llvm_context *ctx)
  5538. {
  5539. struct ac_vs_output_info *outinfo;
  5540. switch (ctx->stage) {
  5541. case MESA_SHADER_FRAGMENT:
  5542. case MESA_SHADER_COMPUTE:
  5543. case MESA_SHADER_TESS_CTRL:
  5544. case MESA_SHADER_GEOMETRY:
  5545. return;
  5546. case MESA_SHADER_VERTEX:
  5547. if (ctx->options->key.vs.as_ls ||
  5548. ctx->options->key.vs.as_es)
  5549. return;
  5550. outinfo = &ctx->shader_info->vs.outinfo;
  5551. break;
  5552. case MESA_SHADER_TESS_EVAL:
  5553. if (ctx->options->key.vs.as_es)
  5554. return;
  5555. outinfo = &ctx->shader_info->tes.outinfo;
  5556. break;
  5557. default:
  5558. unreachable("Unhandled shader type");
  5559. }
  5560. ac_optimize_vs_outputs(&ctx->ac,
  5561. ctx->main_function,
  5562. outinfo->vs_output_param_offset,
  5563. VARYING_SLOT_MAX,
  5564. &outinfo->param_exports);
  5565. }
  5566. static void
  5567. ac_setup_rings(struct nir_to_llvm_context *ctx)
  5568. {
  5569. if ((ctx->stage == MESA_SHADER_VERTEX && ctx->options->key.vs.as_es) ||
  5570. (ctx->stage == MESA_SHADER_TESS_EVAL && ctx->options->key.tes.as_es)) {
  5571. ctx->esgs_ring = ac_build_load_to_sgpr(&ctx->ac, ctx->ring_offsets, LLVMConstInt(ctx->i32, RING_ESGS_VS, false));
  5572. }
  5573. if (ctx->is_gs_copy_shader) {
  5574. ctx->gsvs_ring = ac_build_load_to_sgpr(&ctx->ac, ctx->ring_offsets, LLVMConstInt(ctx->i32, RING_GSVS_VS, false));
  5575. }
  5576. if (ctx->stage == MESA_SHADER_GEOMETRY) {
  5577. LLVMValueRef tmp;
  5578. ctx->esgs_ring = ac_build_load_to_sgpr(&ctx->ac, ctx->ring_offsets, LLVMConstInt(ctx->i32, RING_ESGS_GS, false));
  5579. ctx->gsvs_ring = ac_build_load_to_sgpr(&ctx->ac, ctx->ring_offsets, LLVMConstInt(ctx->i32, RING_GSVS_GS, false));
  5580. ctx->gsvs_ring = LLVMBuildBitCast(ctx->builder, ctx->gsvs_ring, ctx->v4i32, "");
  5581. ctx->gsvs_ring = LLVMBuildInsertElement(ctx->builder, ctx->gsvs_ring, ctx->gsvs_num_entries, LLVMConstInt(ctx->i32, 2, false), "");
  5582. tmp = LLVMBuildExtractElement(ctx->builder, ctx->gsvs_ring, ctx->i32one, "");
  5583. tmp = LLVMBuildOr(ctx->builder, tmp, ctx->gsvs_ring_stride, "");
  5584. ctx->gsvs_ring = LLVMBuildInsertElement(ctx->builder, ctx->gsvs_ring, tmp, ctx->i32one, "");
  5585. }
  5586. if (ctx->stage == MESA_SHADER_TESS_CTRL ||
  5587. ctx->stage == MESA_SHADER_TESS_EVAL) {
  5588. ctx->hs_ring_tess_offchip = ac_build_load_to_sgpr(&ctx->ac, ctx->ring_offsets, LLVMConstInt(ctx->i32, RING_HS_TESS_OFFCHIP, false));
  5589. ctx->hs_ring_tess_factor = ac_build_load_to_sgpr(&ctx->ac, ctx->ring_offsets, LLVMConstInt(ctx->i32, RING_HS_TESS_FACTOR, false));
  5590. }
  5591. }
  5592. static unsigned
  5593. ac_nir_get_max_workgroup_size(enum chip_class chip_class,
  5594. const struct nir_shader *nir)
  5595. {
  5596. switch (nir->stage) {
  5597. case MESA_SHADER_TESS_CTRL:
  5598. return chip_class >= CIK ? 128 : 64;
  5599. case MESA_SHADER_GEOMETRY:
  5600. return 64;
  5601. case MESA_SHADER_COMPUTE:
  5602. break;
  5603. default:
  5604. return 0;
  5605. }
  5606. unsigned max_workgroup_size = nir->info.cs.local_size[0] *
  5607. nir->info.cs.local_size[1] *
  5608. nir->info.cs.local_size[2];
  5609. return max_workgroup_size;
  5610. }
  5611. /* Fixup the HW not emitting the TCS regs if there are no HS threads. */
  5612. static void ac_nir_fixup_ls_hs_input_vgprs(struct nir_to_llvm_context *ctx)
  5613. {
  5614. LLVMValueRef count = ac_build_bfe(&ctx->ac, ctx->merged_wave_info,
  5615. LLVMConstInt(ctx->ac.i32, 8, false),
  5616. LLVMConstInt(ctx->ac.i32, 8, false), false);
  5617. LLVMValueRef hs_empty = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ, count,
  5618. LLVMConstInt(ctx->ac.i32, 0, false), "");
  5619. ctx->abi.instance_id = LLVMBuildSelect(ctx->ac.builder, hs_empty, ctx->rel_auto_id, ctx->abi.instance_id, "");
  5620. ctx->vs_prim_id = LLVMBuildSelect(ctx->ac.builder, hs_empty, ctx->abi.vertex_id, ctx->vs_prim_id, "");
  5621. ctx->rel_auto_id = LLVMBuildSelect(ctx->ac.builder, hs_empty, ctx->tcs_rel_ids, ctx->rel_auto_id, "");
  5622. ctx->abi.vertex_id = LLVMBuildSelect(ctx->ac.builder, hs_empty, ctx->tcs_patch_id, ctx->abi.vertex_id, "");
  5623. }
  5624. void ac_nir_translate(struct ac_llvm_context *ac, struct ac_shader_abi *abi,
  5625. struct nir_shader *nir, struct nir_to_llvm_context *nctx)
  5626. {
  5627. struct ac_nir_context ctx = {};
  5628. struct nir_function *func;
  5629. ctx.ac = *ac;
  5630. ctx.abi = abi;
  5631. ctx.nctx = nctx;
  5632. if (nctx)
  5633. nctx->nir = &ctx;
  5634. ctx.stage = nir->stage;
  5635. ctx.main_function = LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx.ac.builder));
  5636. nir_foreach_variable(variable, &nir->outputs)
  5637. handle_shader_output_decl(&ctx, nir, variable);
  5638. ctx.defs = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
  5639. _mesa_key_pointer_equal);
  5640. ctx.phis = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
  5641. _mesa_key_pointer_equal);
  5642. ctx.vars = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
  5643. _mesa_key_pointer_equal);
  5644. func = (struct nir_function *)exec_list_get_head(&nir->functions);
  5645. setup_locals(&ctx, func);
  5646. if (nir->stage == MESA_SHADER_COMPUTE)
  5647. setup_shared(&ctx, nir);
  5648. visit_cf_list(&ctx, &func->impl->body);
  5649. phi_post_pass(&ctx);
  5650. ctx.abi->emit_outputs(ctx.abi, RADEON_LLVM_MAX_OUTPUTS,
  5651. ctx.outputs);
  5652. free(ctx.locals);
  5653. ralloc_free(ctx.defs);
  5654. ralloc_free(ctx.phis);
  5655. ralloc_free(ctx.vars);
  5656. if (nctx)
  5657. nctx->nir = NULL;
  5658. }
  5659. static
  5660. LLVMModuleRef ac_translate_nir_to_llvm(LLVMTargetMachineRef tm,
  5661. struct nir_shader *const *shaders,
  5662. int shader_count,
  5663. struct ac_shader_variant_info *shader_info,
  5664. const struct ac_nir_compiler_options *options)
  5665. {
  5666. struct nir_to_llvm_context ctx = {0};
  5667. unsigned i;
  5668. ctx.options = options;
  5669. ctx.shader_info = shader_info;
  5670. ctx.context = LLVMContextCreate();
  5671. ctx.module = LLVMModuleCreateWithNameInContext("shader", ctx.context);
  5672. ac_llvm_context_init(&ctx.ac, ctx.context, options->chip_class);
  5673. ctx.ac.module = ctx.module;
  5674. LLVMSetTarget(ctx.module, options->supports_spill ? "amdgcn-mesa-mesa3d" : "amdgcn--");
  5675. LLVMTargetDataRef data_layout = LLVMCreateTargetDataLayout(tm);
  5676. char *data_layout_str = LLVMCopyStringRepOfTargetData(data_layout);
  5677. LLVMSetDataLayout(ctx.module, data_layout_str);
  5678. LLVMDisposeTargetData(data_layout);
  5679. LLVMDisposeMessage(data_layout_str);
  5680. setup_types(&ctx);
  5681. ctx.builder = LLVMCreateBuilderInContext(ctx.context);
  5682. ctx.ac.builder = ctx.builder;
  5683. memset(shader_info, 0, sizeof(*shader_info));
  5684. for(int i = 0; i < shader_count; ++i)
  5685. ac_nir_shader_info_pass(shaders[i], options, &shader_info->info);
  5686. for (i = 0; i < AC_UD_MAX_SETS; i++)
  5687. shader_info->user_sgprs_locs.descriptor_sets[i].sgpr_idx = -1;
  5688. for (i = 0; i < AC_UD_MAX_UD; i++)
  5689. shader_info->user_sgprs_locs.shader_data[i].sgpr_idx = -1;
  5690. ctx.max_workgroup_size = ac_nir_get_max_workgroup_size(ctx.options->chip_class, shaders[0]);
  5691. create_function(&ctx, shaders[shader_count - 1]->stage, shader_count >= 2,
  5692. shader_count >= 2 ? shaders[shader_count - 2]->stage : MESA_SHADER_VERTEX);
  5693. ctx.abi.inputs = &ctx.inputs[0];
  5694. ctx.abi.emit_outputs = handle_shader_outputs_post;
  5695. ctx.abi.load_ssbo = radv_load_ssbo;
  5696. ctx.abi.load_sampler_desc = radv_get_sampler_desc;
  5697. if (shader_count >= 2)
  5698. ac_init_exec_full_mask(&ctx.ac);
  5699. if (ctx.ac.chip_class == GFX9 &&
  5700. shaders[shader_count - 1]->stage == MESA_SHADER_TESS_CTRL)
  5701. ac_nir_fixup_ls_hs_input_vgprs(&ctx);
  5702. for(int i = 0; i < shader_count; ++i) {
  5703. ctx.stage = shaders[i]->stage;
  5704. ctx.output_mask = 0;
  5705. ctx.tess_outputs_written = 0;
  5706. ctx.num_output_clips = shaders[i]->info.clip_distance_array_size;
  5707. ctx.num_output_culls = shaders[i]->info.cull_distance_array_size;
  5708. if (shaders[i]->stage == MESA_SHADER_GEOMETRY) {
  5709. ctx.gs_next_vertex = ac_build_alloca(&ctx.ac, ctx.i32, "gs_next_vertex");
  5710. ctx.gs_max_out_vertices = shaders[i]->info.gs.vertices_out;
  5711. } else if (shaders[i]->stage == MESA_SHADER_TESS_EVAL) {
  5712. ctx.tes_primitive_mode = shaders[i]->info.tess.primitive_mode;
  5713. } else if (shaders[i]->stage == MESA_SHADER_VERTEX) {
  5714. if (shader_info->info.vs.needs_instance_id) {
  5715. ctx.shader_info->vs.vgpr_comp_cnt =
  5716. MAX2(3, ctx.shader_info->vs.vgpr_comp_cnt);
  5717. }
  5718. } else if (shaders[i]->stage == MESA_SHADER_FRAGMENT) {
  5719. shader_info->fs.can_discard = shaders[i]->info.fs.uses_discard;
  5720. }
  5721. if (i)
  5722. emit_barrier(&ctx);
  5723. ac_setup_rings(&ctx);
  5724. LLVMBasicBlockRef merge_block;
  5725. if (shader_count >= 2) {
  5726. LLVMValueRef fn = LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx.ac.builder));
  5727. LLVMBasicBlockRef then_block = LLVMAppendBasicBlockInContext(ctx.ac.context, fn, "");
  5728. merge_block = LLVMAppendBasicBlockInContext(ctx.ac.context, fn, "");
  5729. LLVMValueRef count = ac_build_bfe(&ctx.ac, ctx.merged_wave_info,
  5730. LLVMConstInt(ctx.ac.i32, 8 * i, false),
  5731. LLVMConstInt(ctx.ac.i32, 8, false), false);
  5732. LLVMValueRef thread_id = ac_get_thread_id(&ctx.ac);
  5733. LLVMValueRef cond = LLVMBuildICmp(ctx.ac.builder, LLVMIntULT,
  5734. thread_id, count, "");
  5735. LLVMBuildCondBr(ctx.ac.builder, cond, then_block, merge_block);
  5736. LLVMPositionBuilderAtEnd(ctx.ac.builder, then_block);
  5737. }
  5738. if (shaders[i]->stage == MESA_SHADER_FRAGMENT)
  5739. handle_fs_inputs(&ctx, shaders[i]);
  5740. else if(shaders[i]->stage == MESA_SHADER_VERTEX)
  5741. handle_vs_inputs(&ctx, shaders[i]);
  5742. nir_foreach_variable(variable, &shaders[i]->outputs)
  5743. scan_shader_output_decl(&ctx, variable, shaders[i], shaders[i]->stage);
  5744. ac_nir_translate(&ctx.ac, &ctx.abi, shaders[i], &ctx);
  5745. if (shader_count >= 2) {
  5746. LLVMBuildBr(ctx.ac.builder, merge_block);
  5747. LLVMPositionBuilderAtEnd(ctx.ac.builder, merge_block);
  5748. }
  5749. if (shaders[i]->stage == MESA_SHADER_GEOMETRY) {
  5750. unsigned addclip = shaders[i]->info.clip_distance_array_size +
  5751. shaders[i]->info.cull_distance_array_size > 4;
  5752. shader_info->gs.gsvs_vertex_size = (util_bitcount64(ctx.output_mask) + addclip) * 16;
  5753. shader_info->gs.max_gsvs_emit_size = shader_info->gs.gsvs_vertex_size *
  5754. shaders[i]->info.gs.vertices_out;
  5755. } else if (shaders[i]->stage == MESA_SHADER_TESS_CTRL) {
  5756. shader_info->tcs.outputs_written = ctx.tess_outputs_written;
  5757. shader_info->tcs.patch_outputs_written = ctx.tess_patch_outputs_written;
  5758. } else if (shaders[i]->stage == MESA_SHADER_VERTEX && ctx.options->key.vs.as_ls) {
  5759. shader_info->vs.outputs_written = ctx.tess_outputs_written;
  5760. }
  5761. }
  5762. LLVMBuildRetVoid(ctx.builder);
  5763. ac_llvm_finalize_module(&ctx);
  5764. if (shader_count == 1)
  5765. ac_nir_eliminate_const_vs_outputs(&ctx);
  5766. return ctx.module;
  5767. }
  5768. static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di, void *context)
  5769. {
  5770. unsigned *retval = (unsigned *)context;
  5771. LLVMDiagnosticSeverity severity = LLVMGetDiagInfoSeverity(di);
  5772. char *description = LLVMGetDiagInfoDescription(di);
  5773. if (severity == LLVMDSError) {
  5774. *retval = 1;
  5775. fprintf(stderr, "LLVM triggered Diagnostic Handler: %s\n",
  5776. description);
  5777. }
  5778. LLVMDisposeMessage(description);
  5779. }
  5780. static unsigned ac_llvm_compile(LLVMModuleRef M,
  5781. struct ac_shader_binary *binary,
  5782. LLVMTargetMachineRef tm)
  5783. {
  5784. unsigned retval = 0;
  5785. char *err;
  5786. LLVMContextRef llvm_ctx;
  5787. LLVMMemoryBufferRef out_buffer;
  5788. unsigned buffer_size;
  5789. const char *buffer_data;
  5790. LLVMBool mem_err;
  5791. /* Setup Diagnostic Handler*/
  5792. llvm_ctx = LLVMGetModuleContext(M);
  5793. LLVMContextSetDiagnosticHandler(llvm_ctx, ac_diagnostic_handler,
  5794. &retval);
  5795. /* Compile IR*/
  5796. mem_err = LLVMTargetMachineEmitToMemoryBuffer(tm, M, LLVMObjectFile,
  5797. &err, &out_buffer);
  5798. /* Process Errors/Warnings */
  5799. if (mem_err) {
  5800. fprintf(stderr, "%s: %s", __FUNCTION__, err);
  5801. free(err);
  5802. retval = 1;
  5803. goto out;
  5804. }
  5805. /* Extract Shader Code*/
  5806. buffer_size = LLVMGetBufferSize(out_buffer);
  5807. buffer_data = LLVMGetBufferStart(out_buffer);
  5808. ac_elf_read(buffer_data, buffer_size, binary);
  5809. /* Clean up */
  5810. LLVMDisposeMemoryBuffer(out_buffer);
  5811. out:
  5812. return retval;
  5813. }
  5814. static void ac_compile_llvm_module(LLVMTargetMachineRef tm,
  5815. LLVMModuleRef llvm_module,
  5816. struct ac_shader_binary *binary,
  5817. struct ac_shader_config *config,
  5818. struct ac_shader_variant_info *shader_info,
  5819. gl_shader_stage stage,
  5820. bool dump_shader, bool supports_spill)
  5821. {
  5822. if (dump_shader)
  5823. ac_dump_module(llvm_module);
  5824. memset(binary, 0, sizeof(*binary));
  5825. int v = ac_llvm_compile(llvm_module, binary, tm);
  5826. if (v) {
  5827. fprintf(stderr, "compile failed\n");
  5828. }
  5829. if (dump_shader)
  5830. fprintf(stderr, "disasm:\n%s\n", binary->disasm_string);
  5831. ac_shader_binary_read_config(binary, config, 0, supports_spill);
  5832. LLVMContextRef ctx = LLVMGetModuleContext(llvm_module);
  5833. LLVMDisposeModule(llvm_module);
  5834. LLVMContextDispose(ctx);
  5835. if (stage == MESA_SHADER_FRAGMENT) {
  5836. shader_info->num_input_vgprs = 0;
  5837. if (G_0286CC_PERSP_SAMPLE_ENA(config->spi_ps_input_addr))
  5838. shader_info->num_input_vgprs += 2;
  5839. if (G_0286CC_PERSP_CENTER_ENA(config->spi_ps_input_addr))
  5840. shader_info->num_input_vgprs += 2;
  5841. if (G_0286CC_PERSP_CENTROID_ENA(config->spi_ps_input_addr))
  5842. shader_info->num_input_vgprs += 2;
  5843. if (G_0286CC_PERSP_PULL_MODEL_ENA(config->spi_ps_input_addr))
  5844. shader_info->num_input_vgprs += 3;
  5845. if (G_0286CC_LINEAR_SAMPLE_ENA(config->spi_ps_input_addr))
  5846. shader_info->num_input_vgprs += 2;
  5847. if (G_0286CC_LINEAR_CENTER_ENA(config->spi_ps_input_addr))
  5848. shader_info->num_input_vgprs += 2;
  5849. if (G_0286CC_LINEAR_CENTROID_ENA(config->spi_ps_input_addr))
  5850. shader_info->num_input_vgprs += 2;
  5851. if (G_0286CC_LINE_STIPPLE_TEX_ENA(config->spi_ps_input_addr))
  5852. shader_info->num_input_vgprs += 1;
  5853. if (G_0286CC_POS_X_FLOAT_ENA(config->spi_ps_input_addr))
  5854. shader_info->num_input_vgprs += 1;
  5855. if (G_0286CC_POS_Y_FLOAT_ENA(config->spi_ps_input_addr))
  5856. shader_info->num_input_vgprs += 1;
  5857. if (G_0286CC_POS_Z_FLOAT_ENA(config->spi_ps_input_addr))
  5858. shader_info->num_input_vgprs += 1;
  5859. if (G_0286CC_POS_W_FLOAT_ENA(config->spi_ps_input_addr))
  5860. shader_info->num_input_vgprs += 1;
  5861. if (G_0286CC_FRONT_FACE_ENA(config->spi_ps_input_addr))
  5862. shader_info->num_input_vgprs += 1;
  5863. if (G_0286CC_ANCILLARY_ENA(config->spi_ps_input_addr))
  5864. shader_info->num_input_vgprs += 1;
  5865. if (G_0286CC_SAMPLE_COVERAGE_ENA(config->spi_ps_input_addr))
  5866. shader_info->num_input_vgprs += 1;
  5867. if (G_0286CC_POS_FIXED_PT_ENA(config->spi_ps_input_addr))
  5868. shader_info->num_input_vgprs += 1;
  5869. }
  5870. config->num_vgprs = MAX2(config->num_vgprs, shader_info->num_input_vgprs);
  5871. /* +3 for scratch wave offset and VCC */
  5872. config->num_sgprs = MAX2(config->num_sgprs,
  5873. shader_info->num_input_sgprs + 3);
  5874. }
  5875. static void
  5876. ac_fill_shader_info(struct ac_shader_variant_info *shader_info, struct nir_shader *nir, const struct ac_nir_compiler_options *options)
  5877. {
  5878. switch (nir->stage) {
  5879. case MESA_SHADER_COMPUTE:
  5880. for (int i = 0; i < 3; ++i)
  5881. shader_info->cs.block_size[i] = nir->info.cs.local_size[i];
  5882. break;
  5883. case MESA_SHADER_FRAGMENT:
  5884. shader_info->fs.early_fragment_test = nir->info.fs.early_fragment_tests;
  5885. break;
  5886. case MESA_SHADER_GEOMETRY:
  5887. shader_info->gs.vertices_in = nir->info.gs.vertices_in;
  5888. shader_info->gs.vertices_out = nir->info.gs.vertices_out;
  5889. shader_info->gs.output_prim = nir->info.gs.output_primitive;
  5890. shader_info->gs.invocations = nir->info.gs.invocations;
  5891. break;
  5892. case MESA_SHADER_TESS_EVAL:
  5893. shader_info->tes.primitive_mode = nir->info.tess.primitive_mode;
  5894. shader_info->tes.spacing = nir->info.tess.spacing;
  5895. shader_info->tes.ccw = nir->info.tess.ccw;
  5896. shader_info->tes.point_mode = nir->info.tess.point_mode;
  5897. shader_info->tes.as_es = options->key.tes.as_es;
  5898. break;
  5899. case MESA_SHADER_TESS_CTRL:
  5900. shader_info->tcs.tcs_vertices_out = nir->info.tess.tcs_vertices_out;
  5901. break;
  5902. case MESA_SHADER_VERTEX:
  5903. shader_info->vs.as_es = options->key.vs.as_es;
  5904. shader_info->vs.as_ls = options->key.vs.as_ls;
  5905. /* in LS mode we need at least 1, invocation id needs 3, handled elsewhere */
  5906. if (options->key.vs.as_ls)
  5907. shader_info->vs.vgpr_comp_cnt = MAX2(1, shader_info->vs.vgpr_comp_cnt);
  5908. break;
  5909. default:
  5910. break;
  5911. }
  5912. }
  5913. void ac_compile_nir_shader(LLVMTargetMachineRef tm,
  5914. struct ac_shader_binary *binary,
  5915. struct ac_shader_config *config,
  5916. struct ac_shader_variant_info *shader_info,
  5917. struct nir_shader *const *nir,
  5918. int nir_count,
  5919. const struct ac_nir_compiler_options *options,
  5920. bool dump_shader)
  5921. {
  5922. LLVMModuleRef llvm_module = ac_translate_nir_to_llvm(tm, nir, nir_count, shader_info,
  5923. options);
  5924. ac_compile_llvm_module(tm, llvm_module, binary, config, shader_info, nir[0]->stage, dump_shader, options->supports_spill);
  5925. for (int i = 0; i < nir_count; ++i)
  5926. ac_fill_shader_info(shader_info, nir[i], options);
  5927. }
  5928. static void
  5929. ac_gs_copy_shader_emit(struct nir_to_llvm_context *ctx)
  5930. {
  5931. LLVMValueRef args[9];
  5932. args[0] = ctx->gsvs_ring;
  5933. args[1] = LLVMBuildMul(ctx->builder, ctx->abi.vertex_id, LLVMConstInt(ctx->i32, 4, false), "");
  5934. args[3] = ctx->i32zero;
  5935. args[4] = ctx->i32one; /* OFFEN */
  5936. args[5] = ctx->i32zero; /* IDXEN */
  5937. args[6] = ctx->i32one; /* GLC */
  5938. args[7] = ctx->i32one; /* SLC */
  5939. args[8] = ctx->i32zero; /* TFE */
  5940. int idx = 0;
  5941. for (unsigned i = 0; i < RADEON_LLVM_MAX_OUTPUTS; ++i) {
  5942. int length = 4;
  5943. int slot = idx;
  5944. int slot_inc = 1;
  5945. if (!(ctx->output_mask & (1ull << i)))
  5946. continue;
  5947. if (i == VARYING_SLOT_CLIP_DIST0) {
  5948. /* unpack clip and cull from a single set of slots */
  5949. length = ctx->num_output_clips + ctx->num_output_culls;
  5950. if (length > 4)
  5951. slot_inc = 2;
  5952. }
  5953. for (unsigned j = 0; j < length; j++) {
  5954. LLVMValueRef value;
  5955. args[2] = LLVMConstInt(ctx->i32,
  5956. (slot * 4 + j) *
  5957. ctx->gs_max_out_vertices * 16 * 4, false);
  5958. value = ac_build_intrinsic(&ctx->ac,
  5959. "llvm.SI.buffer.load.dword.i32.i32",
  5960. ctx->i32, args, 9,
  5961. AC_FUNC_ATTR_READONLY |
  5962. AC_FUNC_ATTR_LEGACY);
  5963. LLVMBuildStore(ctx->builder,
  5964. ac_to_float(&ctx->ac, value), ctx->nir->outputs[radeon_llvm_reg_index_soa(i, j)]);
  5965. }
  5966. idx += slot_inc;
  5967. }
  5968. handle_vs_outputs_post(ctx, false, &ctx->shader_info->vs.outinfo);
  5969. }
  5970. void ac_create_gs_copy_shader(LLVMTargetMachineRef tm,
  5971. struct nir_shader *geom_shader,
  5972. struct ac_shader_binary *binary,
  5973. struct ac_shader_config *config,
  5974. struct ac_shader_variant_info *shader_info,
  5975. const struct ac_nir_compiler_options *options,
  5976. bool dump_shader)
  5977. {
  5978. struct nir_to_llvm_context ctx = {0};
  5979. ctx.context = LLVMContextCreate();
  5980. ctx.module = LLVMModuleCreateWithNameInContext("shader", ctx.context);
  5981. ctx.options = options;
  5982. ctx.shader_info = shader_info;
  5983. ac_llvm_context_init(&ctx.ac, ctx.context, options->chip_class);
  5984. ctx.ac.module = ctx.module;
  5985. ctx.is_gs_copy_shader = true;
  5986. LLVMSetTarget(ctx.module, "amdgcn--");
  5987. setup_types(&ctx);
  5988. ctx.builder = LLVMCreateBuilderInContext(ctx.context);
  5989. ctx.ac.builder = ctx.builder;
  5990. ctx.stage = MESA_SHADER_VERTEX;
  5991. create_function(&ctx, MESA_SHADER_VERTEX, false, MESA_SHADER_VERTEX);
  5992. ctx.gs_max_out_vertices = geom_shader->info.gs.vertices_out;
  5993. ac_setup_rings(&ctx);
  5994. ctx.num_output_clips = geom_shader->info.clip_distance_array_size;
  5995. ctx.num_output_culls = geom_shader->info.cull_distance_array_size;
  5996. struct ac_nir_context nir_ctx = {};
  5997. nir_ctx.ac = ctx.ac;
  5998. nir_ctx.abi = &ctx.abi;
  5999. nir_ctx.nctx = &ctx;
  6000. ctx.nir = &nir_ctx;
  6001. nir_foreach_variable(variable, &geom_shader->outputs) {
  6002. scan_shader_output_decl(&ctx, variable, geom_shader, MESA_SHADER_VERTEX);
  6003. handle_shader_output_decl(&nir_ctx, geom_shader, variable);
  6004. }
  6005. ac_gs_copy_shader_emit(&ctx);
  6006. ctx.nir = NULL;
  6007. LLVMBuildRetVoid(ctx.builder);
  6008. ac_llvm_finalize_module(&ctx);
  6009. ac_compile_llvm_module(tm, ctx.module, binary, config, shader_info,
  6010. MESA_SHADER_VERTEX,
  6011. dump_shader, options->supports_spill);
  6012. }