Driver.cpp 245 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394
  1. //===--- Driver.cpp - Clang GCC Compatible Driver -------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "clang/Driver/Driver.h"
  9. #include "ToolChains/AIX.h"
  10. #include "ToolChains/AMDGPU.h"
  11. #include "ToolChains/AMDGPUOpenMP.h"
  12. #include "ToolChains/AVR.h"
  13. #include "ToolChains/Ananas.h"
  14. #include "ToolChains/BareMetal.h"
  15. #include "ToolChains/CSKYToolChain.h"
  16. #include "ToolChains/Clang.h"
  17. #include "ToolChains/CloudABI.h"
  18. #include "ToolChains/Contiki.h"
  19. #include "ToolChains/CrossWindows.h"
  20. #include "ToolChains/Cuda.h"
  21. #include "ToolChains/Darwin.h"
  22. #include "ToolChains/DragonFly.h"
  23. #include "ToolChains/FreeBSD.h"
  24. #include "ToolChains/Fuchsia.h"
  25. #include "ToolChains/Gnu.h"
  26. #include "ToolChains/HIPAMD.h"
  27. #include "ToolChains/HIPSPV.h"
  28. #include "ToolChains/HLSL.h"
  29. #include "ToolChains/Haiku.h"
  30. #include "ToolChains/Hexagon.h"
  31. #include "ToolChains/Hurd.h"
  32. #include "ToolChains/Lanai.h"
  33. #include "ToolChains/Linux.h"
  34. #include "ToolChains/MSP430.h"
  35. #include "ToolChains/MSVC.h"
  36. #include "ToolChains/MinGW.h"
  37. #include "ToolChains/Minix.h"
  38. #include "ToolChains/MipsLinux.h"
  39. #include "ToolChains/Myriad.h"
  40. #include "ToolChains/NaCl.h"
  41. #include "ToolChains/NetBSD.h"
  42. #include "ToolChains/OpenBSD.h"
  43. #include "ToolChains/PPCFreeBSD.h"
  44. #include "ToolChains/PPCLinux.h"
  45. #include "ToolChains/PS4CPU.h"
  46. #include "ToolChains/RISCVToolchain.h"
  47. #include "ToolChains/SPIRV.h"
  48. #include "ToolChains/Solaris.h"
  49. #include "ToolChains/TCE.h"
  50. #include "ToolChains/VEToolchain.h"
  51. #include "ToolChains/WebAssembly.h"
  52. #include "ToolChains/XCore.h"
  53. #include "ToolChains/ZOS.h"
  54. #include "clang/Basic/TargetID.h"
  55. #include "clang/Basic/Version.h"
  56. #include "clang/Config/config.h"
  57. #include "clang/Driver/Action.h"
  58. #include "clang/Driver/Compilation.h"
  59. #include "clang/Driver/DriverDiagnostic.h"
  60. #include "clang/Driver/InputInfo.h"
  61. #include "clang/Driver/Job.h"
  62. #include "clang/Driver/Options.h"
  63. #include "clang/Driver/Phases.h"
  64. #include "clang/Driver/SanitizerArgs.h"
  65. #include "clang/Driver/Tool.h"
  66. #include "clang/Driver/ToolChain.h"
  67. #include "clang/Driver/Types.h"
  68. #include "llvm/ADT/ArrayRef.h"
  69. #include "llvm/ADT/STLExtras.h"
  70. #include "llvm/ADT/SmallSet.h"
  71. #include "llvm/ADT/StringExtras.h"
  72. #include "llvm/ADT/StringRef.h"
  73. #include "llvm/ADT/StringSet.h"
  74. #include "llvm/ADT/StringSwitch.h"
  75. #include "llvm/Config/llvm-config.h"
  76. #include "llvm/MC/TargetRegistry.h"
  77. #include "llvm/Option/Arg.h"
  78. #include "llvm/Option/ArgList.h"
  79. #include "llvm/Option/OptSpecifier.h"
  80. #include "llvm/Option/OptTable.h"
  81. #include "llvm/Option/Option.h"
  82. #include "llvm/Support/CommandLine.h"
  83. #include "llvm/Support/ErrorHandling.h"
  84. #include "llvm/Support/ExitCodes.h"
  85. #include "llvm/Support/FileSystem.h"
  86. #include "llvm/Support/FormatVariadic.h"
  87. #include "llvm/Support/Host.h"
  88. #include "llvm/Support/MD5.h"
  89. #include "llvm/Support/Path.h"
  90. #include "llvm/Support/PrettyStackTrace.h"
  91. #include "llvm/Support/Process.h"
  92. #include "llvm/Support/Program.h"
  93. #include "llvm/Support/StringSaver.h"
  94. #include "llvm/Support/VirtualFileSystem.h"
  95. #include "llvm/Support/raw_ostream.h"
  96. #include <cstdlib> // ::getenv
  97. #include <map>
  98. #include <memory>
  99. #include <optional>
  100. #include <utility>
  101. #if LLVM_ON_UNIX
  102. #include <unistd.h> // getpid
  103. #endif
  104. using namespace clang::driver;
  105. using namespace clang;
  106. using namespace llvm::opt;
  107. static std::optional<llvm::Triple> getOffloadTargetTriple(const Driver &D,
  108. const ArgList &Args) {
  109. auto OffloadTargets = Args.getAllArgValues(options::OPT_offload_EQ);
  110. // Offload compilation flow does not support multiple targets for now. We
  111. // need the HIPActionBuilder (and possibly the CudaActionBuilder{,Base}too)
  112. // to support multiple tool chains first.
  113. switch (OffloadTargets.size()) {
  114. default:
  115. D.Diag(diag::err_drv_only_one_offload_target_supported);
  116. return std::nullopt;
  117. case 0:
  118. D.Diag(diag::err_drv_invalid_or_unsupported_offload_target) << "";
  119. return std::nullopt;
  120. case 1:
  121. break;
  122. }
  123. return llvm::Triple(OffloadTargets[0]);
  124. }
  125. static std::optional<llvm::Triple>
  126. getNVIDIAOffloadTargetTriple(const Driver &D, const ArgList &Args,
  127. const llvm::Triple &HostTriple) {
  128. if (!Args.hasArg(options::OPT_offload_EQ)) {
  129. return llvm::Triple(HostTriple.isArch64Bit() ? "nvptx64-nvidia-cuda"
  130. : "nvptx-nvidia-cuda");
  131. }
  132. auto TT = getOffloadTargetTriple(D, Args);
  133. if (TT && (TT->getArch() == llvm::Triple::spirv32 ||
  134. TT->getArch() == llvm::Triple::spirv64)) {
  135. if (Args.hasArg(options::OPT_emit_llvm))
  136. return TT;
  137. D.Diag(diag::err_drv_cuda_offload_only_emit_bc);
  138. return std::nullopt;
  139. }
  140. D.Diag(diag::err_drv_invalid_or_unsupported_offload_target) << TT->str();
  141. return std::nullopt;
  142. }
  143. static std::optional<llvm::Triple>
  144. getHIPOffloadTargetTriple(const Driver &D, const ArgList &Args) {
  145. if (!Args.hasArg(options::OPT_offload_EQ)) {
  146. return llvm::Triple("amdgcn-amd-amdhsa"); // Default HIP triple.
  147. }
  148. auto TT = getOffloadTargetTriple(D, Args);
  149. if (!TT)
  150. return std::nullopt;
  151. if (TT->getArch() == llvm::Triple::amdgcn &&
  152. TT->getVendor() == llvm::Triple::AMD &&
  153. TT->getOS() == llvm::Triple::AMDHSA)
  154. return TT;
  155. if (TT->getArch() == llvm::Triple::spirv64)
  156. return TT;
  157. D.Diag(diag::err_drv_invalid_or_unsupported_offload_target) << TT->str();
  158. return std::nullopt;
  159. }
  160. // static
  161. std::string Driver::GetResourcesPath(StringRef BinaryPath,
  162. StringRef CustomResourceDir) {
  163. // Since the resource directory is embedded in the module hash, it's important
  164. // that all places that need it call this function, so that they get the
  165. // exact same string ("a/../b/" and "b/" get different hashes, for example).
  166. // Dir is bin/ or lib/, depending on where BinaryPath is.
  167. std::string Dir = std::string(llvm::sys::path::parent_path(BinaryPath));
  168. SmallString<128> P(Dir);
  169. if (CustomResourceDir != "") {
  170. llvm::sys::path::append(P, CustomResourceDir);
  171. } else {
  172. // On Windows, libclang.dll is in bin/.
  173. // On non-Windows, libclang.so/.dylib is in lib/.
  174. // With a static-library build of libclang, LibClangPath will contain the
  175. // path of the embedding binary, which for LLVM binaries will be in bin/.
  176. // ../lib gets us to lib/ in both cases.
  177. P = llvm::sys::path::parent_path(Dir);
  178. llvm::sys::path::append(P, CLANG_INSTALL_LIBDIR_BASENAME, "clang",
  179. CLANG_VERSION_MAJOR_STRING);
  180. }
  181. return std::string(P.str());
  182. }
  183. Driver::Driver(StringRef ClangExecutable, StringRef TargetTriple,
  184. DiagnosticsEngine &Diags, std::string Title,
  185. IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS)
  186. : Diags(Diags), VFS(std::move(VFS)), Mode(GCCMode),
  187. SaveTemps(SaveTempsNone), BitcodeEmbed(EmbedNone),
  188. Offload(OffloadHostDevice), CXX20HeaderType(HeaderMode_None),
  189. ModulesModeCXX20(false), LTOMode(LTOK_None),
  190. ClangExecutable(ClangExecutable), SysRoot(DEFAULT_SYSROOT),
  191. DriverTitle(Title), CCCPrintBindings(false), CCPrintOptions(false),
  192. CCLogDiagnostics(false), CCGenDiagnostics(false),
  193. CCPrintProcessStats(false), TargetTriple(TargetTriple), Saver(Alloc),
  194. CheckInputsExist(true), ProbePrecompiled(true),
  195. SuppressMissingInputWarning(false) {
  196. // Provide a sane fallback if no VFS is specified.
  197. if (!this->VFS)
  198. this->VFS = llvm::vfs::getRealFileSystem();
  199. Name = std::string(llvm::sys::path::filename(ClangExecutable));
  200. Dir = std::string(llvm::sys::path::parent_path(ClangExecutable));
  201. InstalledDir = Dir; // Provide a sensible default installed dir.
  202. if ((!SysRoot.empty()) && llvm::sys::path::is_relative(SysRoot)) {
  203. // Prepend InstalledDir if SysRoot is relative
  204. SmallString<128> P(InstalledDir);
  205. llvm::sys::path::append(P, SysRoot);
  206. SysRoot = std::string(P);
  207. }
  208. #if defined(CLANG_CONFIG_FILE_SYSTEM_DIR)
  209. SystemConfigDir = CLANG_CONFIG_FILE_SYSTEM_DIR;
  210. #endif
  211. #if defined(CLANG_CONFIG_FILE_USER_DIR)
  212. {
  213. SmallString<128> P;
  214. llvm::sys::fs::expand_tilde(CLANG_CONFIG_FILE_USER_DIR, P);
  215. UserConfigDir = static_cast<std::string>(P);
  216. }
  217. #endif
  218. // Compute the path to the resource directory.
  219. ResourceDir = GetResourcesPath(ClangExecutable, CLANG_RESOURCE_DIR);
  220. }
  221. void Driver::setDriverMode(StringRef Value) {
  222. static const std::string OptName =
  223. getOpts().getOption(options::OPT_driver_mode).getPrefixedName();
  224. if (auto M = llvm::StringSwitch<std::optional<DriverMode>>(Value)
  225. .Case("gcc", GCCMode)
  226. .Case("g++", GXXMode)
  227. .Case("cpp", CPPMode)
  228. .Case("cl", CLMode)
  229. .Case("flang", FlangMode)
  230. .Case("dxc", DXCMode)
  231. .Default(std::nullopt))
  232. Mode = *M;
  233. else
  234. Diag(diag::err_drv_unsupported_option_argument) << OptName << Value;
  235. }
  236. InputArgList Driver::ParseArgStrings(ArrayRef<const char *> ArgStrings,
  237. bool IsClCompatMode,
  238. bool &ContainsError) {
  239. llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
  240. ContainsError = false;
  241. unsigned IncludedFlagsBitmask;
  242. unsigned ExcludedFlagsBitmask;
  243. std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) =
  244. getIncludeExcludeOptionFlagMasks(IsClCompatMode);
  245. // Make sure that Flang-only options don't pollute the Clang output
  246. // TODO: Make sure that Clang-only options don't pollute Flang output
  247. if (!IsFlangMode())
  248. ExcludedFlagsBitmask |= options::FlangOnlyOption;
  249. unsigned MissingArgIndex, MissingArgCount;
  250. InputArgList Args =
  251. getOpts().ParseArgs(ArgStrings, MissingArgIndex, MissingArgCount,
  252. IncludedFlagsBitmask, ExcludedFlagsBitmask);
  253. // Check for missing argument error.
  254. if (MissingArgCount) {
  255. Diag(diag::err_drv_missing_argument)
  256. << Args.getArgString(MissingArgIndex) << MissingArgCount;
  257. ContainsError |=
  258. Diags.getDiagnosticLevel(diag::err_drv_missing_argument,
  259. SourceLocation()) > DiagnosticsEngine::Warning;
  260. }
  261. // Check for unsupported options.
  262. for (const Arg *A : Args) {
  263. if (A->getOption().hasFlag(options::Unsupported)) {
  264. unsigned DiagID;
  265. auto ArgString = A->getAsString(Args);
  266. std::string Nearest;
  267. if (getOpts().findNearest(
  268. ArgString, Nearest, IncludedFlagsBitmask,
  269. ExcludedFlagsBitmask | options::Unsupported) > 1) {
  270. DiagID = diag::err_drv_unsupported_opt;
  271. Diag(DiagID) << ArgString;
  272. } else {
  273. DiagID = diag::err_drv_unsupported_opt_with_suggestion;
  274. Diag(DiagID) << ArgString << Nearest;
  275. }
  276. ContainsError |= Diags.getDiagnosticLevel(DiagID, SourceLocation()) >
  277. DiagnosticsEngine::Warning;
  278. continue;
  279. }
  280. // Warn about -mcpu= without an argument.
  281. if (A->getOption().matches(options::OPT_mcpu_EQ) && A->containsValue("")) {
  282. Diag(diag::warn_drv_empty_joined_argument) << A->getAsString(Args);
  283. ContainsError |= Diags.getDiagnosticLevel(
  284. diag::warn_drv_empty_joined_argument,
  285. SourceLocation()) > DiagnosticsEngine::Warning;
  286. }
  287. }
  288. for (const Arg *A : Args.filtered(options::OPT_UNKNOWN)) {
  289. unsigned DiagID;
  290. auto ArgString = A->getAsString(Args);
  291. std::string Nearest;
  292. if (getOpts().findNearest(ArgString, Nearest, IncludedFlagsBitmask,
  293. ExcludedFlagsBitmask) > 1) {
  294. if (!IsCLMode() &&
  295. getOpts().findExact(ArgString, Nearest, options::CC1Option)) {
  296. DiagID = diag::err_drv_unknown_argument_with_suggestion;
  297. Diags.Report(DiagID) << ArgString << "-Xclang " + Nearest;
  298. } else {
  299. DiagID = IsCLMode() ? diag::warn_drv_unknown_argument_clang_cl
  300. : diag::err_drv_unknown_argument;
  301. Diags.Report(DiagID) << ArgString;
  302. }
  303. } else {
  304. DiagID = IsCLMode()
  305. ? diag::warn_drv_unknown_argument_clang_cl_with_suggestion
  306. : diag::err_drv_unknown_argument_with_suggestion;
  307. Diags.Report(DiagID) << ArgString << Nearest;
  308. }
  309. ContainsError |= Diags.getDiagnosticLevel(DiagID, SourceLocation()) >
  310. DiagnosticsEngine::Warning;
  311. }
  312. for (const Arg *A : Args.filtered(options::OPT_o)) {
  313. if (ArgStrings[A->getIndex()] == A->getSpelling())
  314. continue;
  315. // Warn on joined arguments that are similar to a long argument.
  316. std::string ArgString = ArgStrings[A->getIndex()];
  317. std::string Nearest;
  318. if (getOpts().findExact("-" + ArgString, Nearest, IncludedFlagsBitmask,
  319. ExcludedFlagsBitmask))
  320. Diags.Report(diag::warn_drv_potentially_misspelled_joined_argument)
  321. << A->getAsString(Args) << Nearest;
  322. }
  323. return Args;
  324. }
  325. // Determine which compilation mode we are in. We look for options which
  326. // affect the phase, starting with the earliest phases, and record which
  327. // option we used to determine the final phase.
  328. phases::ID Driver::getFinalPhase(const DerivedArgList &DAL,
  329. Arg **FinalPhaseArg) const {
  330. Arg *PhaseArg = nullptr;
  331. phases::ID FinalPhase;
  332. // -{E,EP,P,M,MM} only run the preprocessor.
  333. if (CCCIsCPP() || (PhaseArg = DAL.getLastArg(options::OPT_E)) ||
  334. (PhaseArg = DAL.getLastArg(options::OPT__SLASH_EP)) ||
  335. (PhaseArg = DAL.getLastArg(options::OPT_M, options::OPT_MM)) ||
  336. (PhaseArg = DAL.getLastArg(options::OPT__SLASH_P)) ||
  337. CCGenDiagnostics) {
  338. FinalPhase = phases::Preprocess;
  339. // --precompile only runs up to precompilation.
  340. // Options that cause the output of C++20 compiled module interfaces or
  341. // header units have the same effect.
  342. } else if ((PhaseArg = DAL.getLastArg(options::OPT__precompile)) ||
  343. (PhaseArg = DAL.getLastArg(options::OPT_extract_api)) ||
  344. (PhaseArg = DAL.getLastArg(options::OPT_fmodule_header,
  345. options::OPT_fmodule_header_EQ))) {
  346. FinalPhase = phases::Precompile;
  347. // -{fsyntax-only,-analyze,emit-ast} only run up to the compiler.
  348. } else if ((PhaseArg = DAL.getLastArg(options::OPT_fsyntax_only)) ||
  349. (PhaseArg = DAL.getLastArg(options::OPT_print_supported_cpus)) ||
  350. (PhaseArg = DAL.getLastArg(options::OPT_module_file_info)) ||
  351. (PhaseArg = DAL.getLastArg(options::OPT_verify_pch)) ||
  352. (PhaseArg = DAL.getLastArg(options::OPT_rewrite_objc)) ||
  353. (PhaseArg = DAL.getLastArg(options::OPT_rewrite_legacy_objc)) ||
  354. (PhaseArg = DAL.getLastArg(options::OPT__migrate)) ||
  355. (PhaseArg = DAL.getLastArg(options::OPT__analyze)) ||
  356. (PhaseArg = DAL.getLastArg(options::OPT_emit_ast))) {
  357. FinalPhase = phases::Compile;
  358. // -S only runs up to the backend.
  359. } else if ((PhaseArg = DAL.getLastArg(options::OPT_S))) {
  360. FinalPhase = phases::Backend;
  361. // -c compilation only runs up to the assembler.
  362. } else if ((PhaseArg = DAL.getLastArg(options::OPT_c))) {
  363. FinalPhase = phases::Assemble;
  364. } else if ((PhaseArg = DAL.getLastArg(options::OPT_emit_interface_stubs))) {
  365. FinalPhase = phases::IfsMerge;
  366. // Otherwise do everything.
  367. } else
  368. FinalPhase = phases::Link;
  369. if (FinalPhaseArg)
  370. *FinalPhaseArg = PhaseArg;
  371. return FinalPhase;
  372. }
  373. static Arg *MakeInputArg(DerivedArgList &Args, const OptTable &Opts,
  374. StringRef Value, bool Claim = true) {
  375. Arg *A = new Arg(Opts.getOption(options::OPT_INPUT), Value,
  376. Args.getBaseArgs().MakeIndex(Value), Value.data());
  377. Args.AddSynthesizedArg(A);
  378. if (Claim)
  379. A->claim();
  380. return A;
  381. }
  382. DerivedArgList *Driver::TranslateInputArgs(const InputArgList &Args) const {
  383. const llvm::opt::OptTable &Opts = getOpts();
  384. DerivedArgList *DAL = new DerivedArgList(Args);
  385. bool HasNostdlib = Args.hasArg(options::OPT_nostdlib);
  386. bool HasNostdlibxx = Args.hasArg(options::OPT_nostdlibxx);
  387. bool HasNodefaultlib = Args.hasArg(options::OPT_nodefaultlibs);
  388. bool IgnoreUnused = false;
  389. for (Arg *A : Args) {
  390. if (IgnoreUnused)
  391. A->claim();
  392. if (A->getOption().matches(options::OPT_start_no_unused_arguments)) {
  393. IgnoreUnused = true;
  394. continue;
  395. }
  396. if (A->getOption().matches(options::OPT_end_no_unused_arguments)) {
  397. IgnoreUnused = false;
  398. continue;
  399. }
  400. // Unfortunately, we have to parse some forwarding options (-Xassembler,
  401. // -Xlinker, -Xpreprocessor) because we either integrate their functionality
  402. // (assembler and preprocessor), or bypass a previous driver ('collect2').
  403. // Rewrite linker options, to replace --no-demangle with a custom internal
  404. // option.
  405. if ((A->getOption().matches(options::OPT_Wl_COMMA) ||
  406. A->getOption().matches(options::OPT_Xlinker)) &&
  407. A->containsValue("--no-demangle")) {
  408. // Add the rewritten no-demangle argument.
  409. DAL->AddFlagArg(A, Opts.getOption(options::OPT_Z_Xlinker__no_demangle));
  410. // Add the remaining values as Xlinker arguments.
  411. for (StringRef Val : A->getValues())
  412. if (Val != "--no-demangle")
  413. DAL->AddSeparateArg(A, Opts.getOption(options::OPT_Xlinker), Val);
  414. continue;
  415. }
  416. // Rewrite preprocessor options, to replace -Wp,-MD,FOO which is used by
  417. // some build systems. We don't try to be complete here because we don't
  418. // care to encourage this usage model.
  419. if (A->getOption().matches(options::OPT_Wp_COMMA) &&
  420. (A->getValue(0) == StringRef("-MD") ||
  421. A->getValue(0) == StringRef("-MMD"))) {
  422. // Rewrite to -MD/-MMD along with -MF.
  423. if (A->getValue(0) == StringRef("-MD"))
  424. DAL->AddFlagArg(A, Opts.getOption(options::OPT_MD));
  425. else
  426. DAL->AddFlagArg(A, Opts.getOption(options::OPT_MMD));
  427. if (A->getNumValues() == 2)
  428. DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF), A->getValue(1));
  429. continue;
  430. }
  431. // Rewrite reserved library names.
  432. if (A->getOption().matches(options::OPT_l)) {
  433. StringRef Value = A->getValue();
  434. // Rewrite unless -nostdlib is present.
  435. if (!HasNostdlib && !HasNodefaultlib && !HasNostdlibxx &&
  436. Value == "stdc++") {
  437. DAL->AddFlagArg(A, Opts.getOption(options::OPT_Z_reserved_lib_stdcxx));
  438. continue;
  439. }
  440. // Rewrite unconditionally.
  441. if (Value == "cc_kext") {
  442. DAL->AddFlagArg(A, Opts.getOption(options::OPT_Z_reserved_lib_cckext));
  443. continue;
  444. }
  445. }
  446. // Pick up inputs via the -- option.
  447. if (A->getOption().matches(options::OPT__DASH_DASH)) {
  448. A->claim();
  449. for (StringRef Val : A->getValues())
  450. DAL->append(MakeInputArg(*DAL, Opts, Val, false));
  451. continue;
  452. }
  453. DAL->append(A);
  454. }
  455. // Enforce -static if -miamcu is present.
  456. if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false))
  457. DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_static));
  458. // Add a default value of -mlinker-version=, if one was given and the user
  459. // didn't specify one.
  460. #if defined(HOST_LINK_VERSION)
  461. if (!Args.hasArg(options::OPT_mlinker_version_EQ) &&
  462. strlen(HOST_LINK_VERSION) > 0) {
  463. DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mlinker_version_EQ),
  464. HOST_LINK_VERSION);
  465. DAL->getLastArg(options::OPT_mlinker_version_EQ)->claim();
  466. }
  467. #endif
  468. return DAL;
  469. }
  470. /// Compute target triple from args.
  471. ///
  472. /// This routine provides the logic to compute a target triple from various
  473. /// args passed to the driver and the default triple string.
  474. static llvm::Triple computeTargetTriple(const Driver &D,
  475. StringRef TargetTriple,
  476. const ArgList &Args,
  477. StringRef DarwinArchName = "") {
  478. // FIXME: Already done in Compilation *Driver::BuildCompilation
  479. if (const Arg *A = Args.getLastArg(options::OPT_target))
  480. TargetTriple = A->getValue();
  481. llvm::Triple Target(llvm::Triple::normalize(TargetTriple));
  482. // GNU/Hurd's triples should have been -hurd-gnu*, but were historically made
  483. // -gnu* only, and we can not change this, so we have to detect that case as
  484. // being the Hurd OS.
  485. if (TargetTriple.contains("-unknown-gnu") || TargetTriple.contains("-pc-gnu"))
  486. Target.setOSName("hurd");
  487. // Handle Apple-specific options available here.
  488. if (Target.isOSBinFormatMachO()) {
  489. // If an explicit Darwin arch name is given, that trumps all.
  490. if (!DarwinArchName.empty()) {
  491. tools::darwin::setTripleTypeForMachOArchName(Target, DarwinArchName);
  492. return Target;
  493. }
  494. // Handle the Darwin '-arch' flag.
  495. if (Arg *A = Args.getLastArg(options::OPT_arch)) {
  496. StringRef ArchName = A->getValue();
  497. tools::darwin::setTripleTypeForMachOArchName(Target, ArchName);
  498. }
  499. }
  500. // Handle pseudo-target flags '-mlittle-endian'/'-EL' and
  501. // '-mbig-endian'/'-EB'.
  502. if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
  503. options::OPT_mbig_endian)) {
  504. if (A->getOption().matches(options::OPT_mlittle_endian)) {
  505. llvm::Triple LE = Target.getLittleEndianArchVariant();
  506. if (LE.getArch() != llvm::Triple::UnknownArch)
  507. Target = std::move(LE);
  508. } else {
  509. llvm::Triple BE = Target.getBigEndianArchVariant();
  510. if (BE.getArch() != llvm::Triple::UnknownArch)
  511. Target = std::move(BE);
  512. }
  513. }
  514. // Skip further flag support on OSes which don't support '-m32' or '-m64'.
  515. if (Target.getArch() == llvm::Triple::tce ||
  516. Target.getOS() == llvm::Triple::Minix)
  517. return Target;
  518. // On AIX, the env OBJECT_MODE may affect the resulting arch variant.
  519. if (Target.isOSAIX()) {
  520. if (std::optional<std::string> ObjectModeValue =
  521. llvm::sys::Process::GetEnv("OBJECT_MODE")) {
  522. StringRef ObjectMode = *ObjectModeValue;
  523. llvm::Triple::ArchType AT = llvm::Triple::UnknownArch;
  524. if (ObjectMode.equals("64")) {
  525. AT = Target.get64BitArchVariant().getArch();
  526. } else if (ObjectMode.equals("32")) {
  527. AT = Target.get32BitArchVariant().getArch();
  528. } else {
  529. D.Diag(diag::err_drv_invalid_object_mode) << ObjectMode;
  530. }
  531. if (AT != llvm::Triple::UnknownArch && AT != Target.getArch())
  532. Target.setArch(AT);
  533. }
  534. }
  535. // Handle pseudo-target flags '-m64', '-mx32', '-m32' and '-m16'.
  536. Arg *A = Args.getLastArg(options::OPT_m64, options::OPT_mx32,
  537. options::OPT_m32, options::OPT_m16);
  538. if (A) {
  539. llvm::Triple::ArchType AT = llvm::Triple::UnknownArch;
  540. if (A->getOption().matches(options::OPT_m64)) {
  541. AT = Target.get64BitArchVariant().getArch();
  542. if (Target.getEnvironment() == llvm::Triple::GNUX32)
  543. Target.setEnvironment(llvm::Triple::GNU);
  544. else if (Target.getEnvironment() == llvm::Triple::MuslX32)
  545. Target.setEnvironment(llvm::Triple::Musl);
  546. } else if (A->getOption().matches(options::OPT_mx32) &&
  547. Target.get64BitArchVariant().getArch() == llvm::Triple::x86_64) {
  548. AT = llvm::Triple::x86_64;
  549. if (Target.getEnvironment() == llvm::Triple::Musl)
  550. Target.setEnvironment(llvm::Triple::MuslX32);
  551. else
  552. Target.setEnvironment(llvm::Triple::GNUX32);
  553. } else if (A->getOption().matches(options::OPT_m32)) {
  554. AT = Target.get32BitArchVariant().getArch();
  555. if (Target.getEnvironment() == llvm::Triple::GNUX32)
  556. Target.setEnvironment(llvm::Triple::GNU);
  557. else if (Target.getEnvironment() == llvm::Triple::MuslX32)
  558. Target.setEnvironment(llvm::Triple::Musl);
  559. } else if (A->getOption().matches(options::OPT_m16) &&
  560. Target.get32BitArchVariant().getArch() == llvm::Triple::x86) {
  561. AT = llvm::Triple::x86;
  562. Target.setEnvironment(llvm::Triple::CODE16);
  563. }
  564. if (AT != llvm::Triple::UnknownArch && AT != Target.getArch()) {
  565. Target.setArch(AT);
  566. if (Target.isWindowsGNUEnvironment())
  567. toolchains::MinGW::fixTripleArch(D, Target, Args);
  568. }
  569. }
  570. // Handle -miamcu flag.
  571. if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) {
  572. if (Target.get32BitArchVariant().getArch() != llvm::Triple::x86)
  573. D.Diag(diag::err_drv_unsupported_opt_for_target) << "-miamcu"
  574. << Target.str();
  575. if (A && !A->getOption().matches(options::OPT_m32))
  576. D.Diag(diag::err_drv_argument_not_allowed_with)
  577. << "-miamcu" << A->getBaseArg().getAsString(Args);
  578. Target.setArch(llvm::Triple::x86);
  579. Target.setArchName("i586");
  580. Target.setEnvironment(llvm::Triple::UnknownEnvironment);
  581. Target.setEnvironmentName("");
  582. Target.setOS(llvm::Triple::ELFIAMCU);
  583. Target.setVendor(llvm::Triple::UnknownVendor);
  584. Target.setVendorName("intel");
  585. }
  586. // If target is MIPS adjust the target triple
  587. // accordingly to provided ABI name.
  588. if (Target.isMIPS()) {
  589. if ((A = Args.getLastArg(options::OPT_mabi_EQ))) {
  590. StringRef ABIName = A->getValue();
  591. if (ABIName == "32") {
  592. Target = Target.get32BitArchVariant();
  593. if (Target.getEnvironment() == llvm::Triple::GNUABI64 ||
  594. Target.getEnvironment() == llvm::Triple::GNUABIN32)
  595. Target.setEnvironment(llvm::Triple::GNU);
  596. } else if (ABIName == "n32") {
  597. Target = Target.get64BitArchVariant();
  598. if (Target.getEnvironment() == llvm::Triple::GNU ||
  599. Target.getEnvironment() == llvm::Triple::GNUABI64)
  600. Target.setEnvironment(llvm::Triple::GNUABIN32);
  601. } else if (ABIName == "64") {
  602. Target = Target.get64BitArchVariant();
  603. if (Target.getEnvironment() == llvm::Triple::GNU ||
  604. Target.getEnvironment() == llvm::Triple::GNUABIN32)
  605. Target.setEnvironment(llvm::Triple::GNUABI64);
  606. }
  607. }
  608. }
  609. // If target is RISC-V adjust the target triple according to
  610. // provided architecture name
  611. if (Target.isRISCV()) {
  612. if ((A = Args.getLastArg(options::OPT_march_EQ))) {
  613. StringRef ArchName = A->getValue();
  614. if (ArchName.startswith_insensitive("rv32"))
  615. Target.setArch(llvm::Triple::riscv32);
  616. else if (ArchName.startswith_insensitive("rv64"))
  617. Target.setArch(llvm::Triple::riscv64);
  618. }
  619. }
  620. return Target;
  621. }
  622. // Parse the LTO options and record the type of LTO compilation
  623. // based on which -f(no-)?lto(=.*)? or -f(no-)?offload-lto(=.*)?
  624. // option occurs last.
  625. static driver::LTOKind parseLTOMode(Driver &D, const llvm::opt::ArgList &Args,
  626. OptSpecifier OptEq, OptSpecifier OptNeg) {
  627. if (!Args.hasFlag(OptEq, OptNeg, false))
  628. return LTOK_None;
  629. const Arg *A = Args.getLastArg(OptEq);
  630. StringRef LTOName = A->getValue();
  631. driver::LTOKind LTOMode = llvm::StringSwitch<LTOKind>(LTOName)
  632. .Case("full", LTOK_Full)
  633. .Case("thin", LTOK_Thin)
  634. .Default(LTOK_Unknown);
  635. if (LTOMode == LTOK_Unknown) {
  636. D.Diag(diag::err_drv_unsupported_option_argument)
  637. << A->getSpelling() << A->getValue();
  638. return LTOK_None;
  639. }
  640. return LTOMode;
  641. }
  642. // Parse the LTO options.
  643. void Driver::setLTOMode(const llvm::opt::ArgList &Args) {
  644. LTOMode =
  645. parseLTOMode(*this, Args, options::OPT_flto_EQ, options::OPT_fno_lto);
  646. OffloadLTOMode = parseLTOMode(*this, Args, options::OPT_foffload_lto_EQ,
  647. options::OPT_fno_offload_lto);
  648. // Try to enable `-foffload-lto=full` if `-fopenmp-target-jit` is on.
  649. if (Args.hasFlag(options::OPT_fopenmp_target_jit,
  650. options::OPT_fno_openmp_target_jit, false)) {
  651. if (Arg *A = Args.getLastArg(options::OPT_foffload_lto_EQ,
  652. options::OPT_fno_offload_lto))
  653. if (OffloadLTOMode != LTOK_Full)
  654. Diag(diag::err_drv_incompatible_options)
  655. << A->getSpelling() << "-fopenmp-target-jit";
  656. OffloadLTOMode = LTOK_Full;
  657. }
  658. }
  659. /// Compute the desired OpenMP runtime from the flags provided.
  660. Driver::OpenMPRuntimeKind Driver::getOpenMPRuntime(const ArgList &Args) const {
  661. StringRef RuntimeName(CLANG_DEFAULT_OPENMP_RUNTIME);
  662. const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ);
  663. if (A)
  664. RuntimeName = A->getValue();
  665. auto RT = llvm::StringSwitch<OpenMPRuntimeKind>(RuntimeName)
  666. .Case("libomp", OMPRT_OMP)
  667. .Case("libgomp", OMPRT_GOMP)
  668. .Case("libiomp5", OMPRT_IOMP5)
  669. .Default(OMPRT_Unknown);
  670. if (RT == OMPRT_Unknown) {
  671. if (A)
  672. Diag(diag::err_drv_unsupported_option_argument)
  673. << A->getSpelling() << A->getValue();
  674. else
  675. // FIXME: We could use a nicer diagnostic here.
  676. Diag(diag::err_drv_unsupported_opt) << "-fopenmp";
  677. }
  678. return RT;
  679. }
  680. void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
  681. InputList &Inputs) {
  682. //
  683. // CUDA/HIP
  684. //
  685. // We need to generate a CUDA/HIP toolchain if any of the inputs has a CUDA
  686. // or HIP type. However, mixed CUDA/HIP compilation is not supported.
  687. bool IsCuda =
  688. llvm::any_of(Inputs, [](std::pair<types::ID, const llvm::opt::Arg *> &I) {
  689. return types::isCuda(I.first);
  690. });
  691. bool IsHIP =
  692. llvm::any_of(Inputs,
  693. [](std::pair<types::ID, const llvm::opt::Arg *> &I) {
  694. return types::isHIP(I.first);
  695. }) ||
  696. C.getInputArgs().hasArg(options::OPT_hip_link);
  697. if (IsCuda && IsHIP) {
  698. Diag(clang::diag::err_drv_mix_cuda_hip);
  699. return;
  700. }
  701. if (IsCuda) {
  702. const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>();
  703. const llvm::Triple &HostTriple = HostTC->getTriple();
  704. auto OFK = Action::OFK_Cuda;
  705. auto CudaTriple =
  706. getNVIDIAOffloadTargetTriple(*this, C.getInputArgs(), HostTriple);
  707. if (!CudaTriple)
  708. return;
  709. // Use the CUDA and host triples as the key into the ToolChains map,
  710. // because the device toolchain we create depends on both.
  711. auto &CudaTC = ToolChains[CudaTriple->str() + "/" + HostTriple.str()];
  712. if (!CudaTC) {
  713. CudaTC = std::make_unique<toolchains::CudaToolChain>(
  714. *this, *CudaTriple, *HostTC, C.getInputArgs());
  715. }
  716. C.addOffloadDeviceToolChain(CudaTC.get(), OFK);
  717. } else if (IsHIP) {
  718. if (auto *OMPTargetArg =
  719. C.getInputArgs().getLastArg(options::OPT_fopenmp_targets_EQ)) {
  720. Diag(clang::diag::err_drv_unsupported_opt_for_language_mode)
  721. << OMPTargetArg->getSpelling() << "HIP";
  722. return;
  723. }
  724. const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>();
  725. auto OFK = Action::OFK_HIP;
  726. auto HIPTriple = getHIPOffloadTargetTriple(*this, C.getInputArgs());
  727. if (!HIPTriple)
  728. return;
  729. auto *HIPTC = &getOffloadingDeviceToolChain(C.getInputArgs(), *HIPTriple,
  730. *HostTC, OFK);
  731. assert(HIPTC && "Could not create offloading device tool chain.");
  732. C.addOffloadDeviceToolChain(HIPTC, OFK);
  733. }
  734. //
  735. // OpenMP
  736. //
  737. // We need to generate an OpenMP toolchain if the user specified targets with
  738. // the -fopenmp-targets option or used --offload-arch with OpenMP enabled.
  739. bool IsOpenMPOffloading =
  740. C.getInputArgs().hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
  741. options::OPT_fno_openmp, false) &&
  742. (C.getInputArgs().hasArg(options::OPT_fopenmp_targets_EQ) ||
  743. C.getInputArgs().hasArg(options::OPT_offload_arch_EQ));
  744. if (IsOpenMPOffloading) {
  745. // We expect that -fopenmp-targets is always used in conjunction with the
  746. // option -fopenmp specifying a valid runtime with offloading support, i.e.
  747. // libomp or libiomp.
  748. OpenMPRuntimeKind RuntimeKind = getOpenMPRuntime(C.getInputArgs());
  749. if (RuntimeKind != OMPRT_OMP && RuntimeKind != OMPRT_IOMP5) {
  750. Diag(clang::diag::err_drv_expecting_fopenmp_with_fopenmp_targets);
  751. return;
  752. }
  753. llvm::StringMap<llvm::DenseSet<StringRef>> DerivedArchs;
  754. llvm::StringMap<StringRef> FoundNormalizedTriples;
  755. llvm::SmallVector<StringRef, 4> OpenMPTriples;
  756. // If the user specified -fopenmp-targets= we create a toolchain for each
  757. // valid triple. Otherwise, if only --offload-arch= was specified we instead
  758. // attempt to derive the appropriate toolchains from the arguments.
  759. if (Arg *OpenMPTargets =
  760. C.getInputArgs().getLastArg(options::OPT_fopenmp_targets_EQ)) {
  761. if (OpenMPTargets && !OpenMPTargets->getNumValues()) {
  762. Diag(clang::diag::warn_drv_empty_joined_argument)
  763. << OpenMPTargets->getAsString(C.getInputArgs());
  764. return;
  765. }
  766. llvm::copy(OpenMPTargets->getValues(), std::back_inserter(OpenMPTriples));
  767. } else if (C.getInputArgs().hasArg(options::OPT_offload_arch_EQ) &&
  768. !IsHIP && !IsCuda) {
  769. const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>();
  770. auto AMDTriple = getHIPOffloadTargetTriple(*this, C.getInputArgs());
  771. auto NVPTXTriple = getNVIDIAOffloadTargetTriple(*this, C.getInputArgs(),
  772. HostTC->getTriple());
  773. // Attempt to deduce the offloading triple from the set of architectures.
  774. // We can only correctly deduce NVPTX / AMDGPU triples currently. We need
  775. // to temporarily create these toolchains so that we can access tools for
  776. // inferring architectures.
  777. llvm::DenseSet<StringRef> Archs;
  778. if (NVPTXTriple) {
  779. auto TempTC = std::make_unique<toolchains::CudaToolChain>(
  780. *this, *NVPTXTriple, *HostTC, C.getInputArgs());
  781. for (StringRef Arch : getOffloadArchs(
  782. C, C.getArgs(), Action::OFK_OpenMP, &*TempTC, true))
  783. Archs.insert(Arch);
  784. }
  785. if (AMDTriple) {
  786. auto TempTC = std::make_unique<toolchains::AMDGPUOpenMPToolChain>(
  787. *this, *AMDTriple, *HostTC, C.getInputArgs());
  788. for (StringRef Arch : getOffloadArchs(
  789. C, C.getArgs(), Action::OFK_OpenMP, &*TempTC, true))
  790. Archs.insert(Arch);
  791. }
  792. if (!AMDTriple && !NVPTXTriple) {
  793. for (StringRef Arch :
  794. getOffloadArchs(C, C.getArgs(), Action::OFK_OpenMP, nullptr, true))
  795. Archs.insert(Arch);
  796. }
  797. for (StringRef Arch : Archs) {
  798. if (NVPTXTriple && IsNVIDIAGpuArch(StringToCudaArch(
  799. getProcessorFromTargetID(*NVPTXTriple, Arch)))) {
  800. DerivedArchs[NVPTXTriple->getTriple()].insert(Arch);
  801. } else if (AMDTriple &&
  802. IsAMDGpuArch(StringToCudaArch(
  803. getProcessorFromTargetID(*AMDTriple, Arch)))) {
  804. DerivedArchs[AMDTriple->getTriple()].insert(Arch);
  805. } else {
  806. Diag(clang::diag::err_drv_failed_to_deduce_target_from_arch) << Arch;
  807. return;
  808. }
  809. }
  810. // If the set is empty then we failed to find a native architecture.
  811. if (Archs.empty()) {
  812. Diag(clang::diag::err_drv_failed_to_deduce_target_from_arch)
  813. << "native";
  814. return;
  815. }
  816. for (const auto &TripleAndArchs : DerivedArchs)
  817. OpenMPTriples.push_back(TripleAndArchs.first());
  818. }
  819. for (StringRef Val : OpenMPTriples) {
  820. llvm::Triple TT(ToolChain::getOpenMPTriple(Val));
  821. std::string NormalizedName = TT.normalize();
  822. // Make sure we don't have a duplicate triple.
  823. auto Duplicate = FoundNormalizedTriples.find(NormalizedName);
  824. if (Duplicate != FoundNormalizedTriples.end()) {
  825. Diag(clang::diag::warn_drv_omp_offload_target_duplicate)
  826. << Val << Duplicate->second;
  827. continue;
  828. }
  829. // Store the current triple so that we can check for duplicates in the
  830. // following iterations.
  831. FoundNormalizedTriples[NormalizedName] = Val;
  832. // If the specified target is invalid, emit a diagnostic.
  833. if (TT.getArch() == llvm::Triple::UnknownArch)
  834. Diag(clang::diag::err_drv_invalid_omp_target) << Val;
  835. else {
  836. const ToolChain *TC;
  837. // Device toolchains have to be selected differently. They pair host
  838. // and device in their implementation.
  839. if (TT.isNVPTX() || TT.isAMDGCN()) {
  840. const ToolChain *HostTC =
  841. C.getSingleOffloadToolChain<Action::OFK_Host>();
  842. assert(HostTC && "Host toolchain should be always defined.");
  843. auto &DeviceTC =
  844. ToolChains[TT.str() + "/" + HostTC->getTriple().normalize()];
  845. if (!DeviceTC) {
  846. if (TT.isNVPTX())
  847. DeviceTC = std::make_unique<toolchains::CudaToolChain>(
  848. *this, TT, *HostTC, C.getInputArgs());
  849. else if (TT.isAMDGCN())
  850. DeviceTC = std::make_unique<toolchains::AMDGPUOpenMPToolChain>(
  851. *this, TT, *HostTC, C.getInputArgs());
  852. else
  853. assert(DeviceTC && "Device toolchain not defined.");
  854. }
  855. TC = DeviceTC.get();
  856. } else
  857. TC = &getToolChain(C.getInputArgs(), TT);
  858. C.addOffloadDeviceToolChain(TC, Action::OFK_OpenMP);
  859. if (DerivedArchs.find(TT.getTriple()) != DerivedArchs.end())
  860. KnownArchs[TC] = DerivedArchs[TT.getTriple()];
  861. }
  862. }
  863. } else if (C.getInputArgs().hasArg(options::OPT_fopenmp_targets_EQ)) {
  864. Diag(clang::diag::err_drv_expecting_fopenmp_with_fopenmp_targets);
  865. return;
  866. }
  867. //
  868. // TODO: Add support for other offloading programming models here.
  869. //
  870. }
  871. static void appendOneArg(InputArgList &Args, const Arg *Opt,
  872. const Arg *BaseArg) {
  873. // The args for config files or /clang: flags belong to different InputArgList
  874. // objects than Args. This copies an Arg from one of those other InputArgLists
  875. // to the ownership of Args.
  876. unsigned Index = Args.MakeIndex(Opt->getSpelling());
  877. Arg *Copy = new llvm::opt::Arg(Opt->getOption(), Args.getArgString(Index),
  878. Index, BaseArg);
  879. Copy->getValues() = Opt->getValues();
  880. if (Opt->isClaimed())
  881. Copy->claim();
  882. Copy->setOwnsValues(Opt->getOwnsValues());
  883. Opt->setOwnsValues(false);
  884. Args.append(Copy);
  885. }
  886. bool Driver::readConfigFile(StringRef FileName,
  887. llvm::cl::ExpansionContext &ExpCtx) {
  888. // Try opening the given file.
  889. auto Status = getVFS().status(FileName);
  890. if (!Status) {
  891. Diag(diag::err_drv_cannot_open_config_file)
  892. << FileName << Status.getError().message();
  893. return true;
  894. }
  895. if (Status->getType() != llvm::sys::fs::file_type::regular_file) {
  896. Diag(diag::err_drv_cannot_open_config_file)
  897. << FileName << "not a regular file";
  898. return true;
  899. }
  900. // Try reading the given file.
  901. SmallVector<const char *, 32> NewCfgArgs;
  902. if (llvm::Error Err = ExpCtx.readConfigFile(FileName, NewCfgArgs)) {
  903. Diag(diag::err_drv_cannot_read_config_file)
  904. << FileName << toString(std::move(Err));
  905. return true;
  906. }
  907. // Read options from config file.
  908. llvm::SmallString<128> CfgFileName(FileName);
  909. llvm::sys::path::native(CfgFileName);
  910. bool ContainErrors;
  911. std::unique_ptr<InputArgList> NewOptions = std::make_unique<InputArgList>(
  912. ParseArgStrings(NewCfgArgs, IsCLMode(), ContainErrors));
  913. if (ContainErrors)
  914. return true;
  915. // Claim all arguments that come from a configuration file so that the driver
  916. // does not warn on any that is unused.
  917. for (Arg *A : *NewOptions)
  918. A->claim();
  919. if (!CfgOptions)
  920. CfgOptions = std::move(NewOptions);
  921. else {
  922. // If this is a subsequent config file, append options to the previous one.
  923. for (auto *Opt : *NewOptions) {
  924. const Arg *BaseArg = &Opt->getBaseArg();
  925. if (BaseArg == Opt)
  926. BaseArg = nullptr;
  927. appendOneArg(*CfgOptions, Opt, BaseArg);
  928. }
  929. }
  930. ConfigFiles.push_back(std::string(CfgFileName));
  931. return false;
  932. }
  933. bool Driver::loadConfigFiles() {
  934. llvm::cl::ExpansionContext ExpCtx(Saver.getAllocator(),
  935. llvm::cl::tokenizeConfigFile);
  936. ExpCtx.setVFS(&getVFS());
  937. // Process options that change search path for config files.
  938. if (CLOptions) {
  939. if (CLOptions->hasArg(options::OPT_config_system_dir_EQ)) {
  940. SmallString<128> CfgDir;
  941. CfgDir.append(
  942. CLOptions->getLastArgValue(options::OPT_config_system_dir_EQ));
  943. if (CfgDir.empty() || getVFS().makeAbsolute(CfgDir))
  944. SystemConfigDir.clear();
  945. else
  946. SystemConfigDir = static_cast<std::string>(CfgDir);
  947. }
  948. if (CLOptions->hasArg(options::OPT_config_user_dir_EQ)) {
  949. SmallString<128> CfgDir;
  950. llvm::sys::fs::expand_tilde(
  951. CLOptions->getLastArgValue(options::OPT_config_user_dir_EQ), CfgDir);
  952. if (CfgDir.empty() || getVFS().makeAbsolute(CfgDir))
  953. UserConfigDir.clear();
  954. else
  955. UserConfigDir = static_cast<std::string>(CfgDir);
  956. }
  957. }
  958. // Prepare list of directories where config file is searched for.
  959. StringRef CfgFileSearchDirs[] = {UserConfigDir, SystemConfigDir, Dir};
  960. ExpCtx.setSearchDirs(CfgFileSearchDirs);
  961. // First try to load configuration from the default files, return on error.
  962. if (loadDefaultConfigFiles(ExpCtx))
  963. return true;
  964. // Then load configuration files specified explicitly.
  965. SmallString<128> CfgFilePath;
  966. if (CLOptions) {
  967. for (auto CfgFileName : CLOptions->getAllArgValues(options::OPT_config)) {
  968. // If argument contains directory separator, treat it as a path to
  969. // configuration file.
  970. if (llvm::sys::path::has_parent_path(CfgFileName)) {
  971. CfgFilePath.assign(CfgFileName);
  972. if (llvm::sys::path::is_relative(CfgFilePath)) {
  973. if (getVFS().makeAbsolute(CfgFilePath)) {
  974. Diag(diag::err_drv_cannot_open_config_file)
  975. << CfgFilePath << "cannot get absolute path";
  976. return true;
  977. }
  978. }
  979. } else if (!ExpCtx.findConfigFile(CfgFileName, CfgFilePath)) {
  980. // Report an error that the config file could not be found.
  981. Diag(diag::err_drv_config_file_not_found) << CfgFileName;
  982. for (const StringRef &SearchDir : CfgFileSearchDirs)
  983. if (!SearchDir.empty())
  984. Diag(diag::note_drv_config_file_searched_in) << SearchDir;
  985. return true;
  986. }
  987. // Try to read the config file, return on error.
  988. if (readConfigFile(CfgFilePath, ExpCtx))
  989. return true;
  990. }
  991. }
  992. // No error occurred.
  993. return false;
  994. }
  995. bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) {
  996. // Disable default config if CLANG_NO_DEFAULT_CONFIG is set to a non-empty
  997. // value.
  998. if (const char *NoConfigEnv = ::getenv("CLANG_NO_DEFAULT_CONFIG")) {
  999. if (*NoConfigEnv)
  1000. return false;
  1001. }
  1002. if (CLOptions && CLOptions->hasArg(options::OPT_no_default_config))
  1003. return false;
  1004. std::string RealMode = getExecutableForDriverMode(Mode);
  1005. std::string Triple;
  1006. // If name prefix is present, no --target= override was passed via CLOptions
  1007. // and the name prefix is not a valid triple, force it for backwards
  1008. // compatibility.
  1009. if (!ClangNameParts.TargetPrefix.empty() &&
  1010. computeTargetTriple(*this, "/invalid/", *CLOptions).str() ==
  1011. "/invalid/") {
  1012. llvm::Triple PrefixTriple{ClangNameParts.TargetPrefix};
  1013. if (PrefixTriple.getArch() == llvm::Triple::UnknownArch ||
  1014. PrefixTriple.isOSUnknown())
  1015. Triple = PrefixTriple.str();
  1016. }
  1017. // Otherwise, use the real triple as used by the driver.
  1018. if (Triple.empty()) {
  1019. llvm::Triple RealTriple =
  1020. computeTargetTriple(*this, TargetTriple, *CLOptions);
  1021. Triple = RealTriple.str();
  1022. assert(!Triple.empty());
  1023. }
  1024. // Search for config files in the following order:
  1025. // 1. <triple>-<mode>.cfg using real driver mode
  1026. // (e.g. i386-pc-linux-gnu-clang++.cfg).
  1027. // 2. <triple>-<mode>.cfg using executable suffix
  1028. // (e.g. i386-pc-linux-gnu-clang-g++.cfg for *clang-g++).
  1029. // 3. <triple>.cfg + <mode>.cfg using real driver mode
  1030. // (e.g. i386-pc-linux-gnu.cfg + clang++.cfg).
  1031. // 4. <triple>.cfg + <mode>.cfg using executable suffix
  1032. // (e.g. i386-pc-linux-gnu.cfg + clang-g++.cfg for *clang-g++).
  1033. // Try loading <triple>-<mode>.cfg, and return if we find a match.
  1034. SmallString<128> CfgFilePath;
  1035. std::string CfgFileName = Triple + '-' + RealMode + ".cfg";
  1036. if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath))
  1037. return readConfigFile(CfgFilePath, ExpCtx);
  1038. bool TryModeSuffix = !ClangNameParts.ModeSuffix.empty() &&
  1039. ClangNameParts.ModeSuffix != RealMode;
  1040. if (TryModeSuffix) {
  1041. CfgFileName = Triple + '-' + ClangNameParts.ModeSuffix + ".cfg";
  1042. if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath))
  1043. return readConfigFile(CfgFilePath, ExpCtx);
  1044. }
  1045. // Try loading <mode>.cfg, and return if loading failed. If a matching file
  1046. // was not found, still proceed on to try <triple>.cfg.
  1047. CfgFileName = RealMode + ".cfg";
  1048. if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath)) {
  1049. if (readConfigFile(CfgFilePath, ExpCtx))
  1050. return true;
  1051. } else if (TryModeSuffix) {
  1052. CfgFileName = ClangNameParts.ModeSuffix + ".cfg";
  1053. if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath) &&
  1054. readConfigFile(CfgFilePath, ExpCtx))
  1055. return true;
  1056. }
  1057. // Try loading <triple>.cfg and return if we find a match.
  1058. CfgFileName = Triple + ".cfg";
  1059. if (ExpCtx.findConfigFile(CfgFileName, CfgFilePath))
  1060. return readConfigFile(CfgFilePath, ExpCtx);
  1061. // If we were unable to find a config file deduced from executable name,
  1062. // that is not an error.
  1063. return false;
  1064. }
  1065. Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
  1066. llvm::PrettyStackTraceString CrashInfo("Compilation construction");
  1067. // FIXME: Handle environment options which affect driver behavior, somewhere
  1068. // (client?). GCC_EXEC_PREFIX, LPATH, CC_PRINT_OPTIONS.
  1069. // We look for the driver mode option early, because the mode can affect
  1070. // how other options are parsed.
  1071. auto DriverMode = getDriverMode(ClangExecutable, ArgList.slice(1));
  1072. if (!DriverMode.empty())
  1073. setDriverMode(DriverMode);
  1074. // FIXME: What are we going to do with -V and -b?
  1075. // Arguments specified in command line.
  1076. bool ContainsError;
  1077. CLOptions = std::make_unique<InputArgList>(
  1078. ParseArgStrings(ArgList.slice(1), IsCLMode(), ContainsError));
  1079. // Try parsing configuration file.
  1080. if (!ContainsError)
  1081. ContainsError = loadConfigFiles();
  1082. bool HasConfigFile = !ContainsError && (CfgOptions.get() != nullptr);
  1083. // All arguments, from both config file and command line.
  1084. InputArgList Args = std::move(HasConfigFile ? std::move(*CfgOptions)
  1085. : std::move(*CLOptions));
  1086. if (HasConfigFile)
  1087. for (auto *Opt : *CLOptions) {
  1088. if (Opt->getOption().matches(options::OPT_config))
  1089. continue;
  1090. const Arg *BaseArg = &Opt->getBaseArg();
  1091. if (BaseArg == Opt)
  1092. BaseArg = nullptr;
  1093. appendOneArg(Args, Opt, BaseArg);
  1094. }
  1095. // In CL mode, look for any pass-through arguments
  1096. if (IsCLMode() && !ContainsError) {
  1097. SmallVector<const char *, 16> CLModePassThroughArgList;
  1098. for (const auto *A : Args.filtered(options::OPT__SLASH_clang)) {
  1099. A->claim();
  1100. CLModePassThroughArgList.push_back(A->getValue());
  1101. }
  1102. if (!CLModePassThroughArgList.empty()) {
  1103. // Parse any pass through args using default clang processing rather
  1104. // than clang-cl processing.
  1105. auto CLModePassThroughOptions = std::make_unique<InputArgList>(
  1106. ParseArgStrings(CLModePassThroughArgList, false, ContainsError));
  1107. if (!ContainsError)
  1108. for (auto *Opt : *CLModePassThroughOptions) {
  1109. appendOneArg(Args, Opt, nullptr);
  1110. }
  1111. }
  1112. }
  1113. // Check for working directory option before accessing any files
  1114. if (Arg *WD = Args.getLastArg(options::OPT_working_directory))
  1115. if (VFS->setCurrentWorkingDirectory(WD->getValue()))
  1116. Diag(diag::err_drv_unable_to_set_working_directory) << WD->getValue();
  1117. // FIXME: This stuff needs to go into the Compilation, not the driver.
  1118. bool CCCPrintPhases;
  1119. // -canonical-prefixes, -no-canonical-prefixes are used very early in main.
  1120. Args.ClaimAllArgs(options::OPT_canonical_prefixes);
  1121. Args.ClaimAllArgs(options::OPT_no_canonical_prefixes);
  1122. // f(no-)integated-cc1 is also used very early in main.
  1123. Args.ClaimAllArgs(options::OPT_fintegrated_cc1);
  1124. Args.ClaimAllArgs(options::OPT_fno_integrated_cc1);
  1125. // Ignore -pipe.
  1126. Args.ClaimAllArgs(options::OPT_pipe);
  1127. // Extract -ccc args.
  1128. //
  1129. // FIXME: We need to figure out where this behavior should live. Most of it
  1130. // should be outside in the client; the parts that aren't should have proper
  1131. // options, either by introducing new ones or by overloading gcc ones like -V
  1132. // or -b.
  1133. CCCPrintPhases = Args.hasArg(options::OPT_ccc_print_phases);
  1134. CCCPrintBindings = Args.hasArg(options::OPT_ccc_print_bindings);
  1135. if (const Arg *A = Args.getLastArg(options::OPT_ccc_gcc_name))
  1136. CCCGenericGCCName = A->getValue();
  1137. // Process -fproc-stat-report options.
  1138. if (const Arg *A = Args.getLastArg(options::OPT_fproc_stat_report_EQ)) {
  1139. CCPrintProcessStats = true;
  1140. CCPrintStatReportFilename = A->getValue();
  1141. }
  1142. if (Args.hasArg(options::OPT_fproc_stat_report))
  1143. CCPrintProcessStats = true;
  1144. // FIXME: TargetTriple is used by the target-prefixed calls to as/ld
  1145. // and getToolChain is const.
  1146. if (IsCLMode()) {
  1147. // clang-cl targets MSVC-style Win32.
  1148. llvm::Triple T(TargetTriple);
  1149. T.setOS(llvm::Triple::Win32);
  1150. T.setVendor(llvm::Triple::PC);
  1151. T.setEnvironment(llvm::Triple::MSVC);
  1152. T.setObjectFormat(llvm::Triple::COFF);
  1153. if (Args.hasArg(options::OPT__SLASH_arm64EC))
  1154. T.setArch(llvm::Triple::aarch64, llvm::Triple::AArch64SubArch_arm64ec);
  1155. TargetTriple = T.str();
  1156. } else if (IsDXCMode()) {
  1157. // Build TargetTriple from target_profile option for clang-dxc.
  1158. if (const Arg *A = Args.getLastArg(options::OPT_target_profile)) {
  1159. StringRef TargetProfile = A->getValue();
  1160. if (auto Triple =
  1161. toolchains::HLSLToolChain::parseTargetProfile(TargetProfile))
  1162. TargetTriple = *Triple;
  1163. else
  1164. Diag(diag::err_drv_invalid_directx_shader_module) << TargetProfile;
  1165. A->claim();
  1166. } else {
  1167. Diag(diag::err_drv_dxc_missing_target_profile);
  1168. }
  1169. }
  1170. if (const Arg *A = Args.getLastArg(options::OPT_target))
  1171. TargetTriple = A->getValue();
  1172. if (const Arg *A = Args.getLastArg(options::OPT_ccc_install_dir))
  1173. Dir = InstalledDir = A->getValue();
  1174. for (const Arg *A : Args.filtered(options::OPT_B)) {
  1175. A->claim();
  1176. PrefixDirs.push_back(A->getValue(0));
  1177. }
  1178. if (std::optional<std::string> CompilerPathValue =
  1179. llvm::sys::Process::GetEnv("COMPILER_PATH")) {
  1180. StringRef CompilerPath = *CompilerPathValue;
  1181. while (!CompilerPath.empty()) {
  1182. std::pair<StringRef, StringRef> Split =
  1183. CompilerPath.split(llvm::sys::EnvPathSeparator);
  1184. PrefixDirs.push_back(std::string(Split.first));
  1185. CompilerPath = Split.second;
  1186. }
  1187. }
  1188. if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ))
  1189. SysRoot = A->getValue();
  1190. if (const Arg *A = Args.getLastArg(options::OPT__dyld_prefix_EQ))
  1191. DyldPrefix = A->getValue();
  1192. if (const Arg *A = Args.getLastArg(options::OPT_resource_dir))
  1193. ResourceDir = A->getValue();
  1194. if (const Arg *A = Args.getLastArg(options::OPT_save_temps_EQ)) {
  1195. SaveTemps = llvm::StringSwitch<SaveTempsMode>(A->getValue())
  1196. .Case("cwd", SaveTempsCwd)
  1197. .Case("obj", SaveTempsObj)
  1198. .Default(SaveTempsCwd);
  1199. }
  1200. if (const Arg *A = Args.getLastArg(options::OPT_offload_host_only,
  1201. options::OPT_offload_device_only,
  1202. options::OPT_offload_host_device)) {
  1203. if (A->getOption().matches(options::OPT_offload_host_only))
  1204. Offload = OffloadHost;
  1205. else if (A->getOption().matches(options::OPT_offload_device_only))
  1206. Offload = OffloadDevice;
  1207. else
  1208. Offload = OffloadHostDevice;
  1209. }
  1210. setLTOMode(Args);
  1211. // Process -fembed-bitcode= flags.
  1212. if (Arg *A = Args.getLastArg(options::OPT_fembed_bitcode_EQ)) {
  1213. StringRef Name = A->getValue();
  1214. unsigned Model = llvm::StringSwitch<unsigned>(Name)
  1215. .Case("off", EmbedNone)
  1216. .Case("all", EmbedBitcode)
  1217. .Case("bitcode", EmbedBitcode)
  1218. .Case("marker", EmbedMarker)
  1219. .Default(~0U);
  1220. if (Model == ~0U) {
  1221. Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args)
  1222. << Name;
  1223. } else
  1224. BitcodeEmbed = static_cast<BitcodeEmbedMode>(Model);
  1225. }
  1226. // Remove existing compilation database so that each job can append to it.
  1227. if (Arg *A = Args.getLastArg(options::OPT_MJ))
  1228. llvm::sys::fs::remove(A->getValue());
  1229. // Setting up the jobs for some precompile cases depends on whether we are
  1230. // treating them as PCH, implicit modules or C++20 ones.
  1231. // TODO: inferring the mode like this seems fragile (it meets the objective
  1232. // of not requiring anything new for operation, however).
  1233. const Arg *Std = Args.getLastArg(options::OPT_std_EQ);
  1234. ModulesModeCXX20 =
  1235. !Args.hasArg(options::OPT_fmodules) && Std &&
  1236. (Std->containsValue("c++20") || Std->containsValue("c++2b") ||
  1237. Std->containsValue("c++2a") || Std->containsValue("c++latest"));
  1238. // Process -fmodule-header{=} flags.
  1239. if (Arg *A = Args.getLastArg(options::OPT_fmodule_header_EQ,
  1240. options::OPT_fmodule_header)) {
  1241. // These flags force C++20 handling of headers.
  1242. ModulesModeCXX20 = true;
  1243. if (A->getOption().matches(options::OPT_fmodule_header))
  1244. CXX20HeaderType = HeaderMode_Default;
  1245. else {
  1246. StringRef ArgName = A->getValue();
  1247. unsigned Kind = llvm::StringSwitch<unsigned>(ArgName)
  1248. .Case("user", HeaderMode_User)
  1249. .Case("system", HeaderMode_System)
  1250. .Default(~0U);
  1251. if (Kind == ~0U) {
  1252. Diags.Report(diag::err_drv_invalid_value)
  1253. << A->getAsString(Args) << ArgName;
  1254. } else
  1255. CXX20HeaderType = static_cast<ModuleHeaderMode>(Kind);
  1256. }
  1257. }
  1258. std::unique_ptr<llvm::opt::InputArgList> UArgs =
  1259. std::make_unique<InputArgList>(std::move(Args));
  1260. // Perform the default argument translations.
  1261. DerivedArgList *TranslatedArgs = TranslateInputArgs(*UArgs);
  1262. // Owned by the host.
  1263. const ToolChain &TC = getToolChain(
  1264. *UArgs, computeTargetTriple(*this, TargetTriple, *UArgs));
  1265. // Report warning when arm64EC option is overridden by specified target
  1266. if ((TC.getTriple().getArch() != llvm::Triple::aarch64 ||
  1267. TC.getTriple().getSubArch() != llvm::Triple::AArch64SubArch_arm64ec) &&
  1268. UArgs->hasArg(options::OPT__SLASH_arm64EC)) {
  1269. getDiags().Report(clang::diag::warn_target_override_arm64ec)
  1270. << TC.getTriple().str();
  1271. }
  1272. // The compilation takes ownership of Args.
  1273. Compilation *C = new Compilation(*this, TC, UArgs.release(), TranslatedArgs,
  1274. ContainsError);
  1275. if (!HandleImmediateArgs(*C))
  1276. return C;
  1277. // Construct the list of inputs.
  1278. InputList Inputs;
  1279. BuildInputs(C->getDefaultToolChain(), *TranslatedArgs, Inputs);
  1280. // Populate the tool chains for the offloading devices, if any.
  1281. CreateOffloadingDeviceToolChains(*C, Inputs);
  1282. // Construct the list of abstract actions to perform for this compilation. On
  1283. // MachO targets this uses the driver-driver and universal actions.
  1284. if (TC.getTriple().isOSBinFormatMachO())
  1285. BuildUniversalActions(*C, C->getDefaultToolChain(), Inputs);
  1286. else
  1287. BuildActions(*C, C->getArgs(), Inputs, C->getActions());
  1288. if (CCCPrintPhases) {
  1289. PrintActions(*C);
  1290. return C;
  1291. }
  1292. BuildJobs(*C);
  1293. return C;
  1294. }
  1295. static void printArgList(raw_ostream &OS, const llvm::opt::ArgList &Args) {
  1296. llvm::opt::ArgStringList ASL;
  1297. for (const auto *A : Args) {
  1298. // Use user's original spelling of flags. For example, use
  1299. // `/source-charset:utf-8` instead of `-finput-charset=utf-8` if the user
  1300. // wrote the former.
  1301. while (A->getAlias())
  1302. A = A->getAlias();
  1303. A->render(Args, ASL);
  1304. }
  1305. for (auto I = ASL.begin(), E = ASL.end(); I != E; ++I) {
  1306. if (I != ASL.begin())
  1307. OS << ' ';
  1308. llvm::sys::printArg(OS, *I, true);
  1309. }
  1310. OS << '\n';
  1311. }
  1312. bool Driver::getCrashDiagnosticFile(StringRef ReproCrashFilename,
  1313. SmallString<128> &CrashDiagDir) {
  1314. using namespace llvm::sys;
  1315. assert(llvm::Triple(llvm::sys::getProcessTriple()).isOSDarwin() &&
  1316. "Only knows about .crash files on Darwin");
  1317. // The .crash file can be found on at ~/Library/Logs/DiagnosticReports/
  1318. // (or /Library/Logs/DiagnosticReports for root) and has the filename pattern
  1319. // clang-<VERSION>_<YYYY-MM-DD-HHMMSS>_<hostname>.crash.
  1320. path::home_directory(CrashDiagDir);
  1321. if (CrashDiagDir.startswith("/var/root"))
  1322. CrashDiagDir = "/";
  1323. path::append(CrashDiagDir, "Library/Logs/DiagnosticReports");
  1324. int PID =
  1325. #if LLVM_ON_UNIX
  1326. getpid();
  1327. #else
  1328. 0;
  1329. #endif
  1330. std::error_code EC;
  1331. fs::file_status FileStatus;
  1332. TimePoint<> LastAccessTime;
  1333. SmallString<128> CrashFilePath;
  1334. // Lookup the .crash files and get the one generated by a subprocess spawned
  1335. // by this driver invocation.
  1336. for (fs::directory_iterator File(CrashDiagDir, EC), FileEnd;
  1337. File != FileEnd && !EC; File.increment(EC)) {
  1338. StringRef FileName = path::filename(File->path());
  1339. if (!FileName.startswith(Name))
  1340. continue;
  1341. if (fs::status(File->path(), FileStatus))
  1342. continue;
  1343. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> CrashFile =
  1344. llvm::MemoryBuffer::getFile(File->path());
  1345. if (!CrashFile)
  1346. continue;
  1347. // The first line should start with "Process:", otherwise this isn't a real
  1348. // .crash file.
  1349. StringRef Data = CrashFile.get()->getBuffer();
  1350. if (!Data.startswith("Process:"))
  1351. continue;
  1352. // Parse parent process pid line, e.g: "Parent Process: clang-4.0 [79141]"
  1353. size_t ParentProcPos = Data.find("Parent Process:");
  1354. if (ParentProcPos == StringRef::npos)
  1355. continue;
  1356. size_t LineEnd = Data.find_first_of("\n", ParentProcPos);
  1357. if (LineEnd == StringRef::npos)
  1358. continue;
  1359. StringRef ParentProcess = Data.slice(ParentProcPos+15, LineEnd).trim();
  1360. int OpenBracket = -1, CloseBracket = -1;
  1361. for (size_t i = 0, e = ParentProcess.size(); i < e; ++i) {
  1362. if (ParentProcess[i] == '[')
  1363. OpenBracket = i;
  1364. if (ParentProcess[i] == ']')
  1365. CloseBracket = i;
  1366. }
  1367. // Extract the parent process PID from the .crash file and check whether
  1368. // it matches this driver invocation pid.
  1369. int CrashPID;
  1370. if (OpenBracket < 0 || CloseBracket < 0 ||
  1371. ParentProcess.slice(OpenBracket + 1, CloseBracket)
  1372. .getAsInteger(10, CrashPID) || CrashPID != PID) {
  1373. continue;
  1374. }
  1375. // Found a .crash file matching the driver pid. To avoid getting an older
  1376. // and misleading crash file, continue looking for the most recent.
  1377. // FIXME: the driver can dispatch multiple cc1 invocations, leading to
  1378. // multiple crashes poiting to the same parent process. Since the driver
  1379. // does not collect pid information for the dispatched invocation there's
  1380. // currently no way to distinguish among them.
  1381. const auto FileAccessTime = FileStatus.getLastModificationTime();
  1382. if (FileAccessTime > LastAccessTime) {
  1383. CrashFilePath.assign(File->path());
  1384. LastAccessTime = FileAccessTime;
  1385. }
  1386. }
  1387. // If found, copy it over to the location of other reproducer files.
  1388. if (!CrashFilePath.empty()) {
  1389. EC = fs::copy_file(CrashFilePath, ReproCrashFilename);
  1390. if (EC)
  1391. return false;
  1392. return true;
  1393. }
  1394. return false;
  1395. }
  1396. static const char BugReporMsg[] =
  1397. "\n********************\n\n"
  1398. "PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:\n"
  1399. "Preprocessed source(s) and associated run script(s) are located at:";
  1400. // When clang crashes, produce diagnostic information including the fully
  1401. // preprocessed source file(s). Request that the developer attach the
  1402. // diagnostic information to a bug report.
  1403. void Driver::generateCompilationDiagnostics(
  1404. Compilation &C, const Command &FailingCommand,
  1405. StringRef AdditionalInformation, CompilationDiagnosticReport *Report) {
  1406. if (C.getArgs().hasArg(options::OPT_fno_crash_diagnostics))
  1407. return;
  1408. unsigned Level = 1;
  1409. if (Arg *A = C.getArgs().getLastArg(options::OPT_fcrash_diagnostics_EQ)) {
  1410. Level = llvm::StringSwitch<unsigned>(A->getValue())
  1411. .Case("off", 0)
  1412. .Case("compiler", 1)
  1413. .Case("all", 2)
  1414. .Default(1);
  1415. }
  1416. if (!Level)
  1417. return;
  1418. // Don't try to generate diagnostics for dsymutil jobs.
  1419. if (FailingCommand.getCreator().isDsymutilJob())
  1420. return;
  1421. bool IsLLD = false;
  1422. ArgStringList SavedTemps;
  1423. if (FailingCommand.getCreator().isLinkJob()) {
  1424. C.getDefaultToolChain().GetLinkerPath(&IsLLD);
  1425. if (!IsLLD || Level < 2)
  1426. return;
  1427. // If lld crashed, we will re-run the same command with the input it used
  1428. // to have. In that case we should not remove temp files in
  1429. // initCompilationForDiagnostics yet. They will be added back and removed
  1430. // later.
  1431. SavedTemps = std::move(C.getTempFiles());
  1432. assert(!C.getTempFiles().size());
  1433. }
  1434. // Print the version of the compiler.
  1435. PrintVersion(C, llvm::errs());
  1436. // Suppress driver output and emit preprocessor output to temp file.
  1437. CCGenDiagnostics = true;
  1438. // Save the original job command(s).
  1439. Command Cmd = FailingCommand;
  1440. // Keep track of whether we produce any errors while trying to produce
  1441. // preprocessed sources.
  1442. DiagnosticErrorTrap Trap(Diags);
  1443. // Suppress tool output.
  1444. C.initCompilationForDiagnostics();
  1445. // If lld failed, rerun it again with --reproduce.
  1446. if (IsLLD) {
  1447. const char *TmpName = CreateTempFile(C, "linker-crash", "tar");
  1448. Command NewLLDInvocation = Cmd;
  1449. llvm::opt::ArgStringList ArgList = NewLLDInvocation.getArguments();
  1450. StringRef ReproduceOption =
  1451. C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment()
  1452. ? "/reproduce:"
  1453. : "--reproduce=";
  1454. ArgList.push_back(Saver.save(Twine(ReproduceOption) + TmpName).data());
  1455. NewLLDInvocation.replaceArguments(std::move(ArgList));
  1456. // Redirect stdout/stderr to /dev/null.
  1457. NewLLDInvocation.Execute({std::nullopt, {""}, {""}}, nullptr, nullptr);
  1458. Diag(clang::diag::note_drv_command_failed_diag_msg) << BugReporMsg;
  1459. Diag(clang::diag::note_drv_command_failed_diag_msg) << TmpName;
  1460. Diag(clang::diag::note_drv_command_failed_diag_msg)
  1461. << "\n\n********************";
  1462. if (Report)
  1463. Report->TemporaryFiles.push_back(TmpName);
  1464. return;
  1465. }
  1466. // Construct the list of inputs.
  1467. InputList Inputs;
  1468. BuildInputs(C.getDefaultToolChain(), C.getArgs(), Inputs);
  1469. for (InputList::iterator it = Inputs.begin(), ie = Inputs.end(); it != ie;) {
  1470. bool IgnoreInput = false;
  1471. // Ignore input from stdin or any inputs that cannot be preprocessed.
  1472. // Check type first as not all linker inputs have a value.
  1473. if (types::getPreprocessedType(it->first) == types::TY_INVALID) {
  1474. IgnoreInput = true;
  1475. } else if (!strcmp(it->second->getValue(), "-")) {
  1476. Diag(clang::diag::note_drv_command_failed_diag_msg)
  1477. << "Error generating preprocessed source(s) - "
  1478. "ignoring input from stdin.";
  1479. IgnoreInput = true;
  1480. }
  1481. if (IgnoreInput) {
  1482. it = Inputs.erase(it);
  1483. ie = Inputs.end();
  1484. } else {
  1485. ++it;
  1486. }
  1487. }
  1488. if (Inputs.empty()) {
  1489. Diag(clang::diag::note_drv_command_failed_diag_msg)
  1490. << "Error generating preprocessed source(s) - "
  1491. "no preprocessable inputs.";
  1492. return;
  1493. }
  1494. // Don't attempt to generate preprocessed files if multiple -arch options are
  1495. // used, unless they're all duplicates.
  1496. llvm::StringSet<> ArchNames;
  1497. for (const Arg *A : C.getArgs()) {
  1498. if (A->getOption().matches(options::OPT_arch)) {
  1499. StringRef ArchName = A->getValue();
  1500. ArchNames.insert(ArchName);
  1501. }
  1502. }
  1503. if (ArchNames.size() > 1) {
  1504. Diag(clang::diag::note_drv_command_failed_diag_msg)
  1505. << "Error generating preprocessed source(s) - cannot generate "
  1506. "preprocessed source with multiple -arch options.";
  1507. return;
  1508. }
  1509. // Construct the list of abstract actions to perform for this compilation. On
  1510. // Darwin OSes this uses the driver-driver and builds universal actions.
  1511. const ToolChain &TC = C.getDefaultToolChain();
  1512. if (TC.getTriple().isOSBinFormatMachO())
  1513. BuildUniversalActions(C, TC, Inputs);
  1514. else
  1515. BuildActions(C, C.getArgs(), Inputs, C.getActions());
  1516. BuildJobs(C);
  1517. // If there were errors building the compilation, quit now.
  1518. if (Trap.hasErrorOccurred()) {
  1519. Diag(clang::diag::note_drv_command_failed_diag_msg)
  1520. << "Error generating preprocessed source(s).";
  1521. return;
  1522. }
  1523. // Generate preprocessed output.
  1524. SmallVector<std::pair<int, const Command *>, 4> FailingCommands;
  1525. C.ExecuteJobs(C.getJobs(), FailingCommands);
  1526. // If any of the preprocessing commands failed, clean up and exit.
  1527. if (!FailingCommands.empty()) {
  1528. Diag(clang::diag::note_drv_command_failed_diag_msg)
  1529. << "Error generating preprocessed source(s).";
  1530. return;
  1531. }
  1532. const ArgStringList &TempFiles = C.getTempFiles();
  1533. if (TempFiles.empty()) {
  1534. Diag(clang::diag::note_drv_command_failed_diag_msg)
  1535. << "Error generating preprocessed source(s).";
  1536. return;
  1537. }
  1538. Diag(clang::diag::note_drv_command_failed_diag_msg) << BugReporMsg;
  1539. SmallString<128> VFS;
  1540. SmallString<128> ReproCrashFilename;
  1541. for (const char *TempFile : TempFiles) {
  1542. Diag(clang::diag::note_drv_command_failed_diag_msg) << TempFile;
  1543. if (Report)
  1544. Report->TemporaryFiles.push_back(TempFile);
  1545. if (ReproCrashFilename.empty()) {
  1546. ReproCrashFilename = TempFile;
  1547. llvm::sys::path::replace_extension(ReproCrashFilename, ".crash");
  1548. }
  1549. if (StringRef(TempFile).endswith(".cache")) {
  1550. // In some cases (modules) we'll dump extra data to help with reproducing
  1551. // the crash into a directory next to the output.
  1552. VFS = llvm::sys::path::filename(TempFile);
  1553. llvm::sys::path::append(VFS, "vfs", "vfs.yaml");
  1554. }
  1555. }
  1556. for (const char *TempFile : SavedTemps)
  1557. C.addTempFile(TempFile);
  1558. // Assume associated files are based off of the first temporary file.
  1559. CrashReportInfo CrashInfo(TempFiles[0], VFS);
  1560. llvm::SmallString<128> Script(CrashInfo.Filename);
  1561. llvm::sys::path::replace_extension(Script, "sh");
  1562. std::error_code EC;
  1563. llvm::raw_fd_ostream ScriptOS(Script, EC, llvm::sys::fs::CD_CreateNew,
  1564. llvm::sys::fs::FA_Write,
  1565. llvm::sys::fs::OF_Text);
  1566. if (EC) {
  1567. Diag(clang::diag::note_drv_command_failed_diag_msg)
  1568. << "Error generating run script: " << Script << " " << EC.message();
  1569. } else {
  1570. ScriptOS << "# Crash reproducer for " << getClangFullVersion() << "\n"
  1571. << "# Driver args: ";
  1572. printArgList(ScriptOS, C.getInputArgs());
  1573. ScriptOS << "# Original command: ";
  1574. Cmd.Print(ScriptOS, "\n", /*Quote=*/true);
  1575. Cmd.Print(ScriptOS, "\n", /*Quote=*/true, &CrashInfo);
  1576. if (!AdditionalInformation.empty())
  1577. ScriptOS << "\n# Additional information: " << AdditionalInformation
  1578. << "\n";
  1579. if (Report)
  1580. Report->TemporaryFiles.push_back(std::string(Script.str()));
  1581. Diag(clang::diag::note_drv_command_failed_diag_msg) << Script;
  1582. }
  1583. // On darwin, provide information about the .crash diagnostic report.
  1584. if (llvm::Triple(llvm::sys::getProcessTriple()).isOSDarwin()) {
  1585. SmallString<128> CrashDiagDir;
  1586. if (getCrashDiagnosticFile(ReproCrashFilename, CrashDiagDir)) {
  1587. Diag(clang::diag::note_drv_command_failed_diag_msg)
  1588. << ReproCrashFilename.str();
  1589. } else { // Suggest a directory for the user to look for .crash files.
  1590. llvm::sys::path::append(CrashDiagDir, Name);
  1591. CrashDiagDir += "_<YYYY-MM-DD-HHMMSS>_<hostname>.crash";
  1592. Diag(clang::diag::note_drv_command_failed_diag_msg)
  1593. << "Crash backtrace is located in";
  1594. Diag(clang::diag::note_drv_command_failed_diag_msg)
  1595. << CrashDiagDir.str();
  1596. Diag(clang::diag::note_drv_command_failed_diag_msg)
  1597. << "(choose the .crash file that corresponds to your crash)";
  1598. }
  1599. }
  1600. for (const auto &A : C.getArgs().filtered(options::OPT_frewrite_map_file_EQ))
  1601. Diag(clang::diag::note_drv_command_failed_diag_msg) << A->getValue();
  1602. Diag(clang::diag::note_drv_command_failed_diag_msg)
  1603. << "\n\n********************";
  1604. }
  1605. void Driver::setUpResponseFiles(Compilation &C, Command &Cmd) {
  1606. // Since commandLineFitsWithinSystemLimits() may underestimate system's
  1607. // capacity if the tool does not support response files, there is a chance/
  1608. // that things will just work without a response file, so we silently just
  1609. // skip it.
  1610. if (Cmd.getResponseFileSupport().ResponseKind ==
  1611. ResponseFileSupport::RF_None ||
  1612. llvm::sys::commandLineFitsWithinSystemLimits(Cmd.getExecutable(),
  1613. Cmd.getArguments()))
  1614. return;
  1615. std::string TmpName = GetTemporaryPath("response", "txt");
  1616. Cmd.setResponseFile(C.addTempFile(C.getArgs().MakeArgString(TmpName)));
  1617. }
  1618. int Driver::ExecuteCompilation(
  1619. Compilation &C,
  1620. SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) {
  1621. if (C.getArgs().hasArg(options::OPT_fdriver_only)) {
  1622. if (C.getArgs().hasArg(options::OPT_v))
  1623. C.getJobs().Print(llvm::errs(), "\n", true);
  1624. C.ExecuteJobs(C.getJobs(), FailingCommands, /*LogOnly=*/true);
  1625. // If there were errors building the compilation, quit now.
  1626. if (!FailingCommands.empty() || Diags.hasErrorOccurred())
  1627. return 1;
  1628. return 0;
  1629. }
  1630. // Just print if -### was present.
  1631. if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
  1632. C.getJobs().Print(llvm::errs(), "\n", true);
  1633. return 0;
  1634. }
  1635. // If there were errors building the compilation, quit now.
  1636. if (Diags.hasErrorOccurred())
  1637. return 1;
  1638. // Set up response file names for each command, if necessary.
  1639. for (auto &Job : C.getJobs())
  1640. setUpResponseFiles(C, Job);
  1641. C.ExecuteJobs(C.getJobs(), FailingCommands);
  1642. // If the command succeeded, we are done.
  1643. if (FailingCommands.empty())
  1644. return 0;
  1645. // Otherwise, remove result files and print extra information about abnormal
  1646. // failures.
  1647. int Res = 0;
  1648. for (const auto &CmdPair : FailingCommands) {
  1649. int CommandRes = CmdPair.first;
  1650. const Command *FailingCommand = CmdPair.second;
  1651. // Remove result files if we're not saving temps.
  1652. if (!isSaveTempsEnabled()) {
  1653. const JobAction *JA = cast<JobAction>(&FailingCommand->getSource());
  1654. C.CleanupFileMap(C.getResultFiles(), JA, true);
  1655. // Failure result files are valid unless we crashed.
  1656. if (CommandRes < 0)
  1657. C.CleanupFileMap(C.getFailureResultFiles(), JA, true);
  1658. }
  1659. // llvm/lib/Support/*/Signals.inc will exit with a special return code
  1660. // for SIGPIPE. Do not print diagnostics for this case.
  1661. if (CommandRes == EX_IOERR) {
  1662. Res = CommandRes;
  1663. continue;
  1664. }
  1665. // Print extra information about abnormal failures, if possible.
  1666. //
  1667. // This is ad-hoc, but we don't want to be excessively noisy. If the result
  1668. // status was 1, assume the command failed normally. In particular, if it
  1669. // was the compiler then assume it gave a reasonable error code. Failures
  1670. // in other tools are less common, and they generally have worse
  1671. // diagnostics, so always print the diagnostic there.
  1672. const Tool &FailingTool = FailingCommand->getCreator();
  1673. if (!FailingCommand->getCreator().hasGoodDiagnostics() || CommandRes != 1) {
  1674. // FIXME: See FIXME above regarding result code interpretation.
  1675. if (CommandRes < 0)
  1676. Diag(clang::diag::err_drv_command_signalled)
  1677. << FailingTool.getShortName();
  1678. else
  1679. Diag(clang::diag::err_drv_command_failed)
  1680. << FailingTool.getShortName() << CommandRes;
  1681. }
  1682. }
  1683. return Res;
  1684. }
  1685. void Driver::PrintHelp(bool ShowHidden) const {
  1686. unsigned IncludedFlagsBitmask;
  1687. unsigned ExcludedFlagsBitmask;
  1688. std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) =
  1689. getIncludeExcludeOptionFlagMasks(IsCLMode());
  1690. ExcludedFlagsBitmask |= options::NoDriverOption;
  1691. if (!ShowHidden)
  1692. ExcludedFlagsBitmask |= HelpHidden;
  1693. if (IsFlangMode())
  1694. IncludedFlagsBitmask |= options::FlangOption;
  1695. else
  1696. ExcludedFlagsBitmask |= options::FlangOnlyOption;
  1697. std::string Usage = llvm::formatv("{0} [options] file...", Name).str();
  1698. getOpts().printHelp(llvm::outs(), Usage.c_str(), DriverTitle.c_str(),
  1699. IncludedFlagsBitmask, ExcludedFlagsBitmask,
  1700. /*ShowAllAliases=*/false);
  1701. }
  1702. void Driver::PrintVersion(const Compilation &C, raw_ostream &OS) const {
  1703. if (IsFlangMode()) {
  1704. OS << getClangToolFullVersion("flang-new") << '\n';
  1705. } else {
  1706. // FIXME: The following handlers should use a callback mechanism, we don't
  1707. // know what the client would like to do.
  1708. OS << getClangFullVersion() << '\n';
  1709. }
  1710. const ToolChain &TC = C.getDefaultToolChain();
  1711. OS << "Target: " << TC.getTripleString() << '\n';
  1712. // Print the threading model.
  1713. if (Arg *A = C.getArgs().getLastArg(options::OPT_mthread_model)) {
  1714. // Don't print if the ToolChain would have barfed on it already
  1715. if (TC.isThreadModelSupported(A->getValue()))
  1716. OS << "Thread model: " << A->getValue();
  1717. } else
  1718. OS << "Thread model: " << TC.getThreadModel();
  1719. OS << '\n';
  1720. // Print out the install directory.
  1721. OS << "InstalledDir: " << InstalledDir << '\n';
  1722. // If configuration files were used, print their paths.
  1723. for (auto ConfigFile : ConfigFiles)
  1724. OS << "Configuration file: " << ConfigFile << '\n';
  1725. }
  1726. /// PrintDiagnosticCategories - Implement the --print-diagnostic-categories
  1727. /// option.
  1728. static void PrintDiagnosticCategories(raw_ostream &OS) {
  1729. // Skip the empty category.
  1730. for (unsigned i = 1, max = DiagnosticIDs::getNumberOfCategories(); i != max;
  1731. ++i)
  1732. OS << i << ',' << DiagnosticIDs::getCategoryNameFromID(i) << '\n';
  1733. }
  1734. void Driver::HandleAutocompletions(StringRef PassedFlags) const {
  1735. if (PassedFlags == "")
  1736. return;
  1737. // Print out all options that start with a given argument. This is used for
  1738. // shell autocompletion.
  1739. std::vector<std::string> SuggestedCompletions;
  1740. std::vector<std::string> Flags;
  1741. unsigned int DisableFlags =
  1742. options::NoDriverOption | options::Unsupported | options::Ignored;
  1743. // Make sure that Flang-only options don't pollute the Clang output
  1744. // TODO: Make sure that Clang-only options don't pollute Flang output
  1745. if (!IsFlangMode())
  1746. DisableFlags |= options::FlangOnlyOption;
  1747. // Distinguish "--autocomplete=-someflag" and "--autocomplete=-someflag,"
  1748. // because the latter indicates that the user put space before pushing tab
  1749. // which should end up in a file completion.
  1750. const bool HasSpace = PassedFlags.endswith(",");
  1751. // Parse PassedFlags by "," as all the command-line flags are passed to this
  1752. // function separated by ","
  1753. StringRef TargetFlags = PassedFlags;
  1754. while (TargetFlags != "") {
  1755. StringRef CurFlag;
  1756. std::tie(CurFlag, TargetFlags) = TargetFlags.split(",");
  1757. Flags.push_back(std::string(CurFlag));
  1758. }
  1759. // We want to show cc1-only options only when clang is invoked with -cc1 or
  1760. // -Xclang.
  1761. if (llvm::is_contained(Flags, "-Xclang") || llvm::is_contained(Flags, "-cc1"))
  1762. DisableFlags &= ~options::NoDriverOption;
  1763. const llvm::opt::OptTable &Opts = getOpts();
  1764. StringRef Cur;
  1765. Cur = Flags.at(Flags.size() - 1);
  1766. StringRef Prev;
  1767. if (Flags.size() >= 2) {
  1768. Prev = Flags.at(Flags.size() - 2);
  1769. SuggestedCompletions = Opts.suggestValueCompletions(Prev, Cur);
  1770. }
  1771. if (SuggestedCompletions.empty())
  1772. SuggestedCompletions = Opts.suggestValueCompletions(Cur, "");
  1773. // If Flags were empty, it means the user typed `clang [tab]` where we should
  1774. // list all possible flags. If there was no value completion and the user
  1775. // pressed tab after a space, we should fall back to a file completion.
  1776. // We're printing a newline to be consistent with what we print at the end of
  1777. // this function.
  1778. if (SuggestedCompletions.empty() && HasSpace && !Flags.empty()) {
  1779. llvm::outs() << '\n';
  1780. return;
  1781. }
  1782. // When flag ends with '=' and there was no value completion, return empty
  1783. // string and fall back to the file autocompletion.
  1784. if (SuggestedCompletions.empty() && !Cur.endswith("=")) {
  1785. // If the flag is in the form of "--autocomplete=-foo",
  1786. // we were requested to print out all option names that start with "-foo".
  1787. // For example, "--autocomplete=-fsyn" is expanded to "-fsyntax-only".
  1788. SuggestedCompletions = Opts.findByPrefix(Cur, DisableFlags);
  1789. // We have to query the -W flags manually as they're not in the OptTable.
  1790. // TODO: Find a good way to add them to OptTable instead and them remove
  1791. // this code.
  1792. for (StringRef S : DiagnosticIDs::getDiagnosticFlags())
  1793. if (S.startswith(Cur))
  1794. SuggestedCompletions.push_back(std::string(S));
  1795. }
  1796. // Sort the autocomplete candidates so that shells print them out in a
  1797. // deterministic order. We could sort in any way, but we chose
  1798. // case-insensitive sorting for consistency with the -help option
  1799. // which prints out options in the case-insensitive alphabetical order.
  1800. llvm::sort(SuggestedCompletions, [](StringRef A, StringRef B) {
  1801. if (int X = A.compare_insensitive(B))
  1802. return X < 0;
  1803. return A.compare(B) > 0;
  1804. });
  1805. llvm::outs() << llvm::join(SuggestedCompletions, "\n") << '\n';
  1806. }
  1807. bool Driver::HandleImmediateArgs(const Compilation &C) {
  1808. // The order these options are handled in gcc is all over the place, but we
  1809. // don't expect inconsistencies w.r.t. that to matter in practice.
  1810. if (C.getArgs().hasArg(options::OPT_dumpmachine)) {
  1811. llvm::outs() << C.getDefaultToolChain().getTripleString() << '\n';
  1812. return false;
  1813. }
  1814. if (C.getArgs().hasArg(options::OPT_dumpversion)) {
  1815. // Since -dumpversion is only implemented for pedantic GCC compatibility, we
  1816. // return an answer which matches our definition of __VERSION__.
  1817. llvm::outs() << CLANG_VERSION_STRING << "\n";
  1818. return false;
  1819. }
  1820. if (C.getArgs().hasArg(options::OPT__print_diagnostic_categories)) {
  1821. PrintDiagnosticCategories(llvm::outs());
  1822. return false;
  1823. }
  1824. if (C.getArgs().hasArg(options::OPT_help) ||
  1825. C.getArgs().hasArg(options::OPT__help_hidden)) {
  1826. PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden));
  1827. return false;
  1828. }
  1829. if (C.getArgs().hasArg(options::OPT__version)) {
  1830. // Follow gcc behavior and use stdout for --version and stderr for -v.
  1831. PrintVersion(C, llvm::outs());
  1832. return false;
  1833. }
  1834. if (C.getArgs().hasArg(options::OPT_v) ||
  1835. C.getArgs().hasArg(options::OPT__HASH_HASH_HASH) ||
  1836. C.getArgs().hasArg(options::OPT_print_supported_cpus)) {
  1837. PrintVersion(C, llvm::errs());
  1838. SuppressMissingInputWarning = true;
  1839. }
  1840. if (C.getArgs().hasArg(options::OPT_v)) {
  1841. if (!SystemConfigDir.empty())
  1842. llvm::errs() << "System configuration file directory: "
  1843. << SystemConfigDir << "\n";
  1844. if (!UserConfigDir.empty())
  1845. llvm::errs() << "User configuration file directory: "
  1846. << UserConfigDir << "\n";
  1847. }
  1848. const ToolChain &TC = C.getDefaultToolChain();
  1849. if (C.getArgs().hasArg(options::OPT_v))
  1850. TC.printVerboseInfo(llvm::errs());
  1851. if (C.getArgs().hasArg(options::OPT_print_resource_dir)) {
  1852. llvm::outs() << ResourceDir << '\n';
  1853. return false;
  1854. }
  1855. if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
  1856. llvm::outs() << "programs: =";
  1857. bool separator = false;
  1858. // Print -B and COMPILER_PATH.
  1859. for (const std::string &Path : PrefixDirs) {
  1860. if (separator)
  1861. llvm::outs() << llvm::sys::EnvPathSeparator;
  1862. llvm::outs() << Path;
  1863. separator = true;
  1864. }
  1865. for (const std::string &Path : TC.getProgramPaths()) {
  1866. if (separator)
  1867. llvm::outs() << llvm::sys::EnvPathSeparator;
  1868. llvm::outs() << Path;
  1869. separator = true;
  1870. }
  1871. llvm::outs() << "\n";
  1872. llvm::outs() << "libraries: =" << ResourceDir;
  1873. StringRef sysroot = C.getSysRoot();
  1874. for (const std::string &Path : TC.getFilePaths()) {
  1875. // Always print a separator. ResourceDir was the first item shown.
  1876. llvm::outs() << llvm::sys::EnvPathSeparator;
  1877. // Interpretation of leading '=' is needed only for NetBSD.
  1878. if (Path[0] == '=')
  1879. llvm::outs() << sysroot << Path.substr(1);
  1880. else
  1881. llvm::outs() << Path;
  1882. }
  1883. llvm::outs() << "\n";
  1884. return false;
  1885. }
  1886. if (C.getArgs().hasArg(options::OPT_print_runtime_dir)) {
  1887. std::string RuntimePath;
  1888. // Get the first existing path, if any.
  1889. for (auto Path : TC.getRuntimePaths()) {
  1890. if (getVFS().exists(Path)) {
  1891. RuntimePath = Path;
  1892. break;
  1893. }
  1894. }
  1895. if (!RuntimePath.empty())
  1896. llvm::outs() << RuntimePath << '\n';
  1897. else
  1898. llvm::outs() << TC.getCompilerRTPath() << '\n';
  1899. return false;
  1900. }
  1901. if (C.getArgs().hasArg(options::OPT_print_diagnostic_options)) {
  1902. std::vector<std::string> Flags = DiagnosticIDs::getDiagnosticFlags();
  1903. for (std::size_t I = 0; I != Flags.size(); I += 2)
  1904. llvm::outs() << " " << Flags[I] << "\n " << Flags[I + 1] << "\n\n";
  1905. return false;
  1906. }
  1907. // FIXME: The following handlers should use a callback mechanism, we don't
  1908. // know what the client would like to do.
  1909. if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
  1910. llvm::outs() << GetFilePath(A->getValue(), TC) << "\n";
  1911. return false;
  1912. }
  1913. if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
  1914. StringRef ProgName = A->getValue();
  1915. // Null program name cannot have a path.
  1916. if (! ProgName.empty())
  1917. llvm::outs() << GetProgramPath(ProgName, TC);
  1918. llvm::outs() << "\n";
  1919. return false;
  1920. }
  1921. if (Arg *A = C.getArgs().getLastArg(options::OPT_autocomplete)) {
  1922. StringRef PassedFlags = A->getValue();
  1923. HandleAutocompletions(PassedFlags);
  1924. return false;
  1925. }
  1926. if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
  1927. ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(C.getArgs());
  1928. const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(C.getArgs()));
  1929. RegisterEffectiveTriple TripleRAII(TC, Triple);
  1930. switch (RLT) {
  1931. case ToolChain::RLT_CompilerRT:
  1932. llvm::outs() << TC.getCompilerRT(C.getArgs(), "builtins") << "\n";
  1933. break;
  1934. case ToolChain::RLT_Libgcc:
  1935. llvm::outs() << GetFilePath("libgcc.a", TC) << "\n";
  1936. break;
  1937. }
  1938. return false;
  1939. }
  1940. if (C.getArgs().hasArg(options::OPT_print_multi_lib)) {
  1941. for (const Multilib &Multilib : TC.getMultilibs())
  1942. llvm::outs() << Multilib << "\n";
  1943. return false;
  1944. }
  1945. if (C.getArgs().hasArg(options::OPT_print_multi_directory)) {
  1946. const Multilib &Multilib = TC.getMultilib();
  1947. if (Multilib.gccSuffix().empty())
  1948. llvm::outs() << ".\n";
  1949. else {
  1950. StringRef Suffix(Multilib.gccSuffix());
  1951. assert(Suffix.front() == '/');
  1952. llvm::outs() << Suffix.substr(1) << "\n";
  1953. }
  1954. return false;
  1955. }
  1956. if (C.getArgs().hasArg(options::OPT_print_target_triple)) {
  1957. llvm::outs() << TC.getTripleString() << "\n";
  1958. return false;
  1959. }
  1960. if (C.getArgs().hasArg(options::OPT_print_effective_triple)) {
  1961. const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(C.getArgs()));
  1962. llvm::outs() << Triple.getTriple() << "\n";
  1963. return false;
  1964. }
  1965. if (C.getArgs().hasArg(options::OPT_print_targets)) {
  1966. llvm::TargetRegistry::printRegisteredTargetsForVersion(llvm::outs());
  1967. return false;
  1968. }
  1969. return true;
  1970. }
  1971. enum {
  1972. TopLevelAction = 0,
  1973. HeadSibAction = 1,
  1974. OtherSibAction = 2,
  1975. };
  1976. // Display an action graph human-readably. Action A is the "sink" node
  1977. // and latest-occuring action. Traversal is in pre-order, visiting the
  1978. // inputs to each action before printing the action itself.
  1979. static unsigned PrintActions1(const Compilation &C, Action *A,
  1980. std::map<Action *, unsigned> &Ids,
  1981. Twine Indent = {}, int Kind = TopLevelAction) {
  1982. if (Ids.count(A)) // A was already visited.
  1983. return Ids[A];
  1984. std::string str;
  1985. llvm::raw_string_ostream os(str);
  1986. auto getSibIndent = [](int K) -> Twine {
  1987. return (K == HeadSibAction) ? " " : (K == OtherSibAction) ? "| " : "";
  1988. };
  1989. Twine SibIndent = Indent + getSibIndent(Kind);
  1990. int SibKind = HeadSibAction;
  1991. os << Action::getClassName(A->getKind()) << ", ";
  1992. if (InputAction *IA = dyn_cast<InputAction>(A)) {
  1993. os << "\"" << IA->getInputArg().getValue() << "\"";
  1994. } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) {
  1995. os << '"' << BIA->getArchName() << '"' << ", {"
  1996. << PrintActions1(C, *BIA->input_begin(), Ids, SibIndent, SibKind) << "}";
  1997. } else if (OffloadAction *OA = dyn_cast<OffloadAction>(A)) {
  1998. bool IsFirst = true;
  1999. OA->doOnEachDependence(
  2000. [&](Action *A, const ToolChain *TC, const char *BoundArch) {
  2001. assert(TC && "Unknown host toolchain");
  2002. // E.g. for two CUDA device dependences whose bound arch is sm_20 and
  2003. // sm_35 this will generate:
  2004. // "cuda-device" (nvptx64-nvidia-cuda:sm_20) {#ID}, "cuda-device"
  2005. // (nvptx64-nvidia-cuda:sm_35) {#ID}
  2006. if (!IsFirst)
  2007. os << ", ";
  2008. os << '"';
  2009. os << A->getOffloadingKindPrefix();
  2010. os << " (";
  2011. os << TC->getTriple().normalize();
  2012. if (BoundArch)
  2013. os << ":" << BoundArch;
  2014. os << ")";
  2015. os << '"';
  2016. os << " {" << PrintActions1(C, A, Ids, SibIndent, SibKind) << "}";
  2017. IsFirst = false;
  2018. SibKind = OtherSibAction;
  2019. });
  2020. } else {
  2021. const ActionList *AL = &A->getInputs();
  2022. if (AL->size()) {
  2023. const char *Prefix = "{";
  2024. for (Action *PreRequisite : *AL) {
  2025. os << Prefix << PrintActions1(C, PreRequisite, Ids, SibIndent, SibKind);
  2026. Prefix = ", ";
  2027. SibKind = OtherSibAction;
  2028. }
  2029. os << "}";
  2030. } else
  2031. os << "{}";
  2032. }
  2033. // Append offload info for all options other than the offloading action
  2034. // itself (e.g. (cuda-device, sm_20) or (cuda-host)).
  2035. std::string offload_str;
  2036. llvm::raw_string_ostream offload_os(offload_str);
  2037. if (!isa<OffloadAction>(A)) {
  2038. auto S = A->getOffloadingKindPrefix();
  2039. if (!S.empty()) {
  2040. offload_os << ", (" << S;
  2041. if (A->getOffloadingArch())
  2042. offload_os << ", " << A->getOffloadingArch();
  2043. offload_os << ")";
  2044. }
  2045. }
  2046. auto getSelfIndent = [](int K) -> Twine {
  2047. return (K == HeadSibAction) ? "+- " : (K == OtherSibAction) ? "|- " : "";
  2048. };
  2049. unsigned Id = Ids.size();
  2050. Ids[A] = Id;
  2051. llvm::errs() << Indent + getSelfIndent(Kind) << Id << ": " << os.str() << ", "
  2052. << types::getTypeName(A->getType()) << offload_os.str() << "\n";
  2053. return Id;
  2054. }
  2055. // Print the action graphs in a compilation C.
  2056. // For example "clang -c file1.c file2.c" is composed of two subgraphs.
  2057. void Driver::PrintActions(const Compilation &C) const {
  2058. std::map<Action *, unsigned> Ids;
  2059. for (Action *A : C.getActions())
  2060. PrintActions1(C, A, Ids);
  2061. }
  2062. /// Check whether the given input tree contains any compilation or
  2063. /// assembly actions.
  2064. static bool ContainsCompileOrAssembleAction(const Action *A) {
  2065. if (isa<CompileJobAction>(A) || isa<BackendJobAction>(A) ||
  2066. isa<AssembleJobAction>(A))
  2067. return true;
  2068. return llvm::any_of(A->inputs(), ContainsCompileOrAssembleAction);
  2069. }
  2070. void Driver::BuildUniversalActions(Compilation &C, const ToolChain &TC,
  2071. const InputList &BAInputs) const {
  2072. DerivedArgList &Args = C.getArgs();
  2073. ActionList &Actions = C.getActions();
  2074. llvm::PrettyStackTraceString CrashInfo("Building universal build actions");
  2075. // Collect the list of architectures. Duplicates are allowed, but should only
  2076. // be handled once (in the order seen).
  2077. llvm::StringSet<> ArchNames;
  2078. SmallVector<const char *, 4> Archs;
  2079. for (Arg *A : Args) {
  2080. if (A->getOption().matches(options::OPT_arch)) {
  2081. // Validate the option here; we don't save the type here because its
  2082. // particular spelling may participate in other driver choices.
  2083. llvm::Triple::ArchType Arch =
  2084. tools::darwin::getArchTypeForMachOArchName(A->getValue());
  2085. if (Arch == llvm::Triple::UnknownArch) {
  2086. Diag(clang::diag::err_drv_invalid_arch_name) << A->getAsString(Args);
  2087. continue;
  2088. }
  2089. A->claim();
  2090. if (ArchNames.insert(A->getValue()).second)
  2091. Archs.push_back(A->getValue());
  2092. }
  2093. }
  2094. // When there is no explicit arch for this platform, make sure we still bind
  2095. // the architecture (to the default) so that -Xarch_ is handled correctly.
  2096. if (!Archs.size())
  2097. Archs.push_back(Args.MakeArgString(TC.getDefaultUniversalArchName()));
  2098. ActionList SingleActions;
  2099. BuildActions(C, Args, BAInputs, SingleActions);
  2100. // Add in arch bindings for every top level action, as well as lipo and
  2101. // dsymutil steps if needed.
  2102. for (Action* Act : SingleActions) {
  2103. // Make sure we can lipo this kind of output. If not (and it is an actual
  2104. // output) then we disallow, since we can't create an output file with the
  2105. // right name without overwriting it. We could remove this oddity by just
  2106. // changing the output names to include the arch, which would also fix
  2107. // -save-temps. Compatibility wins for now.
  2108. if (Archs.size() > 1 && !types::canLipoType(Act->getType()))
  2109. Diag(clang::diag::err_drv_invalid_output_with_multiple_archs)
  2110. << types::getTypeName(Act->getType());
  2111. ActionList Inputs;
  2112. for (unsigned i = 0, e = Archs.size(); i != e; ++i)
  2113. Inputs.push_back(C.MakeAction<BindArchAction>(Act, Archs[i]));
  2114. // Lipo if necessary, we do it this way because we need to set the arch flag
  2115. // so that -Xarch_ gets overwritten.
  2116. if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing)
  2117. Actions.append(Inputs.begin(), Inputs.end());
  2118. else
  2119. Actions.push_back(C.MakeAction<LipoJobAction>(Inputs, Act->getType()));
  2120. // Handle debug info queries.
  2121. Arg *A = Args.getLastArg(options::OPT_g_Group);
  2122. bool enablesDebugInfo = A && !A->getOption().matches(options::OPT_g0) &&
  2123. !A->getOption().matches(options::OPT_gstabs);
  2124. if ((enablesDebugInfo || willEmitRemarks(Args)) &&
  2125. ContainsCompileOrAssembleAction(Actions.back())) {
  2126. // Add a 'dsymutil' step if necessary, when debug info is enabled and we
  2127. // have a compile input. We need to run 'dsymutil' ourselves in such cases
  2128. // because the debug info will refer to a temporary object file which
  2129. // will be removed at the end of the compilation process.
  2130. if (Act->getType() == types::TY_Image) {
  2131. ActionList Inputs;
  2132. Inputs.push_back(Actions.back());
  2133. Actions.pop_back();
  2134. Actions.push_back(
  2135. C.MakeAction<DsymutilJobAction>(Inputs, types::TY_dSYM));
  2136. }
  2137. // Verify the debug info output.
  2138. if (Args.hasArg(options::OPT_verify_debug_info)) {
  2139. Action* LastAction = Actions.back();
  2140. Actions.pop_back();
  2141. Actions.push_back(C.MakeAction<VerifyDebugInfoJobAction>(
  2142. LastAction, types::TY_Nothing));
  2143. }
  2144. }
  2145. }
  2146. }
  2147. bool Driver::DiagnoseInputExistence(const DerivedArgList &Args, StringRef Value,
  2148. types::ID Ty, bool TypoCorrect) const {
  2149. if (!getCheckInputsExist())
  2150. return true;
  2151. // stdin always exists.
  2152. if (Value == "-")
  2153. return true;
  2154. // If it's a header to be found in the system or user search path, then defer
  2155. // complaints about its absence until those searches can be done. When we
  2156. // are definitely processing headers for C++20 header units, extend this to
  2157. // allow the user to put "-fmodule-header -xc++-header vector" for example.
  2158. if (Ty == types::TY_CXXSHeader || Ty == types::TY_CXXUHeader ||
  2159. (ModulesModeCXX20 && Ty == types::TY_CXXHeader))
  2160. return true;
  2161. if (getVFS().exists(Value))
  2162. return true;
  2163. if (TypoCorrect) {
  2164. // Check if the filename is a typo for an option flag. OptTable thinks
  2165. // that all args that are not known options and that start with / are
  2166. // filenames, but e.g. `/diagnostic:caret` is more likely a typo for
  2167. // the option `/diagnostics:caret` than a reference to a file in the root
  2168. // directory.
  2169. unsigned IncludedFlagsBitmask;
  2170. unsigned ExcludedFlagsBitmask;
  2171. std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) =
  2172. getIncludeExcludeOptionFlagMasks(IsCLMode());
  2173. std::string Nearest;
  2174. if (getOpts().findNearest(Value, Nearest, IncludedFlagsBitmask,
  2175. ExcludedFlagsBitmask) <= 1) {
  2176. Diag(clang::diag::err_drv_no_such_file_with_suggestion)
  2177. << Value << Nearest;
  2178. return false;
  2179. }
  2180. }
  2181. // In CL mode, don't error on apparently non-existent linker inputs, because
  2182. // they can be influenced by linker flags the clang driver might not
  2183. // understand.
  2184. // Examples:
  2185. // - `clang-cl main.cc ole32.lib` in a non-MSVC shell will make the driver
  2186. // module look for an MSVC installation in the registry. (We could ask
  2187. // the MSVCToolChain object if it can find `ole32.lib`, but the logic to
  2188. // look in the registry might move into lld-link in the future so that
  2189. // lld-link invocations in non-MSVC shells just work too.)
  2190. // - `clang-cl ... /link ...` can pass arbitrary flags to the linker,
  2191. // including /libpath:, which is used to find .lib and .obj files.
  2192. // So do not diagnose this on the driver level. Rely on the linker diagnosing
  2193. // it. (If we don't end up invoking the linker, this means we'll emit a
  2194. // "'linker' input unused [-Wunused-command-line-argument]" warning instead
  2195. // of an error.)
  2196. //
  2197. // Only do this skip after the typo correction step above. `/Brepo` is treated
  2198. // as TY_Object, but it's clearly a typo for `/Brepro`. It seems fine to emit
  2199. // an error if we have a flag that's within an edit distance of 1 from a
  2200. // flag. (Users can use `-Wl,` or `/linker` to launder the flag past the
  2201. // driver in the unlikely case they run into this.)
  2202. //
  2203. // Don't do this for inputs that start with a '/', else we'd pass options
  2204. // like /libpath: through to the linker silently.
  2205. //
  2206. // Emitting an error for linker inputs can also cause incorrect diagnostics
  2207. // with the gcc driver. The command
  2208. // clang -fuse-ld=lld -Wl,--chroot,some/dir /file.o
  2209. // will make lld look for some/dir/file.o, while we will diagnose here that
  2210. // `/file.o` does not exist. However, configure scripts check if
  2211. // `clang /GR-` compiles without error to see if the compiler is cl.exe,
  2212. // so we can't downgrade diagnostics for `/GR-` from an error to a warning
  2213. // in cc mode. (We can in cl mode because cl.exe itself only warns on
  2214. // unknown flags.)
  2215. if (IsCLMode() && Ty == types::TY_Object && !Value.startswith("/"))
  2216. return true;
  2217. Diag(clang::diag::err_drv_no_such_file) << Value;
  2218. return false;
  2219. }
  2220. // Get the C++20 Header Unit type corresponding to the input type.
  2221. static types::ID CXXHeaderUnitType(ModuleHeaderMode HM) {
  2222. switch (HM) {
  2223. case HeaderMode_User:
  2224. return types::TY_CXXUHeader;
  2225. case HeaderMode_System:
  2226. return types::TY_CXXSHeader;
  2227. case HeaderMode_Default:
  2228. break;
  2229. case HeaderMode_None:
  2230. llvm_unreachable("should not be called in this case");
  2231. }
  2232. return types::TY_CXXHUHeader;
  2233. }
  2234. // Construct a the list of inputs and their types.
  2235. void Driver::BuildInputs(const ToolChain &TC, DerivedArgList &Args,
  2236. InputList &Inputs) const {
  2237. const llvm::opt::OptTable &Opts = getOpts();
  2238. // Track the current user specified (-x) input. We also explicitly track the
  2239. // argument used to set the type; we only want to claim the type when we
  2240. // actually use it, so we warn about unused -x arguments.
  2241. types::ID InputType = types::TY_Nothing;
  2242. Arg *InputTypeArg = nullptr;
  2243. // The last /TC or /TP option sets the input type to C or C++ globally.
  2244. if (Arg *TCTP = Args.getLastArgNoClaim(options::OPT__SLASH_TC,
  2245. options::OPT__SLASH_TP)) {
  2246. InputTypeArg = TCTP;
  2247. InputType = TCTP->getOption().matches(options::OPT__SLASH_TC)
  2248. ? types::TY_C
  2249. : types::TY_CXX;
  2250. Arg *Previous = nullptr;
  2251. bool ShowNote = false;
  2252. for (Arg *A :
  2253. Args.filtered(options::OPT__SLASH_TC, options::OPT__SLASH_TP)) {
  2254. if (Previous) {
  2255. Diag(clang::diag::warn_drv_overriding_flag_option)
  2256. << Previous->getSpelling() << A->getSpelling();
  2257. ShowNote = true;
  2258. }
  2259. Previous = A;
  2260. }
  2261. if (ShowNote)
  2262. Diag(clang::diag::note_drv_t_option_is_global);
  2263. // No driver mode exposes -x and /TC or /TP; we don't support mixing them.
  2264. assert(!Args.hasArg(options::OPT_x) && "-x and /TC or /TP is not allowed");
  2265. }
  2266. // Warn -x after last input file has no effect
  2267. {
  2268. Arg *LastXArg = Args.getLastArgNoClaim(options::OPT_x);
  2269. Arg *LastInputArg = Args.getLastArgNoClaim(options::OPT_INPUT);
  2270. if (LastXArg && LastInputArg && LastInputArg->getIndex() < LastXArg->getIndex())
  2271. Diag(clang::diag::warn_drv_unused_x) << LastXArg->getValue();
  2272. }
  2273. for (Arg *A : Args) {
  2274. if (A->getOption().getKind() == Option::InputClass) {
  2275. const char *Value = A->getValue();
  2276. types::ID Ty = types::TY_INVALID;
  2277. // Infer the input type if necessary.
  2278. if (InputType == types::TY_Nothing) {
  2279. // If there was an explicit arg for this, claim it.
  2280. if (InputTypeArg)
  2281. InputTypeArg->claim();
  2282. // stdin must be handled specially.
  2283. if (memcmp(Value, "-", 2) == 0) {
  2284. if (IsFlangMode()) {
  2285. Ty = types::TY_Fortran;
  2286. } else {
  2287. // If running with -E, treat as a C input (this changes the
  2288. // builtin macros, for example). This may be overridden by -ObjC
  2289. // below.
  2290. //
  2291. // Otherwise emit an error but still use a valid type to avoid
  2292. // spurious errors (e.g., no inputs).
  2293. assert(!CCGenDiagnostics && "stdin produces no crash reproducer");
  2294. if (!Args.hasArgNoClaim(options::OPT_E) && !CCCIsCPP())
  2295. Diag(IsCLMode() ? clang::diag::err_drv_unknown_stdin_type_clang_cl
  2296. : clang::diag::err_drv_unknown_stdin_type);
  2297. Ty = types::TY_C;
  2298. }
  2299. } else {
  2300. // Otherwise lookup by extension.
  2301. // Fallback is C if invoked as C preprocessor, C++ if invoked with
  2302. // clang-cl /E, or Object otherwise.
  2303. // We use a host hook here because Darwin at least has its own
  2304. // idea of what .s is.
  2305. if (const char *Ext = strrchr(Value, '.'))
  2306. Ty = TC.LookupTypeForExtension(Ext + 1);
  2307. if (Ty == types::TY_INVALID) {
  2308. if (IsCLMode() && (Args.hasArgNoClaim(options::OPT_E) || CCGenDiagnostics))
  2309. Ty = types::TY_CXX;
  2310. else if (CCCIsCPP() || CCGenDiagnostics)
  2311. Ty = types::TY_C;
  2312. else
  2313. Ty = types::TY_Object;
  2314. }
  2315. // If the driver is invoked as C++ compiler (like clang++ or c++) it
  2316. // should autodetect some input files as C++ for g++ compatibility.
  2317. if (CCCIsCXX()) {
  2318. types::ID OldTy = Ty;
  2319. Ty = types::lookupCXXTypeForCType(Ty);
  2320. // Do not complain about foo.h, when we are known to be processing
  2321. // it as a C++20 header unit.
  2322. if (Ty != OldTy && !(OldTy == types::TY_CHeader && hasHeaderMode()))
  2323. Diag(clang::diag::warn_drv_treating_input_as_cxx)
  2324. << getTypeName(OldTy) << getTypeName(Ty);
  2325. }
  2326. // If running with -fthinlto-index=, extensions that normally identify
  2327. // native object files actually identify LLVM bitcode files.
  2328. if (Args.hasArgNoClaim(options::OPT_fthinlto_index_EQ) &&
  2329. Ty == types::TY_Object)
  2330. Ty = types::TY_LLVM_BC;
  2331. }
  2332. // -ObjC and -ObjC++ override the default language, but only for "source
  2333. // files". We just treat everything that isn't a linker input as a
  2334. // source file.
  2335. //
  2336. // FIXME: Clean this up if we move the phase sequence into the type.
  2337. if (Ty != types::TY_Object) {
  2338. if (Args.hasArg(options::OPT_ObjC))
  2339. Ty = types::TY_ObjC;
  2340. else if (Args.hasArg(options::OPT_ObjCXX))
  2341. Ty = types::TY_ObjCXX;
  2342. }
  2343. // Disambiguate headers that are meant to be header units from those
  2344. // intended to be PCH. Avoid missing '.h' cases that are counted as
  2345. // C headers by default - we know we are in C++ mode and we do not
  2346. // want to issue a complaint about compiling things in the wrong mode.
  2347. if ((Ty == types::TY_CXXHeader || Ty == types::TY_CHeader) &&
  2348. hasHeaderMode())
  2349. Ty = CXXHeaderUnitType(CXX20HeaderType);
  2350. } else {
  2351. assert(InputTypeArg && "InputType set w/o InputTypeArg");
  2352. if (!InputTypeArg->getOption().matches(options::OPT_x)) {
  2353. // If emulating cl.exe, make sure that /TC and /TP don't affect input
  2354. // object files.
  2355. const char *Ext = strrchr(Value, '.');
  2356. if (Ext && TC.LookupTypeForExtension(Ext + 1) == types::TY_Object)
  2357. Ty = types::TY_Object;
  2358. }
  2359. if (Ty == types::TY_INVALID) {
  2360. Ty = InputType;
  2361. InputTypeArg->claim();
  2362. }
  2363. }
  2364. if (DiagnoseInputExistence(Args, Value, Ty, /*TypoCorrect=*/true))
  2365. Inputs.push_back(std::make_pair(Ty, A));
  2366. } else if (A->getOption().matches(options::OPT__SLASH_Tc)) {
  2367. StringRef Value = A->getValue();
  2368. if (DiagnoseInputExistence(Args, Value, types::TY_C,
  2369. /*TypoCorrect=*/false)) {
  2370. Arg *InputArg = MakeInputArg(Args, Opts, A->getValue());
  2371. Inputs.push_back(std::make_pair(types::TY_C, InputArg));
  2372. }
  2373. A->claim();
  2374. } else if (A->getOption().matches(options::OPT__SLASH_Tp)) {
  2375. StringRef Value = A->getValue();
  2376. if (DiagnoseInputExistence(Args, Value, types::TY_CXX,
  2377. /*TypoCorrect=*/false)) {
  2378. Arg *InputArg = MakeInputArg(Args, Opts, A->getValue());
  2379. Inputs.push_back(std::make_pair(types::TY_CXX, InputArg));
  2380. }
  2381. A->claim();
  2382. } else if (A->getOption().hasFlag(options::LinkerInput)) {
  2383. // Just treat as object type, we could make a special type for this if
  2384. // necessary.
  2385. Inputs.push_back(std::make_pair(types::TY_Object, A));
  2386. } else if (A->getOption().matches(options::OPT_x)) {
  2387. InputTypeArg = A;
  2388. InputType = types::lookupTypeForTypeSpecifier(A->getValue());
  2389. A->claim();
  2390. // Follow gcc behavior and treat as linker input for invalid -x
  2391. // options. Its not clear why we shouldn't just revert to unknown; but
  2392. // this isn't very important, we might as well be bug compatible.
  2393. if (!InputType) {
  2394. Diag(clang::diag::err_drv_unknown_language) << A->getValue();
  2395. InputType = types::TY_Object;
  2396. }
  2397. // If the user has put -fmodule-header{,=} then we treat C++ headers as
  2398. // header unit inputs. So we 'promote' -xc++-header appropriately.
  2399. if (InputType == types::TY_CXXHeader && hasHeaderMode())
  2400. InputType = CXXHeaderUnitType(CXX20HeaderType);
  2401. } else if (A->getOption().getID() == options::OPT_U) {
  2402. assert(A->getNumValues() == 1 && "The /U option has one value.");
  2403. StringRef Val = A->getValue(0);
  2404. if (Val.find_first_of("/\\") != StringRef::npos) {
  2405. // Warn about e.g. "/Users/me/myfile.c".
  2406. Diag(diag::warn_slash_u_filename) << Val;
  2407. Diag(diag::note_use_dashdash);
  2408. }
  2409. }
  2410. }
  2411. if (CCCIsCPP() && Inputs.empty()) {
  2412. // If called as standalone preprocessor, stdin is processed
  2413. // if no other input is present.
  2414. Arg *A = MakeInputArg(Args, Opts, "-");
  2415. Inputs.push_back(std::make_pair(types::TY_C, A));
  2416. }
  2417. }
  2418. namespace {
  2419. /// Provides a convenient interface for different programming models to generate
  2420. /// the required device actions.
  2421. class OffloadingActionBuilder final {
  2422. /// Flag used to trace errors in the builder.
  2423. bool IsValid = false;
  2424. /// The compilation that is using this builder.
  2425. Compilation &C;
  2426. /// Map between an input argument and the offload kinds used to process it.
  2427. std::map<const Arg *, unsigned> InputArgToOffloadKindMap;
  2428. /// Map between a host action and its originating input argument.
  2429. std::map<Action *, const Arg *> HostActionToInputArgMap;
  2430. /// Builder interface. It doesn't build anything or keep any state.
  2431. class DeviceActionBuilder {
  2432. public:
  2433. typedef const llvm::SmallVectorImpl<phases::ID> PhasesTy;
  2434. enum ActionBuilderReturnCode {
  2435. // The builder acted successfully on the current action.
  2436. ABRT_Success,
  2437. // The builder didn't have to act on the current action.
  2438. ABRT_Inactive,
  2439. // The builder was successful and requested the host action to not be
  2440. // generated.
  2441. ABRT_Ignore_Host,
  2442. };
  2443. protected:
  2444. /// Compilation associated with this builder.
  2445. Compilation &C;
  2446. /// Tool chains associated with this builder. The same programming
  2447. /// model may have associated one or more tool chains.
  2448. SmallVector<const ToolChain *, 2> ToolChains;
  2449. /// The derived arguments associated with this builder.
  2450. DerivedArgList &Args;
  2451. /// The inputs associated with this builder.
  2452. const Driver::InputList &Inputs;
  2453. /// The associated offload kind.
  2454. Action::OffloadKind AssociatedOffloadKind = Action::OFK_None;
  2455. public:
  2456. DeviceActionBuilder(Compilation &C, DerivedArgList &Args,
  2457. const Driver::InputList &Inputs,
  2458. Action::OffloadKind AssociatedOffloadKind)
  2459. : C(C), Args(Args), Inputs(Inputs),
  2460. AssociatedOffloadKind(AssociatedOffloadKind) {}
  2461. virtual ~DeviceActionBuilder() {}
  2462. /// Fill up the array \a DA with all the device dependences that should be
  2463. /// added to the provided host action \a HostAction. By default it is
  2464. /// inactive.
  2465. virtual ActionBuilderReturnCode
  2466. getDeviceDependences(OffloadAction::DeviceDependences &DA,
  2467. phases::ID CurPhase, phases::ID FinalPhase,
  2468. PhasesTy &Phases) {
  2469. return ABRT_Inactive;
  2470. }
  2471. /// Update the state to include the provided host action \a HostAction as a
  2472. /// dependency of the current device action. By default it is inactive.
  2473. virtual ActionBuilderReturnCode addDeviceDependences(Action *HostAction) {
  2474. return ABRT_Inactive;
  2475. }
  2476. /// Append top level actions generated by the builder.
  2477. virtual void appendTopLevelActions(ActionList &AL) {}
  2478. /// Append linker device actions generated by the builder.
  2479. virtual void appendLinkDeviceActions(ActionList &AL) {}
  2480. /// Append linker host action generated by the builder.
  2481. virtual Action* appendLinkHostActions(ActionList &AL) { return nullptr; }
  2482. /// Append linker actions generated by the builder.
  2483. virtual void appendLinkDependences(OffloadAction::DeviceDependences &DA) {}
  2484. /// Initialize the builder. Return true if any initialization errors are
  2485. /// found.
  2486. virtual bool initialize() { return false; }
  2487. /// Return true if the builder can use bundling/unbundling.
  2488. virtual bool canUseBundlerUnbundler() const { return false; }
  2489. /// Return true if this builder is valid. We have a valid builder if we have
  2490. /// associated device tool chains.
  2491. bool isValid() { return !ToolChains.empty(); }
  2492. /// Return the associated offload kind.
  2493. Action::OffloadKind getAssociatedOffloadKind() {
  2494. return AssociatedOffloadKind;
  2495. }
  2496. };
  2497. /// Base class for CUDA/HIP action builder. It injects device code in
  2498. /// the host backend action.
  2499. class CudaActionBuilderBase : public DeviceActionBuilder {
  2500. protected:
  2501. /// Flags to signal if the user requested host-only or device-only
  2502. /// compilation.
  2503. bool CompileHostOnly = false;
  2504. bool CompileDeviceOnly = false;
  2505. bool EmitLLVM = false;
  2506. bool EmitAsm = false;
  2507. /// ID to identify each device compilation. For CUDA it is simply the
  2508. /// GPU arch string. For HIP it is either the GPU arch string or GPU
  2509. /// arch string plus feature strings delimited by a plus sign, e.g.
  2510. /// gfx906+xnack.
  2511. struct TargetID {
  2512. /// Target ID string which is persistent throughout the compilation.
  2513. const char *ID;
  2514. TargetID(CudaArch Arch) { ID = CudaArchToString(Arch); }
  2515. TargetID(const char *ID) : ID(ID) {}
  2516. operator const char *() { return ID; }
  2517. operator StringRef() { return StringRef(ID); }
  2518. };
  2519. /// List of GPU architectures to use in this compilation.
  2520. SmallVector<TargetID, 4> GpuArchList;
  2521. /// The CUDA actions for the current input.
  2522. ActionList CudaDeviceActions;
  2523. /// The CUDA fat binary if it was generated for the current input.
  2524. Action *CudaFatBinary = nullptr;
  2525. /// Flag that is set to true if this builder acted on the current input.
  2526. bool IsActive = false;
  2527. /// Flag for -fgpu-rdc.
  2528. bool Relocatable = false;
  2529. /// Default GPU architecture if there's no one specified.
  2530. CudaArch DefaultCudaArch = CudaArch::UNKNOWN;
  2531. /// Method to generate compilation unit ID specified by option
  2532. /// '-fuse-cuid='.
  2533. enum UseCUIDKind { CUID_Hash, CUID_Random, CUID_None, CUID_Invalid };
  2534. UseCUIDKind UseCUID = CUID_Hash;
  2535. /// Compilation unit ID specified by option '-cuid='.
  2536. StringRef FixedCUID;
  2537. public:
  2538. CudaActionBuilderBase(Compilation &C, DerivedArgList &Args,
  2539. const Driver::InputList &Inputs,
  2540. Action::OffloadKind OFKind)
  2541. : DeviceActionBuilder(C, Args, Inputs, OFKind) {}
  2542. ActionBuilderReturnCode addDeviceDependences(Action *HostAction) override {
  2543. // While generating code for CUDA, we only depend on the host input action
  2544. // to trigger the creation of all the CUDA device actions.
  2545. // If we are dealing with an input action, replicate it for each GPU
  2546. // architecture. If we are in host-only mode we return 'success' so that
  2547. // the host uses the CUDA offload kind.
  2548. if (auto *IA = dyn_cast<InputAction>(HostAction)) {
  2549. assert(!GpuArchList.empty() &&
  2550. "We should have at least one GPU architecture.");
  2551. // If the host input is not CUDA or HIP, we don't need to bother about
  2552. // this input.
  2553. if (!(IA->getType() == types::TY_CUDA ||
  2554. IA->getType() == types::TY_HIP ||
  2555. IA->getType() == types::TY_PP_HIP)) {
  2556. // The builder will ignore this input.
  2557. IsActive = false;
  2558. return ABRT_Inactive;
  2559. }
  2560. // Set the flag to true, so that the builder acts on the current input.
  2561. IsActive = true;
  2562. if (CompileHostOnly)
  2563. return ABRT_Success;
  2564. // Replicate inputs for each GPU architecture.
  2565. auto Ty = IA->getType() == types::TY_HIP ? types::TY_HIP_DEVICE
  2566. : types::TY_CUDA_DEVICE;
  2567. std::string CUID = FixedCUID.str();
  2568. if (CUID.empty()) {
  2569. if (UseCUID == CUID_Random)
  2570. CUID = llvm::utohexstr(llvm::sys::Process::GetRandomNumber(),
  2571. /*LowerCase=*/true);
  2572. else if (UseCUID == CUID_Hash) {
  2573. llvm::MD5 Hasher;
  2574. llvm::MD5::MD5Result Hash;
  2575. SmallString<256> RealPath;
  2576. llvm::sys::fs::real_path(IA->getInputArg().getValue(), RealPath,
  2577. /*expand_tilde=*/true);
  2578. Hasher.update(RealPath);
  2579. for (auto *A : Args) {
  2580. if (A->getOption().matches(options::OPT_INPUT))
  2581. continue;
  2582. Hasher.update(A->getAsString(Args));
  2583. }
  2584. Hasher.final(Hash);
  2585. CUID = llvm::utohexstr(Hash.low(), /*LowerCase=*/true);
  2586. }
  2587. }
  2588. IA->setId(CUID);
  2589. for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) {
  2590. CudaDeviceActions.push_back(
  2591. C.MakeAction<InputAction>(IA->getInputArg(), Ty, IA->getId()));
  2592. }
  2593. return ABRT_Success;
  2594. }
  2595. // If this is an unbundling action use it as is for each CUDA toolchain.
  2596. if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(HostAction)) {
  2597. // If -fgpu-rdc is disabled, should not unbundle since there is no
  2598. // device code to link.
  2599. if (UA->getType() == types::TY_Object && !Relocatable)
  2600. return ABRT_Inactive;
  2601. CudaDeviceActions.clear();
  2602. auto *IA = cast<InputAction>(UA->getInputs().back());
  2603. std::string FileName = IA->getInputArg().getAsString(Args);
  2604. // Check if the type of the file is the same as the action. Do not
  2605. // unbundle it if it is not. Do not unbundle .so files, for example,
  2606. // which are not object files. Files with extension ".lib" is classified
  2607. // as TY_Object but they are actually archives, therefore should not be
  2608. // unbundled here as objects. They will be handled at other places.
  2609. const StringRef LibFileExt = ".lib";
  2610. if (IA->getType() == types::TY_Object &&
  2611. (!llvm::sys::path::has_extension(FileName) ||
  2612. types::lookupTypeForExtension(
  2613. llvm::sys::path::extension(FileName).drop_front()) !=
  2614. types::TY_Object ||
  2615. llvm::sys::path::extension(FileName) == LibFileExt))
  2616. return ABRT_Inactive;
  2617. for (auto Arch : GpuArchList) {
  2618. CudaDeviceActions.push_back(UA);
  2619. UA->registerDependentActionInfo(ToolChains[0], Arch,
  2620. AssociatedOffloadKind);
  2621. }
  2622. IsActive = true;
  2623. return ABRT_Success;
  2624. }
  2625. return IsActive ? ABRT_Success : ABRT_Inactive;
  2626. }
  2627. void appendTopLevelActions(ActionList &AL) override {
  2628. // Utility to append actions to the top level list.
  2629. auto AddTopLevel = [&](Action *A, TargetID TargetID) {
  2630. OffloadAction::DeviceDependences Dep;
  2631. Dep.add(*A, *ToolChains.front(), TargetID, AssociatedOffloadKind);
  2632. AL.push_back(C.MakeAction<OffloadAction>(Dep, A->getType()));
  2633. };
  2634. // If we have a fat binary, add it to the list.
  2635. if (CudaFatBinary) {
  2636. AddTopLevel(CudaFatBinary, CudaArch::UNUSED);
  2637. CudaDeviceActions.clear();
  2638. CudaFatBinary = nullptr;
  2639. return;
  2640. }
  2641. if (CudaDeviceActions.empty())
  2642. return;
  2643. // If we have CUDA actions at this point, that's because we have a have
  2644. // partial compilation, so we should have an action for each GPU
  2645. // architecture.
  2646. assert(CudaDeviceActions.size() == GpuArchList.size() &&
  2647. "Expecting one action per GPU architecture.");
  2648. assert(ToolChains.size() == 1 &&
  2649. "Expecting to have a single CUDA toolchain.");
  2650. for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I)
  2651. AddTopLevel(CudaDeviceActions[I], GpuArchList[I]);
  2652. CudaDeviceActions.clear();
  2653. }
  2654. /// Get canonicalized offload arch option. \returns empty StringRef if the
  2655. /// option is invalid.
  2656. virtual StringRef getCanonicalOffloadArch(StringRef Arch) = 0;
  2657. virtual std::optional<std::pair<llvm::StringRef, llvm::StringRef>>
  2658. getConflictOffloadArchCombination(const std::set<StringRef> &GpuArchs) = 0;
  2659. bool initialize() override {
  2660. assert(AssociatedOffloadKind == Action::OFK_Cuda ||
  2661. AssociatedOffloadKind == Action::OFK_HIP);
  2662. // We don't need to support CUDA.
  2663. if (AssociatedOffloadKind == Action::OFK_Cuda &&
  2664. !C.hasOffloadToolChain<Action::OFK_Cuda>())
  2665. return false;
  2666. // We don't need to support HIP.
  2667. if (AssociatedOffloadKind == Action::OFK_HIP &&
  2668. !C.hasOffloadToolChain<Action::OFK_HIP>())
  2669. return false;
  2670. Relocatable = Args.hasFlag(options::OPT_fgpu_rdc,
  2671. options::OPT_fno_gpu_rdc, /*Default=*/false);
  2672. const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>();
  2673. assert(HostTC && "No toolchain for host compilation.");
  2674. if (HostTC->getTriple().isNVPTX() ||
  2675. HostTC->getTriple().getArch() == llvm::Triple::amdgcn) {
  2676. // We do not support targeting NVPTX/AMDGCN for host compilation. Throw
  2677. // an error and abort pipeline construction early so we don't trip
  2678. // asserts that assume device-side compilation.
  2679. C.getDriver().Diag(diag::err_drv_cuda_host_arch)
  2680. << HostTC->getTriple().getArchName();
  2681. return true;
  2682. }
  2683. ToolChains.push_back(
  2684. AssociatedOffloadKind == Action::OFK_Cuda
  2685. ? C.getSingleOffloadToolChain<Action::OFK_Cuda>()
  2686. : C.getSingleOffloadToolChain<Action::OFK_HIP>());
  2687. CompileHostOnly = C.getDriver().offloadHostOnly();
  2688. CompileDeviceOnly = C.getDriver().offloadDeviceOnly();
  2689. EmitLLVM = Args.getLastArg(options::OPT_emit_llvm);
  2690. EmitAsm = Args.getLastArg(options::OPT_S);
  2691. FixedCUID = Args.getLastArgValue(options::OPT_cuid_EQ);
  2692. if (Arg *A = Args.getLastArg(options::OPT_fuse_cuid_EQ)) {
  2693. StringRef UseCUIDStr = A->getValue();
  2694. UseCUID = llvm::StringSwitch<UseCUIDKind>(UseCUIDStr)
  2695. .Case("hash", CUID_Hash)
  2696. .Case("random", CUID_Random)
  2697. .Case("none", CUID_None)
  2698. .Default(CUID_Invalid);
  2699. if (UseCUID == CUID_Invalid) {
  2700. C.getDriver().Diag(diag::err_drv_invalid_value)
  2701. << A->getAsString(Args) << UseCUIDStr;
  2702. C.setContainsError();
  2703. return true;
  2704. }
  2705. }
  2706. // --offload and --offload-arch options are mutually exclusive.
  2707. if (Args.hasArgNoClaim(options::OPT_offload_EQ) &&
  2708. Args.hasArgNoClaim(options::OPT_offload_arch_EQ,
  2709. options::OPT_no_offload_arch_EQ)) {
  2710. C.getDriver().Diag(diag::err_opt_not_valid_with_opt) << "--offload-arch"
  2711. << "--offload";
  2712. }
  2713. // Collect all offload arch parameters, removing duplicates.
  2714. std::set<StringRef> GpuArchs;
  2715. bool Error = false;
  2716. for (Arg *A : Args) {
  2717. if (!(A->getOption().matches(options::OPT_offload_arch_EQ) ||
  2718. A->getOption().matches(options::OPT_no_offload_arch_EQ)))
  2719. continue;
  2720. A->claim();
  2721. for (StringRef ArchStr : llvm::split(A->getValue(), ",")) {
  2722. if (A->getOption().matches(options::OPT_no_offload_arch_EQ) &&
  2723. ArchStr == "all") {
  2724. GpuArchs.clear();
  2725. } else if (ArchStr == "native") {
  2726. const ToolChain &TC = *ToolChains.front();
  2727. auto GPUsOrErr = ToolChains.front()->getSystemGPUArchs(Args);
  2728. if (!GPUsOrErr) {
  2729. TC.getDriver().Diag(diag::err_drv_undetermined_gpu_arch)
  2730. << llvm::Triple::getArchTypeName(TC.getArch())
  2731. << llvm::toString(GPUsOrErr.takeError()) << "--offload-arch";
  2732. continue;
  2733. }
  2734. for (auto GPU : *GPUsOrErr) {
  2735. GpuArchs.insert(Args.MakeArgString(GPU));
  2736. }
  2737. } else {
  2738. ArchStr = getCanonicalOffloadArch(ArchStr);
  2739. if (ArchStr.empty()) {
  2740. Error = true;
  2741. } else if (A->getOption().matches(options::OPT_offload_arch_EQ))
  2742. GpuArchs.insert(ArchStr);
  2743. else if (A->getOption().matches(options::OPT_no_offload_arch_EQ))
  2744. GpuArchs.erase(ArchStr);
  2745. else
  2746. llvm_unreachable("Unexpected option.");
  2747. }
  2748. }
  2749. }
  2750. auto &&ConflictingArchs = getConflictOffloadArchCombination(GpuArchs);
  2751. if (ConflictingArchs) {
  2752. C.getDriver().Diag(clang::diag::err_drv_bad_offload_arch_combo)
  2753. << ConflictingArchs->first << ConflictingArchs->second;
  2754. C.setContainsError();
  2755. return true;
  2756. }
  2757. // Collect list of GPUs remaining in the set.
  2758. for (auto Arch : GpuArchs)
  2759. GpuArchList.push_back(Arch.data());
  2760. // Default to sm_20 which is the lowest common denominator for
  2761. // supported GPUs. sm_20 code should work correctly, if
  2762. // suboptimally, on all newer GPUs.
  2763. if (GpuArchList.empty()) {
  2764. if (ToolChains.front()->getTriple().isSPIRV())
  2765. GpuArchList.push_back(CudaArch::Generic);
  2766. else
  2767. GpuArchList.push_back(DefaultCudaArch);
  2768. }
  2769. return Error;
  2770. }
  2771. };
  2772. /// \brief CUDA action builder. It injects device code in the host backend
  2773. /// action.
  2774. class CudaActionBuilder final : public CudaActionBuilderBase {
  2775. public:
  2776. CudaActionBuilder(Compilation &C, DerivedArgList &Args,
  2777. const Driver::InputList &Inputs)
  2778. : CudaActionBuilderBase(C, Args, Inputs, Action::OFK_Cuda) {
  2779. DefaultCudaArch = CudaArch::SM_35;
  2780. }
  2781. StringRef getCanonicalOffloadArch(StringRef ArchStr) override {
  2782. CudaArch Arch = StringToCudaArch(ArchStr);
  2783. if (Arch == CudaArch::UNKNOWN || !IsNVIDIAGpuArch(Arch)) {
  2784. C.getDriver().Diag(clang::diag::err_drv_cuda_bad_gpu_arch) << ArchStr;
  2785. return StringRef();
  2786. }
  2787. return CudaArchToString(Arch);
  2788. }
  2789. std::optional<std::pair<llvm::StringRef, llvm::StringRef>>
  2790. getConflictOffloadArchCombination(
  2791. const std::set<StringRef> &GpuArchs) override {
  2792. return std::nullopt;
  2793. }
  2794. ActionBuilderReturnCode
  2795. getDeviceDependences(OffloadAction::DeviceDependences &DA,
  2796. phases::ID CurPhase, phases::ID FinalPhase,
  2797. PhasesTy &Phases) override {
  2798. if (!IsActive)
  2799. return ABRT_Inactive;
  2800. // If we don't have more CUDA actions, we don't have any dependences to
  2801. // create for the host.
  2802. if (CudaDeviceActions.empty())
  2803. return ABRT_Success;
  2804. assert(CudaDeviceActions.size() == GpuArchList.size() &&
  2805. "Expecting one action per GPU architecture.");
  2806. assert(!CompileHostOnly &&
  2807. "Not expecting CUDA actions in host-only compilation.");
  2808. // If we are generating code for the device or we are in a backend phase,
  2809. // we attempt to generate the fat binary. We compile each arch to ptx and
  2810. // assemble to cubin, then feed the cubin *and* the ptx into a device
  2811. // "link" action, which uses fatbinary to combine these cubins into one
  2812. // fatbin. The fatbin is then an input to the host action if not in
  2813. // device-only mode.
  2814. if (CompileDeviceOnly || CurPhase == phases::Backend) {
  2815. ActionList DeviceActions;
  2816. for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) {
  2817. // Produce the device action from the current phase up to the assemble
  2818. // phase.
  2819. for (auto Ph : Phases) {
  2820. // Skip the phases that were already dealt with.
  2821. if (Ph < CurPhase)
  2822. continue;
  2823. // We have to be consistent with the host final phase.
  2824. if (Ph > FinalPhase)
  2825. break;
  2826. CudaDeviceActions[I] = C.getDriver().ConstructPhaseAction(
  2827. C, Args, Ph, CudaDeviceActions[I], Action::OFK_Cuda);
  2828. if (Ph == phases::Assemble)
  2829. break;
  2830. }
  2831. // If we didn't reach the assemble phase, we can't generate the fat
  2832. // binary. We don't need to generate the fat binary if we are not in
  2833. // device-only mode.
  2834. if (!isa<AssembleJobAction>(CudaDeviceActions[I]) ||
  2835. CompileDeviceOnly)
  2836. continue;
  2837. Action *AssembleAction = CudaDeviceActions[I];
  2838. assert(AssembleAction->getType() == types::TY_Object);
  2839. assert(AssembleAction->getInputs().size() == 1);
  2840. Action *BackendAction = AssembleAction->getInputs()[0];
  2841. assert(BackendAction->getType() == types::TY_PP_Asm);
  2842. for (auto &A : {AssembleAction, BackendAction}) {
  2843. OffloadAction::DeviceDependences DDep;
  2844. DDep.add(*A, *ToolChains.front(), GpuArchList[I], Action::OFK_Cuda);
  2845. DeviceActions.push_back(
  2846. C.MakeAction<OffloadAction>(DDep, A->getType()));
  2847. }
  2848. }
  2849. // We generate the fat binary if we have device input actions.
  2850. if (!DeviceActions.empty()) {
  2851. CudaFatBinary =
  2852. C.MakeAction<LinkJobAction>(DeviceActions, types::TY_CUDA_FATBIN);
  2853. if (!CompileDeviceOnly) {
  2854. DA.add(*CudaFatBinary, *ToolChains.front(), /*BoundArch=*/nullptr,
  2855. Action::OFK_Cuda);
  2856. // Clear the fat binary, it is already a dependence to an host
  2857. // action.
  2858. CudaFatBinary = nullptr;
  2859. }
  2860. // Remove the CUDA actions as they are already connected to an host
  2861. // action or fat binary.
  2862. CudaDeviceActions.clear();
  2863. }
  2864. // We avoid creating host action in device-only mode.
  2865. return CompileDeviceOnly ? ABRT_Ignore_Host : ABRT_Success;
  2866. } else if (CurPhase > phases::Backend) {
  2867. // If we are past the backend phase and still have a device action, we
  2868. // don't have to do anything as this action is already a device
  2869. // top-level action.
  2870. return ABRT_Success;
  2871. }
  2872. assert(CurPhase < phases::Backend && "Generating single CUDA "
  2873. "instructions should only occur "
  2874. "before the backend phase!");
  2875. // By default, we produce an action for each device arch.
  2876. for (Action *&A : CudaDeviceActions)
  2877. A = C.getDriver().ConstructPhaseAction(C, Args, CurPhase, A);
  2878. return ABRT_Success;
  2879. }
  2880. };
  2881. /// \brief HIP action builder. It injects device code in the host backend
  2882. /// action.
  2883. class HIPActionBuilder final : public CudaActionBuilderBase {
  2884. /// The linker inputs obtained for each device arch.
  2885. SmallVector<ActionList, 8> DeviceLinkerInputs;
  2886. // The default bundling behavior depends on the type of output, therefore
  2887. // BundleOutput needs to be tri-value: None, true, or false.
  2888. // Bundle code objects except --no-gpu-output is specified for device
  2889. // only compilation. Bundle other type of output files only if
  2890. // --gpu-bundle-output is specified for device only compilation.
  2891. std::optional<bool> BundleOutput;
  2892. public:
  2893. HIPActionBuilder(Compilation &C, DerivedArgList &Args,
  2894. const Driver::InputList &Inputs)
  2895. : CudaActionBuilderBase(C, Args, Inputs, Action::OFK_HIP) {
  2896. DefaultCudaArch = CudaArch::GFX906;
  2897. if (Args.hasArg(options::OPT_gpu_bundle_output,
  2898. options::OPT_no_gpu_bundle_output))
  2899. BundleOutput = Args.hasFlag(options::OPT_gpu_bundle_output,
  2900. options::OPT_no_gpu_bundle_output, true);
  2901. }
  2902. bool canUseBundlerUnbundler() const override { return true; }
  2903. StringRef getCanonicalOffloadArch(StringRef IdStr) override {
  2904. llvm::StringMap<bool> Features;
  2905. // getHIPOffloadTargetTriple() is known to return valid value as it has
  2906. // been called successfully in the CreateOffloadingDeviceToolChains().
  2907. auto ArchStr = parseTargetID(
  2908. *getHIPOffloadTargetTriple(C.getDriver(), C.getInputArgs()), IdStr,
  2909. &Features);
  2910. if (!ArchStr) {
  2911. C.getDriver().Diag(clang::diag::err_drv_bad_target_id) << IdStr;
  2912. C.setContainsError();
  2913. return StringRef();
  2914. }
  2915. auto CanId = getCanonicalTargetID(*ArchStr, Features);
  2916. return Args.MakeArgStringRef(CanId);
  2917. };
  2918. std::optional<std::pair<llvm::StringRef, llvm::StringRef>>
  2919. getConflictOffloadArchCombination(
  2920. const std::set<StringRef> &GpuArchs) override {
  2921. return getConflictTargetIDCombination(GpuArchs);
  2922. }
  2923. ActionBuilderReturnCode
  2924. getDeviceDependences(OffloadAction::DeviceDependences &DA,
  2925. phases::ID CurPhase, phases::ID FinalPhase,
  2926. PhasesTy &Phases) override {
  2927. if (!IsActive)
  2928. return ABRT_Inactive;
  2929. // amdgcn does not support linking of object files, therefore we skip
  2930. // backend and assemble phases to output LLVM IR. Except for generating
  2931. // non-relocatable device code, where we generate fat binary for device
  2932. // code and pass to host in Backend phase.
  2933. if (CudaDeviceActions.empty())
  2934. return ABRT_Success;
  2935. assert(((CurPhase == phases::Link && Relocatable) ||
  2936. CudaDeviceActions.size() == GpuArchList.size()) &&
  2937. "Expecting one action per GPU architecture.");
  2938. assert(!CompileHostOnly &&
  2939. "Not expecting HIP actions in host-only compilation.");
  2940. if (!Relocatable && CurPhase == phases::Backend && !EmitLLVM &&
  2941. !EmitAsm) {
  2942. // If we are in backend phase, we attempt to generate the fat binary.
  2943. // We compile each arch to IR and use a link action to generate code
  2944. // object containing ISA. Then we use a special "link" action to create
  2945. // a fat binary containing all the code objects for different GPU's.
  2946. // The fat binary is then an input to the host action.
  2947. for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) {
  2948. if (C.getDriver().isUsingLTO(/*IsOffload=*/true)) {
  2949. // When LTO is enabled, skip the backend and assemble phases and
  2950. // use lld to link the bitcode.
  2951. ActionList AL;
  2952. AL.push_back(CudaDeviceActions[I]);
  2953. // Create a link action to link device IR with device library
  2954. // and generate ISA.
  2955. CudaDeviceActions[I] =
  2956. C.MakeAction<LinkJobAction>(AL, types::TY_Image);
  2957. } else {
  2958. // When LTO is not enabled, we follow the conventional
  2959. // compiler phases, including backend and assemble phases.
  2960. ActionList AL;
  2961. Action *BackendAction = nullptr;
  2962. if (ToolChains.front()->getTriple().isSPIRV()) {
  2963. // Emit LLVM bitcode for SPIR-V targets. SPIR-V device tool chain
  2964. // (HIPSPVToolChain) runs post-link LLVM IR passes.
  2965. types::ID Output = Args.hasArg(options::OPT_S)
  2966. ? types::TY_LLVM_IR
  2967. : types::TY_LLVM_BC;
  2968. BackendAction =
  2969. C.MakeAction<BackendJobAction>(CudaDeviceActions[I], Output);
  2970. } else
  2971. BackendAction = C.getDriver().ConstructPhaseAction(
  2972. C, Args, phases::Backend, CudaDeviceActions[I],
  2973. AssociatedOffloadKind);
  2974. auto AssembleAction = C.getDriver().ConstructPhaseAction(
  2975. C, Args, phases::Assemble, BackendAction,
  2976. AssociatedOffloadKind);
  2977. AL.push_back(AssembleAction);
  2978. // Create a link action to link device IR with device library
  2979. // and generate ISA.
  2980. CudaDeviceActions[I] =
  2981. C.MakeAction<LinkJobAction>(AL, types::TY_Image);
  2982. }
  2983. // OffloadingActionBuilder propagates device arch until an offload
  2984. // action. Since the next action for creating fatbin does
  2985. // not have device arch, whereas the above link action and its input
  2986. // have device arch, an offload action is needed to stop the null
  2987. // device arch of the next action being propagated to the above link
  2988. // action.
  2989. OffloadAction::DeviceDependences DDep;
  2990. DDep.add(*CudaDeviceActions[I], *ToolChains.front(), GpuArchList[I],
  2991. AssociatedOffloadKind);
  2992. CudaDeviceActions[I] = C.MakeAction<OffloadAction>(
  2993. DDep, CudaDeviceActions[I]->getType());
  2994. }
  2995. if (!CompileDeviceOnly || !BundleOutput || *BundleOutput) {
  2996. // Create HIP fat binary with a special "link" action.
  2997. CudaFatBinary = C.MakeAction<LinkJobAction>(CudaDeviceActions,
  2998. types::TY_HIP_FATBIN);
  2999. if (!CompileDeviceOnly) {
  3000. DA.add(*CudaFatBinary, *ToolChains.front(), /*BoundArch=*/nullptr,
  3001. AssociatedOffloadKind);
  3002. // Clear the fat binary, it is already a dependence to an host
  3003. // action.
  3004. CudaFatBinary = nullptr;
  3005. }
  3006. // Remove the CUDA actions as they are already connected to an host
  3007. // action or fat binary.
  3008. CudaDeviceActions.clear();
  3009. }
  3010. return CompileDeviceOnly ? ABRT_Ignore_Host : ABRT_Success;
  3011. } else if (CurPhase == phases::Link) {
  3012. // Save CudaDeviceActions to DeviceLinkerInputs for each GPU subarch.
  3013. // This happens to each device action originated from each input file.
  3014. // Later on, device actions in DeviceLinkerInputs are used to create
  3015. // device link actions in appendLinkDependences and the created device
  3016. // link actions are passed to the offload action as device dependence.
  3017. DeviceLinkerInputs.resize(CudaDeviceActions.size());
  3018. auto LI = DeviceLinkerInputs.begin();
  3019. for (auto *A : CudaDeviceActions) {
  3020. LI->push_back(A);
  3021. ++LI;
  3022. }
  3023. // We will pass the device action as a host dependence, so we don't
  3024. // need to do anything else with them.
  3025. CudaDeviceActions.clear();
  3026. return CompileDeviceOnly ? ABRT_Ignore_Host : ABRT_Success;
  3027. }
  3028. // By default, we produce an action for each device arch.
  3029. for (Action *&A : CudaDeviceActions)
  3030. A = C.getDriver().ConstructPhaseAction(C, Args, CurPhase, A,
  3031. AssociatedOffloadKind);
  3032. if (CompileDeviceOnly && CurPhase == FinalPhase && BundleOutput &&
  3033. *BundleOutput) {
  3034. for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) {
  3035. OffloadAction::DeviceDependences DDep;
  3036. DDep.add(*CudaDeviceActions[I], *ToolChains.front(), GpuArchList[I],
  3037. AssociatedOffloadKind);
  3038. CudaDeviceActions[I] = C.MakeAction<OffloadAction>(
  3039. DDep, CudaDeviceActions[I]->getType());
  3040. }
  3041. CudaFatBinary =
  3042. C.MakeAction<OffloadBundlingJobAction>(CudaDeviceActions);
  3043. CudaDeviceActions.clear();
  3044. }
  3045. return (CompileDeviceOnly && CurPhase == FinalPhase) ? ABRT_Ignore_Host
  3046. : ABRT_Success;
  3047. }
  3048. void appendLinkDeviceActions(ActionList &AL) override {
  3049. if (DeviceLinkerInputs.size() == 0)
  3050. return;
  3051. assert(DeviceLinkerInputs.size() == GpuArchList.size() &&
  3052. "Linker inputs and GPU arch list sizes do not match.");
  3053. ActionList Actions;
  3054. unsigned I = 0;
  3055. // Append a new link action for each device.
  3056. // Each entry in DeviceLinkerInputs corresponds to a GPU arch.
  3057. for (auto &LI : DeviceLinkerInputs) {
  3058. types::ID Output = Args.hasArg(options::OPT_emit_llvm)
  3059. ? types::TY_LLVM_BC
  3060. : types::TY_Image;
  3061. auto *DeviceLinkAction = C.MakeAction<LinkJobAction>(LI, Output);
  3062. // Linking all inputs for the current GPU arch.
  3063. // LI contains all the inputs for the linker.
  3064. OffloadAction::DeviceDependences DeviceLinkDeps;
  3065. DeviceLinkDeps.add(*DeviceLinkAction, *ToolChains[0],
  3066. GpuArchList[I], AssociatedOffloadKind);
  3067. Actions.push_back(C.MakeAction<OffloadAction>(
  3068. DeviceLinkDeps, DeviceLinkAction->getType()));
  3069. ++I;
  3070. }
  3071. DeviceLinkerInputs.clear();
  3072. // If emitting LLVM, do not generate final host/device compilation action
  3073. if (Args.hasArg(options::OPT_emit_llvm)) {
  3074. AL.append(Actions);
  3075. return;
  3076. }
  3077. // Create a host object from all the device images by embedding them
  3078. // in a fat binary for mixed host-device compilation. For device-only
  3079. // compilation, creates a fat binary.
  3080. OffloadAction::DeviceDependences DDeps;
  3081. if (!CompileDeviceOnly || !BundleOutput || *BundleOutput) {
  3082. auto *TopDeviceLinkAction = C.MakeAction<LinkJobAction>(
  3083. Actions,
  3084. CompileDeviceOnly ? types::TY_HIP_FATBIN : types::TY_Object);
  3085. DDeps.add(*TopDeviceLinkAction, *ToolChains[0], nullptr,
  3086. AssociatedOffloadKind);
  3087. // Offload the host object to the host linker.
  3088. AL.push_back(
  3089. C.MakeAction<OffloadAction>(DDeps, TopDeviceLinkAction->getType()));
  3090. } else {
  3091. AL.append(Actions);
  3092. }
  3093. }
  3094. Action* appendLinkHostActions(ActionList &AL) override { return AL.back(); }
  3095. void appendLinkDependences(OffloadAction::DeviceDependences &DA) override {}
  3096. };
  3097. ///
  3098. /// TODO: Add the implementation for other specialized builders here.
  3099. ///
  3100. /// Specialized builders being used by this offloading action builder.
  3101. SmallVector<DeviceActionBuilder *, 4> SpecializedBuilders;
  3102. /// Flag set to true if all valid builders allow file bundling/unbundling.
  3103. bool CanUseBundler;
  3104. public:
  3105. OffloadingActionBuilder(Compilation &C, DerivedArgList &Args,
  3106. const Driver::InputList &Inputs)
  3107. : C(C) {
  3108. // Create a specialized builder for each device toolchain.
  3109. IsValid = true;
  3110. // Create a specialized builder for CUDA.
  3111. SpecializedBuilders.push_back(new CudaActionBuilder(C, Args, Inputs));
  3112. // Create a specialized builder for HIP.
  3113. SpecializedBuilders.push_back(new HIPActionBuilder(C, Args, Inputs));
  3114. //
  3115. // TODO: Build other specialized builders here.
  3116. //
  3117. // Initialize all the builders, keeping track of errors. If all valid
  3118. // builders agree that we can use bundling, set the flag to true.
  3119. unsigned ValidBuilders = 0u;
  3120. unsigned ValidBuildersSupportingBundling = 0u;
  3121. for (auto *SB : SpecializedBuilders) {
  3122. IsValid = IsValid && !SB->initialize();
  3123. // Update the counters if the builder is valid.
  3124. if (SB->isValid()) {
  3125. ++ValidBuilders;
  3126. if (SB->canUseBundlerUnbundler())
  3127. ++ValidBuildersSupportingBundling;
  3128. }
  3129. }
  3130. CanUseBundler =
  3131. ValidBuilders && ValidBuilders == ValidBuildersSupportingBundling;
  3132. }
  3133. ~OffloadingActionBuilder() {
  3134. for (auto *SB : SpecializedBuilders)
  3135. delete SB;
  3136. }
  3137. /// Record a host action and its originating input argument.
  3138. void recordHostAction(Action *HostAction, const Arg *InputArg) {
  3139. assert(HostAction && "Invalid host action");
  3140. assert(InputArg && "Invalid input argument");
  3141. auto Loc = HostActionToInputArgMap.find(HostAction);
  3142. if (Loc == HostActionToInputArgMap.end())
  3143. HostActionToInputArgMap[HostAction] = InputArg;
  3144. assert(HostActionToInputArgMap[HostAction] == InputArg &&
  3145. "host action mapped to multiple input arguments");
  3146. }
  3147. /// Generate an action that adds device dependences (if any) to a host action.
  3148. /// If no device dependence actions exist, just return the host action \a
  3149. /// HostAction. If an error is found or if no builder requires the host action
  3150. /// to be generated, return nullptr.
  3151. Action *
  3152. addDeviceDependencesToHostAction(Action *HostAction, const Arg *InputArg,
  3153. phases::ID CurPhase, phases::ID FinalPhase,
  3154. DeviceActionBuilder::PhasesTy &Phases) {
  3155. if (!IsValid)
  3156. return nullptr;
  3157. if (SpecializedBuilders.empty())
  3158. return HostAction;
  3159. assert(HostAction && "Invalid host action!");
  3160. recordHostAction(HostAction, InputArg);
  3161. OffloadAction::DeviceDependences DDeps;
  3162. // Check if all the programming models agree we should not emit the host
  3163. // action. Also, keep track of the offloading kinds employed.
  3164. auto &OffloadKind = InputArgToOffloadKindMap[InputArg];
  3165. unsigned InactiveBuilders = 0u;
  3166. unsigned IgnoringBuilders = 0u;
  3167. for (auto *SB : SpecializedBuilders) {
  3168. if (!SB->isValid()) {
  3169. ++InactiveBuilders;
  3170. continue;
  3171. }
  3172. auto RetCode =
  3173. SB->getDeviceDependences(DDeps, CurPhase, FinalPhase, Phases);
  3174. // If the builder explicitly says the host action should be ignored,
  3175. // we need to increment the variable that tracks the builders that request
  3176. // the host object to be ignored.
  3177. if (RetCode == DeviceActionBuilder::ABRT_Ignore_Host)
  3178. ++IgnoringBuilders;
  3179. // Unless the builder was inactive for this action, we have to record the
  3180. // offload kind because the host will have to use it.
  3181. if (RetCode != DeviceActionBuilder::ABRT_Inactive)
  3182. OffloadKind |= SB->getAssociatedOffloadKind();
  3183. }
  3184. // If all builders agree that the host object should be ignored, just return
  3185. // nullptr.
  3186. if (IgnoringBuilders &&
  3187. SpecializedBuilders.size() == (InactiveBuilders + IgnoringBuilders))
  3188. return nullptr;
  3189. if (DDeps.getActions().empty())
  3190. return HostAction;
  3191. // We have dependences we need to bundle together. We use an offload action
  3192. // for that.
  3193. OffloadAction::HostDependence HDep(
  3194. *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(),
  3195. /*BoundArch=*/nullptr, DDeps);
  3196. return C.MakeAction<OffloadAction>(HDep, DDeps);
  3197. }
  3198. /// Generate an action that adds a host dependence to a device action. The
  3199. /// results will be kept in this action builder. Return true if an error was
  3200. /// found.
  3201. bool addHostDependenceToDeviceActions(Action *&HostAction,
  3202. const Arg *InputArg) {
  3203. if (!IsValid)
  3204. return true;
  3205. recordHostAction(HostAction, InputArg);
  3206. // If we are supporting bundling/unbundling and the current action is an
  3207. // input action of non-source file, we replace the host action by the
  3208. // unbundling action. The bundler tool has the logic to detect if an input
  3209. // is a bundle or not and if the input is not a bundle it assumes it is a
  3210. // host file. Therefore it is safe to create an unbundling action even if
  3211. // the input is not a bundle.
  3212. if (CanUseBundler && isa<InputAction>(HostAction) &&
  3213. InputArg->getOption().getKind() == llvm::opt::Option::InputClass &&
  3214. (!types::isSrcFile(HostAction->getType()) ||
  3215. HostAction->getType() == types::TY_PP_HIP)) {
  3216. auto UnbundlingHostAction =
  3217. C.MakeAction<OffloadUnbundlingJobAction>(HostAction);
  3218. UnbundlingHostAction->registerDependentActionInfo(
  3219. C.getSingleOffloadToolChain<Action::OFK_Host>(),
  3220. /*BoundArch=*/StringRef(), Action::OFK_Host);
  3221. HostAction = UnbundlingHostAction;
  3222. recordHostAction(HostAction, InputArg);
  3223. }
  3224. assert(HostAction && "Invalid host action!");
  3225. // Register the offload kinds that are used.
  3226. auto &OffloadKind = InputArgToOffloadKindMap[InputArg];
  3227. for (auto *SB : SpecializedBuilders) {
  3228. if (!SB->isValid())
  3229. continue;
  3230. auto RetCode = SB->addDeviceDependences(HostAction);
  3231. // Host dependences for device actions are not compatible with that same
  3232. // action being ignored.
  3233. assert(RetCode != DeviceActionBuilder::ABRT_Ignore_Host &&
  3234. "Host dependence not expected to be ignored.!");
  3235. // Unless the builder was inactive for this action, we have to record the
  3236. // offload kind because the host will have to use it.
  3237. if (RetCode != DeviceActionBuilder::ABRT_Inactive)
  3238. OffloadKind |= SB->getAssociatedOffloadKind();
  3239. }
  3240. // Do not use unbundler if the Host does not depend on device action.
  3241. if (OffloadKind == Action::OFK_None && CanUseBundler)
  3242. if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(HostAction))
  3243. HostAction = UA->getInputs().back();
  3244. return false;
  3245. }
  3246. /// Add the offloading top level actions to the provided action list. This
  3247. /// function can replace the host action by a bundling action if the
  3248. /// programming models allow it.
  3249. bool appendTopLevelActions(ActionList &AL, Action *HostAction,
  3250. const Arg *InputArg) {
  3251. if (HostAction)
  3252. recordHostAction(HostAction, InputArg);
  3253. // Get the device actions to be appended.
  3254. ActionList OffloadAL;
  3255. for (auto *SB : SpecializedBuilders) {
  3256. if (!SB->isValid())
  3257. continue;
  3258. SB->appendTopLevelActions(OffloadAL);
  3259. }
  3260. // If we can use the bundler, replace the host action by the bundling one in
  3261. // the resulting list. Otherwise, just append the device actions. For
  3262. // device only compilation, HostAction is a null pointer, therefore only do
  3263. // this when HostAction is not a null pointer.
  3264. if (CanUseBundler && HostAction &&
  3265. HostAction->getType() != types::TY_Nothing && !OffloadAL.empty()) {
  3266. // Add the host action to the list in order to create the bundling action.
  3267. OffloadAL.push_back(HostAction);
  3268. // We expect that the host action was just appended to the action list
  3269. // before this method was called.
  3270. assert(HostAction == AL.back() && "Host action not in the list??");
  3271. HostAction = C.MakeAction<OffloadBundlingJobAction>(OffloadAL);
  3272. recordHostAction(HostAction, InputArg);
  3273. AL.back() = HostAction;
  3274. } else
  3275. AL.append(OffloadAL.begin(), OffloadAL.end());
  3276. // Propagate to the current host action (if any) the offload information
  3277. // associated with the current input.
  3278. if (HostAction)
  3279. HostAction->propagateHostOffloadInfo(InputArgToOffloadKindMap[InputArg],
  3280. /*BoundArch=*/nullptr);
  3281. return false;
  3282. }
  3283. void appendDeviceLinkActions(ActionList &AL) {
  3284. for (DeviceActionBuilder *SB : SpecializedBuilders) {
  3285. if (!SB->isValid())
  3286. continue;
  3287. SB->appendLinkDeviceActions(AL);
  3288. }
  3289. }
  3290. Action *makeHostLinkAction() {
  3291. // Build a list of device linking actions.
  3292. ActionList DeviceAL;
  3293. appendDeviceLinkActions(DeviceAL);
  3294. if (DeviceAL.empty())
  3295. return nullptr;
  3296. // Let builders add host linking actions.
  3297. Action* HA = nullptr;
  3298. for (DeviceActionBuilder *SB : SpecializedBuilders) {
  3299. if (!SB->isValid())
  3300. continue;
  3301. HA = SB->appendLinkHostActions(DeviceAL);
  3302. // This created host action has no originating input argument, therefore
  3303. // needs to set its offloading kind directly.
  3304. if (HA)
  3305. HA->propagateHostOffloadInfo(SB->getAssociatedOffloadKind(),
  3306. /*BoundArch=*/nullptr);
  3307. }
  3308. return HA;
  3309. }
  3310. /// Processes the host linker action. This currently consists of replacing it
  3311. /// with an offload action if there are device link objects and propagate to
  3312. /// the host action all the offload kinds used in the current compilation. The
  3313. /// resulting action is returned.
  3314. Action *processHostLinkAction(Action *HostAction) {
  3315. // Add all the dependences from the device linking actions.
  3316. OffloadAction::DeviceDependences DDeps;
  3317. for (auto *SB : SpecializedBuilders) {
  3318. if (!SB->isValid())
  3319. continue;
  3320. SB->appendLinkDependences(DDeps);
  3321. }
  3322. // Calculate all the offload kinds used in the current compilation.
  3323. unsigned ActiveOffloadKinds = 0u;
  3324. for (auto &I : InputArgToOffloadKindMap)
  3325. ActiveOffloadKinds |= I.second;
  3326. // If we don't have device dependencies, we don't have to create an offload
  3327. // action.
  3328. if (DDeps.getActions().empty()) {
  3329. // Set all the active offloading kinds to the link action. Given that it
  3330. // is a link action it is assumed to depend on all actions generated so
  3331. // far.
  3332. HostAction->setHostOffloadInfo(ActiveOffloadKinds,
  3333. /*BoundArch=*/nullptr);
  3334. // Propagate active offloading kinds for each input to the link action.
  3335. // Each input may have different active offloading kind.
  3336. for (auto *A : HostAction->inputs()) {
  3337. auto ArgLoc = HostActionToInputArgMap.find(A);
  3338. if (ArgLoc == HostActionToInputArgMap.end())
  3339. continue;
  3340. auto OFKLoc = InputArgToOffloadKindMap.find(ArgLoc->second);
  3341. if (OFKLoc == InputArgToOffloadKindMap.end())
  3342. continue;
  3343. A->propagateHostOffloadInfo(OFKLoc->second, /*BoundArch=*/nullptr);
  3344. }
  3345. return HostAction;
  3346. }
  3347. // Create the offload action with all dependences. When an offload action
  3348. // is created the kinds are propagated to the host action, so we don't have
  3349. // to do that explicitly here.
  3350. OffloadAction::HostDependence HDep(
  3351. *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(),
  3352. /*BoundArch*/ nullptr, ActiveOffloadKinds);
  3353. return C.MakeAction<OffloadAction>(HDep, DDeps);
  3354. }
  3355. };
  3356. } // anonymous namespace.
  3357. void Driver::handleArguments(Compilation &C, DerivedArgList &Args,
  3358. const InputList &Inputs,
  3359. ActionList &Actions) const {
  3360. // Ignore /Yc/Yu if both /Yc and /Yu passed but with different filenames.
  3361. Arg *YcArg = Args.getLastArg(options::OPT__SLASH_Yc);
  3362. Arg *YuArg = Args.getLastArg(options::OPT__SLASH_Yu);
  3363. if (YcArg && YuArg && strcmp(YcArg->getValue(), YuArg->getValue()) != 0) {
  3364. Diag(clang::diag::warn_drv_ycyu_different_arg_clang_cl);
  3365. Args.eraseArg(options::OPT__SLASH_Yc);
  3366. Args.eraseArg(options::OPT__SLASH_Yu);
  3367. YcArg = YuArg = nullptr;
  3368. }
  3369. if (YcArg && Inputs.size() > 1) {
  3370. Diag(clang::diag::warn_drv_yc_multiple_inputs_clang_cl);
  3371. Args.eraseArg(options::OPT__SLASH_Yc);
  3372. YcArg = nullptr;
  3373. }
  3374. Arg *FinalPhaseArg;
  3375. phases::ID FinalPhase = getFinalPhase(Args, &FinalPhaseArg);
  3376. if (FinalPhase == phases::Link) {
  3377. // Emitting LLVM while linking disabled except in HIPAMD Toolchain
  3378. if (Args.hasArg(options::OPT_emit_llvm) && !Args.hasArg(options::OPT_hip_link))
  3379. Diag(clang::diag::err_drv_emit_llvm_link);
  3380. if (IsCLMode() && LTOMode != LTOK_None &&
  3381. !Args.getLastArgValue(options::OPT_fuse_ld_EQ)
  3382. .equals_insensitive("lld"))
  3383. Diag(clang::diag::err_drv_lto_without_lld);
  3384. }
  3385. if (FinalPhase == phases::Preprocess || Args.hasArg(options::OPT__SLASH_Y_)) {
  3386. // If only preprocessing or /Y- is used, all pch handling is disabled.
  3387. // Rather than check for it everywhere, just remove clang-cl pch-related
  3388. // flags here.
  3389. Args.eraseArg(options::OPT__SLASH_Fp);
  3390. Args.eraseArg(options::OPT__SLASH_Yc);
  3391. Args.eraseArg(options::OPT__SLASH_Yu);
  3392. YcArg = YuArg = nullptr;
  3393. }
  3394. unsigned LastPLSize = 0;
  3395. for (auto &I : Inputs) {
  3396. types::ID InputType = I.first;
  3397. const Arg *InputArg = I.second;
  3398. auto PL = types::getCompilationPhases(InputType);
  3399. LastPLSize = PL.size();
  3400. // If the first step comes after the final phase we are doing as part of
  3401. // this compilation, warn the user about it.
  3402. phases::ID InitialPhase = PL[0];
  3403. if (InitialPhase > FinalPhase) {
  3404. if (InputArg->isClaimed())
  3405. continue;
  3406. // Claim here to avoid the more general unused warning.
  3407. InputArg->claim();
  3408. // Suppress all unused style warnings with -Qunused-arguments
  3409. if (Args.hasArg(options::OPT_Qunused_arguments))
  3410. continue;
  3411. // Special case when final phase determined by binary name, rather than
  3412. // by a command-line argument with a corresponding Arg.
  3413. if (CCCIsCPP())
  3414. Diag(clang::diag::warn_drv_input_file_unused_by_cpp)
  3415. << InputArg->getAsString(Args) << getPhaseName(InitialPhase);
  3416. // Special case '-E' warning on a previously preprocessed file to make
  3417. // more sense.
  3418. else if (InitialPhase == phases::Compile &&
  3419. (Args.getLastArg(options::OPT__SLASH_EP,
  3420. options::OPT__SLASH_P) ||
  3421. Args.getLastArg(options::OPT_E) ||
  3422. Args.getLastArg(options::OPT_M, options::OPT_MM)) &&
  3423. getPreprocessedType(InputType) == types::TY_INVALID)
  3424. Diag(clang::diag::warn_drv_preprocessed_input_file_unused)
  3425. << InputArg->getAsString(Args) << !!FinalPhaseArg
  3426. << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : "");
  3427. else
  3428. Diag(clang::diag::warn_drv_input_file_unused)
  3429. << InputArg->getAsString(Args) << getPhaseName(InitialPhase)
  3430. << !!FinalPhaseArg
  3431. << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : "");
  3432. continue;
  3433. }
  3434. if (YcArg) {
  3435. // Add a separate precompile phase for the compile phase.
  3436. if (FinalPhase >= phases::Compile) {
  3437. const types::ID HeaderType = lookupHeaderTypeForSourceType(InputType);
  3438. // Build the pipeline for the pch file.
  3439. Action *ClangClPch = C.MakeAction<InputAction>(*InputArg, HeaderType);
  3440. for (phases::ID Phase : types::getCompilationPhases(HeaderType))
  3441. ClangClPch = ConstructPhaseAction(C, Args, Phase, ClangClPch);
  3442. assert(ClangClPch);
  3443. Actions.push_back(ClangClPch);
  3444. // The driver currently exits after the first failed command. This
  3445. // relies on that behavior, to make sure if the pch generation fails,
  3446. // the main compilation won't run.
  3447. // FIXME: If the main compilation fails, the PCH generation should
  3448. // probably not be considered successful either.
  3449. }
  3450. }
  3451. }
  3452. // If we are linking, claim any options which are obviously only used for
  3453. // compilation.
  3454. // FIXME: Understand why the last Phase List length is used here.
  3455. if (FinalPhase == phases::Link && LastPLSize == 1) {
  3456. Args.ClaimAllArgs(options::OPT_CompileOnly_Group);
  3457. Args.ClaimAllArgs(options::OPT_cl_compile_Group);
  3458. }
  3459. }
  3460. void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
  3461. const InputList &Inputs, ActionList &Actions) const {
  3462. llvm::PrettyStackTraceString CrashInfo("Building compilation actions");
  3463. if (!SuppressMissingInputWarning && Inputs.empty()) {
  3464. Diag(clang::diag::err_drv_no_input_files);
  3465. return;
  3466. }
  3467. // Diagnose misuse of /Fo.
  3468. if (Arg *A = Args.getLastArg(options::OPT__SLASH_Fo)) {
  3469. StringRef V = A->getValue();
  3470. if (Inputs.size() > 1 && !V.empty() &&
  3471. !llvm::sys::path::is_separator(V.back())) {
  3472. // Check whether /Fo tries to name an output file for multiple inputs.
  3473. Diag(clang::diag::err_drv_out_file_argument_with_multiple_sources)
  3474. << A->getSpelling() << V;
  3475. Args.eraseArg(options::OPT__SLASH_Fo);
  3476. }
  3477. }
  3478. // Diagnose misuse of /Fa.
  3479. if (Arg *A = Args.getLastArg(options::OPT__SLASH_Fa)) {
  3480. StringRef V = A->getValue();
  3481. if (Inputs.size() > 1 && !V.empty() &&
  3482. !llvm::sys::path::is_separator(V.back())) {
  3483. // Check whether /Fa tries to name an asm file for multiple inputs.
  3484. Diag(clang::diag::err_drv_out_file_argument_with_multiple_sources)
  3485. << A->getSpelling() << V;
  3486. Args.eraseArg(options::OPT__SLASH_Fa);
  3487. }
  3488. }
  3489. // Diagnose misuse of /o.
  3490. if (Arg *A = Args.getLastArg(options::OPT__SLASH_o)) {
  3491. if (A->getValue()[0] == '\0') {
  3492. // It has to have a value.
  3493. Diag(clang::diag::err_drv_missing_argument) << A->getSpelling() << 1;
  3494. Args.eraseArg(options::OPT__SLASH_o);
  3495. }
  3496. }
  3497. handleArguments(C, Args, Inputs, Actions);
  3498. bool UseNewOffloadingDriver =
  3499. C.isOffloadingHostKind(Action::OFK_OpenMP) ||
  3500. Args.hasFlag(options::OPT_offload_new_driver,
  3501. options::OPT_no_offload_new_driver, false);
  3502. // Builder to be used to build offloading actions.
  3503. std::unique_ptr<OffloadingActionBuilder> OffloadBuilder =
  3504. !UseNewOffloadingDriver
  3505. ? std::make_unique<OffloadingActionBuilder>(C, Args, Inputs)
  3506. : nullptr;
  3507. // Construct the actions to perform.
  3508. ExtractAPIJobAction *ExtractAPIAction = nullptr;
  3509. ActionList LinkerInputs;
  3510. ActionList MergerInputs;
  3511. for (auto &I : Inputs) {
  3512. types::ID InputType = I.first;
  3513. const Arg *InputArg = I.second;
  3514. auto PL = types::getCompilationPhases(*this, Args, InputType);
  3515. if (PL.empty())
  3516. continue;
  3517. auto FullPL = types::getCompilationPhases(InputType);
  3518. // Build the pipeline for this file.
  3519. Action *Current = C.MakeAction<InputAction>(*InputArg, InputType);
  3520. // Use the current host action in any of the offloading actions, if
  3521. // required.
  3522. if (!UseNewOffloadingDriver)
  3523. if (OffloadBuilder->addHostDependenceToDeviceActions(Current, InputArg))
  3524. break;
  3525. for (phases::ID Phase : PL) {
  3526. // Add any offload action the host action depends on.
  3527. if (!UseNewOffloadingDriver)
  3528. Current = OffloadBuilder->addDeviceDependencesToHostAction(
  3529. Current, InputArg, Phase, PL.back(), FullPL);
  3530. if (!Current)
  3531. break;
  3532. // Queue linker inputs.
  3533. if (Phase == phases::Link) {
  3534. assert(Phase == PL.back() && "linking must be final compilation step.");
  3535. // We don't need to generate additional link commands if emitting AMD bitcode
  3536. if (!(C.getInputArgs().hasArg(options::OPT_hip_link) &&
  3537. (C.getInputArgs().hasArg(options::OPT_emit_llvm))))
  3538. LinkerInputs.push_back(Current);
  3539. Current = nullptr;
  3540. break;
  3541. }
  3542. // TODO: Consider removing this because the merged may not end up being
  3543. // the final Phase in the pipeline. Perhaps the merged could just merge
  3544. // and then pass an artifact of some sort to the Link Phase.
  3545. // Queue merger inputs.
  3546. if (Phase == phases::IfsMerge) {
  3547. assert(Phase == PL.back() && "merging must be final compilation step.");
  3548. MergerInputs.push_back(Current);
  3549. Current = nullptr;
  3550. break;
  3551. }
  3552. if (Phase == phases::Precompile && ExtractAPIAction) {
  3553. ExtractAPIAction->addHeaderInput(Current);
  3554. Current = nullptr;
  3555. break;
  3556. }
  3557. // FIXME: Should we include any prior module file outputs as inputs of
  3558. // later actions in the same command line?
  3559. // Otherwise construct the appropriate action.
  3560. Action *NewCurrent = ConstructPhaseAction(C, Args, Phase, Current);
  3561. // We didn't create a new action, so we will just move to the next phase.
  3562. if (NewCurrent == Current)
  3563. continue;
  3564. if (auto *EAA = dyn_cast<ExtractAPIJobAction>(NewCurrent))
  3565. ExtractAPIAction = EAA;
  3566. Current = NewCurrent;
  3567. // Use the current host action in any of the offloading actions, if
  3568. // required.
  3569. if (!UseNewOffloadingDriver)
  3570. if (OffloadBuilder->addHostDependenceToDeviceActions(Current, InputArg))
  3571. break;
  3572. // Try to build the offloading actions and add the result as a dependency
  3573. // to the host.
  3574. if (UseNewOffloadingDriver)
  3575. Current = BuildOffloadingActions(C, Args, I, Current);
  3576. if (Current->getType() == types::TY_Nothing)
  3577. break;
  3578. }
  3579. // If we ended with something, add to the output list.
  3580. if (Current)
  3581. Actions.push_back(Current);
  3582. // Add any top level actions generated for offloading.
  3583. if (!UseNewOffloadingDriver)
  3584. OffloadBuilder->appendTopLevelActions(Actions, Current, InputArg);
  3585. else if (Current)
  3586. Current->propagateHostOffloadInfo(C.getActiveOffloadKinds(),
  3587. /*BoundArch=*/nullptr);
  3588. }
  3589. // Add a link action if necessary.
  3590. if (LinkerInputs.empty()) {
  3591. Arg *FinalPhaseArg;
  3592. if (getFinalPhase(Args, &FinalPhaseArg) == phases::Link)
  3593. if (!UseNewOffloadingDriver)
  3594. OffloadBuilder->appendDeviceLinkActions(Actions);
  3595. }
  3596. if (!LinkerInputs.empty()) {
  3597. if (!UseNewOffloadingDriver)
  3598. if (Action *Wrapper = OffloadBuilder->makeHostLinkAction())
  3599. LinkerInputs.push_back(Wrapper);
  3600. Action *LA;
  3601. // Check if this Linker Job should emit a static library.
  3602. if (ShouldEmitStaticLibrary(Args)) {
  3603. LA = C.MakeAction<StaticLibJobAction>(LinkerInputs, types::TY_Image);
  3604. } else if (UseNewOffloadingDriver ||
  3605. Args.hasArg(options::OPT_offload_link)) {
  3606. LA = C.MakeAction<LinkerWrapperJobAction>(LinkerInputs, types::TY_Image);
  3607. LA->propagateHostOffloadInfo(C.getActiveOffloadKinds(),
  3608. /*BoundArch=*/nullptr);
  3609. } else {
  3610. LA = C.MakeAction<LinkJobAction>(LinkerInputs, types::TY_Image);
  3611. }
  3612. if (!UseNewOffloadingDriver)
  3613. LA = OffloadBuilder->processHostLinkAction(LA);
  3614. Actions.push_back(LA);
  3615. }
  3616. // Add an interface stubs merge action if necessary.
  3617. if (!MergerInputs.empty())
  3618. Actions.push_back(
  3619. C.MakeAction<IfsMergeJobAction>(MergerInputs, types::TY_Image));
  3620. if (Args.hasArg(options::OPT_emit_interface_stubs)) {
  3621. auto PhaseList = types::getCompilationPhases(
  3622. types::TY_IFS_CPP,
  3623. Args.hasArg(options::OPT_c) ? phases::Compile : phases::IfsMerge);
  3624. ActionList MergerInputs;
  3625. for (auto &I : Inputs) {
  3626. types::ID InputType = I.first;
  3627. const Arg *InputArg = I.second;
  3628. // Currently clang and the llvm assembler do not support generating symbol
  3629. // stubs from assembly, so we skip the input on asm files. For ifs files
  3630. // we rely on the normal pipeline setup in the pipeline setup code above.
  3631. if (InputType == types::TY_IFS || InputType == types::TY_PP_Asm ||
  3632. InputType == types::TY_Asm)
  3633. continue;
  3634. Action *Current = C.MakeAction<InputAction>(*InputArg, InputType);
  3635. for (auto Phase : PhaseList) {
  3636. switch (Phase) {
  3637. default:
  3638. llvm_unreachable(
  3639. "IFS Pipeline can only consist of Compile followed by IfsMerge.");
  3640. case phases::Compile: {
  3641. // Only IfsMerge (llvm-ifs) can handle .o files by looking for ifs
  3642. // files where the .o file is located. The compile action can not
  3643. // handle this.
  3644. if (InputType == types::TY_Object)
  3645. break;
  3646. Current = C.MakeAction<CompileJobAction>(Current, types::TY_IFS_CPP);
  3647. break;
  3648. }
  3649. case phases::IfsMerge: {
  3650. assert(Phase == PhaseList.back() &&
  3651. "merging must be final compilation step.");
  3652. MergerInputs.push_back(Current);
  3653. Current = nullptr;
  3654. break;
  3655. }
  3656. }
  3657. }
  3658. // If we ended with something, add to the output list.
  3659. if (Current)
  3660. Actions.push_back(Current);
  3661. }
  3662. // Add an interface stubs merge action if necessary.
  3663. if (!MergerInputs.empty())
  3664. Actions.push_back(
  3665. C.MakeAction<IfsMergeJobAction>(MergerInputs, types::TY_Image));
  3666. }
  3667. // If --print-supported-cpus, -mcpu=? or -mtune=? is specified, build a custom
  3668. // Compile phase that prints out supported cpu models and quits.
  3669. if (Arg *A = Args.getLastArg(options::OPT_print_supported_cpus)) {
  3670. // Use the -mcpu=? flag as the dummy input to cc1.
  3671. Actions.clear();
  3672. Action *InputAc = C.MakeAction<InputAction>(*A, types::TY_C);
  3673. Actions.push_back(
  3674. C.MakeAction<PrecompileJobAction>(InputAc, types::TY_Nothing));
  3675. for (auto &I : Inputs)
  3676. I.second->claim();
  3677. }
  3678. // Claim ignored clang-cl options.
  3679. Args.ClaimAllArgs(options::OPT_cl_ignored_Group);
  3680. }
  3681. /// Returns the canonical name for the offloading architecture when using a HIP
  3682. /// or CUDA architecture.
  3683. static StringRef getCanonicalArchString(Compilation &C,
  3684. const llvm::opt::DerivedArgList &Args,
  3685. StringRef ArchStr,
  3686. const llvm::Triple &Triple,
  3687. bool SuppressError = false) {
  3688. // Lookup the CUDA / HIP architecture string. Only report an error if we were
  3689. // expecting the triple to be only NVPTX / AMDGPU.
  3690. CudaArch Arch = StringToCudaArch(getProcessorFromTargetID(Triple, ArchStr));
  3691. if (!SuppressError && Triple.isNVPTX() &&
  3692. (Arch == CudaArch::UNKNOWN || !IsNVIDIAGpuArch(Arch))) {
  3693. C.getDriver().Diag(clang::diag::err_drv_offload_bad_gpu_arch)
  3694. << "CUDA" << ArchStr;
  3695. return StringRef();
  3696. } else if (!SuppressError && Triple.isAMDGPU() &&
  3697. (Arch == CudaArch::UNKNOWN || !IsAMDGpuArch(Arch))) {
  3698. C.getDriver().Diag(clang::diag::err_drv_offload_bad_gpu_arch)
  3699. << "HIP" << ArchStr;
  3700. return StringRef();
  3701. }
  3702. if (IsNVIDIAGpuArch(Arch))
  3703. return Args.MakeArgStringRef(CudaArchToString(Arch));
  3704. if (IsAMDGpuArch(Arch)) {
  3705. llvm::StringMap<bool> Features;
  3706. auto HIPTriple = getHIPOffloadTargetTriple(C.getDriver(), C.getInputArgs());
  3707. if (!HIPTriple)
  3708. return StringRef();
  3709. auto Arch = parseTargetID(*HIPTriple, ArchStr, &Features);
  3710. if (!Arch) {
  3711. C.getDriver().Diag(clang::diag::err_drv_bad_target_id) << ArchStr;
  3712. C.setContainsError();
  3713. return StringRef();
  3714. }
  3715. return Args.MakeArgStringRef(getCanonicalTargetID(*Arch, Features));
  3716. }
  3717. // If the input isn't CUDA or HIP just return the architecture.
  3718. return ArchStr;
  3719. }
  3720. /// Checks if the set offloading architectures does not conflict. Returns the
  3721. /// incompatible pair if a conflict occurs.
  3722. static std::optional<std::pair<llvm::StringRef, llvm::StringRef>>
  3723. getConflictOffloadArchCombination(const llvm::DenseSet<StringRef> &Archs,
  3724. Action::OffloadKind Kind) {
  3725. if (Kind != Action::OFK_HIP)
  3726. return std::nullopt;
  3727. std::set<StringRef> ArchSet;
  3728. llvm::copy(Archs, std::inserter(ArchSet, ArchSet.begin()));
  3729. return getConflictTargetIDCombination(ArchSet);
  3730. }
  3731. llvm::DenseSet<StringRef>
  3732. Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args,
  3733. Action::OffloadKind Kind, const ToolChain *TC,
  3734. bool SuppressError) const {
  3735. if (!TC)
  3736. TC = &C.getDefaultToolChain();
  3737. // --offload and --offload-arch options are mutually exclusive.
  3738. if (Args.hasArgNoClaim(options::OPT_offload_EQ) &&
  3739. Args.hasArgNoClaim(options::OPT_offload_arch_EQ,
  3740. options::OPT_no_offload_arch_EQ)) {
  3741. C.getDriver().Diag(diag::err_opt_not_valid_with_opt)
  3742. << "--offload"
  3743. << (Args.hasArgNoClaim(options::OPT_offload_arch_EQ)
  3744. ? "--offload-arch"
  3745. : "--no-offload-arch");
  3746. }
  3747. if (KnownArchs.find(TC) != KnownArchs.end())
  3748. return KnownArchs.lookup(TC);
  3749. llvm::DenseSet<StringRef> Archs;
  3750. for (auto *Arg : Args) {
  3751. // Extract any '--[no-]offload-arch' arguments intended for this toolchain.
  3752. std::unique_ptr<llvm::opt::Arg> ExtractedArg = nullptr;
  3753. if (Arg->getOption().matches(options::OPT_Xopenmp_target_EQ) &&
  3754. ToolChain::getOpenMPTriple(Arg->getValue(0)) == TC->getTriple()) {
  3755. Arg->claim();
  3756. unsigned Index = Args.getBaseArgs().MakeIndex(Arg->getValue(1));
  3757. ExtractedArg = getOpts().ParseOneArg(Args, Index);
  3758. Arg = ExtractedArg.get();
  3759. }
  3760. // Add or remove the seen architectures in order of appearance. If an
  3761. // invalid architecture is given we simply exit.
  3762. if (Arg->getOption().matches(options::OPT_offload_arch_EQ)) {
  3763. for (StringRef Arch : llvm::split(Arg->getValue(), ",")) {
  3764. if (Arch == "native" || Arch.empty()) {
  3765. auto GPUsOrErr = TC->getSystemGPUArchs(Args);
  3766. if (!GPUsOrErr) {
  3767. if (SuppressError)
  3768. llvm::consumeError(GPUsOrErr.takeError());
  3769. else
  3770. TC->getDriver().Diag(diag::err_drv_undetermined_gpu_arch)
  3771. << llvm::Triple::getArchTypeName(TC->getArch())
  3772. << llvm::toString(GPUsOrErr.takeError()) << "--offload-arch";
  3773. continue;
  3774. }
  3775. for (auto ArchStr : *GPUsOrErr) {
  3776. Archs.insert(
  3777. getCanonicalArchString(C, Args, Args.MakeArgString(ArchStr),
  3778. TC->getTriple(), SuppressError));
  3779. }
  3780. } else {
  3781. StringRef ArchStr = getCanonicalArchString(
  3782. C, Args, Arch, TC->getTriple(), SuppressError);
  3783. if (ArchStr.empty())
  3784. return Archs;
  3785. Archs.insert(ArchStr);
  3786. }
  3787. }
  3788. } else if (Arg->getOption().matches(options::OPT_no_offload_arch_EQ)) {
  3789. for (StringRef Arch : llvm::split(Arg->getValue(), ",")) {
  3790. if (Arch == "all") {
  3791. Archs.clear();
  3792. } else {
  3793. StringRef ArchStr = getCanonicalArchString(
  3794. C, Args, Arch, TC->getTriple(), SuppressError);
  3795. if (ArchStr.empty())
  3796. return Archs;
  3797. Archs.erase(ArchStr);
  3798. }
  3799. }
  3800. }
  3801. }
  3802. if (auto ConflictingArchs = getConflictOffloadArchCombination(Archs, Kind)) {
  3803. C.getDriver().Diag(clang::diag::err_drv_bad_offload_arch_combo)
  3804. << ConflictingArchs->first << ConflictingArchs->second;
  3805. C.setContainsError();
  3806. }
  3807. // Skip filling defaults if we're just querying what is availible.
  3808. if (SuppressError)
  3809. return Archs;
  3810. if (Archs.empty()) {
  3811. if (Kind == Action::OFK_Cuda)
  3812. Archs.insert(CudaArchToString(CudaArch::CudaDefault));
  3813. else if (Kind == Action::OFK_HIP)
  3814. Archs.insert(CudaArchToString(CudaArch::HIPDefault));
  3815. else if (Kind == Action::OFK_OpenMP)
  3816. Archs.insert(StringRef());
  3817. } else {
  3818. Args.ClaimAllArgs(options::OPT_offload_arch_EQ);
  3819. Args.ClaimAllArgs(options::OPT_no_offload_arch_EQ);
  3820. }
  3821. return Archs;
  3822. }
  3823. Action *Driver::BuildOffloadingActions(Compilation &C,
  3824. llvm::opt::DerivedArgList &Args,
  3825. const InputTy &Input,
  3826. Action *HostAction) const {
  3827. // Don't build offloading actions if explicitly disabled or we do not have a
  3828. // valid source input and compile action to embed it in. If preprocessing only
  3829. // ignore embedding.
  3830. if (offloadHostOnly() || !types::isSrcFile(Input.first) ||
  3831. !(isa<CompileJobAction>(HostAction) ||
  3832. getFinalPhase(Args) == phases::Preprocess))
  3833. return HostAction;
  3834. ActionList OffloadActions;
  3835. OffloadAction::DeviceDependences DDeps;
  3836. const Action::OffloadKind OffloadKinds[] = {
  3837. Action::OFK_OpenMP, Action::OFK_Cuda, Action::OFK_HIP};
  3838. for (Action::OffloadKind Kind : OffloadKinds) {
  3839. SmallVector<const ToolChain *, 2> ToolChains;
  3840. ActionList DeviceActions;
  3841. auto TCRange = C.getOffloadToolChains(Kind);
  3842. for (auto TI = TCRange.first, TE = TCRange.second; TI != TE; ++TI)
  3843. ToolChains.push_back(TI->second);
  3844. if (ToolChains.empty())
  3845. continue;
  3846. types::ID InputType = Input.first;
  3847. const Arg *InputArg = Input.second;
  3848. // The toolchain can be active for unsupported file types.
  3849. if ((Kind == Action::OFK_Cuda && !types::isCuda(InputType)) ||
  3850. (Kind == Action::OFK_HIP && !types::isHIP(InputType)))
  3851. continue;
  3852. // Get the product of all bound architectures and toolchains.
  3853. SmallVector<std::pair<const ToolChain *, StringRef>> TCAndArchs;
  3854. for (const ToolChain *TC : ToolChains)
  3855. for (StringRef Arch : getOffloadArchs(C, Args, Kind, TC))
  3856. TCAndArchs.push_back(std::make_pair(TC, Arch));
  3857. for (unsigned I = 0, E = TCAndArchs.size(); I != E; ++I)
  3858. DeviceActions.push_back(C.MakeAction<InputAction>(*InputArg, InputType));
  3859. if (DeviceActions.empty())
  3860. return HostAction;
  3861. auto PL = types::getCompilationPhases(*this, Args, InputType);
  3862. for (phases::ID Phase : PL) {
  3863. if (Phase == phases::Link) {
  3864. assert(Phase == PL.back() && "linking must be final compilation step.");
  3865. break;
  3866. }
  3867. auto TCAndArch = TCAndArchs.begin();
  3868. for (Action *&A : DeviceActions) {
  3869. if (A->getType() == types::TY_Nothing)
  3870. continue;
  3871. // Propagate the ToolChain so we can use it in ConstructPhaseAction.
  3872. A->propagateDeviceOffloadInfo(Kind, TCAndArch->second.data(),
  3873. TCAndArch->first);
  3874. A = ConstructPhaseAction(C, Args, Phase, A, Kind);
  3875. if (isa<CompileJobAction>(A) && isa<CompileJobAction>(HostAction) &&
  3876. Kind == Action::OFK_OpenMP &&
  3877. HostAction->getType() != types::TY_Nothing) {
  3878. // OpenMP offloading has a dependency on the host compile action to
  3879. // identify which declarations need to be emitted. This shouldn't be
  3880. // collapsed with any other actions so we can use it in the device.
  3881. HostAction->setCannotBeCollapsedWithNextDependentAction();
  3882. OffloadAction::HostDependence HDep(
  3883. *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(),
  3884. TCAndArch->second.data(), Kind);
  3885. OffloadAction::DeviceDependences DDep;
  3886. DDep.add(*A, *TCAndArch->first, TCAndArch->second.data(), Kind);
  3887. A = C.MakeAction<OffloadAction>(HDep, DDep);
  3888. }
  3889. ++TCAndArch;
  3890. }
  3891. }
  3892. // Compiling HIP in non-RDC mode requires linking each action individually.
  3893. for (Action *&A : DeviceActions) {
  3894. if ((A->getType() != types::TY_Object &&
  3895. A->getType() != types::TY_LTO_BC) ||
  3896. Kind != Action::OFK_HIP ||
  3897. Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false))
  3898. continue;
  3899. ActionList LinkerInput = {A};
  3900. A = C.MakeAction<LinkJobAction>(LinkerInput, types::TY_Image);
  3901. }
  3902. auto TCAndArch = TCAndArchs.begin();
  3903. for (Action *A : DeviceActions) {
  3904. DDeps.add(*A, *TCAndArch->first, TCAndArch->second.data(), Kind);
  3905. OffloadAction::DeviceDependences DDep;
  3906. DDep.add(*A, *TCAndArch->first, TCAndArch->second.data(), Kind);
  3907. OffloadActions.push_back(C.MakeAction<OffloadAction>(DDep, A->getType()));
  3908. ++TCAndArch;
  3909. }
  3910. }
  3911. if (offloadDeviceOnly())
  3912. return C.MakeAction<OffloadAction>(DDeps, types::TY_Nothing);
  3913. if (OffloadActions.empty())
  3914. return HostAction;
  3915. OffloadAction::DeviceDependences DDep;
  3916. if (C.isOffloadingHostKind(Action::OFK_Cuda) &&
  3917. !Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false)) {
  3918. // If we are not in RDC-mode we just emit the final CUDA fatbinary for
  3919. // each translation unit without requiring any linking.
  3920. Action *FatbinAction =
  3921. C.MakeAction<LinkJobAction>(OffloadActions, types::TY_CUDA_FATBIN);
  3922. DDep.add(*FatbinAction, *C.getSingleOffloadToolChain<Action::OFK_Cuda>(),
  3923. nullptr, Action::OFK_Cuda);
  3924. } else if (C.isOffloadingHostKind(Action::OFK_HIP) &&
  3925. !Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
  3926. false)) {
  3927. // If we are not in RDC-mode we just emit the final HIP fatbinary for each
  3928. // translation unit, linking each input individually.
  3929. Action *FatbinAction =
  3930. C.MakeAction<LinkJobAction>(OffloadActions, types::TY_HIP_FATBIN);
  3931. DDep.add(*FatbinAction, *C.getSingleOffloadToolChain<Action::OFK_HIP>(),
  3932. nullptr, Action::OFK_HIP);
  3933. } else {
  3934. // Package all the offloading actions into a single output that can be
  3935. // embedded in the host and linked.
  3936. Action *PackagerAction =
  3937. C.MakeAction<OffloadPackagerJobAction>(OffloadActions, types::TY_Image);
  3938. DDep.add(*PackagerAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(),
  3939. nullptr, C.getActiveOffloadKinds());
  3940. }
  3941. // If we are unable to embed a single device output into the host, we need to
  3942. // add each device output as a host dependency to ensure they are still built.
  3943. bool SingleDeviceOutput = !llvm::any_of(OffloadActions, [](Action *A) {
  3944. return A->getType() == types::TY_Nothing;
  3945. }) && isa<CompileJobAction>(HostAction);
  3946. OffloadAction::HostDependence HDep(
  3947. *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(),
  3948. /*BoundArch=*/nullptr, SingleDeviceOutput ? DDep : DDeps);
  3949. return C.MakeAction<OffloadAction>(HDep, SingleDeviceOutput ? DDep : DDeps);
  3950. }
  3951. Action *Driver::ConstructPhaseAction(
  3952. Compilation &C, const ArgList &Args, phases::ID Phase, Action *Input,
  3953. Action::OffloadKind TargetDeviceOffloadKind) const {
  3954. llvm::PrettyStackTraceString CrashInfo("Constructing phase actions");
  3955. // Some types skip the assembler phase (e.g., llvm-bc), but we can't
  3956. // encode this in the steps because the intermediate type depends on
  3957. // arguments. Just special case here.
  3958. if (Phase == phases::Assemble && Input->getType() != types::TY_PP_Asm)
  3959. return Input;
  3960. // Build the appropriate action.
  3961. switch (Phase) {
  3962. case phases::Link:
  3963. llvm_unreachable("link action invalid here.");
  3964. case phases::IfsMerge:
  3965. llvm_unreachable("ifsmerge action invalid here.");
  3966. case phases::Preprocess: {
  3967. types::ID OutputTy;
  3968. // -M and -MM specify the dependency file name by altering the output type,
  3969. // -if -MD and -MMD are not specified.
  3970. if (Args.hasArg(options::OPT_M, options::OPT_MM) &&
  3971. !Args.hasArg(options::OPT_MD, options::OPT_MMD)) {
  3972. OutputTy = types::TY_Dependencies;
  3973. } else {
  3974. OutputTy = Input->getType();
  3975. // For these cases, the preprocessor is only translating forms, the Output
  3976. // still needs preprocessing.
  3977. if (!Args.hasFlag(options::OPT_frewrite_includes,
  3978. options::OPT_fno_rewrite_includes, false) &&
  3979. !Args.hasFlag(options::OPT_frewrite_imports,
  3980. options::OPT_fno_rewrite_imports, false) &&
  3981. !Args.hasFlag(options::OPT_fdirectives_only,
  3982. options::OPT_fno_directives_only, false) &&
  3983. !CCGenDiagnostics)
  3984. OutputTy = types::getPreprocessedType(OutputTy);
  3985. assert(OutputTy != types::TY_INVALID &&
  3986. "Cannot preprocess this input type!");
  3987. }
  3988. return C.MakeAction<PreprocessJobAction>(Input, OutputTy);
  3989. }
  3990. case phases::Precompile: {
  3991. // API extraction should not generate an actual precompilation action.
  3992. if (Args.hasArg(options::OPT_extract_api))
  3993. return C.MakeAction<ExtractAPIJobAction>(Input, types::TY_API_INFO);
  3994. types::ID OutputTy = getPrecompiledType(Input->getType());
  3995. assert(OutputTy != types::TY_INVALID &&
  3996. "Cannot precompile this input type!");
  3997. // If we're given a module name, precompile header file inputs as a
  3998. // module, not as a precompiled header.
  3999. const char *ModName = nullptr;
  4000. if (OutputTy == types::TY_PCH) {
  4001. if (Arg *A = Args.getLastArg(options::OPT_fmodule_name_EQ))
  4002. ModName = A->getValue();
  4003. if (ModName)
  4004. OutputTy = types::TY_ModuleFile;
  4005. }
  4006. if (Args.hasArg(options::OPT_fsyntax_only)) {
  4007. // Syntax checks should not emit a PCH file
  4008. OutputTy = types::TY_Nothing;
  4009. }
  4010. return C.MakeAction<PrecompileJobAction>(Input, OutputTy);
  4011. }
  4012. case phases::Compile: {
  4013. if (Args.hasArg(options::OPT_fsyntax_only))
  4014. return C.MakeAction<CompileJobAction>(Input, types::TY_Nothing);
  4015. if (Args.hasArg(options::OPT_rewrite_objc))
  4016. return C.MakeAction<CompileJobAction>(Input, types::TY_RewrittenObjC);
  4017. if (Args.hasArg(options::OPT_rewrite_legacy_objc))
  4018. return C.MakeAction<CompileJobAction>(Input,
  4019. types::TY_RewrittenLegacyObjC);
  4020. if (Args.hasArg(options::OPT__analyze))
  4021. return C.MakeAction<AnalyzeJobAction>(Input, types::TY_Plist);
  4022. if (Args.hasArg(options::OPT__migrate))
  4023. return C.MakeAction<MigrateJobAction>(Input, types::TY_Remap);
  4024. if (Args.hasArg(options::OPT_emit_ast))
  4025. return C.MakeAction<CompileJobAction>(Input, types::TY_AST);
  4026. if (Args.hasArg(options::OPT_module_file_info))
  4027. return C.MakeAction<CompileJobAction>(Input, types::TY_ModuleFile);
  4028. if (Args.hasArg(options::OPT_verify_pch))
  4029. return C.MakeAction<VerifyPCHJobAction>(Input, types::TY_Nothing);
  4030. if (Args.hasArg(options::OPT_extract_api))
  4031. return C.MakeAction<ExtractAPIJobAction>(Input, types::TY_API_INFO);
  4032. return C.MakeAction<CompileJobAction>(Input, types::TY_LLVM_BC);
  4033. }
  4034. case phases::Backend: {
  4035. if (isUsingLTO() && TargetDeviceOffloadKind == Action::OFK_None) {
  4036. types::ID Output =
  4037. Args.hasArg(options::OPT_S) ? types::TY_LTO_IR : types::TY_LTO_BC;
  4038. return C.MakeAction<BackendJobAction>(Input, Output);
  4039. }
  4040. if (isUsingLTO(/* IsOffload */ true) &&
  4041. TargetDeviceOffloadKind != Action::OFK_None) {
  4042. types::ID Output =
  4043. Args.hasArg(options::OPT_S) ? types::TY_LTO_IR : types::TY_LTO_BC;
  4044. return C.MakeAction<BackendJobAction>(Input, Output);
  4045. }
  4046. if (Args.hasArg(options::OPT_emit_llvm) ||
  4047. (((Input->getOffloadingToolChain() &&
  4048. Input->getOffloadingToolChain()->getTriple().isAMDGPU()) ||
  4049. TargetDeviceOffloadKind == Action::OFK_HIP) &&
  4050. (Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
  4051. false) ||
  4052. TargetDeviceOffloadKind == Action::OFK_OpenMP))) {
  4053. types::ID Output =
  4054. Args.hasArg(options::OPT_S) &&
  4055. (TargetDeviceOffloadKind == Action::OFK_None ||
  4056. offloadDeviceOnly() ||
  4057. (TargetDeviceOffloadKind == Action::OFK_HIP &&
  4058. !Args.hasFlag(options::OPT_offload_new_driver,
  4059. options::OPT_no_offload_new_driver, false)))
  4060. ? types::TY_LLVM_IR
  4061. : types::TY_LLVM_BC;
  4062. return C.MakeAction<BackendJobAction>(Input, Output);
  4063. }
  4064. return C.MakeAction<BackendJobAction>(Input, types::TY_PP_Asm);
  4065. }
  4066. case phases::Assemble:
  4067. return C.MakeAction<AssembleJobAction>(std::move(Input), types::TY_Object);
  4068. }
  4069. llvm_unreachable("invalid phase in ConstructPhaseAction");
  4070. }
  4071. void Driver::BuildJobs(Compilation &C) const {
  4072. llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
  4073. Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
  4074. // It is an error to provide a -o option if we are making multiple output
  4075. // files. There are exceptions:
  4076. //
  4077. // IfsMergeJob: when generating interface stubs enabled we want to be able to
  4078. // generate the stub file at the same time that we generate the real
  4079. // library/a.out. So when a .o, .so, etc are the output, with clang interface
  4080. // stubs there will also be a .ifs and .ifso at the same location.
  4081. //
  4082. // CompileJob of type TY_IFS_CPP: when generating interface stubs is enabled
  4083. // and -c is passed, we still want to be able to generate a .ifs file while
  4084. // we are also generating .o files. So we allow more than one output file in
  4085. // this case as well.
  4086. //
  4087. // OffloadClass of type TY_Nothing: device-only output will place many outputs
  4088. // into a single offloading action. We should count all inputs to the action
  4089. // as outputs. Also ignore device-only outputs if we're compiling with
  4090. // -fsyntax-only.
  4091. if (FinalOutput) {
  4092. unsigned NumOutputs = 0;
  4093. unsigned NumIfsOutputs = 0;
  4094. for (const Action *A : C.getActions()) {
  4095. if (A->getType() != types::TY_Nothing &&
  4096. !(A->getKind() == Action::IfsMergeJobClass ||
  4097. (A->getType() == clang::driver::types::TY_IFS_CPP &&
  4098. A->getKind() == clang::driver::Action::CompileJobClass &&
  4099. 0 == NumIfsOutputs++) ||
  4100. (A->getKind() == Action::BindArchClass && A->getInputs().size() &&
  4101. A->getInputs().front()->getKind() == Action::IfsMergeJobClass)))
  4102. ++NumOutputs;
  4103. else if (A->getKind() == Action::OffloadClass &&
  4104. A->getType() == types::TY_Nothing &&
  4105. !C.getArgs().hasArg(options::OPT_fsyntax_only))
  4106. NumOutputs += A->size();
  4107. }
  4108. if (NumOutputs > 1) {
  4109. Diag(clang::diag::err_drv_output_argument_with_multiple_files);
  4110. FinalOutput = nullptr;
  4111. }
  4112. }
  4113. const llvm::Triple &RawTriple = C.getDefaultToolChain().getTriple();
  4114. if (RawTriple.isOSAIX()) {
  4115. if (Arg *A = C.getArgs().getLastArg(options::OPT_G))
  4116. Diag(diag::err_drv_unsupported_opt_for_target)
  4117. << A->getSpelling() << RawTriple.str();
  4118. if (LTOMode == LTOK_Thin)
  4119. Diag(diag::err_drv_clang_unsupported) << "thinLTO on AIX";
  4120. }
  4121. // Collect the list of architectures.
  4122. llvm::StringSet<> ArchNames;
  4123. if (RawTriple.isOSBinFormatMachO())
  4124. for (const Arg *A : C.getArgs())
  4125. if (A->getOption().matches(options::OPT_arch))
  4126. ArchNames.insert(A->getValue());
  4127. // Set of (Action, canonical ToolChain triple) pairs we've built jobs for.
  4128. std::map<std::pair<const Action *, std::string>, InputInfoList> CachedResults;
  4129. for (Action *A : C.getActions()) {
  4130. // If we are linking an image for multiple archs then the linker wants
  4131. // -arch_multiple and -final_output <final image name>. Unfortunately, this
  4132. // doesn't fit in cleanly because we have to pass this information down.
  4133. //
  4134. // FIXME: This is a hack; find a cleaner way to integrate this into the
  4135. // process.
  4136. const char *LinkingOutput = nullptr;
  4137. if (isa<LipoJobAction>(A)) {
  4138. if (FinalOutput)
  4139. LinkingOutput = FinalOutput->getValue();
  4140. else
  4141. LinkingOutput = getDefaultImageName();
  4142. }
  4143. BuildJobsForAction(C, A, &C.getDefaultToolChain(),
  4144. /*BoundArch*/ StringRef(),
  4145. /*AtTopLevel*/ true,
  4146. /*MultipleArchs*/ ArchNames.size() > 1,
  4147. /*LinkingOutput*/ LinkingOutput, CachedResults,
  4148. /*TargetDeviceOffloadKind*/ Action::OFK_None);
  4149. }
  4150. // If we have more than one job, then disable integrated-cc1 for now. Do this
  4151. // also when we need to report process execution statistics.
  4152. if (C.getJobs().size() > 1 || CCPrintProcessStats)
  4153. for (auto &J : C.getJobs())
  4154. J.InProcess = false;
  4155. if (CCPrintProcessStats) {
  4156. C.setPostCallback([=](const Command &Cmd, int Res) {
  4157. std::optional<llvm::sys::ProcessStatistics> ProcStat =
  4158. Cmd.getProcessStatistics();
  4159. if (!ProcStat)
  4160. return;
  4161. const char *LinkingOutput = nullptr;
  4162. if (FinalOutput)
  4163. LinkingOutput = FinalOutput->getValue();
  4164. else if (!Cmd.getOutputFilenames().empty())
  4165. LinkingOutput = Cmd.getOutputFilenames().front().c_str();
  4166. else
  4167. LinkingOutput = getDefaultImageName();
  4168. if (CCPrintStatReportFilename.empty()) {
  4169. using namespace llvm;
  4170. // Human readable output.
  4171. outs() << sys::path::filename(Cmd.getExecutable()) << ": "
  4172. << "output=" << LinkingOutput;
  4173. outs() << ", total="
  4174. << format("%.3f", ProcStat->TotalTime.count() / 1000.) << " ms"
  4175. << ", user="
  4176. << format("%.3f", ProcStat->UserTime.count() / 1000.) << " ms"
  4177. << ", mem=" << ProcStat->PeakMemory << " Kb\n";
  4178. } else {
  4179. // CSV format.
  4180. std::string Buffer;
  4181. llvm::raw_string_ostream Out(Buffer);
  4182. llvm::sys::printArg(Out, llvm::sys::path::filename(Cmd.getExecutable()),
  4183. /*Quote*/ true);
  4184. Out << ',';
  4185. llvm::sys::printArg(Out, LinkingOutput, true);
  4186. Out << ',' << ProcStat->TotalTime.count() << ','
  4187. << ProcStat->UserTime.count() << ',' << ProcStat->PeakMemory
  4188. << '\n';
  4189. Out.flush();
  4190. std::error_code EC;
  4191. llvm::raw_fd_ostream OS(CCPrintStatReportFilename, EC,
  4192. llvm::sys::fs::OF_Append |
  4193. llvm::sys::fs::OF_Text);
  4194. if (EC)
  4195. return;
  4196. auto L = OS.lock();
  4197. if (!L) {
  4198. llvm::errs() << "ERROR: Cannot lock file "
  4199. << CCPrintStatReportFilename << ": "
  4200. << toString(L.takeError()) << "\n";
  4201. return;
  4202. }
  4203. OS << Buffer;
  4204. OS.flush();
  4205. }
  4206. });
  4207. }
  4208. // If the user passed -Qunused-arguments or there were errors, don't warn
  4209. // about any unused arguments.
  4210. if (Diags.hasErrorOccurred() ||
  4211. C.getArgs().hasArg(options::OPT_Qunused_arguments))
  4212. return;
  4213. // Claim -fdriver-only here.
  4214. (void)C.getArgs().hasArg(options::OPT_fdriver_only);
  4215. // Claim -### here.
  4216. (void)C.getArgs().hasArg(options::OPT__HASH_HASH_HASH);
  4217. // Claim --driver-mode, --rsp-quoting, it was handled earlier.
  4218. (void)C.getArgs().hasArg(options::OPT_driver_mode);
  4219. (void)C.getArgs().hasArg(options::OPT_rsp_quoting);
  4220. for (Arg *A : C.getArgs()) {
  4221. // FIXME: It would be nice to be able to send the argument to the
  4222. // DiagnosticsEngine, so that extra values, position, and so on could be
  4223. // printed.
  4224. if (!A->isClaimed()) {
  4225. if (A->getOption().hasFlag(options::NoArgumentUnused))
  4226. continue;
  4227. // Suppress the warning automatically if this is just a flag, and it is an
  4228. // instance of an argument we already claimed.
  4229. const Option &Opt = A->getOption();
  4230. if (Opt.getKind() == Option::FlagClass) {
  4231. bool DuplicateClaimed = false;
  4232. for (const Arg *AA : C.getArgs().filtered(&Opt)) {
  4233. if (AA->isClaimed()) {
  4234. DuplicateClaimed = true;
  4235. break;
  4236. }
  4237. }
  4238. if (DuplicateClaimed)
  4239. continue;
  4240. }
  4241. // In clang-cl, don't mention unknown arguments here since they have
  4242. // already been warned about.
  4243. if (!IsCLMode() || !A->getOption().matches(options::OPT_UNKNOWN))
  4244. Diag(clang::diag::warn_drv_unused_argument)
  4245. << A->getAsString(C.getArgs());
  4246. }
  4247. }
  4248. }
  4249. namespace {
  4250. /// Utility class to control the collapse of dependent actions and select the
  4251. /// tools accordingly.
  4252. class ToolSelector final {
  4253. /// The tool chain this selector refers to.
  4254. const ToolChain &TC;
  4255. /// The compilation this selector refers to.
  4256. const Compilation &C;
  4257. /// The base action this selector refers to.
  4258. const JobAction *BaseAction;
  4259. /// Set to true if the current toolchain refers to host actions.
  4260. bool IsHostSelector;
  4261. /// Set to true if save-temps and embed-bitcode functionalities are active.
  4262. bool SaveTemps;
  4263. bool EmbedBitcode;
  4264. /// Get previous dependent action or null if that does not exist. If
  4265. /// \a CanBeCollapsed is false, that action must be legal to collapse or
  4266. /// null will be returned.
  4267. const JobAction *getPrevDependentAction(const ActionList &Inputs,
  4268. ActionList &SavedOffloadAction,
  4269. bool CanBeCollapsed = true) {
  4270. // An option can be collapsed only if it has a single input.
  4271. if (Inputs.size() != 1)
  4272. return nullptr;
  4273. Action *CurAction = *Inputs.begin();
  4274. if (CanBeCollapsed &&
  4275. !CurAction->isCollapsingWithNextDependentActionLegal())
  4276. return nullptr;
  4277. // If the input action is an offload action. Look through it and save any
  4278. // offload action that can be dropped in the event of a collapse.
  4279. if (auto *OA = dyn_cast<OffloadAction>(CurAction)) {
  4280. // If the dependent action is a device action, we will attempt to collapse
  4281. // only with other device actions. Otherwise, we would do the same but
  4282. // with host actions only.
  4283. if (!IsHostSelector) {
  4284. if (OA->hasSingleDeviceDependence(/*DoNotConsiderHostActions=*/true)) {
  4285. CurAction =
  4286. OA->getSingleDeviceDependence(/*DoNotConsiderHostActions=*/true);
  4287. if (CanBeCollapsed &&
  4288. !CurAction->isCollapsingWithNextDependentActionLegal())
  4289. return nullptr;
  4290. SavedOffloadAction.push_back(OA);
  4291. return dyn_cast<JobAction>(CurAction);
  4292. }
  4293. } else if (OA->hasHostDependence()) {
  4294. CurAction = OA->getHostDependence();
  4295. if (CanBeCollapsed &&
  4296. !CurAction->isCollapsingWithNextDependentActionLegal())
  4297. return nullptr;
  4298. SavedOffloadAction.push_back(OA);
  4299. return dyn_cast<JobAction>(CurAction);
  4300. }
  4301. return nullptr;
  4302. }
  4303. return dyn_cast<JobAction>(CurAction);
  4304. }
  4305. /// Return true if an assemble action can be collapsed.
  4306. bool canCollapseAssembleAction() const {
  4307. return TC.useIntegratedAs() && !SaveTemps &&
  4308. !C.getArgs().hasArg(options::OPT_via_file_asm) &&
  4309. !C.getArgs().hasArg(options::OPT__SLASH_FA) &&
  4310. !C.getArgs().hasArg(options::OPT__SLASH_Fa);
  4311. }
  4312. /// Return true if a preprocessor action can be collapsed.
  4313. bool canCollapsePreprocessorAction() const {
  4314. return !C.getArgs().hasArg(options::OPT_no_integrated_cpp) &&
  4315. !C.getArgs().hasArg(options::OPT_traditional_cpp) && !SaveTemps &&
  4316. !C.getArgs().hasArg(options::OPT_rewrite_objc);
  4317. }
  4318. /// Struct that relates an action with the offload actions that would be
  4319. /// collapsed with it.
  4320. struct JobActionInfo final {
  4321. /// The action this info refers to.
  4322. const JobAction *JA = nullptr;
  4323. /// The offload actions we need to take care off if this action is
  4324. /// collapsed.
  4325. ActionList SavedOffloadAction;
  4326. };
  4327. /// Append collapsed offload actions from the give nnumber of elements in the
  4328. /// action info array.
  4329. static void AppendCollapsedOffloadAction(ActionList &CollapsedOffloadAction,
  4330. ArrayRef<JobActionInfo> &ActionInfo,
  4331. unsigned ElementNum) {
  4332. assert(ElementNum <= ActionInfo.size() && "Invalid number of elements.");
  4333. for (unsigned I = 0; I < ElementNum; ++I)
  4334. CollapsedOffloadAction.append(ActionInfo[I].SavedOffloadAction.begin(),
  4335. ActionInfo[I].SavedOffloadAction.end());
  4336. }
  4337. /// Functions that attempt to perform the combining. They detect if that is
  4338. /// legal, and if so they update the inputs \a Inputs and the offload action
  4339. /// that were collapsed in \a CollapsedOffloadAction. A tool that deals with
  4340. /// the combined action is returned. If the combining is not legal or if the
  4341. /// tool does not exist, null is returned.
  4342. /// Currently three kinds of collapsing are supported:
  4343. /// - Assemble + Backend + Compile;
  4344. /// - Assemble + Backend ;
  4345. /// - Backend + Compile.
  4346. const Tool *
  4347. combineAssembleBackendCompile(ArrayRef<JobActionInfo> ActionInfo,
  4348. ActionList &Inputs,
  4349. ActionList &CollapsedOffloadAction) {
  4350. if (ActionInfo.size() < 3 || !canCollapseAssembleAction())
  4351. return nullptr;
  4352. auto *AJ = dyn_cast<AssembleJobAction>(ActionInfo[0].JA);
  4353. auto *BJ = dyn_cast<BackendJobAction>(ActionInfo[1].JA);
  4354. auto *CJ = dyn_cast<CompileJobAction>(ActionInfo[2].JA);
  4355. if (!AJ || !BJ || !CJ)
  4356. return nullptr;
  4357. // Get compiler tool.
  4358. const Tool *T = TC.SelectTool(*CJ);
  4359. if (!T)
  4360. return nullptr;
  4361. // Can't collapse if we don't have codegen support unless we are
  4362. // emitting LLVM IR.
  4363. bool OutputIsLLVM = types::isLLVMIR(ActionInfo[0].JA->getType());
  4364. if (!T->hasIntegratedBackend() && !(OutputIsLLVM && T->canEmitIR()))
  4365. return nullptr;
  4366. // When using -fembed-bitcode, it is required to have the same tool (clang)
  4367. // for both CompilerJA and BackendJA. Otherwise, combine two stages.
  4368. if (EmbedBitcode) {
  4369. const Tool *BT = TC.SelectTool(*BJ);
  4370. if (BT == T)
  4371. return nullptr;
  4372. }
  4373. if (!T->hasIntegratedAssembler())
  4374. return nullptr;
  4375. Inputs = CJ->getInputs();
  4376. AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo,
  4377. /*NumElements=*/3);
  4378. return T;
  4379. }
  4380. const Tool *combineAssembleBackend(ArrayRef<JobActionInfo> ActionInfo,
  4381. ActionList &Inputs,
  4382. ActionList &CollapsedOffloadAction) {
  4383. if (ActionInfo.size() < 2 || !canCollapseAssembleAction())
  4384. return nullptr;
  4385. auto *AJ = dyn_cast<AssembleJobAction>(ActionInfo[0].JA);
  4386. auto *BJ = dyn_cast<BackendJobAction>(ActionInfo[1].JA);
  4387. if (!AJ || !BJ)
  4388. return nullptr;
  4389. // Get backend tool.
  4390. const Tool *T = TC.SelectTool(*BJ);
  4391. if (!T)
  4392. return nullptr;
  4393. if (!T->hasIntegratedAssembler())
  4394. return nullptr;
  4395. Inputs = BJ->getInputs();
  4396. AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo,
  4397. /*NumElements=*/2);
  4398. return T;
  4399. }
  4400. const Tool *combineBackendCompile(ArrayRef<JobActionInfo> ActionInfo,
  4401. ActionList &Inputs,
  4402. ActionList &CollapsedOffloadAction) {
  4403. if (ActionInfo.size() < 2)
  4404. return nullptr;
  4405. auto *BJ = dyn_cast<BackendJobAction>(ActionInfo[0].JA);
  4406. auto *CJ = dyn_cast<CompileJobAction>(ActionInfo[1].JA);
  4407. if (!BJ || !CJ)
  4408. return nullptr;
  4409. // Check if the initial input (to the compile job or its predessor if one
  4410. // exists) is LLVM bitcode. In that case, no preprocessor step is required
  4411. // and we can still collapse the compile and backend jobs when we have
  4412. // -save-temps. I.e. there is no need for a separate compile job just to
  4413. // emit unoptimized bitcode.
  4414. bool InputIsBitcode = true;
  4415. for (size_t i = 1; i < ActionInfo.size(); i++)
  4416. if (ActionInfo[i].JA->getType() != types::TY_LLVM_BC &&
  4417. ActionInfo[i].JA->getType() != types::TY_LTO_BC) {
  4418. InputIsBitcode = false;
  4419. break;
  4420. }
  4421. if (!InputIsBitcode && !canCollapsePreprocessorAction())
  4422. return nullptr;
  4423. // Get compiler tool.
  4424. const Tool *T = TC.SelectTool(*CJ);
  4425. if (!T)
  4426. return nullptr;
  4427. // Can't collapse if we don't have codegen support unless we are
  4428. // emitting LLVM IR.
  4429. bool OutputIsLLVM = types::isLLVMIR(ActionInfo[0].JA->getType());
  4430. if (!T->hasIntegratedBackend() && !(OutputIsLLVM && T->canEmitIR()))
  4431. return nullptr;
  4432. if (T->canEmitIR() && ((SaveTemps && !InputIsBitcode) || EmbedBitcode))
  4433. return nullptr;
  4434. Inputs = CJ->getInputs();
  4435. AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo,
  4436. /*NumElements=*/2);
  4437. return T;
  4438. }
  4439. /// Updates the inputs if the obtained tool supports combining with
  4440. /// preprocessor action, and the current input is indeed a preprocessor
  4441. /// action. If combining results in the collapse of offloading actions, those
  4442. /// are appended to \a CollapsedOffloadAction.
  4443. void combineWithPreprocessor(const Tool *T, ActionList &Inputs,
  4444. ActionList &CollapsedOffloadAction) {
  4445. if (!T || !canCollapsePreprocessorAction() || !T->hasIntegratedCPP())
  4446. return;
  4447. // Attempt to get a preprocessor action dependence.
  4448. ActionList PreprocessJobOffloadActions;
  4449. ActionList NewInputs;
  4450. for (Action *A : Inputs) {
  4451. auto *PJ = getPrevDependentAction({A}, PreprocessJobOffloadActions);
  4452. if (!PJ || !isa<PreprocessJobAction>(PJ)) {
  4453. NewInputs.push_back(A);
  4454. continue;
  4455. }
  4456. // This is legal to combine. Append any offload action we found and add the
  4457. // current input to preprocessor inputs.
  4458. CollapsedOffloadAction.append(PreprocessJobOffloadActions.begin(),
  4459. PreprocessJobOffloadActions.end());
  4460. NewInputs.append(PJ->input_begin(), PJ->input_end());
  4461. }
  4462. Inputs = NewInputs;
  4463. }
  4464. public:
  4465. ToolSelector(const JobAction *BaseAction, const ToolChain &TC,
  4466. const Compilation &C, bool SaveTemps, bool EmbedBitcode)
  4467. : TC(TC), C(C), BaseAction(BaseAction), SaveTemps(SaveTemps),
  4468. EmbedBitcode(EmbedBitcode) {
  4469. assert(BaseAction && "Invalid base action.");
  4470. IsHostSelector = BaseAction->getOffloadingDeviceKind() == Action::OFK_None;
  4471. }
  4472. /// Check if a chain of actions can be combined and return the tool that can
  4473. /// handle the combination of actions. The pointer to the current inputs \a
  4474. /// Inputs and the list of offload actions \a CollapsedOffloadActions
  4475. /// connected to collapsed actions are updated accordingly. The latter enables
  4476. /// the caller of the selector to process them afterwards instead of just
  4477. /// dropping them. If no suitable tool is found, null will be returned.
  4478. const Tool *getTool(ActionList &Inputs,
  4479. ActionList &CollapsedOffloadAction) {
  4480. //
  4481. // Get the largest chain of actions that we could combine.
  4482. //
  4483. SmallVector<JobActionInfo, 5> ActionChain(1);
  4484. ActionChain.back().JA = BaseAction;
  4485. while (ActionChain.back().JA) {
  4486. const Action *CurAction = ActionChain.back().JA;
  4487. // Grow the chain by one element.
  4488. ActionChain.resize(ActionChain.size() + 1);
  4489. JobActionInfo &AI = ActionChain.back();
  4490. // Attempt to fill it with the
  4491. AI.JA =
  4492. getPrevDependentAction(CurAction->getInputs(), AI.SavedOffloadAction);
  4493. }
  4494. // Pop the last action info as it could not be filled.
  4495. ActionChain.pop_back();
  4496. //
  4497. // Attempt to combine actions. If all combining attempts failed, just return
  4498. // the tool of the provided action. At the end we attempt to combine the
  4499. // action with any preprocessor action it may depend on.
  4500. //
  4501. const Tool *T = combineAssembleBackendCompile(ActionChain, Inputs,
  4502. CollapsedOffloadAction);
  4503. if (!T)
  4504. T = combineAssembleBackend(ActionChain, Inputs, CollapsedOffloadAction);
  4505. if (!T)
  4506. T = combineBackendCompile(ActionChain, Inputs, CollapsedOffloadAction);
  4507. if (!T) {
  4508. Inputs = BaseAction->getInputs();
  4509. T = TC.SelectTool(*BaseAction);
  4510. }
  4511. combineWithPreprocessor(T, Inputs, CollapsedOffloadAction);
  4512. return T;
  4513. }
  4514. };
  4515. }
  4516. /// Return a string that uniquely identifies the result of a job. The bound arch
  4517. /// is not necessarily represented in the toolchain's triple -- for example,
  4518. /// armv7 and armv7s both map to the same triple -- so we need both in our map.
  4519. /// Also, we need to add the offloading device kind, as the same tool chain can
  4520. /// be used for host and device for some programming models, e.g. OpenMP.
  4521. static std::string GetTriplePlusArchString(const ToolChain *TC,
  4522. StringRef BoundArch,
  4523. Action::OffloadKind OffloadKind) {
  4524. std::string TriplePlusArch = TC->getTriple().normalize();
  4525. if (!BoundArch.empty()) {
  4526. TriplePlusArch += "-";
  4527. TriplePlusArch += BoundArch;
  4528. }
  4529. TriplePlusArch += "-";
  4530. TriplePlusArch += Action::GetOffloadKindName(OffloadKind);
  4531. return TriplePlusArch;
  4532. }
  4533. InputInfoList Driver::BuildJobsForAction(
  4534. Compilation &C, const Action *A, const ToolChain *TC, StringRef BoundArch,
  4535. bool AtTopLevel, bool MultipleArchs, const char *LinkingOutput,
  4536. std::map<std::pair<const Action *, std::string>, InputInfoList>
  4537. &CachedResults,
  4538. Action::OffloadKind TargetDeviceOffloadKind) const {
  4539. std::pair<const Action *, std::string> ActionTC = {
  4540. A, GetTriplePlusArchString(TC, BoundArch, TargetDeviceOffloadKind)};
  4541. auto CachedResult = CachedResults.find(ActionTC);
  4542. if (CachedResult != CachedResults.end()) {
  4543. return CachedResult->second;
  4544. }
  4545. InputInfoList Result = BuildJobsForActionNoCache(
  4546. C, A, TC, BoundArch, AtTopLevel, MultipleArchs, LinkingOutput,
  4547. CachedResults, TargetDeviceOffloadKind);
  4548. CachedResults[ActionTC] = Result;
  4549. return Result;
  4550. }
  4551. InputInfoList Driver::BuildJobsForActionNoCache(
  4552. Compilation &C, const Action *A, const ToolChain *TC, StringRef BoundArch,
  4553. bool AtTopLevel, bool MultipleArchs, const char *LinkingOutput,
  4554. std::map<std::pair<const Action *, std::string>, InputInfoList>
  4555. &CachedResults,
  4556. Action::OffloadKind TargetDeviceOffloadKind) const {
  4557. llvm::PrettyStackTraceString CrashInfo("Building compilation jobs");
  4558. InputInfoList OffloadDependencesInputInfo;
  4559. bool BuildingForOffloadDevice = TargetDeviceOffloadKind != Action::OFK_None;
  4560. if (const OffloadAction *OA = dyn_cast<OffloadAction>(A)) {
  4561. // The 'Darwin' toolchain is initialized only when its arguments are
  4562. // computed. Get the default arguments for OFK_None to ensure that
  4563. // initialization is performed before processing the offload action.
  4564. // FIXME: Remove when darwin's toolchain is initialized during construction.
  4565. C.getArgsForToolChain(TC, BoundArch, Action::OFK_None);
  4566. // The offload action is expected to be used in four different situations.
  4567. //
  4568. // a) Set a toolchain/architecture/kind for a host action:
  4569. // Host Action 1 -> OffloadAction -> Host Action 2
  4570. //
  4571. // b) Set a toolchain/architecture/kind for a device action;
  4572. // Device Action 1 -> OffloadAction -> Device Action 2
  4573. //
  4574. // c) Specify a device dependence to a host action;
  4575. // Device Action 1 _
  4576. // \
  4577. // Host Action 1 ---> OffloadAction -> Host Action 2
  4578. //
  4579. // d) Specify a host dependence to a device action.
  4580. // Host Action 1 _
  4581. // \
  4582. // Device Action 1 ---> OffloadAction -> Device Action 2
  4583. //
  4584. // For a) and b), we just return the job generated for the dependences. For
  4585. // c) and d) we override the current action with the host/device dependence
  4586. // if the current toolchain is host/device and set the offload dependences
  4587. // info with the jobs obtained from the device/host dependence(s).
  4588. // If there is a single device option or has no host action, just generate
  4589. // the job for it.
  4590. if (OA->hasSingleDeviceDependence() || !OA->hasHostDependence()) {
  4591. InputInfoList DevA;
  4592. OA->doOnEachDeviceDependence([&](Action *DepA, const ToolChain *DepTC,
  4593. const char *DepBoundArch) {
  4594. DevA.append(BuildJobsForAction(C, DepA, DepTC, DepBoundArch, AtTopLevel,
  4595. /*MultipleArchs*/ !!DepBoundArch,
  4596. LinkingOutput, CachedResults,
  4597. DepA->getOffloadingDeviceKind()));
  4598. });
  4599. return DevA;
  4600. }
  4601. // If 'Action 2' is host, we generate jobs for the device dependences and
  4602. // override the current action with the host dependence. Otherwise, we
  4603. // generate the host dependences and override the action with the device
  4604. // dependence. The dependences can't therefore be a top-level action.
  4605. OA->doOnEachDependence(
  4606. /*IsHostDependence=*/BuildingForOffloadDevice,
  4607. [&](Action *DepA, const ToolChain *DepTC, const char *DepBoundArch) {
  4608. OffloadDependencesInputInfo.append(BuildJobsForAction(
  4609. C, DepA, DepTC, DepBoundArch, /*AtTopLevel=*/false,
  4610. /*MultipleArchs*/ !!DepBoundArch, LinkingOutput, CachedResults,
  4611. DepA->getOffloadingDeviceKind()));
  4612. });
  4613. A = BuildingForOffloadDevice
  4614. ? OA->getSingleDeviceDependence(/*DoNotConsiderHostActions=*/true)
  4615. : OA->getHostDependence();
  4616. // We may have already built this action as a part of the offloading
  4617. // toolchain, return the cached input if so.
  4618. std::pair<const Action *, std::string> ActionTC = {
  4619. OA->getHostDependence(),
  4620. GetTriplePlusArchString(TC, BoundArch, TargetDeviceOffloadKind)};
  4621. if (CachedResults.find(ActionTC) != CachedResults.end()) {
  4622. InputInfoList Inputs = CachedResults[ActionTC];
  4623. Inputs.append(OffloadDependencesInputInfo);
  4624. return Inputs;
  4625. }
  4626. }
  4627. if (const InputAction *IA = dyn_cast<InputAction>(A)) {
  4628. // FIXME: It would be nice to not claim this here; maybe the old scheme of
  4629. // just using Args was better?
  4630. const Arg &Input = IA->getInputArg();
  4631. Input.claim();
  4632. if (Input.getOption().matches(options::OPT_INPUT)) {
  4633. const char *Name = Input.getValue();
  4634. return {InputInfo(A, Name, /* _BaseInput = */ Name)};
  4635. }
  4636. return {InputInfo(A, &Input, /* _BaseInput = */ "")};
  4637. }
  4638. if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) {
  4639. const ToolChain *TC;
  4640. StringRef ArchName = BAA->getArchName();
  4641. if (!ArchName.empty())
  4642. TC = &getToolChain(C.getArgs(),
  4643. computeTargetTriple(*this, TargetTriple,
  4644. C.getArgs(), ArchName));
  4645. else
  4646. TC = &C.getDefaultToolChain();
  4647. return BuildJobsForAction(C, *BAA->input_begin(), TC, ArchName, AtTopLevel,
  4648. MultipleArchs, LinkingOutput, CachedResults,
  4649. TargetDeviceOffloadKind);
  4650. }
  4651. ActionList Inputs = A->getInputs();
  4652. const JobAction *JA = cast<JobAction>(A);
  4653. ActionList CollapsedOffloadActions;
  4654. ToolSelector TS(JA, *TC, C, isSaveTempsEnabled(),
  4655. embedBitcodeInObject() && !isUsingLTO());
  4656. const Tool *T = TS.getTool(Inputs, CollapsedOffloadActions);
  4657. if (!T)
  4658. return {InputInfo()};
  4659. // If we've collapsed action list that contained OffloadAction we
  4660. // need to build jobs for host/device-side inputs it may have held.
  4661. for (const auto *OA : CollapsedOffloadActions)
  4662. cast<OffloadAction>(OA)->doOnEachDependence(
  4663. /*IsHostDependence=*/BuildingForOffloadDevice,
  4664. [&](Action *DepA, const ToolChain *DepTC, const char *DepBoundArch) {
  4665. OffloadDependencesInputInfo.append(BuildJobsForAction(
  4666. C, DepA, DepTC, DepBoundArch, /* AtTopLevel */ false,
  4667. /*MultipleArchs=*/!!DepBoundArch, LinkingOutput, CachedResults,
  4668. DepA->getOffloadingDeviceKind()));
  4669. });
  4670. // Only use pipes when there is exactly one input.
  4671. InputInfoList InputInfos;
  4672. for (const Action *Input : Inputs) {
  4673. // Treat dsymutil and verify sub-jobs as being at the top-level too, they
  4674. // shouldn't get temporary output names.
  4675. // FIXME: Clean this up.
  4676. bool SubJobAtTopLevel =
  4677. AtTopLevel && (isa<DsymutilJobAction>(A) || isa<VerifyJobAction>(A));
  4678. InputInfos.append(BuildJobsForAction(
  4679. C, Input, TC, BoundArch, SubJobAtTopLevel, MultipleArchs, LinkingOutput,
  4680. CachedResults, A->getOffloadingDeviceKind()));
  4681. }
  4682. // Always use the first file input as the base input.
  4683. const char *BaseInput = InputInfos[0].getBaseInput();
  4684. for (auto &Info : InputInfos) {
  4685. if (Info.isFilename()) {
  4686. BaseInput = Info.getBaseInput();
  4687. break;
  4688. }
  4689. }
  4690. // ... except dsymutil actions, which use their actual input as the base
  4691. // input.
  4692. if (JA->getType() == types::TY_dSYM)
  4693. BaseInput = InputInfos[0].getFilename();
  4694. // Append outputs of offload device jobs to the input list
  4695. if (!OffloadDependencesInputInfo.empty())
  4696. InputInfos.append(OffloadDependencesInputInfo.begin(),
  4697. OffloadDependencesInputInfo.end());
  4698. // Set the effective triple of the toolchain for the duration of this job.
  4699. llvm::Triple EffectiveTriple;
  4700. const ToolChain &ToolTC = T->getToolChain();
  4701. const ArgList &Args =
  4702. C.getArgsForToolChain(TC, BoundArch, A->getOffloadingDeviceKind());
  4703. if (InputInfos.size() != 1) {
  4704. EffectiveTriple = llvm::Triple(ToolTC.ComputeEffectiveClangTriple(Args));
  4705. } else {
  4706. // Pass along the input type if it can be unambiguously determined.
  4707. EffectiveTriple = llvm::Triple(
  4708. ToolTC.ComputeEffectiveClangTriple(Args, InputInfos[0].getType()));
  4709. }
  4710. RegisterEffectiveTriple TripleRAII(ToolTC, EffectiveTriple);
  4711. // Determine the place to write output to, if any.
  4712. InputInfo Result;
  4713. InputInfoList UnbundlingResults;
  4714. if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(JA)) {
  4715. // If we have an unbundling job, we need to create results for all the
  4716. // outputs. We also update the results cache so that other actions using
  4717. // this unbundling action can get the right results.
  4718. for (auto &UI : UA->getDependentActionsInfo()) {
  4719. assert(UI.DependentOffloadKind != Action::OFK_None &&
  4720. "Unbundling with no offloading??");
  4721. // Unbundling actions are never at the top level. When we generate the
  4722. // offloading prefix, we also do that for the host file because the
  4723. // unbundling action does not change the type of the output which can
  4724. // cause a overwrite.
  4725. std::string OffloadingPrefix = Action::GetOffloadingFileNamePrefix(
  4726. UI.DependentOffloadKind,
  4727. UI.DependentToolChain->getTriple().normalize(),
  4728. /*CreatePrefixForHost=*/true);
  4729. auto CurI = InputInfo(
  4730. UA,
  4731. GetNamedOutputPath(C, *UA, BaseInput, UI.DependentBoundArch,
  4732. /*AtTopLevel=*/false,
  4733. MultipleArchs ||
  4734. UI.DependentOffloadKind == Action::OFK_HIP,
  4735. OffloadingPrefix),
  4736. BaseInput);
  4737. // Save the unbundling result.
  4738. UnbundlingResults.push_back(CurI);
  4739. // Get the unique string identifier for this dependence and cache the
  4740. // result.
  4741. StringRef Arch;
  4742. if (TargetDeviceOffloadKind == Action::OFK_HIP) {
  4743. if (UI.DependentOffloadKind == Action::OFK_Host)
  4744. Arch = StringRef();
  4745. else
  4746. Arch = UI.DependentBoundArch;
  4747. } else
  4748. Arch = BoundArch;
  4749. CachedResults[{A, GetTriplePlusArchString(UI.DependentToolChain, Arch,
  4750. UI.DependentOffloadKind)}] = {
  4751. CurI};
  4752. }
  4753. // Now that we have all the results generated, select the one that should be
  4754. // returned for the current depending action.
  4755. std::pair<const Action *, std::string> ActionTC = {
  4756. A, GetTriplePlusArchString(TC, BoundArch, TargetDeviceOffloadKind)};
  4757. assert(CachedResults.find(ActionTC) != CachedResults.end() &&
  4758. "Result does not exist??");
  4759. Result = CachedResults[ActionTC].front();
  4760. } else if (JA->getType() == types::TY_Nothing)
  4761. Result = {InputInfo(A, BaseInput)};
  4762. else {
  4763. // We only have to generate a prefix for the host if this is not a top-level
  4764. // action.
  4765. std::string OffloadingPrefix = Action::GetOffloadingFileNamePrefix(
  4766. A->getOffloadingDeviceKind(), TC->getTriple().normalize(),
  4767. /*CreatePrefixForHost=*/isa<OffloadPackagerJobAction>(A) ||
  4768. !(A->getOffloadingHostActiveKinds() == Action::OFK_None ||
  4769. AtTopLevel));
  4770. Result = InputInfo(A, GetNamedOutputPath(C, *JA, BaseInput, BoundArch,
  4771. AtTopLevel, MultipleArchs,
  4772. OffloadingPrefix),
  4773. BaseInput);
  4774. }
  4775. if (CCCPrintBindings && !CCGenDiagnostics) {
  4776. llvm::errs() << "# \"" << T->getToolChain().getTripleString() << '"'
  4777. << " - \"" << T->getName() << "\", inputs: [";
  4778. for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) {
  4779. llvm::errs() << InputInfos[i].getAsString();
  4780. if (i + 1 != e)
  4781. llvm::errs() << ", ";
  4782. }
  4783. if (UnbundlingResults.empty())
  4784. llvm::errs() << "], output: " << Result.getAsString() << "\n";
  4785. else {
  4786. llvm::errs() << "], outputs: [";
  4787. for (unsigned i = 0, e = UnbundlingResults.size(); i != e; ++i) {
  4788. llvm::errs() << UnbundlingResults[i].getAsString();
  4789. if (i + 1 != e)
  4790. llvm::errs() << ", ";
  4791. }
  4792. llvm::errs() << "] \n";
  4793. }
  4794. } else {
  4795. if (UnbundlingResults.empty())
  4796. T->ConstructJob(
  4797. C, *JA, Result, InputInfos,
  4798. C.getArgsForToolChain(TC, BoundArch, JA->getOffloadingDeviceKind()),
  4799. LinkingOutput);
  4800. else
  4801. T->ConstructJobMultipleOutputs(
  4802. C, *JA, UnbundlingResults, InputInfos,
  4803. C.getArgsForToolChain(TC, BoundArch, JA->getOffloadingDeviceKind()),
  4804. LinkingOutput);
  4805. }
  4806. return {Result};
  4807. }
  4808. const char *Driver::getDefaultImageName() const {
  4809. llvm::Triple Target(llvm::Triple::normalize(TargetTriple));
  4810. return Target.isOSWindows() ? "a.exe" : "a.out";
  4811. }
  4812. /// Create output filename based on ArgValue, which could either be a
  4813. /// full filename, filename without extension, or a directory. If ArgValue
  4814. /// does not provide a filename, then use BaseName, and use the extension
  4815. /// suitable for FileType.
  4816. static const char *MakeCLOutputFilename(const ArgList &Args, StringRef ArgValue,
  4817. StringRef BaseName,
  4818. types::ID FileType) {
  4819. SmallString<128> Filename = ArgValue;
  4820. if (ArgValue.empty()) {
  4821. // If the argument is empty, output to BaseName in the current dir.
  4822. Filename = BaseName;
  4823. } else if (llvm::sys::path::is_separator(Filename.back())) {
  4824. // If the argument is a directory, output to BaseName in that dir.
  4825. llvm::sys::path::append(Filename, BaseName);
  4826. }
  4827. if (!llvm::sys::path::has_extension(ArgValue)) {
  4828. // If the argument didn't provide an extension, then set it.
  4829. const char *Extension = types::getTypeTempSuffix(FileType, true);
  4830. if (FileType == types::TY_Image &&
  4831. Args.hasArg(options::OPT__SLASH_LD, options::OPT__SLASH_LDd)) {
  4832. // The output file is a dll.
  4833. Extension = "dll";
  4834. }
  4835. llvm::sys::path::replace_extension(Filename, Extension);
  4836. }
  4837. return Args.MakeArgString(Filename.c_str());
  4838. }
  4839. static bool HasPreprocessOutput(const Action &JA) {
  4840. if (isa<PreprocessJobAction>(JA))
  4841. return true;
  4842. if (isa<OffloadAction>(JA) && isa<PreprocessJobAction>(JA.getInputs()[0]))
  4843. return true;
  4844. if (isa<OffloadBundlingJobAction>(JA) &&
  4845. HasPreprocessOutput(*(JA.getInputs()[0])))
  4846. return true;
  4847. return false;
  4848. }
  4849. const char *Driver::CreateTempFile(Compilation &C, StringRef Prefix,
  4850. StringRef Suffix, bool MultipleArchs,
  4851. StringRef BoundArch) const {
  4852. SmallString<128> TmpName;
  4853. Arg *A = C.getArgs().getLastArg(options::OPT_fcrash_diagnostics_dir);
  4854. std::optional<std::string> CrashDirectory =
  4855. CCGenDiagnostics && A
  4856. ? std::string(A->getValue())
  4857. : llvm::sys::Process::GetEnv("CLANG_CRASH_DIAGNOSTICS_DIR");
  4858. if (CrashDirectory) {
  4859. if (!getVFS().exists(*CrashDirectory))
  4860. llvm::sys::fs::create_directories(*CrashDirectory);
  4861. SmallString<128> Path(*CrashDirectory);
  4862. llvm::sys::path::append(Path, Prefix);
  4863. const char *Middle = !Suffix.empty() ? "-%%%%%%." : "-%%%%%%";
  4864. if (std::error_code EC =
  4865. llvm::sys::fs::createUniqueFile(Path + Middle + Suffix, TmpName)) {
  4866. Diag(clang::diag::err_unable_to_make_temp) << EC.message();
  4867. return "";
  4868. }
  4869. } else {
  4870. if (MultipleArchs && !BoundArch.empty()) {
  4871. TmpName = GetTemporaryDirectory(Prefix);
  4872. llvm::sys::path::append(TmpName,
  4873. Twine(Prefix) + "-" + BoundArch + "." + Suffix);
  4874. } else {
  4875. TmpName = GetTemporaryPath(Prefix, Suffix);
  4876. }
  4877. }
  4878. return C.addTempFile(C.getArgs().MakeArgString(TmpName));
  4879. }
  4880. // Calculate the output path of the module file when compiling a module unit
  4881. // with the `-fmodule-output` option or `-fmodule-output=` option specified.
  4882. // The behavior is:
  4883. // - If `-fmodule-output=` is specfied, then the module file is
  4884. // writing to the value.
  4885. // - Otherwise if the output object file of the module unit is specified, the
  4886. // output path
  4887. // of the module file should be the same with the output object file except
  4888. // the corresponding suffix. This requires both `-o` and `-c` are specified.
  4889. // - Otherwise, the output path of the module file will be the same with the
  4890. // input with the corresponding suffix.
  4891. static const char *GetModuleOutputPath(Compilation &C, const JobAction &JA,
  4892. const char *BaseInput) {
  4893. assert(isa<PrecompileJobAction>(JA) && JA.getType() == types::TY_ModuleFile &&
  4894. (C.getArgs().hasArg(options::OPT_fmodule_output) ||
  4895. C.getArgs().hasArg(options::OPT_fmodule_output_EQ)));
  4896. if (Arg *ModuleOutputEQ =
  4897. C.getArgs().getLastArg(options::OPT_fmodule_output_EQ))
  4898. return C.addResultFile(ModuleOutputEQ->getValue(), &JA);
  4899. SmallString<64> OutputPath;
  4900. Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
  4901. if (FinalOutput && C.getArgs().hasArg(options::OPT_c))
  4902. OutputPath = FinalOutput->getValue();
  4903. else
  4904. OutputPath = BaseInput;
  4905. const char *Extension = types::getTypeTempSuffix(JA.getType());
  4906. llvm::sys::path::replace_extension(OutputPath, Extension);
  4907. return C.addResultFile(C.getArgs().MakeArgString(OutputPath.c_str()), &JA);
  4908. }
  4909. const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA,
  4910. const char *BaseInput,
  4911. StringRef OrigBoundArch, bool AtTopLevel,
  4912. bool MultipleArchs,
  4913. StringRef OffloadingPrefix) const {
  4914. std::string BoundArch = OrigBoundArch.str();
  4915. if (is_style_windows(llvm::sys::path::Style::native)) {
  4916. // BoundArch may contains ':', which is invalid in file names on Windows,
  4917. // therefore replace it with '%'.
  4918. std::replace(BoundArch.begin(), BoundArch.end(), ':', '@');
  4919. }
  4920. llvm::PrettyStackTraceString CrashInfo("Computing output path");
  4921. // Output to a user requested destination?
  4922. if (AtTopLevel && !isa<DsymutilJobAction>(JA) && !isa<VerifyJobAction>(JA)) {
  4923. if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o))
  4924. return C.addResultFile(FinalOutput->getValue(), &JA);
  4925. }
  4926. // For /P, preprocess to file named after BaseInput.
  4927. if (C.getArgs().hasArg(options::OPT__SLASH_P)) {
  4928. assert(AtTopLevel && isa<PreprocessJobAction>(JA));
  4929. StringRef BaseName = llvm::sys::path::filename(BaseInput);
  4930. StringRef NameArg;
  4931. if (Arg *A = C.getArgs().getLastArg(options::OPT__SLASH_Fi))
  4932. NameArg = A->getValue();
  4933. return C.addResultFile(
  4934. MakeCLOutputFilename(C.getArgs(), NameArg, BaseName, types::TY_PP_C),
  4935. &JA);
  4936. }
  4937. // Default to writing to stdout?
  4938. if (AtTopLevel && !CCGenDiagnostics && HasPreprocessOutput(JA)) {
  4939. return "-";
  4940. }
  4941. if (JA.getType() == types::TY_ModuleFile &&
  4942. C.getArgs().getLastArg(options::OPT_module_file_info)) {
  4943. return "-";
  4944. }
  4945. if (IsDXCMode() && !C.getArgs().hasArg(options::OPT_o))
  4946. return "-";
  4947. // Is this the assembly listing for /FA?
  4948. if (JA.getType() == types::TY_PP_Asm &&
  4949. (C.getArgs().hasArg(options::OPT__SLASH_FA) ||
  4950. C.getArgs().hasArg(options::OPT__SLASH_Fa))) {
  4951. // Use /Fa and the input filename to determine the asm file name.
  4952. StringRef BaseName = llvm::sys::path::filename(BaseInput);
  4953. StringRef FaValue = C.getArgs().getLastArgValue(options::OPT__SLASH_Fa);
  4954. return C.addResultFile(
  4955. MakeCLOutputFilename(C.getArgs(), FaValue, BaseName, JA.getType()),
  4956. &JA);
  4957. }
  4958. bool SpecifiedModuleOutput =
  4959. C.getArgs().hasArg(options::OPT_fmodule_output) ||
  4960. C.getArgs().hasArg(options::OPT_fmodule_output_EQ);
  4961. if (MultipleArchs && SpecifiedModuleOutput)
  4962. Diag(clang::diag::err_drv_module_output_with_multiple_arch);
  4963. // If we're emitting a module output with the specified option
  4964. // `-fmodule-output`.
  4965. if (!AtTopLevel && isa<PrecompileJobAction>(JA) &&
  4966. JA.getType() == types::TY_ModuleFile && SpecifiedModuleOutput)
  4967. return GetModuleOutputPath(C, JA, BaseInput);
  4968. // Output to a temporary file?
  4969. if ((!AtTopLevel && !isSaveTempsEnabled() &&
  4970. !C.getArgs().hasArg(options::OPT__SLASH_Fo)) ||
  4971. CCGenDiagnostics) {
  4972. StringRef Name = llvm::sys::path::filename(BaseInput);
  4973. std::pair<StringRef, StringRef> Split = Name.split('.');
  4974. const char *Suffix = types::getTypeTempSuffix(JA.getType(), IsCLMode());
  4975. return CreateTempFile(C, Split.first, Suffix, MultipleArchs, BoundArch);
  4976. }
  4977. SmallString<128> BasePath(BaseInput);
  4978. SmallString<128> ExternalPath("");
  4979. StringRef BaseName;
  4980. // Dsymutil actions should use the full path.
  4981. if (isa<DsymutilJobAction>(JA) && C.getArgs().hasArg(options::OPT_dsym_dir)) {
  4982. ExternalPath += C.getArgs().getLastArg(options::OPT_dsym_dir)->getValue();
  4983. // We use posix style here because the tests (specifically
  4984. // darwin-dsymutil.c) demonstrate that posix style paths are acceptable
  4985. // even on Windows and if we don't then the similar test covering this
  4986. // fails.
  4987. llvm::sys::path::append(ExternalPath, llvm::sys::path::Style::posix,
  4988. llvm::sys::path::filename(BasePath));
  4989. BaseName = ExternalPath;
  4990. } else if (isa<DsymutilJobAction>(JA) || isa<VerifyJobAction>(JA))
  4991. BaseName = BasePath;
  4992. else
  4993. BaseName = llvm::sys::path::filename(BasePath);
  4994. // Determine what the derived output name should be.
  4995. const char *NamedOutput;
  4996. if ((JA.getType() == types::TY_Object || JA.getType() == types::TY_LTO_BC) &&
  4997. C.getArgs().hasArg(options::OPT__SLASH_Fo, options::OPT__SLASH_o)) {
  4998. // The /Fo or /o flag decides the object filename.
  4999. StringRef Val =
  5000. C.getArgs()
  5001. .getLastArg(options::OPT__SLASH_Fo, options::OPT__SLASH_o)
  5002. ->getValue();
  5003. NamedOutput =
  5004. MakeCLOutputFilename(C.getArgs(), Val, BaseName, types::TY_Object);
  5005. } else if (JA.getType() == types::TY_Image &&
  5006. C.getArgs().hasArg(options::OPT__SLASH_Fe,
  5007. options::OPT__SLASH_o)) {
  5008. // The /Fe or /o flag names the linked file.
  5009. StringRef Val =
  5010. C.getArgs()
  5011. .getLastArg(options::OPT__SLASH_Fe, options::OPT__SLASH_o)
  5012. ->getValue();
  5013. NamedOutput =
  5014. MakeCLOutputFilename(C.getArgs(), Val, BaseName, types::TY_Image);
  5015. } else if (JA.getType() == types::TY_Image) {
  5016. if (IsCLMode()) {
  5017. // clang-cl uses BaseName for the executable name.
  5018. NamedOutput =
  5019. MakeCLOutputFilename(C.getArgs(), "", BaseName, types::TY_Image);
  5020. } else {
  5021. SmallString<128> Output(getDefaultImageName());
  5022. // HIP image for device compilation with -fno-gpu-rdc is per compilation
  5023. // unit.
  5024. bool IsHIPNoRDC = JA.getOffloadingDeviceKind() == Action::OFK_HIP &&
  5025. !C.getArgs().hasFlag(options::OPT_fgpu_rdc,
  5026. options::OPT_fno_gpu_rdc, false);
  5027. bool UseOutExtension = IsHIPNoRDC || isa<OffloadPackagerJobAction>(JA);
  5028. if (UseOutExtension) {
  5029. Output = BaseName;
  5030. llvm::sys::path::replace_extension(Output, "");
  5031. }
  5032. Output += OffloadingPrefix;
  5033. if (MultipleArchs && !BoundArch.empty()) {
  5034. Output += "-";
  5035. Output.append(BoundArch);
  5036. }
  5037. if (UseOutExtension)
  5038. Output += ".out";
  5039. NamedOutput = C.getArgs().MakeArgString(Output.c_str());
  5040. }
  5041. } else if (JA.getType() == types::TY_PCH && IsCLMode()) {
  5042. NamedOutput = C.getArgs().MakeArgString(GetClPchPath(C, BaseName));
  5043. } else if ((JA.getType() == types::TY_Plist || JA.getType() == types::TY_AST) &&
  5044. C.getArgs().hasArg(options::OPT__SLASH_o)) {
  5045. StringRef Val =
  5046. C.getArgs()
  5047. .getLastArg(options::OPT__SLASH_o)
  5048. ->getValue();
  5049. NamedOutput =
  5050. MakeCLOutputFilename(C.getArgs(), Val, BaseName, types::TY_Object);
  5051. } else {
  5052. const char *Suffix = types::getTypeTempSuffix(JA.getType(), IsCLMode());
  5053. assert(Suffix && "All types used for output should have a suffix.");
  5054. std::string::size_type End = std::string::npos;
  5055. if (!types::appendSuffixForType(JA.getType()))
  5056. End = BaseName.rfind('.');
  5057. SmallString<128> Suffixed(BaseName.substr(0, End));
  5058. Suffixed += OffloadingPrefix;
  5059. if (MultipleArchs && !BoundArch.empty()) {
  5060. Suffixed += "-";
  5061. Suffixed.append(BoundArch);
  5062. }
  5063. // When using both -save-temps and -emit-llvm, use a ".tmp.bc" suffix for
  5064. // the unoptimized bitcode so that it does not get overwritten by the ".bc"
  5065. // optimized bitcode output.
  5066. auto IsAMDRDCInCompilePhase = [](const JobAction &JA,
  5067. const llvm::opt::DerivedArgList &Args) {
  5068. // The relocatable compilation in HIP and OpenMP implies -emit-llvm.
  5069. // Similarly, use a ".tmp.bc" suffix for the unoptimized bitcode
  5070. // (generated in the compile phase.)
  5071. const ToolChain *TC = JA.getOffloadingToolChain();
  5072. return isa<CompileJobAction>(JA) &&
  5073. ((JA.getOffloadingDeviceKind() == Action::OFK_HIP &&
  5074. Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
  5075. false)) ||
  5076. (JA.getOffloadingDeviceKind() == Action::OFK_OpenMP && TC &&
  5077. TC->getTriple().isAMDGPU()));
  5078. };
  5079. if (!AtTopLevel && JA.getType() == types::TY_LLVM_BC &&
  5080. (C.getArgs().hasArg(options::OPT_emit_llvm) ||
  5081. IsAMDRDCInCompilePhase(JA, C.getArgs())))
  5082. Suffixed += ".tmp";
  5083. Suffixed += '.';
  5084. Suffixed += Suffix;
  5085. NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str());
  5086. }
  5087. // Prepend object file path if -save-temps=obj
  5088. if (!AtTopLevel && isSaveTempsObj() && C.getArgs().hasArg(options::OPT_o) &&
  5089. JA.getType() != types::TY_PCH) {
  5090. Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o);
  5091. SmallString<128> TempPath(FinalOutput->getValue());
  5092. llvm::sys::path::remove_filename(TempPath);
  5093. StringRef OutputFileName = llvm::sys::path::filename(NamedOutput);
  5094. llvm::sys::path::append(TempPath, OutputFileName);
  5095. NamedOutput = C.getArgs().MakeArgString(TempPath.c_str());
  5096. }
  5097. // If we're saving temps and the temp file conflicts with the input file,
  5098. // then avoid overwriting input file.
  5099. if (!AtTopLevel && isSaveTempsEnabled() && NamedOutput == BaseName) {
  5100. bool SameFile = false;
  5101. SmallString<256> Result;
  5102. llvm::sys::fs::current_path(Result);
  5103. llvm::sys::path::append(Result, BaseName);
  5104. llvm::sys::fs::equivalent(BaseInput, Result.c_str(), SameFile);
  5105. // Must share the same path to conflict.
  5106. if (SameFile) {
  5107. StringRef Name = llvm::sys::path::filename(BaseInput);
  5108. std::pair<StringRef, StringRef> Split = Name.split('.');
  5109. std::string TmpName = GetTemporaryPath(
  5110. Split.first, types::getTypeTempSuffix(JA.getType(), IsCLMode()));
  5111. return C.addTempFile(C.getArgs().MakeArgString(TmpName));
  5112. }
  5113. }
  5114. // As an annoying special case, PCH generation doesn't strip the pathname.
  5115. if (JA.getType() == types::TY_PCH && !IsCLMode()) {
  5116. llvm::sys::path::remove_filename(BasePath);
  5117. if (BasePath.empty())
  5118. BasePath = NamedOutput;
  5119. else
  5120. llvm::sys::path::append(BasePath, NamedOutput);
  5121. return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()), &JA);
  5122. }
  5123. return C.addResultFile(NamedOutput, &JA);
  5124. }
  5125. std::string Driver::GetFilePath(StringRef Name, const ToolChain &TC) const {
  5126. // Search for Name in a list of paths.
  5127. auto SearchPaths = [&](const llvm::SmallVectorImpl<std::string> &P)
  5128. -> std::optional<std::string> {
  5129. // Respect a limited subset of the '-Bprefix' functionality in GCC by
  5130. // attempting to use this prefix when looking for file paths.
  5131. for (const auto &Dir : P) {
  5132. if (Dir.empty())
  5133. continue;
  5134. SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir);
  5135. llvm::sys::path::append(P, Name);
  5136. if (llvm::sys::fs::exists(Twine(P)))
  5137. return std::string(P);
  5138. }
  5139. return std::nullopt;
  5140. };
  5141. if (auto P = SearchPaths(PrefixDirs))
  5142. return *P;
  5143. SmallString<128> R(ResourceDir);
  5144. llvm::sys::path::append(R, Name);
  5145. if (llvm::sys::fs::exists(Twine(R)))
  5146. return std::string(R.str());
  5147. SmallString<128> P(TC.getCompilerRTPath());
  5148. llvm::sys::path::append(P, Name);
  5149. if (llvm::sys::fs::exists(Twine(P)))
  5150. return std::string(P.str());
  5151. SmallString<128> D(Dir);
  5152. llvm::sys::path::append(D, "..", Name);
  5153. if (llvm::sys::fs::exists(Twine(D)))
  5154. return std::string(D.str());
  5155. if (auto P = SearchPaths(TC.getLibraryPaths()))
  5156. return *P;
  5157. if (auto P = SearchPaths(TC.getFilePaths()))
  5158. return *P;
  5159. return std::string(Name);
  5160. }
  5161. void Driver::generatePrefixedToolNames(
  5162. StringRef Tool, const ToolChain &TC,
  5163. SmallVectorImpl<std::string> &Names) const {
  5164. // FIXME: Needs a better variable than TargetTriple
  5165. Names.emplace_back((TargetTriple + "-" + Tool).str());
  5166. Names.emplace_back(Tool);
  5167. }
  5168. static bool ScanDirForExecutable(SmallString<128> &Dir, StringRef Name) {
  5169. llvm::sys::path::append(Dir, Name);
  5170. if (llvm::sys::fs::can_execute(Twine(Dir)))
  5171. return true;
  5172. llvm::sys::path::remove_filename(Dir);
  5173. return false;
  5174. }
  5175. std::string Driver::GetProgramPath(StringRef Name, const ToolChain &TC) const {
  5176. SmallVector<std::string, 2> TargetSpecificExecutables;
  5177. generatePrefixedToolNames(Name, TC, TargetSpecificExecutables);
  5178. // Respect a limited subset of the '-Bprefix' functionality in GCC by
  5179. // attempting to use this prefix when looking for program paths.
  5180. for (const auto &PrefixDir : PrefixDirs) {
  5181. if (llvm::sys::fs::is_directory(PrefixDir)) {
  5182. SmallString<128> P(PrefixDir);
  5183. if (ScanDirForExecutable(P, Name))
  5184. return std::string(P.str());
  5185. } else {
  5186. SmallString<128> P((PrefixDir + Name).str());
  5187. if (llvm::sys::fs::can_execute(Twine(P)))
  5188. return std::string(P.str());
  5189. }
  5190. }
  5191. const ToolChain::path_list &List = TC.getProgramPaths();
  5192. for (const auto &TargetSpecificExecutable : TargetSpecificExecutables) {
  5193. // For each possible name of the tool look for it in
  5194. // program paths first, then the path.
  5195. // Higher priority names will be first, meaning that
  5196. // a higher priority name in the path will be found
  5197. // instead of a lower priority name in the program path.
  5198. // E.g. <triple>-gcc on the path will be found instead
  5199. // of gcc in the program path
  5200. for (const auto &Path : List) {
  5201. SmallString<128> P(Path);
  5202. if (ScanDirForExecutable(P, TargetSpecificExecutable))
  5203. return std::string(P.str());
  5204. }
  5205. // Fall back to the path
  5206. if (llvm::ErrorOr<std::string> P =
  5207. llvm::sys::findProgramByName(TargetSpecificExecutable))
  5208. return *P;
  5209. }
  5210. return std::string(Name);
  5211. }
  5212. std::string Driver::GetTemporaryPath(StringRef Prefix, StringRef Suffix) const {
  5213. SmallString<128> Path;
  5214. std::error_code EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, Path);
  5215. if (EC) {
  5216. Diag(clang::diag::err_unable_to_make_temp) << EC.message();
  5217. return "";
  5218. }
  5219. return std::string(Path.str());
  5220. }
  5221. std::string Driver::GetTemporaryDirectory(StringRef Prefix) const {
  5222. SmallString<128> Path;
  5223. std::error_code EC = llvm::sys::fs::createUniqueDirectory(Prefix, Path);
  5224. if (EC) {
  5225. Diag(clang::diag::err_unable_to_make_temp) << EC.message();
  5226. return "";
  5227. }
  5228. return std::string(Path.str());
  5229. }
  5230. std::string Driver::GetClPchPath(Compilation &C, StringRef BaseName) const {
  5231. SmallString<128> Output;
  5232. if (Arg *FpArg = C.getArgs().getLastArg(options::OPT__SLASH_Fp)) {
  5233. // FIXME: If anybody needs it, implement this obscure rule:
  5234. // "If you specify a directory without a file name, the default file name
  5235. // is VCx0.pch., where x is the major version of Visual C++ in use."
  5236. Output = FpArg->getValue();
  5237. // "If you do not specify an extension as part of the path name, an
  5238. // extension of .pch is assumed. "
  5239. if (!llvm::sys::path::has_extension(Output))
  5240. Output += ".pch";
  5241. } else {
  5242. if (Arg *YcArg = C.getArgs().getLastArg(options::OPT__SLASH_Yc))
  5243. Output = YcArg->getValue();
  5244. if (Output.empty())
  5245. Output = BaseName;
  5246. llvm::sys::path::replace_extension(Output, ".pch");
  5247. }
  5248. return std::string(Output.str());
  5249. }
  5250. const ToolChain &Driver::getToolChain(const ArgList &Args,
  5251. const llvm::Triple &Target) const {
  5252. auto &TC = ToolChains[Target.str()];
  5253. if (!TC) {
  5254. switch (Target.getOS()) {
  5255. case llvm::Triple::AIX:
  5256. TC = std::make_unique<toolchains::AIX>(*this, Target, Args);
  5257. break;
  5258. case llvm::Triple::Haiku:
  5259. TC = std::make_unique<toolchains::Haiku>(*this, Target, Args);
  5260. break;
  5261. case llvm::Triple::Ananas:
  5262. TC = std::make_unique<toolchains::Ananas>(*this, Target, Args);
  5263. break;
  5264. case llvm::Triple::CloudABI:
  5265. TC = std::make_unique<toolchains::CloudABI>(*this, Target, Args);
  5266. break;
  5267. case llvm::Triple::Darwin:
  5268. case llvm::Triple::MacOSX:
  5269. case llvm::Triple::IOS:
  5270. case llvm::Triple::TvOS:
  5271. case llvm::Triple::WatchOS:
  5272. case llvm::Triple::DriverKit:
  5273. TC = std::make_unique<toolchains::DarwinClang>(*this, Target, Args);
  5274. break;
  5275. case llvm::Triple::DragonFly:
  5276. TC = std::make_unique<toolchains::DragonFly>(*this, Target, Args);
  5277. break;
  5278. case llvm::Triple::OpenBSD:
  5279. TC = std::make_unique<toolchains::OpenBSD>(*this, Target, Args);
  5280. break;
  5281. case llvm::Triple::NetBSD:
  5282. TC = std::make_unique<toolchains::NetBSD>(*this, Target, Args);
  5283. break;
  5284. case llvm::Triple::FreeBSD:
  5285. if (Target.isPPC())
  5286. TC = std::make_unique<toolchains::PPCFreeBSDToolChain>(*this, Target,
  5287. Args);
  5288. else
  5289. TC = std::make_unique<toolchains::FreeBSD>(*this, Target, Args);
  5290. break;
  5291. case llvm::Triple::Minix:
  5292. TC = std::make_unique<toolchains::Minix>(*this, Target, Args);
  5293. break;
  5294. case llvm::Triple::Linux:
  5295. case llvm::Triple::ELFIAMCU:
  5296. if (Target.getArch() == llvm::Triple::hexagon)
  5297. TC = std::make_unique<toolchains::HexagonToolChain>(*this, Target,
  5298. Args);
  5299. else if ((Target.getVendor() == llvm::Triple::MipsTechnologies) &&
  5300. !Target.hasEnvironment())
  5301. TC = std::make_unique<toolchains::MipsLLVMToolChain>(*this, Target,
  5302. Args);
  5303. else if (Target.isPPC())
  5304. TC = std::make_unique<toolchains::PPCLinuxToolChain>(*this, Target,
  5305. Args);
  5306. else if (Target.getArch() == llvm::Triple::ve)
  5307. TC = std::make_unique<toolchains::VEToolChain>(*this, Target, Args);
  5308. else
  5309. TC = std::make_unique<toolchains::Linux>(*this, Target, Args);
  5310. break;
  5311. case llvm::Triple::NaCl:
  5312. TC = std::make_unique<toolchains::NaClToolChain>(*this, Target, Args);
  5313. break;
  5314. case llvm::Triple::Fuchsia:
  5315. TC = std::make_unique<toolchains::Fuchsia>(*this, Target, Args);
  5316. break;
  5317. case llvm::Triple::Solaris:
  5318. TC = std::make_unique<toolchains::Solaris>(*this, Target, Args);
  5319. break;
  5320. case llvm::Triple::CUDA:
  5321. TC = std::make_unique<toolchains::NVPTXToolChain>(*this, Target, Args);
  5322. break;
  5323. case llvm::Triple::AMDHSA:
  5324. TC = std::make_unique<toolchains::ROCMToolChain>(*this, Target, Args);
  5325. break;
  5326. case llvm::Triple::AMDPAL:
  5327. case llvm::Triple::Mesa3D:
  5328. TC = std::make_unique<toolchains::AMDGPUToolChain>(*this, Target, Args);
  5329. break;
  5330. case llvm::Triple::Win32:
  5331. switch (Target.getEnvironment()) {
  5332. default:
  5333. if (Target.isOSBinFormatELF())
  5334. TC = std::make_unique<toolchains::Generic_ELF>(*this, Target, Args);
  5335. else if (Target.isOSBinFormatMachO())
  5336. TC = std::make_unique<toolchains::MachO>(*this, Target, Args);
  5337. else
  5338. TC = std::make_unique<toolchains::Generic_GCC>(*this, Target, Args);
  5339. break;
  5340. case llvm::Triple::GNU:
  5341. TC = std::make_unique<toolchains::MinGW>(*this, Target, Args);
  5342. break;
  5343. case llvm::Triple::Itanium:
  5344. TC = std::make_unique<toolchains::CrossWindowsToolChain>(*this, Target,
  5345. Args);
  5346. break;
  5347. case llvm::Triple::MSVC:
  5348. case llvm::Triple::UnknownEnvironment:
  5349. if (Args.getLastArgValue(options::OPT_fuse_ld_EQ)
  5350. .startswith_insensitive("bfd"))
  5351. TC = std::make_unique<toolchains::CrossWindowsToolChain>(
  5352. *this, Target, Args);
  5353. else
  5354. TC =
  5355. std::make_unique<toolchains::MSVCToolChain>(*this, Target, Args);
  5356. break;
  5357. }
  5358. break;
  5359. case llvm::Triple::PS4:
  5360. TC = std::make_unique<toolchains::PS4CPU>(*this, Target, Args);
  5361. break;
  5362. case llvm::Triple::PS5:
  5363. TC = std::make_unique<toolchains::PS5CPU>(*this, Target, Args);
  5364. break;
  5365. case llvm::Triple::Contiki:
  5366. TC = std::make_unique<toolchains::Contiki>(*this, Target, Args);
  5367. break;
  5368. case llvm::Triple::Hurd:
  5369. TC = std::make_unique<toolchains::Hurd>(*this, Target, Args);
  5370. break;
  5371. case llvm::Triple::ZOS:
  5372. TC = std::make_unique<toolchains::ZOS>(*this, Target, Args);
  5373. break;
  5374. case llvm::Triple::ShaderModel:
  5375. TC = std::make_unique<toolchains::HLSLToolChain>(*this, Target, Args);
  5376. break;
  5377. default:
  5378. // Of these targets, Hexagon is the only one that might have
  5379. // an OS of Linux, in which case it got handled above already.
  5380. switch (Target.getArch()) {
  5381. case llvm::Triple::tce:
  5382. TC = std::make_unique<toolchains::TCEToolChain>(*this, Target, Args);
  5383. break;
  5384. case llvm::Triple::tcele:
  5385. TC = std::make_unique<toolchains::TCELEToolChain>(*this, Target, Args);
  5386. break;
  5387. case llvm::Triple::hexagon:
  5388. TC = std::make_unique<toolchains::HexagonToolChain>(*this, Target,
  5389. Args);
  5390. break;
  5391. case llvm::Triple::lanai:
  5392. TC = std::make_unique<toolchains::LanaiToolChain>(*this, Target, Args);
  5393. break;
  5394. case llvm::Triple::xcore:
  5395. TC = std::make_unique<toolchains::XCoreToolChain>(*this, Target, Args);
  5396. break;
  5397. case llvm::Triple::wasm32:
  5398. case llvm::Triple::wasm64:
  5399. TC = std::make_unique<toolchains::WebAssembly>(*this, Target, Args);
  5400. break;
  5401. case llvm::Triple::avr:
  5402. TC = std::make_unique<toolchains::AVRToolChain>(*this, Target, Args);
  5403. break;
  5404. case llvm::Triple::msp430:
  5405. TC =
  5406. std::make_unique<toolchains::MSP430ToolChain>(*this, Target, Args);
  5407. break;
  5408. case llvm::Triple::riscv32:
  5409. case llvm::Triple::riscv64:
  5410. if (toolchains::RISCVToolChain::hasGCCToolchain(*this, Args))
  5411. TC =
  5412. std::make_unique<toolchains::RISCVToolChain>(*this, Target, Args);
  5413. else
  5414. TC = std::make_unique<toolchains::BareMetal>(*this, Target, Args);
  5415. break;
  5416. case llvm::Triple::ve:
  5417. TC = std::make_unique<toolchains::VEToolChain>(*this, Target, Args);
  5418. break;
  5419. case llvm::Triple::spirv32:
  5420. case llvm::Triple::spirv64:
  5421. TC = std::make_unique<toolchains::SPIRVToolChain>(*this, Target, Args);
  5422. break;
  5423. case llvm::Triple::csky:
  5424. TC = std::make_unique<toolchains::CSKYToolChain>(*this, Target, Args);
  5425. break;
  5426. default:
  5427. if (Target.getVendor() == llvm::Triple::Myriad)
  5428. TC = std::make_unique<toolchains::MyriadToolChain>(*this, Target,
  5429. Args);
  5430. else if (toolchains::BareMetal::handlesTarget(Target))
  5431. TC = std::make_unique<toolchains::BareMetal>(*this, Target, Args);
  5432. else if (Target.isOSBinFormatELF())
  5433. TC = std::make_unique<toolchains::Generic_ELF>(*this, Target, Args);
  5434. else if (Target.isOSBinFormatMachO())
  5435. TC = std::make_unique<toolchains::MachO>(*this, Target, Args);
  5436. else
  5437. TC = std::make_unique<toolchains::Generic_GCC>(*this, Target, Args);
  5438. }
  5439. }
  5440. }
  5441. return *TC;
  5442. }
  5443. const ToolChain &Driver::getOffloadingDeviceToolChain(
  5444. const ArgList &Args, const llvm::Triple &Target, const ToolChain &HostTC,
  5445. const Action::OffloadKind &TargetDeviceOffloadKind) const {
  5446. // Use device / host triples as the key into the ToolChains map because the
  5447. // device ToolChain we create depends on both.
  5448. auto &TC = ToolChains[Target.str() + "/" + HostTC.getTriple().str()];
  5449. if (!TC) {
  5450. // Categorized by offload kind > arch rather than OS > arch like
  5451. // the normal getToolChain call, as it seems a reasonable way to categorize
  5452. // things.
  5453. switch (TargetDeviceOffloadKind) {
  5454. case Action::OFK_HIP: {
  5455. if (Target.getArch() == llvm::Triple::amdgcn &&
  5456. Target.getVendor() == llvm::Triple::AMD &&
  5457. Target.getOS() == llvm::Triple::AMDHSA)
  5458. TC = std::make_unique<toolchains::HIPAMDToolChain>(*this, Target,
  5459. HostTC, Args);
  5460. else if (Target.getArch() == llvm::Triple::spirv64 &&
  5461. Target.getVendor() == llvm::Triple::UnknownVendor &&
  5462. Target.getOS() == llvm::Triple::UnknownOS)
  5463. TC = std::make_unique<toolchains::HIPSPVToolChain>(*this, Target,
  5464. HostTC, Args);
  5465. break;
  5466. }
  5467. default:
  5468. break;
  5469. }
  5470. }
  5471. return *TC;
  5472. }
  5473. bool Driver::ShouldUseClangCompiler(const JobAction &JA) const {
  5474. // Say "no" if there is not exactly one input of a type clang understands.
  5475. if (JA.size() != 1 ||
  5476. !types::isAcceptedByClang((*JA.input_begin())->getType()))
  5477. return false;
  5478. // And say "no" if this is not a kind of action clang understands.
  5479. if (!isa<PreprocessJobAction>(JA) && !isa<PrecompileJobAction>(JA) &&
  5480. !isa<CompileJobAction>(JA) && !isa<BackendJobAction>(JA) &&
  5481. !isa<ExtractAPIJobAction>(JA))
  5482. return false;
  5483. return true;
  5484. }
  5485. bool Driver::ShouldUseFlangCompiler(const JobAction &JA) const {
  5486. // Say "no" if there is not exactly one input of a type flang understands.
  5487. if (JA.size() != 1 ||
  5488. !types::isAcceptedByFlang((*JA.input_begin())->getType()))
  5489. return false;
  5490. // And say "no" if this is not a kind of action flang understands.
  5491. if (!isa<PreprocessJobAction>(JA) && !isa<CompileJobAction>(JA) &&
  5492. !isa<BackendJobAction>(JA))
  5493. return false;
  5494. return true;
  5495. }
  5496. bool Driver::ShouldEmitStaticLibrary(const ArgList &Args) const {
  5497. // Only emit static library if the flag is set explicitly.
  5498. if (Args.hasArg(options::OPT_emit_static_lib))
  5499. return true;
  5500. return false;
  5501. }
  5502. /// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the
  5503. /// grouped values as integers. Numbers which are not provided are set to 0.
  5504. ///
  5505. /// \return True if the entire string was parsed (9.2), or all groups were
  5506. /// parsed (10.3.5extrastuff).
  5507. bool Driver::GetReleaseVersion(StringRef Str, unsigned &Major, unsigned &Minor,
  5508. unsigned &Micro, bool &HadExtra) {
  5509. HadExtra = false;
  5510. Major = Minor = Micro = 0;
  5511. if (Str.empty())
  5512. return false;
  5513. if (Str.consumeInteger(10, Major))
  5514. return false;
  5515. if (Str.empty())
  5516. return true;
  5517. if (Str[0] != '.')
  5518. return false;
  5519. Str = Str.drop_front(1);
  5520. if (Str.consumeInteger(10, Minor))
  5521. return false;
  5522. if (Str.empty())
  5523. return true;
  5524. if (Str[0] != '.')
  5525. return false;
  5526. Str = Str.drop_front(1);
  5527. if (Str.consumeInteger(10, Micro))
  5528. return false;
  5529. if (!Str.empty())
  5530. HadExtra = true;
  5531. return true;
  5532. }
  5533. /// Parse digits from a string \p Str and fulfill \p Digits with
  5534. /// the parsed numbers. This method assumes that the max number of
  5535. /// digits to look for is equal to Digits.size().
  5536. ///
  5537. /// \return True if the entire string was parsed and there are
  5538. /// no extra characters remaining at the end.
  5539. bool Driver::GetReleaseVersion(StringRef Str,
  5540. MutableArrayRef<unsigned> Digits) {
  5541. if (Str.empty())
  5542. return false;
  5543. unsigned CurDigit = 0;
  5544. while (CurDigit < Digits.size()) {
  5545. unsigned Digit;
  5546. if (Str.consumeInteger(10, Digit))
  5547. return false;
  5548. Digits[CurDigit] = Digit;
  5549. if (Str.empty())
  5550. return true;
  5551. if (Str[0] != '.')
  5552. return false;
  5553. Str = Str.drop_front(1);
  5554. CurDigit++;
  5555. }
  5556. // More digits than requested, bail out...
  5557. return false;
  5558. }
  5559. std::pair<unsigned, unsigned>
  5560. Driver::getIncludeExcludeOptionFlagMasks(bool IsClCompatMode) const {
  5561. unsigned IncludedFlagsBitmask = 0;
  5562. unsigned ExcludedFlagsBitmask = options::NoDriverOption;
  5563. if (IsClCompatMode) {
  5564. // Include CL and Core options.
  5565. IncludedFlagsBitmask |= options::CLOption;
  5566. IncludedFlagsBitmask |= options::CLDXCOption;
  5567. IncludedFlagsBitmask |= options::CoreOption;
  5568. } else {
  5569. ExcludedFlagsBitmask |= options::CLOption;
  5570. }
  5571. if (IsDXCMode()) {
  5572. // Include DXC and Core options.
  5573. IncludedFlagsBitmask |= options::DXCOption;
  5574. IncludedFlagsBitmask |= options::CLDXCOption;
  5575. IncludedFlagsBitmask |= options::CoreOption;
  5576. } else {
  5577. ExcludedFlagsBitmask |= options::DXCOption;
  5578. }
  5579. if (!IsClCompatMode && !IsDXCMode())
  5580. ExcludedFlagsBitmask |= options::CLDXCOption;
  5581. return std::make_pair(IncludedFlagsBitmask, ExcludedFlagsBitmask);
  5582. }
  5583. const char *Driver::getExecutableForDriverMode(DriverMode Mode) {
  5584. switch (Mode) {
  5585. case GCCMode:
  5586. return "clang";
  5587. case GXXMode:
  5588. return "clang++";
  5589. case CPPMode:
  5590. return "clang-cpp";
  5591. case CLMode:
  5592. return "clang-cl";
  5593. case FlangMode:
  5594. return "flang";
  5595. case DXCMode:
  5596. return "clang-dxc";
  5597. }
  5598. llvm_unreachable("Unhandled Mode");
  5599. }
  5600. bool clang::driver::isOptimizationLevelFast(const ArgList &Args) {
  5601. return Args.hasFlag(options::OPT_Ofast, options::OPT_O_Group, false);
  5602. }
  5603. bool clang::driver::willEmitRemarks(const ArgList &Args) {
  5604. // -fsave-optimization-record enables it.
  5605. if (Args.hasFlag(options::OPT_fsave_optimization_record,
  5606. options::OPT_fno_save_optimization_record, false))
  5607. return true;
  5608. // -fsave-optimization-record=<format> enables it as well.
  5609. if (Args.hasFlag(options::OPT_fsave_optimization_record_EQ,
  5610. options::OPT_fno_save_optimization_record, false))
  5611. return true;
  5612. // -foptimization-record-file alone enables it too.
  5613. if (Args.hasFlag(options::OPT_foptimization_record_file_EQ,
  5614. options::OPT_fno_save_optimization_record, false))
  5615. return true;
  5616. // -foptimization-record-passes alone enables it too.
  5617. if (Args.hasFlag(options::OPT_foptimization_record_passes_EQ,
  5618. options::OPT_fno_save_optimization_record, false))
  5619. return true;
  5620. return false;
  5621. }
  5622. llvm::StringRef clang::driver::getDriverMode(StringRef ProgName,
  5623. ArrayRef<const char *> Args) {
  5624. static const std::string OptName =
  5625. getDriverOptTable().getOption(options::OPT_driver_mode).getPrefixedName();
  5626. llvm::StringRef Opt;
  5627. for (StringRef Arg : Args) {
  5628. if (!Arg.startswith(OptName))
  5629. continue;
  5630. Opt = Arg;
  5631. }
  5632. if (Opt.empty())
  5633. Opt = ToolChain::getTargetAndModeFromProgramName(ProgName).DriverMode;
  5634. return Opt.consume_front(OptName) ? Opt : "";
  5635. }
  5636. bool driver::IsClangCL(StringRef DriverMode) { return DriverMode.equals("cl"); }