| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088 |
- //! IR to DXIL, one machine instruction at a time (decision 0003).
- //!
- //! Every register dword is an `i32` (SCC an `i1`); float operations bitcast
- //! their operands. Lane masks exist only as the per-lane booleans of
- //! structurisation; branches and guards are built from them. A value the
- //! emitter cannot express is `Missing` with a reason, and only fails the
- //! program when something with an effect (an export, a branch) needs it.
- use std::collections::{BTreeMap, HashMap, HashSet};
- use super::UserData;
- use super::bitcode::{Attr, Bin, Cast, Term, V, pred};
- use super::container::SigElement;
- use super::shader::{Shader, flags};
- use crate::ShaderError;
- use crate::gfx10::{Dpp, Fields, NULL, Operand};
- use crate::io::{Barycentric, Export, Input, Interp, Io, Target};
- use crate::ir::{EXEC, Exit, Inst, Program, Reg, Value};
- use crate::native::{HwStage, NativeProgram, ResourceType, RootType, StageIo, Usage, UsedResource};
- use crate::resource::{Address, CODE, Resources, Role, Site, Source};
- use crate::structure::{Atom, Bool, LaneExit, Structured};
- type Why = &'static str;
- #[derive(Debug, Clone, Copy)]
- enum Val {
- I32(V),
- Bit(V),
- Missing(Why),
- }
- pub(crate) struct Lowered {
- pub dxil: Vec<u8>,
- /// Bit n: immediate sampler n is used for comparisons.
- pub cmp_samplers: u64,
- pub root_cbvs: Vec<u8>,
- pub root_srvs: Vec<u8>,
- pub user_data: Vec<UserData>,
- pub samplers: Vec<[u32; 4]>,
- }
- pub(crate) struct Parts<'a> {
- pub native: &'a NativeProgram,
- pub p: &'a Program,
- pub st: &'a Structured,
- pub io: &'a Io,
- pub res: &'a Resources,
- pub inputs: Vec<SigElement>,
- pub outputs: Vec<SigElement>,
- pub prims: Vec<SigElement>,
- /// Mesh: what the group id arrives multiplied by.
- pub mesh_stride: u32,
- }
- enum Stop {
- /// A phi whose incoming value has no DXIL form was read: redo with it missing.
- Retry(Value, Why),
- Fail(ShaderError),
- }
- pub(crate) fn lower(parts: &Parts) -> Result<Lowered, ShaderError> {
- let mut bad = HashMap::new();
- loop {
- match Lower::new(parts, &bad).run() {
- Ok(l) => return Ok(l),
- Err(Stop::Retry(v, why)) => {
- if bad.insert(v, why).is_some() {
- return Err(ShaderError::Unsupported {
- offset: 0,
- what: "a phi that does not settle",
- });
- }
- }
- Err(Stop::Fail(e)) => return Err(e),
- }
- }
- }
- const TWO_PI: f32 = std::f32::consts::TAU;
- /// `s_sendmsg` id of a mesh or NGG stage's output allocation request.
- const GS_ALLOC_REQ: u16 = 9;
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
- enum Numeric {
- Unorm,
- Snorm,
- Uscaled,
- Sscaled,
- Uint,
- Sint,
- Float,
- }
- /// A GFX10 buffer format (the unified format of typed buffer loads): the
- /// channel widths from the lowest bits up, and how the channels convert.
- /// Formats are numbered in families (`8`, `16`, `8_8`, ...: AMD names
- /// list channels from the highest bits), each with its numeric formats in
- /// the order unorm, snorm, uscaled, sscaled, uint, sint, float.
- fn buffer_format(format: u8) -> Option<(&'static [u32], Numeric)> {
- use Numeric::*;
- const NORM: &[Numeric] = &[Unorm, Snorm, Uscaled, Sscaled, Uint, Sint];
- const ALL: &[Numeric] = &[Unorm, Snorm, Uscaled, Sscaled, Uint, Sint, Float];
- const WIDE: &[Numeric] = &[Uint, Sint, Float];
- // Numbers and order from LLVM's `UfmtGFX10` (`SIDefines.h`); the GFX10
- // buffer formats have no 5_9_9_9. Channel order from Mesa
- // (`ac_formats.c`): R11G11B10_FLOAT is 10_11_11, and 10_10_10_2 has
- // its 2-bit channel in the lowest bits.
- const FAMILIES: [(&[u32], &[Numeric]); 14] = [
- (&[8], NORM),
- (&[16], ALL),
- (&[8, 8], NORM),
- (&[32], WIDE),
- (&[16, 16], ALL),
- (&[11, 11, 10], ALL), // 10_11_11
- (&[10, 11, 11], ALL), // 11_11_10
- (&[2, 10, 10, 10], NORM), // 10_10_10_2
- (&[10, 10, 10, 2], NORM), // 2_10_10_10
- (&[8, 8, 8, 8], NORM),
- (&[32, 32], WIDE),
- (&[16, 16, 16, 16], ALL),
- (&[32, 32, 32], WIDE),
- (&[32, 32, 32, 32], WIDE),
- ];
- let mut n = usize::from(format.checked_sub(1)?);
- for (bits, numerics) in FAMILIES {
- if n < numerics.len() {
- return Some((bits, numerics[n]));
- }
- n -= numerics.len();
- }
- None
- }
- /// How far a float channel of `width` bits (16, or the unsigned 11- and
- /// 10-bit floats of the packed formats) shifts up to be a half:
- /// the small floats are a half's exponent (5 bits, biased 15) and the top
- /// of its mantissa, without the sign, so `legacyF16ToF32` widens them
- /// exactly, denormals, infinities and NaNs included.
- fn half_shift(width: u32) -> u32 {
- debug_assert!(matches!(width, 10 | 11 | 16), "{width}-bit float");
- if width == 16 { 0 } else { 15 - width }
- }
- struct Lower<'a> {
- s: Shader,
- x: &'a Parts<'a>,
- bad: &'a HashMap<Value, Why>,
- lane_fail: HashMap<Value, Why>,
- vals: HashMap<Value, Val>,
- floats: HashMap<Value, V>,
- /// DXIL block per IR block (unreachable: none).
- block: Vec<Option<usize>>,
- cur: usize,
- bools: HashMap<(usize, Bool), V>,
- lane_bits: HashMap<Value, V>,
- /// IR phi location by value.
- phi_at: HashMap<Value, (usize, usize)>,
- mask_phis: HashMap<Value, V>,
- pending_masks: Vec<(V, usize, usize)>,
- data_phis: Vec<(V, usize, usize)>,
- read_phis: HashSet<Value>,
- params: HashMap<Value, (usize, u32)>,
- user_data: Option<V>,
- user_reads: BTreeMap<u32, UserData>,
- samplers: Vec<[u32; 4]>,
- descriptors: HashMap<(Site, bool), &'a Source>,
- scalar: HashMap<Site, Address>,
- exports: HashMap<Site, &'a Export>,
- interps: HashMap<Site, &'a Interp>,
- handles: HashMap<(usize, Site, u8, bool, u32), V>,
- /// Blocks splitting the edge from one IR block to another.
- edges: HashMap<(usize, usize), usize>,
- /// Per IR block: a `done` export is behind every lane entering it.
- exported: Vec<bool>,
- /// Reverse-postorder index per IR block.
- rpo: Vec<usize>,
- /// DXIL block each IR block ends in (guarded stores split blocks).
- tail: Vec<Option<usize>>,
- /// The program writes UAVs: barriers fence them too.
- writes_uavs: bool,
- /// src0 of the current instruction as DPP fetched it.
- src0: Option<V>,
- /// Compute code that reads other lanes: thread ids follow the host
- /// lanes, so lane n is console lane n.
- console_lanes: bool,
- /// Mesh: the thread index in the group (made in the entry block).
- flat: Option<V>,
- /// Mesh: IR blocks whose `s_barrier` is left out.
- no_barrier: HashSet<usize>,
- /// Vertex and mesh outputs holding 16-bit values: row, whether two per
- /// dword, and the row packed after it.
- half_outputs: HashMap<u8, (bool, Option<u8>)>,
- /// How lane masks become data.
- mask_data: MaskData,
- /// The instruction being lowered may read lane masks as data.
- mask_ok: bool,
- /// Ballots of masks read as data, by low dword: (low, high).
- ballots: HashMap<Value, (V, V)>,
- /// Masks whose ballot is being built: a mask whose lane bit needs its
- /// own value as a number (an opaque mask keyed by a dword it shares
- /// with another) would otherwise recurse until the stack overflows.
- balloting: HashSet<Value>,
- /// Block writing each value.
- def_block: HashMap<Value, usize>,
- /// IR block being lowered.
- cur_ir: usize,
- /// Whole-wave sections by block and start index.
- sections: HashMap<usize, HashMap<usize, super::wave::Section>>,
- /// Values written in a section that code outside it reads.
- section_out: HashSet<Value>,
- /// The program's constant table (the code from the first byte read
- /// through `s_getpc_b64` on): global, first byte, dwords.
- code_table: Option<(V, u32, u32)>,
- /// Loop headers' iteration counters (see [`LOOP_LIMIT`]): the phi by
- /// IR block, and its incoming (value, DXIL block) pairs.
- counters: HashMap<usize, (V, Vec<(V, usize)>)>,
- /// The block a loop stopped by [`LOOP_LIMIT`] leaves the program from.
- abort: Option<usize>,
- }
- /// Iterations after which a loop is left (per entry into it), unless
- /// [`super::loop_limit`] says otherwise. The console runs a loop as long as
- /// its data says; data the host has not mirrored yet (a null view reads
- /// zeros) can make that four billion iterations, which hangs the host GPU
- /// until the driver resets it. A lane that reaches the limit leaves the
- /// program as killed (a pixel discards).
- // shortcut: a genuine loop of more iterations than this is cut short too.
- pub(super) const LOOP_LIMIT: u32 = 1 << 20;
- /// Lane masks read as numbers (`s_bcnt1`, `s_ff1`, `v_mbcnt`, scalar
- /// compares, bit tests) are ballots of the lanes' booleans, taken where the
- /// mask is written: structurisation's `active` masks hold only lanes that
- /// reach that block, so the ballot misses none.
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
- enum MaskData {
- /// Compute: host lane n is console lane n (the program declares the
- /// console's wave size), so ballots are the console's masks bit for bit
- /// and any use is exact.
- Console,
- /// Pixel and vertex programs: ballots over the host's wave, whatever its
- /// size. Only uses that mean the same on any wave are allowed: counts,
- /// the first lane, the count below a lane, equality (the HLSL wave
- /// operations the console compiler expands). Lane numbers stay host
- /// lanes throughout (`v_mbcnt`, `v_readlane` at a computed lane).
- Host,
- /// Mesh programs: their lanes and waves (LDS per console wave) are the
- /// console's but no host wave size can be declared.
- None,
- }
- /// What a value holding a lane mask read as a number stops with.
- const LANE_MASK: Why = "a lane mask read as data";
- /// Instructions that read a lane mask (not EXEC implicitly, not a lane's
- /// bit) as a number.
- fn reads_mask_as_data(st: &Structured, i: &Inst) -> bool {
- let is_mask = |v: Value| st.masks.contains_key(&v) || st.highs.contains_key(&v);
- let ops: Vec<&Operand> = match &i.fields {
- // A mask operation (its result a mask) is no data use.
- Fields::Sop { .. }
- if i.writes
- .iter()
- .any(|&(r, w)| matches!(r, Reg::S(_)) && is_mask(w)) =>
- {
- return false;
- }
- Fields::Sop { src, .. } => src.iter().collect(),
- Fields::Valu(v)
- if !matches!(i.name(), "v_cndmask_b32") && !i.name().contains("_co_ci_") =>
- {
- v.src.iter().collect()
- }
- _ => return false,
- };
- ops.into_iter().any(|op| match *op {
- Operand::Scalar { code, .. } if code != NULL => i.read(Reg::S(code)).is_some_and(is_mask),
- _ => false,
- })
- }
- /// Where half `h` (dword h / 2, the high half when odd) of a parameter
- /// packing 16-bit rows `a` and `b` two per dword comes from: a row and
- /// channel of `elements`, none for padding. Channel c of `a` is half c;
- /// `b` starts at the dword after `a`'s last channel.
- fn packed_half(elements: &[SigElement], a: u8, b: Option<u8>, h: u8) -> Option<(u8, u8)> {
- let extent = |r: u8| {
- elements
- .iter()
- .filter(|e| e.row == Some(u32::from(r)))
- .map(|e| e.col + e.cols)
- .max()
- };
- let na = extent(a)?;
- let a_halves = na.div_ceil(2) * 2;
- if h < a_halves {
- return (h < na).then_some((a, h));
- }
- let b = b?;
- let c = h - a_halves;
- (c < extent(b)?).then_some((b, c))
- }
- /// Vertex or mesh outputs of 16-bit values (see [`Lower::half_outputs`]):
- /// a packed row's partner is the next 16-bit row of the signature that no
- /// output entry names, taken in row order.
- fn half_outputs(io: &StageIo, outputs: &[SigElement]) -> HashMap<u8, (bool, Option<u8>)> {
- let entries = match io {
- StageIo::Vs { outputs, .. } => outputs,
- StageIo::Ms(ms) => &ms.outputs,
- _ => return HashMap::new(),
- };
- let per_vertex = || entries.iter().filter(|o| o.param & 0x80 == 0);
- let mut free: Vec<u8> = outputs
- .iter()
- .filter(|e| e.half && e.kind == 0)
- .filter_map(|e| u8::try_from(e.row?).ok())
- .filter(|&r| !per_vertex().any(|o| o.row == r))
- .collect();
- free.sort_unstable();
- free.dedup();
- let mut halves: Vec<_> = per_vertex().filter(|o| o.fp16).collect();
- halves.sort_by_key(|o| o.row);
- halves
- .into_iter()
- .map(|o| {
- let b = o
- .pack2
- .then(|| free.iter().position(|&r| r > o.row))
- .flatten();
- (o.row, (o.pack2, b.map(|i| free.remove(i))))
- })
- .collect()
- }
- fn unsupported(offset: usize, what: Why) -> Stop {
- Stop::Fail(ShaderError::Unsupported { offset, what })
- }
- impl<'a> Lower<'a> {
- fn new(x: &'a Parts<'a>, bad: &'a HashMap<Value, Why>) -> Self {
- let p = x.p;
- let mut phi_at = HashMap::new();
- for (b, block) in p.blocks.iter().enumerate() {
- for (i, phi) in block.phis.iter().enumerate() {
- phi_at.insert(phi.value, (b, i));
- }
- }
- let mut s = Shader::new(x.native.stage == HwStage::Ps);
- s.inputs = x.inputs.clone();
- s.outputs = x.outputs.clone();
- let insts = || p.blocks.iter().flat_map(|b| &b.insts);
- let writes_uavs = insts().any(|i| {
- matches!(i.fields, Fields::Image { .. } | Fields::Buffer { .. })
- && (i.name().contains("store") || i.name().contains("atomic"))
- });
- let mut console_lanes = false;
- if let StageIo::Cs(cs) = x.native.io {
- s.threads = Some(cs.threads);
- s.lds_dwords = cs.lds_bytes() / 4;
- // Code reading other lanes needs the console's wave size and
- // lane order; so do lane masks read as numbers, and LDS shared
- // within one wave, where the console compiler leaves out barriers.
- console_lanes = insts().any(|i| {
- let n = i.name();
- n.starts_with("ds_swizzle")
- || n.contains("permute")
- || n.starts_with("v_mbcnt")
- || matches!(n, "v_readlane_b32" | "v_writelane_b32")
- || matches!(&i.fields, Fields::Valu(v) if v.dpp.is_some())
- || reads_mask_as_data(x.st, i)
- }) || {
- // Masks held as numbers or constant lane sets: the lane
- // tests its own bit by its console lane.
- let whens = x.st.exits.iter().filter_map(|e| match *e {
- LaneExit::Branch { when, .. } => Some(when),
- _ => None,
- });
- let mut all: Vec<Bool> =
- x.st.guards
- .iter()
- .flatten()
- .flatten()
- .copied()
- .chain(whens)
- .collect();
- all.sort_unstable();
- all.dedup();
- all.into_iter().any(|g| {
- x.st.bools
- .support(g)
- .iter()
- .any(|a| matches!(a, Atom::Opaque(_) | Atom::Constant(_)))
- })
- };
- let wave = if x.native.wave64 { 64 } else { 32 };
- let one_wave = cs.threads.iter().product::<u32>() <= wave;
- if console_lanes || one_wave && s.lds_dwords > 0 {
- s.wave_size = Some(wave);
- }
- }
- let mut no_barrier = HashSet::new();
- if let StageIo::Ms(ms) = &x.native.io {
- s.prims = x.prims.clone();
- s.threads = Some([ms.threads, 1, 1]);
- s.lds_dwords = ms.lds_bytes.div_ceil(4);
- s.mesh = Some(super::shader::Mesh {
- max_vertices: ms.max_vertices,
- max_primitives: ms.max_primitives,
- topology: ms.topology,
- });
- // Barriers order the lanes' LDS accesses; the console's
- // allocation request needs them too, but the host does not. One
- // lane sends that request, and what it dominates (the path for
- // counts out of range) runs in that lane alone, so its barrier
- // would hang the host.
- let order = crate::ir::reverse_postorder(&p.blocks);
- let mut index = vec![usize::MAX; p.blocks.len()];
- for (n, &b) in order.iter().enumerate() {
- index[b] = n;
- }
- let idom = crate::ir::idoms(&p.blocks, &order, &index);
- let alloc: Vec<usize> = (0..p.blocks.len())
- .filter(|&b| {
- p.blocks[b].insts.iter().any(|i| {
- i.name() == "s_sendmsg"
- && matches!(i.fields, Fields::Sopp { imm } if imm & 0xF == GS_ALLOC_REQ)
- })
- })
- .collect();
- for &b in &order {
- let mut d = b;
- let dominated = loop {
- if alloc.contains(&d) {
- break true;
- }
- if idom[d] == d || idom[d] == usize::MAX {
- break false;
- }
- d = idom[d];
- };
- if ms.lds_bytes == 0 || dominated {
- no_barrier.insert(b);
- }
- }
- }
- Lower {
- s,
- x,
- bad,
- lane_fail: HashMap::new(),
- vals: HashMap::new(),
- floats: HashMap::new(),
- block: vec![None; p.blocks.len()],
- cur: 0,
- bools: HashMap::new(),
- lane_bits: HashMap::new(),
- phi_at,
- mask_phis: HashMap::new(),
- pending_masks: Vec::new(),
- data_phis: Vec::new(),
- read_phis: HashSet::new(),
- params: x.res.params.iter().map(|&(v, p, d)| (v, (p, d))).collect(),
- user_data: None,
- user_reads: BTreeMap::new(),
- samplers: Vec::new(),
- descriptors: x
- .res
- .descriptors
- .iter()
- .map(|d| ((d.site, d.role == Role::Sampler), &d.source))
- .collect(),
- scalar: x.res.scalar.iter().copied().collect(),
- exports: x.io.exports.iter().map(|e| (e.site, e)).collect(),
- interps: x.io.interps.iter().map(|(s, i)| (*s, i)).collect(),
- handles: HashMap::new(),
- edges: HashMap::new(),
- exported: vec![false; p.blocks.len()],
- rpo: vec![usize::MAX; p.blocks.len()],
- tail: vec![None; p.blocks.len()],
- writes_uavs,
- src0: None,
- console_lanes,
- flat: None,
- no_barrier,
- half_outputs: half_outputs(&x.native.io, &x.outputs),
- mask_data: match x.native.stage {
- HwStage::Cs => MaskData::Console,
- HwStage::Ps | HwStage::Vs => MaskData::Host,
- _ => MaskData::None,
- },
- mask_ok: false,
- ballots: HashMap::new(),
- balloting: HashSet::new(),
- def_block: p
- .blocks
- .iter()
- .enumerate()
- .flat_map(|(b, block)| {
- block
- .insts
- .iter()
- .flat_map(move |i| i.writes.iter().map(move |w| (w.1, b)))
- .chain(block.phis.iter().map(move |phi| (phi.value, b)))
- })
- .collect(),
- cur_ir: 0,
- sections: HashMap::new(),
- section_out: HashSet::new(),
- code_table: None,
- counters: HashMap::new(),
- abort: None,
- }
- }
- /// The dword of the program's code at byte `byte` (a constant table
- /// the compiler appends to the code), as an internal constant array.
- // shortcut: an index outside the table reads its last dword (the
- // console would read whatever memory follows the code).
- fn code_dword(&mut self, byte: V) -> Result<V, Why> {
- let (g, base, len) = match self.code_table {
- Some(t) => t,
- None => {
- let first = self
- .x
- .res
- .scalar
- .iter()
- .map(|s| s.1)
- .chain(self.x.res.descriptors.iter().flat_map(|d| {
- d.source.leaves().into_iter().filter_map(|l| match l {
- Source::Built { address, .. } => Some(*address),
- _ => None,
- })
- }))
- .filter(|a| a.param == CODE)
- .map(|a| a.offset & !3)
- .min()
- .ok_or("a code table")?;
- let code = &self.x.native.code;
- let words: Vec<u32> = code
- .get(first as usize..)
- .ok_or("a code table past the code")?
- .chunks(4)
- .map(|c| {
- let mut w = [0u8; 4];
- w[..c.len()].copy_from_slice(c);
- u32::from_le_bytes(w)
- })
- .collect();
- if words.is_empty() {
- return Err("a code table past the code");
- }
- let elems: Vec<V> = words.iter().map(|&w| self.s.m.i32(w)).collect();
- let arr = self.s.m.array(words.len() as u32, self.s.t.i32);
- let init = self.s.m.aggregate(arr, &elems);
- let g = self.s.m.constant_global("code", init);
- let t = (g, first, words.len() as u32);
- self.code_table = Some(t);
- t
- }
- };
- let c = self.cur;
- let k = self.s.m.i32(base);
- let rel = self.s.m.bin(c, Bin::Sub, byte, k);
- let two = self.s.m.i32(2);
- let index = self.s.m.bin(c, Bin::LShr, rel, two);
- let last = self.s.m.i32(len - 1);
- let over = self.s.m.cmp(c, pred::IUGT, index, last);
- let index = self.s.m.select(c, over, last, index);
- let at = self.s.m.element(c, g, index);
- Ok(self.s.m.load(c, at))
- }
- /// A global memory load (`global_load_*`): from the program's constant
- /// table, or from a root SRV at the address the title sets.
- fn flat(&mut self, inst: &Inst, b: usize, i: usize) -> Result<(), Why> {
- let Fields::Flat { dst, .. } = &inst.fields else {
- unreachable!()
- };
- let name = inst.name();
- let rest = name
- .strip_prefix("global_load_")
- .ok_or("a global memory write")?;
- let (n, sub) = match rest {
- "dword" => (1, None),
- "dwordx2" => (2, None),
- "dwordx3" => (3, None),
- "dwordx4" => (4, None),
- "ubyte" => (1, Some((8, false))),
- "sbyte" => (1, Some((8, true))),
- "ushort" => (1, Some((16, false))),
- "sshort" => (1, Some((16, true))),
- _ => return Err(name),
- };
- let a = *self
- .scalar
- .get(&(b, i))
- .ok_or("an untracked global address")?;
- let guard = self.guard(b, i)?;
- let Some(Operand::Vector { reg, .. }) = *dst else {
- return Err("a global load result");
- };
- let c = self.cur;
- let mut off = self.s.m.i32(a.offset);
- if let Some(d) = a.dynamic {
- let d = self.i32_of(d)?;
- off = self.s.m.bin(c, Bin::Add, off, d);
- }
- let h = if a.param == CODE {
- None
- } else {
- let src = Source::Built {
- address: a,
- bits: 0,
- words: [0, 0],
- };
- Some(self.handle((b, i), &src, false, (11, 0))?)
- };
- let dword = |l: &mut Self, byte: V| -> Result<V, Why> {
- match h {
- None => l.code_dword(byte),
- Some(h) => {
- let c = l.cur;
- let (u, mask, al) = (l.s.m.undef(l.s.t.i32), l.s.m.i8(1), l.s.m.i32(4));
- let r = l.s.op(
- c,
- "rawBufferLoad.f32",
- Attr::ReadOnly,
- l.s.t.res_ret,
- 139,
- &[h, byte, u, mask, al],
- );
- let x = l.s.m.extract(c, r, 0);
- Ok(l.as_int(x))
- }
- }
- };
- for k in 0..n {
- let x = match sub {
- None => {
- let four = self.s.m.i32(4 * k);
- let at = self.s.m.bin(c, Bin::Add, off, four);
- dword(self, at)?
- }
- Some((width, signed)) => {
- // shortcut: the field is taken not to cross a dword.
- let (not3, three) = (self.s.m.i32(!3), self.s.m.i32(3));
- let at = self.s.m.bin(c, Bin::And, off, not3);
- let w = dword(self, at)?;
- let byte = self.s.m.bin(c, Bin::And, off, three);
- let sh = self.s.m.bin(c, Bin::Shl, byte, three);
- let x = self.s.m.bin(c, Bin::LShr, w, sh);
- let left = self.s.m.i32(32 - width);
- let x = self.s.m.bin(c, Bin::Shl, x, left);
- self.s
- .m
- .bin(c, if signed { Bin::AShr } else { Bin::LShr }, x, left)
- }
- };
- self.write(inst, Reg::V(reg + k as u8), x, guard);
- }
- Ok(())
- }
- /// Finds the whole-wave sections and the values they leave.
- fn find_sections(&mut self) {
- let p = self.x.p;
- let mut inside: HashMap<Value, (usize, usize, usize)> = HashMap::new();
- for b in 0..p.blocks.len() {
- let found = super::wave::sections(p, b);
- for (&start, sec) in &found {
- for i in start + 1..sec.end {
- for &(_, w) in &p.blocks[b].insts[i].writes {
- inside.insert(w, (b, start, sec.end));
- }
- }
- }
- if !found.is_empty() {
- self.sections.insert(b, found);
- }
- }
- if inside.is_empty() {
- return;
- }
- for (b, block) in p.blocks.iter().enumerate() {
- for phi in &block.phis {
- self.section_out
- .extend(phi.args.iter().filter(|a| inside.contains_key(a)));
- }
- for (i, inst) in block.insts.iter().enumerate() {
- for &(_, r) in &inst.reads {
- if let Some(&(sb, start, end)) = inside.get(&r)
- && !(sb == b && i > start && i < end)
- {
- self.section_out.insert(r);
- }
- }
- }
- }
- }
- /// An instruction inside a whole-wave section: the values that leave
- /// the section are made from what the simulation found them to be.
- fn section_inst(&mut self, b: usize, i: usize, start: usize) {
- const UNKNOWN: Why = "a whole-wave section step the simulation does not follow";
- let inst = &self.x.p.blocks[b].insts[i];
- for &(_, w) in &inst.writes {
- let val = if self.section_out.contains(&w) {
- let shape = self.sections[&b][&start].shapes.get(&w).copied();
- match shape.ok_or(UNKNOWN).and_then(|sh| self.shape(sh)) {
- Ok(x) => Val::I32(x),
- Err(why) => Val::Missing(why),
- }
- } else {
- Val::Missing(UNKNOWN)
- };
- self.vals.insert(w, val);
- }
- }
- /// A value a whole-wave section leaves, as DXIL wave operations over the
- /// host's active lanes: the console's result with console lanes; on
- /// another wave size, what the HLSL wave operation the console compiler
- /// expanded gives on that wave.
- fn shape(&mut self, shape: super::wave::Shape) -> Result<V, Why> {
- use super::wave::{Op, Shape};
- let (op, src, mask, ident, prefix) = match shape {
- Shape::K(k) => return Ok(self.s.m.i32(k)),
- Shape::Value(v) => return self.i32_of(v),
- Shape::Active(op, src, mask, ident) => (op, src, mask, ident, None),
- Shape::Masked(src, mask, ident) => {
- let c = self.cur;
- let &m = self
- .x
- .st
- .masks
- .get(&mask)
- .ok_or("a wave reduction's untracked mask")?;
- let on = self.boolean(m, c)?;
- let x = self.i32_of(src)?;
- let id = self.s.m.i32(ident);
- return Ok(self.s.m.select(c, on, x, id));
- }
- Shape::Prefix(op, src, mask, ident, inclusive) => {
- (op, src, mask, ident, Some(inclusive))
- }
- };
- if self.mask_data == MaskData::None {
- return Err("a wave reduction in a mesh shader");
- }
- let c = self.cur;
- let &m = self
- .x
- .st
- .masks
- .get(&mask)
- .ok_or("a wave reduction's untracked mask")?;
- let on = self.boolean(m, c)?;
- let x = self.i32_of(src)?;
- let id = self.s.m.i32(ident);
- let x = self.s.m.select(c, on, x, id);
- self.s.flags |= flags::WAVE_OPS;
- let float = matches!(op, Op::AddF | Op::MinF | Op::MaxF);
- let (i32t, f32t) = (self.s.t.i32, self.s.t.f32);
- let arg = if float { self.as_float(x) } else { x };
- let ty = if float { f32t } else { i32t };
- let sfx = if float { "f32" } else { "i32" };
- let r = match (op, prefix) {
- (Op::AddU | Op::AddF, Some(inclusive)) => {
- let (k0, k1) = (self.s.m.i8(0), self.s.m.i8(1));
- let r = self.s.op(
- c,
- &format!("wavePrefixOp.{sfx}"),
- Attr::NoUnwind,
- ty,
- 121,
- &[arg, k0, k1],
- );
- if inclusive {
- self.s.m.bin(c, Bin::Add, r, arg)
- } else {
- r
- }
- }
- (_, Some(_)) => return Err("a wave prefix of an operation other than a sum"),
- (Op::Or | Op::And | Op::Xor, None) => {
- let k = self.s.m.i8(match op {
- Op::And => 0,
- Op::Or => 1,
- _ => 2,
- });
- self.s
- .op(c, "waveActiveBit.i32", Attr::NoUnwind, i32t, 120, &[arg, k])
- }
- (_, None) => {
- let (code, unsigned) = match op {
- Op::AddU => (0, 1),
- Op::AddF => (0, 0),
- Op::MinU => (2, 1),
- Op::MaxU => (3, 1),
- Op::MinI | Op::MinF => (2, 0),
- _ => (3, 0),
- };
- let (k, u) = (self.s.m.i8(code), self.s.m.i8(unsigned));
- self.s.op(
- c,
- &format!("waveActiveOp.{sfx}"),
- Attr::NoUnwind,
- ty,
- 119,
- &[arg, k, u],
- )
- }
- };
- Ok(if float { self.as_int(r) } else { r })
- }
- fn run(mut self) -> Result<Lowered, Stop> {
- let p = self.x.p;
- let order = crate::ir::reverse_postorder(&p.blocks);
- for (n, &b) in order.iter().enumerate() {
- self.rpo[b] = n;
- }
- for (n, &b) in order.iter().enumerate() {
- self.block[b] = Some(if n == 0 {
- self.s.entry
- } else {
- self.s.m.new_block()
- });
- }
- // Loop headers count iterations (`LOOP_LIMIT`).
- for &b in &order {
- let header = p.blocks[b]
- .preds
- .iter()
- .any(|&q| self.block[q].is_some() && self.rpo[b] <= self.rpo[q]);
- if header {
- let blk = self.block[b].expect("reachable");
- let phi = self.s.m.phi(blk, self.s.t.i32);
- self.counters.insert(b, (phi, Vec::new()));
- }
- }
- self.cur = self.s.entry;
- self.find_sections();
- self.inputs()?;
- if let Some(mesh) = self.s.mesh {
- self.mesh_prologue(mesh).map_err(|w| unsupported(0, w))?;
- self.block[order[0]] = Some(self.cur);
- }
- for &b in &order {
- self.cur = self.block[b].expect("reachable");
- self.cur_ir = b;
- let preds: Vec<usize> = p.blocks[b]
- .preds
- .iter()
- .copied()
- .filter(|&q| self.block[q].is_some())
- .collect();
- self.exported[b] = !preds.is_empty() && preds.iter().all(|&q| self.exported_out(q));
- for (i, phi) in p.blocks[b].phis.iter().enumerate() {
- let val = match phi.reg {
- Reg::S(c) if c == EXEC || c == EXEC + 1 => Val::Missing("a lane mask"),
- _ if self.bad.contains_key(&phi.value) => Val::Missing(self.bad[&phi.value]),
- Reg::Scc => {
- let v = self.s.m.phi(self.cur, self.s.t.i1);
- self.data_phis.push((v, b, i));
- Val::Bit(v)
- }
- _ => {
- let v = self.s.m.phi(self.cur, self.s.t.i32);
- self.data_phis.push((v, b, i));
- Val::I32(v)
- }
- };
- self.vals.insert(phi.value, val);
- }
- let mut i = 0;
- while i < p.blocks[b].insts.len() {
- self.inst(b, i)?;
- if let Some(end) = self.sections.get(&b).and_then(|s| s.get(&i)).map(|s| s.end) {
- for j in i + 1..end {
- self.section_inst(b, j, i);
- }
- i = end;
- continue;
- }
- i += 1;
- }
- self.mask_ok = self.mask_data == MaskData::Console;
- self.tail[b] = Some(self.cur);
- self.exit(b)?;
- }
- self.fill_phis()?;
- let user_data = self.user_reads.values().copied().collect();
- self.s.samplers = self.samplers.len() as u32;
- let root_cbvs = self.s.root_cbvs.iter().map(|r| r.0).collect();
- let root_srvs = self.s.root_srvs.iter().map(|r| r.0).collect();
- let cmp_samplers = self.s.cmp_samplers;
- Ok(Lowered {
- cmp_samplers,
- root_cbvs,
- root_srvs,
- dxil: self.s.finish(),
- user_data,
- samplers: self.samplers,
- })
- }
- fn exported_out(&self, b: usize) -> bool {
- self.exported[b] || self.has_done_export(b)
- }
- fn has_done_export(&self, b: usize) -> bool {
- (0..self.x.p.blocks[b].insts.len())
- .any(|i| self.exports.get(&(b, i)).is_some_and(|e| e.done))
- }
- /// Values of the program's input registers.
- fn inputs(&mut self) -> Result<(), Stop> {
- let io_inputs: HashMap<Value, Input> = self.x.io.inputs.iter().copied().collect();
- for &(reg, v) in &self.x.p.inputs {
- let val = match reg {
- Reg::S(_) if self.params.contains_key(&v) => continue, // loaded on first use
- Reg::S(n) => match io_inputs.get(&v) {
- Some(&input) => self.input(input),
- // Above the SGPRs the hardware initializes, a register
- // holds what the last wave left, so no program depends
- // on it: a read the paths only seem to allow (a branch
- // past the write, taken only with one past the read)
- // reads zero, as VGPRs do.
- None if n <= 105 && self.x.io.initialized_sgprs.is_some_and(|k| n >= k) => {
- Val::I32(self.s.m.i32(0))
- }
- None => Val::Missing("a hardware SGPR input"),
- },
- Reg::Scc => Val::Missing("SCC on entry"),
- Reg::V(_) => match io_inputs.get(&v) {
- Some(&input) => self.input(input),
- // Registers no one set read as zero (the validator
- // refuses reads of undef).
- None => Val::I32(self.s.m.i32(0)),
- },
- };
- self.vals.insert(v, val);
- }
- Ok(())
- }
- fn input(&mut self, input: Input) -> Val {
- let (kind, col) = match input {
- Input::VertexId => (1, 0),
- Input::InstanceId => (2, 0),
- Input::Position(c) => (3, c),
- Input::Attribute { row, channel } => {
- return match self.find_input(row, channel) {
- Some((id, col)) => self.load_input(id, col),
- None => Val::Missing("a vertex input the signature lacks"),
- };
- }
- Input::Barycentric { .. } => return Val::Missing("barycentrics read directly"),
- Input::PrimitiveId => return Val::Missing("the primitive id"),
- // The console code tests it against 0 (`v_cmp_ne_u32`), as the
- // host's value can be.
- Input::FrontFace => (13, 0),
- Input::ThreadIdInGroup(c) => return self.thread_id(c),
- Input::GroupId(c) => return self.compute_id("groupId.i32", 94, c),
- Input::GroupInfo => return Val::Missing("the thread-group info SGPR"),
- Input::MeshGroup => {
- let Val::I32(g) = self.compute_id("groupId.i32", 94, 0) else {
- unreachable!()
- };
- let n = self.s.m.i32(self.x.mesh_stride);
- return Val::I32(self.s.m.bin(self.s.entry, Bin::Mul, g, n));
- }
- Input::MergedWaveInfo => {
- // shortcut: only the wave index (bits 24-27) is set; the
- // console's other bits (lane counts) are read by no program.
- let f = self.mesh_thread();
- let shift = self.s.m.i32(self.wave().trailing_zeros());
- let w = self.s.m.bin(self.s.entry, Bin::LShr, f, shift);
- let k24 = self.s.m.i32(24);
- return Val::I32(self.s.m.bin(self.s.entry, Bin::Shl, w, k24));
- }
- _ => return Val::Missing("a pixel system input"),
- };
- let free = self
- .s
- .inputs
- .iter()
- .filter_map(|e| e.row)
- .map(|r| r + 1)
- .max()
- .unwrap_or(0);
- let id = match self.s.inputs.iter().position(|e| e.kind == kind) {
- Some(id) => {
- if kind != 3 {
- self.s.inputs[id].row.get_or_insert(free);
- }
- id
- }
- None if kind >= 3 => return Val::Missing("a pixel system input the signature lacks"),
- None => {
- // The console signature leaves out vertex and instance ids; PC
- // gives them a row after the others.
- let row = self
- .s
- .inputs
- .iter()
- .filter_map(|e| e.row)
- .map(|r| r + 1)
- .max()
- .unwrap_or(0);
- self.s.inputs.push(SigElement {
- name: ["", "SV_VertexID", "SV_InstanceID"][kind as usize].into(),
- index: 0,
- kind,
- comp: 5,
- interp: 0,
- row: Some(row),
- col: 0,
- cols: 1,
- usage: 0,
- half: false,
- });
- self.s.inputs.len() - 1
- }
- };
- self.load_input(id, col)
- }
- /// A component of a compute thread or group id, read in the entry block.
- fn compute_id(&mut self, name: &str, opcode: u32, c: u8) -> Val {
- let (entry, c) = (self.s.entry, self.s.m.i32(u32::from(c)));
- Val::I32(
- self.s
- .op(entry, name, Attr::ReadNone, self.s.t.i32, opcode, &[c]),
- )
- }
- fn find_input(&self, row: u8, channel: u8) -> Option<(usize, u8)> {
- self.s.inputs.iter().enumerate().find_map(|(id, e)| {
- let r = e.row?;
- // `then`, not `then_some`: the channel is only past `col` when
- // the condition holds.
- (r == u32::from(row) && channel >= e.col && channel < e.col + e.cols)
- .then(|| (id, channel - e.col))
- })
- }
- fn load_input(&mut self, id: usize, col: u8) -> Val {
- if self.s.inputs[id].half {
- // shortcut: 16-bit vertex inputs (the fetch routine's) are not
- // unpacked; pixel inputs are (`interp`).
- return Val::Missing("a 16-bit input");
- }
- self.load_any_input(id, col)
- }
- /// An input element's channel as declared (16-bit ones as 32-bit floats).
- fn load_any_input(&mut self, id: usize, col: u8) -> Val {
- let e = &mut self.s.inputs[id];
- e.usage |= 1 << col;
- let (int, bit) = (e.comp != 9, e.comp == 1);
- let ty = if bit {
- self.s.t.i1
- } else if int {
- self.s.t.i32
- } else {
- self.s.t.f32
- };
- let suffix = if bit {
- ".i1"
- } else if int {
- ".i32"
- } else {
- ".f32"
- };
- let entry = self.s.entry;
- let args = [
- self.s.m.i32(id as u32),
- self.s.m.i32(0),
- self.s.m.i8(col),
- self.s.m.undef(self.s.t.i32),
- ];
- let v = self.s.op(
- entry,
- &format!("loadInput{suffix}"),
- Attr::ReadNone,
- ty,
- 4,
- &args,
- );
- if bit {
- let z = self.s.m.cast(entry, Cast::ZExt, v, self.s.t.i32);
- Val::I32(z)
- } else if int {
- Val::I32(v)
- } else {
- Val::I32(self.s.m.cast(entry, Cast::Bitcast, v, self.s.t.i32))
- }
- }
- /// The DXIL form of an IR value.
- fn get(&mut self, v: Value) -> Result<Val, Why> {
- if self.phi_at.contains_key(&v) {
- self.read_phis.insert(v);
- }
- let is_mask = self.x.st.masks.contains_key(&v) || self.x.st.highs.contains_key(&v);
- match self.vals.get(&v) {
- Some(&Val::Missing(_)) | None if self.mask_ok && is_mask => {
- return self.mask_dword(v).map(Val::I32);
- }
- Some(&Val::Missing(w)) => return Err(w),
- Some(&val) => return Ok(val),
- None => {}
- }
- if let Some(&(param, dword)) = self.params.get(&v) {
- let val = self.user(param, dword);
- self.vals.insert(v, val);
- return Ok(val);
- }
- Err("a value defined after its use")
- }
- fn i32_of(&mut self, v: Value) -> Result<V, Why> {
- match self.get(v)? {
- Val::I32(x) => Ok(x),
- Val::Bit(x) => Ok(self.s.m.cast(self.cur, Cast::ZExt, x, self.s.t.i32)),
- Val::Missing(w) => Err(w),
- }
- }
- /// A user-data dword: a root parameter's value.
- fn user(&mut self, param: usize, dword: u32) -> Val {
- let rp = self.x.native.layout[param];
- match rp.root {
- RootType::ResourceHeap | RootType::SamplerHeap => return Val::I32(self.s.m.i32(0)),
- RootType::Constants | RootType::Table => {}
- _ => return Val::Missing("a root descriptor"),
- }
- let index = u32::from(rp.user_data) + dword;
- self.user_reads.insert(
- index,
- UserData {
- index,
- slot: rp.slot,
- root: rp.root,
- dword,
- },
- );
- self.s.user_data = self.s.user_data.max(index + 1);
- let h = match self.user_data {
- Some(h) => h,
- None => {
- let h = self.s.user_data_handle();
- self.user_data = Some(h);
- h
- }
- };
- let entry = self.s.entry;
- let row = self.s.m.i32(index / 4);
- let r = self.s.op(
- entry,
- "cbufferLoadLegacy.i32",
- Attr::ReadOnly,
- self.s.t.cbuf_ret,
- 59,
- &[h, row],
- );
- Val::I32(self.s.m.extract(entry, r, index % 4))
- }
- /// A dword of a lane mask as a number: the ballot of the lanes' bits,
- /// taken in the block that writes the mask (see [`MaskData`]).
- fn mask_dword(&mut self, v: Value) -> Result<V, Why> {
- let (lo, high) = match self.x.st.highs.get(&v) {
- Some(&lo) => (lo, true),
- None => (v, false),
- };
- let &m = self.x.st.masks.get(&lo).ok_or(LANE_MASK)?;
- if !self.x.st.active.contains(&lo) {
- return Err("a lane mask holding lanes elsewhere read as data");
- }
- let (x, y) = match self.ballots.get(&lo) {
- Some(&xy) => xy,
- None => {
- let at = match self.def_block.get(&lo) {
- // Program inputs (EXEC on entry).
- None => self.s.entry,
- Some(&b) if b == self.cur_ir => self.cur,
- Some(&b) => self.tail[b].ok_or("a lane mask written after its use")?,
- };
- if !self.balloting.insert(lo) {
- return Err("a lane mask whose lanes depend on its own value");
- }
- let bit = self.boolean(m, at);
- self.balloting.remove(&lo);
- let bit = bit?;
- let i32t = self.s.t.i32;
- let four = self.s.m.named_struct("dx.types.fouri32", vec![i32t; 4]);
- let r = self
- .s
- .op(at, "waveActiveBallot", Attr::NoUnwind, four, 116, &[bit]);
- self.s.flags |= flags::WAVE_OPS;
- let xy = (self.s.m.extract(at, r, 0), self.s.m.extract(at, r, 1));
- self.ballots.insert(lo, xy);
- xy
- }
- };
- Ok(if high { y } else { x })
- }
- // ---- operands ----
- fn operand(&mut self, inst: &Inst, op: &Operand) -> Result<V, Why> {
- match *op {
- Operand::Scalar { code: NULL, .. } => Ok(self.s.m.i32(0)),
- Operand::Scalar { code, .. } => {
- let v = inst.read(Reg::S(code)).ok_or("an unread operand")?;
- self.i32_of(v)
- }
- Operand::Vector { reg, .. } => {
- let v = inst.read(Reg::V(reg)).ok_or("an unread operand")?;
- self.i32_of(v)
- }
- Operand::Const(c) => crate::resource::inline(c)
- .map(|x| self.s.m.i32(x))
- .ok_or("a special operand (vccz, execz, scc, lds_direct)"),
- Operand::Literal(x) => Ok(self.s.m.i32(x)),
- Operand::Attr { .. } => Err("an attribute operand"),
- }
- }
- /// GFX10 puts an inline f16 constant in the low half of the operand.
- fn f16_operand(&mut self, inst: &Inst, op: &Operand) -> Result<V, Why> {
- match *op {
- Operand::Const(c @ 240..=248) => {
- let h = [
- 0x3800, 0xB800, 0x3C00, 0xBC00, 0x4000, 0xC000, 0x4400, 0xC400, 0x3118,
- ][usize::from(c - 240)];
- Ok(self.s.m.i32(h))
- }
- _ => self.operand(inst, op),
- }
- }
- fn as_float(&mut self, x: V) -> V {
- self.s.m.cast(self.cur, Cast::Bitcast, x, self.s.t.f32)
- }
- fn as_int(&mut self, f: V) -> V {
- self.s.m.cast(self.cur, Cast::Bitcast, f, self.s.t.i32)
- }
- /// The SDWA operand selection of source `k`: a byte or word of the
- /// dword, zero- or sign-extended (none: the whole dword).
- fn sdwa_src(inst: &Inst, k: usize) -> Option<(u8, bool)> {
- let Fields::Valu(v) = &inst.fields else {
- return None;
- };
- let s = v.sdwa?;
- let sel = *s.src_sel.get(k)?;
- let sext = s.sext >> k & 1 != 0;
- (sel < 6).then_some((sel, sext))
- }
- /// Bits `sel` (0-3 a byte, 4-5 a word) of `x`, extended.
- fn select_bits(&mut self, x: V, sel: u8, sext: bool) -> V {
- let c = self.cur;
- let (width, at) = if sel < 4 {
- (8, 8 * sel)
- } else {
- (16, 16 * (sel - 4))
- };
- let left = self.s.m.i32(u32::from(32 - width - at));
- let t = self.s.m.bin(c, Bin::Shl, x, left);
- let right = self.s.m.i32(32 - u32::from(width));
- let op = if sext { Bin::AShr } else { Bin::LShr };
- self.s.m.bin(c, op, t, right)
- }
- /// A float source with its abs/neg modifiers.
- fn fsrc(&mut self, inst: &Inst, k: usize) -> Result<V, Why> {
- let Fields::Valu(v) = &inst.fields else {
- unreachable!()
- };
- let op = *v.src.get(k).ok_or("a missing operand")?;
- let selected = Self::sdwa_src(inst, k);
- let mut f = match op {
- _ if selected.is_some() => {
- let (sel, sext) = selected.expect("checked");
- let x = self.operand(inst, &op)?;
- let x = self.select_bits(x, sel, sext);
- self.as_float(x)
- }
- _ if k == 0 && self.src0.is_some() => {
- let x = self.src0.expect("checked");
- self.as_float(x)
- }
- Operand::Vector { reg, .. } | Operand::Scalar { code: reg, .. }
- if !matches!(op, Operand::Scalar { code: NULL, .. }) =>
- {
- let r = if matches!(op, Operand::Vector { .. }) {
- Reg::V(reg)
- } else {
- Reg::S(reg)
- };
- let val = inst.read(r).ok_or("an unread operand")?;
- match self.floats.get(&val) {
- Some(&f) => f,
- None => {
- let x = self.i32_of(val)?;
- self.as_float(x)
- }
- }
- }
- _ => {
- let x = self.operand(inst, &op)?;
- self.as_float(x)
- }
- };
- if v.abs >> k & 1 != 0 {
- f = self.unary(6, f);
- }
- if v.neg >> k & 1 != 0 {
- let z = self.s.m.f32(0x8000_0000);
- f = self.s.m.bin(self.cur, Bin::Sub, z, f);
- }
- Ok(f)
- }
- fn src(&mut self, inst: &Inst, k: usize) -> Result<V, Why> {
- let Fields::Valu(v) = &inst.fields else {
- unreachable!()
- };
- let op = *v.src.get(k).ok_or("a missing operand")?;
- if v.abs >> k & 1 != 0 || v.neg >> k & 1 != 0 {
- return Err("integer operand modifiers");
- }
- match self.src0 {
- Some(x) if k == 0 => Ok(x),
- _ => {
- let x = self.operand(inst, &op)?;
- Ok(match Self::sdwa_src(inst, k) {
- Some((sel, sext)) => self.select_bits(x, sel, sext),
- None => x,
- })
- }
- }
- }
- fn unary(&mut self, op: u32, f: V) -> V {
- self.s.op(
- self.cur,
- "unary.f32",
- Attr::ReadNone,
- self.s.t.f32,
- op,
- &[f],
- )
- }
- fn binary(&mut self, op: u32, a: V, b: V) -> V {
- self.s.op(
- self.cur,
- "binary.f32",
- Attr::ReadNone,
- self.s.t.f32,
- op,
- &[a, b],
- )
- }
- /// The hardware's maximum (`max`) or minimum of two floats: as DXIL's
- /// FMax/FMin, but -0 orders below +0 (RDNA2 ISA, `v_max_f32`), where
- /// the compare and select DXIL specifies gives either zero.
- fn max_min(&mut self, max: bool, a: V, b: V) -> V {
- let c = self.cur;
- let r = self.binary(if max { 35 } else { 36 }, a, b);
- let (ia, ib) = (self.as_int(a), self.as_int(b));
- let either = self.s.m.bin(c, Bin::Or, ia, ib);
- let magnitude = self.s.m.i32(0x7FFF_FFFF);
- let either = self.s.m.bin(c, Bin::And, either, magnitude);
- let zero = self.s.m.i32(0);
- let zeros = self.s.m.cmp(c, pred::EQ, either, zero);
- // Of two zeros, +0 unless both are -0 (max), -0 unless neither (min).
- let op = if max { Bin::And } else { Bin::Or };
- let sign = self.s.m.bin(c, op, ia, ib);
- let signed = self.as_float(sign);
- self.s.m.select(c, zeros, signed, r)
- }
- fn fmad(&mut self, a: V, b: V, c: V) -> V {
- self.s.op(
- self.cur,
- "tertiary.f32",
- Attr::ReadNone,
- self.s.t.f32,
- 46,
- &[a, b, c],
- )
- }
- /// `f` as an f16 in the low bits, rounded toward zero: the nearest-even
- /// conversion, one step back toward zero where it rounded away (which
- /// also turns an overflow to infinity into the largest finite half).
- fn f16_toward_zero(&mut self, f: V) -> V {
- let c = self.cur;
- let i32t = self.s.t.i32;
- let h = self
- .s
- .op(c, "legacyF32ToF16", Attr::ReadNone, i32t, 130, &[f]);
- let back = self
- .s
- .op(c, "legacyF16ToF32", Attr::ReadNone, self.s.t.f32, 131, &[h]);
- let (fa, ba) = (self.unary(6, f), self.unary(6, back));
- // Ordered: never for NaN.
- let away = self.s.m.cmp(c, pred::OGT, ba, fa);
- let one = self.s.m.i32(1);
- let stepped = self.s.m.bin(c, Bin::Sub, h, one);
- self.s.m.select(c, away, stepped, h)
- }
- /// `(a >> off[4:0]) & ((1 << width) - 1)`, sign-extended from the field's
- /// top bit when `signed`; widths from 32 on take every bit. A signed
- /// field that runs past bit 31 extends from bit 31, as the hardware
- /// does: the shift is arithmetic then.
- fn bfe(&mut self, a: V, off: V, width: V, signed: bool) -> V {
- let c = self.cur;
- let (k31, k32, zero, one, ones) = (
- self.s.m.i32(31),
- self.s.m.i32(32),
- self.s.m.i32(0),
- self.s.m.i32(1),
- self.s.m.i32(u32::MAX),
- );
- let off = self.s.m.bin(c, Bin::And, off, k31);
- let shift = if signed { Bin::AShr } else { Bin::LShr };
- let shifted = self.s.m.bin(c, shift, a, off);
- let full = self.s.m.cmp(c, pred::IUGE, width, k32);
- let w = self.s.m.bin(c, Bin::And, width, k31);
- let mask = self.s.m.bin(c, Bin::Shl, one, w);
- let mask = self.s.m.bin(c, Bin::Sub, mask, one);
- let mask = self.s.m.select(c, full, ones, mask);
- let r = self.s.m.bin(c, Bin::And, shifted, mask);
- if !signed {
- return r;
- }
- // Sign-extend a field narrower than 32 bits (none for width 0).
- let left = self.s.m.bin(c, Bin::Sub, k32, w);
- let left = self.s.m.bin(c, Bin::And, left, k31);
- let t = self.s.m.bin(c, Bin::Shl, r, left);
- let t = self.s.m.bin(c, Bin::AShr, t, left);
- let is0 = self.s.m.cmp(c, pred::EQ, w, zero);
- let keep = self.s.m.bin(c, Bin::Or, full, is0);
- self.s.m.select(c, keep, r, t)
- }
- /// The median of three. With a NaN among them the hardware gives the
- /// minimum of the three instead (the host's minimum skips NaNs, so that
- /// is the smallest number among them).
- fn med3(&mut self, a: V, b: V, third: V) -> V {
- let c = self.cur;
- let lo = self.binary(36, a, b);
- let hi = self.binary(35, a, b);
- let t = self.binary(36, hi, third);
- let median = self.binary(35, lo, t);
- let least = self.binary(36, lo, third);
- let ab = self.s.m.cmp(c, pred::UNO, a, b);
- let nan3 = self.s.m.cmp(c, pred::UNO, third, third);
- let nan = self.s.m.bin(c, Bin::Or, ab, nan3);
- self.s.m.select(c, nan, least, median)
- }
- /// `a - floor(a)`, which on the hardware never reaches 1.0: where it
- /// would round to it (a tiny negative `a`), the result is `below_one`,
- /// the largest value under 1.0 in the operation's width. Hosts differ
- /// here (some give 1.0). Comparing against a `threshold` distinct from
- /// `below_one` prevents drivers (observed on NVIDIA) from folding
- /// `r > k ? k : r` into `minNum` (`FMNMX`) and swallowing NaN.
- fn fract(&mut self, a: V, threshold: f32, below_one: f32) -> V {
- let c = self.cur;
- let r = self.unary(22, a);
- let limit = self.s.m.f32(threshold.to_bits());
- let k = self.s.m.f32(below_one.to_bits());
- let over = self.s.m.cmp(c, pred::OGE, r, limit);
- self.s.m.select(c, over, k, r)
- }
- /// Sine, or cosine, of an angle in turns, as the hardware takes it. The
- /// whole turns are taken off first (`x - round_even(x)`, exact, within
- /// half a turn), so a large angle keeps its fraction; scaling to radians
- /// first rounds it away. A zero passes unreduced (the sine of -0 is -0).
- /// The sine of a half turn and the cosine of a quarter turn are exactly
- /// +0 on the hardware, where the host's function of the rounded angle
- /// gives about 1e-7. Infinities and NaN give NaN.
- /// shortcut: below 2^-12 turns the hardware scales by a slightly
- /// different constant than 2π (a last-place difference).
- fn trig_turns(&mut self, x: V, cosine: bool) -> V {
- let c = self.cur;
- let zero = self.s.m.f32(0);
- let whole = self.unary(26, x);
- let part = self.s.m.bin(c, Bin::Sub, x, whole);
- let is_zero = self.s.m.cmp(c, pred::OEQ, x, zero);
- let part = self.s.m.select(c, is_zero, x, part);
- let k = self.s.m.f32(TWO_PI.to_bits());
- let angle = self.s.m.bin(c, Bin::Mul, part, k);
- let r = self.unary(if cosine { 12 } else { 13 }, angle);
- let at = if cosine { 0.25f32 } else { 0.5 };
- let at = self.s.m.f32(at.to_bits());
- let size = self.unary(6, part);
- let exact = self.s.m.cmp(c, pred::OEQ, size, at);
- self.s.m.select(c, exact, zero, r)
- }
- /// The high dword of a 32 × 32-bit product.
- fn mul_hi(&mut self, a: V, b: V, signed: bool) -> V {
- let c = self.cur;
- let i64t = self.s.m.int(64);
- let ext = if signed { Cast::SExt } else { Cast::ZExt };
- let a = self.s.m.cast(c, ext, a, i64t);
- let b = self.s.m.cast(c, ext, b, i64t);
- let m = self.s.m.bin(c, Bin::Mul, a, b);
- let k32 = self.s.m.int_const(i64t, 32);
- let hi = self.s.m.bin(c, Bin::LShr, m, k32);
- self.s.flags |= flags::INT64;
- self.s.m.cast(c, Cast::Trunc, hi, self.s.t.i32)
- }
- /// `0 × anything = 0`, as in D3D9.
- fn legacy_mul(&mut self, a: V, b: V) -> V {
- let c = self.cur;
- let m = self.s.m.bin(c, Bin::Mul, a, b);
- let zero = self.s.m.f32(0);
- let az = self.s.m.cmp(c, pred::OEQ, a, zero);
- let bz = self.s.m.cmp(c, pred::OEQ, b, zero);
- let z = self.s.m.bin(c, Bin::Or, az, bz);
- self.s.m.select(c, z, zero, m)
- }
- // ---- lane booleans ----
- fn boolean(&mut self, b: Bool, at: usize) -> Result<V, Why> {
- if b == Bool::TRUE || b == Bool::FALSE {
- return Ok(self.s.m.bool(b == Bool::TRUE));
- }
- if let Some(&v) = self.bools.get(&(at, b)) {
- return Ok(v);
- }
- let (atom, lo, hi) = self.x.st.bools.node(b).expect("not a constant");
- let a = self.atom(atom, at)?;
- let m = &mut self.s.m;
- let v = match (lo, hi) {
- (Bool::FALSE, Bool::TRUE) => a,
- (Bool::TRUE, Bool::FALSE) => {
- let t = m.bool(true);
- m.bin(at, Bin::Xor, a, t)
- }
- (Bool::FALSE, h) => {
- let h = self.boolean(h, at)?;
- self.s.m.bin(at, Bin::And, a, h)
- }
- (l, Bool::TRUE) => {
- let l = self.boolean(l, at)?;
- self.s.m.bin(at, Bin::Or, a, l)
- }
- (l, h) => {
- let l = self.boolean(l, at)?;
- let h = self.boolean(h, at)?;
- self.s.m.select(at, a, h, l)
- }
- };
- self.bools.insert((at, b), v);
- Ok(v)
- }
- fn atom(&mut self, atom: Atom, at: usize) -> Result<V, Why> {
- match atom {
- Atom::Lane(v) => self.lane_bits.get(&v).copied().ok_or_else(|| {
- self.lane_fail
- .get(&v)
- .copied()
- .unwrap_or("a lane mask from a carry")
- }),
- Atom::Uniform(v) => match self.get(v)? {
- Val::Bit(x) => Ok(x),
- Val::I32(x) => {
- let z = self.s.m.i32(0);
- Ok(self.s.m.cmp(at, pred::NE, x, z))
- }
- Val::Missing(w) => Err(w),
- },
- Atom::Phi(id) => {
- if let Some(&v) = self.mask_phis.get(&id) {
- return Ok(v);
- }
- let &(b, i) = self
- .phi_at
- .get(&id)
- .ok_or("a lane-mask join without a phi")?;
- let blk = self.block[b].ok_or("an unreachable join")?;
- let v = self.s.m.phi(blk, self.s.t.i1);
- self.mask_phis.insert(id, v);
- self.pending_masks.push((v, b, i));
- Ok(v)
- }
- // shortcut: "some lane of the quad is set" is taken as true: the
- // lanes it adds only feed derivatives, and exports keep their own
- // masks. Wrong if such a section had a side effect.
- Atom::Quad(_) => Ok(self.s.m.bool(true)),
- // With console lanes, a mask held as a number is the console's:
- // the lane tests its own bit.
- Atom::Opaque(v) if self.console_lanes => {
- let hi = match self.pair_high(v) {
- Some(h) => Some(self.get(h)?),
- None => None,
- };
- let lo = self.get(v)?;
- let word = |w: Val| match w {
- Val::I32(x) => Ok(x),
- _ => Err("an opaque lane mask"),
- };
- let lo = word(lo)?;
- let hi = hi.map(word).transpose()?;
- Ok(self.lane_bit(at, lo, hi))
- }
- Atom::Constant(k) if self.console_lanes => {
- let lo = self.s.m.i32(k as u32);
- let hi = self.s.m.i32((k >> 32) as u32);
- Ok(self.lane_bit(at, lo, Some(hi)))
- }
- Atom::Opaque(_) => Err("an opaque lane mask"),
- Atom::Constant(_) => Err("a constant lane mask"),
- }
- }
- /// This lane's bit of the mask `lo`, `hi` (wave64).
- fn lane_bit(&mut self, at: usize, lo: V, hi: Option<V>) -> V {
- let lane = self.s.op(
- at,
- "waveGetLaneIndex",
- Attr::ReadOnly,
- self.s.t.i32,
- 111,
- &[],
- );
- let (k31, k32, one, z) = (
- self.s.m.i32(31),
- self.s.m.i32(32),
- self.s.m.i32(1),
- self.s.m.i32(0),
- );
- let word = match hi {
- Some(hi) => {
- let low = self.s.m.cmp(at, pred::IULT, lane, k32);
- self.s.m.select(at, low, lo, hi)
- }
- None => lo,
- };
- let sh = self.s.m.bin(at, Bin::And, lane, k31);
- let t = self.s.m.bin(at, Bin::LShr, word, sh);
- let t = self.s.m.bin(at, Bin::And, t, one);
- self.s.m.cmp(at, pred::NE, t, z)
- }
- /// The value of the high dword of the wave64 register pair whose low
- /// dword holds `lo` (none in wave32).
- fn pair_high(&self, lo: Value) -> Option<Value> {
- if !self.x.native.wave64 {
- return None;
- }
- let p = self.x.p;
- let pick = |reg: Reg| match reg {
- Reg::S(c) => Some(Reg::S(c + 1)),
- _ => None,
- };
- if let Some(&(reg, _)) = p.inputs.iter().find(|i| i.1 == lo) {
- let h = pick(reg)?;
- return p.inputs.iter().find(|i| i.0 == h).map(|i| i.1);
- }
- if let Some(&(b, i)) = self.phi_at.get(&lo) {
- let h = pick(p.blocks[b].phis[i].reg)?;
- return p.blocks[b]
- .phis
- .iter()
- .find(|q| q.reg == h)
- .map(|q| q.value);
- }
- let b = *self.def_block.get(&lo)?;
- let inst = p.blocks[b]
- .insts
- .iter()
- .find(|i| i.writes.iter().any(|w| w.1 == lo))?;
- let &(reg, _) = inst.writes.iter().find(|w| w.1 == lo)?;
- inst.write(pick(reg)?)
- }
- fn guard(&mut self, b: usize, i: usize) -> Result<Option<V>, Why> {
- match self.x.st.guards[b][i] {
- None => Ok(None),
- Some(g) => Ok(Some(self.boolean(g, self.cur)?)),
- }
- }
- // ---- results ----
- fn set(&mut self, v: Option<Value>, val: Val) {
- if let Some(v) = v {
- self.vals.insert(v, val);
- }
- }
- /// Writes VGPR `reg` (or the SGPR of a lane read), under `guard`.
- fn write(&mut self, inst: &Inst, reg: Reg, x: V, guard: Option<V>) {
- let x = match (Self::sdwa_dst(inst), reg) {
- (Some((sel, unused)), Reg::V(_)) => self.place_bits(inst, reg, x, sel, unused),
- _ => x,
- };
- self.write_placed(inst, reg, x, guard);
- }
- /// [`Self::write`] of a dword already placed (SDWA applied).
- fn write_placed(&mut self, inst: &Inst, reg: Reg, x: V, guard: Option<V>) {
- let Some(new) = inst.write(reg) else { return };
- let x = match (guard, reg) {
- (Some(g), Reg::V(_)) => {
- let old = match inst.read(reg).map(|o| self.i32_of(o)) {
- Some(Ok(o)) => o,
- // The validator refuses reads of undef.
- _ => self.s.m.i32(0),
- };
- self.s.m.select(self.cur, g, x, old)
- }
- _ => x,
- };
- self.vals.insert(new, Val::I32(x));
- }
- /// The SDWA destination selection: a byte or word, and what the other
- /// bits get (0 zeros, 1 the sign above the field, 2 the old bits).
- fn sdwa_dst(inst: &Inst) -> Option<(u8, u8)> {
- let Fields::Valu(v) = &inst.fields else {
- return None;
- };
- let s = v.sdwa?;
- (s.dst_sel < 6).then_some((s.dst_sel, s.dst_unused))
- }
- /// The low byte or word of `x` placed at `sel` in VGPR `reg`.
- fn place_bits(&mut self, inst: &Inst, reg: Reg, x: V, sel: u8, unused: u8) -> V {
- let c = self.cur;
- let (width, at) = if sel < 4 {
- (8u32, 8 * u32::from(sel))
- } else {
- (16, 16 * u32::from(sel - 4))
- };
- let left = self.s.m.i32(32 - width);
- let t = self.s.m.bin(c, Bin::Shl, x, left);
- let right = self.s.m.i32(32 - width - at);
- if unused == 1 {
- // Bits below the field shift in as zeros.
- return self.s.m.bin(c, Bin::AShr, t, right);
- }
- let field = self.s.m.bin(c, Bin::LShr, t, right);
- if unused != 2 {
- return field;
- }
- let old = match inst.read(reg).map(|o| self.i32_of(o)) {
- Some(Ok(o)) => o,
- _ => self.s.m.i32(0),
- };
- let keep = self.s.m.i32(!((((1u64 << width) - 1) as u32) << at));
- let old = self.s.m.bin(c, Bin::And, old, keep);
- self.s.m.bin(c, Bin::Or, old, field)
- }
- fn write_f(&mut self, inst: &Inst, f: V, guard: Option<V>) -> Result<(), Why> {
- let Fields::Valu(v) = &inst.fields else {
- unreachable!()
- };
- let mut f = f;
- match v.omod {
- 0 => {}
- k => {
- let factor = [1.0f32, 2.0, 4.0, 0.5][usize::from(k)];
- let c = self.s.m.f32(factor.to_bits());
- f = self.s.m.bin(self.cur, Bin::Mul, f, c);
- }
- }
- if v.clamp {
- f = self.unary(7, f);
- }
- let Some(Operand::Vector { reg, .. }) = v.dst else {
- return Err("a float result outside a VGPR");
- };
- let x = self.as_int(f);
- self.write(inst, Reg::V(reg), x, guard);
- if guard.is_none()
- && Self::sdwa_dst(inst).is_none()
- && let Some(new) = inst.write(Reg::V(reg))
- {
- self.floats.insert(new, f);
- }
- Ok(())
- }
- fn write_i(&mut self, inst: &Inst, x: V, guard: Option<V>) -> Result<(), Why> {
- let Fields::Valu(v) = &inst.fields else {
- unreachable!()
- };
- if v.clamp || v.omod != 0 {
- return Err("integer output modifiers");
- }
- match v.dst {
- Some(Operand::Vector { reg, .. }) => self.write(inst, Reg::V(reg), x, guard),
- Some(Operand::Scalar { code, .. }) => self.write(inst, Reg::S(code), x, None),
- _ => return Err("a result without a register"),
- }
- Ok(())
- }
- /// Starts code only the lanes where `guard` holds run (a store under a
- /// lane guard): returns where it started and the join block.
- fn begin_guarded(&mut self, guard: Option<V>) -> Option<(usize, usize)> {
- let g = guard?;
- let (then, join) = (self.s.m.new_block(), self.s.m.new_block());
- self.s.m.terminate(self.cur, Term::CondBr(g, then, join));
- let head = self.cur;
- self.cur = then;
- Some((head, join))
- }
- /// Ends guarded code; each (value inside, value outside) becomes a phi.
- fn end_guarded(&mut self, at: Option<(usize, usize)>, values: &[(V, V)]) -> Vec<V> {
- let Some((head, join)) = at else {
- return values.iter().map(|v| v.0).collect();
- };
- let then = self.cur;
- self.s.m.terminate(then, Term::Br(join));
- self.cur = join;
- values
- .iter()
- .map(|&(inside, outside)| {
- let ty = self.s.m.type_of(inside);
- let phi = self.s.m.phi(join, ty);
- self.s.m.set_phi(phi, vec![(inside, then), (outside, head)]);
- phi
- })
- .collect()
- }
- /// Code that depends on the lane layout: wave operations, and in compute
- /// the console's wave size (lanes are thread ids in group order there).
- /// Pixel lanes match only within a quad.
- fn cross_lane(&mut self, quad: bool) -> Result<(), Why> {
- if self.s.mesh.is_some() {
- // shortcut: no wave size for mesh shaders in SM 6.6, so host
- // lanes cannot follow the console's; emulate through group-shared
- // memory if mesh programs turn out to need these.
- return Err("a cross-lane operation in a mesh shader");
- }
- if self.s.threads.is_none() && !(quad && self.s.pixel) {
- return Err("a cross-lane operation outside compute");
- }
- debug_assert!(self.s.threads.is_none() || self.console_lanes);
- self.s.flags |= flags::WAVE_OPS;
- Ok(())
- }
- /// Component `c` of the thread id in the group. With console lanes, the
- /// thread is relabelled so that its host lane is its console lane: the
- /// flat id is the wave's first lane's, rounded down to the wave size,
- /// plus the lane index. (A bijection while each host wave holds a
- /// wave-aligned run of flat ids, as the console's waves do.)
- fn thread_id(&mut self, c: u8) -> Val {
- if !self.console_lanes {
- return self.compute_id("threadIdInGroup.i32", 95, c);
- }
- let [x, y, _] = self.s.threads.expect("compute");
- let e = self.s.entry;
- let i32t = self.s.t.i32;
- let flat = self.s.op(
- e,
- "flattenedThreadIdInGroup.i32",
- Attr::ReadNone,
- i32t,
- 96,
- &[],
- );
- let first = self.s.op(
- e,
- "waveReadLaneFirst.i32",
- Attr::NoUnwind,
- i32t,
- 118,
- &[flat],
- );
- let wave = self.s.wave_size.expect("set with console lanes");
- let mask = self.s.m.i32(!(wave - 1));
- let base = self.s.m.bin(e, Bin::And, first, mask);
- let lane = self.lane_index();
- let f = self.s.m.bin(e, Bin::Add, base, lane);
- self.s.flags |= flags::WAVE_OPS;
- let (xk, yk, xy) = (self.s.m.i32(x), self.s.m.i32(y), self.s.m.i32(x * y));
- Val::I32(match c {
- 0 => self.s.m.bin(e, Bin::URem, f, xk),
- 1 => {
- let t = self.s.m.bin(e, Bin::UDiv, f, xk);
- self.s.m.bin(e, Bin::URem, t, yk)
- }
- _ => self.s.m.bin(e, Bin::UDiv, f, xy),
- })
- }
- /// Mesh: the counts and the primitives no lane exports. The console's
- /// counts come from one lane's allocation request, which DXIL cannot
- /// express (one call, dominating every output write).
- // shortcut: the counts are the declared maxima and each lane first culls
- // its primitive (all indices 0, zero area), so primitives past the
- // program's count are culled and vertices past it unused; this costs
- // the host the unused vertex and primitive slots. Pass the program's own
- // counts if that shows in frame time.
- fn mesh_prologue(&mut self, mesh: super::shader::Mesh) -> Result<(), Why> {
- let threads = self.s.threads.expect("mesh")[0];
- if mesh.topology != 2 {
- return Err("a mesh shader not drawing triangles");
- }
- if threads > 128 || mesh.max_vertices > 256 || mesh.max_primitives > 256 {
- return Err("a mesh shader beyond the host's limits");
- }
- let e = self.s.entry;
- let void = self.s.t.void;
- let (v, p) = (
- self.s.m.i32(mesh.max_vertices),
- self.s.m.i32(mesh.max_primitives),
- );
- self.s
- .op(e, "setMeshOutputCounts", Attr::NoUnwind, void, 168, &[v, p]);
- let flat = self.mesh_thread();
- let guard = (threads > mesh.max_primitives).then(|| self.s.m.cmp(e, pred::IULT, flat, p));
- let at = self.begin_guarded(guard);
- let z = self.s.m.i32(0);
- self.s.op(
- self.cur,
- "emitIndices",
- Attr::NoUnwind,
- void,
- 169,
- &[flat, z, z, z],
- );
- self.end_guarded(at, &[]);
- Ok(())
- }
- /// Mesh: the thread's index in the group, which is its vertex and
- /// primitive index.
- fn mesh_thread(&mut self) -> V {
- if let Some(f) = self.flat {
- return f;
- }
- let e = self.s.entry;
- let f = self.s.op(
- e,
- "flattenedThreadIdInGroup.i32",
- Attr::ReadNone,
- self.s.t.i32,
- 96,
- &[],
- );
- self.flat = Some(f);
- f
- }
- /// The console wave size.
- fn wave(&self) -> u32 {
- if self.x.native.wave64 { 64 } else { 32 }
- }
- fn lane_index(&mut self) -> V {
- if self.s.mesh.is_some() {
- // The console's lane: mesh code reads no other lanes, so host
- // waves need not match the console's.
- let f = self.mesh_thread();
- let m = self.s.m.i32(self.wave() - 1);
- return self.s.m.bin(self.s.entry, Bin::And, f, m);
- }
- self.s.op(
- self.cur,
- "waveGetLaneIndex",
- Attr::ReadOnly,
- self.s.t.i32,
- 111,
- &[],
- )
- }
- /// `x` of lane `src` (in this lane's quad) through quad reads, whose
- /// lane must be a constant.
- fn quad_pick(&mut self, x: V, src: V) -> V {
- self.s.flags |= flags::WAVE_OPS;
- let c = self.cur;
- let three = self.s.m.i32(3);
- let q = self.s.m.bin(c, Bin::And, src, three);
- let mut r = x;
- for n in 0..4 {
- let kn = self.s.m.i32(n);
- let read = self.s.op(
- c,
- "quadReadLaneAt.i32",
- Attr::NoUnwind,
- self.s.t.i32,
- 122,
- &[x, kn],
- );
- let is = self.s.m.cmp(c, pred::EQ, q, kn);
- r = self.s.m.select(c, is, read, r);
- }
- r
- }
- fn read_lane(&mut self, x: V, lane: V) -> V {
- self.s.op(
- self.cur,
- "waveReadLaneAt.i32",
- Attr::NoUnwind,
- self.s.t.i32,
- 117,
- &[x, lane],
- )
- }
- /// The float value of a register value.
- fn float_of(&mut self, v: Value) -> Result<V, Why> {
- if let Some(&f) = self.floats.get(&v) {
- return Ok(f);
- }
- let x = self.i32_of(v)?;
- Ok(self.as_float(x))
- }
- /// The values of VGPRs `reg..reg + n` as `inst` reads them.
- fn vgprs(&mut self, inst: &Inst, op: &Operand, n: u8) -> Result<Vec<V>, Why> {
- let Operand::Vector { reg, .. } = *op else {
- return Err("a vector operand");
- };
- (0..n)
- .map(|k| {
- let v = inst.read(Reg::V(reg + k)).ok_or("an unread operand")?;
- self.i32_of(v)
- })
- .collect()
- }
- /// LDS (thread-group shared memory) and `ds_swizzle`.
- fn ds(&mut self, inst: &Inst, b: usize, i: usize) -> Result<(), Why> {
- let Fields::Ds {
- dst,
- addr,
- data,
- offset0,
- offset1,
- gds,
- } = &inst.fields
- else {
- unreachable!()
- };
- if *gds {
- return Err("GDS");
- }
- let name = inst.name();
- let guard = self.guard(b, i)?;
- let c = self.cur;
- let offset = u32::from(*offset0) | u32::from(*offset1) << 8;
- let dst_reg = match dst {
- Some(Operand::Vector { reg, .. }) => Some(*reg),
- _ => None,
- };
- if name == "ds_swizzle_b32" {
- // Bit 15 set: each lane of a quad picks a lane of it; else lanes
- // of each group of 32 combine their index with and/or/xor masks.
- let quad = offset & 0x8000 != 0
- || offset & 0x1C == 0x1C && offset & 0x380 == 0 && offset >> 10 & 0x1F < 4;
- // Mesh: host quads are threads 4n..4n+3 of a 1D group, as the
- // console's, so quad-local swizzles read through quad operations.
- let mesh_quad = self.s.mesh.is_some() && quad;
- if !mesh_quad {
- self.cross_lane(quad)?;
- }
- // The source sits in the address field.
- let x = self.vgprs(inst, addr.as_ref().ok_or("a swizzle source")?, 1)?[0];
- let lane = self.lane_index();
- let k = |l: &mut Self, v: u32| l.s.m.i32(v);
- let src = if offset & 0x8000 != 0 {
- let (three, one) = (k(self, 3), k(self, 1));
- let sel = self.s.m.bin(c, Bin::And, lane, three);
- let sh = self.s.m.bin(c, Bin::Shl, sel, one);
- let pattern = k(self, offset & 0xFF);
- let pick = self.s.m.bin(c, Bin::LShr, pattern, sh);
- let pick = self.s.m.bin(c, Bin::And, pick, three);
- let not3 = k(self, !3);
- let base = self.s.m.bin(c, Bin::And, lane, not3);
- self.s.m.bin(c, Bin::Or, base, pick)
- } else {
- let and = k(self, offset & 0x1F | !0x1F);
- let or = k(self, offset >> 5 & 0x1F);
- let xor = k(self, offset >> 10 & 0x1F);
- let t = self.s.m.bin(c, Bin::And, lane, and);
- let t = self.s.m.bin(c, Bin::Or, t, or);
- self.s.m.bin(c, Bin::Xor, t, xor)
- };
- let r = if mesh_quad {
- self.quad_pick(x, src)
- } else {
- self.read_lane(x, src)
- };
- let reg = dst_reg.ok_or("a swizzle result")?;
- self.write(inst, Reg::V(reg), r, guard);
- return Ok(());
- }
- if self.s.lds_dwords == 0 {
- return Err("LDS the program does not allocate");
- }
- let a = self.vgprs(inst, addr.as_ref().ok_or("an LDS address")?, 1)?[0];
- // shortcut: LDS addresses are taken as dword-aligned and in bounds
- // (out-of-bounds console accesses read 0 and drop writes).
- let dword = |l: &mut Self, bytes: u32| -> V {
- let c = l.cur;
- let o = l.s.m.i32(bytes);
- let at = l.s.m.bin(c, Bin::Add, a, o);
- let two = l.s.m.i32(2);
- let index = l.s.m.bin(c, Bin::LShr, at, two);
- let lds = l.s.lds();
- l.s.m.element(c, lds, index)
- };
- let width = |n: &str| match n.rsplit('_').next() {
- Some("b64") => 2,
- Some("b96") => 3,
- Some("b128") => 4,
- _ => 1,
- };
- // (byte offset, dwords) per element; read2/write2 scale their two
- // offsets by the element size (×64 for st64).
- let pair = |n: &str| -> Vec<(u32, u32)> {
- let w = width(n);
- let scale = 4 * w * if n.contains("st64") { 64 } else { 1 };
- vec![
- (u32::from(*offset0) * scale, w),
- (u32::from(*offset1) * scale, w),
- ]
- };
- if let Some(rest) = name.strip_prefix("ds_read") {
- let elements = if rest.starts_with('2') {
- pair(name)
- } else if matches!(rest, "_b32" | "_b64" | "_b96" | "_b128") {
- vec![(offset, width(name))]
- } else {
- return Err("an LDS read of bytes or halves");
- };
- let reg = dst_reg.ok_or("an LDS read result")?;
- let mut n = 0;
- for (off, w) in elements {
- for k in 0..w {
- let p = dword(self, off + 4 * k);
- let x = self.s.m.load(self.cur, p);
- self.write(inst, Reg::V(reg + n), x, guard);
- n += 1;
- }
- }
- return Ok(());
- }
- if let Some(rest) = name.strip_prefix("ds_write") {
- let elements = if rest.starts_with('2') {
- pair(name)
- } else if matches!(rest, "_b32" | "_b64" | "_b96" | "_b128") {
- vec![(offset, width(name))]
- } else {
- return Err("an LDS write of bytes or halves");
- };
- let mut stores = Vec::new();
- for (e, (off, w)) in elements.into_iter().enumerate() {
- let vals = self.vgprs(inst, data.get(e).ok_or("an LDS write")?, w as u8)?;
- for (k, x) in vals.into_iter().enumerate() {
- stores.push((off + 4 * k as u32, x));
- }
- }
- let at = self.begin_guarded(guard);
- for (off, x) in stores {
- let p = dword(self, off);
- self.s.m.store(self.cur, p, x);
- }
- self.end_guarded(at, &[]);
- return Ok(());
- }
- // Atomics: the old value returns with `_rtn`.
- let op = name
- .strip_prefix("ds_")
- .map(|n| n.replace("_rtn", ""))
- .ok_or("an LDS operation")?;
- use super::bitcode::Rmw;
- let rmw = match op.as_str() {
- "add_u32" => Some(Rmw::Add),
- "sub_u32" => Some(Rmw::Sub),
- "and_b32" => Some(Rmw::And),
- "or_b32" => Some(Rmw::Or),
- "xor_b32" => Some(Rmw::Xor),
- "min_i32" => Some(Rmw::Min),
- "max_i32" => Some(Rmw::Max),
- "min_u32" => Some(Rmw::UMin),
- "max_u32" => Some(Rmw::UMax),
- "wrxchg_b32" => Some(Rmw::Xchg),
- "cmpst_b32" => None,
- _ => return Err("an LDS operation"),
- };
- let v0 = self.vgprs(inst, data.first().ok_or("an LDS atomic")?, 1)?[0];
- let v1 = match data.get(1) {
- Some(d) => Some(self.vgprs(inst, d, 1)?[0]),
- None => None,
- };
- let old_reg = dst_reg.and_then(|r| inst.read(Reg::V(r)).map(|v| (r, v)));
- let outside = match old_reg {
- Some((_, v)) => self.i32_of(v).unwrap_or(self.s.m.i32(0)),
- None => self.s.m.i32(0),
- };
- let p = dword(self, offset);
- let at = self.begin_guarded(guard);
- let r = match rmw {
- Some(op) => self.s.m.atomic(self.cur, op, p, v0),
- // Compare with data0, store data1.
- None => self
- .s
- .m
- .cmpxchg(self.cur, p, v0, v1.ok_or("an LDS compare")?),
- };
- let r = self.end_guarded(at, &[(r, outside)])[0];
- if let Some(reg) = dst_reg {
- self.write(inst, Reg::V(reg), r, None);
- }
- Ok(())
- }
- // ---- instructions ----
- fn inst(&mut self, b: usize, i: usize) -> Result<(), Stop> {
- let inst: &'a Inst = &self.x.p.blocks[b].insts[i];
- let offset = inst.machine.offset;
- let name = inst.name();
- self.mask_ok = match self.mask_data {
- MaskData::Console => true,
- MaskData::Host => {
- name.starts_with("s_bcnt1_i32_")
- || name.starts_with("s_ff1_i32_")
- || name.starts_with("v_mbcnt_")
- || matches!(
- name,
- "s_cmp_eq_u32" | "s_cmp_lg_u32" | "s_cmp_eq_u64" | "s_cmp_lg_u64"
- )
- }
- MaskData::None => false,
- };
- let effect = matches!(inst.fields, Fields::Exp { .. })
- || matches!(inst.fields, Fields::Flat { .. }) && !name.starts_with("global_load_")
- || name.contains("store")
- || name.contains("atomic")
- || name == "s_barrier"
- || matches!(inst.fields, Fields::Ds { .. })
- && !(name.starts_with("ds_read")
- || name.starts_with("ds_swizzle")
- || name.contains("permute"));
- let r = match &inst.fields {
- Fields::Sop { .. } => self.sop(inst),
- Fields::Sopp { .. } if name == "s_barrier" && self.no_barrier.contains(&b) => Ok(()),
- Fields::Sopp { .. } if name == "s_barrier" => {
- // Thread-group sync with a shared-memory fence, and a UAV
- // fence when the program writes UAVs (the waits before an
- // s_barrier make those writes visible on the console).
- let mode = if self.writes_uavs { 1 | 2 | 8 } else { 1 | 8 };
- let mode = self.s.m.i32(mode);
- let void = self.s.t.void;
- self.s
- .op(self.cur, "barrier", Attr::NoDuplicate, void, 80, &[mode]);
- Ok(())
- }
- Fields::Sopp { .. } => Ok(()),
- Fields::Smem { .. } => self.smem(inst, (b, i)),
- Fields::Valu(_) if name.starts_with("v_interp_") => self.interp(inst, (b, i)),
- Fields::Valu(_) => self.valu(inst, b, i),
- Fields::Vintrp { .. } => self.interp(inst, (b, i)),
- Fields::Image { .. } => self.image(inst, b, i),
- Fields::Buffer { .. } => self.buffer(inst, b, i),
- Fields::Exp { .. } => self.export(b, i),
- Fields::Ds { .. } => self.ds(inst, b, i),
- Fields::Flat { .. } if name.starts_with("global_") => self.flat(inst, b, i),
- Fields::Flat { .. } => Err("flat memory"),
- };
- match r {
- Ok(()) => Ok(()),
- Err(why) if effect => Err(unsupported(offset, why)),
- Err(why) => {
- for &(_, v) in &inst.writes {
- self.vals.insert(v, Val::Missing(why));
- }
- Ok(())
- }
- }
- }
- fn is_mask(&self, v: Option<Value>) -> bool {
- v.is_some_and(|v| self.x.st.masks.contains_key(&v))
- }
- fn sop(&mut self, inst: &Inst) -> Result<(), Why> {
- let Fields::Sop { dst, src, imm } = &inst.fields else {
- unreachable!()
- };
- let name = inst.name();
- let scc = inst.write(Reg::Scc);
- let dst_code = match dst {
- Some(Operand::Scalar { code, .. }) => Some(*code),
- _ => None,
- };
- let lo = dst_code.and_then(|c| inst.write(Reg::S(c)));
- if inst.write(Reg::S(EXEC)).is_some() || self.is_mask(lo) {
- return Err(LANE_MASK);
- }
- if matches!(
- name,
- "s_setreg_imm32_b32" | "s_setreg_b32" | "s_swappc_b64" | "s_version"
- ) || name.starts_with("s_waitcnt")
- {
- // shortcut: the float MODE register is not modelled; s_swappc_b64 is the
- // vertex fetch call, whose results arrive as inputs.
- return Err("a register the emitter does not model");
- }
- let c = self.cur;
- let src32 = |l: &mut Self, k: usize| -> Result<V, Why> {
- // SOPK arithmetic reads its destination as the first source.
- if k == 0 && matches!(name, "s_addk_i32" | "s_mulk_i32") {
- let d = dst.as_ref().ok_or("a missing operand")?;
- return l.operand(inst, d);
- }
- let op = src.get(k).ok_or("a missing operand")?;
- l.operand(inst, op)
- };
- let sext16 = |l: &mut Self| l.s.m.i32(i32::from(imm.unwrap_or(0) as i16) as u32);
- // Dword `h` of 64-bit source `k` (inline constants sign-extend).
- let half = |l: &mut Self, k: usize, h: u8| -> Result<V, Why> {
- match src.get(k) {
- Some(&Operand::Scalar { code, .. }) if code != NULL => {
- let v = inst.read(Reg::S(code + h)).ok_or("an unread operand")?;
- l.i32_of(v)
- }
- Some(op) if h == 0 => l.operand(inst, op),
- Some(Operand::Const(k)) => {
- let x = crate::resource::inline(*k).ok_or("a special operand")?;
- Ok(l.s.m.i32(if (x as i32) < 0 { u32::MAX } else { 0 }))
- }
- _ => Ok(l.s.m.i32(0)),
- }
- };
- if let Some(op) = name.strip_prefix("s_bitcmp") {
- // SCC is whether bit src1 (mod 32, or mod 64 for the 64-bit form)
- // of src0 is clear (bitcmp0) or set (bitcmp1).
- let (bit, k31) = (src32(self, 1)?, self.s.m.i32(31));
- let idx = self.s.m.bin(c, Bin::And, bit, k31);
- let word = if op.ends_with("_b64") {
- let (a, b) = (half(self, 0, 0)?, half(self, 0, 1)?);
- let k32 = self.s.m.i32(32);
- let upper = self.s.m.bin(c, Bin::And, bit, k32);
- let z = self.s.m.i32(0);
- let upper = self.s.m.cmp(c, pred::NE, upper, z);
- self.s.m.select(c, upper, b, a)
- } else {
- src32(self, 0)?
- };
- let shifted = self.s.m.bin(c, Bin::LShr, word, idx);
- let one = self.s.m.i32(1);
- let tested = self.s.m.bin(c, Bin::And, shifted, one);
- let want = self.s.m.i32(u32::from(op.starts_with('1')));
- let r = self.s.m.cmp(c, pred::EQ, tested, want);
- self.set(scc, Val::Bit(r));
- return Ok(());
- }
- if let Some(op) = name.strip_prefix("s_cmp_").filter(|o| o.ends_with("_u64")) {
- // eq and lg only: both dwords.
- let mut r = None;
- for h in 0..2 {
- let (a, b) = (half(self, 0, h)?, half(self, 1, h)?);
- let e = self.s.m.cmp(c, pred::EQ, a, b);
- r = Some(match r {
- Some(x) => self.s.m.bin(c, Bin::And, x, e),
- None => e,
- });
- }
- let mut r = r.expect("two dwords");
- if op.starts_with("lg") {
- let t = self.s.m.bool(true);
- r = self.s.m.bin(c, Bin::Xor, r, t);
- }
- self.set(scc, Val::Bit(r));
- return Ok(());
- }
- if let Some(op) = name.strip_prefix("s_cmp_").or(name.strip_prefix("s_cmpk_")) {
- let (a, b) = if name.starts_with("s_cmpk_") {
- let b = if op.ends_with("_i32") {
- sext16(self)
- } else {
- self.s.m.i32(u32::from(imm.unwrap_or(0)))
- };
- (src32(self, 0)?, b)
- } else {
- if op.ends_with("64") {
- return Err("a 64-bit scalar compare");
- }
- (src32(self, 0)?, src32(self, 1)?)
- };
- let signed = op.ends_with("_i32");
- let p = match &op[..2] {
- "eq" => pred::EQ,
- "lg" => pred::NE,
- "gt" if signed => pred::SGT,
- "ge" if signed => pred::SGE,
- "lt" if signed => pred::SLT,
- "le" if signed => pred::SLE,
- "gt" => pred::IUGT,
- "ge" => pred::IUGE,
- "lt" => pred::IULT,
- "le" => pred::IULE,
- _ => return Err("a scalar compare"),
- };
- let r = self.s.m.cmp(c, p, a, b);
- self.set(scc, Val::Bit(r));
- return Ok(());
- }
- let wide = name.ends_with("_b64") || name.ends_with("_u64") || name.ends_with("_i64");
- if wide {
- let hi = dst_code.and_then(|c| inst.write(Reg::S(c + 1)));
- let nonzero = |l: &mut Self, a: V, b: V| {
- let o = l.s.m.bin(c, Bin::Or, a, b);
- let z = l.s.m.i32(0);
- l.s.m.cmp(c, pred::NE, o, z)
- };
- return match name {
- "s_bcnt1_i32_b64" => {
- let (a, b) = (half(self, 0, 0)?, half(self, 0, 1)?);
- let i32t = self.s.t.i32;
- let na = self
- .s
- .op(c, "unaryBits.i32", Attr::ReadNone, i32t, 31, &[a]);
- let nb = self
- .s
- .op(c, "unaryBits.i32", Attr::ReadNone, i32t, 31, &[b]);
- let r = self.s.m.bin(c, Bin::Add, na, nb);
- self.set(lo, Val::I32(r));
- let z = self.s.m.i32(0);
- let s = self.s.m.cmp(c, pred::NE, r, z);
- self.set(scc, Val::Bit(s));
- Ok(())
- }
- "s_ff1_i32_b64" => {
- // The lowest set bit; -1 for none (as DXIL's first bit low).
- let (a, b) = (half(self, 0, 0)?, half(self, 0, 1)?);
- let i32t = self.s.t.i32;
- let fa = self
- .s
- .op(c, "unaryBits.i32", Attr::ReadNone, i32t, 32, &[a]);
- let fb = self
- .s
- .op(c, "unaryBits.i32", Attr::ReadNone, i32t, 32, &[b]);
- let (z, k32) = (self.s.m.i32(0), self.s.m.i32(32));
- let fb32 = self.s.m.bin(c, Bin::Add, fb, k32);
- let none = self.s.m.cmp(c, pred::EQ, b, z);
- let fb = self.s.m.select(c, none, fb, fb32);
- let low = self.s.m.cmp(c, pred::NE, a, z);
- let r = self.s.m.select(c, low, fa, fb);
- self.set(lo, Val::I32(r));
- Ok(())
- }
- "s_and_b64" | "s_or_b64" | "s_xor_b64" | "s_andn2_b64" | "s_orn2_b64"
- | "s_nand_b64" | "s_nor_b64" | "s_xnor_b64" | "s_not_b64" => {
- let ones = self.s.m.i32(u32::MAX);
- let mut r = Vec::new();
- for h in 0..2u8 {
- let a = half(self, 0, h)?;
- let x = if name == "s_not_b64" {
- self.s.m.bin(c, Bin::Xor, a, ones)
- } else {
- let mut b = half(self, 1, h)?;
- if name.contains("n2") {
- b = self.s.m.bin(c, Bin::Xor, b, ones);
- }
- let op = match &name[2..5] {
- "and" | "nan" => Bin::And,
- "or_" | "orn" | "nor" => Bin::Or,
- _ => Bin::Xor,
- };
- let x = self.s.m.bin(c, op, a, b);
- if matches!(name, "s_nand_b64" | "s_nor_b64" | "s_xnor_b64") {
- self.s.m.bin(c, Bin::Xor, x, ones)
- } else {
- x
- }
- };
- r.push(x);
- }
- self.set(lo, Val::I32(r[0]));
- self.set(hi, Val::I32(r[1]));
- let s = nonzero(self, r[0], r[1]);
- self.set(scc, Val::Bit(s));
- Ok(())
- }
- "s_lshl_b64" | "s_lshr_b64" | "s_ashr_i64" => {
- let (a, b) = (half(self, 0, 0)?, half(self, 0, 1)?);
- let n = src32(self, 1)?;
- let i64t = self.s.m.int(64);
- self.s.flags |= flags::INT64;
- let a = self.s.m.cast(c, Cast::ZExt, a, i64t);
- let b = self.s.m.cast(c, Cast::ZExt, b, i64t);
- let k32 = self.s.m.int_const(i64t, 32);
- let b = self.s.m.bin(c, Bin::Shl, b, k32);
- let x = self.s.m.bin(c, Bin::Or, a, b);
- let k63 = self.s.m.i32(63);
- let n = self.s.m.bin(c, Bin::And, n, k63);
- let n = self.s.m.cast(c, Cast::ZExt, n, i64t);
- let op = match name {
- "s_lshl_b64" => Bin::Shl,
- "s_lshr_b64" => Bin::LShr,
- _ => Bin::AShr,
- };
- let r = self.s.m.bin(c, op, x, n);
- let rl = self.s.m.cast(c, Cast::Trunc, r, self.s.t.i32);
- let rh = self.s.m.bin(c, Bin::LShr, r, k32);
- let rh = self.s.m.cast(c, Cast::Trunc, rh, self.s.t.i32);
- self.set(lo, Val::I32(rl));
- self.set(hi, Val::I32(rh));
- let s = nonzero(self, rl, rh);
- self.set(scc, Val::Bit(s));
- Ok(())
- }
- "s_mov_b64" => {
- let (a, b) = (half(self, 0, 0)?, half(self, 0, 1)?);
- self.set(lo, Val::I32(a));
- self.set(hi, Val::I32(b));
- Ok(())
- }
- "s_cselect_b64" => {
- let cond = self.scc_in(inst)?;
- for (h, v) in [(0, lo), (1, hi)] {
- let (a, b) = (half(self, 0, h)?, half(self, 1, h)?);
- let r = self.s.m.select(c, cond, a, b);
- self.set(v, Val::I32(r));
- }
- Ok(())
- }
- _ => Err(name),
- };
- }
- let m = &mut self.s.m;
- let _ = m;
- let (r, carry): (V, Option<V>) = match name {
- "s_mov_b32" => (src32(self, 0)?, None),
- "s_movk_i32" => (sext16(self), None),
- "s_cselect_b32" => {
- let cond = self.scc_in(inst)?;
- let (a, b) = (src32(self, 0)?, src32(self, 1)?);
- (self.s.m.select(c, cond, a, b), None)
- }
- "s_brev_b32" => {
- let a = src32(self, 0)?;
- let r = self
- .s
- .op(c, "unary.i32", Attr::ReadNone, self.s.t.i32, 30, &[a]);
- (r, None)
- }
- "s_not_b32" => {
- let a = src32(self, 0)?;
- let ones = self.s.m.i32(u32::MAX);
- (self.s.m.bin(c, Bin::Xor, a, ones), None)
- }
- "s_add_u32" | "s_add_i32" | "s_addk_i32" => {
- let a = src32(self, 0)?;
- let b = if name == "s_addk_i32" {
- sext16(self)
- } else {
- src32(self, 1)?
- };
- let r = self.s.m.bin(c, Bin::Add, a, b);
- let carry = if name == "s_add_u32" {
- Some(self.s.m.cmp(c, pred::IULT, r, a))
- } else {
- None
- };
- (r, carry)
- }
- "s_sub_u32" | "s_sub_i32" => {
- let (a, b) = (src32(self, 0)?, src32(self, 1)?);
- let r = self.s.m.bin(c, Bin::Sub, a, b);
- let carry = (name == "s_sub_u32").then(|| self.s.m.cmp(c, pred::IULT, a, b));
- (r, carry)
- }
- "s_addc_u32" | "s_subb_u32" => {
- let (a, b) = (src32(self, 0)?, src32(self, 1)?);
- let cin = self.scc_in(inst)?;
- let (r, carry) = self.add_sub_carry(c, name == "s_addc_u32", a, b, Some(cin));
- (r, Some(carry))
- }
- "s_mul_i32" | "s_mulk_i32" => {
- let a = src32(self, 0)?;
- let b = if name == "s_mulk_i32" {
- sext16(self)
- } else {
- src32(self, 1)?
- };
- (self.s.m.bin(c, Bin::Mul, a, b), None)
- }
- "s_mul_hi_u32" | "s_mul_hi_i32" => {
- let (a, b) = (src32(self, 0)?, src32(self, 1)?);
- (self.mul_hi(a, b, name.ends_with("i32")), None)
- }
- "s_flbit_i32_b32" | "s_flbit_i32" => {
- // The first bit from the top (differing from the sign for
- // the signed form), -1 for none: DXIL's first-bit-high.
- let a = src32(self, 0)?;
- let op = if name == "s_flbit_i32_b32" { 33 } else { 34 };
- let r = self
- .s
- .op(c, "unaryBits.i32", Attr::ReadNone, self.s.t.i32, op, &[a]);
- (r, None)
- }
- "s_ff1_i32_b32" | "s_bcnt1_i32_b32" => {
- // First set bit (-1 for zero, as DXIL's), or the bit count.
- let a = src32(self, 0)?;
- let op = if name == "s_ff1_i32_b32" { 32 } else { 31 };
- let r = self
- .s
- .op(c, "unaryBits.i32", Attr::ReadNone, self.s.t.i32, op, &[a]);
- (r, None)
- }
- "s_bfe_u32" | "s_bfe_i32" => {
- // src1: offset in bits 0-4, width in bits 16-22.
- let (a, b) = (src32(self, 0)?, src32(self, 1)?);
- let (k16, k127) = (self.s.m.i32(16), self.s.m.i32(0x7F));
- let w = self.s.m.bin(c, Bin::LShr, b, k16);
- let w = self.s.m.bin(c, Bin::And, w, k127);
- (self.bfe(a, b, w, name == "s_bfe_i32"), None)
- }
- "s_and_b32" | "s_or_b32" | "s_xor_b32" | "s_andn2_b32" | "s_orn2_b32"
- | "s_nand_b32" | "s_nor_b32" | "s_xnor_b32" => {
- let (a, mut b) = (src32(self, 0)?, src32(self, 1)?);
- let ones = self.s.m.i32(u32::MAX);
- if name.contains("n2") {
- b = self.s.m.bin(c, Bin::Xor, b, ones);
- }
- let op = match &name[2..5] {
- "and" | "nan" => Bin::And,
- "or_" | "orn" | "nor" => Bin::Or,
- _ => Bin::Xor,
- };
- let mut r = self.s.m.bin(c, op, a, b);
- if matches!(name, "s_nand_b32" | "s_nor_b32" | "s_xnor_b32") {
- r = self.s.m.bin(c, Bin::Xor, r, ones);
- }
- (r, None)
- }
- "s_lshl_b32" | "s_lshr_b32" | "s_ashr_i32" => {
- let (a, b) = (src32(self, 0)?, src32(self, 1)?);
- let k31 = self.s.m.i32(31);
- let b = self.s.m.bin(c, Bin::And, b, k31);
- let op = match name {
- "s_lshl_b32" => Bin::Shl,
- "s_lshr_b32" => Bin::LShr,
- _ => Bin::AShr,
- };
- (self.s.m.bin(c, op, a, b), None)
- }
- "s_lshl1_add_u32" | "s_lshl2_add_u32" | "s_lshl3_add_u32" | "s_lshl4_add_u32" => {
- let (a, b) = (src32(self, 0)?, src32(self, 1)?);
- let k = self.s.m.i32(u32::from(name.as_bytes()[6] - b'0'));
- let t = self.s.m.bin(c, Bin::Shl, a, k);
- (self.s.m.bin(c, Bin::Add, t, b), None)
- }
- "s_min_u32" | "s_max_u32" | "s_min_i32" | "s_max_i32" => {
- let (a, b) = (src32(self, 0)?, src32(self, 1)?);
- let p = match name {
- "s_min_u32" => pred::IULT,
- "s_max_u32" => pred::IUGT,
- "s_min_i32" => pred::SLT,
- _ => pred::SGT,
- };
- let t = self.s.m.cmp(c, p, a, b);
- (self.s.m.select(c, t, a, b), None)
- }
- "s_abs_i32" => {
- let a = src32(self, 0)?;
- let z = self.s.m.i32(0);
- let neg = self.s.m.cmp(c, pred::SLT, a, z);
- let n = self.s.m.bin(c, Bin::Sub, z, a);
- (self.s.m.select(c, neg, n, a), None)
- }
- "s_bitset0_b32" | "s_bitset1_b32" => {
- // Sets (clears) bit src[4:0] of the destination it reads.
- let old = self.operand(inst, dst.as_ref().ok_or("a missing operand")?)?;
- let a = src32(self, 0)?;
- let (k31, one) = (self.s.m.i32(31), self.s.m.i32(1));
- let a = self.s.m.bin(c, Bin::And, a, k31);
- let bit = self.s.m.bin(c, Bin::Shl, one, a);
- if name == "s_bitset1_b32" {
- (self.s.m.bin(c, Bin::Or, old, bit), None)
- } else {
- let ones = self.s.m.i32(u32::MAX);
- let nb = self.s.m.bin(c, Bin::Xor, bit, ones);
- (self.s.m.bin(c, Bin::And, old, nb), None)
- }
- }
- "s_bfm_b32" => {
- let (a, b) = (src32(self, 0)?, src32(self, 1)?);
- let k31 = self.s.m.i32(31);
- let one = self.s.m.i32(1);
- let a = self.s.m.bin(c, Bin::And, a, k31);
- let b = self.s.m.bin(c, Bin::And, b, k31);
- let m = self.s.m.bin(c, Bin::Shl, one, a);
- let m = self.s.m.bin(c, Bin::Sub, m, one);
- (self.s.m.bin(c, Bin::Shl, m, b), None)
- }
- _ => return Err(name),
- };
- self.set(lo, Val::I32(r));
- if let Some(scc) = scc {
- let val = match carry {
- Some(k) => Val::Bit(k),
- None if matches!(
- name,
- "s_mov_b32"
- | "s_movk_i32"
- | "s_cselect_b32"
- | "s_brev_b32"
- | "s_mul_i32"
- | "s_mulk_i32"
- ) =>
- {
- return Ok(());
- }
- None if name.starts_with("s_add") || name.starts_with("s_sub") => {
- Val::Missing("the carry of a scalar operation")
- }
- None => {
- let z = self.s.m.i32(0);
- Val::Bit(self.s.m.cmp(c, pred::NE, r, z))
- }
- };
- self.vals.insert(scc, val);
- }
- Ok(())
- }
- /// `a + b (+ cin)` or `a - b (- cin)` with the carry (borrow) out, as the
- /// scalar and vector add-with-carry instructions set it.
- fn add_sub_carry(&mut self, c: usize, add: bool, a: V, b: V, cin: Option<V>) -> (V, V) {
- let op = if add { Bin::Add } else { Bin::Sub };
- let mut r = self.s.m.bin(c, op, a, b);
- // Without a carry in: a + b wraps below a; a - b borrows when a < b.
- let mut carry = if add {
- self.s.m.cmp(c, pred::IULT, r, a)
- } else {
- self.s.m.cmp(c, pred::IULT, a, b)
- };
- if let Some(cin) = cin {
- let one = self.s.m.cast(c, Cast::ZExt, cin, self.s.t.i32);
- let t = r;
- r = self.s.m.bin(c, op, t, one);
- // The carry in carries on when the partial result is all ones
- // (add) or zero (sub).
- let edge = self.s.m.i32(if add { u32::MAX } else { 0 });
- let at_edge = self.s.m.cmp(c, pred::EQ, t, edge);
- let more = self.s.m.bin(c, Bin::And, cin, at_edge);
- carry = self.s.m.bin(c, Bin::Or, carry, more);
- }
- (r, carry)
- }
- fn scc_in(&mut self, inst: &Inst) -> Result<V, Why> {
- let v = inst.read(Reg::Scc).ok_or("an unread SCC")?;
- match self.get(v)? {
- Val::Bit(x) => Ok(x),
- _ => Err("SCC without a value"),
- }
- }
- fn smem(&mut self, inst: &Inst, site: Site) -> Result<(), Why> {
- let Fields::Smem {
- data,
- offset,
- soffset,
- ..
- } = &inst.fields
- else {
- unreachable!()
- };
- let name = inst.name();
- if let Some(a) = self.scalar.get(&site).copied().filter(|a| a.param == CODE) {
- return self.code_smem(inst, a);
- }
- let Some(rest) = name.strip_prefix("s_buffer_load_dword") else {
- return self.descriptor_data(inst, site);
- };
- let count = match rest {
- "" => 1,
- "x2" => 2,
- "x4" => 4,
- "x8" => 8,
- "x16" => 16,
- _ => return Err("a scalar buffer load"),
- };
- let Some(Operand::Scalar { code, .. }) = *data else {
- return Err("a scalar buffer load");
- };
- let src = self
- .descriptors
- .get(&(site, false))
- .copied()
- .ok_or("an untracked descriptor")?;
- let kind = self.used_kind(src);
- let c = self.cur;
- let (base, extra) = self.built_offset(src)?;
- let offset = &((*offset as u32).wrapping_add(base) as i32);
- let mut addr = self.s.m.i32(*offset as u32);
- if let Some(x) = extra {
- addr = self.s.m.bin(c, Bin::Add, addr, x);
- }
- if !matches!(soffset, Operand::Scalar { code: NULL, .. }) {
- let so = self.operand(inst, soffset)?;
- addr = self.s.m.bin(c, Bin::Add, addr, so);
- }
- if matches!(src, Source::Built { address, .. } if address.param == CODE) {
- // A descriptor built around a table in the code.
- for k in 0..count {
- let off = self.s.m.i32(4 * k);
- let a = self.s.m.bin(c, Bin::Add, addr, off);
- let x = self.code_dword(a)?;
- self.write(inst, Reg::S(code + k as u8), x, None);
- }
- return Ok(());
- }
- if matches!(kind, Some(u) if u.kind == ResourceType::Srv) {
- let h = self.handle(site, src, false, (11, 0))?;
- for k in 0..count {
- let off = self.s.m.i32(4 * k);
- let a = self.s.m.bin(c, Bin::Add, addr, off);
- let mask = self.s.m.i8(1);
- let (u, al) = (self.s.m.undef(self.s.t.i32), self.s.m.i32(4));
- let r = self.s.op(
- c,
- "rawBufferLoad.f32",
- Attr::ReadOnly,
- self.s.t.res_ret,
- 139,
- &[h, a, u, mask, al],
- );
- let x = self.s.m.extract(c, r, 0);
- let x = self.as_int(x);
- self.write(inst, Reg::S(code + k as u8), x, None);
- }
- return Ok(());
- }
- let h = self.handle(site, src, false, (13, 0x1_0000))?;
- let constant = match addr {
- V::Const(_)
- if matches!(soffset, Operand::Scalar { code: NULL, .. }) && extra.is_none() =>
- {
- Some(*offset as u32)
- }
- _ => None,
- };
- let mut rows: HashMap<u32, V> = HashMap::new();
- for k in 0..count {
- let x = match constant {
- Some(base) => {
- let byte = base + 4 * k;
- let row = match rows.get(&(byte >> 4)) {
- Some(&r) => r,
- None => {
- let ri = self.s.m.i32(byte >> 4);
- let r = self.s.op(
- c,
- "cbufferLoadLegacy.i32",
- Attr::ReadOnly,
- self.s.t.cbuf_ret,
- 59,
- &[h, ri],
- );
- rows.insert(byte >> 4, r);
- r
- }
- };
- self.s.m.extract(c, row, (byte >> 2) & 3)
- }
- None => {
- let off = self.s.m.i32(4 * k);
- let a = self.s.m.bin(c, Bin::Add, addr, off);
- self.cbuffer_dword(h, a)
- }
- };
- self.write(inst, Reg::S(code + k as u8), x, None);
- }
- Ok(())
- }
- /// A descriptor load (`s_load_dword*` from a table or heap) whose dwords
- /// the code also reads as numbers: the console compiler computes sizes
- /// from descriptor bits (an image's width, height, depth, levels; a
- /// buffer's record count) instead of querying them. Those fields are
- /// rebuilt from the host view's dimensions in the GFX10 layout (image
- /// dword 1 bits 30-31 and dword 2 bits 0-13 width - 1, dword 2 bits
- /// 14-29 height - 1, dword 3 identity swizzle, last level at 16, type at
- /// 28, dword 4 depth or layers - 1; buffer dword 2 the record count).
- // shortcut: the base level reads 0 (the host view starts at its own
- // level 0, so sizes the code shifts by it come out right) and the other
- // dwords (addresses, formats, tiling) have no host value: reading them
- // stops the program.
- fn descriptor_data(&mut self, inst: &Inst, site: Site) -> Result<(), Why> {
- const OTHER: Why = "a descriptor's address or format read as data";
- let Fields::Smem {
- data: Some(Operand::Scalar { code, .. }),
- ..
- } = inst.fields
- else {
- return Err("a scalar memory read as data");
- };
- let count = match inst.name().strip_prefix("s_load_dword") {
- Some("") => 1u32,
- Some("x2") => 2,
- Some("x4") => 4,
- Some("x8") => 8,
- Some("x16") => 16,
- _ => return Err("a scalar memory read as data"),
- };
- // How the program uses the descriptor tells the view's kind.
- let use_at = self
- .x
- .res
- .descriptors
- .iter()
- .filter(|d| d.role == Role::Resource)
- .find_map(|d| {
- d.source.leaves().into_iter().find_map(|l| match *l {
- Source::Loaded { load, first } if load == site => Some((d.site, first)),
- _ => None,
- })
- });
- let Some((at, first)) = use_at else {
- return Err("a descriptor read only as data");
- };
- let src = Source::Loaded { load: site, first };
- let used = self.used_kind(&src);
- let ui: &Inst = &self.x.p.blocks[at.0].insts[at.1];
- let un = ui.name();
- let uav = un.contains("store")
- || un.contains("atomic")
- || used.is_some_and(|u| u.kind == ResourceType::Uav);
- let u = if uav { 0x1000 } else { 0 };
- let (props, image) = match &ui.fields {
- Fields::Image { dim, .. } => {
- let kind = match dim {
- 0 => 1,
- 1 => 2,
- 2 => 4,
- 4 => 6,
- 5 => 7,
- _ => return Err("an image dimension (cube or MSAA)"),
- };
- ((kind | u, 0x409), Some(kind))
- }
- Fields::Buffer { idxen, format, .. } => {
- let stride = used.map_or(0, |u| u.stride);
- let props = if un.starts_with("buffer_load_format")
- || un.starts_with("buffer_store_format")
- {
- (10 | u, 0x409)
- } else if *idxen && format.is_none() && stride >= 4 && stride.is_multiple_of(4) {
- (12 | u, stride)
- } else {
- (11 | u, 0)
- };
- if used.is_some_and(|u| u.kind == ResourceType::Cbv) {
- return Err("a constant buffer's descriptor read as data");
- }
- (props, None)
- }
- _ => return Err("a scalar memory read as data"),
- };
- let h = self.handle(site, &src, false, props)?;
- let c = self.cur;
- let i32t = self.s.t.i32;
- let dims = self.s.m.named_struct("dx.types.Dimensions", vec![i32t; 4]);
- let mip = if image.is_some() && !uav {
- self.s.m.i32(0)
- } else {
- self.s.m.undef(i32t)
- };
- let d = self
- .s
- .op(c, "getDimensions", Attr::ReadOnly, dims, 72, &[h, mip]);
- let field = |l: &mut Self, k: u32| l.s.m.extract(c, d, k);
- let words: Vec<(u32, V)> = match image {
- Some(kind) => {
- let one = self.s.m.i32(1);
- let w = field(self, 0);
- let (height, depth) = match kind {
- 1 => (one, one),
- 2 => (field(self, 1), one),
- 6 => (one, field(self, 1)),
- _ => (field(self, 1), field(self, 2)),
- };
- let levels = if uav { one } else { field(self, 3) };
- let less = |l: &mut Self, x: V| l.s.m.bin(c, Bin::Sub, x, one);
- let (w, height, depth, levels) = (
- less(self, w),
- less(self, height),
- less(self, depth),
- less(self, levels),
- );
- let k = |l: &mut Self, v: u32| l.s.m.i32(v);
- let (k30, k2, k14, k16, mask) = (
- k(self, 30),
- k(self, 2),
- k(self, 14),
- k(self, 16),
- k(self, 0x3FFF),
- );
- let d1 = self.s.m.bin(c, Bin::Shl, w, k30);
- let hi = self.s.m.bin(c, Bin::LShr, w, k2);
- let hi = self.s.m.bin(c, Bin::And, hi, mask);
- let ht = self.s.m.bin(c, Bin::Shl, height, k14);
- let d2 = self.s.m.bin(c, Bin::Or, hi, ht);
- let ty = [0, 8, 9, 0, 10, 0, 12, 13][kind as usize];
- let base = k(self, 0xFAC | ty << 28);
- let last = self.s.m.bin(c, Bin::Shl, levels, k16);
- let d3 = self.s.m.bin(c, Bin::Or, base, last);
- vec![(1, d1), (2, d2), (3, d3), (4, depth)]
- }
- None => vec![(2, field(self, 0))],
- };
- for k in 0..count {
- let rel = k.wrapping_sub(u32::from(first));
- match words.iter().find(|w| w.0 == rel) {
- Some(&(_, x)) => self.write(inst, Reg::S(code + k as u8), x, None),
- None => {
- if let Some(v) = inst.write(Reg::S(code + k as u8)) {
- self.vals.insert(v, Val::Missing(OTHER));
- }
- }
- }
- }
- Ok(())
- }
- /// `s_load_dword*` from the program's constant table.
- fn code_smem(&mut self, inst: &Inst, a: Address) -> Result<(), Why> {
- let Fields::Smem {
- data: Some(Operand::Scalar { code, .. }),
- ..
- } = inst.fields
- else {
- return Err("a scalar load");
- };
- let count = match inst.name().strip_prefix("s_load_dword") {
- Some("") => 1u32,
- Some("x2") => 2,
- Some("x4") => 4,
- Some("x8") => 8,
- Some("x16") => 16,
- _ => return Err("a scalar load from the code"),
- };
- let c = self.cur;
- let mut addr = self.s.m.i32(a.offset);
- if let Some(d) = a.dynamic {
- let d = self.i32_of(d)?;
- addr = self.s.m.bin(c, Bin::Add, addr, d);
- }
- for k in 0..count {
- let off = self.s.m.i32(4 * k);
- let at = self.s.m.bin(c, Bin::Add, addr, off);
- let x = self.code_dword(at)?;
- self.write(inst, Reg::S(code + k as u8), x, None);
- }
- Ok(())
- }
- /// The dword at a computed byte address of a constant buffer.
- fn cbuffer_dword(&mut self, h: V, a: V) -> V {
- let c = self.cur;
- let four = self.s.m.i32(4);
- let ri = self.s.m.bin(c, Bin::LShr, a, four);
- let r = self.s.op(
- c,
- "cbufferLoadLegacy.i32",
- Attr::ReadOnly,
- self.s.t.cbuf_ret,
- 59,
- &[h, ri],
- );
- let two = self.s.m.i32(2);
- let three = self.s.m.i32(3);
- let comp = self.s.m.bin(c, Bin::LShr, a, two);
- let comp = self.s.m.bin(c, Bin::And, comp, three);
- let mut x = self.s.m.extract(c, r, 0);
- for j in 1..4 {
- let e = self.s.m.extract(c, r, j);
- let jc = self.s.m.i32(j);
- let is = self.s.m.cmp(c, pred::EQ, comp, jc);
- x = self.s.m.select(c, is, e, x);
- }
- x
- }
- /// `channels` channels of the element at byte `off` of raw buffer `h`,
- /// converted from the GFX10 buffer format `format` as the hardware's
- /// typed loads do (channels past the format's read 0, 0, 0, 1).
- // shortcut: the descriptor's channel swizzle is taken as the identity
- // (it is for raw views and the descriptors code builds), elements of 4
- // bytes or more as dword-aligned. Norm formats divide, which the AMD
- // driver does 1 ULP off the hardware's correctly rounded conversion for
- // some values; correct it with an integer check if a title compares
- // converted values exactly.
- fn format_load(&mut self, h: V, off: V, format: u8, channels: u32) -> Result<Vec<V>, Why> {
- let (bits, numeric) = buffer_format(format).ok_or("a buffer format")?;
- let c = self.cur;
- let total: u32 = bits.iter().sum();
- let (u, align) = (self.s.m.undef(self.s.t.i32), self.s.m.i32(4));
- let load = |l: &mut Self, at: V, n: u32| -> Vec<V> {
- let mask = l.s.m.i8((1u8 << n) - 1);
- let r = l.s.op(
- c,
- "rawBufferLoad.f32",
- Attr::ReadOnly,
- l.s.t.res_ret,
- 139,
- &[h, at, u, mask, align],
- );
- (0..n)
- .map(|k| {
- let x = l.s.m.extract(c, r, k);
- l.as_int(x)
- })
- .collect()
- };
- let words = if total < 32 {
- // A byte or half inside the dword that holds it.
- let (not3, three) = (self.s.m.i32(!3), self.s.m.i32(3));
- let at = self.s.m.bin(c, Bin::And, off, not3);
- let w = load(self, at, 1)[0];
- let byte = self.s.m.bin(c, Bin::And, off, three);
- let sh = self.s.m.bin(c, Bin::Shl, byte, three);
- vec![self.s.m.bin(c, Bin::LShr, w, sh)]
- } else {
- load(self, off, total / 32)
- };
- let float = |l: &mut Self, f: V| l.as_int(f);
- let mut out = Vec::new();
- let mut at = 0;
- for &w in bits {
- let word = words[(at / 32) as usize];
- let field = if w == 32 {
- word
- } else {
- let sh = self.s.m.i32(at % 32);
- let t = self.s.m.bin(c, Bin::LShr, word, sh);
- let m = self.s.m.i32((1 << w) - 1);
- self.s.m.bin(c, Bin::And, t, m)
- };
- at += w;
- // Sign-extended from the field's top bit.
- let signed = |l: &mut Self| {
- if w == 32 {
- return field;
- }
- let k = l.s.m.i32(32 - w);
- let t = l.s.m.bin(c, Bin::Shl, field, k);
- l.s.m.bin(c, Bin::AShr, t, k)
- };
- let f32t = self.s.t.f32;
- let x = match numeric {
- Numeric::Unorm => {
- let f = self.s.m.cast(c, Cast::UiToFp, field, f32t);
- let max = self.s.m.f32((((1u64 << w) - 1) as f32).to_bits());
- let q = self.s.m.bin(c, Bin::SDiv, f, max);
- float(self, q)
- }
- Numeric::Snorm => {
- let s = signed(self);
- let f = self.s.m.cast(c, Cast::SiToFp, s, f32t);
- let max = self.s.m.f32((((1u64 << (w - 1)) - 1) as f32).to_bits());
- let q = self.s.m.bin(c, Bin::SDiv, f, max);
- let lo = self.s.m.f32((-1.0f32).to_bits());
- let q = self.binary(35, q, lo);
- float(self, q)
- }
- Numeric::Uscaled => {
- let f = self.s.m.cast(c, Cast::UiToFp, field, f32t);
- float(self, f)
- }
- Numeric::Sscaled => {
- let s = signed(self);
- let f = self.s.m.cast(c, Cast::SiToFp, s, f32t);
- float(self, f)
- }
- Numeric::Uint => field,
- Numeric::Sint => signed(self),
- Numeric::Float if w == 32 => field,
- Numeric::Float => {
- let h = match half_shift(w) {
- 0 => field,
- k => {
- let sh = self.s.m.i32(k);
- self.s.m.bin(c, Bin::Shl, field, sh)
- }
- };
- let f = self
- .s
- .op(c, "legacyF16ToF32", Attr::ReadNone, f32t, 131, &[h]);
- float(self, f)
- }
- };
- out.push(x);
- }
- let one = match numeric {
- Numeric::Uint | Numeric::Sint => 1,
- _ => 1.0f32.to_bits(),
- };
- for k in out.len() as u32..channels {
- out.push(self.s.m.i32(if k == 3 { one } else { 0 }));
- }
- out.truncate(channels as usize);
- Ok(out)
- }
- /// The byte offset a buffer descriptor built around a root descriptor adds
- /// (constant and computed parts); none for other descriptors.
- fn built_offset(&mut self, src: &Source) -> Result<(u32, Option<V>), Why> {
- match src {
- Source::Built { address, .. } => {
- let x = match address.dynamic {
- Some(v) => Some(self.i32_of(v)?),
- None => None,
- };
- Ok((address.offset, x))
- }
- _ => Ok((0, None)),
- }
- }
- /// The used-resource entry a descriptor loaded from a table falls in.
- fn used_kind(&self, src: &Source) -> Option<&'a UsedResource> {
- if let Source::Phi { args, .. } = src {
- return self.used_kind(args.first()?);
- }
- if let Source::Built { address, .. } = src {
- let rp = self.x.native.layout.get(address.param)?;
- return self
- .x
- .native
- .used
- .iter()
- .find(|u| u.root == RootType::Descriptor && u.user_data == rp.user_data);
- }
- let Source::Loaded { load, first } = src else {
- return None;
- };
- let a = self.scalar.get(load)?;
- let rp = self.x.native.layout.get(a.param)?;
- if rp.root != RootType::Table {
- return None;
- }
- let dword = (a.offset / 4) + u32::from(*first);
- self.x
- .native
- .used
- .iter()
- .filter(|u| {
- u.root == RootType::Table
- && u.user_data == rp.user_data
- && u.table_offset_dwords <= dword
- })
- .max_by_key(|u| u.table_offset_dwords)
- }
- /// A handle for the descriptor `src` read at `site`.
- fn handle(
- &mut self,
- site: Site,
- src: &Source,
- sampler: bool,
- props: (u32, u32),
- ) -> Result<V, Why> {
- match src {
- Source::Loaded { load, first } => {
- let key = (self.cur, *load, *first, sampler, props.0);
- if let Some(&h) = self.handles.get(&key) {
- return Ok(h);
- }
- let index = self.heap_index(*load, *first, sampler)?;
- let h = self.s.heap_handle(self.cur, index, sampler, props);
- self.handles.insert(key, h);
- Ok(h)
- }
- Source::Constant(words) if sampler => {
- let words: [u32; 4] = words
- .get(..4)
- .and_then(|w| w.try_into().ok())
- .ok_or("a sampler constant")?;
- let n = match self.samplers.iter().position(|w| *w == words) {
- Some(n) => n,
- None => {
- self.samplers.push(words);
- self.samplers.len() - 1
- }
- };
- if props.0 & 0x8000 != 0 {
- // shortcut: one sampler used with and without comparison
- // would need two static samplers; it becomes a comparison one.
- self.s.cmp_samplers |= 1 << n;
- }
- Ok(self.s.sampler_handle(self.cur, n as u32))
- }
- Source::Constant(_) => Err("a constant resource descriptor"),
- Source::Built { address, .. } => {
- let rp = *self
- .x
- .native
- .layout
- .get(address.param)
- .ok_or("a descriptor built around a table in the code")?;
- let kind = self
- .x
- .native
- .used
- .iter()
- .find(|u| u.user_data == rp.user_data)
- .map(|u| u.kind);
- match (rp.root, kind, props.0) {
- (RootType::Descriptor, Some(ResourceType::Cbv) | None, 13) => {
- Ok(self.s.root_cbv_handle(rp.slot))
- }
- (RootType::Descriptor, Some(ResourceType::Srv), 11) => {
- Ok(self.s.root_srv_handle(rp.slot))
- }
- _ => Err("a root UAV or typed root view"),
- }
- }
- Source::Phi { block, args } => {
- // Each predecessor computes its heap index at its end; the
- // join picks one with a phi.
- let _ = site;
- let mut incoming = Vec::new();
- for (&q, arg) in self.x.p.blocks[*block].preds.iter().zip(args) {
- if self.block[q].is_none() {
- continue;
- }
- let (Some(tail), Source::Loaded { load, first }) = (self.tail[q], arg) else {
- return Err("a descriptor chosen by control flow");
- };
- let at = self.edges.get(&(q, *block)).copied().unwrap_or(tail);
- let here = std::mem::replace(&mut self.cur, at);
- let index = self.heap_index(*load, *first, sampler);
- self.cur = here;
- incoming.push((index?, at));
- }
- let join = self.block[*block].ok_or("a descriptor chosen by control flow")?;
- let phi = self.s.m.phi(join, self.s.t.i32);
- self.s.m.set_phi(phi, incoming);
- Ok(self.s.heap_handle(self.cur, phi, sampler, props))
- }
- }
- }
- /// The heap index of the descriptor at dword `first` of what the scalar
- /// load at `load` reads, computed in the current block.
- fn heap_index(&mut self, load: Site, first: u8, sampler: bool) -> Result<V, Why> {
- let a = *self
- .scalar
- .get(&load)
- .ok_or("an untracked descriptor load")?;
- match self.x.native.layout.get(a.param).map(|p| p.root) {
- Some(RootType::Table | RootType::ResourceHeap | RootType::SamplerHeap) => {}
- _ => return Err("a descriptor outside the heaps"),
- }
- let li: &Inst = &self.x.p.blocks[load.0].insts[load.1];
- let Fields::Smem {
- base,
- offset,
- soffset,
- ..
- } = &li.fields
- else {
- return Err("a descriptor load");
- };
- let Operand::Scalar { code, .. } = *base else {
- return Err("a descriptor load");
- };
- let c = self.cur;
- let base = li.read(Reg::S(code)).ok_or("a descriptor load")?;
- let mut addr = self.i32_of(base)?;
- let k = self
- .s
- .m
- .i32((*offset as u32).wrapping_add(4 * u32::from(first)));
- addr = self.s.m.bin(c, Bin::Add, addr, k);
- if !matches!(soffset, Operand::Scalar { code: NULL, .. }) {
- let so = self.operand(li, soffset)?;
- addr = self.s.m.bin(c, Bin::Add, addr, so);
- }
- let shift = self.s.m.i32(if sampler { 4 } else { 5 });
- Ok(self.s.m.bin(c, Bin::LShr, addr, shift))
- }
- fn valu(&mut self, inst: &Inst, b: usize, i: usize) -> Result<(), Why> {
- let Fields::Valu(v) = &inst.fields else {
- unreachable!()
- };
- let name = inst.name();
- if let Some(rest) = name.strip_prefix("v_cmp_").or(name.strip_prefix("v_cmpx_")) {
- let r = if v.dpp.is_some() {
- Err("a compare with DPP")
- } else {
- self.compare(inst, rest)
- };
- if let Err(why) = r {
- for &(_, w) in &inst.writes {
- self.lane_fail.insert(w, why);
- }
- }
- return r;
- }
- let mut guard = self.guard(b, i)?;
- if let Some(d) = v.dpp {
- let (x, enable) = self.dpp(inst, d)?;
- guard = match (guard, enable) {
- (Some(g), Some(e)) => Some(self.s.m.bin(self.cur, Bin::And, g, e)),
- (g, e) => g.or(e),
- };
- self.src0 = Some(x);
- }
- let r = self.valu_op(inst, guard);
- self.src0 = None;
- r
- }
- /// DPP: src0 as read from another lane, and which lanes write their
- /// result (none: all).
- fn dpp(&mut self, inst: &Inst, dpp: Dpp) -> Result<(V, Option<V>), Why> {
- let Fields::Valu(v) = &inst.fields else {
- unreachable!()
- };
- let Some(&Operand::Vector { reg, .. }) = v.src.first() else {
- return Err("a DPP operand");
- };
- let x = self.i32_of(inst.read(Reg::V(reg)).ok_or("a DPP operand")?)?;
- // Quad permutations and DPP8 stay in groups the pixel layout shares.
- let quad = matches!(dpp, Dpp::Row { ctrl: 0..=0xFF, .. });
- let mesh_quad = self.s.mesh.is_some() && quad;
- if !mesh_quad {
- self.cross_lane(quad)?;
- }
- let c = self.cur;
- let lane = self.lane_index();
- let k = |l: &mut Self, n: u32| l.s.m.i32(n);
- let (sixteen_mask, low) = (k(self, !15), k(self, 15));
- let row = self.s.m.bin(c, Bin::And, lane, sixteen_mask);
- let pos = self.s.m.bin(c, Bin::And, lane, low);
- // Source lane, and whether it lies in the row.
- let (src, inside, masks, zero) = match dpp {
- Dpp::Lanes { sel, .. } => {
- let (seven, not7, three) = (k(self, 7), k(self, !7), k(self, 3));
- let p = self.s.m.bin(c, Bin::And, lane, seven);
- let sh = self.s.m.bin(c, Bin::Mul, p, three);
- let sel = k(self, sel);
- let pick = self.s.m.bin(c, Bin::LShr, sel, sh);
- let pick = self.s.m.bin(c, Bin::And, pick, seven);
- let base = self.s.m.bin(c, Bin::And, lane, not7);
- (self.s.m.bin(c, Bin::Or, base, pick), None, None, false)
- }
- Dpp::Row {
- ctrl,
- row_mask,
- bank_mask,
- bound_ctrl,
- ..
- } => {
- let n = u32::from(ctrl & 15);
- let (src, inside) = match ctrl {
- 0..=0xFF => {
- let (three, one, not3) = (k(self, 3), k(self, 1), k(self, !3));
- let p = self.s.m.bin(c, Bin::And, lane, three);
- let sh = self.s.m.bin(c, Bin::Shl, p, one);
- let perm = k(self, u32::from(ctrl));
- let pick = self.s.m.bin(c, Bin::LShr, perm, sh);
- let pick = self.s.m.bin(c, Bin::And, pick, three);
- let base = self.s.m.bin(c, Bin::And, lane, not3);
- (self.s.m.bin(c, Bin::Or, base, pick), None)
- }
- 0x101..=0x10F | 0x111..=0x11F => {
- let nn = k(self, n);
- let left = ctrl < 0x110;
- let (op, p) = if left {
- (Bin::Add, pred::IULT)
- } else {
- (Bin::Sub, pred::IUGE)
- };
- let s = self.s.m.bin(c, op, lane, nn);
- // Left: pos + n < 16; right: pos >= n.
- let bound = if left { k(self, 16 - n) } else { nn };
- (s, Some(self.s.m.cmp(c, p, pos, bound)))
- }
- 0x121..=0x12F => {
- let nn = k(self, n);
- let t = self.s.m.bin(c, Bin::Sub, pos, nn);
- let t = self.s.m.bin(c, Bin::And, t, low);
- (self.s.m.bin(c, Bin::Or, row, t), None)
- }
- 0x140 | 0x141 => {
- let m = k(self, if ctrl == 0x140 { 15 } else { 7 });
- let t = self.s.m.bin(c, Bin::Xor, lane, m);
- (t, None)
- }
- 0x150..=0x15F => {
- let nn = k(self, n);
- (self.s.m.bin(c, Bin::Or, row, nn), None)
- }
- 0x160..=0x16F => {
- let nn = k(self, n);
- (self.s.m.bin(c, Bin::Xor, lane, nn), None)
- }
- _ => return Err("a DPP lane pattern"),
- };
- let masks = (row_mask != 0xF || bank_mask != 0xF).then_some((row_mask, bank_mask));
- (src, inside, masks, bound_ctrl)
- }
- };
- let mut r = if mesh_quad {
- self.quad_pick(x, src)
- } else {
- self.read_lane(x, src)
- };
- let mut enable = None;
- if let Some(inside) = inside {
- if zero {
- let z = k(self, 0);
- r = self.s.m.select(c, inside, r, z);
- } else {
- enable = Some(inside);
- }
- }
- if let Some((rows, banks)) = masks {
- // Row (16 lanes) and bank (4 lanes of a row) write masks.
- let (four, two, three) = (k(self, 4), k(self, 2), k(self, 3));
- let ri = self.s.m.bin(c, Bin::LShr, lane, four);
- let ri = self.s.m.bin(c, Bin::And, ri, three);
- let bi = self.s.m.bin(c, Bin::LShr, lane, two);
- let bi = self.s.m.bin(c, Bin::And, bi, three);
- let (rm, bm) = (k(self, u32::from(rows)), k(self, u32::from(banks)));
- let rb = self.s.m.bin(c, Bin::LShr, rm, ri);
- let bb = self.s.m.bin(c, Bin::LShr, bm, bi);
- let both = self.s.m.bin(c, Bin::And, rb, bb);
- let (one, z) = (k(self, 1), k(self, 0));
- let both = self.s.m.bin(c, Bin::And, both, one);
- let on = self.s.m.cmp(c, pred::NE, both, z);
- enable = Some(match enable {
- Some(e) => self.s.m.bin(c, Bin::And, e, on),
- None => on,
- });
- }
- Ok((r, enable))
- }
- fn valu_op(&mut self, inst: &Inst, guard: Option<V>) -> Result<(), Why> {
- let Fields::Valu(v) = &inst.fields else {
- unreachable!()
- };
- let name = inst.name();
- let c = self.cur;
- let f = |l: &mut Self, k| l.fsrc(inst, k);
- let s = |l: &mut Self, k| l.src(inst, k);
- let float = |l: &mut Self, r: V| l.write_f(inst, r, guard);
- let int = |l: &mut Self, r: V| l.write_i(inst, r, guard);
- if let Some(r) = self.valu16(inst, name, guard) {
- return r;
- }
- if let Some(op) = name.strip_prefix("v_pk_") {
- return self.packed16(inst, op, guard);
- }
- match name {
- "v_lshlrev_b64" | "v_lshrrev_b64" | "v_ashrrev_i64" => {
- let n = s(self, 0)?;
- let x = self.src64(inst, 1)?;
- let i64t = self.s.m.int(64);
- let k63 = self.s.m.i32(63);
- let n = self.s.m.bin(c, Bin::And, n, k63);
- let n = self.s.m.cast(c, Cast::ZExt, n, i64t);
- let op = match name {
- "v_lshlrev_b64" => Bin::Shl,
- "v_lshrrev_b64" => Bin::LShr,
- _ => Bin::AShr,
- };
- let r = self.s.m.bin(c, op, x, n);
- let Some(Operand::Vector { reg, .. }) = v.dst else {
- return Err("a 64-bit result");
- };
- let (lo, hi) = self.split64(r);
- self.write(inst, Reg::V(reg), lo, guard);
- self.write(inst, Reg::V(reg + 1), hi, guard);
- Ok(())
- }
- "v_cubeid_f32" | "v_cubesc_f32" | "v_cubetc_f32" | "v_cubema_f32" => {
- // Cube-map face coordinates (AMD's ISA): the major axis is z
- // when |z| >= |x| and |z| >= |y|, else y when |y| >= |x|,
- // else x.
- let (x, y, z) = (f(self, 0)?, f(self, 1)?, f(self, 2)?);
- let (ax, ay, az) = (self.unary(6, x), self.unary(6, y), self.unary(6, z));
- let zx = self.s.m.cmp(c, pred::OGE, az, ax);
- let zy = self.s.m.cmp(c, pred::OGE, az, ay);
- let zmaj = self.s.m.bin(c, Bin::And, zx, zy);
- let ymaj = self.s.m.cmp(c, pred::OGE, ay, ax);
- let zero = self.s.m.f32(0);
- let neg = |l: &mut Self, v: V| l.s.m.cmp(c, pred::OLT, v, zero);
- let minus = |l: &mut Self, v: V| {
- let z = l.s.m.f32(0x8000_0000);
- l.s.m.bin(c, Bin::Sub, z, v)
- };
- let pick = |l: &mut Self, zv: V, yv: V, xv: V| {
- let t = l.s.m.select(c, ymaj, yv, xv);
- l.s.m.select(c, zmaj, zv, t)
- };
- let r = match name {
- "v_cubeid_f32" => {
- let k = |l: &mut Self, v: f32| l.s.m.f32(v.to_bits());
- let (nz, ny, nx) = (neg(self, z), neg(self, y), neg(self, x));
- let (f5, f4, f3, f2, f1, f0) = (
- k(self, 5.0),
- k(self, 4.0),
- k(self, 3.0),
- k(self, 2.0),
- k(self, 1.0),
- k(self, 0.0),
- );
- let zf = self.s.m.select(c, nz, f5, f4);
- let yf = self.s.m.select(c, ny, f3, f2);
- let xf = self.s.m.select(c, nx, f1, f0);
- pick(self, zf, yf, xf)
- }
- "v_cubesc_f32" => {
- let (nz, nx) = (neg(self, z), neg(self, x));
- let (mx, mz) = (minus(self, x), minus(self, z));
- let zf = self.s.m.select(c, nz, mx, x);
- let xf = self.s.m.select(c, nx, z, mz);
- pick(self, zf, x, xf)
- }
- "v_cubetc_f32" => {
- let ny = neg(self, y);
- let (my, mz) = (minus(self, y), minus(self, z));
- let yf = self.s.m.select(c, ny, mz, z);
- pick(self, my, yf, my)
- }
- _ => {
- let t = pick(self, z, y, x);
- let two = self.s.m.f32(2.0f32.to_bits());
- self.s.m.bin(c, Bin::Mul, t, two)
- }
- };
- float(self, r)
- }
- "v_movrels_b32" | "v_movreld_b32" | "v_movrelsd_b32" => {
- // M0 indexes the source (movrels), the destination (movreld)
- // or both, from the encoded register up to the program's
- // VGPR count (the registers the instruction reads or writes).
- let m0 = self.i32_of(inst.read(Reg::S(crate::ir::M0)).ok_or("M0")?)?;
- let (
- Some(&Operand::Vector { reg: sreg, .. }),
- Some(Operand::Vector { reg: dreg, .. }),
- ) = (v.src.first(), v.dst)
- else {
- return Err("an M0-relative move");
- };
- let indexed_src = name != "v_movreld_b32";
- let value = if indexed_src {
- let mut r = self.s.m.i32(0);
- for k in 0..=255u8.saturating_sub(sreg) {
- let Some(x) = inst.read(Reg::V(sreg + k)) else {
- break;
- };
- let x = self.i32_of(x)?;
- let kk = self.s.m.i32(u32::from(k));
- let is = self.s.m.cmp(c, pred::EQ, m0, kk);
- r = self.s.m.select(c, is, x, r);
- }
- r
- } else {
- s(self, 0)?
- };
- if name == "v_movrels_b32" {
- return int(self, value);
- }
- for k in 0..=255u8.saturating_sub(dreg) {
- let Some(_) = inst.write(Reg::V(dreg + k)) else {
- break;
- };
- let old = match inst.read(Reg::V(dreg + k)) {
- Some(o) => self.i32_of(o)?,
- None => self.s.m.i32(0),
- };
- let kk = self.s.m.i32(u32::from(k));
- let is = self.s.m.cmp(c, pred::EQ, m0, kk);
- let x = self.s.m.select(c, is, value, old);
- self.write_placed(inst, Reg::V(dreg + k), x, guard);
- }
- Ok(())
- }
- "v_swap_b32" => {
- let (
- Some(&Operand::Vector { reg: sreg, .. }),
- Some(Operand::Vector { reg: dreg, .. }),
- ) = (v.src.first(), v.dst)
- else {
- return Err("v_swap_b32");
- };
- let a = self.i32_of(inst.read(Reg::V(dreg)).ok_or("v_swap_b32")?)?;
- let b = self.i32_of(inst.read(Reg::V(sreg)).ok_or("v_swap_b32")?)?;
- self.write_placed(inst, Reg::V(dreg), b, guard);
- if dreg != sreg {
- self.write_placed(inst, Reg::V(sreg), a, guard);
- }
- Ok(())
- }
- "v_ffbh_u32" | "v_ffbh_i32" => {
- // Counted from the top bit, -1 when none (DXIL's too).
- let a = s(self, 0)?;
- let op = if name == "v_ffbh_u32" { 33 } else { 34 };
- let r = self
- .s
- .op(c, "unaryBits.i32", Attr::ReadNone, self.s.t.i32, op, &[a]);
- int(self, r)
- }
- "v_mov_b32" => {
- if v.abs != 0 || v.neg != 0 || v.clamp || v.omod != 0 {
- let a = f(self, 0)?;
- return float(self, a);
- }
- let a = s(self, 0)?;
- int(self, a)
- }
- "v_add_f32" | "v_sub_f32" | "v_subrev_f32" | "v_mul_f32" | "v_mul_legacy_f32"
- | "v_max_f32" | "v_min_f32" => {
- let (a, bb) = (f(self, 0)?, f(self, 1)?);
- let r = match name {
- "v_add_f32" => self.s.m.bin(c, Bin::Add, a, bb),
- "v_sub_f32" => self.s.m.bin(c, Bin::Sub, a, bb),
- "v_subrev_f32" => self.s.m.bin(c, Bin::Sub, bb, a),
- "v_mul_f32" => self.s.m.bin(c, Bin::Mul, a, bb),
- "v_mul_legacy_f32" => self.legacy_mul(a, bb),
- "v_max_f32" => self.max_min(true, a, bb),
- _ => self.max_min(false, a, bb),
- };
- float(self, r)
- }
- "v_fma_f32" | "v_fmaak_f32" | "v_fmamk_f32" => {
- // shortcut: DXIL has no fused f32 multiply-add; FMad lets the driver fuse.
- let (a, bb, cc) = (f(self, 0)?, f(self, 1)?, f(self, 2)?);
- let r = self.fmad(a, bb, cc);
- float(self, r)
- }
- "v_fmac_f32" | "v_mac_f32" | "v_mac_legacy_f32" => {
- let (a, bb) = (f(self, 0)?, f(self, 1)?);
- let Some(Operand::Vector { reg, .. }) = v.dst else {
- return Err("an accumulator");
- };
- let acc = inst.read(Reg::V(reg)).ok_or("an accumulator")?;
- let acc = match self.floats.get(&acc) {
- Some(&x) => x,
- None => {
- let x = self.i32_of(acc)?;
- self.as_float(x)
- }
- };
- let r = match name {
- "v_fmac_f32" => self.fmad(a, bb, acc),
- "v_mac_f32" => {
- let m = self.s.m.bin(c, Bin::Mul, a, bb);
- self.s.m.bin(c, Bin::Add, m, acc)
- }
- _ => {
- let m = self.legacy_mul(a, bb);
- self.s.m.bin(c, Bin::Add, m, acc)
- }
- };
- float(self, r)
- }
- "v_mad_f32" | "v_madak_f32" | "v_madmk_f32" | "v_mad_legacy_f32" => {
- let (a, bb, cc) = (f(self, 0)?, f(self, 1)?, f(self, 2)?);
- let m = if name == "v_mad_legacy_f32" {
- self.legacy_mul(a, bb)
- } else {
- self.s.m.bin(c, Bin::Mul, a, bb)
- };
- let r = self.s.m.bin(c, Bin::Add, m, cc);
- float(self, r)
- }
- "v_max3_f32" | "v_min3_f32" | "v_med3_f32" => {
- let (a, bb, cc) = (f(self, 0)?, f(self, 1)?, f(self, 2)?);
- let r = match name {
- "v_max3_f32" => {
- let t = self.max_min(true, a, bb);
- self.max_min(true, t, cc)
- }
- "v_min3_f32" => {
- let t = self.max_min(false, a, bb);
- self.max_min(false, t, cc)
- }
- _ => self.med3(a, bb, cc),
- };
- float(self, r)
- }
- "v_rcp_f32" | "v_rcp_iflag_f32" => {
- // shortcut: the hardware reciprocal is approximate (1 ULP); this divides.
- let a = f(self, 0)?;
- let one = self.s.m.f32(1.0f32.to_bits());
- let r = self.s.m.bin(c, Bin::SDiv, one, a);
- float(self, r)
- }
- "v_rsq_f32" | "v_sqrt_f32" | "v_log_f32" | "v_exp_f32" | "v_fract_f32"
- | "v_trunc_f32" | "v_floor_f32" | "v_ceil_f32" | "v_rndne_f32" | "v_sin_f32"
- | "v_cos_f32" => {
- let a = f(self, 0)?;
- let op = match name {
- "v_rsq_f32" => 25,
- "v_sqrt_f32" => 24,
- "v_log_f32" => 23,
- "v_exp_f32" => 21,
- "v_fract_f32" => 22,
- "v_trunc_f32" => 29,
- "v_floor_f32" => 27,
- "v_ceil_f32" => 28,
- "v_rndne_f32" => 26,
- "v_sin_f32" => 13,
- _ => 12,
- };
- let r = match op {
- 12 | 13 => self.trig_turns(a, op == 12),
- 22 => self.fract(a, 1.0, f32::from_bits(0x3F7F_FFFF)),
- _ => self.unary(op, a),
- };
- float(self, r)
- }
- "v_cvt_i32_f32" | "v_cvt_u32_f32" | "v_cvt_flr_i32_f32" | "v_cvt_rpi_i32_f32" => {
- let mut a = f(self, 0)?;
- if name == "v_cvt_rpi_i32_f32" {
- let h = self.s.m.f32(0.5f32.to_bits());
- a = self.s.m.bin(c, Bin::Add, a, h);
- }
- if name != "v_cvt_i32_f32" && name != "v_cvt_u32_f32" {
- a = self.unary(27, a);
- }
- // D3D's conversions saturate and give 0 for NaN, as the
- // hardware does (checked on the RX 6700 XT and WARP).
- let op = if name == "v_cvt_u32_f32" {
- Cast::FpToUi
- } else {
- Cast::FpToSi
- };
- let r = self.s.m.cast(c, op, a, self.s.t.i32);
- int(self, r)
- }
- "v_cvt_f32_i32" | "v_cvt_f32_u32" | "v_cvt_f32_ubyte0" | "v_cvt_f32_ubyte1"
- | "v_cvt_f32_ubyte2" | "v_cvt_f32_ubyte3" => {
- let mut a = s(self, 0)?;
- if let Some(n) = name.strip_prefix("v_cvt_f32_ubyte") {
- let sh = self.s.m.i32(8 * u32::from(n.as_bytes()[0] - b'0'));
- let ff = self.s.m.i32(0xff);
- a = self.s.m.bin(c, Bin::LShr, a, sh);
- a = self.s.m.bin(c, Bin::And, a, ff);
- }
- let op = if name == "v_cvt_f32_i32" {
- Cast::SiToFp
- } else {
- Cast::UiToFp
- };
- let r = self.s.m.cast(c, op, a, self.s.t.f32);
- float(self, r)
- }
- "v_fma_mix_f32" | "v_fma_mixlo_f16" | "v_fma_mixhi_f16" => {
- // Each source is f32, or with op_sel_hi the low (op_sel: high)
- // f16 half; neg_hi is the absolute value here.
- let mut xs = Vec::new();
- for i in 0..3 {
- let op = v.src.get(i).ok_or("a missing operand")?;
- let x = self.operand(inst, op)?;
- let mut x = if v.opsel_hi >> i & 1 != 0 {
- let h = if v.opsel >> i & 1 != 0 {
- let k16 = self.s.m.i32(16);
- self.s.m.bin(c, Bin::LShr, x, k16)
- } else {
- x
- };
- self.s
- .op(c, "legacyF16ToF32", Attr::ReadNone, self.s.t.f32, 131, &[h])
- } else {
- self.as_float(x)
- };
- if v.neg_hi >> i & 1 != 0 {
- x = self.unary(6, x);
- }
- if v.neg >> i & 1 != 0 {
- let z = self.s.m.f32(0x8000_0000);
- x = self.s.m.bin(c, Bin::Sub, z, x);
- }
- xs.push(x);
- }
- let mut r = self.fmad(xs[0], xs[1], xs[2]);
- if name == "v_fma_mix_f32" {
- return float(self, r);
- }
- if v.clamp {
- r = self.unary(7, r);
- }
- // The f16 result goes to one half; the other half stays.
- let Some(Operand::Vector { reg, .. }) = v.dst else {
- return Err("a mixed-precision result");
- };
- let old = match inst.read(Reg::V(reg)) {
- Some(o) => self.i32_of(o)?,
- None => self.s.m.i32(0),
- };
- let h = self
- .s
- .op(c, "legacyF32ToF16", Attr::ReadNone, self.s.t.i32, 130, &[r]);
- let (keep, h) = if name == "v_fma_mixhi_f16" {
- let k16 = self.s.m.i32(16);
- (0xFFFF, self.s.m.bin(c, Bin::Shl, h, k16))
- } else {
- (0xFFFF_0000, h)
- };
- let keep = self.s.m.i32(keep);
- let old = self.s.m.bin(c, Bin::And, old, keep);
- let x = self.s.m.bin(c, Bin::Or, old, h);
- self.write(inst, Reg::V(reg), x, guard);
- Ok(())
- }
- "v_cvt_pkrtz_f16_f32" => {
- let (a, bb) = (f(self, 0)?, f(self, 1)?);
- let lo = self.f16_toward_zero(a);
- let hi = self.f16_toward_zero(bb);
- let sh = self.s.m.i32(16);
- let hi = self.s.m.bin(c, Bin::Shl, hi, sh);
- let r = self.s.m.bin(c, Bin::Or, lo, hi);
- int(self, r)
- }
- "v_cvt_f16_f32" if v.opsel == 0 => {
- // Rounded to nearest even (the MODE default) into the low half.
- let a = f(self, 0)?;
- let r = self
- .s
- .op(c, "legacyF32ToF16", Attr::ReadNone, self.s.t.i32, 130, &[a]);
- let Some(Operand::Vector { reg, .. }) = v.dst else {
- return Err("a 16-bit result outside a VGPR");
- };
- let r = self.low_half(inst, reg, r);
- int(self, r)
- }
- "v_pack_b32_f16" => {
- // Two f16 sources (op_sel: their high halves; abs and neg on
- // the sign bit) into the low and high half.
- let mut halves = Vec::new();
- for k in 0..2 {
- let op = v.src.get(k).ok_or("a missing operand")?;
- let mut x = self.operand(inst, op)?;
- if v.opsel >> k & 1 != 0 {
- let k16 = self.s.m.i32(16);
- x = self.s.m.bin(c, Bin::LShr, x, k16);
- }
- let mask = if v.abs >> k & 1 != 0 { 0x7FFF } else { 0xFFFF };
- let mask = self.s.m.i32(mask);
- x = self.s.m.bin(c, Bin::And, x, mask);
- if v.neg >> k & 1 != 0 {
- let sign = self.s.m.i32(0x8000);
- x = self.s.m.bin(c, Bin::Xor, x, sign);
- }
- halves.push(x);
- }
- let k16 = self.s.m.i32(16);
- let hi = self.s.m.bin(c, Bin::Shl, halves[1], k16);
- let r = self.s.m.bin(c, Bin::Or, halves[0], hi);
- int(self, r)
- }
- "v_cvt_f32_f16" if v.opsel == 0 => {
- let a = s(self, 0)?;
- let r = self
- .s
- .op(c, "legacyF16ToF32", Attr::ReadNone, self.s.t.f32, 131, &[a]);
- float(self, r)
- }
- "v_cndmask_b32" => {
- let mask = match v.src.get(2) {
- Some(&Operand::Scalar { code, .. }) => inst.read(Reg::S(code)),
- _ => None,
- };
- let m = mask
- .and_then(|m| self.x.st.masks.get(&m).copied())
- .ok_or("a select on an untracked mask")?;
- let cond = self.boolean(m, c)?;
- if v.abs != 0 || v.neg != 0 {
- let (a, bb) = (f(self, 0)?, f(self, 1)?);
- let r = self.s.m.select(c, cond, bb, a);
- return float(self, r);
- }
- let (a, bb) = (s(self, 0)?, s(self, 1)?);
- let r = self.s.m.select(c, cond, bb, a);
- int(self, r)
- }
- "v_add_co_u32" | "v_sub_co_u32" | "v_subrev_co_u32" | "v_add_co_ci_u32"
- | "v_sub_co_ci_u32" | "v_subrev_co_ci_u32" => {
- // The carry (borrow) out is a lane mask: this lane's bit.
- let (mut a, mut bb) = (s(self, 0)?, s(self, 1)?);
- if name.contains("subrev") {
- std::mem::swap(&mut a, &mut bb);
- }
- let cin = if name.ends_with("_ci_u32") {
- let m = match v.src.get(2) {
- Some(&Operand::Scalar { code, .. }) => inst.read(Reg::S(code)),
- _ => None,
- };
- let m = m
- .and_then(|m| self.x.st.masks.get(&m).copied())
- .ok_or("a carry-in from an untracked mask")?;
- Some(self.boolean(m, c)?)
- } else {
- None
- };
- let (r, carry) = self.add_sub_carry(c, name.starts_with("v_add"), a, bb, cin);
- int(self, r)?;
- if let Some(Operand::Scalar { code, .. }) = v.carry
- && let Some(w) = inst.write(Reg::S(code))
- {
- self.lane_bits.insert(w, carry);
- }
- for &(_, w) in &inst.writes {
- self.vals
- .entry(w)
- .or_insert(Val::Missing("a lane mask read as data"));
- }
- Ok(())
- }
- "v_readlane_b32" => {
- let (a, lane) = (s(self, 0)?, s(self, 1)?);
- // A computed lane in a pixel or vertex program is a host lane
- // (from a mask or `v_mbcnt`); a constant one is the console's.
- if self.mask_data == MaskData::Host && !matches!(lane, V::Const(_)) {
- self.s.flags |= flags::WAVE_OPS;
- } else {
- self.cross_lane(false)?;
- }
- let r = self.read_lane(a, lane);
- int(self, r)
- }
- "v_writelane_b32" => {
- // The lane given takes the scalar; the others keep their value.
- let (a, lane) = (s(self, 0)?, s(self, 1)?);
- let Some(Operand::Vector { reg, .. }) = v.dst else {
- return Err("a lane write");
- };
- let old = match inst.read(Reg::V(reg)) {
- Some(o) => self.i32_of(o)?,
- None => self.s.m.i32(0),
- };
- self.cross_lane(false)?;
- let me = self.lane_index();
- let is = self.s.m.cmp(c, pred::EQ, me, lane);
- let r = self.s.m.select(c, is, a, old);
- int(self, r)
- }
- "v_readfirstlane_b32" => {
- let a = s(self, 0)?;
- self.s.flags |= flags::WAVE_OPS;
- let r = self.s.op(
- c,
- "waveReadLaneFirst.i32",
- Attr::NoUnwind,
- self.s.t.i32,
- 118,
- &[a],
- );
- int(self, r)
- }
- _ => {
- let r = self.integer(inst, name)?;
- int(self, r)
- }
- }
- }
- /// A 64-bit source (a register pair; inline constants sign-extend).
- fn src64(&mut self, inst: &Inst, k: usize) -> Result<V, Why> {
- let Fields::Valu(v) = &inst.fields else {
- unreachable!()
- };
- if v.abs >> k & 1 != 0 || v.neg >> k & 1 != 0 || v.sdwa.is_some() || v.dpp.is_some() {
- return Err("64-bit operand modifiers");
- }
- let c = self.cur;
- let i64t = self.s.m.int(64);
- self.s.flags |= flags::INT64;
- let op = *v.src.get(k).ok_or("a missing operand")?;
- let pair = |l: &mut Self, r: Reg, r1: Reg| -> Result<V, Why> {
- let lo = l.i32_of(inst.read(r).ok_or("an unread operand")?)?;
- let hi = l.i32_of(inst.read(r1).ok_or("an unread operand")?)?;
- let lo = l.s.m.cast(c, Cast::ZExt, lo, i64t);
- let hi = l.s.m.cast(c, Cast::ZExt, hi, i64t);
- let k32 = l.s.m.int_const(i64t, 32);
- let hi = l.s.m.bin(c, Bin::Shl, hi, k32);
- Ok(l.s.m.bin(c, Bin::Or, lo, hi))
- };
- match op {
- Operand::Vector { reg, .. } => pair(self, Reg::V(reg), Reg::V(reg + 1)),
- Operand::Scalar { code, .. } if code != NULL => {
- pair(self, Reg::S(code), Reg::S(code + 1))
- }
- Operand::Const(k @ 128..=208) => {
- let x = crate::resource::inline(k).ok_or("a special operand")?;
- Ok(self.s.m.int_const(i64t, i64::from(x as i32)))
- }
- _ => {
- // Literals and the null register: zero-extended.
- let x = self.operand(inst, &op)?;
- Ok(self.s.m.cast(c, Cast::ZExt, x, i64t))
- }
- }
- }
- /// The low and high dwords of an i64.
- fn split64(&mut self, x: V) -> (V, V) {
- let c = self.cur;
- let i64t = self.s.m.int(64);
- let k32 = self.s.m.int_const(i64t, 32);
- let lo = self.s.m.cast(c, Cast::Trunc, x, self.s.t.i32);
- let hi = self.s.m.bin(c, Bin::LShr, x, k32);
- let hi = self.s.m.cast(c, Cast::Trunc, hi, self.s.t.i32);
- (lo, hi)
- }
- /// Source `k` of a 16-bit operation in the low 16 bits (the rest 0):
- /// op_sel (or an SDWA word select) picks the high half; inline float
- /// constants are f16 values here.
- fn half_src(&mut self, inst: &Inst, k: usize) -> Result<V, Why> {
- let Fields::Valu(v) = &inst.fields else {
- unreachable!()
- };
- let op = *v.src.get(k).ok_or("a missing operand")?;
- let mut high = v.opsel >> k & 1 != 0;
- if let Some(sd) = v.sdwa {
- match sd.src_sel.get(k).copied().unwrap_or(6) {
- 4 => {}
- 5 => high = true,
- 6 => {}
- _ => return Err("a byte select on a 16-bit operation"),
- }
- }
- let x = match self.src0 {
- Some(x) if k == 0 => x,
- _ => self.f16_operand(inst, &op)?,
- };
- let c = self.cur;
- let x = if high {
- let k16 = self.s.m.i32(16);
- self.s.m.bin(c, Bin::LShr, x, k16)
- } else {
- x
- };
- let m = self.s.m.i32(0xFFFF);
- Ok(self.s.m.bin(c, Bin::And, x, m))
- }
- /// An f16 source as an f32 (exact), with its abs/neg modifiers.
- fn half_fsrc(&mut self, inst: &Inst, k: usize) -> Result<V, Why> {
- let Fields::Valu(v) = &inst.fields else {
- unreachable!()
- };
- let h = self.half_src(inst, k)?;
- let f32t = self.s.t.f32;
- let mut f = self
- .s
- .op(self.cur, "legacyF16ToF32", Attr::ReadNone, f32t, 131, &[h]);
- if v.abs >> k & 1 != 0 {
- f = self.unary(6, f);
- }
- if v.neg >> k & 1 != 0 {
- let z = self.s.m.f32(0x8000_0000);
- f = self.s.m.bin(self.cur, Bin::Sub, z, f);
- }
- Ok(f)
- }
- /// Writes a 16-bit result (low bits of `x`) into the destination's low
- /// half, or its high half with op_sel bit 3 (or an SDWA WORD_1 select);
- /// the other half is kept (GFX10).
- fn write_half(&mut self, inst: &Inst, x: V, guard: Option<V>) -> Result<(), Why> {
- let Fields::Valu(v) = &inst.fields else {
- unreachable!()
- };
- let Some(Operand::Vector { reg, .. }) = v.dst else {
- return Err("a 16-bit result outside a VGPR");
- };
- let mut high = v.opsel >> 3 & 1 != 0;
- // SDWA: the word written, the other one kept (preserve) or zero
- // (pad; a sign extension above the high word adds nothing).
- let mut keep = true;
- if let Some(sd) = v.sdwa {
- match (sd.dst_sel, sd.dst_unused) {
- (4, 2) => {}
- (5, 2) => high = true,
- (4 | 6, _) => keep = false,
- (5, _) => (high, keep) = (true, false),
- _ => return Err("an SDWA destination of a 16-bit operation"),
- }
- }
- let c = self.cur;
- let old = match inst.read(Reg::V(reg)).map(|o| self.i32_of(o)) {
- Some(Ok(o)) if keep => o,
- _ => self.s.m.i32(0),
- };
- let (k16, low) = (self.s.m.i32(16), self.s.m.i32(0xFFFF));
- let x = self.s.m.bin(c, Bin::And, x, low);
- let r = if high {
- let o = self.s.m.bin(c, Bin::And, old, low);
- let x = self.s.m.bin(c, Bin::Shl, x, k16);
- self.s.m.bin(c, Bin::Or, o, x)
- } else {
- let keep = self.s.m.i32(0xFFFF_0000);
- let o = self.s.m.bin(c, Bin::And, old, keep);
- self.s.m.bin(c, Bin::Or, o, x)
- };
- self.write_placed(inst, Reg::V(reg), r, guard);
- Ok(())
- }
- /// 16-bit operations, computed in f32 or i32: f16 sums, differences and
- /// products are exact there and round once to f16 (f32 has the 2 x 11 +
- /// 2 bits that make double rounding harmless); `None` if `name` is none.
- fn valu16(&mut self, inst: &Inst, name: &str, guard: Option<V>) -> Option<Result<(), Why>> {
- const FLOAT2: &[&str] = &[
- "v_add_f16",
- "v_sub_f16",
- "v_subrev_f16",
- "v_mul_f16",
- "v_max_f16",
- "v_min_f16",
- ];
- const FLOAT3: &[&str] = &[
- "v_fma_f16",
- "v_mad_f16",
- "v_fmac_f16",
- "v_mac_f16",
- "v_max3_f16",
- "v_min3_f16",
- "v_med3_f16",
- ];
- const FLOAT1: &[&str] = &[
- "v_rcp_f16",
- "v_sqrt_f16",
- "v_rsq_f16",
- "v_log_f16",
- "v_exp_f16",
- "v_fract_f16",
- "v_trunc_f16",
- "v_floor_f16",
- "v_ceil_f16",
- "v_rndne_f16",
- "v_sin_f16",
- "v_cos_f16",
- "v_cvt_f16_u16",
- "v_cvt_f16_i16",
- ];
- const INT: &[&str] = &[
- "v_add_nc_u16",
- "v_sub_nc_u16",
- "v_subrev_nc_u16",
- "v_mul_lo_u16",
- "v_lshlrev_b16",
- "v_lshrrev_b16",
- "v_ashrrev_i16",
- "v_max_u16",
- "v_max_i16",
- "v_min_u16",
- "v_min_i16",
- "v_cvt_u16_f16",
- "v_cvt_i16_f16",
- "v_cvt_norm_i16_f16",
- "v_cvt_norm_u16_f16",
- ];
- let all = [FLOAT1, FLOAT2, FLOAT3, INT];
- if !all.iter().any(|l| l.contains(&name))
- && !matches!(name, "v_cvt_f16_f32" | "v_cvt_f32_f16")
- {
- return None;
- }
- Some(self.valu16_op(inst, name, guard, FLOAT1, FLOAT2, FLOAT3))
- }
- #[allow(clippy::too_many_arguments)]
- fn valu16_op(
- &mut self,
- inst: &Inst,
- name: &str,
- guard: Option<V>,
- float1: &[&str],
- float2: &[&str],
- float3: &[&str],
- ) -> Result<(), Why> {
- let Fields::Valu(v) = &inst.fields else {
- unreachable!()
- };
- let c = self.cur;
- let i32t = self.s.t.i32;
- let f32t = self.s.t.f32;
- if name == "v_cvt_f32_f16" {
- let f = self.half_fsrc(inst, 0)?;
- return self.write_f(inst, f, guard);
- }
- let float_result = |l: &mut Self, mut f: V| -> Result<(), Why> {
- if v.omod != 0 {
- return Err("an output modifier on a 16-bit operation");
- }
- if v.clamp {
- f = l.unary(7, f);
- }
- let h =
- l.s.op(l.cur, "legacyF32ToF16", Attr::ReadNone, i32t, 130, &[f]);
- l.write_half(inst, h, guard)
- };
- if name == "v_cvt_f16_f32" {
- let f = self.fsrc(inst, 0)?;
- return float_result(self, f);
- }
- if float2.contains(&name) {
- let (a, b) = (self.half_fsrc(inst, 0)?, self.half_fsrc(inst, 1)?);
- let r = match name {
- "v_add_f16" => self.s.m.bin(c, Bin::Add, a, b),
- "v_sub_f16" => self.s.m.bin(c, Bin::Sub, a, b),
- "v_subrev_f16" => self.s.m.bin(c, Bin::Sub, b, a),
- "v_mul_f16" => self.s.m.bin(c, Bin::Mul, a, b),
- "v_max_f16" => self.max_min(true, a, b),
- _ => self.max_min(false, a, b),
- };
- return float_result(self, r);
- }
- if float3.contains(&name) {
- let (a, b) = (self.half_fsrc(inst, 0)?, self.half_fsrc(inst, 1)?);
- let acc = if matches!(name, "v_fmac_f16" | "v_mac_f16") {
- // The accumulator is the destination's half that is written.
- let Some(Operand::Vector { reg, .. }) = v.dst else {
- return Err("an accumulator");
- };
- let x = self.i32_of(inst.read(Reg::V(reg)).ok_or("an accumulator")?)?;
- let x = if v.opsel >> 3 & 1 != 0 {
- let k16 = self.s.m.i32(16);
- self.s.m.bin(c, Bin::LShr, x, k16)
- } else {
- x
- };
- let m = self.s.m.i32(0xFFFF);
- let x = self.s.m.bin(c, Bin::And, x, m);
- self.s
- .op(c, "legacyF16ToF32", Attr::ReadNone, f32t, 131, &[x])
- } else {
- self.half_fsrc(inst, 2)?
- };
- let r = match name {
- // shortcut: fused f16 multiply-adds as FMad in f32 (the
- // product is exact; the sum rounds twice).
- "v_fma_f16" | "v_fmac_f16" | "v_mad_f16" | "v_mac_f16" => self.fmad(a, b, acc),
- "v_max3_f16" => {
- let t = self.max_min(true, a, b);
- self.max_min(true, t, acc)
- }
- "v_min3_f16" => {
- let t = self.max_min(false, a, b);
- self.max_min(false, t, acc)
- }
- _ => self.med3(a, b, acc),
- };
- return float_result(self, r);
- }
- if float1.contains(&name) {
- let r = match name {
- "v_cvt_f16_u16" | "v_cvt_f16_i16" => {
- let mut x = self.half_src(inst, 0)?;
- let op = if name == "v_cvt_f16_i16" {
- let k16 = self.s.m.i32(16);
- let t = self.s.m.bin(c, Bin::Shl, x, k16);
- x = self.s.m.bin(c, Bin::AShr, t, k16);
- Cast::SiToFp
- } else {
- Cast::UiToFp
- };
- self.s.m.cast(c, op, x, f32t)
- }
- "v_rcp_f16" => {
- let a = self.half_fsrc(inst, 0)?;
- let one = self.s.m.f32(1.0f32.to_bits());
- self.s.m.bin(c, Bin::SDiv, one, a)
- }
- _ => {
- let a = self.half_fsrc(inst, 0)?;
- let op = match name {
- "v_sqrt_f16" => 24,
- "v_rsq_f16" => 25,
- "v_log_f16" => 23,
- "v_exp_f16" => 21,
- "v_fract_f16" => 22,
- "v_trunc_f16" => 29,
- "v_floor_f16" => 27,
- "v_ceil_f16" => 28,
- "v_rndne_f16" => 26,
- "v_sin_f16" => 13,
- _ => 12,
- };
- match op {
- 12 | 13 => self.trig_turns(a, op == 12),
- // Any f16 in (-2^-11, 0) has fract >= 1 - 2^-12; clamp to 0x3bff (1 - 2^-11).
- 22 => self.fract(a, 1.0 - 1.0 / 4096.0, 1.0 - 1.0 / 2048.0),
- _ => self.unary(op, a),
- }
- }
- };
- return float_result(self, r);
- }
- if matches!(name, "v_cvt_norm_i16_f16" | "v_cvt_norm_u16_f16") {
- if v.clamp || v.omod != 0 {
- return Err("modifiers on a 16-bit integer operation");
- }
- let r = self.cvt_norm16(inst, name == "v_cvt_norm_i16_f16")?;
- return self.write_half(inst, r, guard);
- }
- // 16-bit integers: computed on the low halves, the low 16 bits kept.
- if v.clamp || v.omod != 0 || v.abs != 0 || v.neg != 0 {
- return Err("modifiers on a 16-bit integer operation");
- }
- let r = match name {
- "v_cvt_u16_f16" | "v_cvt_i16_f16" => {
- // Toward zero, saturating at the 16-bit range (infinities
- // included); NaN gives 0. Clamped in f32 first, where every
- // f16 is exact, so the conversion never leaves i32's range.
- let a = self.half_fsrc(inst, 0)?;
- let (lo, hi) = if name == "v_cvt_u16_f16" {
- (0.0f32, 65535.0f32)
- } else {
- (-32768.0, 32767.0)
- };
- let (lo, hi) = (self.s.m.f32(lo.to_bits()), self.s.m.f32(hi.to_bits()));
- let t = self.binary(35, a, lo);
- let t = self.binary(36, t, hi);
- let r = self.s.m.cast(c, Cast::FpToSi, t, i32t);
- let nan = self.s.m.cmp(c, pred::UNO, a, a);
- let z = self.s.m.i32(0);
- self.s.m.select(c, nan, z, r)
- }
- _ => {
- let (mut a, mut b) = (self.half_src(inst, 0)?, self.half_src(inst, 1)?);
- let k16 = self.s.m.i32(16);
- let sext = |l: &mut Self, x: V| {
- let t = l.s.m.bin(c, Bin::Shl, x, k16);
- l.s.m.bin(c, Bin::AShr, t, k16)
- };
- if name.ends_with("_i16") {
- a = sext(self, a);
- b = sext(self, b);
- }
- let k15 = self.s.m.i32(15);
- match name {
- "v_add_nc_u16" => self.s.m.bin(c, Bin::Add, a, b),
- "v_sub_nc_u16" => self.s.m.bin(c, Bin::Sub, a, b),
- "v_subrev_nc_u16" => self.s.m.bin(c, Bin::Sub, b, a),
- "v_mul_lo_u16" => self.s.m.bin(c, Bin::Mul, a, b),
- "v_lshlrev_b16" | "v_lshrrev_b16" | "v_ashrrev_i16" => {
- // src0 is the shift (4 bits), src1 the value.
- let sh = self.s.m.bin(c, Bin::And, a, k15);
- let x = if name == "v_ashrrev_i16" {
- sext(self, b)
- } else {
- b
- };
- let op = match name {
- "v_lshlrev_b16" => Bin::Shl,
- "v_lshrrev_b16" => Bin::LShr,
- _ => Bin::AShr,
- };
- self.s.m.bin(c, op, x, sh)
- }
- _ => {
- let p = match name {
- "v_max_u16" => pred::IUGT,
- "v_min_u16" => pred::IULT,
- "v_max_i16" => pred::SGT,
- _ => pred::SLT,
- };
- let t = self.s.m.cmp(c, p, a, b);
- self.s.m.select(c, t, a, b)
- }
- }
- }
- };
- self.write_half(inst, r, guard)
- }
- /// `v_cvt_norm_i16_f16` / `v_cvt_norm_u16_f16`: the f16 source (with its
- /// abs/neg modifiers) clamped to [-1, 1] or [0, 1], times 32767 or 65535,
- /// rounded to nearest even once from the exact product; NaN gives 0.
- /// Worked on the f16's bits in integers, because in f32 the product
- /// (up to 26 significant bits) would round before the rounding to an
- /// integer.
- fn cvt_norm16(&mut self, inst: &Inst, signed: bool) -> Result<V, Why> {
- let Fields::Valu(v) = &inst.fields else {
- unreachable!()
- };
- let c = self.cur;
- let k = |l: &mut Self, x: u32| l.s.m.i32(x);
- let mut h = self.half_src(inst, 0)?;
- if v.abs & 1 != 0 {
- let m = k(self, 0x7FFF);
- h = self.s.m.bin(c, Bin::And, h, m);
- }
- if v.neg & 1 != 0 {
- let m = k(self, 0x8000);
- h = self.s.m.bin(c, Bin::Xor, h, m);
- }
- let scale = if signed { 0x7FFF } else { 0xFFFF };
- let (k0, k1, k10) = (k(self, 0), k(self, 1), k(self, 10));
- let mag_mask = k(self, 0x7FFF);
- let mag = self.s.m.bin(c, Bin::And, h, mag_mask);
- let k15 = k(self, 15);
- let neg = self.s.m.bin(c, Bin::LShr, h, k15);
- let neg = self.s.m.cmp(c, pred::NE, neg, k0);
- // |x| < 1: x = m * 2^-s with m < 2^11 and 11 <= s <= 24.
- let exp = self.s.m.bin(c, Bin::LShr, mag, k10);
- let man_mask = k(self, 0x3FF);
- let man = self.s.m.bin(c, Bin::And, mag, man_mask);
- let implicit = k(self, 0x400);
- let normal = self.s.m.bin(c, Bin::Or, man, implicit);
- let sub = self.s.m.cmp(c, pred::EQ, exp, k0);
- let m = self.s.m.select(c, sub, man, normal);
- let k25 = k(self, 25);
- let s_norm = self.s.m.bin(c, Bin::Sub, k25, exp);
- let k24 = k(self, 24);
- let sh = self.s.m.select(c, sub, k24, s_norm);
- let ks = k(self, scale);
- let p = self.s.m.bin(c, Bin::Mul, m, ks);
- let q = self.s.m.bin(c, Bin::LShr, p, sh);
- let one_s = self.s.m.bin(c, Bin::Shl, k1, sh);
- let low_mask = self.s.m.bin(c, Bin::Sub, one_s, k1);
- let rem = self.s.m.bin(c, Bin::And, p, low_mask);
- let half = self.s.m.bin(c, Bin::LShr, one_s, k1);
- let above = self.s.m.cmp(c, pred::IUGT, rem, half);
- let tie = self.s.m.cmp(c, pred::EQ, rem, half);
- let odd = self.s.m.bin(c, Bin::And, q, k1);
- let odd = self.s.m.cmp(c, pred::NE, odd, k0);
- let tie_up = self.s.m.bin(c, Bin::And, tie, odd);
- let up = self.s.m.bin(c, Bin::Or, above, tie_up);
- let r = self.s.m.select(c, up, k1, k0);
- let r = self.s.m.bin(c, Bin::Add, q, r);
- // |x| >= 1 (and infinity) saturates; NaN gives 0.
- let one_h = k(self, 0x3C00);
- let big = self.s.m.cmp(c, pred::IUGE, mag, one_h);
- let r = self.s.m.select(c, big, ks, r);
- let inf = k(self, 0x7C00);
- let nan = self.s.m.cmp(c, pred::IUGT, mag, inf);
- let r = self.s.m.select(c, nan, k0, r);
- let r = if signed {
- let n = self.s.m.bin(c, Bin::Sub, k0, r);
- self.s.m.select(c, neg, n, r)
- } else {
- self.s.m.select(c, neg, k0, r)
- };
- let m16 = k(self, 0xFFFF);
- Ok(self.s.m.bin(c, Bin::And, r, m16))
- }
- /// Half `high` of source `k` of a packed (VOP3P) operation, in the low
- /// 16 bits: op_sel picks the half the low result reads, op_sel_hi the
- /// half the high result reads. Packed integer instructions see the f32
- /// bits of an inline float constant.
- fn packed_src(&mut self, inst: &Inst, k: usize, high: bool, float: bool) -> Result<V, Why> {
- let Fields::Valu(v) = &inst.fields else {
- unreachable!()
- };
- let op = *v.src.get(k).ok_or("a missing operand")?;
- let sel = if high { v.opsel_hi } else { v.opsel } >> k & 1 != 0;
- let x = if float {
- self.f16_operand(inst, &op)?
- } else {
- self.operand(inst, &op)?
- };
- let c = self.cur;
- let x = if sel {
- let k16 = self.s.m.i32(16);
- self.s.m.bin(c, Bin::LShr, x, k16)
- } else {
- x
- };
- let m = self.s.m.i32(0xFFFF);
- Ok(self.s.m.bin(c, Bin::And, x, m))
- }
- /// Packed 16-bit operations (`v_pk_*`): each half computed as the
- /// 16-bit operation does, f16 through f32 as in [`Self::valu16`].
- fn packed16(&mut self, inst: &Inst, op: &str, guard: Option<V>) -> Result<(), Why> {
- let Fields::Valu(v) = &inst.fields else {
- unreachable!()
- };
- let float = op.ends_with("_f16");
- let n = match op {
- "fma_f16" | "mad_u16" | "mad_i16" => 3,
- "add_f16" | "mul_f16" | "min_f16" | "max_f16" | "add_u16" | "sub_u16" | "add_i16"
- | "sub_i16" | "mul_lo_u16" | "lshlrev_b16" | "lshrrev_b16" | "ashrrev_i16"
- | "max_u16" | "min_u16" | "max_i16" | "min_i16" => 2,
- _ => return Err("a packed 16-bit operation"),
- };
- if v.omod != 0 || !float && (v.clamp || v.neg != 0 || v.neg_hi != 0) {
- return Err("modifiers on a packed integer operation");
- }
- let c = self.cur;
- let (i32t, f32t) = (self.s.t.i32, self.s.t.f32);
- let mut halves = Vec::new();
- for high in [false, true] {
- let mut xs = Vec::new();
- for k in 0..n {
- let h = self.packed_src(inst, k, high, float)?;
- if float {
- let mut f = self
- .s
- .op(c, "legacyF16ToF32", Attr::ReadNone, f32t, 131, &[h]);
- let neg = if high { v.neg_hi } else { v.neg };
- if neg >> k & 1 != 0 {
- let z = self.s.m.f32(0x8000_0000);
- f = self.s.m.bin(c, Bin::Sub, z, f);
- }
- xs.push(f);
- } else {
- xs.push(h);
- }
- }
- let r = if float {
- let mut f = match op {
- "add_f16" => self.s.m.bin(c, Bin::Add, xs[0], xs[1]),
- "mul_f16" => self.s.m.bin(c, Bin::Mul, xs[0], xs[1]),
- "min_f16" => self.max_min(false, xs[0], xs[1]),
- "max_f16" => self.max_min(true, xs[0], xs[1]),
- // shortcut: FMad in f32, as v_fma_f16.
- _ => self.fmad(xs[0], xs[1], xs[2]),
- };
- if v.clamp {
- f = self.unary(7, f);
- }
- self.s
- .op(c, "legacyF32ToF16", Attr::ReadNone, i32t, 130, &[f])
- } else {
- let k16 = self.s.m.i32(16);
- let sext = |l: &mut Self, x: V| {
- let t = l.s.m.bin(c, Bin::Shl, x, k16);
- l.s.m.bin(c, Bin::AShr, t, k16)
- };
- if op.ends_with("_i16") {
- for x in &mut xs {
- *x = sext(self, *x);
- }
- }
- let k15 = self.s.m.i32(15);
- match op {
- "add_u16" | "add_i16" => self.s.m.bin(c, Bin::Add, xs[0], xs[1]),
- "sub_u16" | "sub_i16" => self.s.m.bin(c, Bin::Sub, xs[0], xs[1]),
- "mul_lo_u16" => self.s.m.bin(c, Bin::Mul, xs[0], xs[1]),
- "mad_u16" | "mad_i16" => {
- let m = self.s.m.bin(c, Bin::Mul, xs[0], xs[1]);
- self.s.m.bin(c, Bin::Add, m, xs[2])
- }
- "lshlrev_b16" | "lshrrev_b16" | "ashrrev_i16" => {
- let sh = self.s.m.bin(c, Bin::And, xs[0], k15);
- let o = match op {
- "lshlrev_b16" => Bin::Shl,
- "lshrrev_b16" => Bin::LShr,
- _ => Bin::AShr,
- };
- let x = if op == "ashrrev_i16" {
- sext(self, xs[1])
- } else {
- xs[1]
- };
- self.s.m.bin(c, o, x, sh)
- }
- _ => {
- let p = match op {
- "max_u16" => pred::IUGT,
- "min_u16" => pred::IULT,
- "max_i16" => pred::SGT,
- _ => pred::SLT,
- };
- let t = self.s.m.cmp(c, p, xs[0], xs[1]);
- self.s.m.select(c, t, xs[0], xs[1])
- }
- }
- };
- let m = self.s.m.i32(0xFFFF);
- halves.push(self.s.m.bin(c, Bin::And, r, m));
- }
- let k16 = self.s.m.i32(16);
- let hi = self.s.m.bin(c, Bin::Shl, halves[1], k16);
- let r = self.s.m.bin(c, Bin::Or, halves[0], hi);
- let Some(Operand::Vector { reg, .. }) = v.dst else {
- return Err("a packed result outside a VGPR");
- };
- self.write(inst, Reg::V(reg), r, guard);
- Ok(())
- }
- fn integer(&mut self, inst: &Inst, name: &'static str) -> Result<V, Why> {
- let c = self.cur;
- let s = |l: &mut Self, k| l.src(inst, k);
- let k = |l: &mut Self, x: u32| l.s.m.i32(x);
- let m31 = |l: &mut Self, x: V| {
- let k31 = l.s.m.i32(31);
- l.s.m.bin(c, Bin::And, x, k31)
- };
- Ok(match name {
- "v_and_b32" | "v_or_b32" | "v_xor_b32" | "v_add_nc_u32" | "v_sub_nc_u32"
- | "v_subrev_nc_u32" | "v_mul_lo_u32" => {
- let (a, b) = (s(self, 0)?, s(self, 1)?);
- let (op, a, b) = match name {
- "v_and_b32" => (Bin::And, a, b),
- "v_or_b32" => (Bin::Or, a, b),
- "v_xor_b32" => (Bin::Xor, a, b),
- "v_add_nc_u32" => (Bin::Add, a, b),
- "v_sub_nc_u32" => (Bin::Sub, a, b),
- "v_subrev_nc_u32" => (Bin::Sub, b, a),
- _ => (Bin::Mul, a, b),
- };
- self.s.m.bin(c, op, a, b)
- }
- "v_xnor_b32" => {
- let (a, b) = (s(self, 0)?, s(self, 1)?);
- let x = self.s.m.bin(c, Bin::Xor, a, b);
- let ones = k(self, u32::MAX);
- self.s.m.bin(c, Bin::Xor, x, ones)
- }
- "v_not_b32" => {
- let a = s(self, 0)?;
- let ones = k(self, u32::MAX);
- self.s.m.bin(c, Bin::Xor, a, ones)
- }
- "v_lshlrev_b32" | "v_lshrrev_b32" | "v_ashrrev_i32" => {
- let (sh, a) = (s(self, 0)?, s(self, 1)?);
- let sh = m31(self, sh);
- let op = match name {
- "v_lshlrev_b32" => Bin::Shl,
- "v_lshrrev_b32" => Bin::LShr,
- _ => Bin::AShr,
- };
- self.s.m.bin(c, op, a, sh)
- }
- "v_lshl_add_u32" | "v_lshl_or_b32" | "v_add_lshl_u32" | "v_and_or_b32"
- | "v_or3_b32" | "v_xad_u32" | "v_add3_u32" | "v_xor3_b32" => {
- let (a, b, d) = (s(self, 0)?, s(self, 1)?, s(self, 2)?);
- match name {
- "v_lshl_add_u32" | "v_lshl_or_b32" => {
- let b = m31(self, b);
- let t = self.s.m.bin(c, Bin::Shl, a, b);
- let op = if name == "v_lshl_add_u32" {
- Bin::Add
- } else {
- Bin::Or
- };
- self.s.m.bin(c, op, t, d)
- }
- "v_add_lshl_u32" => {
- let t = self.s.m.bin(c, Bin::Add, a, b);
- let d = m31(self, d);
- self.s.m.bin(c, Bin::Shl, t, d)
- }
- _ => {
- let (o1, o2) = match name {
- "v_and_or_b32" => (Bin::And, Bin::Or),
- "v_or3_b32" => (Bin::Or, Bin::Or),
- "v_xad_u32" => (Bin::Xor, Bin::Add),
- "v_add3_u32" => (Bin::Add, Bin::Add),
- _ => (Bin::Xor, Bin::Xor),
- };
- let t = self.s.m.bin(c, o1, a, b);
- self.s.m.bin(c, o2, t, d)
- }
- }
- }
- "v_mul_u32_u24" | "v_mul_i32_i24" => {
- let (a, b) = (s(self, 0)?, s(self, 1)?);
- let (sh, op) = (
- k(self, 8),
- if name == "v_mul_i32_i24" {
- Bin::AShr
- } else {
- Bin::LShr
- },
- );
- let a = self.s.m.bin(c, Bin::Shl, a, sh);
- let a = self.s.m.bin(c, op, a, sh);
- let b = self.s.m.bin(c, Bin::Shl, b, sh);
- let b = self.s.m.bin(c, op, b, sh);
- self.s.m.bin(c, Bin::Mul, a, b)
- }
- "v_bfe_u32" | "v_bfe_i32" => {
- let (a, off, w) = (s(self, 0)?, s(self, 1)?, s(self, 2)?);
- let w = m31(self, w);
- self.bfe(a, off, w, name == "v_bfe_i32")
- }
- "v_bfi_b32" => {
- let (m, a, b) = (s(self, 0)?, s(self, 1)?, s(self, 2)?);
- let ones = k(self, u32::MAX);
- let nm = self.s.m.bin(c, Bin::Xor, m, ones);
- let x = self.s.m.bin(c, Bin::And, m, a);
- let y = self.s.m.bin(c, Bin::And, nm, b);
- self.s.m.bin(c, Bin::Or, x, y)
- }
- "v_max_i32" | "v_min_i32" | "v_max_u32" | "v_min_u32" => {
- let (a, b) = (s(self, 0)?, s(self, 1)?);
- let p = match name {
- "v_max_i32" => pred::SGT,
- "v_min_i32" => pred::SLT,
- "v_max_u32" => pred::IUGT,
- _ => pred::IULT,
- };
- let t = self.s.m.cmp(c, p, a, b);
- self.s.m.select(c, t, a, b)
- }
- "v_mul_hi_u32" | "v_mul_hi_i32" => {
- let (a, b) = (s(self, 0)?, s(self, 1)?);
- self.mul_hi(a, b, name == "v_mul_hi_i32")
- }
- "v_alignbit_b32" | "v_alignbyte_b32" => {
- // The low dword of {a, b} >> shift.
- let (a, b, sh) = (s(self, 0)?, s(self, 1)?, s(self, 2)?);
- let sh = if name == "v_alignbyte_b32" {
- let three = k(self, 3);
- let t = self.s.m.bin(c, Bin::And, sh, three);
- let k3 = k(self, 3);
- self.s.m.bin(c, Bin::Shl, t, k3)
- } else {
- m31(self, sh)
- };
- let k32 = k(self, 32);
- let lo = self.s.m.bin(c, Bin::LShr, b, sh);
- let left = self.s.m.bin(c, Bin::Sub, k32, sh);
- let left = m31(self, left);
- let hi = self.s.m.bin(c, Bin::Shl, a, left);
- let zero = k(self, 0);
- let none = self.s.m.cmp(c, pred::EQ, sh, zero);
- let hi = self.s.m.select(c, none, zero, hi);
- self.s.m.bin(c, Bin::Or, lo, hi)
- }
- "v_bfrev_b32" => {
- let a = s(self, 0)?;
- self.s
- .op(c, "unary.i32", Attr::ReadNone, self.s.t.i32, 30, &[a])
- }
- "v_bcnt_u32_b32" => {
- let (a, b) = (s(self, 0)?, s(self, 1)?);
- let n = self
- .s
- .op(c, "unaryBits.i32", Attr::ReadNone, self.s.t.i32, 31, &[a]);
- self.s.m.bin(c, Bin::Add, n, b)
- }
- "v_mad_u32_u24" | "v_mad_i32_i24" => {
- let (a, b, d) = (s(self, 0)?, s(self, 1)?, s(self, 2)?);
- let (sh, op) = (
- k(self, 8),
- if name == "v_mad_i32_i24" {
- Bin::AShr
- } else {
- Bin::LShr
- },
- );
- let a = self.s.m.bin(c, Bin::Shl, a, sh);
- let a = self.s.m.bin(c, op, a, sh);
- let b = self.s.m.bin(c, Bin::Shl, b, sh);
- let b = self.s.m.bin(c, op, b, sh);
- let m = self.s.m.bin(c, Bin::Mul, a, b);
- self.s.m.bin(c, Bin::Add, m, d)
- }
- "v_min3_i32" | "v_max3_i32" | "v_min3_u32" | "v_max3_u32" | "v_med3_i32"
- | "v_med3_u32" => {
- let (a, b, d) = (s(self, 0)?, s(self, 1)?, s(self, 2)?);
- let signed = name.ends_with("i32");
- let (lt, gt) = if signed {
- (pred::SLT, pred::SGT)
- } else {
- (pred::IULT, pred::IUGT)
- };
- let pick = |l: &mut Self, p, x, y| {
- let t = l.s.m.cmp(c, p, x, y);
- l.s.m.select(c, t, x, y)
- };
- match &name[2..6] {
- "min3" => {
- let t = pick(self, lt, a, b);
- pick(self, lt, t, d)
- }
- "max3" => {
- let t = pick(self, gt, a, b);
- pick(self, gt, t, d)
- }
- _ => {
- let lo = pick(self, lt, a, b);
- let hi = pick(self, gt, a, b);
- let t = pick(self, lt, hi, d);
- pick(self, gt, lo, t)
- }
- }
- }
- "v_perm_b32" => {
- // Byte i of the result is byte S of {src0, src1} (src1 the
- // low dword) for selector byte S below 8; 8-11 repeat the
- // sign of bytes 1 and 3 of src1, then of src0; 12 is 0x00,
- // higher 0xFF.
- let Fields::Valu(v) = &inst.fields else {
- unreachable!()
- };
- let sel = match v.src.get(2) {
- Some(&Operand::Literal(x)) => x,
- Some(&Operand::Const(k)) => crate::resource::inline(k).ok_or(name)?,
- _ => {
- // A computed selector: each byte picked at run time.
- let (a, b, sv) = (s(self, 0)?, s(self, 1)?, s(self, 2)?);
- let mut r = k(self, 0);
- for i in 0..4u32 {
- let (sh, ff) = (k(self, 8 * i), k(self, 0xFF));
- let sel = self.s.m.bin(c, Bin::LShr, sv, sh);
- let sel = self.s.m.bin(c, Bin::And, sel, ff);
- // Select a dword and a byte within it.
- // Later selects handle codes 8 and above.
- let four = k(self, 4);
- let zero = k(self, 0);
- let high = self.s.m.bin(c, Bin::And, sel, four);
- let high = self.s.m.cmp(c, pred::NE, high, zero);
- let word = self.s.m.select(c, high, a, b);
- let three = k(self, 3);
- let index = self.s.m.bin(c, Bin::And, sel, three);
- let bits = self.s.m.bin(c, Bin::Shl, index, three);
- let x = self.s.m.bin(c, Bin::LShr, word, bits);
- let mut byte = self.s.m.bin(c, Bin::And, x, ff);
- // 8-11: the sign of bytes 1, 3 of src1, then of src0.
- for (n, src, bit) in [(8, b, 15), (9, b, 31), (10, a, 15), (11, a, 31)]
- {
- let left = k(self, 31 - bit);
- let t = self.s.m.bin(c, Bin::Shl, src, left);
- let k31 = k(self, 31);
- let t = self.s.m.bin(c, Bin::AShr, t, k31);
- let t = self.s.m.bin(c, Bin::And, t, ff);
- let kn = k(self, n);
- let is = self.s.m.cmp(c, pred::EQ, sel, kn);
- byte = self.s.m.select(c, is, t, byte);
- }
- let (k12, z) = (k(self, 12), k(self, 0));
- let is12 = self.s.m.cmp(c, pred::EQ, sel, k12);
- byte = self.s.m.select(c, is12, z, byte);
- let above = self.s.m.cmp(c, pred::IUGT, sel, k12);
- byte = self.s.m.select(c, above, ff, byte);
- let at = k(self, 8 * i);
- let byte = self.s.m.bin(c, Bin::Shl, byte, at);
- r = self.s.m.bin(c, Bin::Or, r, byte);
- }
- return Ok(r);
- }
- };
- let (a, b) = (s(self, 0)?, s(self, 1)?);
- let mut r = k(self, 0);
- for i in 0..4 {
- let pick = sel >> (8 * i) & 0xFF;
- let byte = match pick {
- 0..=7 => {
- let (src, n) = if pick < 4 { (b, pick) } else { (a, pick - 4) };
- let sh = k(self, 8 * n);
- let t = self.s.m.bin(c, Bin::LShr, src, sh);
- let ff = k(self, 0xFF);
- self.s.m.bin(c, Bin::And, t, ff)
- }
- 8..=11 => {
- let src = if pick < 10 { b } else { a };
- let bit = if pick % 2 == 0 { 15 } else { 31 };
- let left = k(self, 31 - bit);
- let t = self.s.m.bin(c, Bin::Shl, src, left);
- let k31 = k(self, 31);
- let t = self.s.m.bin(c, Bin::AShr, t, k31);
- let ff = k(self, 0xFF);
- self.s.m.bin(c, Bin::And, t, ff)
- }
- 12 => continue,
- _ => k(self, 0xFF),
- };
- let at = k(self, 8 * i);
- let byte = self.s.m.bin(c, Bin::Shl, byte, at);
- r = self.s.m.bin(c, Bin::Or, r, byte);
- }
- r
- }
- "v_bfm_b32" => {
- // ((1 << src0[4:0]) - 1) << src1[4:0]
- let (a, b) = (s(self, 0)?, s(self, 1)?);
- let one = k(self, 1);
- let (a, b) = (m31(self, a), m31(self, b));
- let m = self.s.m.bin(c, Bin::Shl, one, a);
- let m = self.s.m.bin(c, Bin::Sub, m, one);
- self.s.m.bin(c, Bin::Shl, m, b)
- }
- "v_cvt_pk_u8_f32" => {
- // src0 rounded to nearest even and saturated to a byte, into
- // byte src1[1:0] of src2.
- let f = self.fsrc(inst, 0)?;
- let (b, d) = (s(self, 1)?, s(self, 2)?);
- let f = self.unary(26, f);
- let (zero, max) = (self.s.m.f32(0), self.s.m.f32(255.0f32.to_bits()));
- let f = self.binary(35, f, zero);
- let f = self.binary(36, f, max);
- let x = self.s.m.cast(c, Cast::FpToUi, f, self.s.t.i32);
- let (three, k3) = (k(self, 3), k(self, 3));
- let sh = self.s.m.bin(c, Bin::And, b, three);
- let sh = self.s.m.bin(c, Bin::Shl, sh, k3);
- let x = self.s.m.bin(c, Bin::Shl, x, sh);
- let ff = k(self, 0xFF);
- let hole = self.s.m.bin(c, Bin::Shl, ff, sh);
- let ones = k(self, u32::MAX);
- let hole = self.s.m.bin(c, Bin::Xor, hole, ones);
- let d = self.s.m.bin(c, Bin::And, d, hole);
- self.s.m.bin(c, Bin::Or, d, x)
- }
- "v_ffbl_b32" => {
- // -1 for zero, as DXIL's first-bit-low.
- let a = s(self, 0)?;
- self.s
- .op(c, "unaryBits.i32", Attr::ReadNone, self.s.t.i32, 32, &[a])
- }
- "v_mbcnt_lo_u32_b32" | "v_mbcnt_hi_u32_b32" => {
- // Bits of the mask's low (high) half below this lane, plus src1:
- // with an all-ones mask, the lane index.
- let (a, b) = (s(self, 0)?, s(self, 1)?);
- // Mesh lanes are the console's by construction; pixel and
- // vertex lanes are the host's, as their masks are.
- if self.mask_data == MaskData::Host {
- // Host lanes: the mask must be one (a ballot) or all lanes.
- let Fields::Valu(v) = &inst.fields else {
- unreachable!()
- };
- let host = match v.src.first() {
- Some(&Operand::Const(193)) => true,
- Some(&Operand::Scalar { code, .. }) => {
- inst.read(Reg::S(code)).is_some_and(|m| {
- self.x.st.masks.contains_key(&m) || self.x.st.highs.contains_key(&m)
- })
- }
- _ => false,
- };
- if !host {
- return Err("v_mbcnt of a mask that is no ballot (console lane layout)");
- }
- self.s.flags |= flags::WAVE_OPS;
- } else if self.s.mesh.is_none() {
- self.cross_lane(false)?;
- }
- let lane = self.lane_index();
- let below = if name == "v_mbcnt_lo_u32_b32" {
- lane
- } else {
- let (k32, z) = (k(self, 32), k(self, 0));
- let t = self.s.m.bin(c, Bin::Sub, lane, k32);
- let low = self.s.m.cmp(c, pred::IULT, lane, k32);
- self.s.m.select(c, low, z, t)
- };
- // (1 << below) - 1, all ones from 32 on.
- let (one, k32, ones) = (k(self, 1), k(self, 32), k(self, u32::MAX));
- let sh = m31(self, below);
- let m = self.s.m.bin(c, Bin::Shl, one, sh);
- let m = self.s.m.bin(c, Bin::Sub, m, one);
- let full = self.s.m.cmp(c, pred::IUGE, below, k32);
- let m = self.s.m.select(c, full, ones, m);
- let x = self.s.m.bin(c, Bin::And, a, m);
- let n = self
- .s
- .op(c, "unaryBits.i32", Attr::ReadNone, self.s.t.i32, 31, &[x]);
- self.s.m.bin(c, Bin::Add, n, b)
- }
- _ => return Err(name),
- })
- }
- fn compare(&mut self, inst: &Inst, rest: &str) -> Result<(), Why> {
- let (op, ty) = rest.rsplit_once('_').ok_or("a compare")?;
- let c = self.cur;
- let r = match ty {
- "f32" | "f16" if op == "class" => {
- // Bit k of src1 tests class k: signalling NaN, quiet NaN,
- // -inf, -normal, -denormal, -0, +0, +denormal, +normal, +inf.
- let f16 = ty == "f16";
- let x = if f16 {
- let Fields::Valu(v) = &inst.fields else {
- unreachable!()
- };
- let mut h = self.half_src(inst, 0)?;
- if v.abs & 1 != 0 {
- let m = self.s.m.i32(0x7FFF);
- h = self.s.m.bin(c, Bin::And, h, m);
- }
- if v.neg & 1 != 0 {
- let s = self.s.m.i32(0x8000);
- h = self.s.m.bin(c, Bin::Xor, h, s);
- }
- h
- } else {
- let x = self.fsrc(inst, 0)?;
- self.as_int(x)
- };
- let mask = self.src(inst, 1)?;
- let k = |l: &mut Self, v: u32| l.s.m.i32(v);
- let (exp_sh, sign_sh, exp_mask, mant_mask, quiet_bit) = if f16 {
- (10, 15, 0x1F, 0x03FF, 0x0200)
- } else {
- (23, 31, 0xFF, 0x7F_FFFF, 0x40_0000)
- };
- let (k_exp, k_sign, k_emax, mant, quiet) = (
- k(self, exp_sh),
- k(self, sign_sh),
- k(self, exp_mask),
- k(self, mant_mask),
- k(self, quiet_bit),
- );
- let e = self.s.m.bin(c, Bin::LShr, x, k_exp);
- let e = self.s.m.bin(c, Bin::And, e, k_emax);
- let m = self.s.m.bin(c, Bin::And, x, mant);
- let neg = self.s.m.bin(c, Bin::LShr, x, k_sign);
- let zero = k(self, 0);
- let (e0, emax) = (
- self.s.m.cmp(c, pred::EQ, e, zero),
- self.s.m.cmp(c, pred::EQ, e, k_emax),
- );
- let m0 = self.s.m.cmp(c, pred::EQ, m, zero);
- let q = self.s.m.bin(c, Bin::And, x, quiet);
- let q = self.s.m.cmp(c, pred::NE, q, zero);
- // Positive classes 6-9; negative ones are 11 minus those.
- let (normal, inf, zero_c, denorm) =
- (k(self, 8), k(self, 9), k(self, 6), k(self, 7));
- let t = self.s.m.select(c, m0, zero_c, denorm);
- let t = self.s.m.select(c, e0, t, normal);
- let t = self.s.m.select(c, emax, inf, t);
- let eleven = k(self, 11);
- let mirrored = self.s.m.bin(c, Bin::Sub, eleven, t);
- let is_neg = self.s.m.cmp(c, pred::NE, neg, zero);
- let t = self.s.m.select(c, is_neg, mirrored, t);
- let (one, snan) = (k(self, 1), k(self, 0));
- let nan_class = self.s.m.select(c, q, one, snan);
- let yes = self.s.m.bool(true);
- let nan = self.s.m.bin(c, Bin::Xor, m0, yes);
- let nan = self.s.m.bin(c, Bin::And, nan, emax);
- let t = self.s.m.select(c, nan, nan_class, t);
- let bit = self.s.m.bin(c, Bin::LShr, mask, t);
- let bit = self.s.m.bin(c, Bin::And, bit, one);
- self.s.m.cmp(c, pred::NE, bit, zero)
- }
- "f32" | "f16" => {
- let (a, b) = if ty == "f16" {
- (self.half_fsrc(inst, 0)?, self.half_fsrc(inst, 1)?)
- } else {
- (self.fsrc(inst, 0)?, self.fsrc(inst, 1)?)
- };
- if op == "lg" {
- // Ordered and not equal, false for a NaN, as `olt | ogt`.
- // `fcmp one` means the same, but the AMD D3D12 driver
- // runs it as unordered (true for a NaN) where NVIDIA
- // and WARP do not; olt and ogt agree on all three.
- let lt = self.s.m.cmp(c, pred::OLT, a, b);
- let gt = self.s.m.cmp(c, pred::OGT, a, b);
- self.s.m.bin(c, Bin::Or, lt, gt)
- } else {
- let p = match op {
- "f" => pred::FALSE,
- "lt" => pred::OLT,
- "eq" => pred::OEQ,
- "le" => pred::OLE,
- "gt" => pred::OGT,
- "ge" => pred::OGE,
- "o" => pred::ORD,
- "u" => pred::UNO,
- "nge" => pred::ULT,
- "nlg" => pred::UEQ,
- "ngt" => pred::ULE,
- "nle" => pred::UGT,
- "neq" => pred::UNE,
- "nlt" => pred::UGE,
- "tru" => pred::TRUE,
- _ => return Err(inst.name()),
- };
- self.s.m.cmp(c, p, a, b)
- }
- }
- "u64" | "i64" => {
- let (a, b) = (self.src64(inst, 0)?, self.src64(inst, 1)?);
- let signed = ty == "i64";
- let p = match op {
- "eq" => pred::EQ,
- "ne" | "lg" => pred::NE,
- "lt" if signed => pred::SLT,
- "le" if signed => pred::SLE,
- "gt" if signed => pred::SGT,
- "ge" if signed => pred::SGE,
- "lt" => pred::IULT,
- "le" => pred::IULE,
- "gt" => pred::IUGT,
- "ge" => pred::IUGE,
- _ => return Err("a 64-bit compare"),
- };
- self.s.m.cmp(c, p, a, b)
- }
- "u32" | "i32" | "u16" | "i16" => {
- let (a, b) = if ty.ends_with("16") {
- let (mut a, mut b) = (self.half_src(inst, 0)?, self.half_src(inst, 1)?);
- if ty == "i16" {
- let k16 = self.s.m.i32(16);
- for x in [&mut a, &mut b] {
- let t = self.s.m.bin(c, Bin::Shl, *x, k16);
- *x = self.s.m.bin(c, Bin::AShr, t, k16);
- }
- }
- (a, b)
- } else {
- (self.src(inst, 0)?, self.src(inst, 1)?)
- };
- let signed = ty == "i32" || ty == "i16";
- let p = match op {
- "eq" => pred::EQ,
- "ne" | "lg" => pred::NE,
- "lt" if signed => pred::SLT,
- "le" if signed => pred::SLE,
- "gt" if signed => pred::SGT,
- "ge" if signed => pred::SGE,
- "lt" => pred::IULT,
- "le" => pred::IULE,
- "gt" => pred::IUGT,
- "ge" => pred::IUGE,
- "f" | "t" => {
- self.lane(inst, Some(op == "t"));
- return Ok(());
- }
- _ => return Err("an integer compare"),
- };
- self.s.m.cmp(c, p, a, b)
- }
- _ => return Err(inst.name()),
- };
- self.lane_bits_for(inst, r);
- Ok(())
- }
- fn lane(&mut self, inst: &Inst, value: Option<bool>) {
- let b = self.s.m.bool(value == Some(true));
- self.lane_bits_for(inst, b);
- }
- fn lane_bits_for(&mut self, inst: &Inst, r: V) {
- let Fields::Valu(v) = &inst.fields else {
- return;
- };
- let code = if inst.name().starts_with("v_cmpx") {
- Some(EXEC)
- } else {
- match v.dst {
- Some(Operand::Scalar { code, .. }) => Some(code),
- _ => None,
- }
- };
- if let Some(lo) = code.and_then(|c| inst.write(Reg::S(c))) {
- self.lane_bits.insert(lo, r);
- }
- for &(_, w) in &inst.writes {
- self.vals
- .entry(w)
- .or_insert(Val::Missing("a lane mask read as data"));
- }
- }
- fn interp(&mut self, inst: &Inst, site: Site) -> Result<(), Why> {
- let Some(&interp) = self.interps.get(&site) else {
- return Err("the first half of an interpolation");
- };
- let (attribute, mode) = match interp {
- Interp::Interpolated {
- attribute,
- barycentric,
- ..
- } => (
- attribute,
- match barycentric {
- Barycentric::PerspectiveCenter => 2,
- Barycentric::PerspectiveCentroid => 3,
- Barycentric::PerspectiveSample => 6,
- Barycentric::LinearCenter => 4,
- Barycentric::LinearCentroid => 5,
- Barycentric::LinearSample => 7,
- Barycentric::PerspectivePullModel => return Err("pull-model interpolation"),
- },
- ),
- // P0, the provoking vertex: a flat (nointerpolation) input.
- Interp::Vertex {
- attribute,
- vertex: 2,
- } => (attribute, 1),
- Interp::Vertex { .. } => return Err("a non-provoking vertex value"),
- };
- let dst = match &inst.fields {
- Fields::Vintrp { dst, .. } => *dst,
- Fields::Valu(v) => v.dst.ok_or("an interpolation")?,
- _ => unreachable!(),
- };
- let Operand::Vector { reg, .. } = dst else {
- return Err("an interpolation");
- };
- // A channel of an input element, interpolated as `mode` says.
- let channel = |l: &mut Self, row: u8, ch: u8| -> Result<V, Why> {
- let (id, col) = l
- .find_input(row, ch)
- .ok_or("an input the signature lacks")?;
- let e = &mut l.s.inputs[id];
- if e.interp != mode && e.usage != 0 {
- return Err("one input interpolated two ways");
- }
- e.interp = mode;
- match l.load_any_input(id, col) {
- Val::I32(x) => Ok(x),
- _ => Err("an input"),
- }
- };
- let packing = match &self.x.native.io {
- StageIo::Ps { inputs, .. } => inputs
- .iter()
- .find(|i| i.row == attribute.row && i.fp16)
- .map(|i| (i.pack2, (i.row2 != i.row).then_some(i.row2))),
- _ => None,
- };
- let f16 = inst.name().ends_with("_f16");
- let x = match packing {
- None if f16 || attribute.high => return Err("a 16-bit read of a 32-bit input"),
- None => channel(self, attribute.row, attribute.channel)?,
- Some((pack2, b)) => {
- // 16-bit values: one in the low half of each dword, or two
- // per dword (`packed_half`); the host's are 32-bit floats.
- let half = |l: &mut Self, h: u8| -> Result<Option<V>, Why> {
- let src = if pack2 {
- packed_half(&l.s.inputs, attribute.row, b, h)
- } else {
- h.is_multiple_of(2).then_some((attribute.row, h / 2))
- };
- let Some((row, ch)) = src else {
- return Ok(None);
- };
- let x = channel(l, row, ch)?;
- let f = l.as_float(x);
- let i32t = l.s.t.i32;
- Ok(Some(l.s.op(
- l.cur,
- "legacyF32ToF16",
- Attr::ReadNone,
- i32t,
- 130,
- &[f],
- )))
- };
- let h = 2 * attribute.channel;
- let c = self.cur;
- if f16 {
- let lo = half(self, h + u8::from(attribute.high))?
- .ok_or("a packed input half no element holds")?;
- self.low_half(inst, reg, lo)
- } else if mode == 1 {
- // The whole dword of a flat input: both halves.
- let z = self.s.m.i32(0);
- let lo = half(self, h)?.unwrap_or(z);
- let hi = half(self, h + 1)?.unwrap_or(z);
- let k16 = self.s.m.i32(16);
- let hi = self.s.m.bin(c, Bin::Shl, hi, k16);
- self.s.m.bin(c, Bin::Or, lo, hi)
- } else {
- return Err("32-bit interpolation of a 16-bit input");
- }
- }
- };
- // A pixel interpolates in every lane; guards only hide inactive ones.
- self.write(inst, Reg::V(reg), x, None);
- Ok(())
- }
- /// `x` (an f16 in the low bits) as the low half of VGPR `reg`: 16-bit
- /// results keep the register's high half (GFX10).
- fn low_half(&mut self, inst: &Inst, reg: u8, x: V) -> V {
- let c = self.cur;
- let old = match inst.read(Reg::V(reg)).map(|o| self.i32_of(o)) {
- Some(Ok(o)) => o,
- _ => self.s.m.i32(0),
- };
- let (high, low) = (self.s.m.i32(0xFFFF_0000), self.s.m.i32(0xFFFF));
- let old = self.s.m.bin(c, Bin::And, old, high);
- let x = self.s.m.bin(c, Bin::And, x, low);
- self.s.m.bin(c, Bin::Or, old, x)
- }
- fn image(&mut self, inst: &Inst, b: usize, i: usize) -> Result<(), Why> {
- let Fields::Image {
- data,
- addr,
- dmask,
- dim,
- unorm,
- a16,
- d16,
- tfe,
- lwe,
- ..
- } = &inst.fields
- else {
- unreachable!()
- };
- if *tfe || *lwe {
- return Err("an image access with tfe or lwe");
- }
- let name = inst.name();
- if *a16 && name.starts_with("image_sample_b") {
- return Err("a biased image sample with 16-bit coordinates");
- }
- // A load or sample with no channel enabled returns the first.
- let dmask = if *dmask == 0 { &1 } else { dmask };
- let site = (b, i);
- let guard = self.guard(b, i)?;
- let c = self.cur;
- let (kind, coords) = match dim {
- 0 => (1, 1),
- 1 => (2, 2),
- 2 => (4, 3),
- // Sampled (face coordinates and face, see `cube_direction`) or
- // sized only.
- 3 if name.starts_with("image_sample")
- || name.starts_with("image_gather")
- || name == "image_get_resinfo" =>
- {
- (5, 3)
- }
- 4 => (6, 2),
- 5 => (7, 3),
- _ => return Err("an image dimension (cube loads or MSAA)"),
- };
- // Address VGPRs in order.
- let mut regs = Vec::new();
- for a in addr {
- if let Operand::Vector { reg, dwords } = *a {
- regs.extend((0..dwords).map(|k| reg + k));
- }
- }
- // With a16, two 16-bit components per VGPR, low half first.
- let addr_val = |l: &mut Self, k: usize| -> Result<V, Why> {
- let r = *regs
- .get(if *a16 { k / 2 } else { k })
- .ok_or("an image address")?;
- let v = inst.read(Reg::V(r)).ok_or("an image address")?;
- let x = l.i32_of(v)?;
- if !*a16 {
- return Ok(x);
- }
- let c = l.cur;
- let (sh, mask) = (l.s.m.i32(16 * (k as u32 % 2)), l.s.m.i32(0xFFFF));
- let x = l.s.m.bin(c, Bin::LShr, x, sh);
- Ok(l.s.m.bin(c, Bin::And, x, mask))
- };
- let src = self
- .descriptors
- .get(&(site, false))
- .copied()
- .ok_or("an untracked image")?;
- let store = name.starts_with("image_store");
- let uav = store
- || self
- .used_kind(src)
- .is_some_and(|u| u.kind == ResourceType::Uav);
- let props = (kind | if uav { 0x1000 } else { 0 }, 0x409);
- if let Some(op) = name.strip_prefix("image_atomic_") {
- // DXIL's atomic operations on a 32-bit integer texture UAV; the
- // old value returns with glc. Sub is the add of the negation.
- let code = match op {
- "add" | "sub" => 0,
- "and" => 1,
- "or" => 2,
- "xor" => 3,
- "smin" => 4,
- "smax" => 5,
- "umin" => 6,
- "umax" => 7,
- "swap" => 8,
- "cmpswap" => u32::MAX,
- _ => return Err(name),
- };
- let Fields::Image { cache, .. } = &inst.fields else {
- unreachable!()
- };
- if *d16 || *a16 {
- return Err("a 16-bit image atomic");
- }
- // dmask: the data dwords (cmpswap: twice the value's).
- // shortcut: 64-bit image atomics (two dwords, an R32G32 view)
- // need the host's 64-bit typed atomics; they stop the program.
- if *dmask != if code == u32::MAX { 3 } else { 1 } {
- return Err("a 64-bit image atomic");
- }
- let iu = self.s.m.undef(self.s.t.i32);
- let mut cs = vec![iu; 3];
- for (k, slot) in cs.iter_mut().enumerate().take(coords) {
- *slot = addr_val(self, k)?;
- }
- let Operand::Vector { reg, .. } = *data else {
- return Err("image atomic data");
- };
- let vals = self.vgprs(inst, data, if code == u32::MAX { 2 } else { 1 })?;
- let mut value = vals[0];
- if op == "sub" {
- let z = self.s.m.i32(0);
- value = self.s.m.bin(c, Bin::Sub, z, value);
- }
- let old = match inst.read(Reg::V(reg)) {
- Some(v) if cache.glc => self.i32_of(v)?,
- _ => self.s.m.i32(0),
- };
- let h = self.handle(site, src, false, (kind | 0x1000, 0x105))?;
- let at = self.begin_guarded(guard);
- let i32t = self.s.t.i32;
- let mut args = vec![h];
- let r = if code == u32::MAX {
- // cmpswap: data is (new, compare).
- args.extend(cs);
- args.extend([vals[1], vals[0]]);
- self.s.op(
- self.cur,
- "atomicCompareExchange.i32",
- Attr::NoUnwind,
- i32t,
- 79,
- &args,
- )
- } else {
- args.push(self.s.m.i32(code));
- args.extend(cs);
- args.push(value);
- self.s
- .op(self.cur, "atomicBinOp.i32", Attr::NoUnwind, i32t, 78, &args)
- };
- let r = self.end_guarded(at, &[(r, old)])[0];
- if cache.glc {
- self.write(inst, Reg::V(reg), r, None);
- }
- return Ok(());
- }
- if store {
- if name != "image_store" {
- return Err("an image store to a mip level");
- }
- // shortcut: stores are taken to write all four channels of a
- // float format (the console converts by the view's format).
- if *dmask != 0xF {
- return Err("an image store of some channels");
- }
- let iu = self.s.m.undef(self.s.t.i32);
- let mut cs = vec![iu; 3];
- for (k, slot) in cs.iter_mut().enumerate().take(coords) {
- *slot = addr_val(self, k)?;
- }
- let Operand::Vector { reg, .. } = *data else {
- return Err("image store data");
- };
- let mut vals = Vec::new();
- for k in 0..4u8 {
- // d16: two f16 channels per VGPR, low half first.
- let r = if *d16 { k / 2 } else { k };
- let v = inst.read(Reg::V(reg + r)).ok_or("image store data")?;
- vals.push(if *d16 {
- let mut x = self.i32_of(v)?;
- if k % 2 == 1 {
- let k16 = self.s.m.i32(16);
- x = self.s.m.bin(self.cur, Bin::LShr, x, k16);
- }
- let f32t = self.s.t.f32;
- self.s
- .op(self.cur, "legacyF16ToF32", Attr::ReadNone, f32t, 131, &[x])
- } else {
- self.float_of(v)?
- });
- }
- let h = self.handle(site, src, false, props)?;
- let at = self.begin_guarded(guard);
- let mut args = vec![h];
- args.extend(cs);
- args.extend(vals);
- args.push(self.s.m.i8(0xF));
- let void = self.s.t.void;
- self.s.op(
- self.cur,
- "textureStore.f32",
- Attr::NoUnwind,
- void,
- 67,
- &args,
- );
- self.end_guarded(at, &[]);
- return Ok(());
- }
- let (op, sample) = match name {
- "image_sample" | "image_sample_o" => (60, true),
- "image_sample_lz" | "image_sample_l" => (62, true),
- "image_sample_b" => (61, true),
- "image_sample_c_lz" => (65, true),
- "image_sample_d" => (63, true),
- "image_get_lod" if self.s.pixel => (81, true),
- // Implicit derivatives: pixel shaders only.
- "image_sample_c" if self.s.pixel => (64, true),
- "image_gather4" | "image_gather4_lz" | "image_gather4_lz_o" => (73, true),
- "image_load" | "image_load_mip" => (66, false),
- "image_get_resinfo" => (72, false),
- _ => return Err(name),
- };
- if op == 72 {
- // Width, height, depth or layers, levels (per dmask) of the level
- // in the first address: DXIL's dimensions; a channel DXIL leaves
- // out reads 1 (UAVs: one level).
- let h = self.handle(site, src, false, props)?;
- let mip = if uav {
- self.s.m.undef(self.s.t.i32)
- } else {
- addr_val(self, 0)?
- };
- let i32t = self.s.t.i32;
- let dims = self.s.m.named_struct("dx.types.Dimensions", vec![i32t; 4]);
- let d = self
- .s
- .op(c, "getDimensions", Attr::ReadOnly, dims, 72, &[h, mip]);
- let one = self.s.m.i32(1);
- let present = match kind {
- 1 => [true, false, false, true],
- 2 | 5 | 6 => [true, true, false, true],
- _ => [true, true, true, true],
- };
- let Operand::Vector { reg, .. } = *data else {
- return Err("an image result");
- };
- for (k, ch) in (0..4u32).filter(|ch| dmask >> ch & 1 != 0).enumerate() {
- let x = if ch == 3 && uav || !present[ch as usize] {
- one
- } else {
- self.s.m.extract(c, d, ch)
- };
- self.write(inst, Reg::V(reg + k as u8), x, guard);
- }
- return Ok(());
- }
- if uav && op != 66 {
- return Err("sampling a UAV");
- }
- let h = self.handle(site, src, false, props)?;
- let fu = self.s.m.undef(self.s.t.f32);
- let iu = self.s.m.undef(self.s.t.i32);
- let r = if sample {
- let ss = self
- .descriptors
- .get(&(site, true))
- .copied()
- .ok_or("an untracked sampler")?;
- // Comparison: the reference value is the first address after
- // the bias, and the sampler is a comparison sampler.
- let cmp = matches!(op, 64 | 65);
- if cmp && (*a16 || dmask.count_ones() != 1) {
- return Err("a comparison sample of 16-bit coordinates or several channels");
- }
- let sh = self.handle(site, ss, true, (14 | if cmp { 0x8000 } else { 0 }, 0))?;
- // Texel offsets (`_o`) come first: x in bits 0-5, y in 8-13,
- // signed (programmable offsets: gathers only).
- let offset_word = name.ends_with("_o");
- let first =
- usize::from(offset_word) + usize::from(name == "image_sample_b") + usize::from(cmp);
- // Coordinates the size divides (not the array slice); explicit
- // gradients (x then y) come before the coordinates.
- let spatial = coords - usize::from(kind == 6 || kind == 7);
- let grads = if op == 63 { 2 * spatial } else { 0 };
- let mut cs = vec![fu; 4];
- // With a16, sampling coordinates and LOD are f16.
- let to_float = |l: &mut Self, x: V| {
- if *a16 {
- let f = l.s.t.f32;
- l.s.op(l.cur, "legacyF16ToF32", Attr::ReadNone, f, 131, &[x])
- } else {
- l.as_float(x)
- }
- };
- for (k, slot) in cs.iter_mut().enumerate().take(coords) {
- let x = addr_val(self, first + grads + k)?;
- *slot = to_float(self, x);
- }
- if kind == 5 {
- if *unorm || op == 63 || op == 81 {
- return Err("a cube sample with texel coordinates, gradients or a level query");
- }
- let dir = self.cube_direction(cs[0], cs[1], cs[2]);
- cs[..3].copy_from_slice(&dir);
- }
- if *unorm {
- // Texel coordinates: divided by the size of level 0.
- let i32t = self.s.t.i32;
- let dims = self.s.m.named_struct("dx.types.Dimensions", vec![i32t; 4]);
- let z = self.s.m.i32(0);
- let d = self
- .s
- .op(c, "getDimensions", Attr::ReadOnly, dims, 72, &[h, z]);
- for (k, slot) in cs.iter_mut().enumerate().take(spatial) {
- let n = self.s.m.extract(c, d, k as u32);
- let n = self.s.m.cast(c, Cast::UiToFp, n, self.s.t.f32);
- *slot = self.s.m.bin(c, Bin::SDiv, *slot, n);
- }
- }
- if op == 81 {
- // The clamped then the unclamped level of detail.
- let Operand::Vector { reg, .. } = *data else {
- return Err("an image result");
- };
- let f32t = self.s.t.f32;
- for (k, ch) in (0..2u32).filter(|ch| dmask >> ch & 1 != 0).enumerate() {
- let clamped = self.s.m.bool(ch == 0);
- let lod = self.s.op(
- c,
- "calculateLOD.f32",
- Attr::ReadOnly,
- f32t,
- 81,
- &[h, sh, cs[0], cs[1], cs[2], clamped],
- );
- let x = self.as_int(lod);
- self.write(inst, Reg::V(reg + k as u8), x, guard);
- }
- return Ok(());
- }
- let offsets = if kind == 5 {
- 0
- } else {
- (coords - usize::from(kind == 6 || kind == 7)).min(3)
- };
- let zero = self.s.m.i32(0);
- let mut os = vec![iu; 3];
- for o in os.iter_mut().take(offsets) {
- *o = zero;
- }
- let mut args = vec![h, sh];
- args.extend(&cs);
- match op {
- 60 => {
- if offset_word {
- // DXIL takes immediate offsets in -8..=7; the
- // console's are 6-bit and may be computed.
- let w = addr_val(self, 0)?;
- let w = self
- .s
- .m
- .int_value(w)
- .ok_or("a sample offset that is not constant")?
- as u32;
- for (k, o) in os.iter_mut().enumerate().take(offsets) {
- let x = (w >> (8 * k) & 0x3F) as i32;
- let x = if x >= 32 { x - 64 } else { x };
- if !(-8..=7).contains(&x) {
- return Err("a sample offset past -8..7");
- }
- *o = self.s.m.i32(x as u32);
- }
- }
- args.extend(&os);
- args.push(fu);
- self.s
- .op(c, "sample.f32", Attr::ReadOnly, self.s.t.res_ret, 60, &args)
- }
- 61 => {
- let bias = addr_val(self, 0)?;
- let bias = self.as_float(bias);
- args.extend(&os);
- args.extend([bias, fu]);
- self.s.op(
- c,
- "sampleBias.f32",
- Attr::ReadOnly,
- self.s.t.res_ret,
- 61,
- &args,
- )
- }
- 63 => {
- let mut g = vec![fu; 6];
- for k in 0..spatial {
- let x = addr_val(self, first + k)?;
- g[k] = to_float(self, x);
- let y = addr_val(self, first + spatial + k)?;
- g[3 + k] = to_float(self, y);
- }
- args.extend(&os);
- args.extend(g);
- args.push(fu);
- self.s.op(
- c,
- "sampleGrad.f32",
- Attr::ReadOnly,
- self.s.t.res_ret,
- 63,
- &args,
- )
- }
- 64 | 65 => {
- let reference = addr_val(self, first - 1)?;
- let reference = self.as_float(reference);
- args.extend(&os);
- args.push(reference);
- let name = if op == 64 {
- args.push(fu);
- "sampleCmp.f32"
- } else {
- "sampleCmpLevelZero.f32"
- };
- self.s
- .op(c, name, Attr::ReadOnly, self.s.t.res_ret, op, &args)
- }
- 62 => {
- let lod = if name == "image_sample_l" {
- let x = addr_val(self, coords)?;
- to_float(self, x)
- } else {
- self.s.m.f32(0)
- };
- args.extend(&os);
- args.push(lod);
- self.s.op(
- c,
- "sampleLevel.f32",
- Attr::ReadOnly,
- self.s.t.res_ret,
- 62,
- &args,
- )
- }
- _ => {
- if name == "image_gather4" {
- return Err("a gather with implicit level");
- }
- if dmask.count_ones() != 1 {
- return Err("a gather of several channels");
- }
- if offset_word {
- let w = addr_val(self, 0)?;
- for (k, at) in [(0, 26), (1, 18)] {
- // Bits at..at+5 sign-extended: shift up, then down.
- let left = self.s.m.i32(at);
- let t = self.s.m.bin(c, Bin::Shl, w, left);
- let k26 = self.s.m.i32(26);
- os[k] = self.s.m.bin(c, Bin::AShr, t, k26);
- }
- }
- args.extend(&os[..2]);
- args.push(self.s.m.i32(dmask.trailing_zeros()));
- self.s.op(
- c,
- "textureGather.f32",
- Attr::ReadOnly,
- self.s.t.res_ret,
- 73,
- &args,
- )
- }
- }
- } else {
- let mut cs = vec![iu; 3];
- for (k, slot) in cs.iter_mut().enumerate().take(coords) {
- *slot = addr_val(self, k)?;
- }
- let mip = if name == "image_load_mip" {
- addr_val(self, coords)?
- } else if uav {
- // Our declarations have four channels.
- self.s.flags |= flags::TYPED_UAV_LOADS;
- iu
- } else {
- self.s.m.i32(0)
- };
- let mut args = vec![h, mip];
- args.extend(&cs);
- args.extend([iu, iu, iu]);
- self.s.op(
- c,
- "textureLoad.f32",
- Attr::ReadOnly,
- self.s.t.res_ret,
- 66,
- &args,
- )
- };
- let Operand::Vector { reg, .. } = *data else {
- return Err("an image result");
- };
- let channels: Vec<u32> = if op == 73 {
- vec![0, 1, 2, 3]
- } else {
- (0..4).filter(|ch| dmask >> ch & 1 != 0).collect()
- };
- let mut xs = Vec::new();
- for ch in channels {
- let x = self.s.m.extract(c, r, ch);
- xs.push(if *d16 {
- // shortcut: d16 results are taken as floats (rounded to
- // nearest even); integer formats would keep 16 bits.
- let i32t = self.s.t.i32;
- self.s
- .op(c, "legacyF32ToF16", Attr::ReadNone, i32t, 130, &[x])
- } else {
- self.as_int(x)
- });
- }
- if *d16 {
- // Two channels per VGPR, low half first (an odd last high half 0).
- let k16 = self.s.m.i32(16);
- xs = xs
- .chunks(2)
- .map(|p| match *p {
- [lo, hi] => {
- let hi = self.s.m.bin(c, Bin::Shl, hi, k16);
- self.s.m.bin(c, Bin::Or, lo, hi)
- }
- _ => p[0],
- })
- .collect();
- }
- for (k, x) in xs.into_iter().enumerate() {
- self.write(inst, Reg::V(reg + k as u8), x, guard);
- }
- Ok(())
- }
- /// The direction a cube sample's face coordinates stand for: the
- /// console takes s and t in [1, 2] (`v_cubesc`/`v_cubetc` over
- /// `|v_cubema|`, plus 1.5) and the face id (`v_cubeid`), which the
- /// host's cube sampling turns back into the same texel.
- // shortcut: the view is taken as a cube, not a cube array (whose face
- // operand adds 8 × the slice).
- fn cube_direction(&mut self, s: V, t: V, face: V) -> [V; 3] {
- let c = self.cur;
- let k = |l: &mut Self, v: f32| l.s.m.f32(v.to_bits());
- let (two, three) = (k(self, 2.0), k(self, 3.0));
- let u = self.s.m.bin(c, Bin::Mul, s, two);
- let u = self.s.m.bin(c, Bin::Sub, u, three);
- let v = self.s.m.bin(c, Bin::Mul, t, two);
- let v = self.s.m.bin(c, Bin::Sub, v, three);
- let z = self.s.m.f32(0x8000_0000);
- let (nu, nv) = (
- self.s.m.bin(c, Bin::Sub, z, u),
- self.s.m.bin(c, Bin::Sub, z, v),
- );
- let (one, mone) = (k(self, 1.0), k(self, -1.0));
- let f = self.s.m.cast(c, Cast::FpToUi, face, self.s.t.i32);
- // Per face +x, -x, +y, -y, +z, -z.
- let table = [
- [one, mone, u, u, u, nu],
- [nv, nv, one, mone, nv, nv],
- [nu, u, v, nv, one, mone],
- ];
- table.map(|row| {
- let mut r = row[0];
- for (n, &x) in row.iter().enumerate().skip(1) {
- let kn = self.s.m.i32(n as u32);
- let is = self.s.m.cmp(c, pred::EQ, f, kn);
- r = self.s.m.select(c, is, x, r);
- }
- r
- })
- }
- fn buffer(&mut self, inst: &Inst, b: usize, i: usize) -> Result<(), Why> {
- let Fields::Buffer {
- data,
- addr,
- soffset,
- offset,
- offen,
- idxen,
- format,
- lds,
- tfe,
- ..
- } = &inst.fields
- else {
- unreachable!()
- };
- let name = inst.name();
- if *lds || *tfe {
- return Err("a buffer access to LDS or with tfe");
- }
- if format.is_some() && !name.starts_with("tbuffer_load_format_") {
- return Err(name);
- }
- let site = (b, i);
- let guard = self.guard(b, i)?;
- let c = self.cur;
- let store = name.starts_with("buffer_store_");
- // DXIL atomic operations: add, and, or, xor, signed and unsigned
- // min and max, exchange; compare-exchange apart.
- let atomic = match name.strip_prefix("buffer_atomic_") {
- None => None,
- Some("add") => Some(0),
- Some("and") => Some(1),
- Some("or") => Some(2),
- Some("xor") => Some(3),
- Some("smin") => Some(4),
- Some("smax") => Some(5),
- Some("umin") => Some(6),
- Some("umax") => Some(7),
- Some("swap") => Some(8),
- Some("cmpswap") => Some(u32::MAX),
- Some(_) => return Err(name),
- };
- // d16: two 16-bit channels per VGPR, low half first.
- let d16 = name.starts_with("buffer_") && name.contains("_format_d16_");
- let plain = name.replace("_format_d16_", "_format_");
- let (typed, count) = match plain
- .strip_prefix("buffer_load_")
- .or(plain.strip_prefix("buffer_store_"))
- .or(plain.strip_prefix("tbuffer_load_"))
- {
- _ if atomic.is_some() => (false, 1),
- Some("dword") => (false, 1),
- Some("dwordx2") => (false, 2),
- Some("dwordx3") => (false, 3),
- Some("dwordx4") => (false, 4),
- Some("format_x") => (true, 1),
- Some("format_xy") => (true, 2),
- Some("format_xyz") => (true, 3),
- Some("format_xyzw") => (true, 4),
- _ => return Err(name),
- };
- let Operand::Vector { reg: areg, .. } = *addr else {
- return Err("a buffer address");
- };
- let vaddr = |l: &mut Self, k: u8| -> Result<V, Why> {
- let v = inst.read(Reg::V(areg + k)).ok_or("a buffer address")?;
- l.i32_of(v)
- };
- let index = if *idxen { Some(vaddr(self, 0)?) } else { None };
- let mut off = self.s.m.i32(u32::from(*offset));
- if *offen {
- let o = vaddr(self, u8::from(*idxen))?;
- off = self.s.m.bin(c, Bin::Add, off, o);
- }
- if !matches!(
- soffset,
- Operand::Scalar { code: NULL, .. } | Operand::Const(128)
- ) {
- let so = self.operand(inst, soffset)?;
- off = self.s.m.bin(c, Bin::Add, off, so);
- }
- let src = self
- .descriptors
- .get(&(site, false))
- .copied()
- .ok_or("an untracked buffer")?;
- let used = self.used_kind(src);
- let (base, extra) = self.built_offset(src)?;
- if base != 0 {
- let b = self.s.m.i32(base);
- off = self.s.m.bin(c, Bin::Add, off, b);
- }
- if let Some(x) = extra {
- off = self.s.m.bin(c, Bin::Add, off, x);
- }
- if used.is_some_and(|u| u.kind == ResourceType::Cbv) && !typed {
- // A constant buffer read per lane (a dynamically indexed array).
- // shortcut: the console's CBV descriptor is taken to have a 16-byte
- // stride, as the vector loads of whole rows suggest.
- let h = self.handle(site, src, false, (13, 0x1_0000))?;
- let byte = match index {
- Some(i) => {
- let sixteen = self.s.m.i32(16);
- let rows = self.s.m.bin(c, Bin::Mul, i, sixteen);
- self.s.m.bin(c, Bin::Add, rows, off)
- }
- None => off,
- };
- let Some(Operand::Vector { reg, .. }) = *data else {
- return Err("a buffer result");
- };
- for k in 0..count {
- let four = self.s.m.i32(4 * k);
- let a = self.s.m.bin(c, Bin::Add, byte, four);
- let x = self.cbuffer_dword(h, a);
- self.write(inst, Reg::V(reg + k as u8), x, guard);
- }
- return Ok(());
- }
- let uav = store || atomic.is_some() || used.is_some_and(|u| u.kind == ResourceType::Uav);
- let u = if uav { 0x1000 } else { 0 };
- let iu = self.s.m.undef(self.s.t.i32);
- if let Some(op) = atomic {
- let d = data.as_ref().ok_or("buffer atomic data")?;
- let vals = self.vgprs(inst, d, if op == u32::MAX { 2 } else { 1 })?;
- let (h, coords) = match index {
- Some(index) => {
- let stride = used.map_or(0, |u| u.stride);
- if stride < 4 || !stride.is_multiple_of(4) {
- return Err("a structured buffer of unknown stride");
- }
- let h = self.handle(site, src, false, (12 | u, stride))?;
- (h, [index, off, iu])
- }
- None => (self.handle(site, src, false, (11 | u, 0))?, [off, iu, iu]),
- };
- let Operand::Vector { reg, .. } = *d else {
- return Err("buffer atomic data");
- };
- let returns = matches!(inst.fields, Fields::Buffer { cache, .. } if cache.glc);
- let old = match inst.read(Reg::V(reg)) {
- Some(v) if returns => self.i32_of(v)?,
- _ => self.s.m.i32(0),
- };
- let at = self.begin_guarded(guard);
- let i32t = self.s.t.i32;
- let r = if op == u32::MAX {
- // cmpswap: data is (new, compare).
- let mut args = vec![h];
- args.extend(coords);
- args.extend([vals[1], vals[0]]);
- self.s.op(
- self.cur,
- "atomicCompareExchange.i32",
- Attr::NoUnwind,
- i32t,
- 79,
- &args,
- )
- } else {
- let mut args = vec![h, self.s.m.i32(op)];
- args.extend(coords);
- args.push(vals[0]);
- self.s
- .op(self.cur, "atomicBinOp.i32", Attr::NoUnwind, i32t, 78, &args)
- };
- let r = self.end_guarded(at, &[(r, old)])[0];
- if returns {
- self.write(inst, Reg::V(reg), r, None);
- }
- return Ok(());
- }
- if store {
- let regs = if d16 { count.div_ceil(2) } else { count };
- let mut vals =
- self.vgprs(inst, data.as_ref().ok_or("buffer store data")?, regs as u8)?;
- if d16 {
- // shortcut: d16 data is taken as f16 (integer formats would
- // keep 16 bits), widened for the 32-bit float store.
- let mut wide = Vec::new();
- for k in 0..count {
- let mut x = vals[(k / 2) as usize];
- if k % 2 == 1 {
- let k16 = self.s.m.i32(16);
- x = self.s.m.bin(c, Bin::LShr, x, k16);
- }
- let f32t = self.s.t.f32;
- let f = self
- .s
- .op(c, "legacyF16ToF32", Attr::ReadNone, f32t, 131, &[x]);
- wide.push(self.as_int(f));
- }
- vals = wide;
- }
- let mut args = Vec::new();
- let (op, name) = if typed {
- // shortcut: typed stores are taken to write all four channels
- // of a float format.
- if count != 4 || *offen || *offset != 0 {
- return Err("a typed buffer store of some channels or at an offset");
- }
- args.push(self.handle(site, src, false, (10 | u, 0x409))?);
- args.extend([index.unwrap_or(self.s.m.i32(0)), iu]);
- for x in vals {
- args.push(self.as_float(x));
- }
- (69, "bufferStore.f32")
- } else {
- match index {
- Some(index) => {
- let stride = used.map_or(0, |u| u.stride);
- if stride < 4 || !stride.is_multiple_of(4) {
- return Err("a structured buffer of unknown stride");
- }
- args.push(self.handle(site, src, false, (12 | u, stride))?);
- args.extend([index, off]);
- }
- None => {
- args.push(self.handle(site, src, false, (11 | u, 0))?);
- args.extend([off, iu]);
- }
- }
- args.extend(&vals);
- args.resize(3 + 4, iu);
- (140, "rawBufferStore.i32")
- };
- args.push(self.s.m.i8(if typed { 0xF } else { (1u8 << count) - 1 }));
- if !typed {
- args.push(self.s.m.i32(4));
- }
- let at = self.begin_guarded(guard);
- let void = self.s.t.void;
- self.s.op(self.cur, name, Attr::NoUnwind, void, op, &args);
- self.end_guarded(at, &[]);
- return Ok(());
- }
- if let Some(f) = *format {
- if index.is_some() {
- return Err("a typed-format load by index");
- }
- let h = self.handle(site, src, false, (11 | u, 0))?;
- let Some(Operand::Vector { reg, .. }) = *data else {
- return Err("a buffer result");
- };
- for (k, x) in self.format_load(h, off, f, count)?.into_iter().enumerate() {
- self.write(inst, Reg::V(reg + k as u8), x, guard);
- }
- return Ok(());
- }
- let r = if typed {
- if *offen || matches!(off, V::Inst(_)) || *offset != 0 {
- return Err("a typed buffer load at an offset");
- }
- if uav {
- self.s.flags |= flags::TYPED_UAV_LOADS;
- }
- let h = self.handle(site, src, false, (10 | u, 0x409))?;
- let index = index.unwrap_or(self.s.m.i32(0));
- self.s.op(
- c,
- "bufferLoad.f32",
- Attr::ReadOnly,
- self.s.t.res_ret,
- 68,
- &[h, index, iu],
- )
- } else {
- let mask = self.s.m.i8((1u8 << count) - 1);
- let align = self.s.m.i32(4);
- match index {
- Some(index) => {
- let stride = used.map_or(0, |u| u.stride);
- if stride < 4 || !stride.is_multiple_of(4) {
- return Err("a structured buffer of unknown stride");
- }
- let h = self.handle(site, src, false, (12 | u, stride))?;
- self.s.op(
- c,
- "rawBufferLoad.f32",
- Attr::ReadOnly,
- self.s.t.res_ret,
- 139,
- &[h, index, off, mask, align],
- )
- }
- None => {
- let h = self.handle(site, src, false, (11 | u, 0))?;
- self.s.op(
- c,
- "rawBufferLoad.f32",
- Attr::ReadOnly,
- self.s.t.res_ret,
- 139,
- &[h, off, iu, mask, align],
- )
- }
- }
- };
- let Some(Operand::Vector { reg, .. }) = *data else {
- return Err("a buffer result");
- };
- if d16 {
- // Two f16 channels per VGPR (an odd last high half 0).
- let k16 = self.s.m.i32(16);
- let i32t = self.s.t.i32;
- for k in 0..count.div_ceil(2) {
- let mut x = None;
- for ch in (2 * k..(2 * k + 2).min(count)).rev() {
- let f = self.s.m.extract(c, r, ch);
- let h = self
- .s
- .op(c, "legacyF32ToF16", Attr::ReadNone, i32t, 130, &[f]);
- x = Some(match x {
- Some(hi) => {
- let hi = self.s.m.bin(c, Bin::Shl, hi, k16);
- self.s.m.bin(c, Bin::Or, h, hi)
- }
- None => h,
- });
- }
- self.write(inst, Reg::V(reg + k as u8), x.expect("a channel"), guard);
- }
- return Ok(());
- }
- for k in 0..count {
- let x = self.s.m.extract(c, r, k);
- let x = self.as_int(x);
- self.write(inst, Reg::V(reg + k as u8), x, guard);
- }
- Ok(())
- }
- fn export(&mut self, b: usize, i: usize) -> Result<(), Why> {
- let e = *self
- .exports
- .get(&(b, i))
- .ok_or("an export I/O raising did not see")?;
- if e.compressed {
- return Err("a compressed export");
- }
- let guard = self.guard(b, i)?;
- let mesh = self.s.mesh.is_some();
- let c = self.cur;
- if let Some(g) = guard
- && !mesh
- {
- if !(e.done && self.s.pixel) {
- return Err("a conditional export");
- }
- let t = self.s.m.bool(true);
- let kill = self.s.m.bin(c, Bin::Xor, g, t);
- self.s
- .op(c, "discard", Attr::NoUnwind, self.s.t.void, 82, &[kill]);
- }
- if mesh && e.target == Target::Primitive {
- return self.emit_indices(e, guard);
- }
- let param_row = |usage: Usage, row: u8| match usage {
- // The 2020 compiler leaves a mesh program's first entry zeroed
- // (usage "position"): its row is the one no other entry names.
- Usage::Other(0) if mesh => self.unnamed_row(),
- _ => Some(row),
- };
- // (output element, channel) per exported channel; per-primitive
- // outputs are elements of `prims`.
- let mut stores = Vec::new();
- let primitive = matches!(e.target, Target::PrimitiveParam { .. });
- let outputs = if primitive {
- &self.s.prims
- } else {
- &self.s.outputs
- };
- let by_kind = |kind: u8| outputs.iter().position(|o| o.kind == kind);
- // 16-bit vertex outputs: the halves each dword carries, as (output
- // element, channel, high half) per channel.
- let halves = match e.target {
- Target::Param { usage, row } => {
- param_row(usage, row).and_then(|r| self.half_outputs.get(&r).map(|&h| (r, h)))
- }
- _ => None,
- };
- if let Some((row, (pack2, b))) = halves {
- for ch in 0..4u8 {
- let Some(value) = e.values[usize::from(ch)] else {
- continue;
- };
- for high in [false, true] {
- let h = 2 * ch + u8::from(high);
- let src = if pack2 {
- packed_half(outputs, row, b, h)
- } else {
- (!high).then_some((row, ch))
- };
- let found = src.and_then(|(r, c)| {
- outputs.iter().position(|o| {
- o.row == Some(u32::from(r)) && c >= o.col && c < o.col + o.cols
- })
- });
- if let Some(id) = found {
- let col = src.expect("found").1 - outputs[id].col;
- stores.push((id, col, value, Some(high)));
- }
- }
- }
- }
- for ch in 0..4u8 {
- let Some(value) = e.values[usize::from(ch)] else {
- continue;
- };
- if halves.is_some() {
- break;
- }
- let found = match e.target {
- Target::Color(n) => outputs
- .iter()
- .position(|o| o.kind == 16 && o.row == Some(u32::from(n)))
- .map(|id| (id, ch)),
- Target::Position(0) => by_kind(3).map(|id| (id, ch)),
- Target::Depth => match ch {
- 0 => by_kind(17)
- .or(by_kind(18))
- .or(by_kind(19))
- .map(|id| (id, 0)),
- 1 => by_kind(20).map(|id| (id, 0)),
- 2 => by_kind(14).map(|id| (id, 0)),
- _ => None,
- },
- Target::Param { usage, row } | Target::PrimitiveParam { usage, row } => {
- let row = param_row(usage, row);
- outputs
- .iter()
- .position(|o| {
- row.is_some_and(|r| o.row == Some(u32::from(r)))
- && ch >= o.col
- && ch < o.col + o.cols
- })
- .map(|id| (id, ch - outputs[id].col))
- }
- Target::Null | Target::Unread => None,
- Target::Position(n @ 1..=2) => {
- // shortcut: without the misc vector (point size, render
- // target or viewport index) pos1 and pos2 carry the clip
- // then cull distances, in semantic order.
- if outputs.iter().any(|o| matches!(o.kind, 4 | 5)) {
- return Err("render-target or viewport index exports");
- }
- let mut dists: Vec<usize> = (0..outputs.len())
- .filter(|&i| matches!(outputs[i].kind, 6 | 7))
- .collect();
- dists.sort_by_key(|&i| (outputs[i].kind, outputs[i].index));
- let flat: Vec<(usize, u8)> = dists
- .iter()
- .flat_map(|&i| (0..outputs[i].cols).map(move |c| (i, c)))
- .collect();
- // Channels past the distances the signature declares are
- // ones the clipper does not enable (`PA_CL_VS_OUT_CNTL`).
- let k = usize::from(n - 1) * 4 + usize::from(ch);
- flat.get(k).copied()
- }
- Target::Position(_) => return Err("clip, cull or point-size exports"),
- Target::Primitive => return Err("a primitive export"),
- };
- if let Some((id, col)) = found {
- stores.push((id, col, value, None));
- }
- }
- fn elements<'l>(l: &'l mut Lower, primitive: bool) -> &'l mut Vec<SigElement> {
- if primitive {
- &mut l.s.prims
- } else {
- &mut l.s.outputs
- }
- }
- if stores
- .iter()
- .any(|s| s.3.is_none() && elements(self, primitive)[s.0].half)
- {
- return Err("a 16-bit output the stage data does not mark");
- }
- // A mesh lane writes its own vertex or primitive, where it exports.
- let at = if mesh {
- self.begin_guarded(guard)
- } else {
- None
- };
- let (name, opcode) = match (mesh, primitive) {
- (false, _) => ("storeOutput", 5),
- (true, false) => ("storeVertexOutput", 171),
- (true, true) => ("storePrimitiveOutput", 172),
- };
- for (id, col, value, half) in stores {
- let mut x = self.i32_of(value)?;
- if let Some(high) = half {
- // An f16 half, declared a 32-bit float on the host.
- let c = self.cur;
- if high {
- let k16 = self.s.m.i32(16);
- x = self.s.m.bin(c, Bin::LShr, x, k16);
- }
- let f32t = self.s.t.f32;
- let f = self
- .s
- .op(c, "legacyF16ToF32", Attr::ReadNone, f32t, 131, &[x]);
- x = self.as_int(f);
- }
- let el = &mut elements(self, primitive)[id];
- el.usage |= 1 << col;
- let (suffix, x) = match el.comp {
- 9 if half.is_some() => (".f32", self.as_float(x)),
- 9 => {
- let f = match self.floats.get(&value) {
- Some(&f) => f,
- None => self.as_float(x),
- };
- (".f32", f)
- }
- _ => (".i32", x),
- };
- let mut args = vec![
- self.s.m.i32(id as u32),
- self.s.m.i32(0),
- self.s.m.i8(col),
- x,
- ];
- if mesh {
- args.push(self.mesh_thread());
- }
- let void = self.s.t.void;
- self.s.op(
- self.cur,
- &format!("{name}{suffix}"),
- Attr::NoUnwind,
- void,
- opcode,
- &args,
- );
- }
- self.end_guarded(at, &[]);
- Ok(())
- }
- /// Mesh: `exp prim` as the lane's primitive. Bit 31 culls it (all
- /// indices 0: no area).
- fn emit_indices(&mut self, e: &Export, guard: Option<V>) -> Result<(), Why> {
- let v = e.values[0].ok_or("a primitive export without its indices")?;
- let x = self.i32_of(v)?;
- let at = self.begin_guarded(guard);
- let c = self.cur;
- let z = self.s.m.i32(0);
- let culled = self.s.m.cmp(c, pred::SLT, x, z);
- let mut args = vec![self.mesh_thread()];
- for k in 0..3 {
- let (shift, mask) = (self.s.m.i32(10 * k), self.s.m.i32(0x3FF));
- let i = self.s.m.bin(c, Bin::LShr, x, shift);
- let i = self.s.m.bin(c, Bin::And, i, mask);
- args.push(self.s.m.select(c, culled, z, i));
- }
- let void = self.s.t.void;
- self.s
- .op(c, "emitIndices", Attr::NoUnwind, void, 169, &args);
- self.end_guarded(at, &[]);
- Ok(())
- }
- /// The per-vertex arbitrary row no mesh output entry names but one
- /// (the 2020 compiler's zeroed entry).
- fn unnamed_row(&self) -> Option<u8> {
- let StageIo::Ms(ms) = &self.x.native.io else {
- return None;
- };
- let named = |r: u32| {
- ms.outputs
- .iter()
- .any(|o| o.param & 0x80 == 0 && o.usage != Usage::Other(0) && u32::from(o.row) == r)
- };
- let free: std::collections::BTreeSet<u32> = self
- .s
- .outputs
- .iter()
- .filter(|o| o.kind == 0)
- .filter_map(|o| o.row)
- .filter(|&r| !named(r))
- .collect();
- match free.into_iter().collect::<Vec<_>>()[..] {
- [r] => u8::try_from(r).ok(),
- _ => None,
- }
- }
- fn exit(&mut self, b: usize) -> Result<(), Stop> {
- let offset = self.x.p.blocks[b]
- .insts
- .last()
- .map_or(self.x.p.blocks[b].offset, |i| i.machine.offset);
- let term = match self.x.st.exits[b] {
- LaneExit::Jump(t) if self.counters.contains_key(&t) && self.rpo[t] <= self.rpo[b] => {
- let ok = self.count_iteration(t, pred::IULT);
- let abort = self.abort_block();
- Term::CondBr(ok, self.block[t].expect("reachable"), abort)
- }
- LaneExit::Jump(t) => {
- self.enter_loop(b, t, self.cur);
- Term::Br(self.block[t].expect("reachable"))
- }
- LaneExit::Branch {
- when,
- taken,
- not_taken,
- } => {
- // A negated condition branches the other way round.
- let (cond, taken, not_taken) = match self.x.st.bools.node(when) {
- Some((atom, Bool::TRUE, Bool::FALSE)) => {
- let a = self
- .atom(atom, self.cur)
- .map_err(|w| unsupported(offset, w))?;
- (a, not_taken, taken)
- }
- _ => {
- let c = self
- .boolean(when, self.cur)
- .map_err(|w| unsupported(offset, w))?;
- (c, taken, not_taken)
- }
- };
- // A back edge is taken only below the iteration limit.
- let back =
- |l: &Self, to: usize| l.counters.contains_key(&to) && l.rpo[to] <= l.rpo[b];
- // (No `xor` of a compare: a loop exit testing one hung the
- // AMD driver.)
- let cond = if back(self, taken) {
- let ok = self.count_iteration(taken, pred::IULT);
- self.s.m.bin(self.cur, Bin::And, cond, ok)
- } else if back(self, not_taken) {
- let stop = self.count_iteration(not_taken, pred::IUGE);
- self.s.m.bin(self.cur, Bin::Or, cond, stop)
- } else {
- cond
- };
- // Critical edges get a block of their own, so loops have
- // dedicated exits and preheaders as DXC emits them.
- let (t, f) = (self.edge(b, taken), self.edge(b, not_taken));
- for (to, via) in [(taken, t), (not_taken, f)] {
- if !back(self, to) {
- // From an edge block, or from here.
- let from = if Some(via) == self.block[to] {
- self.cur
- } else {
- via
- };
- self.enter_loop(b, to, from);
- }
- }
- Term::CondBr(cond, t, f)
- }
- LaneExit::End => {
- if self.s.pixel && !self.exported[b] && !self.has_done_export(b) {
- // A lane leaving without its final export was killed.
- let t = self.s.m.bool(true);
- self.s
- .op(self.cur, "discard", Attr::NoUnwind, self.s.t.void, 82, &[t]);
- }
- Term::Ret
- }
- LaneExit::Indirect => return Err(unsupported(offset, "an indirect jump")),
- };
- let _ = Exit::End;
- self.s.m.terminate(self.cur, term);
- Ok(())
- }
- /// A forward edge into loop header `to` (from DXIL block `via`) starts
- /// its count at 0.
- fn enter_loop(&mut self, _from: usize, to: usize, via: usize) {
- if let Some((_, incoming)) = self.counters.get_mut(&to) {
- let z = self.s.m.i32(0);
- incoming.push((z, via));
- }
- }
- /// A back edge to header `to` (a back edge keeps its block, no
- /// critical-edge block): the count goes up; the result compares it with
- /// [`LOOP_LIMIT`] by `p` (below: go on; at or above: stop).
- fn count_iteration(&mut self, to: usize, p: u8) -> V {
- let c = self.cur;
- let (phi, _) = self.counters[&to];
- let one = self.s.m.i32(1);
- let next = self.s.m.bin(c, Bin::Add, phi, one);
- let limit = self.s.m.i32(super::loop_limit());
- self.counters
- .get_mut(&to)
- .expect("header")
- .1
- .push((next, c));
- self.s.m.cmp(c, p, next, limit)
- }
- /// The block a lane stopped by [`LOOP_LIMIT`] leaves through.
- fn abort_block(&mut self) -> usize {
- if let Some(a) = self.abort {
- return a;
- }
- let a = self.s.m.new_block();
- if self.s.pixel {
- let t = self.s.m.bool(true);
- self.s
- .op(a, "discard", Attr::NoUnwind, self.s.t.void, 82, &[t]);
- }
- self.s.m.terminate(a, Term::Ret);
- self.abort = Some(a);
- a
- }
- /// The DXIL block a branch from IR block `from` to `to` goes to.
- fn edge(&mut self, from: usize, to: usize) -> usize {
- let target = self.block[to].expect("reachable");
- let preds = self.x.p.blocks[to]
- .preds
- .iter()
- .filter(|&&q| self.block[q].is_some())
- .count();
- // Back edges stay as they are: the exiting block is the latch.
- if preds < 2 || self.rpo[to] <= self.rpo[from] {
- return target;
- }
- let e = self.s.m.new_block();
- self.s.m.terminate(e, Term::Br(target));
- self.edges.insert((from, to), e);
- e
- }
- fn fill_phis(&mut self) -> Result<(), Stop> {
- let p = self.x.p;
- // A phi that only another read phi consumes is live too. Without this, a value with no DXIL form that
- // reaches a read through two joins is taken for dead at the first join and replaced by zero below, and the
- // lowering accepts what it refuses when the same value is read directly.
- let mut work: Vec<Value> = self.read_phis.iter().copied().collect();
- while let Some(v) = work.pop() {
- let Some(&(b, i)) = self.phi_at.get(&v) else {
- continue;
- };
- for &arg in &p.blocks[b].phis[i].args {
- if self.phi_at.contains_key(&arg) && self.read_phis.insert(arg) {
- work.push(arg);
- }
- }
- }
- for (v, b, i) in std::mem::take(&mut self.data_phis) {
- let phi = &p.blocks[b].phis[i];
- let bit = phi.reg == Reg::Scc;
- let mut incoming = Vec::new();
- for (n, &q) in p.blocks[b].preds.iter().enumerate() {
- let Some(qb) = self.tail[q] else { continue };
- let qb = self.edges.get(&(q, b)).copied().unwrap_or(qb);
- let arg = phi.args[n];
- let x = match (self.vals.get(&arg).copied(), bit) {
- (Some(Val::I32(x)), false) | (Some(Val::Bit(x)), true) => x,
- (Some(Val::Bit(_)), false) if self.read_phis.contains(&phi.value) => {
- return Err(Stop::Retry(phi.value, "an SCC joined with SGPR values"));
- }
- (Some(Val::Missing(why)), _) if self.read_phis.contains(&phi.value) => {
- return Err(Stop::Retry(phi.value, why));
- }
- _ if self.read_phis.contains(&phi.value) => {
- return Err(Stop::Retry(phi.value, "a join of mismatched values"));
- }
- _ if bit => self.s.m.bool(false),
- _ => self.s.m.i32(0),
- };
- incoming.push((x, qb));
- }
- self.s.m.set_phi(v, incoming);
- }
- for (phi, incoming) in std::mem::take(&mut self.counters).into_values() {
- self.s.m.set_phi(phi, incoming);
- }
- while let Some((v, b, i)) = self.pending_masks.pop() {
- let phi = &p.blocks[b].phis[i];
- let offset = p.blocks[b].offset;
- let mut incoming = Vec::new();
- for (n, &q) in p.blocks[b].preds.iter().enumerate() {
- let Some(qb) = self.tail[q] else { continue };
- let qb = self.edges.get(&(q, b)).copied().unwrap_or(qb);
- let m =
- *self.x.st.masks.get(&phi.args[n]).ok_or_else(|| {
- unsupported(offset, "a lane-mask join of an untracked mask")
- })?;
- let x = self.boolean(m, qb).map_err(|w| unsupported(offset, w))?;
- incoming.push((x, qb));
- }
- self.s.m.set_phi(v, incoming);
- }
- Ok(())
- }
- }
- #[cfg(test)]
- mod tests {
- use super::{Numeric, Parts, buffer_format, half_shift, lower};
- use crate::native::{ComputeIo, HwStage, NativeProgram, StageIo};
- use crate::{io, ir, resource, structure};
- const S_ENDPGM: u32 = 0xBF81_0000;
- /// Whether a synthetic compute program translates with these input registers.
- fn lowers(words: &[u32], rsrc2: u32) -> Result<(), crate::ShaderError> {
- let code: Vec<u8> = words.iter().flat_map(|w| w.to_le_bytes()).collect();
- let native = NativeProgram {
- stage: HwStage::Cs,
- wave64: false,
- vgprs: 256,
- layout: vec![],
- used: vec![],
- io: StageIo::Cs(ComputeIo {
- rsrc2,
- threads: [1, 1, 1],
- }),
- code,
- };
- let p = ir::build(&native.code, false, 256).unwrap();
- let res = resource::track(&p, &native.layout, native.stage).unwrap();
- let io = io::raise(&p, &native).unwrap();
- let st = structure::structure(&p).unwrap();
- lower(&Parts {
- native: &native,
- p: &p,
- st: &st,
- io: &io,
- res: &res,
- inputs: vec![],
- outputs: vec![],
- prims: vec![],
- mesh_stride: 0,
- })
- .map(|_| ())
- }
- #[test]
- fn a_scalar_carry_chain_reaches_the_select() {
- // The high dword of a 64-bit add or subtract, then a select on the
- // carry (borrow) the high half leaves in SCC, and a branch on it
- // (only code with an effect needs its operands).
- let add = [
- 0xBE80_03C1, // s_mov_b32 s0, -1
- 0xBE81_0380, // s_mov_b32 s1, 0
- 0x8000_8100, // s_add_u32 s0, s0, 1
- 0x8201_8001, // s_addc_u32 s1, s1, 0
- 0x8502_0001, // s_cselect_b32 s2, s1, s0
- 0xBF07_8002, // s_cmp_lg_u32 s2, 0
- 0xBF85_0001, // s_cbranch_scc1 -> s_endpgm
- 0xBE83_0380, // s_mov_b32 s3, 0
- S_ENDPGM,
- ];
- let sub = [
- 0xBE80_0380, // s_mov_b32 s0, 0
- 0xBE81_0380, // s_mov_b32 s1, 0
- 0x8080_8100, // s_sub_u32 s0, s0, 1
- 0x8281_8001, // s_subb_u32 s1, s1, 0
- 0x8502_0001, // s_cselect_b32 s2, s1, s0
- 0xBF07_8002, // s_cmp_lg_u32 s2, 0
- 0xBF85_0001, // s_cbranch_scc1 -> s_endpgm
- 0xBE83_0380, // s_mov_b32 s3, 0
- S_ENDPGM,
- ];
- for words in [add, sub] {
- lowers(&words, 0).unwrap();
- }
- }
- fn perm_features(selector: u32) -> u64 {
- // Synthetic compute shader:
- // v0 is the thread ID and can supply a dynamic selector.
- // Store the result in LDS to keep the permutation live.
- let words = [
- 0x7E02_02FFu32,
- 0x817F_34AB,
- 0x7E04_02FF,
- 0xFE01_56CD,
- 0xD744_0003,
- 257 | (258 << 9) | (selector << 18),
- 0x3408_0082,
- 0xD834_0000,
- 0x0000_0304,
- S_ENDPGM,
- ];
- let code: Vec<u8> = words.iter()
- .flat_map(|w| w.to_le_bytes())
- .collect();
- let native = NativeProgram {
- stage: HwStage::Cs,
- wave64: false,
- vgprs: 256,
- layout: vec![],
- used: vec![],
- io: StageIo::Cs(ComputeIo {
- rsrc2: 1 << 15,
- threads: [64, 1, 1],
- }),
- code,
- };
- let p = ir::build(&native.code, false, 256).unwrap();
- assert!(p.blocks.iter()
- .flat_map(|b| &b.insts)
- .any(|i| i.name() == "v_perm_b32"));
- let res = resource::track(
- &p, &native.layout, native.stage
- ).unwrap();
- let io = io::raise(&p, &native).unwrap();
- let st = structure::structure(&p).unwrap();
- let emitted = lower(&Parts {
- native: &native,
- p: &p,
- st: &st,
- io: &io,
- res: &res,
- inputs: vec![],
- outputs: vec![],
- prims: vec![],
- mesh_stride: 0,
- }).unwrap();
- let container =
- crate::Container::parse(&emitted.dxil).unwrap();
- let features =
- container.part(crate::FourCC::SFI0).unwrap();
- u64::from_le_bytes(features.data.try_into().unwrap())
- }
- #[test]
- fn perm_constant_selector_needs_no_int64() {
- assert_eq!(perm_features(128) & 0x8000, 0);
- }
- #[test]
- fn perm_dynamic_selector_needs_no_int64() {
- assert_eq!(
- perm_features(256) & 0x8000,
- 0,
- "dynamic v_perm_b32 must not require INT64"
- );
- }
- #[test]
- fn scalar_bit_tests_lower() {
- // Each tests one bit of a constant into SCC and branches on it (only
- // code with an effect needs its operands).
- let tail = [
- 0xBF85_0001, // s_cbranch_scc1 -> s_endpgm
- 0xBE84_0380, // s_mov_b32 s4, 0
- S_ENDPGM,
- ];
- let bit_tests: [&[u32]; 4] = [
- &[0xBE81_0387, 0xBF0C_8301], // s_mov_b32 s1, 7; s_bitcmp0_b32 s1, 3
- &[0xBE81_0387, 0xBF0D_8301], // s_mov_b32 s1, 7; s_bitcmp1_b32 s1, 3
- &[
- 0xBE80_0387, // s_mov_b32 s0, 7
- 0xBE81_0380, // s_mov_b32 s1, 0
- 0xBF0E_8300, // s_bitcmp0_b64 s[0:1], 3
- ],
- &[
- 0xBE80_0387, // s_mov_b32 s0, 7
- 0xBE81_0380, // s_mov_b32 s1, 0
- 0xBF0F_8300, // s_bitcmp1_b64 s[0:1], 3
- ],
- ];
- for head in bit_tests {
- let words: Vec<u32> = head.iter().chain(&tail).copied().collect();
- lowers(&words, 0).unwrap();
- }
- }
- #[test]
- fn a_missing_value_stays_missing_through_two_live_joins() {
- let mut words = [
- 0xF490_0180,
- 0, // s_memtime s[6:7]
- 0xBF06_8000, // s_cmp_eq_u32 s0, 0 (s0 = group id X)
- 0xBF85_0001, // skip the first overwrite
- 0xBE86_0380, // s_mov_b32 s6, 0
- 0xBF06_8101, // s_cmp_eq_u32 s1, 1 (s1 = group id Y)
- 0xBF85_0001, // skip the second overwrite
- 0xBE86_0381, // s_mov_b32 s6, 1
- 0xBF07_8006, // s_cmp_lg_u32 s6, 0
- 0xBF85_0001, // use the result to branch
- 0xBE83_0380,
- S_ENDPGM,
- ];
- let group_ids_xy = 3 << 7;
- // Compare to a direct read so future timestamp support works in both cases.
- let refusal = |code: &[u32]| match lowers(code, group_ids_xy) {
- Ok(()) => None,
- Err(crate::ShaderError::Unsupported { what, .. }) => Some(what),
- Err(error) => panic!("{error}"),
- };
- let direct = [&words[..2], &words[8..]].concat();
- assert_eq!(refusal(&words), refusal(&direct));
- words[8] = 0xBF07_8000; // read the group id instead; the timestamp is dead
- lowers(&words, group_ids_xy).expect("dead timestamp");
- }
- #[test]
- fn buffer_formats_by_number() {
- // The numbers the disassembler names 8_8_8_8_UNORM, 16_16_SNORM,
- // 2_10_10_10_UNORM, 16_FLOAT, 32_32_32_32_FLOAT.
- assert_eq!(buffer_format(56), Some((&[8, 8, 8, 8][..], Numeric::Unorm)));
- assert_eq!(buffer_format(24), Some((&[16, 16][..], Numeric::Snorm)));
- assert_eq!(
- buffer_format(50),
- Some((&[10, 10, 10, 2][..], Numeric::Unorm))
- );
- assert_eq!(buffer_format(13), Some((&[16][..], Numeric::Float)));
- assert_eq!(
- buffer_format(77),
- Some((&[32, 32, 32, 32][..], Numeric::Float))
- );
- // Invalid, past the table.
- assert_eq!(buffer_format(0), None);
- assert_eq!(buffer_format(78), None);
- }
- #[test]
- fn packed_buffer_formats_by_number() {
- // LLVM's `UfmtGFX10`: 10_11_11 is 30-36, 11_11_10 37-43,
- // 10_10_10_2 44-49. Channels from the lowest bits: R11G11B10_FLOAT
- // is 10_11_11 (Mesa), 10_10_10_2 has the 2-bit channel first.
- assert_eq!(buffer_format(30), Some((&[11, 11, 10][..], Numeric::Unorm)));
- assert_eq!(buffer_format(36), Some((&[11, 11, 10][..], Numeric::Float)));
- assert_eq!(buffer_format(37), Some((&[10, 11, 11][..], Numeric::Unorm)));
- assert_eq!(buffer_format(43), Some((&[10, 11, 11][..], Numeric::Float)));
- assert_eq!(
- buffer_format(44),
- Some((&[2, 10, 10, 10][..], Numeric::Unorm))
- );
- assert_eq!(
- buffer_format(45),
- Some((&[2, 10, 10, 10][..], Numeric::Snorm))
- );
- assert_eq!(
- buffer_format(46),
- Some((&[2, 10, 10, 10][..], Numeric::Uscaled))
- );
- assert_eq!(
- buffer_format(47),
- Some((&[2, 10, 10, 10][..], Numeric::Sscaled))
- );
- assert_eq!(
- buffer_format(48),
- Some((&[2, 10, 10, 10][..], Numeric::Uint))
- );
- assert_eq!(
- buffer_format(49),
- Some((&[2, 10, 10, 10][..], Numeric::Sint))
- );
- }
- /// A half's value (sign, 5-bit exponent biased 15, 10-bit mantissa), as
- /// `legacyF16ToF32` widens it.
- fn half(h: u32) -> f32 {
- let (s, e, m) = ((h >> 15) & 1, (h >> 10) & 31, h & 1023);
- let v = match e {
- 0 => m as f32 * 2f32.powi(-24),
- 31 if m == 0 => f32::INFINITY,
- 31 => f32::NAN,
- _ => (1024 + m) as f32 * 2f32.powi(e as i32 - 25),
- };
- if s == 1 { -v } else { v }
- }
- #[test]
- fn small_floats_widen_as_halves() {
- // Unsigned 11- and 10-bit floats: a 5-bit exponent biased 15 over a
- // 6- or 5-bit mantissa, no sign; values from the D3D/GL spec.
- let f11 = |x: u32| half(x << half_shift(11));
- let f10 = |x: u32| half(x << half_shift(10));
- assert_eq!(f11(0), 0.0);
- assert_eq!(f11(0x001), 2f32.powi(-20)); // smallest denormal
- assert_eq!(f11(0x03F), 63.0 * 2f32.powi(-20)); // largest denormal
- assert_eq!(f11(0x040), 2f32.powi(-14)); // smallest normal
- assert_eq!(f11(0x3C0), 1.0);
- assert_eq!(f11(0x3E0), 1.5);
- assert_eq!(f11(0x7BF), 65024.0); // largest
- assert_eq!(f11(0x7C0), f32::INFINITY);
- assert!(f11(0x7C1).is_nan());
- assert_eq!(f10(0x001), 2f32.powi(-19));
- assert_eq!(f10(0x020), 2f32.powi(-14));
- assert_eq!(f10(0x1E0), 1.0);
- assert_eq!(f10(0x3DF), 64512.0);
- assert_eq!(f10(0x3E0), f32::INFINITY);
- assert!(f10(0x3FF).is_nan());
- // A 16-bit float is a half already, sign included.
- assert_eq!(half(0xBC00 << half_shift(16)), -1.0);
- }
- }
|