| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682 |
- //! 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];
- // `None`: a family not decoded (packed floats, 10_10_10_2).
- const FAMILIES: [(Option<&[u32]>, &[Numeric]); 14] = [
- (Some(&[8]), NORM),
- (Some(&[16]), ALL),
- (Some(&[8, 8]), NORM),
- (Some(&[32]), WIDE),
- (Some(&[16, 16]), ALL),
- (None, ALL), // 10_11_11
- (None, ALL), // 11_11_10
- (None, NORM), // 10_10_10_2
- (Some(&[10, 10, 10, 2]), NORM), // 2_10_10_10
- (Some(&[8, 8, 8, 8]), NORM),
- (Some(&[32, 32]), WIDE),
- (Some(&[16, 16, 16, 16]), ALL),
- (Some(&[32, 32, 32]), WIDE),
- (Some(&[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
- }
- 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(_) => match io_inputs.get(&v) {
- Some(&input) => self.input(input),
- 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"),
- }
- }
- 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],
- )
- }
- 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_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, and the packed float and 10_10_10_2
- // layouts are refused; decode them when a program needs them. 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 f = self
- .s
- .op(c, "legacyF16ToF32", Attr::ReadNone, f32t, 131, &[field]);
- 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.binary(35, a, bb),
- _ => self.binary(36, 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.binary(35, a, bb);
- self.binary(35, t, cc)
- }
- "v_min3_f32" => {
- let t = self.binary(36, a, bb);
- self.binary(36, 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 (op, self.src0) {
- (_, Some(x)) if k == 0 => x,
- (Operand::Const(c @ 240..=248), _) => {
- let h = [
- 0x3800, 0xB800, 0x3C00, 0xBC00, 0x4000, 0xC000, 0x4400, 0xC400, 0x3118,
- ][usize::from(c - 240)];
- self.s.m.i32(h)
- }
- _ => self.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",
- ];
- 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.binary(35, a, b),
- _ => self.binary(36, 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.binary(35, a, b);
- self.binary(35, t, acc)
- }
- "v_min3_f16" => {
- let t = self.binary(36, a, b);
- self.binary(36, 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);
- }
- // 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" => {
- // shortcut: converted through 32 bits, so out-of-range values
- // wrap instead of saturating at 16 bits.
- let a = self.half_fsrc(inst, 0)?;
- let op = if name == "v_cvt_u16_f16" {
- Cast::FpToUi
- } else {
- Cast::FpToSi
- };
- self.s.m.cast(c, op, a, i32t)
- }
- _ => {
- 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)
- }
- /// 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. Inline float constants are f16 values.
- fn packed_src(&mut self, inst: &Inst, k: usize, high: 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 = match op {
- Operand::Const(c @ 240..=248) => {
- let h = [
- 0x3800, 0xB800, 0x3C00, 0xBC00, 0x4000, 0xC000, 0x4400, 0xC400, 0x3118,
- ][usize::from(c - 240)];
- // shortcut: taken to fill both halves.
- self.s.m.i32(h | h << 16)
- }
- _ => 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)?;
- 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.binary(36, xs[0], xs[1]),
- "max_f16" => self.binary(35, 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 i64t = self.s.m.int(64);
- self.s.flags |= flags::INT64;
- let wa = self.s.m.cast(c, Cast::ZExt, a, i64t);
- let wb = self.s.m.cast(c, Cast::ZExt, b, i64t);
- let k32 = self.s.m.int_const(i64t, 32);
- let wa = self.s.m.bin(c, Bin::Shl, wa, k32);
- let both = self.s.m.bin(c, Bin::Or, wa, wb);
- 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);
- // Bytes 0-7 of {src0, src1}.
- let k3 = k(self, 3);
- let bits = self.s.m.bin(c, Bin::Shl, sel, k3);
- let bits = self.s.m.cast(c, Cast::ZExt, bits, i64t);
- let x = self.s.m.bin(c, Bin::LShr, both, bits);
- let x = self.s.m.cast(c, Cast::Trunc, x, self.s.t.i32);
- 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" if op == "class" => {
- // Bit k of src1 tests class k: signalling NaN, quiet NaN,
- // -inf, -normal, -denormal, -0, +0, +denormal, +normal, +inf.
- let x = self.fsrc(inst, 0)?;
- let x = self.as_int(x);
- let mask = self.src(inst, 1)?;
- let k = |l: &mut Self, v: u32| l.s.m.i32(v);
- let (k23, k31, kff, mant, quiet) = (
- k(self, 23),
- k(self, 31),
- k(self, 0xFF),
- k(self, 0x7F_FFFF),
- k(self, 0x40_0000),
- );
- let e = self.s.m.bin(c, Bin::LShr, x, k23);
- let e = self.s.m.bin(c, Bin::And, e, kff);
- let m = self.s.m.bin(c, Bin::And, x, mant);
- let neg = self.s.m.bin(c, Bin::LShr, x, k31);
- let zero = k(self, 0);
- let (e0, e255) = (
- self.s.m.cmp(c, pred::EQ, e, zero),
- self.s.m.cmp(c, pred::EQ, e, kff),
- );
- 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, e255, 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, e255);
- 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)?)
- };
- let p = match op {
- "f" => pred::FALSE,
- "lt" => pred::OLT,
- "eq" => pred::OEQ,
- "le" => pred::OLE,
- "gt" => pred::OGT,
- "lg" => pred::ONE,
- "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" => (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 => {
- 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;
- 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, 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();
- }
- }
- #[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, a packed float, past the table.
- assert_eq!(buffer_format(0), None);
- assert_eq!(buffer_format(36), None);
- assert_eq!(buffer_format(78), None);
- }
- }
|