SemaOpenMP.cpp 536 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505
  1. //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. /// \file
  10. /// This file implements semantic analysis for OpenMP directives and
  11. /// clauses.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "TreeTransform.h"
  15. #include "clang/AST/ASTContext.h"
  16. #include "clang/AST/ASTMutationListener.h"
  17. #include "clang/AST/CXXInheritance.h"
  18. #include "clang/AST/Decl.h"
  19. #include "clang/AST/DeclCXX.h"
  20. #include "clang/AST/DeclOpenMP.h"
  21. #include "clang/AST/StmtCXX.h"
  22. #include "clang/AST/StmtOpenMP.h"
  23. #include "clang/AST/StmtVisitor.h"
  24. #include "clang/Basic/OpenMPKinds.h"
  25. #include "clang/Sema/Initialization.h"
  26. #include "clang/Sema/Lookup.h"
  27. #include "clang/Sema/Scope.h"
  28. #include "clang/Sema/ScopeInfo.h"
  29. #include "clang/Sema/SemaInternal.h"
  30. #include "llvm/ADT/PointerEmbeddedInt.h"
  31. using namespace clang;
  32. //===----------------------------------------------------------------------===//
  33. // Stack of data-sharing attributes for variables
  34. //===----------------------------------------------------------------------===//
  35. static const Expr *checkMapClauseExpressionBase(
  36. Sema &SemaRef, Expr *E,
  37. OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
  38. OpenMPClauseKind CKind, bool NoDiagnose);
  39. namespace {
  40. /// Default data sharing attributes, which can be applied to directive.
  41. enum DefaultDataSharingAttributes {
  42. DSA_unspecified = 0, /// Data sharing attribute not specified.
  43. DSA_none = 1 << 0, /// Default data sharing attribute 'none'.
  44. DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'.
  45. };
  46. /// Attributes of the defaultmap clause.
  47. enum DefaultMapAttributes {
  48. DMA_unspecified, /// Default mapping is not specified.
  49. DMA_tofrom_scalar, /// Default mapping is 'tofrom:scalar'.
  50. };
  51. /// Stack for tracking declarations used in OpenMP directives and
  52. /// clauses and their data-sharing attributes.
  53. class DSAStackTy {
  54. public:
  55. struct DSAVarData {
  56. OpenMPDirectiveKind DKind = OMPD_unknown;
  57. OpenMPClauseKind CKind = OMPC_unknown;
  58. const Expr *RefExpr = nullptr;
  59. DeclRefExpr *PrivateCopy = nullptr;
  60. SourceLocation ImplicitDSALoc;
  61. DSAVarData() = default;
  62. DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
  63. const Expr *RefExpr, DeclRefExpr *PrivateCopy,
  64. SourceLocation ImplicitDSALoc)
  65. : DKind(DKind), CKind(CKind), RefExpr(RefExpr),
  66. PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {}
  67. };
  68. using OperatorOffsetTy =
  69. llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>;
  70. using DoacrossDependMapTy =
  71. llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>;
  72. private:
  73. struct DSAInfo {
  74. OpenMPClauseKind Attributes = OMPC_unknown;
  75. /// Pointer to a reference expression and a flag which shows that the
  76. /// variable is marked as lastprivate(true) or not (false).
  77. llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
  78. DeclRefExpr *PrivateCopy = nullptr;
  79. };
  80. using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>;
  81. using AlignedMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>;
  82. using LCDeclInfo = std::pair<unsigned, VarDecl *>;
  83. using LoopControlVariablesMapTy =
  84. llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>;
  85. /// Struct that associates a component with the clause kind where they are
  86. /// found.
  87. struct MappedExprComponentTy {
  88. OMPClauseMappableExprCommon::MappableExprComponentLists Components;
  89. OpenMPClauseKind Kind = OMPC_unknown;
  90. };
  91. using MappedExprComponentsTy =
  92. llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
  93. using CriticalsWithHintsTy =
  94. llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
  95. struct ReductionData {
  96. using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>;
  97. SourceRange ReductionRange;
  98. llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
  99. ReductionData() = default;
  100. void set(BinaryOperatorKind BO, SourceRange RR) {
  101. ReductionRange = RR;
  102. ReductionOp = BO;
  103. }
  104. void set(const Expr *RefExpr, SourceRange RR) {
  105. ReductionRange = RR;
  106. ReductionOp = RefExpr;
  107. }
  108. };
  109. using DeclReductionMapTy =
  110. llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>;
  111. struct SharingMapTy {
  112. DeclSAMapTy SharingMap;
  113. DeclReductionMapTy ReductionMap;
  114. AlignedMapTy AlignedMap;
  115. MappedExprComponentsTy MappedExprComponents;
  116. LoopControlVariablesMapTy LCVMap;
  117. DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
  118. SourceLocation DefaultAttrLoc;
  119. DefaultMapAttributes DefaultMapAttr = DMA_unspecified;
  120. SourceLocation DefaultMapAttrLoc;
  121. OpenMPDirectiveKind Directive = OMPD_unknown;
  122. DeclarationNameInfo DirectiveName;
  123. Scope *CurScope = nullptr;
  124. SourceLocation ConstructLoc;
  125. /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
  126. /// get the data (loop counters etc.) about enclosing loop-based construct.
  127. /// This data is required during codegen.
  128. DoacrossDependMapTy DoacrossDepends;
  129. /// first argument (Expr *) contains optional argument of the
  130. /// 'ordered' clause, the second one is true if the regions has 'ordered'
  131. /// clause, false otherwise.
  132. llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion;
  133. bool NowaitRegion = false;
  134. bool CancelRegion = false;
  135. unsigned AssociatedLoops = 1;
  136. SourceLocation InnerTeamsRegionLoc;
  137. /// Reference to the taskgroup task_reduction reference expression.
  138. Expr *TaskgroupReductionRef = nullptr;
  139. SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
  140. Scope *CurScope, SourceLocation Loc)
  141. : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
  142. ConstructLoc(Loc) {}
  143. SharingMapTy() = default;
  144. };
  145. using StackTy = SmallVector<SharingMapTy, 4>;
  146. /// Stack of used declaration and their data-sharing attributes.
  147. DeclSAMapTy Threadprivates;
  148. const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
  149. SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
  150. /// true, if check for DSA must be from parent directive, false, if
  151. /// from current directive.
  152. OpenMPClauseKind ClauseKindMode = OMPC_unknown;
  153. Sema &SemaRef;
  154. bool ForceCapturing = false;
  155. CriticalsWithHintsTy Criticals;
  156. using iterator = StackTy::const_reverse_iterator;
  157. DSAVarData getDSA(iterator &Iter, ValueDecl *D) const;
  158. /// Checks if the variable is a local for OpenMP region.
  159. bool isOpenMPLocal(VarDecl *D, iterator Iter) const;
  160. bool isStackEmpty() const {
  161. return Stack.empty() ||
  162. Stack.back().second != CurrentNonCapturingFunctionScope ||
  163. Stack.back().first.empty();
  164. }
  165. /// Vector of previously declared requires directives
  166. SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
  167. public:
  168. explicit DSAStackTy(Sema &S) : SemaRef(S) {}
  169. bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
  170. OpenMPClauseKind getClauseParsingMode() const {
  171. assert(isClauseParsingMode() && "Must be in clause parsing mode.");
  172. return ClauseKindMode;
  173. }
  174. void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
  175. bool isForceVarCapturing() const { return ForceCapturing; }
  176. void setForceVarCapturing(bool V) { ForceCapturing = V; }
  177. void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
  178. Scope *CurScope, SourceLocation Loc) {
  179. if (Stack.empty() ||
  180. Stack.back().second != CurrentNonCapturingFunctionScope)
  181. Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
  182. Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
  183. Stack.back().first.back().DefaultAttrLoc = Loc;
  184. }
  185. void pop() {
  186. assert(!Stack.back().first.empty() &&
  187. "Data-sharing attributes stack is empty!");
  188. Stack.back().first.pop_back();
  189. }
  190. /// Start new OpenMP region stack in new non-capturing function.
  191. void pushFunction() {
  192. const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
  193. assert(!isa<CapturingScopeInfo>(CurFnScope));
  194. CurrentNonCapturingFunctionScope = CurFnScope;
  195. }
  196. /// Pop region stack for non-capturing function.
  197. void popFunction(const FunctionScopeInfo *OldFSI) {
  198. if (!Stack.empty() && Stack.back().second == OldFSI) {
  199. assert(Stack.back().first.empty());
  200. Stack.pop_back();
  201. }
  202. CurrentNonCapturingFunctionScope = nullptr;
  203. for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
  204. if (!isa<CapturingScopeInfo>(FSI)) {
  205. CurrentNonCapturingFunctionScope = FSI;
  206. break;
  207. }
  208. }
  209. }
  210. void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
  211. Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint);
  212. }
  213. const std::pair<const OMPCriticalDirective *, llvm::APSInt>
  214. getCriticalWithHint(const DeclarationNameInfo &Name) const {
  215. auto I = Criticals.find(Name.getAsString());
  216. if (I != Criticals.end())
  217. return I->second;
  218. return std::make_pair(nullptr, llvm::APSInt());
  219. }
  220. /// If 'aligned' declaration for given variable \a D was not seen yet,
  221. /// add it and return NULL; otherwise return previous occurrence's expression
  222. /// for diagnostics.
  223. const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
  224. /// Register specified variable as loop control variable.
  225. void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
  226. /// Check if the specified variable is a loop control variable for
  227. /// current region.
  228. /// \return The index of the loop control variable in the list of associated
  229. /// for-loops (from outer to inner).
  230. const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
  231. /// Check if the specified variable is a loop control variable for
  232. /// parent region.
  233. /// \return The index of the loop control variable in the list of associated
  234. /// for-loops (from outer to inner).
  235. const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
  236. /// Get the loop control variable for the I-th loop (or nullptr) in
  237. /// parent directive.
  238. const ValueDecl *getParentLoopControlVariable(unsigned I) const;
  239. /// Adds explicit data sharing attribute to the specified declaration.
  240. void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
  241. DeclRefExpr *PrivateCopy = nullptr);
  242. /// Adds additional information for the reduction items with the reduction id
  243. /// represented as an operator.
  244. void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
  245. BinaryOperatorKind BOK);
  246. /// Adds additional information for the reduction items with the reduction id
  247. /// represented as reduction identifier.
  248. void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
  249. const Expr *ReductionRef);
  250. /// Returns the location and reduction operation from the innermost parent
  251. /// region for the given \p D.
  252. const DSAVarData
  253. getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
  254. BinaryOperatorKind &BOK,
  255. Expr *&TaskgroupDescriptor) const;
  256. /// Returns the location and reduction operation from the innermost parent
  257. /// region for the given \p D.
  258. const DSAVarData
  259. getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
  260. const Expr *&ReductionRef,
  261. Expr *&TaskgroupDescriptor) const;
  262. /// Return reduction reference expression for the current taskgroup.
  263. Expr *getTaskgroupReductionRef() const {
  264. assert(Stack.back().first.back().Directive == OMPD_taskgroup &&
  265. "taskgroup reference expression requested for non taskgroup "
  266. "directive.");
  267. return Stack.back().first.back().TaskgroupReductionRef;
  268. }
  269. /// Checks if the given \p VD declaration is actually a taskgroup reduction
  270. /// descriptor variable at the \p Level of OpenMP regions.
  271. bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
  272. return Stack.back().first[Level].TaskgroupReductionRef &&
  273. cast<DeclRefExpr>(Stack.back().first[Level].TaskgroupReductionRef)
  274. ->getDecl() == VD;
  275. }
  276. /// Returns data sharing attributes from top of the stack for the
  277. /// specified declaration.
  278. const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
  279. /// Returns data-sharing attributes for the specified declaration.
  280. const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
  281. /// Checks if the specified variables has data-sharing attributes which
  282. /// match specified \a CPred predicate in any directive which matches \a DPred
  283. /// predicate.
  284. const DSAVarData
  285. hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
  286. const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
  287. bool FromParent) const;
  288. /// Checks if the specified variables has data-sharing attributes which
  289. /// match specified \a CPred predicate in any innermost directive which
  290. /// matches \a DPred predicate.
  291. const DSAVarData
  292. hasInnermostDSA(ValueDecl *D,
  293. const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
  294. const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
  295. bool FromParent) const;
  296. /// Checks if the specified variables has explicit data-sharing
  297. /// attributes which match specified \a CPred predicate at the specified
  298. /// OpenMP region.
  299. bool hasExplicitDSA(const ValueDecl *D,
  300. const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
  301. unsigned Level, bool NotLastprivate = false) const;
  302. /// Returns true if the directive at level \Level matches in the
  303. /// specified \a DPred predicate.
  304. bool hasExplicitDirective(
  305. const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
  306. unsigned Level) const;
  307. /// Finds a directive which matches specified \a DPred predicate.
  308. bool hasDirective(
  309. const llvm::function_ref<bool(
  310. OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
  311. DPred,
  312. bool FromParent) const;
  313. /// Returns currently analyzed directive.
  314. OpenMPDirectiveKind getCurrentDirective() const {
  315. return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive;
  316. }
  317. /// Returns directive kind at specified level.
  318. OpenMPDirectiveKind getDirective(unsigned Level) const {
  319. assert(!isStackEmpty() && "No directive at specified level.");
  320. return Stack.back().first[Level].Directive;
  321. }
  322. /// Returns parent directive.
  323. OpenMPDirectiveKind getParentDirective() const {
  324. if (isStackEmpty() || Stack.back().first.size() == 1)
  325. return OMPD_unknown;
  326. return std::next(Stack.back().first.rbegin())->Directive;
  327. }
  328. /// Add requires decl to internal vector
  329. void addRequiresDecl(OMPRequiresDecl *RD) {
  330. RequiresDecls.push_back(RD);
  331. }
  332. /// Checks for a duplicate clause amongst previously declared requires
  333. /// directives
  334. bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
  335. bool IsDuplicate = false;
  336. for (OMPClause *CNew : ClauseList) {
  337. for (const OMPRequiresDecl *D : RequiresDecls) {
  338. for (const OMPClause *CPrev : D->clauselists()) {
  339. if (CNew->getClauseKind() == CPrev->getClauseKind()) {
  340. SemaRef.Diag(CNew->getBeginLoc(),
  341. diag::err_omp_requires_clause_redeclaration)
  342. << getOpenMPClauseName(CNew->getClauseKind());
  343. SemaRef.Diag(CPrev->getBeginLoc(),
  344. diag::note_omp_requires_previous_clause)
  345. << getOpenMPClauseName(CPrev->getClauseKind());
  346. IsDuplicate = true;
  347. }
  348. }
  349. }
  350. }
  351. return IsDuplicate;
  352. }
  353. /// Set default data sharing attribute to none.
  354. void setDefaultDSANone(SourceLocation Loc) {
  355. assert(!isStackEmpty());
  356. Stack.back().first.back().DefaultAttr = DSA_none;
  357. Stack.back().first.back().DefaultAttrLoc = Loc;
  358. }
  359. /// Set default data sharing attribute to shared.
  360. void setDefaultDSAShared(SourceLocation Loc) {
  361. assert(!isStackEmpty());
  362. Stack.back().first.back().DefaultAttr = DSA_shared;
  363. Stack.back().first.back().DefaultAttrLoc = Loc;
  364. }
  365. /// Set default data mapping attribute to 'tofrom:scalar'.
  366. void setDefaultDMAToFromScalar(SourceLocation Loc) {
  367. assert(!isStackEmpty());
  368. Stack.back().first.back().DefaultMapAttr = DMA_tofrom_scalar;
  369. Stack.back().first.back().DefaultMapAttrLoc = Loc;
  370. }
  371. DefaultDataSharingAttributes getDefaultDSA() const {
  372. return isStackEmpty() ? DSA_unspecified
  373. : Stack.back().first.back().DefaultAttr;
  374. }
  375. SourceLocation getDefaultDSALocation() const {
  376. return isStackEmpty() ? SourceLocation()
  377. : Stack.back().first.back().DefaultAttrLoc;
  378. }
  379. DefaultMapAttributes getDefaultDMA() const {
  380. return isStackEmpty() ? DMA_unspecified
  381. : Stack.back().first.back().DefaultMapAttr;
  382. }
  383. DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const {
  384. return Stack.back().first[Level].DefaultMapAttr;
  385. }
  386. SourceLocation getDefaultDMALocation() const {
  387. return isStackEmpty() ? SourceLocation()
  388. : Stack.back().first.back().DefaultMapAttrLoc;
  389. }
  390. /// Checks if the specified variable is a threadprivate.
  391. bool isThreadPrivate(VarDecl *D) {
  392. const DSAVarData DVar = getTopDSA(D, false);
  393. return isOpenMPThreadPrivate(DVar.CKind);
  394. }
  395. /// Marks current region as ordered (it has an 'ordered' clause).
  396. void setOrderedRegion(bool IsOrdered, const Expr *Param,
  397. OMPOrderedClause *Clause) {
  398. assert(!isStackEmpty());
  399. if (IsOrdered)
  400. Stack.back().first.back().OrderedRegion.emplace(Param, Clause);
  401. else
  402. Stack.back().first.back().OrderedRegion.reset();
  403. }
  404. /// Returns true, if region is ordered (has associated 'ordered' clause),
  405. /// false - otherwise.
  406. bool isOrderedRegion() const {
  407. if (isStackEmpty())
  408. return false;
  409. return Stack.back().first.rbegin()->OrderedRegion.hasValue();
  410. }
  411. /// Returns optional parameter for the ordered region.
  412. std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
  413. if (isStackEmpty() ||
  414. !Stack.back().first.rbegin()->OrderedRegion.hasValue())
  415. return std::make_pair(nullptr, nullptr);
  416. return Stack.back().first.rbegin()->OrderedRegion.getValue();
  417. }
  418. /// Returns true, if parent region is ordered (has associated
  419. /// 'ordered' clause), false - otherwise.
  420. bool isParentOrderedRegion() const {
  421. if (isStackEmpty() || Stack.back().first.size() == 1)
  422. return false;
  423. return std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue();
  424. }
  425. /// Returns optional parameter for the ordered region.
  426. std::pair<const Expr *, OMPOrderedClause *>
  427. getParentOrderedRegionParam() const {
  428. if (isStackEmpty() || Stack.back().first.size() == 1 ||
  429. !std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue())
  430. return std::make_pair(nullptr, nullptr);
  431. return std::next(Stack.back().first.rbegin())->OrderedRegion.getValue();
  432. }
  433. /// Marks current region as nowait (it has a 'nowait' clause).
  434. void setNowaitRegion(bool IsNowait = true) {
  435. assert(!isStackEmpty());
  436. Stack.back().first.back().NowaitRegion = IsNowait;
  437. }
  438. /// Returns true, if parent region is nowait (has associated
  439. /// 'nowait' clause), false - otherwise.
  440. bool isParentNowaitRegion() const {
  441. if (isStackEmpty() || Stack.back().first.size() == 1)
  442. return false;
  443. return std::next(Stack.back().first.rbegin())->NowaitRegion;
  444. }
  445. /// Marks parent region as cancel region.
  446. void setParentCancelRegion(bool Cancel = true) {
  447. if (!isStackEmpty() && Stack.back().first.size() > 1) {
  448. auto &StackElemRef = *std::next(Stack.back().first.rbegin());
  449. StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel;
  450. }
  451. }
  452. /// Return true if current region has inner cancel construct.
  453. bool isCancelRegion() const {
  454. return isStackEmpty() ? false : Stack.back().first.back().CancelRegion;
  455. }
  456. /// Set collapse value for the region.
  457. void setAssociatedLoops(unsigned Val) {
  458. assert(!isStackEmpty());
  459. Stack.back().first.back().AssociatedLoops = Val;
  460. }
  461. /// Return collapse value for region.
  462. unsigned getAssociatedLoops() const {
  463. return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops;
  464. }
  465. /// Marks current target region as one with closely nested teams
  466. /// region.
  467. void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
  468. if (!isStackEmpty() && Stack.back().first.size() > 1) {
  469. std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc =
  470. TeamsRegionLoc;
  471. }
  472. }
  473. /// Returns true, if current region has closely nested teams region.
  474. bool hasInnerTeamsRegion() const {
  475. return getInnerTeamsRegionLoc().isValid();
  476. }
  477. /// Returns location of the nested teams region (if any).
  478. SourceLocation getInnerTeamsRegionLoc() const {
  479. return isStackEmpty() ? SourceLocation()
  480. : Stack.back().first.back().InnerTeamsRegionLoc;
  481. }
  482. Scope *getCurScope() const {
  483. return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
  484. }
  485. SourceLocation getConstructLoc() const {
  486. return isStackEmpty() ? SourceLocation()
  487. : Stack.back().first.back().ConstructLoc;
  488. }
  489. /// Do the check specified in \a Check to all component lists and return true
  490. /// if any issue is found.
  491. bool checkMappableExprComponentListsForDecl(
  492. const ValueDecl *VD, bool CurrentRegionOnly,
  493. const llvm::function_ref<
  494. bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
  495. OpenMPClauseKind)>
  496. Check) const {
  497. if (isStackEmpty())
  498. return false;
  499. auto SI = Stack.back().first.rbegin();
  500. auto SE = Stack.back().first.rend();
  501. if (SI == SE)
  502. return false;
  503. if (CurrentRegionOnly)
  504. SE = std::next(SI);
  505. else
  506. std::advance(SI, 1);
  507. for (; SI != SE; ++SI) {
  508. auto MI = SI->MappedExprComponents.find(VD);
  509. if (MI != SI->MappedExprComponents.end())
  510. for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
  511. MI->second.Components)
  512. if (Check(L, MI->second.Kind))
  513. return true;
  514. }
  515. return false;
  516. }
  517. /// Do the check specified in \a Check to all component lists at a given level
  518. /// and return true if any issue is found.
  519. bool checkMappableExprComponentListsForDeclAtLevel(
  520. const ValueDecl *VD, unsigned Level,
  521. const llvm::function_ref<
  522. bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
  523. OpenMPClauseKind)>
  524. Check) const {
  525. if (isStackEmpty())
  526. return false;
  527. auto StartI = Stack.back().first.begin();
  528. auto EndI = Stack.back().first.end();
  529. if (std::distance(StartI, EndI) <= (int)Level)
  530. return false;
  531. std::advance(StartI, Level);
  532. auto MI = StartI->MappedExprComponents.find(VD);
  533. if (MI != StartI->MappedExprComponents.end())
  534. for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
  535. MI->second.Components)
  536. if (Check(L, MI->second.Kind))
  537. return true;
  538. return false;
  539. }
  540. /// Create a new mappable expression component list associated with a given
  541. /// declaration and initialize it with the provided list of components.
  542. void addMappableExpressionComponents(
  543. const ValueDecl *VD,
  544. OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
  545. OpenMPClauseKind WhereFoundClauseKind) {
  546. assert(!isStackEmpty() &&
  547. "Not expecting to retrieve components from a empty stack!");
  548. MappedExprComponentTy &MEC =
  549. Stack.back().first.back().MappedExprComponents[VD];
  550. // Create new entry and append the new components there.
  551. MEC.Components.resize(MEC.Components.size() + 1);
  552. MEC.Components.back().append(Components.begin(), Components.end());
  553. MEC.Kind = WhereFoundClauseKind;
  554. }
  555. unsigned getNestingLevel() const {
  556. assert(!isStackEmpty());
  557. return Stack.back().first.size() - 1;
  558. }
  559. void addDoacrossDependClause(OMPDependClause *C,
  560. const OperatorOffsetTy &OpsOffs) {
  561. assert(!isStackEmpty() && Stack.back().first.size() > 1);
  562. SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
  563. assert(isOpenMPWorksharingDirective(StackElem.Directive));
  564. StackElem.DoacrossDepends.try_emplace(C, OpsOffs);
  565. }
  566. llvm::iterator_range<DoacrossDependMapTy::const_iterator>
  567. getDoacrossDependClauses() const {
  568. assert(!isStackEmpty());
  569. const SharingMapTy &StackElem = Stack.back().first.back();
  570. if (isOpenMPWorksharingDirective(StackElem.Directive)) {
  571. const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
  572. return llvm::make_range(Ref.begin(), Ref.end());
  573. }
  574. return llvm::make_range(StackElem.DoacrossDepends.end(),
  575. StackElem.DoacrossDepends.end());
  576. }
  577. };
  578. bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
  579. return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
  580. isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
  581. }
  582. } // namespace
  583. static const Expr *getExprAsWritten(const Expr *E) {
  584. if (const auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
  585. E = ExprTemp->getSubExpr();
  586. if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
  587. E = MTE->GetTemporaryExpr();
  588. while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
  589. E = Binder->getSubExpr();
  590. if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
  591. E = ICE->getSubExprAsWritten();
  592. return E->IgnoreParens();
  593. }
  594. static Expr *getExprAsWritten(Expr *E) {
  595. return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
  596. }
  597. static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
  598. if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
  599. if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
  600. D = ME->getMemberDecl();
  601. const auto *VD = dyn_cast<VarDecl>(D);
  602. const auto *FD = dyn_cast<FieldDecl>(D);
  603. if (VD != nullptr) {
  604. VD = VD->getCanonicalDecl();
  605. D = VD;
  606. } else {
  607. assert(FD);
  608. FD = FD->getCanonicalDecl();
  609. D = FD;
  610. }
  611. return D;
  612. }
  613. static ValueDecl *getCanonicalDecl(ValueDecl *D) {
  614. return const_cast<ValueDecl *>(
  615. getCanonicalDecl(const_cast<const ValueDecl *>(D)));
  616. }
  617. DSAStackTy::DSAVarData DSAStackTy::getDSA(iterator &Iter,
  618. ValueDecl *D) const {
  619. D = getCanonicalDecl(D);
  620. auto *VD = dyn_cast<VarDecl>(D);
  621. const auto *FD = dyn_cast<FieldDecl>(D);
  622. DSAVarData DVar;
  623. if (isStackEmpty() || Iter == Stack.back().first.rend()) {
  624. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  625. // in a region but not in construct]
  626. // File-scope or namespace-scope variables referenced in called routines
  627. // in the region are shared unless they appear in a threadprivate
  628. // directive.
  629. if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
  630. DVar.CKind = OMPC_shared;
  631. // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
  632. // in a region but not in construct]
  633. // Variables with static storage duration that are declared in called
  634. // routines in the region are shared.
  635. if (VD && VD->hasGlobalStorage())
  636. DVar.CKind = OMPC_shared;
  637. // Non-static data members are shared by default.
  638. if (FD)
  639. DVar.CKind = OMPC_shared;
  640. return DVar;
  641. }
  642. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  643. // in a Construct, C/C++, predetermined, p.1]
  644. // Variables with automatic storage duration that are declared in a scope
  645. // inside the construct are private.
  646. if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
  647. (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
  648. DVar.CKind = OMPC_private;
  649. return DVar;
  650. }
  651. DVar.DKind = Iter->Directive;
  652. // Explicitly specified attributes and local variables with predetermined
  653. // attributes.
  654. if (Iter->SharingMap.count(D)) {
  655. const DSAInfo &Data = Iter->SharingMap.lookup(D);
  656. DVar.RefExpr = Data.RefExpr.getPointer();
  657. DVar.PrivateCopy = Data.PrivateCopy;
  658. DVar.CKind = Data.Attributes;
  659. DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
  660. return DVar;
  661. }
  662. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  663. // in a Construct, C/C++, implicitly determined, p.1]
  664. // In a parallel or task construct, the data-sharing attributes of these
  665. // variables are determined by the default clause, if present.
  666. switch (Iter->DefaultAttr) {
  667. case DSA_shared:
  668. DVar.CKind = OMPC_shared;
  669. DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
  670. return DVar;
  671. case DSA_none:
  672. return DVar;
  673. case DSA_unspecified:
  674. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  675. // in a Construct, implicitly determined, p.2]
  676. // In a parallel construct, if no default clause is present, these
  677. // variables are shared.
  678. DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
  679. if (isOpenMPParallelDirective(DVar.DKind) ||
  680. isOpenMPTeamsDirective(DVar.DKind)) {
  681. DVar.CKind = OMPC_shared;
  682. return DVar;
  683. }
  684. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  685. // in a Construct, implicitly determined, p.4]
  686. // In a task construct, if no default clause is present, a variable that in
  687. // the enclosing context is determined to be shared by all implicit tasks
  688. // bound to the current team is shared.
  689. if (isOpenMPTaskingDirective(DVar.DKind)) {
  690. DSAVarData DVarTemp;
  691. iterator I = Iter, E = Stack.back().first.rend();
  692. do {
  693. ++I;
  694. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
  695. // Referenced in a Construct, implicitly determined, p.6]
  696. // In a task construct, if no default clause is present, a variable
  697. // whose data-sharing attribute is not determined by the rules above is
  698. // firstprivate.
  699. DVarTemp = getDSA(I, D);
  700. if (DVarTemp.CKind != OMPC_shared) {
  701. DVar.RefExpr = nullptr;
  702. DVar.CKind = OMPC_firstprivate;
  703. return DVar;
  704. }
  705. } while (I != E && !isParallelOrTaskRegion(I->Directive));
  706. DVar.CKind =
  707. (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
  708. return DVar;
  709. }
  710. }
  711. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  712. // in a Construct, implicitly determined, p.3]
  713. // For constructs other than task, if no default clause is present, these
  714. // variables inherit their data-sharing attributes from the enclosing
  715. // context.
  716. return getDSA(++Iter, D);
  717. }
  718. const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
  719. const Expr *NewDE) {
  720. assert(!isStackEmpty() && "Data sharing attributes stack is empty");
  721. D = getCanonicalDecl(D);
  722. SharingMapTy &StackElem = Stack.back().first.back();
  723. auto It = StackElem.AlignedMap.find(D);
  724. if (It == StackElem.AlignedMap.end()) {
  725. assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
  726. StackElem.AlignedMap[D] = NewDE;
  727. return nullptr;
  728. }
  729. assert(It->second && "Unexpected nullptr expr in the aligned map");
  730. return It->second;
  731. }
  732. void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
  733. assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
  734. D = getCanonicalDecl(D);
  735. SharingMapTy &StackElem = Stack.back().first.back();
  736. StackElem.LCVMap.try_emplace(
  737. D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
  738. }
  739. const DSAStackTy::LCDeclInfo
  740. DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
  741. assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
  742. D = getCanonicalDecl(D);
  743. const SharingMapTy &StackElem = Stack.back().first.back();
  744. auto It = StackElem.LCVMap.find(D);
  745. if (It != StackElem.LCVMap.end())
  746. return It->second;
  747. return {0, nullptr};
  748. }
  749. const DSAStackTy::LCDeclInfo
  750. DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
  751. assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
  752. "Data-sharing attributes stack is empty");
  753. D = getCanonicalDecl(D);
  754. const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
  755. auto It = StackElem.LCVMap.find(D);
  756. if (It != StackElem.LCVMap.end())
  757. return It->second;
  758. return {0, nullptr};
  759. }
  760. const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
  761. assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
  762. "Data-sharing attributes stack is empty");
  763. const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
  764. if (StackElem.LCVMap.size() < I)
  765. return nullptr;
  766. for (const auto &Pair : StackElem.LCVMap)
  767. if (Pair.second.first == I)
  768. return Pair.first;
  769. return nullptr;
  770. }
  771. void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
  772. DeclRefExpr *PrivateCopy) {
  773. D = getCanonicalDecl(D);
  774. if (A == OMPC_threadprivate) {
  775. DSAInfo &Data = Threadprivates[D];
  776. Data.Attributes = A;
  777. Data.RefExpr.setPointer(E);
  778. Data.PrivateCopy = nullptr;
  779. } else {
  780. assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
  781. DSAInfo &Data = Stack.back().first.back().SharingMap[D];
  782. assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
  783. (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
  784. (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
  785. (isLoopControlVariable(D).first && A == OMPC_private));
  786. if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
  787. Data.RefExpr.setInt(/*IntVal=*/true);
  788. return;
  789. }
  790. const bool IsLastprivate =
  791. A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
  792. Data.Attributes = A;
  793. Data.RefExpr.setPointerAndInt(E, IsLastprivate);
  794. Data.PrivateCopy = PrivateCopy;
  795. if (PrivateCopy) {
  796. DSAInfo &Data =
  797. Stack.back().first.back().SharingMap[PrivateCopy->getDecl()];
  798. Data.Attributes = A;
  799. Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
  800. Data.PrivateCopy = nullptr;
  801. }
  802. }
  803. }
  804. /// Build a variable declaration for OpenMP loop iteration variable.
  805. static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
  806. StringRef Name, const AttrVec *Attrs = nullptr,
  807. DeclRefExpr *OrigRef = nullptr) {
  808. DeclContext *DC = SemaRef.CurContext;
  809. IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
  810. TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
  811. auto *Decl =
  812. VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
  813. if (Attrs) {
  814. for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
  815. I != E; ++I)
  816. Decl->addAttr(*I);
  817. }
  818. Decl->setImplicit();
  819. if (OrigRef) {
  820. Decl->addAttr(
  821. OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
  822. }
  823. return Decl;
  824. }
  825. static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
  826. SourceLocation Loc,
  827. bool RefersToCapture = false) {
  828. D->setReferenced();
  829. D->markUsed(S.Context);
  830. return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
  831. SourceLocation(), D, RefersToCapture, Loc, Ty,
  832. VK_LValue);
  833. }
  834. void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
  835. BinaryOperatorKind BOK) {
  836. D = getCanonicalDecl(D);
  837. assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
  838. assert(
  839. Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
  840. "Additional reduction info may be specified only for reduction items.");
  841. ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
  842. assert(ReductionData.ReductionRange.isInvalid() &&
  843. Stack.back().first.back().Directive == OMPD_taskgroup &&
  844. "Additional reduction info may be specified only once for reduction "
  845. "items.");
  846. ReductionData.set(BOK, SR);
  847. Expr *&TaskgroupReductionRef =
  848. Stack.back().first.back().TaskgroupReductionRef;
  849. if (!TaskgroupReductionRef) {
  850. VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
  851. SemaRef.Context.VoidPtrTy, ".task_red.");
  852. TaskgroupReductionRef =
  853. buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
  854. }
  855. }
  856. void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
  857. const Expr *ReductionRef) {
  858. D = getCanonicalDecl(D);
  859. assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
  860. assert(
  861. Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
  862. "Additional reduction info may be specified only for reduction items.");
  863. ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
  864. assert(ReductionData.ReductionRange.isInvalid() &&
  865. Stack.back().first.back().Directive == OMPD_taskgroup &&
  866. "Additional reduction info may be specified only once for reduction "
  867. "items.");
  868. ReductionData.set(ReductionRef, SR);
  869. Expr *&TaskgroupReductionRef =
  870. Stack.back().first.back().TaskgroupReductionRef;
  871. if (!TaskgroupReductionRef) {
  872. VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
  873. SemaRef.Context.VoidPtrTy, ".task_red.");
  874. TaskgroupReductionRef =
  875. buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
  876. }
  877. }
  878. const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
  879. const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
  880. Expr *&TaskgroupDescriptor) const {
  881. D = getCanonicalDecl(D);
  882. assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
  883. if (Stack.back().first.empty())
  884. return DSAVarData();
  885. for (iterator I = std::next(Stack.back().first.rbegin(), 1),
  886. E = Stack.back().first.rend();
  887. I != E; std::advance(I, 1)) {
  888. const DSAInfo &Data = I->SharingMap.lookup(D);
  889. if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
  890. continue;
  891. const ReductionData &ReductionData = I->ReductionMap.lookup(D);
  892. if (!ReductionData.ReductionOp ||
  893. ReductionData.ReductionOp.is<const Expr *>())
  894. return DSAVarData();
  895. SR = ReductionData.ReductionRange;
  896. BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
  897. assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
  898. "expression for the descriptor is not "
  899. "set.");
  900. TaskgroupDescriptor = I->TaskgroupReductionRef;
  901. return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
  902. Data.PrivateCopy, I->DefaultAttrLoc);
  903. }
  904. return DSAVarData();
  905. }
  906. const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
  907. const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
  908. Expr *&TaskgroupDescriptor) const {
  909. D = getCanonicalDecl(D);
  910. assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
  911. if (Stack.back().first.empty())
  912. return DSAVarData();
  913. for (iterator I = std::next(Stack.back().first.rbegin(), 1),
  914. E = Stack.back().first.rend();
  915. I != E; std::advance(I, 1)) {
  916. const DSAInfo &Data = I->SharingMap.lookup(D);
  917. if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
  918. continue;
  919. const ReductionData &ReductionData = I->ReductionMap.lookup(D);
  920. if (!ReductionData.ReductionOp ||
  921. !ReductionData.ReductionOp.is<const Expr *>())
  922. return DSAVarData();
  923. SR = ReductionData.ReductionRange;
  924. ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
  925. assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
  926. "expression for the descriptor is not "
  927. "set.");
  928. TaskgroupDescriptor = I->TaskgroupReductionRef;
  929. return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
  930. Data.PrivateCopy, I->DefaultAttrLoc);
  931. }
  932. return DSAVarData();
  933. }
  934. bool DSAStackTy::isOpenMPLocal(VarDecl *D, iterator Iter) const {
  935. D = D->getCanonicalDecl();
  936. if (!isStackEmpty()) {
  937. iterator I = Iter, E = Stack.back().first.rend();
  938. Scope *TopScope = nullptr;
  939. while (I != E && !isParallelOrTaskRegion(I->Directive) &&
  940. !isOpenMPTargetExecutionDirective(I->Directive))
  941. ++I;
  942. if (I == E)
  943. return false;
  944. TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
  945. Scope *CurScope = getCurScope();
  946. while (CurScope != TopScope && !CurScope->isDeclScope(D))
  947. CurScope = CurScope->getParent();
  948. return CurScope != TopScope;
  949. }
  950. return false;
  951. }
  952. const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
  953. bool FromParent) {
  954. D = getCanonicalDecl(D);
  955. DSAVarData DVar;
  956. auto *VD = dyn_cast<VarDecl>(D);
  957. auto TI = Threadprivates.find(D);
  958. if (TI != Threadprivates.end()) {
  959. DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
  960. DVar.CKind = OMPC_threadprivate;
  961. return DVar;
  962. }
  963. if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
  964. DVar.RefExpr = buildDeclRefExpr(
  965. SemaRef, VD, D->getType().getNonReferenceType(),
  966. VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
  967. DVar.CKind = OMPC_threadprivate;
  968. addDSA(D, DVar.RefExpr, OMPC_threadprivate);
  969. return DVar;
  970. }
  971. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  972. // in a Construct, C/C++, predetermined, p.1]
  973. // Variables appearing in threadprivate directives are threadprivate.
  974. if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
  975. !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
  976. SemaRef.getLangOpts().OpenMPUseTLS &&
  977. SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
  978. (VD && VD->getStorageClass() == SC_Register &&
  979. VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
  980. DVar.RefExpr = buildDeclRefExpr(
  981. SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
  982. DVar.CKind = OMPC_threadprivate;
  983. addDSA(D, DVar.RefExpr, OMPC_threadprivate);
  984. return DVar;
  985. }
  986. if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
  987. VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
  988. !isLoopControlVariable(D).first) {
  989. iterator IterTarget =
  990. std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(),
  991. [](const SharingMapTy &Data) {
  992. return isOpenMPTargetExecutionDirective(Data.Directive);
  993. });
  994. if (IterTarget != Stack.back().first.rend()) {
  995. iterator ParentIterTarget = std::next(IterTarget, 1);
  996. for (iterator Iter = Stack.back().first.rbegin();
  997. Iter != ParentIterTarget; std::advance(Iter, 1)) {
  998. if (isOpenMPLocal(VD, Iter)) {
  999. DVar.RefExpr =
  1000. buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
  1001. D->getLocation());
  1002. DVar.CKind = OMPC_threadprivate;
  1003. return DVar;
  1004. }
  1005. }
  1006. if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) {
  1007. auto DSAIter = IterTarget->SharingMap.find(D);
  1008. if (DSAIter != IterTarget->SharingMap.end() &&
  1009. isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
  1010. DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
  1011. DVar.CKind = OMPC_threadprivate;
  1012. return DVar;
  1013. }
  1014. iterator End = Stack.back().first.rend();
  1015. if (!SemaRef.isOpenMPCapturedByRef(
  1016. D, std::distance(ParentIterTarget, End))) {
  1017. DVar.RefExpr =
  1018. buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
  1019. IterTarget->ConstructLoc);
  1020. DVar.CKind = OMPC_threadprivate;
  1021. return DVar;
  1022. }
  1023. }
  1024. }
  1025. }
  1026. if (isStackEmpty())
  1027. // Not in OpenMP execution region and top scope was already checked.
  1028. return DVar;
  1029. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  1030. // in a Construct, C/C++, predetermined, p.4]
  1031. // Static data members are shared.
  1032. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  1033. // in a Construct, C/C++, predetermined, p.7]
  1034. // Variables with static storage duration that are declared in a scope
  1035. // inside the construct are shared.
  1036. auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
  1037. if (VD && VD->isStaticDataMember()) {
  1038. DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
  1039. if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
  1040. return DVar;
  1041. DVar.CKind = OMPC_shared;
  1042. return DVar;
  1043. }
  1044. QualType Type = D->getType().getNonReferenceType().getCanonicalType();
  1045. bool IsConstant = Type.isConstant(SemaRef.getASTContext());
  1046. Type = SemaRef.getASTContext().getBaseElementType(Type);
  1047. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  1048. // in a Construct, C/C++, predetermined, p.6]
  1049. // Variables with const qualified type having no mutable member are
  1050. // shared.
  1051. const CXXRecordDecl *RD =
  1052. SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
  1053. if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
  1054. if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
  1055. RD = CTD->getTemplatedDecl();
  1056. if (IsConstant &&
  1057. !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
  1058. RD->hasMutableFields())) {
  1059. // Variables with const-qualified type having no mutable member may be
  1060. // listed in a firstprivate clause, even if they are static data members.
  1061. DSAVarData DVarTemp =
  1062. hasDSA(D, [](OpenMPClauseKind C) { return C == OMPC_firstprivate; },
  1063. MatchesAlways, FromParent);
  1064. if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
  1065. return DVarTemp;
  1066. DVar.CKind = OMPC_shared;
  1067. return DVar;
  1068. }
  1069. // Explicitly specified attributes and local variables with predetermined
  1070. // attributes.
  1071. iterator I = Stack.back().first.rbegin();
  1072. iterator EndI = Stack.back().first.rend();
  1073. if (FromParent && I != EndI)
  1074. std::advance(I, 1);
  1075. auto It = I->SharingMap.find(D);
  1076. if (It != I->SharingMap.end()) {
  1077. const DSAInfo &Data = It->getSecond();
  1078. DVar.RefExpr = Data.RefExpr.getPointer();
  1079. DVar.PrivateCopy = Data.PrivateCopy;
  1080. DVar.CKind = Data.Attributes;
  1081. DVar.ImplicitDSALoc = I->DefaultAttrLoc;
  1082. DVar.DKind = I->Directive;
  1083. }
  1084. return DVar;
  1085. }
  1086. const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
  1087. bool FromParent) const {
  1088. if (isStackEmpty()) {
  1089. iterator I;
  1090. return getDSA(I, D);
  1091. }
  1092. D = getCanonicalDecl(D);
  1093. iterator StartI = Stack.back().first.rbegin();
  1094. iterator EndI = Stack.back().first.rend();
  1095. if (FromParent && StartI != EndI)
  1096. std::advance(StartI, 1);
  1097. return getDSA(StartI, D);
  1098. }
  1099. const DSAStackTy::DSAVarData
  1100. DSAStackTy::hasDSA(ValueDecl *D,
  1101. const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
  1102. const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
  1103. bool FromParent) const {
  1104. if (isStackEmpty())
  1105. return {};
  1106. D = getCanonicalDecl(D);
  1107. iterator I = Stack.back().first.rbegin();
  1108. iterator EndI = Stack.back().first.rend();
  1109. if (FromParent && I != EndI)
  1110. std::advance(I, 1);
  1111. for (; I != EndI; std::advance(I, 1)) {
  1112. if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
  1113. continue;
  1114. iterator NewI = I;
  1115. DSAVarData DVar = getDSA(NewI, D);
  1116. if (I == NewI && CPred(DVar.CKind))
  1117. return DVar;
  1118. }
  1119. return {};
  1120. }
  1121. const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
  1122. ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
  1123. const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
  1124. bool FromParent) const {
  1125. if (isStackEmpty())
  1126. return {};
  1127. D = getCanonicalDecl(D);
  1128. iterator StartI = Stack.back().first.rbegin();
  1129. iterator EndI = Stack.back().first.rend();
  1130. if (FromParent && StartI != EndI)
  1131. std::advance(StartI, 1);
  1132. if (StartI == EndI || !DPred(StartI->Directive))
  1133. return {};
  1134. iterator NewI = StartI;
  1135. DSAVarData DVar = getDSA(NewI, D);
  1136. return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
  1137. }
  1138. bool DSAStackTy::hasExplicitDSA(
  1139. const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
  1140. unsigned Level, bool NotLastprivate) const {
  1141. if (isStackEmpty())
  1142. return false;
  1143. D = getCanonicalDecl(D);
  1144. auto StartI = Stack.back().first.begin();
  1145. auto EndI = Stack.back().first.end();
  1146. if (std::distance(StartI, EndI) <= (int)Level)
  1147. return false;
  1148. std::advance(StartI, Level);
  1149. auto I = StartI->SharingMap.find(D);
  1150. return (I != StartI->SharingMap.end()) &&
  1151. I->getSecond().RefExpr.getPointer() &&
  1152. CPred(I->getSecond().Attributes) &&
  1153. (!NotLastprivate || !I->getSecond().RefExpr.getInt());
  1154. }
  1155. bool DSAStackTy::hasExplicitDirective(
  1156. const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
  1157. unsigned Level) const {
  1158. if (isStackEmpty())
  1159. return false;
  1160. auto StartI = Stack.back().first.begin();
  1161. auto EndI = Stack.back().first.end();
  1162. if (std::distance(StartI, EndI) <= (int)Level)
  1163. return false;
  1164. std::advance(StartI, Level);
  1165. return DPred(StartI->Directive);
  1166. }
  1167. bool DSAStackTy::hasDirective(
  1168. const llvm::function_ref<bool(OpenMPDirectiveKind,
  1169. const DeclarationNameInfo &, SourceLocation)>
  1170. DPred,
  1171. bool FromParent) const {
  1172. // We look only in the enclosing region.
  1173. if (isStackEmpty())
  1174. return false;
  1175. auto StartI = std::next(Stack.back().first.rbegin());
  1176. auto EndI = Stack.back().first.rend();
  1177. if (FromParent && StartI != EndI)
  1178. StartI = std::next(StartI);
  1179. for (auto I = StartI, EE = EndI; I != EE; ++I) {
  1180. if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
  1181. return true;
  1182. }
  1183. return false;
  1184. }
  1185. void Sema::InitDataSharingAttributesStack() {
  1186. VarDataSharingAttributesStack = new DSAStackTy(*this);
  1187. }
  1188. #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
  1189. void Sema::pushOpenMPFunctionRegion() {
  1190. DSAStack->pushFunction();
  1191. }
  1192. void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
  1193. DSAStack->popFunction(OldFSI);
  1194. }
  1195. bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const {
  1196. assert(LangOpts.OpenMP && "OpenMP is not allowed");
  1197. ASTContext &Ctx = getASTContext();
  1198. bool IsByRef = true;
  1199. // Find the directive that is associated with the provided scope.
  1200. D = cast<ValueDecl>(D->getCanonicalDecl());
  1201. QualType Ty = D->getType();
  1202. if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
  1203. // This table summarizes how a given variable should be passed to the device
  1204. // given its type and the clauses where it appears. This table is based on
  1205. // the description in OpenMP 4.5 [2.10.4, target Construct] and
  1206. // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
  1207. //
  1208. // =========================================================================
  1209. // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
  1210. // | |(tofrom:scalar)| | pvt | | | |
  1211. // =========================================================================
  1212. // | scl | | | | - | | bycopy|
  1213. // | scl | | - | x | - | - | bycopy|
  1214. // | scl | | x | - | - | - | null |
  1215. // | scl | x | | | - | | byref |
  1216. // | scl | x | - | x | - | - | bycopy|
  1217. // | scl | x | x | - | - | - | null |
  1218. // | scl | | - | - | - | x | byref |
  1219. // | scl | x | - | - | - | x | byref |
  1220. //
  1221. // | agg | n.a. | | | - | | byref |
  1222. // | agg | n.a. | - | x | - | - | byref |
  1223. // | agg | n.a. | x | - | - | - | null |
  1224. // | agg | n.a. | - | - | - | x | byref |
  1225. // | agg | n.a. | - | - | - | x[] | byref |
  1226. //
  1227. // | ptr | n.a. | | | - | | bycopy|
  1228. // | ptr | n.a. | - | x | - | - | bycopy|
  1229. // | ptr | n.a. | x | - | - | - | null |
  1230. // | ptr | n.a. | - | - | - | x | byref |
  1231. // | ptr | n.a. | - | - | - | x[] | bycopy|
  1232. // | ptr | n.a. | - | - | x | | bycopy|
  1233. // | ptr | n.a. | - | - | x | x | bycopy|
  1234. // | ptr | n.a. | - | - | x | x[] | bycopy|
  1235. // =========================================================================
  1236. // Legend:
  1237. // scl - scalar
  1238. // ptr - pointer
  1239. // agg - aggregate
  1240. // x - applies
  1241. // - - invalid in this combination
  1242. // [] - mapped with an array section
  1243. // byref - should be mapped by reference
  1244. // byval - should be mapped by value
  1245. // null - initialize a local variable to null on the device
  1246. //
  1247. // Observations:
  1248. // - All scalar declarations that show up in a map clause have to be passed
  1249. // by reference, because they may have been mapped in the enclosing data
  1250. // environment.
  1251. // - If the scalar value does not fit the size of uintptr, it has to be
  1252. // passed by reference, regardless the result in the table above.
  1253. // - For pointers mapped by value that have either an implicit map or an
  1254. // array section, the runtime library may pass the NULL value to the
  1255. // device instead of the value passed to it by the compiler.
  1256. if (Ty->isReferenceType())
  1257. Ty = Ty->castAs<ReferenceType>()->getPointeeType();
  1258. // Locate map clauses and see if the variable being captured is referred to
  1259. // in any of those clauses. Here we only care about variables, not fields,
  1260. // because fields are part of aggregates.
  1261. bool IsVariableUsedInMapClause = false;
  1262. bool IsVariableAssociatedWithSection = false;
  1263. DSAStack->checkMappableExprComponentListsForDeclAtLevel(
  1264. D, Level,
  1265. [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
  1266. OMPClauseMappableExprCommon::MappableExprComponentListRef
  1267. MapExprComponents,
  1268. OpenMPClauseKind WhereFoundClauseKind) {
  1269. // Only the map clause information influences how a variable is
  1270. // captured. E.g. is_device_ptr does not require changing the default
  1271. // behavior.
  1272. if (WhereFoundClauseKind != OMPC_map)
  1273. return false;
  1274. auto EI = MapExprComponents.rbegin();
  1275. auto EE = MapExprComponents.rend();
  1276. assert(EI != EE && "Invalid map expression!");
  1277. if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
  1278. IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
  1279. ++EI;
  1280. if (EI == EE)
  1281. return false;
  1282. if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
  1283. isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
  1284. isa<MemberExpr>(EI->getAssociatedExpression())) {
  1285. IsVariableAssociatedWithSection = true;
  1286. // There is nothing more we need to know about this variable.
  1287. return true;
  1288. }
  1289. // Keep looking for more map info.
  1290. return false;
  1291. });
  1292. if (IsVariableUsedInMapClause) {
  1293. // If variable is identified in a map clause it is always captured by
  1294. // reference except if it is a pointer that is dereferenced somehow.
  1295. IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
  1296. } else {
  1297. // By default, all the data that has a scalar type is mapped by copy
  1298. // (except for reduction variables).
  1299. IsByRef =
  1300. !Ty->isScalarType() ||
  1301. DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
  1302. DSAStack->hasExplicitDSA(
  1303. D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
  1304. }
  1305. }
  1306. if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
  1307. IsByRef =
  1308. !DSAStack->hasExplicitDSA(
  1309. D,
  1310. [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
  1311. Level, /*NotLastprivate=*/true) &&
  1312. // If the variable is artificial and must be captured by value - try to
  1313. // capture by value.
  1314. !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
  1315. !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
  1316. }
  1317. // When passing data by copy, we need to make sure it fits the uintptr size
  1318. // and alignment, because the runtime library only deals with uintptr types.
  1319. // If it does not fit the uintptr size, we need to pass the data by reference
  1320. // instead.
  1321. if (!IsByRef &&
  1322. (Ctx.getTypeSizeInChars(Ty) >
  1323. Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
  1324. Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
  1325. IsByRef = true;
  1326. }
  1327. return IsByRef;
  1328. }
  1329. unsigned Sema::getOpenMPNestingLevel() const {
  1330. assert(getLangOpts().OpenMP);
  1331. return DSAStack->getNestingLevel();
  1332. }
  1333. bool Sema::isInOpenMPTargetExecutionDirective() const {
  1334. return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
  1335. !DSAStack->isClauseParsingMode()) ||
  1336. DSAStack->hasDirective(
  1337. [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
  1338. SourceLocation) -> bool {
  1339. return isOpenMPTargetExecutionDirective(K);
  1340. },
  1341. false);
  1342. }
  1343. VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D) {
  1344. assert(LangOpts.OpenMP && "OpenMP is not allowed");
  1345. D = getCanonicalDecl(D);
  1346. // If we are attempting to capture a global variable in a directive with
  1347. // 'target' we return true so that this global is also mapped to the device.
  1348. //
  1349. auto *VD = dyn_cast<VarDecl>(D);
  1350. if (VD && !VD->hasLocalStorage()) {
  1351. if (isInOpenMPDeclareTargetContext() &&
  1352. (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
  1353. // Try to mark variable as declare target if it is used in capturing
  1354. // regions.
  1355. if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
  1356. checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
  1357. return nullptr;
  1358. } else if (isInOpenMPTargetExecutionDirective()) {
  1359. // If the declaration is enclosed in a 'declare target' directive,
  1360. // then it should not be captured.
  1361. //
  1362. if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
  1363. return nullptr;
  1364. return VD;
  1365. }
  1366. }
  1367. if (DSAStack->getCurrentDirective() != OMPD_unknown &&
  1368. (!DSAStack->isClauseParsingMode() ||
  1369. DSAStack->getParentDirective() != OMPD_unknown)) {
  1370. auto &&Info = DSAStack->isLoopControlVariable(D);
  1371. if (Info.first ||
  1372. (VD && VD->hasLocalStorage() &&
  1373. isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
  1374. (VD && DSAStack->isForceVarCapturing()))
  1375. return VD ? VD : Info.second;
  1376. DSAStackTy::DSAVarData DVarPrivate =
  1377. DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
  1378. if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
  1379. return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
  1380. DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate,
  1381. [](OpenMPDirectiveKind) { return true; },
  1382. DSAStack->isClauseParsingMode());
  1383. if (DVarPrivate.CKind != OMPC_unknown)
  1384. return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
  1385. }
  1386. return nullptr;
  1387. }
  1388. void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
  1389. unsigned Level) const {
  1390. SmallVector<OpenMPDirectiveKind, 4> Regions;
  1391. getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
  1392. FunctionScopesIndex -= Regions.size();
  1393. }
  1394. bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const {
  1395. assert(LangOpts.OpenMP && "OpenMP is not allowed");
  1396. return DSAStack->hasExplicitDSA(
  1397. D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) ||
  1398. (DSAStack->isClauseParsingMode() &&
  1399. DSAStack->getClauseParsingMode() == OMPC_private) ||
  1400. // Consider taskgroup reduction descriptor variable a private to avoid
  1401. // possible capture in the region.
  1402. (DSAStack->hasExplicitDirective(
  1403. [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
  1404. Level) &&
  1405. DSAStack->isTaskgroupReductionRef(D, Level));
  1406. }
  1407. void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
  1408. unsigned Level) {
  1409. assert(LangOpts.OpenMP && "OpenMP is not allowed");
  1410. D = getCanonicalDecl(D);
  1411. OpenMPClauseKind OMPC = OMPC_unknown;
  1412. for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
  1413. const unsigned NewLevel = I - 1;
  1414. if (DSAStack->hasExplicitDSA(D,
  1415. [&OMPC](const OpenMPClauseKind K) {
  1416. if (isOpenMPPrivate(K)) {
  1417. OMPC = K;
  1418. return true;
  1419. }
  1420. return false;
  1421. },
  1422. NewLevel))
  1423. break;
  1424. if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
  1425. D, NewLevel,
  1426. [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
  1427. OpenMPClauseKind) { return true; })) {
  1428. OMPC = OMPC_map;
  1429. break;
  1430. }
  1431. if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
  1432. NewLevel)) {
  1433. OMPC = OMPC_map;
  1434. if (D->getType()->isScalarType() &&
  1435. DSAStack->getDefaultDMAAtLevel(NewLevel) !=
  1436. DefaultMapAttributes::DMA_tofrom_scalar)
  1437. OMPC = OMPC_firstprivate;
  1438. break;
  1439. }
  1440. }
  1441. if (OMPC != OMPC_unknown)
  1442. FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
  1443. }
  1444. bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D,
  1445. unsigned Level) const {
  1446. assert(LangOpts.OpenMP && "OpenMP is not allowed");
  1447. // Return true if the current level is no longer enclosed in a target region.
  1448. const auto *VD = dyn_cast<VarDecl>(D);
  1449. return VD && !VD->hasLocalStorage() &&
  1450. DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
  1451. Level);
  1452. }
  1453. void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
  1454. void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
  1455. const DeclarationNameInfo &DirName,
  1456. Scope *CurScope, SourceLocation Loc) {
  1457. DSAStack->push(DKind, DirName, CurScope, Loc);
  1458. PushExpressionEvaluationContext(
  1459. ExpressionEvaluationContext::PotentiallyEvaluated);
  1460. }
  1461. void Sema::StartOpenMPClause(OpenMPClauseKind K) {
  1462. DSAStack->setClauseParsingMode(K);
  1463. }
  1464. void Sema::EndOpenMPClause() {
  1465. DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
  1466. }
  1467. void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
  1468. // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
  1469. // A variable of class type (or array thereof) that appears in a lastprivate
  1470. // clause requires an accessible, unambiguous default constructor for the
  1471. // class type, unless the list item is also specified in a firstprivate
  1472. // clause.
  1473. if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
  1474. for (OMPClause *C : D->clauses()) {
  1475. if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
  1476. SmallVector<Expr *, 8> PrivateCopies;
  1477. for (Expr *DE : Clause->varlists()) {
  1478. if (DE->isValueDependent() || DE->isTypeDependent()) {
  1479. PrivateCopies.push_back(nullptr);
  1480. continue;
  1481. }
  1482. auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
  1483. auto *VD = cast<VarDecl>(DRE->getDecl());
  1484. QualType Type = VD->getType().getNonReferenceType();
  1485. const DSAStackTy::DSAVarData DVar =
  1486. DSAStack->getTopDSA(VD, /*FromParent=*/false);
  1487. if (DVar.CKind == OMPC_lastprivate) {
  1488. // Generate helper private variable and initialize it with the
  1489. // default value. The address of the original variable is replaced
  1490. // by the address of the new private variable in CodeGen. This new
  1491. // variable is not added to IdResolver, so the code in the OpenMP
  1492. // region uses original variable for proper diagnostics.
  1493. VarDecl *VDPrivate = buildVarDecl(
  1494. *this, DE->getExprLoc(), Type.getUnqualifiedType(),
  1495. VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
  1496. ActOnUninitializedDecl(VDPrivate);
  1497. if (VDPrivate->isInvalidDecl())
  1498. continue;
  1499. PrivateCopies.push_back(buildDeclRefExpr(
  1500. *this, VDPrivate, DE->getType(), DE->getExprLoc()));
  1501. } else {
  1502. // The variable is also a firstprivate, so initialization sequence
  1503. // for private copy is generated already.
  1504. PrivateCopies.push_back(nullptr);
  1505. }
  1506. }
  1507. // Set initializers to private copies if no errors were found.
  1508. if (PrivateCopies.size() == Clause->varlist_size())
  1509. Clause->setPrivateCopies(PrivateCopies);
  1510. }
  1511. }
  1512. }
  1513. DSAStack->pop();
  1514. DiscardCleanupsInEvaluationContext();
  1515. PopExpressionEvaluationContext();
  1516. }
  1517. static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
  1518. Expr *NumIterations, Sema &SemaRef,
  1519. Scope *S, DSAStackTy *Stack);
  1520. namespace {
  1521. class VarDeclFilterCCC final : public CorrectionCandidateCallback {
  1522. private:
  1523. Sema &SemaRef;
  1524. public:
  1525. explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
  1526. bool ValidateCandidate(const TypoCorrection &Candidate) override {
  1527. NamedDecl *ND = Candidate.getCorrectionDecl();
  1528. if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
  1529. return VD->hasGlobalStorage() &&
  1530. SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
  1531. SemaRef.getCurScope());
  1532. }
  1533. return false;
  1534. }
  1535. };
  1536. class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
  1537. private:
  1538. Sema &SemaRef;
  1539. public:
  1540. explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
  1541. bool ValidateCandidate(const TypoCorrection &Candidate) override {
  1542. NamedDecl *ND = Candidate.getCorrectionDecl();
  1543. if (ND && (isa<VarDecl>(ND) || isa<FunctionDecl>(ND))) {
  1544. return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
  1545. SemaRef.getCurScope());
  1546. }
  1547. return false;
  1548. }
  1549. };
  1550. } // namespace
  1551. ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
  1552. CXXScopeSpec &ScopeSpec,
  1553. const DeclarationNameInfo &Id) {
  1554. LookupResult Lookup(*this, Id, LookupOrdinaryName);
  1555. LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
  1556. if (Lookup.isAmbiguous())
  1557. return ExprError();
  1558. VarDecl *VD;
  1559. if (!Lookup.isSingleResult()) {
  1560. if (TypoCorrection Corrected = CorrectTypo(
  1561. Id, LookupOrdinaryName, CurScope, nullptr,
  1562. llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
  1563. diagnoseTypo(Corrected,
  1564. PDiag(Lookup.empty()
  1565. ? diag::err_undeclared_var_use_suggest
  1566. : diag::err_omp_expected_var_arg_suggest)
  1567. << Id.getName());
  1568. VD = Corrected.getCorrectionDeclAs<VarDecl>();
  1569. } else {
  1570. Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
  1571. : diag::err_omp_expected_var_arg)
  1572. << Id.getName();
  1573. return ExprError();
  1574. }
  1575. } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
  1576. Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
  1577. Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
  1578. return ExprError();
  1579. }
  1580. Lookup.suppressDiagnostics();
  1581. // OpenMP [2.9.2, Syntax, C/C++]
  1582. // Variables must be file-scope, namespace-scope, or static block-scope.
  1583. if (!VD->hasGlobalStorage()) {
  1584. Diag(Id.getLoc(), diag::err_omp_global_var_arg)
  1585. << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
  1586. bool IsDecl =
  1587. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  1588. Diag(VD->getLocation(),
  1589. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  1590. << VD;
  1591. return ExprError();
  1592. }
  1593. VarDecl *CanonicalVD = VD->getCanonicalDecl();
  1594. NamedDecl *ND = CanonicalVD;
  1595. // OpenMP [2.9.2, Restrictions, C/C++, p.2]
  1596. // A threadprivate directive for file-scope variables must appear outside
  1597. // any definition or declaration.
  1598. if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
  1599. !getCurLexicalContext()->isTranslationUnit()) {
  1600. Diag(Id.getLoc(), diag::err_omp_var_scope)
  1601. << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
  1602. bool IsDecl =
  1603. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  1604. Diag(VD->getLocation(),
  1605. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  1606. << VD;
  1607. return ExprError();
  1608. }
  1609. // OpenMP [2.9.2, Restrictions, C/C++, p.3]
  1610. // A threadprivate directive for static class member variables must appear
  1611. // in the class definition, in the same scope in which the member
  1612. // variables are declared.
  1613. if (CanonicalVD->isStaticDataMember() &&
  1614. !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
  1615. Diag(Id.getLoc(), diag::err_omp_var_scope)
  1616. << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
  1617. bool IsDecl =
  1618. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  1619. Diag(VD->getLocation(),
  1620. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  1621. << VD;
  1622. return ExprError();
  1623. }
  1624. // OpenMP [2.9.2, Restrictions, C/C++, p.4]
  1625. // A threadprivate directive for namespace-scope variables must appear
  1626. // outside any definition or declaration other than the namespace
  1627. // definition itself.
  1628. if (CanonicalVD->getDeclContext()->isNamespace() &&
  1629. (!getCurLexicalContext()->isFileContext() ||
  1630. !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
  1631. Diag(Id.getLoc(), diag::err_omp_var_scope)
  1632. << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
  1633. bool IsDecl =
  1634. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  1635. Diag(VD->getLocation(),
  1636. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  1637. << VD;
  1638. return ExprError();
  1639. }
  1640. // OpenMP [2.9.2, Restrictions, C/C++, p.6]
  1641. // A threadprivate directive for static block-scope variables must appear
  1642. // in the scope of the variable and not in a nested scope.
  1643. if (CanonicalVD->isStaticLocal() && CurScope &&
  1644. !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
  1645. Diag(Id.getLoc(), diag::err_omp_var_scope)
  1646. << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
  1647. bool IsDecl =
  1648. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  1649. Diag(VD->getLocation(),
  1650. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  1651. << VD;
  1652. return ExprError();
  1653. }
  1654. // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
  1655. // A threadprivate directive must lexically precede all references to any
  1656. // of the variables in its list.
  1657. if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
  1658. Diag(Id.getLoc(), diag::err_omp_var_used)
  1659. << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
  1660. return ExprError();
  1661. }
  1662. QualType ExprType = VD->getType().getNonReferenceType();
  1663. return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
  1664. SourceLocation(), VD,
  1665. /*RefersToEnclosingVariableOrCapture=*/false,
  1666. Id.getLoc(), ExprType, VK_LValue);
  1667. }
  1668. Sema::DeclGroupPtrTy
  1669. Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
  1670. ArrayRef<Expr *> VarList) {
  1671. if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
  1672. CurContext->addDecl(D);
  1673. return DeclGroupPtrTy::make(DeclGroupRef(D));
  1674. }
  1675. return nullptr;
  1676. }
  1677. namespace {
  1678. class LocalVarRefChecker final
  1679. : public ConstStmtVisitor<LocalVarRefChecker, bool> {
  1680. Sema &SemaRef;
  1681. public:
  1682. bool VisitDeclRefExpr(const DeclRefExpr *E) {
  1683. if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
  1684. if (VD->hasLocalStorage()) {
  1685. SemaRef.Diag(E->getBeginLoc(),
  1686. diag::err_omp_local_var_in_threadprivate_init)
  1687. << E->getSourceRange();
  1688. SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
  1689. << VD << VD->getSourceRange();
  1690. return true;
  1691. }
  1692. }
  1693. return false;
  1694. }
  1695. bool VisitStmt(const Stmt *S) {
  1696. for (const Stmt *Child : S->children()) {
  1697. if (Child && Visit(Child))
  1698. return true;
  1699. }
  1700. return false;
  1701. }
  1702. explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
  1703. };
  1704. } // namespace
  1705. OMPThreadPrivateDecl *
  1706. Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
  1707. SmallVector<Expr *, 8> Vars;
  1708. for (Expr *RefExpr : VarList) {
  1709. auto *DE = cast<DeclRefExpr>(RefExpr);
  1710. auto *VD = cast<VarDecl>(DE->getDecl());
  1711. SourceLocation ILoc = DE->getExprLoc();
  1712. // Mark variable as used.
  1713. VD->setReferenced();
  1714. VD->markUsed(Context);
  1715. QualType QType = VD->getType();
  1716. if (QType->isDependentType() || QType->isInstantiationDependentType()) {
  1717. // It will be analyzed later.
  1718. Vars.push_back(DE);
  1719. continue;
  1720. }
  1721. // OpenMP [2.9.2, Restrictions, C/C++, p.10]
  1722. // A threadprivate variable must not have an incomplete type.
  1723. if (RequireCompleteType(ILoc, VD->getType(),
  1724. diag::err_omp_threadprivate_incomplete_type)) {
  1725. continue;
  1726. }
  1727. // OpenMP [2.9.2, Restrictions, C/C++, p.10]
  1728. // A threadprivate variable must not have a reference type.
  1729. if (VD->getType()->isReferenceType()) {
  1730. Diag(ILoc, diag::err_omp_ref_type_arg)
  1731. << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
  1732. bool IsDecl =
  1733. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  1734. Diag(VD->getLocation(),
  1735. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  1736. << VD;
  1737. continue;
  1738. }
  1739. // Check if this is a TLS variable. If TLS is not being supported, produce
  1740. // the corresponding diagnostic.
  1741. if ((VD->getTLSKind() != VarDecl::TLS_None &&
  1742. !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
  1743. getLangOpts().OpenMPUseTLS &&
  1744. getASTContext().getTargetInfo().isTLSSupported())) ||
  1745. (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
  1746. !VD->isLocalVarDecl())) {
  1747. Diag(ILoc, diag::err_omp_var_thread_local)
  1748. << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
  1749. bool IsDecl =
  1750. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  1751. Diag(VD->getLocation(),
  1752. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  1753. << VD;
  1754. continue;
  1755. }
  1756. // Check if initial value of threadprivate variable reference variable with
  1757. // local storage (it is not supported by runtime).
  1758. if (const Expr *Init = VD->getAnyInitializer()) {
  1759. LocalVarRefChecker Checker(*this);
  1760. if (Checker.Visit(Init))
  1761. continue;
  1762. }
  1763. Vars.push_back(RefExpr);
  1764. DSAStack->addDSA(VD, DE, OMPC_threadprivate);
  1765. VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
  1766. Context, SourceRange(Loc, Loc)));
  1767. if (ASTMutationListener *ML = Context.getASTMutationListener())
  1768. ML->DeclarationMarkedOpenMPThreadPrivate(VD);
  1769. }
  1770. OMPThreadPrivateDecl *D = nullptr;
  1771. if (!Vars.empty()) {
  1772. D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
  1773. Vars);
  1774. D->setAccess(AS_public);
  1775. }
  1776. return D;
  1777. }
  1778. Sema::DeclGroupPtrTy
  1779. Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
  1780. ArrayRef<OMPClause *> ClauseList) {
  1781. OMPRequiresDecl *D = nullptr;
  1782. if (!CurContext->isFileContext()) {
  1783. Diag(Loc, diag::err_omp_invalid_scope) << "requires";
  1784. } else {
  1785. D = CheckOMPRequiresDecl(Loc, ClauseList);
  1786. if (D) {
  1787. CurContext->addDecl(D);
  1788. DSAStack->addRequiresDecl(D);
  1789. }
  1790. }
  1791. return DeclGroupPtrTy::make(DeclGroupRef(D));
  1792. }
  1793. OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
  1794. ArrayRef<OMPClause *> ClauseList) {
  1795. if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
  1796. return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
  1797. ClauseList);
  1798. return nullptr;
  1799. }
  1800. static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
  1801. const ValueDecl *D,
  1802. const DSAStackTy::DSAVarData &DVar,
  1803. bool IsLoopIterVar = false) {
  1804. if (DVar.RefExpr) {
  1805. SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
  1806. << getOpenMPClauseName(DVar.CKind);
  1807. return;
  1808. }
  1809. enum {
  1810. PDSA_StaticMemberShared,
  1811. PDSA_StaticLocalVarShared,
  1812. PDSA_LoopIterVarPrivate,
  1813. PDSA_LoopIterVarLinear,
  1814. PDSA_LoopIterVarLastprivate,
  1815. PDSA_ConstVarShared,
  1816. PDSA_GlobalVarShared,
  1817. PDSA_TaskVarFirstprivate,
  1818. PDSA_LocalVarPrivate,
  1819. PDSA_Implicit
  1820. } Reason = PDSA_Implicit;
  1821. bool ReportHint = false;
  1822. auto ReportLoc = D->getLocation();
  1823. auto *VD = dyn_cast<VarDecl>(D);
  1824. if (IsLoopIterVar) {
  1825. if (DVar.CKind == OMPC_private)
  1826. Reason = PDSA_LoopIterVarPrivate;
  1827. else if (DVar.CKind == OMPC_lastprivate)
  1828. Reason = PDSA_LoopIterVarLastprivate;
  1829. else
  1830. Reason = PDSA_LoopIterVarLinear;
  1831. } else if (isOpenMPTaskingDirective(DVar.DKind) &&
  1832. DVar.CKind == OMPC_firstprivate) {
  1833. Reason = PDSA_TaskVarFirstprivate;
  1834. ReportLoc = DVar.ImplicitDSALoc;
  1835. } else if (VD && VD->isStaticLocal())
  1836. Reason = PDSA_StaticLocalVarShared;
  1837. else if (VD && VD->isStaticDataMember())
  1838. Reason = PDSA_StaticMemberShared;
  1839. else if (VD && VD->isFileVarDecl())
  1840. Reason = PDSA_GlobalVarShared;
  1841. else if (D->getType().isConstant(SemaRef.getASTContext()))
  1842. Reason = PDSA_ConstVarShared;
  1843. else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
  1844. ReportHint = true;
  1845. Reason = PDSA_LocalVarPrivate;
  1846. }
  1847. if (Reason != PDSA_Implicit) {
  1848. SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
  1849. << Reason << ReportHint
  1850. << getOpenMPDirectiveName(Stack->getCurrentDirective());
  1851. } else if (DVar.ImplicitDSALoc.isValid()) {
  1852. SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
  1853. << getOpenMPClauseName(DVar.CKind);
  1854. }
  1855. }
  1856. namespace {
  1857. class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
  1858. DSAStackTy *Stack;
  1859. Sema &SemaRef;
  1860. bool ErrorFound = false;
  1861. CapturedStmt *CS = nullptr;
  1862. llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
  1863. llvm::SmallVector<Expr *, 4> ImplicitMap;
  1864. Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
  1865. llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
  1866. public:
  1867. void VisitDeclRefExpr(DeclRefExpr *E) {
  1868. if (E->isTypeDependent() || E->isValueDependent() ||
  1869. E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
  1870. return;
  1871. if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
  1872. VD = VD->getCanonicalDecl();
  1873. // Skip internally declared variables.
  1874. if (VD->hasLocalStorage() && !CS->capturesVariable(VD))
  1875. return;
  1876. DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
  1877. // Check if the variable has explicit DSA set and stop analysis if it so.
  1878. if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
  1879. return;
  1880. // Skip internally declared static variables.
  1881. llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
  1882. OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
  1883. if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) &&
  1884. (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
  1885. return;
  1886. SourceLocation ELoc = E->getExprLoc();
  1887. OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
  1888. // The default(none) clause requires that each variable that is referenced
  1889. // in the construct, and does not have a predetermined data-sharing
  1890. // attribute, must have its data-sharing attribute explicitly determined
  1891. // by being listed in a data-sharing attribute clause.
  1892. if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
  1893. isParallelOrTaskRegion(DKind) &&
  1894. VarsWithInheritedDSA.count(VD) == 0) {
  1895. VarsWithInheritedDSA[VD] = E;
  1896. return;
  1897. }
  1898. if (isOpenMPTargetExecutionDirective(DKind) &&
  1899. !Stack->isLoopControlVariable(VD).first) {
  1900. if (!Stack->checkMappableExprComponentListsForDecl(
  1901. VD, /*CurrentRegionOnly=*/true,
  1902. [](OMPClauseMappableExprCommon::MappableExprComponentListRef
  1903. StackComponents,
  1904. OpenMPClauseKind) {
  1905. // Variable is used if it has been marked as an array, array
  1906. // section or the variable iself.
  1907. return StackComponents.size() == 1 ||
  1908. std::all_of(
  1909. std::next(StackComponents.rbegin()),
  1910. StackComponents.rend(),
  1911. [](const OMPClauseMappableExprCommon::
  1912. MappableComponent &MC) {
  1913. return MC.getAssociatedDeclaration() ==
  1914. nullptr &&
  1915. (isa<OMPArraySectionExpr>(
  1916. MC.getAssociatedExpression()) ||
  1917. isa<ArraySubscriptExpr>(
  1918. MC.getAssociatedExpression()));
  1919. });
  1920. })) {
  1921. bool IsFirstprivate = false;
  1922. // By default lambdas are captured as firstprivates.
  1923. if (const auto *RD =
  1924. VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
  1925. IsFirstprivate = RD->isLambda();
  1926. IsFirstprivate =
  1927. IsFirstprivate ||
  1928. (VD->getType().getNonReferenceType()->isScalarType() &&
  1929. Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res);
  1930. if (IsFirstprivate)
  1931. ImplicitFirstprivate.emplace_back(E);
  1932. else
  1933. ImplicitMap.emplace_back(E);
  1934. return;
  1935. }
  1936. }
  1937. // OpenMP [2.9.3.6, Restrictions, p.2]
  1938. // A list item that appears in a reduction clause of the innermost
  1939. // enclosing worksharing or parallel construct may not be accessed in an
  1940. // explicit task.
  1941. DVar = Stack->hasInnermostDSA(
  1942. VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
  1943. [](OpenMPDirectiveKind K) {
  1944. return isOpenMPParallelDirective(K) ||
  1945. isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
  1946. },
  1947. /*FromParent=*/true);
  1948. if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
  1949. ErrorFound = true;
  1950. SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
  1951. reportOriginalDsa(SemaRef, Stack, VD, DVar);
  1952. return;
  1953. }
  1954. // Define implicit data-sharing attributes for task.
  1955. DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
  1956. if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
  1957. !Stack->isLoopControlVariable(VD).first)
  1958. ImplicitFirstprivate.push_back(E);
  1959. }
  1960. }
  1961. void VisitMemberExpr(MemberExpr *E) {
  1962. if (E->isTypeDependent() || E->isValueDependent() ||
  1963. E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
  1964. return;
  1965. auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
  1966. OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
  1967. if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
  1968. if (!FD)
  1969. return;
  1970. DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
  1971. // Check if the variable has explicit DSA set and stop analysis if it
  1972. // so.
  1973. if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
  1974. return;
  1975. if (isOpenMPTargetExecutionDirective(DKind) &&
  1976. !Stack->isLoopControlVariable(FD).first &&
  1977. !Stack->checkMappableExprComponentListsForDecl(
  1978. FD, /*CurrentRegionOnly=*/true,
  1979. [](OMPClauseMappableExprCommon::MappableExprComponentListRef
  1980. StackComponents,
  1981. OpenMPClauseKind) {
  1982. return isa<CXXThisExpr>(
  1983. cast<MemberExpr>(
  1984. StackComponents.back().getAssociatedExpression())
  1985. ->getBase()
  1986. ->IgnoreParens());
  1987. })) {
  1988. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
  1989. // A bit-field cannot appear in a map clause.
  1990. //
  1991. if (FD->isBitField())
  1992. return;
  1993. ImplicitMap.emplace_back(E);
  1994. return;
  1995. }
  1996. SourceLocation ELoc = E->getExprLoc();
  1997. // OpenMP [2.9.3.6, Restrictions, p.2]
  1998. // A list item that appears in a reduction clause of the innermost
  1999. // enclosing worksharing or parallel construct may not be accessed in
  2000. // an explicit task.
  2001. DVar = Stack->hasInnermostDSA(
  2002. FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
  2003. [](OpenMPDirectiveKind K) {
  2004. return isOpenMPParallelDirective(K) ||
  2005. isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
  2006. },
  2007. /*FromParent=*/true);
  2008. if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
  2009. ErrorFound = true;
  2010. SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
  2011. reportOriginalDsa(SemaRef, Stack, FD, DVar);
  2012. return;
  2013. }
  2014. // Define implicit data-sharing attributes for task.
  2015. DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
  2016. if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
  2017. !Stack->isLoopControlVariable(FD).first)
  2018. ImplicitFirstprivate.push_back(E);
  2019. return;
  2020. }
  2021. if (isOpenMPTargetExecutionDirective(DKind)) {
  2022. OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
  2023. if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
  2024. /*NoDiagnose=*/true))
  2025. return;
  2026. const auto *VD = cast<ValueDecl>(
  2027. CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
  2028. if (!Stack->checkMappableExprComponentListsForDecl(
  2029. VD, /*CurrentRegionOnly=*/true,
  2030. [&CurComponents](
  2031. OMPClauseMappableExprCommon::MappableExprComponentListRef
  2032. StackComponents,
  2033. OpenMPClauseKind) {
  2034. auto CCI = CurComponents.rbegin();
  2035. auto CCE = CurComponents.rend();
  2036. for (const auto &SC : llvm::reverse(StackComponents)) {
  2037. // Do both expressions have the same kind?
  2038. if (CCI->getAssociatedExpression()->getStmtClass() !=
  2039. SC.getAssociatedExpression()->getStmtClass())
  2040. if (!(isa<OMPArraySectionExpr>(
  2041. SC.getAssociatedExpression()) &&
  2042. isa<ArraySubscriptExpr>(
  2043. CCI->getAssociatedExpression())))
  2044. return false;
  2045. const Decl *CCD = CCI->getAssociatedDeclaration();
  2046. const Decl *SCD = SC.getAssociatedDeclaration();
  2047. CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
  2048. SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
  2049. if (SCD != CCD)
  2050. return false;
  2051. std::advance(CCI, 1);
  2052. if (CCI == CCE)
  2053. break;
  2054. }
  2055. return true;
  2056. })) {
  2057. Visit(E->getBase());
  2058. }
  2059. } else {
  2060. Visit(E->getBase());
  2061. }
  2062. }
  2063. void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
  2064. for (OMPClause *C : S->clauses()) {
  2065. // Skip analysis of arguments of implicitly defined firstprivate clause
  2066. // for task|target directives.
  2067. // Skip analysis of arguments of implicitly defined map clause for target
  2068. // directives.
  2069. if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
  2070. C->isImplicit())) {
  2071. for (Stmt *CC : C->children()) {
  2072. if (CC)
  2073. Visit(CC);
  2074. }
  2075. }
  2076. }
  2077. }
  2078. void VisitStmt(Stmt *S) {
  2079. for (Stmt *C : S->children()) {
  2080. if (C && !isa<OMPExecutableDirective>(C))
  2081. Visit(C);
  2082. }
  2083. }
  2084. bool isErrorFound() const { return ErrorFound; }
  2085. ArrayRef<Expr *> getImplicitFirstprivate() const {
  2086. return ImplicitFirstprivate;
  2087. }
  2088. ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
  2089. const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
  2090. return VarsWithInheritedDSA;
  2091. }
  2092. DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
  2093. : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
  2094. };
  2095. } // namespace
  2096. void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
  2097. switch (DKind) {
  2098. case OMPD_parallel:
  2099. case OMPD_parallel_for:
  2100. case OMPD_parallel_for_simd:
  2101. case OMPD_parallel_sections:
  2102. case OMPD_teams:
  2103. case OMPD_teams_distribute:
  2104. case OMPD_teams_distribute_simd: {
  2105. QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
  2106. QualType KmpInt32PtrTy =
  2107. Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
  2108. Sema::CapturedParamNameType Params[] = {
  2109. std::make_pair(".global_tid.", KmpInt32PtrTy),
  2110. std::make_pair(".bound_tid.", KmpInt32PtrTy),
  2111. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2112. };
  2113. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2114. Params);
  2115. break;
  2116. }
  2117. case OMPD_target_teams:
  2118. case OMPD_target_parallel:
  2119. case OMPD_target_parallel_for:
  2120. case OMPD_target_parallel_for_simd:
  2121. case OMPD_target_teams_distribute:
  2122. case OMPD_target_teams_distribute_simd: {
  2123. QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
  2124. QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
  2125. QualType KmpInt32PtrTy =
  2126. Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
  2127. QualType Args[] = {VoidPtrTy};
  2128. FunctionProtoType::ExtProtoInfo EPI;
  2129. EPI.Variadic = true;
  2130. QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
  2131. Sema::CapturedParamNameType Params[] = {
  2132. std::make_pair(".global_tid.", KmpInt32Ty),
  2133. std::make_pair(".part_id.", KmpInt32PtrTy),
  2134. std::make_pair(".privates.", VoidPtrTy),
  2135. std::make_pair(
  2136. ".copy_fn.",
  2137. Context.getPointerType(CopyFnType).withConst().withRestrict()),
  2138. std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
  2139. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2140. };
  2141. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2142. Params);
  2143. // Mark this captured region as inlined, because we don't use outlined
  2144. // function directly.
  2145. getCurCapturedRegion()->TheCapturedDecl->addAttr(
  2146. AlwaysInlineAttr::CreateImplicit(
  2147. Context, AlwaysInlineAttr::Keyword_forceinline));
  2148. Sema::CapturedParamNameType ParamsTarget[] = {
  2149. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2150. };
  2151. // Start a captured region for 'target' with no implicit parameters.
  2152. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2153. ParamsTarget);
  2154. Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
  2155. std::make_pair(".global_tid.", KmpInt32PtrTy),
  2156. std::make_pair(".bound_tid.", KmpInt32PtrTy),
  2157. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2158. };
  2159. // Start a captured region for 'teams' or 'parallel'. Both regions have
  2160. // the same implicit parameters.
  2161. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2162. ParamsTeamsOrParallel);
  2163. break;
  2164. }
  2165. case OMPD_target:
  2166. case OMPD_target_simd: {
  2167. QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
  2168. QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
  2169. QualType KmpInt32PtrTy =
  2170. Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
  2171. QualType Args[] = {VoidPtrTy};
  2172. FunctionProtoType::ExtProtoInfo EPI;
  2173. EPI.Variadic = true;
  2174. QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
  2175. Sema::CapturedParamNameType Params[] = {
  2176. std::make_pair(".global_tid.", KmpInt32Ty),
  2177. std::make_pair(".part_id.", KmpInt32PtrTy),
  2178. std::make_pair(".privates.", VoidPtrTy),
  2179. std::make_pair(
  2180. ".copy_fn.",
  2181. Context.getPointerType(CopyFnType).withConst().withRestrict()),
  2182. std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
  2183. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2184. };
  2185. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2186. Params);
  2187. // Mark this captured region as inlined, because we don't use outlined
  2188. // function directly.
  2189. getCurCapturedRegion()->TheCapturedDecl->addAttr(
  2190. AlwaysInlineAttr::CreateImplicit(
  2191. Context, AlwaysInlineAttr::Keyword_forceinline));
  2192. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2193. std::make_pair(StringRef(), QualType()));
  2194. break;
  2195. }
  2196. case OMPD_simd:
  2197. case OMPD_for:
  2198. case OMPD_for_simd:
  2199. case OMPD_sections:
  2200. case OMPD_section:
  2201. case OMPD_single:
  2202. case OMPD_master:
  2203. case OMPD_critical:
  2204. case OMPD_taskgroup:
  2205. case OMPD_distribute:
  2206. case OMPD_distribute_simd:
  2207. case OMPD_ordered:
  2208. case OMPD_atomic:
  2209. case OMPD_target_data: {
  2210. Sema::CapturedParamNameType Params[] = {
  2211. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2212. };
  2213. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2214. Params);
  2215. break;
  2216. }
  2217. case OMPD_task: {
  2218. QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
  2219. QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
  2220. QualType KmpInt32PtrTy =
  2221. Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
  2222. QualType Args[] = {VoidPtrTy};
  2223. FunctionProtoType::ExtProtoInfo EPI;
  2224. EPI.Variadic = true;
  2225. QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
  2226. Sema::CapturedParamNameType Params[] = {
  2227. std::make_pair(".global_tid.", KmpInt32Ty),
  2228. std::make_pair(".part_id.", KmpInt32PtrTy),
  2229. std::make_pair(".privates.", VoidPtrTy),
  2230. std::make_pair(
  2231. ".copy_fn.",
  2232. Context.getPointerType(CopyFnType).withConst().withRestrict()),
  2233. std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
  2234. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2235. };
  2236. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2237. Params);
  2238. // Mark this captured region as inlined, because we don't use outlined
  2239. // function directly.
  2240. getCurCapturedRegion()->TheCapturedDecl->addAttr(
  2241. AlwaysInlineAttr::CreateImplicit(
  2242. Context, AlwaysInlineAttr::Keyword_forceinline));
  2243. break;
  2244. }
  2245. case OMPD_taskloop:
  2246. case OMPD_taskloop_simd: {
  2247. QualType KmpInt32Ty =
  2248. Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
  2249. .withConst();
  2250. QualType KmpUInt64Ty =
  2251. Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
  2252. .withConst();
  2253. QualType KmpInt64Ty =
  2254. Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
  2255. .withConst();
  2256. QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
  2257. QualType KmpInt32PtrTy =
  2258. Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
  2259. QualType Args[] = {VoidPtrTy};
  2260. FunctionProtoType::ExtProtoInfo EPI;
  2261. EPI.Variadic = true;
  2262. QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
  2263. Sema::CapturedParamNameType Params[] = {
  2264. std::make_pair(".global_tid.", KmpInt32Ty),
  2265. std::make_pair(".part_id.", KmpInt32PtrTy),
  2266. std::make_pair(".privates.", VoidPtrTy),
  2267. std::make_pair(
  2268. ".copy_fn.",
  2269. Context.getPointerType(CopyFnType).withConst().withRestrict()),
  2270. std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
  2271. std::make_pair(".lb.", KmpUInt64Ty),
  2272. std::make_pair(".ub.", KmpUInt64Ty),
  2273. std::make_pair(".st.", KmpInt64Ty),
  2274. std::make_pair(".liter.", KmpInt32Ty),
  2275. std::make_pair(".reductions.", VoidPtrTy),
  2276. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2277. };
  2278. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2279. Params);
  2280. // Mark this captured region as inlined, because we don't use outlined
  2281. // function directly.
  2282. getCurCapturedRegion()->TheCapturedDecl->addAttr(
  2283. AlwaysInlineAttr::CreateImplicit(
  2284. Context, AlwaysInlineAttr::Keyword_forceinline));
  2285. break;
  2286. }
  2287. case OMPD_distribute_parallel_for_simd:
  2288. case OMPD_distribute_parallel_for: {
  2289. QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
  2290. QualType KmpInt32PtrTy =
  2291. Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
  2292. Sema::CapturedParamNameType Params[] = {
  2293. std::make_pair(".global_tid.", KmpInt32PtrTy),
  2294. std::make_pair(".bound_tid.", KmpInt32PtrTy),
  2295. std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
  2296. std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
  2297. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2298. };
  2299. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2300. Params);
  2301. break;
  2302. }
  2303. case OMPD_target_teams_distribute_parallel_for:
  2304. case OMPD_target_teams_distribute_parallel_for_simd: {
  2305. QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
  2306. QualType KmpInt32PtrTy =
  2307. Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
  2308. QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
  2309. QualType Args[] = {VoidPtrTy};
  2310. FunctionProtoType::ExtProtoInfo EPI;
  2311. EPI.Variadic = true;
  2312. QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
  2313. Sema::CapturedParamNameType Params[] = {
  2314. std::make_pair(".global_tid.", KmpInt32Ty),
  2315. std::make_pair(".part_id.", KmpInt32PtrTy),
  2316. std::make_pair(".privates.", VoidPtrTy),
  2317. std::make_pair(
  2318. ".copy_fn.",
  2319. Context.getPointerType(CopyFnType).withConst().withRestrict()),
  2320. std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
  2321. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2322. };
  2323. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2324. Params);
  2325. // Mark this captured region as inlined, because we don't use outlined
  2326. // function directly.
  2327. getCurCapturedRegion()->TheCapturedDecl->addAttr(
  2328. AlwaysInlineAttr::CreateImplicit(
  2329. Context, AlwaysInlineAttr::Keyword_forceinline));
  2330. Sema::CapturedParamNameType ParamsTarget[] = {
  2331. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2332. };
  2333. // Start a captured region for 'target' with no implicit parameters.
  2334. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2335. ParamsTarget);
  2336. Sema::CapturedParamNameType ParamsTeams[] = {
  2337. std::make_pair(".global_tid.", KmpInt32PtrTy),
  2338. std::make_pair(".bound_tid.", KmpInt32PtrTy),
  2339. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2340. };
  2341. // Start a captured region for 'target' with no implicit parameters.
  2342. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2343. ParamsTeams);
  2344. Sema::CapturedParamNameType ParamsParallel[] = {
  2345. std::make_pair(".global_tid.", KmpInt32PtrTy),
  2346. std::make_pair(".bound_tid.", KmpInt32PtrTy),
  2347. std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
  2348. std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
  2349. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2350. };
  2351. // Start a captured region for 'teams' or 'parallel'. Both regions have
  2352. // the same implicit parameters.
  2353. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2354. ParamsParallel);
  2355. break;
  2356. }
  2357. case OMPD_teams_distribute_parallel_for:
  2358. case OMPD_teams_distribute_parallel_for_simd: {
  2359. QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
  2360. QualType KmpInt32PtrTy =
  2361. Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
  2362. Sema::CapturedParamNameType ParamsTeams[] = {
  2363. std::make_pair(".global_tid.", KmpInt32PtrTy),
  2364. std::make_pair(".bound_tid.", KmpInt32PtrTy),
  2365. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2366. };
  2367. // Start a captured region for 'target' with no implicit parameters.
  2368. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2369. ParamsTeams);
  2370. Sema::CapturedParamNameType ParamsParallel[] = {
  2371. std::make_pair(".global_tid.", KmpInt32PtrTy),
  2372. std::make_pair(".bound_tid.", KmpInt32PtrTy),
  2373. std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
  2374. std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
  2375. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2376. };
  2377. // Start a captured region for 'teams' or 'parallel'. Both regions have
  2378. // the same implicit parameters.
  2379. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2380. ParamsParallel);
  2381. break;
  2382. }
  2383. case OMPD_target_update:
  2384. case OMPD_target_enter_data:
  2385. case OMPD_target_exit_data: {
  2386. QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
  2387. QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
  2388. QualType KmpInt32PtrTy =
  2389. Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
  2390. QualType Args[] = {VoidPtrTy};
  2391. FunctionProtoType::ExtProtoInfo EPI;
  2392. EPI.Variadic = true;
  2393. QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
  2394. Sema::CapturedParamNameType Params[] = {
  2395. std::make_pair(".global_tid.", KmpInt32Ty),
  2396. std::make_pair(".part_id.", KmpInt32PtrTy),
  2397. std::make_pair(".privates.", VoidPtrTy),
  2398. std::make_pair(
  2399. ".copy_fn.",
  2400. Context.getPointerType(CopyFnType).withConst().withRestrict()),
  2401. std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
  2402. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2403. };
  2404. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2405. Params);
  2406. // Mark this captured region as inlined, because we don't use outlined
  2407. // function directly.
  2408. getCurCapturedRegion()->TheCapturedDecl->addAttr(
  2409. AlwaysInlineAttr::CreateImplicit(
  2410. Context, AlwaysInlineAttr::Keyword_forceinline));
  2411. break;
  2412. }
  2413. case OMPD_threadprivate:
  2414. case OMPD_taskyield:
  2415. case OMPD_barrier:
  2416. case OMPD_taskwait:
  2417. case OMPD_cancellation_point:
  2418. case OMPD_cancel:
  2419. case OMPD_flush:
  2420. case OMPD_declare_reduction:
  2421. case OMPD_declare_simd:
  2422. case OMPD_declare_target:
  2423. case OMPD_end_declare_target:
  2424. case OMPD_requires:
  2425. llvm_unreachable("OpenMP Directive is not allowed");
  2426. case OMPD_unknown:
  2427. llvm_unreachable("Unknown OpenMP directive");
  2428. }
  2429. }
  2430. int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
  2431. SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
  2432. getOpenMPCaptureRegions(CaptureRegions, DKind);
  2433. return CaptureRegions.size();
  2434. }
  2435. static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
  2436. Expr *CaptureExpr, bool WithInit,
  2437. bool AsExpression) {
  2438. assert(CaptureExpr);
  2439. ASTContext &C = S.getASTContext();
  2440. Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
  2441. QualType Ty = Init->getType();
  2442. if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
  2443. if (S.getLangOpts().CPlusPlus) {
  2444. Ty = C.getLValueReferenceType(Ty);
  2445. } else {
  2446. Ty = C.getPointerType(Ty);
  2447. ExprResult Res =
  2448. S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
  2449. if (!Res.isUsable())
  2450. return nullptr;
  2451. Init = Res.get();
  2452. }
  2453. WithInit = true;
  2454. }
  2455. auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
  2456. CaptureExpr->getBeginLoc());
  2457. if (!WithInit)
  2458. CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
  2459. S.CurContext->addHiddenDecl(CED);
  2460. S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
  2461. return CED;
  2462. }
  2463. static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
  2464. bool WithInit) {
  2465. OMPCapturedExprDecl *CD;
  2466. if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
  2467. CD = cast<OMPCapturedExprDecl>(VD);
  2468. else
  2469. CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
  2470. /*AsExpression=*/false);
  2471. return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
  2472. CaptureExpr->getExprLoc());
  2473. }
  2474. static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
  2475. CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
  2476. if (!Ref) {
  2477. OMPCapturedExprDecl *CD = buildCaptureDecl(
  2478. S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
  2479. /*WithInit=*/true, /*AsExpression=*/true);
  2480. Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
  2481. CaptureExpr->getExprLoc());
  2482. }
  2483. ExprResult Res = Ref;
  2484. if (!S.getLangOpts().CPlusPlus &&
  2485. CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
  2486. Ref->getType()->isPointerType()) {
  2487. Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
  2488. if (!Res.isUsable())
  2489. return ExprError();
  2490. }
  2491. return S.DefaultLvalueConversion(Res.get());
  2492. }
  2493. namespace {
  2494. // OpenMP directives parsed in this section are represented as a
  2495. // CapturedStatement with an associated statement. If a syntax error
  2496. // is detected during the parsing of the associated statement, the
  2497. // compiler must abort processing and close the CapturedStatement.
  2498. //
  2499. // Combined directives such as 'target parallel' have more than one
  2500. // nested CapturedStatements. This RAII ensures that we unwind out
  2501. // of all the nested CapturedStatements when an error is found.
  2502. class CaptureRegionUnwinderRAII {
  2503. private:
  2504. Sema &S;
  2505. bool &ErrorFound;
  2506. OpenMPDirectiveKind DKind = OMPD_unknown;
  2507. public:
  2508. CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
  2509. OpenMPDirectiveKind DKind)
  2510. : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
  2511. ~CaptureRegionUnwinderRAII() {
  2512. if (ErrorFound) {
  2513. int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
  2514. while (--ThisCaptureLevel >= 0)
  2515. S.ActOnCapturedRegionError();
  2516. }
  2517. }
  2518. };
  2519. } // namespace
  2520. StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
  2521. ArrayRef<OMPClause *> Clauses) {
  2522. bool ErrorFound = false;
  2523. CaptureRegionUnwinderRAII CaptureRegionUnwinder(
  2524. *this, ErrorFound, DSAStack->getCurrentDirective());
  2525. if (!S.isUsable()) {
  2526. ErrorFound = true;
  2527. return StmtError();
  2528. }
  2529. SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
  2530. getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
  2531. OMPOrderedClause *OC = nullptr;
  2532. OMPScheduleClause *SC = nullptr;
  2533. SmallVector<const OMPLinearClause *, 4> LCs;
  2534. SmallVector<const OMPClauseWithPreInit *, 4> PICs;
  2535. // This is required for proper codegen.
  2536. for (OMPClause *Clause : Clauses) {
  2537. if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
  2538. Clause->getClauseKind() == OMPC_in_reduction) {
  2539. // Capture taskgroup task_reduction descriptors inside the tasking regions
  2540. // with the corresponding in_reduction items.
  2541. auto *IRC = cast<OMPInReductionClause>(Clause);
  2542. for (Expr *E : IRC->taskgroup_descriptors())
  2543. if (E)
  2544. MarkDeclarationsReferencedInExpr(E);
  2545. }
  2546. if (isOpenMPPrivate(Clause->getClauseKind()) ||
  2547. Clause->getClauseKind() == OMPC_copyprivate ||
  2548. (getLangOpts().OpenMPUseTLS &&
  2549. getASTContext().getTargetInfo().isTLSSupported() &&
  2550. Clause->getClauseKind() == OMPC_copyin)) {
  2551. DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
  2552. // Mark all variables in private list clauses as used in inner region.
  2553. for (Stmt *VarRef : Clause->children()) {
  2554. if (auto *E = cast_or_null<Expr>(VarRef)) {
  2555. MarkDeclarationsReferencedInExpr(E);
  2556. }
  2557. }
  2558. DSAStack->setForceVarCapturing(/*V=*/false);
  2559. } else if (CaptureRegions.size() > 1 ||
  2560. CaptureRegions.back() != OMPD_unknown) {
  2561. if (auto *C = OMPClauseWithPreInit::get(Clause))
  2562. PICs.push_back(C);
  2563. if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
  2564. if (Expr *E = C->getPostUpdateExpr())
  2565. MarkDeclarationsReferencedInExpr(E);
  2566. }
  2567. }
  2568. if (Clause->getClauseKind() == OMPC_schedule)
  2569. SC = cast<OMPScheduleClause>(Clause);
  2570. else if (Clause->getClauseKind() == OMPC_ordered)
  2571. OC = cast<OMPOrderedClause>(Clause);
  2572. else if (Clause->getClauseKind() == OMPC_linear)
  2573. LCs.push_back(cast<OMPLinearClause>(Clause));
  2574. }
  2575. // OpenMP, 2.7.1 Loop Construct, Restrictions
  2576. // The nonmonotonic modifier cannot be specified if an ordered clause is
  2577. // specified.
  2578. if (SC &&
  2579. (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
  2580. SC->getSecondScheduleModifier() ==
  2581. OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
  2582. OC) {
  2583. Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
  2584. ? SC->getFirstScheduleModifierLoc()
  2585. : SC->getSecondScheduleModifierLoc(),
  2586. diag::err_omp_schedule_nonmonotonic_ordered)
  2587. << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
  2588. ErrorFound = true;
  2589. }
  2590. if (!LCs.empty() && OC && OC->getNumForLoops()) {
  2591. for (const OMPLinearClause *C : LCs) {
  2592. Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
  2593. << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
  2594. }
  2595. ErrorFound = true;
  2596. }
  2597. if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
  2598. isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
  2599. OC->getNumForLoops()) {
  2600. Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
  2601. << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
  2602. ErrorFound = true;
  2603. }
  2604. if (ErrorFound) {
  2605. return StmtError();
  2606. }
  2607. StmtResult SR = S;
  2608. for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
  2609. // Mark all variables in private list clauses as used in inner region.
  2610. // Required for proper codegen of combined directives.
  2611. // TODO: add processing for other clauses.
  2612. if (ThisCaptureRegion != OMPD_unknown) {
  2613. for (const clang::OMPClauseWithPreInit *C : PICs) {
  2614. OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
  2615. // Find the particular capture region for the clause if the
  2616. // directive is a combined one with multiple capture regions.
  2617. // If the directive is not a combined one, the capture region
  2618. // associated with the clause is OMPD_unknown and is generated
  2619. // only once.
  2620. if (CaptureRegion == ThisCaptureRegion ||
  2621. CaptureRegion == OMPD_unknown) {
  2622. if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
  2623. for (Decl *D : DS->decls())
  2624. MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
  2625. }
  2626. }
  2627. }
  2628. }
  2629. SR = ActOnCapturedRegionEnd(SR.get());
  2630. }
  2631. return SR;
  2632. }
  2633. static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
  2634. OpenMPDirectiveKind CancelRegion,
  2635. SourceLocation StartLoc) {
  2636. // CancelRegion is only needed for cancel and cancellation_point.
  2637. if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
  2638. return false;
  2639. if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
  2640. CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
  2641. return false;
  2642. SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
  2643. << getOpenMPDirectiveName(CancelRegion);
  2644. return true;
  2645. }
  2646. static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
  2647. OpenMPDirectiveKind CurrentRegion,
  2648. const DeclarationNameInfo &CurrentName,
  2649. OpenMPDirectiveKind CancelRegion,
  2650. SourceLocation StartLoc) {
  2651. if (Stack->getCurScope()) {
  2652. OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
  2653. OpenMPDirectiveKind OffendingRegion = ParentRegion;
  2654. bool NestingProhibited = false;
  2655. bool CloseNesting = true;
  2656. bool OrphanSeen = false;
  2657. enum {
  2658. NoRecommend,
  2659. ShouldBeInParallelRegion,
  2660. ShouldBeInOrderedRegion,
  2661. ShouldBeInTargetRegion,
  2662. ShouldBeInTeamsRegion
  2663. } Recommend = NoRecommend;
  2664. if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
  2665. // OpenMP [2.16, Nesting of Regions]
  2666. // OpenMP constructs may not be nested inside a simd region.
  2667. // OpenMP [2.8.1,simd Construct, Restrictions]
  2668. // An ordered construct with the simd clause is the only OpenMP
  2669. // construct that can appear in the simd region.
  2670. // Allowing a SIMD construct nested in another SIMD construct is an
  2671. // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
  2672. // message.
  2673. SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
  2674. ? diag::err_omp_prohibited_region_simd
  2675. : diag::warn_omp_nesting_simd);
  2676. return CurrentRegion != OMPD_simd;
  2677. }
  2678. if (ParentRegion == OMPD_atomic) {
  2679. // OpenMP [2.16, Nesting of Regions]
  2680. // OpenMP constructs may not be nested inside an atomic region.
  2681. SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
  2682. return true;
  2683. }
  2684. if (CurrentRegion == OMPD_section) {
  2685. // OpenMP [2.7.2, sections Construct, Restrictions]
  2686. // Orphaned section directives are prohibited. That is, the section
  2687. // directives must appear within the sections construct and must not be
  2688. // encountered elsewhere in the sections region.
  2689. if (ParentRegion != OMPD_sections &&
  2690. ParentRegion != OMPD_parallel_sections) {
  2691. SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
  2692. << (ParentRegion != OMPD_unknown)
  2693. << getOpenMPDirectiveName(ParentRegion);
  2694. return true;
  2695. }
  2696. return false;
  2697. }
  2698. // Allow some constructs (except teams) to be orphaned (they could be
  2699. // used in functions, called from OpenMP regions with the required
  2700. // preconditions).
  2701. if (ParentRegion == OMPD_unknown &&
  2702. !isOpenMPNestingTeamsDirective(CurrentRegion))
  2703. return false;
  2704. if (CurrentRegion == OMPD_cancellation_point ||
  2705. CurrentRegion == OMPD_cancel) {
  2706. // OpenMP [2.16, Nesting of Regions]
  2707. // A cancellation point construct for which construct-type-clause is
  2708. // taskgroup must be nested inside a task construct. A cancellation
  2709. // point construct for which construct-type-clause is not taskgroup must
  2710. // be closely nested inside an OpenMP construct that matches the type
  2711. // specified in construct-type-clause.
  2712. // A cancel construct for which construct-type-clause is taskgroup must be
  2713. // nested inside a task construct. A cancel construct for which
  2714. // construct-type-clause is not taskgroup must be closely nested inside an
  2715. // OpenMP construct that matches the type specified in
  2716. // construct-type-clause.
  2717. NestingProhibited =
  2718. !((CancelRegion == OMPD_parallel &&
  2719. (ParentRegion == OMPD_parallel ||
  2720. ParentRegion == OMPD_target_parallel)) ||
  2721. (CancelRegion == OMPD_for &&
  2722. (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
  2723. ParentRegion == OMPD_target_parallel_for ||
  2724. ParentRegion == OMPD_distribute_parallel_for ||
  2725. ParentRegion == OMPD_teams_distribute_parallel_for ||
  2726. ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
  2727. (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
  2728. (CancelRegion == OMPD_sections &&
  2729. (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
  2730. ParentRegion == OMPD_parallel_sections)));
  2731. } else if (CurrentRegion == OMPD_master) {
  2732. // OpenMP [2.16, Nesting of Regions]
  2733. // A master region may not be closely nested inside a worksharing,
  2734. // atomic, or explicit task region.
  2735. NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
  2736. isOpenMPTaskingDirective(ParentRegion);
  2737. } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
  2738. // OpenMP [2.16, Nesting of Regions]
  2739. // A critical region may not be nested (closely or otherwise) inside a
  2740. // critical region with the same name. Note that this restriction is not
  2741. // sufficient to prevent deadlock.
  2742. SourceLocation PreviousCriticalLoc;
  2743. bool DeadLock = Stack->hasDirective(
  2744. [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
  2745. const DeclarationNameInfo &DNI,
  2746. SourceLocation Loc) {
  2747. if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
  2748. PreviousCriticalLoc = Loc;
  2749. return true;
  2750. }
  2751. return false;
  2752. },
  2753. false /* skip top directive */);
  2754. if (DeadLock) {
  2755. SemaRef.Diag(StartLoc,
  2756. diag::err_omp_prohibited_region_critical_same_name)
  2757. << CurrentName.getName();
  2758. if (PreviousCriticalLoc.isValid())
  2759. SemaRef.Diag(PreviousCriticalLoc,
  2760. diag::note_omp_previous_critical_region);
  2761. return true;
  2762. }
  2763. } else if (CurrentRegion == OMPD_barrier) {
  2764. // OpenMP [2.16, Nesting of Regions]
  2765. // A barrier region may not be closely nested inside a worksharing,
  2766. // explicit task, critical, ordered, atomic, or master region.
  2767. NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
  2768. isOpenMPTaskingDirective(ParentRegion) ||
  2769. ParentRegion == OMPD_master ||
  2770. ParentRegion == OMPD_critical ||
  2771. ParentRegion == OMPD_ordered;
  2772. } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
  2773. !isOpenMPParallelDirective(CurrentRegion) &&
  2774. !isOpenMPTeamsDirective(CurrentRegion)) {
  2775. // OpenMP [2.16, Nesting of Regions]
  2776. // A worksharing region may not be closely nested inside a worksharing,
  2777. // explicit task, critical, ordered, atomic, or master region.
  2778. NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
  2779. isOpenMPTaskingDirective(ParentRegion) ||
  2780. ParentRegion == OMPD_master ||
  2781. ParentRegion == OMPD_critical ||
  2782. ParentRegion == OMPD_ordered;
  2783. Recommend = ShouldBeInParallelRegion;
  2784. } else if (CurrentRegion == OMPD_ordered) {
  2785. // OpenMP [2.16, Nesting of Regions]
  2786. // An ordered region may not be closely nested inside a critical,
  2787. // atomic, or explicit task region.
  2788. // An ordered region must be closely nested inside a loop region (or
  2789. // parallel loop region) with an ordered clause.
  2790. // OpenMP [2.8.1,simd Construct, Restrictions]
  2791. // An ordered construct with the simd clause is the only OpenMP construct
  2792. // that can appear in the simd region.
  2793. NestingProhibited = ParentRegion == OMPD_critical ||
  2794. isOpenMPTaskingDirective(ParentRegion) ||
  2795. !(isOpenMPSimdDirective(ParentRegion) ||
  2796. Stack->isParentOrderedRegion());
  2797. Recommend = ShouldBeInOrderedRegion;
  2798. } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
  2799. // OpenMP [2.16, Nesting of Regions]
  2800. // If specified, a teams construct must be contained within a target
  2801. // construct.
  2802. NestingProhibited = ParentRegion != OMPD_target;
  2803. OrphanSeen = ParentRegion == OMPD_unknown;
  2804. Recommend = ShouldBeInTargetRegion;
  2805. }
  2806. if (!NestingProhibited &&
  2807. !isOpenMPTargetExecutionDirective(CurrentRegion) &&
  2808. !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
  2809. (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
  2810. // OpenMP [2.16, Nesting of Regions]
  2811. // distribute, parallel, parallel sections, parallel workshare, and the
  2812. // parallel loop and parallel loop SIMD constructs are the only OpenMP
  2813. // constructs that can be closely nested in the teams region.
  2814. NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
  2815. !isOpenMPDistributeDirective(CurrentRegion);
  2816. Recommend = ShouldBeInParallelRegion;
  2817. }
  2818. if (!NestingProhibited &&
  2819. isOpenMPNestingDistributeDirective(CurrentRegion)) {
  2820. // OpenMP 4.5 [2.17 Nesting of Regions]
  2821. // The region associated with the distribute construct must be strictly
  2822. // nested inside a teams region
  2823. NestingProhibited =
  2824. (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
  2825. Recommend = ShouldBeInTeamsRegion;
  2826. }
  2827. if (!NestingProhibited &&
  2828. (isOpenMPTargetExecutionDirective(CurrentRegion) ||
  2829. isOpenMPTargetDataManagementDirective(CurrentRegion))) {
  2830. // OpenMP 4.5 [2.17 Nesting of Regions]
  2831. // If a target, target update, target data, target enter data, or
  2832. // target exit data construct is encountered during execution of a
  2833. // target region, the behavior is unspecified.
  2834. NestingProhibited = Stack->hasDirective(
  2835. [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
  2836. SourceLocation) {
  2837. if (isOpenMPTargetExecutionDirective(K)) {
  2838. OffendingRegion = K;
  2839. return true;
  2840. }
  2841. return false;
  2842. },
  2843. false /* don't skip top directive */);
  2844. CloseNesting = false;
  2845. }
  2846. if (NestingProhibited) {
  2847. if (OrphanSeen) {
  2848. SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
  2849. << getOpenMPDirectiveName(CurrentRegion) << Recommend;
  2850. } else {
  2851. SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
  2852. << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
  2853. << Recommend << getOpenMPDirectiveName(CurrentRegion);
  2854. }
  2855. return true;
  2856. }
  2857. }
  2858. return false;
  2859. }
  2860. static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
  2861. ArrayRef<OMPClause *> Clauses,
  2862. ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
  2863. bool ErrorFound = false;
  2864. unsigned NamedModifiersNumber = 0;
  2865. SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
  2866. OMPD_unknown + 1);
  2867. SmallVector<SourceLocation, 4> NameModifierLoc;
  2868. for (const OMPClause *C : Clauses) {
  2869. if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
  2870. // At most one if clause without a directive-name-modifier can appear on
  2871. // the directive.
  2872. OpenMPDirectiveKind CurNM = IC->getNameModifier();
  2873. if (FoundNameModifiers[CurNM]) {
  2874. S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
  2875. << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
  2876. << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
  2877. ErrorFound = true;
  2878. } else if (CurNM != OMPD_unknown) {
  2879. NameModifierLoc.push_back(IC->getNameModifierLoc());
  2880. ++NamedModifiersNumber;
  2881. }
  2882. FoundNameModifiers[CurNM] = IC;
  2883. if (CurNM == OMPD_unknown)
  2884. continue;
  2885. // Check if the specified name modifier is allowed for the current
  2886. // directive.
  2887. // At most one if clause with the particular directive-name-modifier can
  2888. // appear on the directive.
  2889. bool MatchFound = false;
  2890. for (auto NM : AllowedNameModifiers) {
  2891. if (CurNM == NM) {
  2892. MatchFound = true;
  2893. break;
  2894. }
  2895. }
  2896. if (!MatchFound) {
  2897. S.Diag(IC->getNameModifierLoc(),
  2898. diag::err_omp_wrong_if_directive_name_modifier)
  2899. << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
  2900. ErrorFound = true;
  2901. }
  2902. }
  2903. }
  2904. // If any if clause on the directive includes a directive-name-modifier then
  2905. // all if clauses on the directive must include a directive-name-modifier.
  2906. if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
  2907. if (NamedModifiersNumber == AllowedNameModifiers.size()) {
  2908. S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
  2909. diag::err_omp_no_more_if_clause);
  2910. } else {
  2911. std::string Values;
  2912. std::string Sep(", ");
  2913. unsigned AllowedCnt = 0;
  2914. unsigned TotalAllowedNum =
  2915. AllowedNameModifiers.size() - NamedModifiersNumber;
  2916. for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
  2917. ++Cnt) {
  2918. OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
  2919. if (!FoundNameModifiers[NM]) {
  2920. Values += "'";
  2921. Values += getOpenMPDirectiveName(NM);
  2922. Values += "'";
  2923. if (AllowedCnt + 2 == TotalAllowedNum)
  2924. Values += " or ";
  2925. else if (AllowedCnt + 1 != TotalAllowedNum)
  2926. Values += Sep;
  2927. ++AllowedCnt;
  2928. }
  2929. }
  2930. S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
  2931. diag::err_omp_unnamed_if_clause)
  2932. << (TotalAllowedNum > 1) << Values;
  2933. }
  2934. for (SourceLocation Loc : NameModifierLoc) {
  2935. S.Diag(Loc, diag::note_omp_previous_named_if_clause);
  2936. }
  2937. ErrorFound = true;
  2938. }
  2939. return ErrorFound;
  2940. }
  2941. StmtResult Sema::ActOnOpenMPExecutableDirective(
  2942. OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
  2943. OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
  2944. Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
  2945. StmtResult Res = StmtError();
  2946. // First check CancelRegion which is then used in checkNestingOfRegions.
  2947. if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
  2948. checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
  2949. StartLoc))
  2950. return StmtError();
  2951. llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
  2952. VarsWithInheritedDSAType VarsWithInheritedDSA;
  2953. bool ErrorFound = false;
  2954. ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
  2955. if (AStmt && !CurContext->isDependentContext()) {
  2956. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  2957. // Check default data sharing attributes for referenced variables.
  2958. DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
  2959. int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
  2960. Stmt *S = AStmt;
  2961. while (--ThisCaptureLevel >= 0)
  2962. S = cast<CapturedStmt>(S)->getCapturedStmt();
  2963. DSAChecker.Visit(S);
  2964. if (DSAChecker.isErrorFound())
  2965. return StmtError();
  2966. // Generate list of implicitly defined firstprivate variables.
  2967. VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
  2968. SmallVector<Expr *, 4> ImplicitFirstprivates(
  2969. DSAChecker.getImplicitFirstprivate().begin(),
  2970. DSAChecker.getImplicitFirstprivate().end());
  2971. SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
  2972. DSAChecker.getImplicitMap().end());
  2973. // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
  2974. for (OMPClause *C : Clauses) {
  2975. if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
  2976. for (Expr *E : IRC->taskgroup_descriptors())
  2977. if (E)
  2978. ImplicitFirstprivates.emplace_back(E);
  2979. }
  2980. }
  2981. if (!ImplicitFirstprivates.empty()) {
  2982. if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
  2983. ImplicitFirstprivates, SourceLocation(), SourceLocation(),
  2984. SourceLocation())) {
  2985. ClausesWithImplicit.push_back(Implicit);
  2986. ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
  2987. ImplicitFirstprivates.size();
  2988. } else {
  2989. ErrorFound = true;
  2990. }
  2991. }
  2992. if (!ImplicitMaps.empty()) {
  2993. if (OMPClause *Implicit = ActOnOpenMPMapClause(
  2994. OMPC_MAP_unknown, OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true,
  2995. SourceLocation(), SourceLocation(), ImplicitMaps,
  2996. SourceLocation(), SourceLocation(), SourceLocation())) {
  2997. ClausesWithImplicit.emplace_back(Implicit);
  2998. ErrorFound |=
  2999. cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
  3000. } else {
  3001. ErrorFound = true;
  3002. }
  3003. }
  3004. }
  3005. llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
  3006. switch (Kind) {
  3007. case OMPD_parallel:
  3008. Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
  3009. EndLoc);
  3010. AllowedNameModifiers.push_back(OMPD_parallel);
  3011. break;
  3012. case OMPD_simd:
  3013. Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
  3014. VarsWithInheritedDSA);
  3015. break;
  3016. case OMPD_for:
  3017. Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
  3018. VarsWithInheritedDSA);
  3019. break;
  3020. case OMPD_for_simd:
  3021. Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
  3022. EndLoc, VarsWithInheritedDSA);
  3023. break;
  3024. case OMPD_sections:
  3025. Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
  3026. EndLoc);
  3027. break;
  3028. case OMPD_section:
  3029. assert(ClausesWithImplicit.empty() &&
  3030. "No clauses are allowed for 'omp section' directive");
  3031. Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
  3032. break;
  3033. case OMPD_single:
  3034. Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
  3035. EndLoc);
  3036. break;
  3037. case OMPD_master:
  3038. assert(ClausesWithImplicit.empty() &&
  3039. "No clauses are allowed for 'omp master' directive");
  3040. Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
  3041. break;
  3042. case OMPD_critical:
  3043. Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
  3044. StartLoc, EndLoc);
  3045. break;
  3046. case OMPD_parallel_for:
  3047. Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
  3048. EndLoc, VarsWithInheritedDSA);
  3049. AllowedNameModifiers.push_back(OMPD_parallel);
  3050. break;
  3051. case OMPD_parallel_for_simd:
  3052. Res = ActOnOpenMPParallelForSimdDirective(
  3053. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3054. AllowedNameModifiers.push_back(OMPD_parallel);
  3055. break;
  3056. case OMPD_parallel_sections:
  3057. Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
  3058. StartLoc, EndLoc);
  3059. AllowedNameModifiers.push_back(OMPD_parallel);
  3060. break;
  3061. case OMPD_task:
  3062. Res =
  3063. ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
  3064. AllowedNameModifiers.push_back(OMPD_task);
  3065. break;
  3066. case OMPD_taskyield:
  3067. assert(ClausesWithImplicit.empty() &&
  3068. "No clauses are allowed for 'omp taskyield' directive");
  3069. assert(AStmt == nullptr &&
  3070. "No associated statement allowed for 'omp taskyield' directive");
  3071. Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
  3072. break;
  3073. case OMPD_barrier:
  3074. assert(ClausesWithImplicit.empty() &&
  3075. "No clauses are allowed for 'omp barrier' directive");
  3076. assert(AStmt == nullptr &&
  3077. "No associated statement allowed for 'omp barrier' directive");
  3078. Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
  3079. break;
  3080. case OMPD_taskwait:
  3081. assert(ClausesWithImplicit.empty() &&
  3082. "No clauses are allowed for 'omp taskwait' directive");
  3083. assert(AStmt == nullptr &&
  3084. "No associated statement allowed for 'omp taskwait' directive");
  3085. Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
  3086. break;
  3087. case OMPD_taskgroup:
  3088. Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
  3089. EndLoc);
  3090. break;
  3091. case OMPD_flush:
  3092. assert(AStmt == nullptr &&
  3093. "No associated statement allowed for 'omp flush' directive");
  3094. Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
  3095. break;
  3096. case OMPD_ordered:
  3097. Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
  3098. EndLoc);
  3099. break;
  3100. case OMPD_atomic:
  3101. Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
  3102. EndLoc);
  3103. break;
  3104. case OMPD_teams:
  3105. Res =
  3106. ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
  3107. break;
  3108. case OMPD_target:
  3109. Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
  3110. EndLoc);
  3111. AllowedNameModifiers.push_back(OMPD_target);
  3112. break;
  3113. case OMPD_target_parallel:
  3114. Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
  3115. StartLoc, EndLoc);
  3116. AllowedNameModifiers.push_back(OMPD_target);
  3117. AllowedNameModifiers.push_back(OMPD_parallel);
  3118. break;
  3119. case OMPD_target_parallel_for:
  3120. Res = ActOnOpenMPTargetParallelForDirective(
  3121. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3122. AllowedNameModifiers.push_back(OMPD_target);
  3123. AllowedNameModifiers.push_back(OMPD_parallel);
  3124. break;
  3125. case OMPD_cancellation_point:
  3126. assert(ClausesWithImplicit.empty() &&
  3127. "No clauses are allowed for 'omp cancellation point' directive");
  3128. assert(AStmt == nullptr && "No associated statement allowed for 'omp "
  3129. "cancellation point' directive");
  3130. Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
  3131. break;
  3132. case OMPD_cancel:
  3133. assert(AStmt == nullptr &&
  3134. "No associated statement allowed for 'omp cancel' directive");
  3135. Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
  3136. CancelRegion);
  3137. AllowedNameModifiers.push_back(OMPD_cancel);
  3138. break;
  3139. case OMPD_target_data:
  3140. Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
  3141. EndLoc);
  3142. AllowedNameModifiers.push_back(OMPD_target_data);
  3143. break;
  3144. case OMPD_target_enter_data:
  3145. Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
  3146. EndLoc, AStmt);
  3147. AllowedNameModifiers.push_back(OMPD_target_enter_data);
  3148. break;
  3149. case OMPD_target_exit_data:
  3150. Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
  3151. EndLoc, AStmt);
  3152. AllowedNameModifiers.push_back(OMPD_target_exit_data);
  3153. break;
  3154. case OMPD_taskloop:
  3155. Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
  3156. EndLoc, VarsWithInheritedDSA);
  3157. AllowedNameModifiers.push_back(OMPD_taskloop);
  3158. break;
  3159. case OMPD_taskloop_simd:
  3160. Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
  3161. EndLoc, VarsWithInheritedDSA);
  3162. AllowedNameModifiers.push_back(OMPD_taskloop);
  3163. break;
  3164. case OMPD_distribute:
  3165. Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
  3166. EndLoc, VarsWithInheritedDSA);
  3167. break;
  3168. case OMPD_target_update:
  3169. Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
  3170. EndLoc, AStmt);
  3171. AllowedNameModifiers.push_back(OMPD_target_update);
  3172. break;
  3173. case OMPD_distribute_parallel_for:
  3174. Res = ActOnOpenMPDistributeParallelForDirective(
  3175. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3176. AllowedNameModifiers.push_back(OMPD_parallel);
  3177. break;
  3178. case OMPD_distribute_parallel_for_simd:
  3179. Res = ActOnOpenMPDistributeParallelForSimdDirective(
  3180. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3181. AllowedNameModifiers.push_back(OMPD_parallel);
  3182. break;
  3183. case OMPD_distribute_simd:
  3184. Res = ActOnOpenMPDistributeSimdDirective(
  3185. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3186. break;
  3187. case OMPD_target_parallel_for_simd:
  3188. Res = ActOnOpenMPTargetParallelForSimdDirective(
  3189. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3190. AllowedNameModifiers.push_back(OMPD_target);
  3191. AllowedNameModifiers.push_back(OMPD_parallel);
  3192. break;
  3193. case OMPD_target_simd:
  3194. Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
  3195. EndLoc, VarsWithInheritedDSA);
  3196. AllowedNameModifiers.push_back(OMPD_target);
  3197. break;
  3198. case OMPD_teams_distribute:
  3199. Res = ActOnOpenMPTeamsDistributeDirective(
  3200. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3201. break;
  3202. case OMPD_teams_distribute_simd:
  3203. Res = ActOnOpenMPTeamsDistributeSimdDirective(
  3204. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3205. break;
  3206. case OMPD_teams_distribute_parallel_for_simd:
  3207. Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
  3208. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3209. AllowedNameModifiers.push_back(OMPD_parallel);
  3210. break;
  3211. case OMPD_teams_distribute_parallel_for:
  3212. Res = ActOnOpenMPTeamsDistributeParallelForDirective(
  3213. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3214. AllowedNameModifiers.push_back(OMPD_parallel);
  3215. break;
  3216. case OMPD_target_teams:
  3217. Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
  3218. EndLoc);
  3219. AllowedNameModifiers.push_back(OMPD_target);
  3220. break;
  3221. case OMPD_target_teams_distribute:
  3222. Res = ActOnOpenMPTargetTeamsDistributeDirective(
  3223. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3224. AllowedNameModifiers.push_back(OMPD_target);
  3225. break;
  3226. case OMPD_target_teams_distribute_parallel_for:
  3227. Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
  3228. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3229. AllowedNameModifiers.push_back(OMPD_target);
  3230. AllowedNameModifiers.push_back(OMPD_parallel);
  3231. break;
  3232. case OMPD_target_teams_distribute_parallel_for_simd:
  3233. Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
  3234. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3235. AllowedNameModifiers.push_back(OMPD_target);
  3236. AllowedNameModifiers.push_back(OMPD_parallel);
  3237. break;
  3238. case OMPD_target_teams_distribute_simd:
  3239. Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
  3240. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3241. AllowedNameModifiers.push_back(OMPD_target);
  3242. break;
  3243. case OMPD_declare_target:
  3244. case OMPD_end_declare_target:
  3245. case OMPD_threadprivate:
  3246. case OMPD_declare_reduction:
  3247. case OMPD_declare_simd:
  3248. case OMPD_requires:
  3249. llvm_unreachable("OpenMP Directive is not allowed");
  3250. case OMPD_unknown:
  3251. llvm_unreachable("Unknown OpenMP directive");
  3252. }
  3253. for (const auto &P : VarsWithInheritedDSA) {
  3254. Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
  3255. << P.first << P.second->getSourceRange();
  3256. }
  3257. ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
  3258. if (!AllowedNameModifiers.empty())
  3259. ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
  3260. ErrorFound;
  3261. if (ErrorFound)
  3262. return StmtError();
  3263. return Res;
  3264. }
  3265. Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
  3266. DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
  3267. ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
  3268. ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
  3269. ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
  3270. assert(Aligneds.size() == Alignments.size());
  3271. assert(Linears.size() == LinModifiers.size());
  3272. assert(Linears.size() == Steps.size());
  3273. if (!DG || DG.get().isNull())
  3274. return DeclGroupPtrTy();
  3275. if (!DG.get().isSingleDecl()) {
  3276. Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
  3277. return DG;
  3278. }
  3279. Decl *ADecl = DG.get().getSingleDecl();
  3280. if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
  3281. ADecl = FTD->getTemplatedDecl();
  3282. auto *FD = dyn_cast<FunctionDecl>(ADecl);
  3283. if (!FD) {
  3284. Diag(ADecl->getLocation(), diag::err_omp_function_expected);
  3285. return DeclGroupPtrTy();
  3286. }
  3287. // OpenMP [2.8.2, declare simd construct, Description]
  3288. // The parameter of the simdlen clause must be a constant positive integer
  3289. // expression.
  3290. ExprResult SL;
  3291. if (Simdlen)
  3292. SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
  3293. // OpenMP [2.8.2, declare simd construct, Description]
  3294. // The special this pointer can be used as if was one of the arguments to the
  3295. // function in any of the linear, aligned, or uniform clauses.
  3296. // The uniform clause declares one or more arguments to have an invariant
  3297. // value for all concurrent invocations of the function in the execution of a
  3298. // single SIMD loop.
  3299. llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
  3300. const Expr *UniformedLinearThis = nullptr;
  3301. for (const Expr *E : Uniforms) {
  3302. E = E->IgnoreParenImpCasts();
  3303. if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
  3304. if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
  3305. if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
  3306. FD->getParamDecl(PVD->getFunctionScopeIndex())
  3307. ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
  3308. UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
  3309. continue;
  3310. }
  3311. if (isa<CXXThisExpr>(E)) {
  3312. UniformedLinearThis = E;
  3313. continue;
  3314. }
  3315. Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
  3316. << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
  3317. }
  3318. // OpenMP [2.8.2, declare simd construct, Description]
  3319. // The aligned clause declares that the object to which each list item points
  3320. // is aligned to the number of bytes expressed in the optional parameter of
  3321. // the aligned clause.
  3322. // The special this pointer can be used as if was one of the arguments to the
  3323. // function in any of the linear, aligned, or uniform clauses.
  3324. // The type of list items appearing in the aligned clause must be array,
  3325. // pointer, reference to array, or reference to pointer.
  3326. llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
  3327. const Expr *AlignedThis = nullptr;
  3328. for (const Expr *E : Aligneds) {
  3329. E = E->IgnoreParenImpCasts();
  3330. if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
  3331. if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
  3332. const VarDecl *CanonPVD = PVD->getCanonicalDecl();
  3333. if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
  3334. FD->getParamDecl(PVD->getFunctionScopeIndex())
  3335. ->getCanonicalDecl() == CanonPVD) {
  3336. // OpenMP [2.8.1, simd construct, Restrictions]
  3337. // A list-item cannot appear in more than one aligned clause.
  3338. if (AlignedArgs.count(CanonPVD) > 0) {
  3339. Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
  3340. << 1 << E->getSourceRange();
  3341. Diag(AlignedArgs[CanonPVD]->getExprLoc(),
  3342. diag::note_omp_explicit_dsa)
  3343. << getOpenMPClauseName(OMPC_aligned);
  3344. continue;
  3345. }
  3346. AlignedArgs[CanonPVD] = E;
  3347. QualType QTy = PVD->getType()
  3348. .getNonReferenceType()
  3349. .getUnqualifiedType()
  3350. .getCanonicalType();
  3351. const Type *Ty = QTy.getTypePtrOrNull();
  3352. if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
  3353. Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
  3354. << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
  3355. Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
  3356. }
  3357. continue;
  3358. }
  3359. }
  3360. if (isa<CXXThisExpr>(E)) {
  3361. if (AlignedThis) {
  3362. Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
  3363. << 2 << E->getSourceRange();
  3364. Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
  3365. << getOpenMPClauseName(OMPC_aligned);
  3366. }
  3367. AlignedThis = E;
  3368. continue;
  3369. }
  3370. Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
  3371. << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
  3372. }
  3373. // The optional parameter of the aligned clause, alignment, must be a constant
  3374. // positive integer expression. If no optional parameter is specified,
  3375. // implementation-defined default alignments for SIMD instructions on the
  3376. // target platforms are assumed.
  3377. SmallVector<const Expr *, 4> NewAligns;
  3378. for (Expr *E : Alignments) {
  3379. ExprResult Align;
  3380. if (E)
  3381. Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
  3382. NewAligns.push_back(Align.get());
  3383. }
  3384. // OpenMP [2.8.2, declare simd construct, Description]
  3385. // The linear clause declares one or more list items to be private to a SIMD
  3386. // lane and to have a linear relationship with respect to the iteration space
  3387. // of a loop.
  3388. // The special this pointer can be used as if was one of the arguments to the
  3389. // function in any of the linear, aligned, or uniform clauses.
  3390. // When a linear-step expression is specified in a linear clause it must be
  3391. // either a constant integer expression or an integer-typed parameter that is
  3392. // specified in a uniform clause on the directive.
  3393. llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
  3394. const bool IsUniformedThis = UniformedLinearThis != nullptr;
  3395. auto MI = LinModifiers.begin();
  3396. for (const Expr *E : Linears) {
  3397. auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
  3398. ++MI;
  3399. E = E->IgnoreParenImpCasts();
  3400. if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
  3401. if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
  3402. const VarDecl *CanonPVD = PVD->getCanonicalDecl();
  3403. if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
  3404. FD->getParamDecl(PVD->getFunctionScopeIndex())
  3405. ->getCanonicalDecl() == CanonPVD) {
  3406. // OpenMP [2.15.3.7, linear Clause, Restrictions]
  3407. // A list-item cannot appear in more than one linear clause.
  3408. if (LinearArgs.count(CanonPVD) > 0) {
  3409. Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
  3410. << getOpenMPClauseName(OMPC_linear)
  3411. << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
  3412. Diag(LinearArgs[CanonPVD]->getExprLoc(),
  3413. diag::note_omp_explicit_dsa)
  3414. << getOpenMPClauseName(OMPC_linear);
  3415. continue;
  3416. }
  3417. // Each argument can appear in at most one uniform or linear clause.
  3418. if (UniformedArgs.count(CanonPVD) > 0) {
  3419. Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
  3420. << getOpenMPClauseName(OMPC_linear)
  3421. << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
  3422. Diag(UniformedArgs[CanonPVD]->getExprLoc(),
  3423. diag::note_omp_explicit_dsa)
  3424. << getOpenMPClauseName(OMPC_uniform);
  3425. continue;
  3426. }
  3427. LinearArgs[CanonPVD] = E;
  3428. if (E->isValueDependent() || E->isTypeDependent() ||
  3429. E->isInstantiationDependent() ||
  3430. E->containsUnexpandedParameterPack())
  3431. continue;
  3432. (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
  3433. PVD->getOriginalType());
  3434. continue;
  3435. }
  3436. }
  3437. if (isa<CXXThisExpr>(E)) {
  3438. if (UniformedLinearThis) {
  3439. Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
  3440. << getOpenMPClauseName(OMPC_linear)
  3441. << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
  3442. << E->getSourceRange();
  3443. Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
  3444. << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
  3445. : OMPC_linear);
  3446. continue;
  3447. }
  3448. UniformedLinearThis = E;
  3449. if (E->isValueDependent() || E->isTypeDependent() ||
  3450. E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
  3451. continue;
  3452. (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
  3453. E->getType());
  3454. continue;
  3455. }
  3456. Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
  3457. << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
  3458. }
  3459. Expr *Step = nullptr;
  3460. Expr *NewStep = nullptr;
  3461. SmallVector<Expr *, 4> NewSteps;
  3462. for (Expr *E : Steps) {
  3463. // Skip the same step expression, it was checked already.
  3464. if (Step == E || !E) {
  3465. NewSteps.push_back(E ? NewStep : nullptr);
  3466. continue;
  3467. }
  3468. Step = E;
  3469. if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
  3470. if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
  3471. const VarDecl *CanonPVD = PVD->getCanonicalDecl();
  3472. if (UniformedArgs.count(CanonPVD) == 0) {
  3473. Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
  3474. << Step->getSourceRange();
  3475. } else if (E->isValueDependent() || E->isTypeDependent() ||
  3476. E->isInstantiationDependent() ||
  3477. E->containsUnexpandedParameterPack() ||
  3478. CanonPVD->getType()->hasIntegerRepresentation()) {
  3479. NewSteps.push_back(Step);
  3480. } else {
  3481. Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
  3482. << Step->getSourceRange();
  3483. }
  3484. continue;
  3485. }
  3486. NewStep = Step;
  3487. if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
  3488. !Step->isInstantiationDependent() &&
  3489. !Step->containsUnexpandedParameterPack()) {
  3490. NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
  3491. .get();
  3492. if (NewStep)
  3493. NewStep = VerifyIntegerConstantExpression(NewStep).get();
  3494. }
  3495. NewSteps.push_back(NewStep);
  3496. }
  3497. auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
  3498. Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
  3499. Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
  3500. const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
  3501. const_cast<Expr **>(Linears.data()), Linears.size(),
  3502. const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
  3503. NewSteps.data(), NewSteps.size(), SR);
  3504. ADecl->addAttr(NewAttr);
  3505. return ConvertDeclToDeclGroup(ADecl);
  3506. }
  3507. StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
  3508. Stmt *AStmt,
  3509. SourceLocation StartLoc,
  3510. SourceLocation EndLoc) {
  3511. if (!AStmt)
  3512. return StmtError();
  3513. auto *CS = cast<CapturedStmt>(AStmt);
  3514. // 1.2.2 OpenMP Language Terminology
  3515. // Structured block - An executable statement with a single entry at the
  3516. // top and a single exit at the bottom.
  3517. // The point of exit cannot be a branch out of the structured block.
  3518. // longjmp() and throw() must not violate the entry/exit criteria.
  3519. CS->getCapturedDecl()->setNothrow();
  3520. setFunctionHasBranchProtectedScope();
  3521. return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
  3522. DSAStack->isCancelRegion());
  3523. }
  3524. namespace {
  3525. /// Helper class for checking canonical form of the OpenMP loops and
  3526. /// extracting iteration space of each loop in the loop nest, that will be used
  3527. /// for IR generation.
  3528. class OpenMPIterationSpaceChecker {
  3529. /// Reference to Sema.
  3530. Sema &SemaRef;
  3531. /// A location for diagnostics (when there is no some better location).
  3532. SourceLocation DefaultLoc;
  3533. /// A location for diagnostics (when increment is not compatible).
  3534. SourceLocation ConditionLoc;
  3535. /// A source location for referring to loop init later.
  3536. SourceRange InitSrcRange;
  3537. /// A source location for referring to condition later.
  3538. SourceRange ConditionSrcRange;
  3539. /// A source location for referring to increment later.
  3540. SourceRange IncrementSrcRange;
  3541. /// Loop variable.
  3542. ValueDecl *LCDecl = nullptr;
  3543. /// Reference to loop variable.
  3544. Expr *LCRef = nullptr;
  3545. /// Lower bound (initializer for the var).
  3546. Expr *LB = nullptr;
  3547. /// Upper bound.
  3548. Expr *UB = nullptr;
  3549. /// Loop step (increment).
  3550. Expr *Step = nullptr;
  3551. /// This flag is true when condition is one of:
  3552. /// Var < UB
  3553. /// Var <= UB
  3554. /// UB > Var
  3555. /// UB >= Var
  3556. bool TestIsLessOp = false;
  3557. /// This flag is true when condition is strict ( < or > ).
  3558. bool TestIsStrictOp = false;
  3559. /// This flag is true when step is subtracted on each iteration.
  3560. bool SubtractStep = false;
  3561. public:
  3562. OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
  3563. : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
  3564. /// Check init-expr for canonical loop form and save loop counter
  3565. /// variable - #Var and its initialization value - #LB.
  3566. bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
  3567. /// Check test-expr for canonical form, save upper-bound (#UB), flags
  3568. /// for less/greater and for strict/non-strict comparison.
  3569. bool checkAndSetCond(Expr *S);
  3570. /// Check incr-expr for canonical loop form and return true if it
  3571. /// does not conform, otherwise save loop step (#Step).
  3572. bool checkAndSetInc(Expr *S);
  3573. /// Return the loop counter variable.
  3574. ValueDecl *getLoopDecl() const { return LCDecl; }
  3575. /// Return the reference expression to loop counter variable.
  3576. Expr *getLoopDeclRefExpr() const { return LCRef; }
  3577. /// Source range of the loop init.
  3578. SourceRange getInitSrcRange() const { return InitSrcRange; }
  3579. /// Source range of the loop condition.
  3580. SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
  3581. /// Source range of the loop increment.
  3582. SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
  3583. /// True if the step should be subtracted.
  3584. bool shouldSubtractStep() const { return SubtractStep; }
  3585. /// Build the expression to calculate the number of iterations.
  3586. Expr *buildNumIterations(
  3587. Scope *S, const bool LimitedType,
  3588. llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
  3589. /// Build the precondition expression for the loops.
  3590. Expr *
  3591. buildPreCond(Scope *S, Expr *Cond,
  3592. llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
  3593. /// Build reference expression to the counter be used for codegen.
  3594. DeclRefExpr *
  3595. buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
  3596. DSAStackTy &DSA) const;
  3597. /// Build reference expression to the private counter be used for
  3598. /// codegen.
  3599. Expr *buildPrivateCounterVar() const;
  3600. /// Build initialization of the counter be used for codegen.
  3601. Expr *buildCounterInit() const;
  3602. /// Build step of the counter be used for codegen.
  3603. Expr *buildCounterStep() const;
  3604. /// Build loop data with counter value for depend clauses in ordered
  3605. /// directives.
  3606. Expr *
  3607. buildOrderedLoopData(Scope *S, Expr *Counter,
  3608. llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
  3609. SourceLocation Loc, Expr *Inc = nullptr,
  3610. OverloadedOperatorKind OOK = OO_Amp);
  3611. /// Return true if any expression is dependent.
  3612. bool dependent() const;
  3613. private:
  3614. /// Check the right-hand side of an assignment in the increment
  3615. /// expression.
  3616. bool checkAndSetIncRHS(Expr *RHS);
  3617. /// Helper to set loop counter variable and its initializer.
  3618. bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
  3619. /// Helper to set upper bound.
  3620. bool setUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
  3621. SourceLocation SL);
  3622. /// Helper to set loop increment.
  3623. bool setStep(Expr *NewStep, bool Subtract);
  3624. };
  3625. bool OpenMPIterationSpaceChecker::dependent() const {
  3626. if (!LCDecl) {
  3627. assert(!LB && !UB && !Step);
  3628. return false;
  3629. }
  3630. return LCDecl->getType()->isDependentType() ||
  3631. (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
  3632. (Step && Step->isValueDependent());
  3633. }
  3634. bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
  3635. Expr *NewLCRefExpr,
  3636. Expr *NewLB) {
  3637. // State consistency checking to ensure correct usage.
  3638. assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
  3639. UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
  3640. if (!NewLCDecl || !NewLB)
  3641. return true;
  3642. LCDecl = getCanonicalDecl(NewLCDecl);
  3643. LCRef = NewLCRefExpr;
  3644. if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
  3645. if (const CXXConstructorDecl *Ctor = CE->getConstructor())
  3646. if ((Ctor->isCopyOrMoveConstructor() ||
  3647. Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
  3648. CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
  3649. NewLB = CE->getArg(0)->IgnoreParenImpCasts();
  3650. LB = NewLB;
  3651. return false;
  3652. }
  3653. bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, bool LessOp, bool StrictOp,
  3654. SourceRange SR, SourceLocation SL) {
  3655. // State consistency checking to ensure correct usage.
  3656. assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
  3657. Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
  3658. if (!NewUB)
  3659. return true;
  3660. UB = NewUB;
  3661. TestIsLessOp = LessOp;
  3662. TestIsStrictOp = StrictOp;
  3663. ConditionSrcRange = SR;
  3664. ConditionLoc = SL;
  3665. return false;
  3666. }
  3667. bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
  3668. // State consistency checking to ensure correct usage.
  3669. assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
  3670. if (!NewStep)
  3671. return true;
  3672. if (!NewStep->isValueDependent()) {
  3673. // Check that the step is integer expression.
  3674. SourceLocation StepLoc = NewStep->getBeginLoc();
  3675. ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
  3676. StepLoc, getExprAsWritten(NewStep));
  3677. if (Val.isInvalid())
  3678. return true;
  3679. NewStep = Val.get();
  3680. // OpenMP [2.6, Canonical Loop Form, Restrictions]
  3681. // If test-expr is of form var relational-op b and relational-op is < or
  3682. // <= then incr-expr must cause var to increase on each iteration of the
  3683. // loop. If test-expr is of form var relational-op b and relational-op is
  3684. // > or >= then incr-expr must cause var to decrease on each iteration of
  3685. // the loop.
  3686. // If test-expr is of form b relational-op var and relational-op is < or
  3687. // <= then incr-expr must cause var to decrease on each iteration of the
  3688. // loop. If test-expr is of form b relational-op var and relational-op is
  3689. // > or >= then incr-expr must cause var to increase on each iteration of
  3690. // the loop.
  3691. llvm::APSInt Result;
  3692. bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
  3693. bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
  3694. bool IsConstNeg =
  3695. IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
  3696. bool IsConstPos =
  3697. IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
  3698. bool IsConstZero = IsConstant && !Result.getBoolValue();
  3699. if (UB && (IsConstZero ||
  3700. (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
  3701. : (IsConstPos || (IsUnsigned && !Subtract))))) {
  3702. SemaRef.Diag(NewStep->getExprLoc(),
  3703. diag::err_omp_loop_incr_not_compatible)
  3704. << LCDecl << TestIsLessOp << NewStep->getSourceRange();
  3705. SemaRef.Diag(ConditionLoc,
  3706. diag::note_omp_loop_cond_requres_compatible_incr)
  3707. << TestIsLessOp << ConditionSrcRange;
  3708. return true;
  3709. }
  3710. if (TestIsLessOp == Subtract) {
  3711. NewStep =
  3712. SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
  3713. .get();
  3714. Subtract = !Subtract;
  3715. }
  3716. }
  3717. Step = NewStep;
  3718. SubtractStep = Subtract;
  3719. return false;
  3720. }
  3721. bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
  3722. // Check init-expr for canonical loop form and save loop counter
  3723. // variable - #Var and its initialization value - #LB.
  3724. // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
  3725. // var = lb
  3726. // integer-type var = lb
  3727. // random-access-iterator-type var = lb
  3728. // pointer-type var = lb
  3729. //
  3730. if (!S) {
  3731. if (EmitDiags) {
  3732. SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
  3733. }
  3734. return true;
  3735. }
  3736. if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
  3737. if (!ExprTemp->cleanupsHaveSideEffects())
  3738. S = ExprTemp->getSubExpr();
  3739. InitSrcRange = S->getSourceRange();
  3740. if (Expr *E = dyn_cast<Expr>(S))
  3741. S = E->IgnoreParens();
  3742. if (auto *BO = dyn_cast<BinaryOperator>(S)) {
  3743. if (BO->getOpcode() == BO_Assign) {
  3744. Expr *LHS = BO->getLHS()->IgnoreParens();
  3745. if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
  3746. if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
  3747. if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
  3748. return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
  3749. return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
  3750. }
  3751. if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
  3752. if (ME->isArrow() &&
  3753. isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
  3754. return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
  3755. }
  3756. }
  3757. } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
  3758. if (DS->isSingleDecl()) {
  3759. if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
  3760. if (Var->hasInit() && !Var->getType()->isReferenceType()) {
  3761. // Accept non-canonical init form here but emit ext. warning.
  3762. if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
  3763. SemaRef.Diag(S->getBeginLoc(),
  3764. diag::ext_omp_loop_not_canonical_init)
  3765. << S->getSourceRange();
  3766. return setLCDeclAndLB(
  3767. Var,
  3768. buildDeclRefExpr(SemaRef, Var,
  3769. Var->getType().getNonReferenceType(),
  3770. DS->getBeginLoc()),
  3771. Var->getInit());
  3772. }
  3773. }
  3774. }
  3775. } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
  3776. if (CE->getOperator() == OO_Equal) {
  3777. Expr *LHS = CE->getArg(0);
  3778. if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
  3779. if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
  3780. if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
  3781. return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
  3782. return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
  3783. }
  3784. if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
  3785. if (ME->isArrow() &&
  3786. isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
  3787. return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
  3788. }
  3789. }
  3790. }
  3791. if (dependent() || SemaRef.CurContext->isDependentContext())
  3792. return false;
  3793. if (EmitDiags) {
  3794. SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
  3795. << S->getSourceRange();
  3796. }
  3797. return true;
  3798. }
  3799. /// Ignore parenthesizes, implicit casts, copy constructor and return the
  3800. /// variable (which may be the loop variable) if possible.
  3801. static const ValueDecl *getInitLCDecl(const Expr *E) {
  3802. if (!E)
  3803. return nullptr;
  3804. E = getExprAsWritten(E);
  3805. if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
  3806. if (const CXXConstructorDecl *Ctor = CE->getConstructor())
  3807. if ((Ctor->isCopyOrMoveConstructor() ||
  3808. Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
  3809. CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
  3810. E = CE->getArg(0)->IgnoreParenImpCasts();
  3811. if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
  3812. if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
  3813. return getCanonicalDecl(VD);
  3814. }
  3815. if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
  3816. if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
  3817. return getCanonicalDecl(ME->getMemberDecl());
  3818. return nullptr;
  3819. }
  3820. bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
  3821. // Check test-expr for canonical form, save upper-bound UB, flags for
  3822. // less/greater and for strict/non-strict comparison.
  3823. // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
  3824. // var relational-op b
  3825. // b relational-op var
  3826. //
  3827. if (!S) {
  3828. SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
  3829. return true;
  3830. }
  3831. S = getExprAsWritten(S);
  3832. SourceLocation CondLoc = S->getBeginLoc();
  3833. if (auto *BO = dyn_cast<BinaryOperator>(S)) {
  3834. if (BO->isRelationalOp()) {
  3835. if (getInitLCDecl(BO->getLHS()) == LCDecl)
  3836. return setUB(BO->getRHS(),
  3837. (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
  3838. (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
  3839. BO->getSourceRange(), BO->getOperatorLoc());
  3840. if (getInitLCDecl(BO->getRHS()) == LCDecl)
  3841. return setUB(BO->getLHS(),
  3842. (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
  3843. (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
  3844. BO->getSourceRange(), BO->getOperatorLoc());
  3845. }
  3846. } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
  3847. if (CE->getNumArgs() == 2) {
  3848. auto Op = CE->getOperator();
  3849. switch (Op) {
  3850. case OO_Greater:
  3851. case OO_GreaterEqual:
  3852. case OO_Less:
  3853. case OO_LessEqual:
  3854. if (getInitLCDecl(CE->getArg(0)) == LCDecl)
  3855. return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
  3856. Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
  3857. CE->getOperatorLoc());
  3858. if (getInitLCDecl(CE->getArg(1)) == LCDecl)
  3859. return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
  3860. Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
  3861. CE->getOperatorLoc());
  3862. break;
  3863. default:
  3864. break;
  3865. }
  3866. }
  3867. }
  3868. if (dependent() || SemaRef.CurContext->isDependentContext())
  3869. return false;
  3870. SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
  3871. << S->getSourceRange() << LCDecl;
  3872. return true;
  3873. }
  3874. bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
  3875. // RHS of canonical loop form increment can be:
  3876. // var + incr
  3877. // incr + var
  3878. // var - incr
  3879. //
  3880. RHS = RHS->IgnoreParenImpCasts();
  3881. if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
  3882. if (BO->isAdditiveOp()) {
  3883. bool IsAdd = BO->getOpcode() == BO_Add;
  3884. if (getInitLCDecl(BO->getLHS()) == LCDecl)
  3885. return setStep(BO->getRHS(), !IsAdd);
  3886. if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
  3887. return setStep(BO->getLHS(), /*Subtract=*/false);
  3888. }
  3889. } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
  3890. bool IsAdd = CE->getOperator() == OO_Plus;
  3891. if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
  3892. if (getInitLCDecl(CE->getArg(0)) == LCDecl)
  3893. return setStep(CE->getArg(1), !IsAdd);
  3894. if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
  3895. return setStep(CE->getArg(0), /*Subtract=*/false);
  3896. }
  3897. }
  3898. if (dependent() || SemaRef.CurContext->isDependentContext())
  3899. return false;
  3900. SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
  3901. << RHS->getSourceRange() << LCDecl;
  3902. return true;
  3903. }
  3904. bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
  3905. // Check incr-expr for canonical loop form and return true if it
  3906. // does not conform.
  3907. // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
  3908. // ++var
  3909. // var++
  3910. // --var
  3911. // var--
  3912. // var += incr
  3913. // var -= incr
  3914. // var = var + incr
  3915. // var = incr + var
  3916. // var = var - incr
  3917. //
  3918. if (!S) {
  3919. SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
  3920. return true;
  3921. }
  3922. if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
  3923. if (!ExprTemp->cleanupsHaveSideEffects())
  3924. S = ExprTemp->getSubExpr();
  3925. IncrementSrcRange = S->getSourceRange();
  3926. S = S->IgnoreParens();
  3927. if (auto *UO = dyn_cast<UnaryOperator>(S)) {
  3928. if (UO->isIncrementDecrementOp() &&
  3929. getInitLCDecl(UO->getSubExpr()) == LCDecl)
  3930. return setStep(SemaRef
  3931. .ActOnIntegerConstant(UO->getBeginLoc(),
  3932. (UO->isDecrementOp() ? -1 : 1))
  3933. .get(),
  3934. /*Subtract=*/false);
  3935. } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
  3936. switch (BO->getOpcode()) {
  3937. case BO_AddAssign:
  3938. case BO_SubAssign:
  3939. if (getInitLCDecl(BO->getLHS()) == LCDecl)
  3940. return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
  3941. break;
  3942. case BO_Assign:
  3943. if (getInitLCDecl(BO->getLHS()) == LCDecl)
  3944. return checkAndSetIncRHS(BO->getRHS());
  3945. break;
  3946. default:
  3947. break;
  3948. }
  3949. } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
  3950. switch (CE->getOperator()) {
  3951. case OO_PlusPlus:
  3952. case OO_MinusMinus:
  3953. if (getInitLCDecl(CE->getArg(0)) == LCDecl)
  3954. return setStep(SemaRef
  3955. .ActOnIntegerConstant(
  3956. CE->getBeginLoc(),
  3957. ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
  3958. .get(),
  3959. /*Subtract=*/false);
  3960. break;
  3961. case OO_PlusEqual:
  3962. case OO_MinusEqual:
  3963. if (getInitLCDecl(CE->getArg(0)) == LCDecl)
  3964. return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
  3965. break;
  3966. case OO_Equal:
  3967. if (getInitLCDecl(CE->getArg(0)) == LCDecl)
  3968. return checkAndSetIncRHS(CE->getArg(1));
  3969. break;
  3970. default:
  3971. break;
  3972. }
  3973. }
  3974. if (dependent() || SemaRef.CurContext->isDependentContext())
  3975. return false;
  3976. SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
  3977. << S->getSourceRange() << LCDecl;
  3978. return true;
  3979. }
  3980. static ExprResult
  3981. tryBuildCapture(Sema &SemaRef, Expr *Capture,
  3982. llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
  3983. if (SemaRef.CurContext->isDependentContext())
  3984. return ExprResult(Capture);
  3985. if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
  3986. return SemaRef.PerformImplicitConversion(
  3987. Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
  3988. /*AllowExplicit=*/true);
  3989. auto I = Captures.find(Capture);
  3990. if (I != Captures.end())
  3991. return buildCapture(SemaRef, Capture, I->second);
  3992. DeclRefExpr *Ref = nullptr;
  3993. ExprResult Res = buildCapture(SemaRef, Capture, Ref);
  3994. Captures[Capture] = Ref;
  3995. return Res;
  3996. }
  3997. /// Build the expression to calculate the number of iterations.
  3998. Expr *OpenMPIterationSpaceChecker::buildNumIterations(
  3999. Scope *S, const bool LimitedType,
  4000. llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
  4001. ExprResult Diff;
  4002. QualType VarType = LCDecl->getType().getNonReferenceType();
  4003. if (VarType->isIntegerType() || VarType->isPointerType() ||
  4004. SemaRef.getLangOpts().CPlusPlus) {
  4005. // Upper - Lower
  4006. Expr *UBExpr = TestIsLessOp ? UB : LB;
  4007. Expr *LBExpr = TestIsLessOp ? LB : UB;
  4008. Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
  4009. Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
  4010. if (!Upper || !Lower)
  4011. return nullptr;
  4012. Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
  4013. if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
  4014. // BuildBinOp already emitted error, this one is to point user to upper
  4015. // and lower bound, and to tell what is passed to 'operator-'.
  4016. SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
  4017. << Upper->getSourceRange() << Lower->getSourceRange();
  4018. return nullptr;
  4019. }
  4020. }
  4021. if (!Diff.isUsable())
  4022. return nullptr;
  4023. // Upper - Lower [- 1]
  4024. if (TestIsStrictOp)
  4025. Diff = SemaRef.BuildBinOp(
  4026. S, DefaultLoc, BO_Sub, Diff.get(),
  4027. SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
  4028. if (!Diff.isUsable())
  4029. return nullptr;
  4030. // Upper - Lower [- 1] + Step
  4031. ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
  4032. if (!NewStep.isUsable())
  4033. return nullptr;
  4034. Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
  4035. if (!Diff.isUsable())
  4036. return nullptr;
  4037. // Parentheses (for dumping/debugging purposes only).
  4038. Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
  4039. if (!Diff.isUsable())
  4040. return nullptr;
  4041. // (Upper - Lower [- 1] + Step) / Step
  4042. Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
  4043. if (!Diff.isUsable())
  4044. return nullptr;
  4045. // OpenMP runtime requires 32-bit or 64-bit loop variables.
  4046. QualType Type = Diff.get()->getType();
  4047. ASTContext &C = SemaRef.Context;
  4048. bool UseVarType = VarType->hasIntegerRepresentation() &&
  4049. C.getTypeSize(Type) > C.getTypeSize(VarType);
  4050. if (!Type->isIntegerType() || UseVarType) {
  4051. unsigned NewSize =
  4052. UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
  4053. bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
  4054. : Type->hasSignedIntegerRepresentation();
  4055. Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
  4056. if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
  4057. Diff = SemaRef.PerformImplicitConversion(
  4058. Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
  4059. if (!Diff.isUsable())
  4060. return nullptr;
  4061. }
  4062. }
  4063. if (LimitedType) {
  4064. unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
  4065. if (NewSize != C.getTypeSize(Type)) {
  4066. if (NewSize < C.getTypeSize(Type)) {
  4067. assert(NewSize == 64 && "incorrect loop var size");
  4068. SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
  4069. << InitSrcRange << ConditionSrcRange;
  4070. }
  4071. QualType NewType = C.getIntTypeForBitwidth(
  4072. NewSize, Type->hasSignedIntegerRepresentation() ||
  4073. C.getTypeSize(Type) < NewSize);
  4074. if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
  4075. Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
  4076. Sema::AA_Converting, true);
  4077. if (!Diff.isUsable())
  4078. return nullptr;
  4079. }
  4080. }
  4081. }
  4082. return Diff.get();
  4083. }
  4084. Expr *OpenMPIterationSpaceChecker::buildPreCond(
  4085. Scope *S, Expr *Cond,
  4086. llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
  4087. // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
  4088. bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
  4089. SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
  4090. ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
  4091. ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
  4092. if (!NewLB.isUsable() || !NewUB.isUsable())
  4093. return nullptr;
  4094. ExprResult CondExpr =
  4095. SemaRef.BuildBinOp(S, DefaultLoc,
  4096. TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
  4097. : (TestIsStrictOp ? BO_GT : BO_GE),
  4098. NewLB.get(), NewUB.get());
  4099. if (CondExpr.isUsable()) {
  4100. if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
  4101. SemaRef.Context.BoolTy))
  4102. CondExpr = SemaRef.PerformImplicitConversion(
  4103. CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
  4104. /*AllowExplicit=*/true);
  4105. }
  4106. SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
  4107. // Otherwise use original loop conditon and evaluate it in runtime.
  4108. return CondExpr.isUsable() ? CondExpr.get() : Cond;
  4109. }
  4110. /// Build reference expression to the counter be used for codegen.
  4111. DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
  4112. llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
  4113. DSAStackTy &DSA) const {
  4114. auto *VD = dyn_cast<VarDecl>(LCDecl);
  4115. if (!VD) {
  4116. VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
  4117. DeclRefExpr *Ref = buildDeclRefExpr(
  4118. SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
  4119. const DSAStackTy::DSAVarData Data =
  4120. DSA.getTopDSA(LCDecl, /*FromParent=*/false);
  4121. // If the loop control decl is explicitly marked as private, do not mark it
  4122. // as captured again.
  4123. if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
  4124. Captures.insert(std::make_pair(LCRef, Ref));
  4125. return Ref;
  4126. }
  4127. return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
  4128. DefaultLoc);
  4129. }
  4130. Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
  4131. if (LCDecl && !LCDecl->isInvalidDecl()) {
  4132. QualType Type = LCDecl->getType().getNonReferenceType();
  4133. VarDecl *PrivateVar = buildVarDecl(
  4134. SemaRef, DefaultLoc, Type, LCDecl->getName(),
  4135. LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
  4136. isa<VarDecl>(LCDecl)
  4137. ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
  4138. : nullptr);
  4139. if (PrivateVar->isInvalidDecl())
  4140. return nullptr;
  4141. return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
  4142. }
  4143. return nullptr;
  4144. }
  4145. /// Build initialization of the counter to be used for codegen.
  4146. Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
  4147. /// Build step of the counter be used for codegen.
  4148. Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
  4149. Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
  4150. Scope *S, Expr *Counter,
  4151. llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
  4152. Expr *Inc, OverloadedOperatorKind OOK) {
  4153. Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
  4154. if (!Cnt)
  4155. return nullptr;
  4156. if (Inc) {
  4157. assert((OOK == OO_Plus || OOK == OO_Minus) &&
  4158. "Expected only + or - operations for depend clauses.");
  4159. BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
  4160. Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
  4161. if (!Cnt)
  4162. return nullptr;
  4163. }
  4164. ExprResult Diff;
  4165. QualType VarType = LCDecl->getType().getNonReferenceType();
  4166. if (VarType->isIntegerType() || VarType->isPointerType() ||
  4167. SemaRef.getLangOpts().CPlusPlus) {
  4168. // Upper - Lower
  4169. Expr *Upper =
  4170. TestIsLessOp ? Cnt : tryBuildCapture(SemaRef, UB, Captures).get();
  4171. Expr *Lower =
  4172. TestIsLessOp ? tryBuildCapture(SemaRef, LB, Captures).get() : Cnt;
  4173. if (!Upper || !Lower)
  4174. return nullptr;
  4175. Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
  4176. if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
  4177. // BuildBinOp already emitted error, this one is to point user to upper
  4178. // and lower bound, and to tell what is passed to 'operator-'.
  4179. SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
  4180. << Upper->getSourceRange() << Lower->getSourceRange();
  4181. return nullptr;
  4182. }
  4183. }
  4184. if (!Diff.isUsable())
  4185. return nullptr;
  4186. // Parentheses (for dumping/debugging purposes only).
  4187. Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
  4188. if (!Diff.isUsable())
  4189. return nullptr;
  4190. ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
  4191. if (!NewStep.isUsable())
  4192. return nullptr;
  4193. // (Upper - Lower) / Step
  4194. Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
  4195. if (!Diff.isUsable())
  4196. return nullptr;
  4197. return Diff.get();
  4198. }
  4199. /// Iteration space of a single for loop.
  4200. struct LoopIterationSpace final {
  4201. /// Condition of the loop.
  4202. Expr *PreCond = nullptr;
  4203. /// This expression calculates the number of iterations in the loop.
  4204. /// It is always possible to calculate it before starting the loop.
  4205. Expr *NumIterations = nullptr;
  4206. /// The loop counter variable.
  4207. Expr *CounterVar = nullptr;
  4208. /// Private loop counter variable.
  4209. Expr *PrivateCounterVar = nullptr;
  4210. /// This is initializer for the initial value of #CounterVar.
  4211. Expr *CounterInit = nullptr;
  4212. /// This is step for the #CounterVar used to generate its update:
  4213. /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
  4214. Expr *CounterStep = nullptr;
  4215. /// Should step be subtracted?
  4216. bool Subtract = false;
  4217. /// Source range of the loop init.
  4218. SourceRange InitSrcRange;
  4219. /// Source range of the loop condition.
  4220. SourceRange CondSrcRange;
  4221. /// Source range of the loop increment.
  4222. SourceRange IncSrcRange;
  4223. };
  4224. } // namespace
  4225. void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
  4226. assert(getLangOpts().OpenMP && "OpenMP is not active.");
  4227. assert(Init && "Expected loop in canonical form.");
  4228. unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
  4229. if (AssociatedLoops > 0 &&
  4230. isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
  4231. OpenMPIterationSpaceChecker ISC(*this, ForLoc);
  4232. if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
  4233. if (ValueDecl *D = ISC.getLoopDecl()) {
  4234. auto *VD = dyn_cast<VarDecl>(D);
  4235. if (!VD) {
  4236. if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
  4237. VD = Private;
  4238. } else {
  4239. DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
  4240. /*WithInit=*/false);
  4241. VD = cast<VarDecl>(Ref->getDecl());
  4242. }
  4243. }
  4244. DSAStack->addLoopControlVariable(D, VD);
  4245. }
  4246. }
  4247. DSAStack->setAssociatedLoops(AssociatedLoops - 1);
  4248. }
  4249. }
  4250. /// Called on a for stmt to check and extract its iteration space
  4251. /// for further processing (such as collapsing).
  4252. static bool checkOpenMPIterationSpace(
  4253. OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
  4254. unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
  4255. unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
  4256. Expr *OrderedLoopCountExpr,
  4257. Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
  4258. LoopIterationSpace &ResultIterSpace,
  4259. llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
  4260. // OpenMP [2.6, Canonical Loop Form]
  4261. // for (init-expr; test-expr; incr-expr) structured-block
  4262. auto *For = dyn_cast_or_null<ForStmt>(S);
  4263. if (!For) {
  4264. SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
  4265. << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
  4266. << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
  4267. << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
  4268. if (TotalNestedLoopCount > 1) {
  4269. if (CollapseLoopCountExpr && OrderedLoopCountExpr)
  4270. SemaRef.Diag(DSA.getConstructLoc(),
  4271. diag::note_omp_collapse_ordered_expr)
  4272. << 2 << CollapseLoopCountExpr->getSourceRange()
  4273. << OrderedLoopCountExpr->getSourceRange();
  4274. else if (CollapseLoopCountExpr)
  4275. SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
  4276. diag::note_omp_collapse_ordered_expr)
  4277. << 0 << CollapseLoopCountExpr->getSourceRange();
  4278. else
  4279. SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
  4280. diag::note_omp_collapse_ordered_expr)
  4281. << 1 << OrderedLoopCountExpr->getSourceRange();
  4282. }
  4283. return true;
  4284. }
  4285. assert(For->getBody());
  4286. OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
  4287. // Check init.
  4288. Stmt *Init = For->getInit();
  4289. if (ISC.checkAndSetInit(Init))
  4290. return true;
  4291. bool HasErrors = false;
  4292. // Check loop variable's type.
  4293. if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
  4294. Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
  4295. // OpenMP [2.6, Canonical Loop Form]
  4296. // Var is one of the following:
  4297. // A variable of signed or unsigned integer type.
  4298. // For C++, a variable of a random access iterator type.
  4299. // For C, a variable of a pointer type.
  4300. QualType VarType = LCDecl->getType().getNonReferenceType();
  4301. if (!VarType->isDependentType() && !VarType->isIntegerType() &&
  4302. !VarType->isPointerType() &&
  4303. !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
  4304. SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
  4305. << SemaRef.getLangOpts().CPlusPlus;
  4306. HasErrors = true;
  4307. }
  4308. // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
  4309. // a Construct
  4310. // The loop iteration variable(s) in the associated for-loop(s) of a for or
  4311. // parallel for construct is (are) private.
  4312. // The loop iteration variable in the associated for-loop of a simd
  4313. // construct with just one associated for-loop is linear with a
  4314. // constant-linear-step that is the increment of the associated for-loop.
  4315. // Exclude loop var from the list of variables with implicitly defined data
  4316. // sharing attributes.
  4317. VarsWithImplicitDSA.erase(LCDecl);
  4318. // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
  4319. // in a Construct, C/C++].
  4320. // The loop iteration variable in the associated for-loop of a simd
  4321. // construct with just one associated for-loop may be listed in a linear
  4322. // clause with a constant-linear-step that is the increment of the
  4323. // associated for-loop.
  4324. // The loop iteration variable(s) in the associated for-loop(s) of a for or
  4325. // parallel for construct may be listed in a private or lastprivate clause.
  4326. DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
  4327. // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
  4328. // declared in the loop and it is predetermined as a private.
  4329. OpenMPClauseKind PredeterminedCKind =
  4330. isOpenMPSimdDirective(DKind)
  4331. ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
  4332. : OMPC_private;
  4333. if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
  4334. DVar.CKind != PredeterminedCKind) ||
  4335. ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
  4336. isOpenMPDistributeDirective(DKind)) &&
  4337. !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
  4338. DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
  4339. (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
  4340. SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
  4341. << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
  4342. << getOpenMPClauseName(PredeterminedCKind);
  4343. if (DVar.RefExpr == nullptr)
  4344. DVar.CKind = PredeterminedCKind;
  4345. reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
  4346. HasErrors = true;
  4347. } else if (LoopDeclRefExpr != nullptr) {
  4348. // Make the loop iteration variable private (for worksharing constructs),
  4349. // linear (for simd directives with the only one associated loop) or
  4350. // lastprivate (for simd directives with several collapsed or ordered
  4351. // loops).
  4352. if (DVar.CKind == OMPC_unknown)
  4353. DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
  4354. [](OpenMPDirectiveKind) -> bool { return true; },
  4355. /*FromParent=*/false);
  4356. DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
  4357. }
  4358. assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
  4359. // Check test-expr.
  4360. HasErrors |= ISC.checkAndSetCond(For->getCond());
  4361. // Check incr-expr.
  4362. HasErrors |= ISC.checkAndSetInc(For->getInc());
  4363. }
  4364. if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
  4365. return HasErrors;
  4366. // Build the loop's iteration space representation.
  4367. ResultIterSpace.PreCond =
  4368. ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures);
  4369. ResultIterSpace.NumIterations = ISC.buildNumIterations(
  4370. DSA.getCurScope(),
  4371. (isOpenMPWorksharingDirective(DKind) ||
  4372. isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
  4373. Captures);
  4374. ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA);
  4375. ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar();
  4376. ResultIterSpace.CounterInit = ISC.buildCounterInit();
  4377. ResultIterSpace.CounterStep = ISC.buildCounterStep();
  4378. ResultIterSpace.InitSrcRange = ISC.getInitSrcRange();
  4379. ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange();
  4380. ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange();
  4381. ResultIterSpace.Subtract = ISC.shouldSubtractStep();
  4382. HasErrors |= (ResultIterSpace.PreCond == nullptr ||
  4383. ResultIterSpace.NumIterations == nullptr ||
  4384. ResultIterSpace.CounterVar == nullptr ||
  4385. ResultIterSpace.PrivateCounterVar == nullptr ||
  4386. ResultIterSpace.CounterInit == nullptr ||
  4387. ResultIterSpace.CounterStep == nullptr);
  4388. if (!HasErrors && DSA.isOrderedRegion()) {
  4389. if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
  4390. if (CurrentNestedLoopCount <
  4391. DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
  4392. DSA.getOrderedRegionParam().second->setLoopNumIterations(
  4393. CurrentNestedLoopCount, ResultIterSpace.NumIterations);
  4394. DSA.getOrderedRegionParam().second->setLoopCounter(
  4395. CurrentNestedLoopCount, ResultIterSpace.CounterVar);
  4396. }
  4397. }
  4398. for (auto &Pair : DSA.getDoacrossDependClauses()) {
  4399. if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
  4400. // Erroneous case - clause has some problems.
  4401. continue;
  4402. }
  4403. if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
  4404. Pair.second.size() <= CurrentNestedLoopCount) {
  4405. // Erroneous case - clause has some problems.
  4406. Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
  4407. continue;
  4408. }
  4409. Expr *CntValue;
  4410. if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
  4411. CntValue = ISC.buildOrderedLoopData(
  4412. DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
  4413. Pair.first->getDependencyLoc());
  4414. else
  4415. CntValue = ISC.buildOrderedLoopData(
  4416. DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
  4417. Pair.first->getDependencyLoc(),
  4418. Pair.second[CurrentNestedLoopCount].first,
  4419. Pair.second[CurrentNestedLoopCount].second);
  4420. Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
  4421. }
  4422. }
  4423. return HasErrors;
  4424. }
  4425. /// Build 'VarRef = Start.
  4426. static ExprResult
  4427. buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
  4428. ExprResult Start,
  4429. llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
  4430. // Build 'VarRef = Start.
  4431. ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
  4432. if (!NewStart.isUsable())
  4433. return ExprError();
  4434. if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
  4435. VarRef.get()->getType())) {
  4436. NewStart = SemaRef.PerformImplicitConversion(
  4437. NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
  4438. /*AllowExplicit=*/true);
  4439. if (!NewStart.isUsable())
  4440. return ExprError();
  4441. }
  4442. ExprResult Init =
  4443. SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
  4444. return Init;
  4445. }
  4446. /// Build 'VarRef = Start + Iter * Step'.
  4447. static ExprResult buildCounterUpdate(
  4448. Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
  4449. ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
  4450. llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
  4451. // Add parentheses (for debugging purposes only).
  4452. Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
  4453. if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
  4454. !Step.isUsable())
  4455. return ExprError();
  4456. ExprResult NewStep = Step;
  4457. if (Captures)
  4458. NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
  4459. if (NewStep.isInvalid())
  4460. return ExprError();
  4461. ExprResult Update =
  4462. SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
  4463. if (!Update.isUsable())
  4464. return ExprError();
  4465. // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
  4466. // 'VarRef = Start (+|-) Iter * Step'.
  4467. ExprResult NewStart = Start;
  4468. if (Captures)
  4469. NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
  4470. if (NewStart.isInvalid())
  4471. return ExprError();
  4472. // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
  4473. ExprResult SavedUpdate = Update;
  4474. ExprResult UpdateVal;
  4475. if (VarRef.get()->getType()->isOverloadableType() ||
  4476. NewStart.get()->getType()->isOverloadableType() ||
  4477. Update.get()->getType()->isOverloadableType()) {
  4478. bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
  4479. SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
  4480. Update =
  4481. SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
  4482. if (Update.isUsable()) {
  4483. UpdateVal =
  4484. SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
  4485. VarRef.get(), SavedUpdate.get());
  4486. if (UpdateVal.isUsable()) {
  4487. Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
  4488. UpdateVal.get());
  4489. }
  4490. }
  4491. SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
  4492. }
  4493. // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
  4494. if (!Update.isUsable() || !UpdateVal.isUsable()) {
  4495. Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
  4496. NewStart.get(), SavedUpdate.get());
  4497. if (!Update.isUsable())
  4498. return ExprError();
  4499. if (!SemaRef.Context.hasSameType(Update.get()->getType(),
  4500. VarRef.get()->getType())) {
  4501. Update = SemaRef.PerformImplicitConversion(
  4502. Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
  4503. if (!Update.isUsable())
  4504. return ExprError();
  4505. }
  4506. Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
  4507. }
  4508. return Update;
  4509. }
  4510. /// Convert integer expression \a E to make it have at least \a Bits
  4511. /// bits.
  4512. static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
  4513. if (E == nullptr)
  4514. return ExprError();
  4515. ASTContext &C = SemaRef.Context;
  4516. QualType OldType = E->getType();
  4517. unsigned HasBits = C.getTypeSize(OldType);
  4518. if (HasBits >= Bits)
  4519. return ExprResult(E);
  4520. // OK to convert to signed, because new type has more bits than old.
  4521. QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
  4522. return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
  4523. true);
  4524. }
  4525. /// Check if the given expression \a E is a constant integer that fits
  4526. /// into \a Bits bits.
  4527. static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
  4528. if (E == nullptr)
  4529. return false;
  4530. llvm::APSInt Result;
  4531. if (E->isIntegerConstantExpr(Result, SemaRef.Context))
  4532. return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
  4533. return false;
  4534. }
  4535. /// Build preinits statement for the given declarations.
  4536. static Stmt *buildPreInits(ASTContext &Context,
  4537. MutableArrayRef<Decl *> PreInits) {
  4538. if (!PreInits.empty()) {
  4539. return new (Context) DeclStmt(
  4540. DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
  4541. SourceLocation(), SourceLocation());
  4542. }
  4543. return nullptr;
  4544. }
  4545. /// Build preinits statement for the given declarations.
  4546. static Stmt *
  4547. buildPreInits(ASTContext &Context,
  4548. const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
  4549. if (!Captures.empty()) {
  4550. SmallVector<Decl *, 16> PreInits;
  4551. for (const auto &Pair : Captures)
  4552. PreInits.push_back(Pair.second->getDecl());
  4553. return buildPreInits(Context, PreInits);
  4554. }
  4555. return nullptr;
  4556. }
  4557. /// Build postupdate expression for the given list of postupdates expressions.
  4558. static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
  4559. Expr *PostUpdate = nullptr;
  4560. if (!PostUpdates.empty()) {
  4561. for (Expr *E : PostUpdates) {
  4562. Expr *ConvE = S.BuildCStyleCastExpr(
  4563. E->getExprLoc(),
  4564. S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
  4565. E->getExprLoc(), E)
  4566. .get();
  4567. PostUpdate = PostUpdate
  4568. ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
  4569. PostUpdate, ConvE)
  4570. .get()
  4571. : ConvE;
  4572. }
  4573. }
  4574. return PostUpdate;
  4575. }
  4576. /// Called on a for stmt to check itself and nested loops (if any).
  4577. /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
  4578. /// number of collapsed loops otherwise.
  4579. static unsigned
  4580. checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
  4581. Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
  4582. DSAStackTy &DSA,
  4583. Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
  4584. OMPLoopDirective::HelperExprs &Built) {
  4585. unsigned NestedLoopCount = 1;
  4586. if (CollapseLoopCountExpr) {
  4587. // Found 'collapse' clause - calculate collapse number.
  4588. llvm::APSInt Result;
  4589. if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
  4590. NestedLoopCount = Result.getLimitedValue();
  4591. }
  4592. unsigned OrderedLoopCount = 1;
  4593. if (OrderedLoopCountExpr) {
  4594. // Found 'ordered' clause - calculate collapse number.
  4595. llvm::APSInt Result;
  4596. if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
  4597. if (Result.getLimitedValue() < NestedLoopCount) {
  4598. SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
  4599. diag::err_omp_wrong_ordered_loop_count)
  4600. << OrderedLoopCountExpr->getSourceRange();
  4601. SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
  4602. diag::note_collapse_loop_count)
  4603. << CollapseLoopCountExpr->getSourceRange();
  4604. }
  4605. OrderedLoopCount = Result.getLimitedValue();
  4606. }
  4607. }
  4608. // This is helper routine for loop directives (e.g., 'for', 'simd',
  4609. // 'for simd', etc.).
  4610. llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
  4611. SmallVector<LoopIterationSpace, 4> IterSpaces;
  4612. IterSpaces.resize(std::max(OrderedLoopCount, NestedLoopCount));
  4613. Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
  4614. for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
  4615. if (checkOpenMPIterationSpace(
  4616. DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
  4617. std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
  4618. OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
  4619. Captures))
  4620. return 0;
  4621. // Move on to the next nested for loop, or to the loop body.
  4622. // OpenMP [2.8.1, simd construct, Restrictions]
  4623. // All loops associated with the construct must be perfectly nested; that
  4624. // is, there must be no intervening code nor any OpenMP directive between
  4625. // any two loops.
  4626. CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
  4627. }
  4628. for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
  4629. if (checkOpenMPIterationSpace(
  4630. DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
  4631. std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
  4632. OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
  4633. Captures))
  4634. return 0;
  4635. if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
  4636. // Handle initialization of captured loop iterator variables.
  4637. auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
  4638. if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
  4639. Captures[DRE] = DRE;
  4640. }
  4641. }
  4642. // Move on to the next nested for loop, or to the loop body.
  4643. // OpenMP [2.8.1, simd construct, Restrictions]
  4644. // All loops associated with the construct must be perfectly nested; that
  4645. // is, there must be no intervening code nor any OpenMP directive between
  4646. // any two loops.
  4647. CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
  4648. }
  4649. Built.clear(/* size */ NestedLoopCount);
  4650. if (SemaRef.CurContext->isDependentContext())
  4651. return NestedLoopCount;
  4652. // An example of what is generated for the following code:
  4653. //
  4654. // #pragma omp simd collapse(2) ordered(2)
  4655. // for (i = 0; i < NI; ++i)
  4656. // for (k = 0; k < NK; ++k)
  4657. // for (j = J0; j < NJ; j+=2) {
  4658. // <loop body>
  4659. // }
  4660. //
  4661. // We generate the code below.
  4662. // Note: the loop body may be outlined in CodeGen.
  4663. // Note: some counters may be C++ classes, operator- is used to find number of
  4664. // iterations and operator+= to calculate counter value.
  4665. // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
  4666. // or i64 is currently supported).
  4667. //
  4668. // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
  4669. // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
  4670. // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
  4671. // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
  4672. // // similar updates for vars in clauses (e.g. 'linear')
  4673. // <loop body (using local i and j)>
  4674. // }
  4675. // i = NI; // assign final values of counters
  4676. // j = NJ;
  4677. //
  4678. // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
  4679. // the iteration counts of the collapsed for loops.
  4680. // Precondition tests if there is at least one iteration (all conditions are
  4681. // true).
  4682. auto PreCond = ExprResult(IterSpaces[0].PreCond);
  4683. Expr *N0 = IterSpaces[0].NumIterations;
  4684. ExprResult LastIteration32 =
  4685. widenIterationCount(/*Bits=*/32,
  4686. SemaRef
  4687. .PerformImplicitConversion(
  4688. N0->IgnoreImpCasts(), N0->getType(),
  4689. Sema::AA_Converting, /*AllowExplicit=*/true)
  4690. .get(),
  4691. SemaRef);
  4692. ExprResult LastIteration64 = widenIterationCount(
  4693. /*Bits=*/64,
  4694. SemaRef
  4695. .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
  4696. Sema::AA_Converting,
  4697. /*AllowExplicit=*/true)
  4698. .get(),
  4699. SemaRef);
  4700. if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
  4701. return NestedLoopCount;
  4702. ASTContext &C = SemaRef.Context;
  4703. bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
  4704. Scope *CurScope = DSA.getCurScope();
  4705. for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
  4706. if (PreCond.isUsable()) {
  4707. PreCond =
  4708. SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
  4709. PreCond.get(), IterSpaces[Cnt].PreCond);
  4710. }
  4711. Expr *N = IterSpaces[Cnt].NumIterations;
  4712. SourceLocation Loc = N->getExprLoc();
  4713. AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
  4714. if (LastIteration32.isUsable())
  4715. LastIteration32 = SemaRef.BuildBinOp(
  4716. CurScope, Loc, BO_Mul, LastIteration32.get(),
  4717. SemaRef
  4718. .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
  4719. Sema::AA_Converting,
  4720. /*AllowExplicit=*/true)
  4721. .get());
  4722. if (LastIteration64.isUsable())
  4723. LastIteration64 = SemaRef.BuildBinOp(
  4724. CurScope, Loc, BO_Mul, LastIteration64.get(),
  4725. SemaRef
  4726. .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
  4727. Sema::AA_Converting,
  4728. /*AllowExplicit=*/true)
  4729. .get());
  4730. }
  4731. // Choose either the 32-bit or 64-bit version.
  4732. ExprResult LastIteration = LastIteration64;
  4733. if (LastIteration32.isUsable() &&
  4734. C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
  4735. (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
  4736. fitsInto(
  4737. /*Bits=*/32,
  4738. LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
  4739. LastIteration64.get(), SemaRef)))
  4740. LastIteration = LastIteration32;
  4741. QualType VType = LastIteration.get()->getType();
  4742. QualType RealVType = VType;
  4743. QualType StrideVType = VType;
  4744. if (isOpenMPTaskLoopDirective(DKind)) {
  4745. VType =
  4746. SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
  4747. StrideVType =
  4748. SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
  4749. }
  4750. if (!LastIteration.isUsable())
  4751. return 0;
  4752. // Save the number of iterations.
  4753. ExprResult NumIterations = LastIteration;
  4754. {
  4755. LastIteration = SemaRef.BuildBinOp(
  4756. CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
  4757. LastIteration.get(),
  4758. SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
  4759. if (!LastIteration.isUsable())
  4760. return 0;
  4761. }
  4762. // Calculate the last iteration number beforehand instead of doing this on
  4763. // each iteration. Do not do this if the number of iterations may be kfold-ed.
  4764. llvm::APSInt Result;
  4765. bool IsConstant =
  4766. LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
  4767. ExprResult CalcLastIteration;
  4768. if (!IsConstant) {
  4769. ExprResult SaveRef =
  4770. tryBuildCapture(SemaRef, LastIteration.get(), Captures);
  4771. LastIteration = SaveRef;
  4772. // Prepare SaveRef + 1.
  4773. NumIterations = SemaRef.BuildBinOp(
  4774. CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
  4775. SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
  4776. if (!NumIterations.isUsable())
  4777. return 0;
  4778. }
  4779. SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
  4780. // Build variables passed into runtime, necessary for worksharing directives.
  4781. ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
  4782. if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
  4783. isOpenMPDistributeDirective(DKind)) {
  4784. // Lower bound variable, initialized with zero.
  4785. VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
  4786. LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
  4787. SemaRef.AddInitializerToDecl(LBDecl,
  4788. SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
  4789. /*DirectInit*/ false);
  4790. // Upper bound variable, initialized with last iteration number.
  4791. VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
  4792. UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
  4793. SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
  4794. /*DirectInit*/ false);
  4795. // A 32-bit variable-flag where runtime returns 1 for the last iteration.
  4796. // This will be used to implement clause 'lastprivate'.
  4797. QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
  4798. VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
  4799. IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
  4800. SemaRef.AddInitializerToDecl(ILDecl,
  4801. SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
  4802. /*DirectInit*/ false);
  4803. // Stride variable returned by runtime (we initialize it to 1 by default).
  4804. VarDecl *STDecl =
  4805. buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
  4806. ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
  4807. SemaRef.AddInitializerToDecl(STDecl,
  4808. SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
  4809. /*DirectInit*/ false);
  4810. // Build expression: UB = min(UB, LastIteration)
  4811. // It is necessary for CodeGen of directives with static scheduling.
  4812. ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
  4813. UB.get(), LastIteration.get());
  4814. ExprResult CondOp = SemaRef.ActOnConditionalOp(
  4815. LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
  4816. LastIteration.get(), UB.get());
  4817. EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
  4818. CondOp.get());
  4819. EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
  4820. // If we have a combined directive that combines 'distribute', 'for' or
  4821. // 'simd' we need to be able to access the bounds of the schedule of the
  4822. // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
  4823. // by scheduling 'distribute' have to be passed to the schedule of 'for'.
  4824. if (isOpenMPLoopBoundSharingDirective(DKind)) {
  4825. // Lower bound variable, initialized with zero.
  4826. VarDecl *CombLBDecl =
  4827. buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
  4828. CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
  4829. SemaRef.AddInitializerToDecl(
  4830. CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
  4831. /*DirectInit*/ false);
  4832. // Upper bound variable, initialized with last iteration number.
  4833. VarDecl *CombUBDecl =
  4834. buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
  4835. CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
  4836. SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
  4837. /*DirectInit*/ false);
  4838. ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
  4839. CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
  4840. ExprResult CombCondOp =
  4841. SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
  4842. LastIteration.get(), CombUB.get());
  4843. CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
  4844. CombCondOp.get());
  4845. CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get());
  4846. const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
  4847. // We expect to have at least 2 more parameters than the 'parallel'
  4848. // directive does - the lower and upper bounds of the previous schedule.
  4849. assert(CD->getNumParams() >= 4 &&
  4850. "Unexpected number of parameters in loop combined directive");
  4851. // Set the proper type for the bounds given what we learned from the
  4852. // enclosed loops.
  4853. ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
  4854. ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
  4855. // Previous lower and upper bounds are obtained from the region
  4856. // parameters.
  4857. PrevLB =
  4858. buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
  4859. PrevUB =
  4860. buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
  4861. }
  4862. }
  4863. // Build the iteration variable and its initialization before loop.
  4864. ExprResult IV;
  4865. ExprResult Init, CombInit;
  4866. {
  4867. VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
  4868. IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
  4869. Expr *RHS =
  4870. (isOpenMPWorksharingDirective(DKind) ||
  4871. isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
  4872. ? LB.get()
  4873. : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
  4874. Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
  4875. Init = SemaRef.ActOnFinishFullExpr(Init.get());
  4876. if (isOpenMPLoopBoundSharingDirective(DKind)) {
  4877. Expr *CombRHS =
  4878. (isOpenMPWorksharingDirective(DKind) ||
  4879. isOpenMPTaskLoopDirective(DKind) ||
  4880. isOpenMPDistributeDirective(DKind))
  4881. ? CombLB.get()
  4882. : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
  4883. CombInit =
  4884. SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
  4885. CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get());
  4886. }
  4887. }
  4888. // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
  4889. SourceLocation CondLoc = AStmt->getBeginLoc();
  4890. ExprResult Cond =
  4891. (isOpenMPWorksharingDirective(DKind) ||
  4892. isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
  4893. ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
  4894. : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
  4895. NumIterations.get());
  4896. ExprResult CombCond;
  4897. if (isOpenMPLoopBoundSharingDirective(DKind)) {
  4898. CombCond =
  4899. SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get());
  4900. }
  4901. // Loop increment (IV = IV + 1)
  4902. SourceLocation IncLoc = AStmt->getBeginLoc();
  4903. ExprResult Inc =
  4904. SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
  4905. SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
  4906. if (!Inc.isUsable())
  4907. return 0;
  4908. Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
  4909. Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
  4910. if (!Inc.isUsable())
  4911. return 0;
  4912. // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
  4913. // Used for directives with static scheduling.
  4914. // In combined construct, add combined version that use CombLB and CombUB
  4915. // base variables for the update
  4916. ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
  4917. if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
  4918. isOpenMPDistributeDirective(DKind)) {
  4919. // LB + ST
  4920. NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
  4921. if (!NextLB.isUsable())
  4922. return 0;
  4923. // LB = LB + ST
  4924. NextLB =
  4925. SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
  4926. NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
  4927. if (!NextLB.isUsable())
  4928. return 0;
  4929. // UB + ST
  4930. NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
  4931. if (!NextUB.isUsable())
  4932. return 0;
  4933. // UB = UB + ST
  4934. NextUB =
  4935. SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
  4936. NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
  4937. if (!NextUB.isUsable())
  4938. return 0;
  4939. if (isOpenMPLoopBoundSharingDirective(DKind)) {
  4940. CombNextLB =
  4941. SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
  4942. if (!NextLB.isUsable())
  4943. return 0;
  4944. // LB = LB + ST
  4945. CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
  4946. CombNextLB.get());
  4947. CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get());
  4948. if (!CombNextLB.isUsable())
  4949. return 0;
  4950. // UB + ST
  4951. CombNextUB =
  4952. SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
  4953. if (!CombNextUB.isUsable())
  4954. return 0;
  4955. // UB = UB + ST
  4956. CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
  4957. CombNextUB.get());
  4958. CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get());
  4959. if (!CombNextUB.isUsable())
  4960. return 0;
  4961. }
  4962. }
  4963. // Create increment expression for distribute loop when combined in a same
  4964. // directive with for as IV = IV + ST; ensure upper bound expression based
  4965. // on PrevUB instead of NumIterations - used to implement 'for' when found
  4966. // in combination with 'distribute', like in 'distribute parallel for'
  4967. SourceLocation DistIncLoc = AStmt->getBeginLoc();
  4968. ExprResult DistCond, DistInc, PrevEUB;
  4969. if (isOpenMPLoopBoundSharingDirective(DKind)) {
  4970. DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get());
  4971. assert(DistCond.isUsable() && "distribute cond expr was not built");
  4972. DistInc =
  4973. SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
  4974. assert(DistInc.isUsable() && "distribute inc expr was not built");
  4975. DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
  4976. DistInc.get());
  4977. DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get());
  4978. assert(DistInc.isUsable() && "distribute inc expr was not built");
  4979. // Build expression: UB = min(UB, prevUB) for #for in composite or combined
  4980. // construct
  4981. SourceLocation DistEUBLoc = AStmt->getBeginLoc();
  4982. ExprResult IsUBGreater =
  4983. SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
  4984. ExprResult CondOp = SemaRef.ActOnConditionalOp(
  4985. DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
  4986. PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
  4987. CondOp.get());
  4988. PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get());
  4989. }
  4990. // Build updates and final values of the loop counters.
  4991. bool HasErrors = false;
  4992. Built.Counters.resize(NestedLoopCount);
  4993. Built.Inits.resize(NestedLoopCount);
  4994. Built.Updates.resize(NestedLoopCount);
  4995. Built.Finals.resize(NestedLoopCount);
  4996. {
  4997. ExprResult Div;
  4998. // Go from inner nested loop to outer.
  4999. for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
  5000. LoopIterationSpace &IS = IterSpaces[Cnt];
  5001. SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
  5002. // Build: Iter = (IV / Div) % IS.NumIters
  5003. // where Div is product of previous iterations' IS.NumIters.
  5004. ExprResult Iter;
  5005. if (Div.isUsable()) {
  5006. Iter =
  5007. SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
  5008. } else {
  5009. Iter = IV;
  5010. assert((Cnt == (int)NestedLoopCount - 1) &&
  5011. "unusable div expected on first iteration only");
  5012. }
  5013. if (Cnt != 0 && Iter.isUsable())
  5014. Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
  5015. IS.NumIterations);
  5016. if (!Iter.isUsable()) {
  5017. HasErrors = true;
  5018. break;
  5019. }
  5020. // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
  5021. auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
  5022. DeclRefExpr *CounterVar = buildDeclRefExpr(
  5023. SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
  5024. /*RefersToCapture=*/true);
  5025. ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
  5026. IS.CounterInit, Captures);
  5027. if (!Init.isUsable()) {
  5028. HasErrors = true;
  5029. break;
  5030. }
  5031. ExprResult Update = buildCounterUpdate(
  5032. SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
  5033. IS.CounterStep, IS.Subtract, &Captures);
  5034. if (!Update.isUsable()) {
  5035. HasErrors = true;
  5036. break;
  5037. }
  5038. // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
  5039. ExprResult Final = buildCounterUpdate(
  5040. SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
  5041. IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
  5042. if (!Final.isUsable()) {
  5043. HasErrors = true;
  5044. break;
  5045. }
  5046. // Build Div for the next iteration: Div <- Div * IS.NumIters
  5047. if (Cnt != 0) {
  5048. if (Div.isUnset())
  5049. Div = IS.NumIterations;
  5050. else
  5051. Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
  5052. IS.NumIterations);
  5053. // Add parentheses (for debugging purposes only).
  5054. if (Div.isUsable())
  5055. Div = tryBuildCapture(SemaRef, Div.get(), Captures);
  5056. if (!Div.isUsable()) {
  5057. HasErrors = true;
  5058. break;
  5059. }
  5060. }
  5061. if (!Update.isUsable() || !Final.isUsable()) {
  5062. HasErrors = true;
  5063. break;
  5064. }
  5065. // Save results
  5066. Built.Counters[Cnt] = IS.CounterVar;
  5067. Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
  5068. Built.Inits[Cnt] = Init.get();
  5069. Built.Updates[Cnt] = Update.get();
  5070. Built.Finals[Cnt] = Final.get();
  5071. }
  5072. }
  5073. if (HasErrors)
  5074. return 0;
  5075. // Save results
  5076. Built.IterationVarRef = IV.get();
  5077. Built.LastIteration = LastIteration.get();
  5078. Built.NumIterations = NumIterations.get();
  5079. Built.CalcLastIteration =
  5080. SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
  5081. Built.PreCond = PreCond.get();
  5082. Built.PreInits = buildPreInits(C, Captures);
  5083. Built.Cond = Cond.get();
  5084. Built.Init = Init.get();
  5085. Built.Inc = Inc.get();
  5086. Built.LB = LB.get();
  5087. Built.UB = UB.get();
  5088. Built.IL = IL.get();
  5089. Built.ST = ST.get();
  5090. Built.EUB = EUB.get();
  5091. Built.NLB = NextLB.get();
  5092. Built.NUB = NextUB.get();
  5093. Built.PrevLB = PrevLB.get();
  5094. Built.PrevUB = PrevUB.get();
  5095. Built.DistInc = DistInc.get();
  5096. Built.PrevEUB = PrevEUB.get();
  5097. Built.DistCombinedFields.LB = CombLB.get();
  5098. Built.DistCombinedFields.UB = CombUB.get();
  5099. Built.DistCombinedFields.EUB = CombEUB.get();
  5100. Built.DistCombinedFields.Init = CombInit.get();
  5101. Built.DistCombinedFields.Cond = CombCond.get();
  5102. Built.DistCombinedFields.NLB = CombNextLB.get();
  5103. Built.DistCombinedFields.NUB = CombNextUB.get();
  5104. return NestedLoopCount;
  5105. }
  5106. static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
  5107. auto CollapseClauses =
  5108. OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
  5109. if (CollapseClauses.begin() != CollapseClauses.end())
  5110. return (*CollapseClauses.begin())->getNumForLoops();
  5111. return nullptr;
  5112. }
  5113. static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
  5114. auto OrderedClauses =
  5115. OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
  5116. if (OrderedClauses.begin() != OrderedClauses.end())
  5117. return (*OrderedClauses.begin())->getNumForLoops();
  5118. return nullptr;
  5119. }
  5120. static bool checkSimdlenSafelenSpecified(Sema &S,
  5121. const ArrayRef<OMPClause *> Clauses) {
  5122. const OMPSafelenClause *Safelen = nullptr;
  5123. const OMPSimdlenClause *Simdlen = nullptr;
  5124. for (const OMPClause *Clause : Clauses) {
  5125. if (Clause->getClauseKind() == OMPC_safelen)
  5126. Safelen = cast<OMPSafelenClause>(Clause);
  5127. else if (Clause->getClauseKind() == OMPC_simdlen)
  5128. Simdlen = cast<OMPSimdlenClause>(Clause);
  5129. if (Safelen && Simdlen)
  5130. break;
  5131. }
  5132. if (Simdlen && Safelen) {
  5133. llvm::APSInt SimdlenRes, SafelenRes;
  5134. const Expr *SimdlenLength = Simdlen->getSimdlen();
  5135. const Expr *SafelenLength = Safelen->getSafelen();
  5136. if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
  5137. SimdlenLength->isInstantiationDependent() ||
  5138. SimdlenLength->containsUnexpandedParameterPack())
  5139. return false;
  5140. if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
  5141. SafelenLength->isInstantiationDependent() ||
  5142. SafelenLength->containsUnexpandedParameterPack())
  5143. return false;
  5144. SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
  5145. SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
  5146. // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
  5147. // If both simdlen and safelen clauses are specified, the value of the
  5148. // simdlen parameter must be less than or equal to the value of the safelen
  5149. // parameter.
  5150. if (SimdlenRes > SafelenRes) {
  5151. S.Diag(SimdlenLength->getExprLoc(),
  5152. diag::err_omp_wrong_simdlen_safelen_values)
  5153. << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
  5154. return true;
  5155. }
  5156. }
  5157. return false;
  5158. }
  5159. StmtResult
  5160. Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
  5161. SourceLocation StartLoc, SourceLocation EndLoc,
  5162. VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  5163. if (!AStmt)
  5164. return StmtError();
  5165. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  5166. OMPLoopDirective::HelperExprs B;
  5167. // In presence of clause 'collapse' or 'ordered' with number of loops, it will
  5168. // define the nested loops number.
  5169. unsigned NestedLoopCount = checkOpenMPLoop(
  5170. OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
  5171. AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
  5172. if (NestedLoopCount == 0)
  5173. return StmtError();
  5174. assert((CurContext->isDependentContext() || B.builtAll()) &&
  5175. "omp simd loop exprs were not built");
  5176. if (!CurContext->isDependentContext()) {
  5177. // Finalize the clauses that need pre-built expressions for CodeGen.
  5178. for (OMPClause *C : Clauses) {
  5179. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  5180. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  5181. B.NumIterations, *this, CurScope,
  5182. DSAStack))
  5183. return StmtError();
  5184. }
  5185. }
  5186. if (checkSimdlenSafelenSpecified(*this, Clauses))
  5187. return StmtError();
  5188. setFunctionHasBranchProtectedScope();
  5189. return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
  5190. Clauses, AStmt, B);
  5191. }
  5192. StmtResult
  5193. Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
  5194. SourceLocation StartLoc, SourceLocation EndLoc,
  5195. VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  5196. if (!AStmt)
  5197. return StmtError();
  5198. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  5199. OMPLoopDirective::HelperExprs B;
  5200. // In presence of clause 'collapse' or 'ordered' with number of loops, it will
  5201. // define the nested loops number.
  5202. unsigned NestedLoopCount = checkOpenMPLoop(
  5203. OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
  5204. AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
  5205. if (NestedLoopCount == 0)
  5206. return StmtError();
  5207. assert((CurContext->isDependentContext() || B.builtAll()) &&
  5208. "omp for loop exprs were not built");
  5209. if (!CurContext->isDependentContext()) {
  5210. // Finalize the clauses that need pre-built expressions for CodeGen.
  5211. for (OMPClause *C : Clauses) {
  5212. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  5213. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  5214. B.NumIterations, *this, CurScope,
  5215. DSAStack))
  5216. return StmtError();
  5217. }
  5218. }
  5219. setFunctionHasBranchProtectedScope();
  5220. return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
  5221. Clauses, AStmt, B, DSAStack->isCancelRegion());
  5222. }
  5223. StmtResult Sema::ActOnOpenMPForSimdDirective(
  5224. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  5225. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  5226. if (!AStmt)
  5227. return StmtError();
  5228. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  5229. OMPLoopDirective::HelperExprs B;
  5230. // In presence of clause 'collapse' or 'ordered' with number of loops, it will
  5231. // define the nested loops number.
  5232. unsigned NestedLoopCount =
  5233. checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
  5234. getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
  5235. VarsWithImplicitDSA, B);
  5236. if (NestedLoopCount == 0)
  5237. return StmtError();
  5238. assert((CurContext->isDependentContext() || B.builtAll()) &&
  5239. "omp for simd loop exprs were not built");
  5240. if (!CurContext->isDependentContext()) {
  5241. // Finalize the clauses that need pre-built expressions for CodeGen.
  5242. for (OMPClause *C : Clauses) {
  5243. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  5244. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  5245. B.NumIterations, *this, CurScope,
  5246. DSAStack))
  5247. return StmtError();
  5248. }
  5249. }
  5250. if (checkSimdlenSafelenSpecified(*this, Clauses))
  5251. return StmtError();
  5252. setFunctionHasBranchProtectedScope();
  5253. return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
  5254. Clauses, AStmt, B);
  5255. }
  5256. StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
  5257. Stmt *AStmt,
  5258. SourceLocation StartLoc,
  5259. SourceLocation EndLoc) {
  5260. if (!AStmt)
  5261. return StmtError();
  5262. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  5263. auto BaseStmt = AStmt;
  5264. while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
  5265. BaseStmt = CS->getCapturedStmt();
  5266. if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
  5267. auto S = C->children();
  5268. if (S.begin() == S.end())
  5269. return StmtError();
  5270. // All associated statements must be '#pragma omp section' except for
  5271. // the first one.
  5272. for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
  5273. if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
  5274. if (SectionStmt)
  5275. Diag(SectionStmt->getBeginLoc(),
  5276. diag::err_omp_sections_substmt_not_section);
  5277. return StmtError();
  5278. }
  5279. cast<OMPSectionDirective>(SectionStmt)
  5280. ->setHasCancel(DSAStack->isCancelRegion());
  5281. }
  5282. } else {
  5283. Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
  5284. return StmtError();
  5285. }
  5286. setFunctionHasBranchProtectedScope();
  5287. return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
  5288. DSAStack->isCancelRegion());
  5289. }
  5290. StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
  5291. SourceLocation StartLoc,
  5292. SourceLocation EndLoc) {
  5293. if (!AStmt)
  5294. return StmtError();
  5295. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  5296. setFunctionHasBranchProtectedScope();
  5297. DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
  5298. return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
  5299. DSAStack->isCancelRegion());
  5300. }
  5301. StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
  5302. Stmt *AStmt,
  5303. SourceLocation StartLoc,
  5304. SourceLocation EndLoc) {
  5305. if (!AStmt)
  5306. return StmtError();
  5307. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  5308. setFunctionHasBranchProtectedScope();
  5309. // OpenMP [2.7.3, single Construct, Restrictions]
  5310. // The copyprivate clause must not be used with the nowait clause.
  5311. const OMPClause *Nowait = nullptr;
  5312. const OMPClause *Copyprivate = nullptr;
  5313. for (const OMPClause *Clause : Clauses) {
  5314. if (Clause->getClauseKind() == OMPC_nowait)
  5315. Nowait = Clause;
  5316. else if (Clause->getClauseKind() == OMPC_copyprivate)
  5317. Copyprivate = Clause;
  5318. if (Copyprivate && Nowait) {
  5319. Diag(Copyprivate->getBeginLoc(),
  5320. diag::err_omp_single_copyprivate_with_nowait);
  5321. Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
  5322. return StmtError();
  5323. }
  5324. }
  5325. return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
  5326. }
  5327. StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
  5328. SourceLocation StartLoc,
  5329. SourceLocation EndLoc) {
  5330. if (!AStmt)
  5331. return StmtError();
  5332. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  5333. setFunctionHasBranchProtectedScope();
  5334. return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
  5335. }
  5336. StmtResult Sema::ActOnOpenMPCriticalDirective(
  5337. const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
  5338. Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
  5339. if (!AStmt)
  5340. return StmtError();
  5341. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  5342. bool ErrorFound = false;
  5343. llvm::APSInt Hint;
  5344. SourceLocation HintLoc;
  5345. bool DependentHint = false;
  5346. for (const OMPClause *C : Clauses) {
  5347. if (C->getClauseKind() == OMPC_hint) {
  5348. if (!DirName.getName()) {
  5349. Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
  5350. ErrorFound = true;
  5351. }
  5352. Expr *E = cast<OMPHintClause>(C)->getHint();
  5353. if (E->isTypeDependent() || E->isValueDependent() ||
  5354. E->isInstantiationDependent()) {
  5355. DependentHint = true;
  5356. } else {
  5357. Hint = E->EvaluateKnownConstInt(Context);
  5358. HintLoc = C->getBeginLoc();
  5359. }
  5360. }
  5361. }
  5362. if (ErrorFound)
  5363. return StmtError();
  5364. const auto Pair = DSAStack->getCriticalWithHint(DirName);
  5365. if (Pair.first && DirName.getName() && !DependentHint) {
  5366. if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
  5367. Diag(StartLoc, diag::err_omp_critical_with_hint);
  5368. if (HintLoc.isValid())
  5369. Diag(HintLoc, diag::note_omp_critical_hint_here)
  5370. << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
  5371. else
  5372. Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
  5373. if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
  5374. Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
  5375. << 1
  5376. << C->getHint()->EvaluateKnownConstInt(Context).toString(
  5377. /*Radix=*/10, /*Signed=*/false);
  5378. } else {
  5379. Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
  5380. }
  5381. }
  5382. }
  5383. setFunctionHasBranchProtectedScope();
  5384. auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
  5385. Clauses, AStmt);
  5386. if (!Pair.first && DirName.getName() && !DependentHint)
  5387. DSAStack->addCriticalWithHint(Dir, Hint);
  5388. return Dir;
  5389. }
  5390. StmtResult Sema::ActOnOpenMPParallelForDirective(
  5391. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  5392. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  5393. if (!AStmt)
  5394. return StmtError();
  5395. auto *CS = cast<CapturedStmt>(AStmt);
  5396. // 1.2.2 OpenMP Language Terminology
  5397. // Structured block - An executable statement with a single entry at the
  5398. // top and a single exit at the bottom.
  5399. // The point of exit cannot be a branch out of the structured block.
  5400. // longjmp() and throw() must not violate the entry/exit criteria.
  5401. CS->getCapturedDecl()->setNothrow();
  5402. OMPLoopDirective::HelperExprs B;
  5403. // In presence of clause 'collapse' or 'ordered' with number of loops, it will
  5404. // define the nested loops number.
  5405. unsigned NestedLoopCount =
  5406. checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
  5407. getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
  5408. VarsWithImplicitDSA, B);
  5409. if (NestedLoopCount == 0)
  5410. return StmtError();
  5411. assert((CurContext->isDependentContext() || B.builtAll()) &&
  5412. "omp parallel for loop exprs were not built");
  5413. if (!CurContext->isDependentContext()) {
  5414. // Finalize the clauses that need pre-built expressions for CodeGen.
  5415. for (OMPClause *C : Clauses) {
  5416. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  5417. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  5418. B.NumIterations, *this, CurScope,
  5419. DSAStack))
  5420. return StmtError();
  5421. }
  5422. }
  5423. setFunctionHasBranchProtectedScope();
  5424. return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
  5425. NestedLoopCount, Clauses, AStmt, B,
  5426. DSAStack->isCancelRegion());
  5427. }
  5428. StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
  5429. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  5430. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  5431. if (!AStmt)
  5432. return StmtError();
  5433. auto *CS = cast<CapturedStmt>(AStmt);
  5434. // 1.2.2 OpenMP Language Terminology
  5435. // Structured block - An executable statement with a single entry at the
  5436. // top and a single exit at the bottom.
  5437. // The point of exit cannot be a branch out of the structured block.
  5438. // longjmp() and throw() must not violate the entry/exit criteria.
  5439. CS->getCapturedDecl()->setNothrow();
  5440. OMPLoopDirective::HelperExprs B;
  5441. // In presence of clause 'collapse' or 'ordered' with number of loops, it will
  5442. // define the nested loops number.
  5443. unsigned NestedLoopCount =
  5444. checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
  5445. getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
  5446. VarsWithImplicitDSA, B);
  5447. if (NestedLoopCount == 0)
  5448. return StmtError();
  5449. if (!CurContext->isDependentContext()) {
  5450. // Finalize the clauses that need pre-built expressions for CodeGen.
  5451. for (OMPClause *C : Clauses) {
  5452. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  5453. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  5454. B.NumIterations, *this, CurScope,
  5455. DSAStack))
  5456. return StmtError();
  5457. }
  5458. }
  5459. if (checkSimdlenSafelenSpecified(*this, Clauses))
  5460. return StmtError();
  5461. setFunctionHasBranchProtectedScope();
  5462. return OMPParallelForSimdDirective::Create(
  5463. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
  5464. }
  5465. StmtResult
  5466. Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
  5467. Stmt *AStmt, SourceLocation StartLoc,
  5468. SourceLocation EndLoc) {
  5469. if (!AStmt)
  5470. return StmtError();
  5471. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  5472. auto BaseStmt = AStmt;
  5473. while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
  5474. BaseStmt = CS->getCapturedStmt();
  5475. if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
  5476. auto S = C->children();
  5477. if (S.begin() == S.end())
  5478. return StmtError();
  5479. // All associated statements must be '#pragma omp section' except for
  5480. // the first one.
  5481. for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
  5482. if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
  5483. if (SectionStmt)
  5484. Diag(SectionStmt->getBeginLoc(),
  5485. diag::err_omp_parallel_sections_substmt_not_section);
  5486. return StmtError();
  5487. }
  5488. cast<OMPSectionDirective>(SectionStmt)
  5489. ->setHasCancel(DSAStack->isCancelRegion());
  5490. }
  5491. } else {
  5492. Diag(AStmt->getBeginLoc(),
  5493. diag::err_omp_parallel_sections_not_compound_stmt);
  5494. return StmtError();
  5495. }
  5496. setFunctionHasBranchProtectedScope();
  5497. return OMPParallelSectionsDirective::Create(
  5498. Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
  5499. }
  5500. StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
  5501. Stmt *AStmt, SourceLocation StartLoc,
  5502. SourceLocation EndLoc) {
  5503. if (!AStmt)
  5504. return StmtError();
  5505. auto *CS = cast<CapturedStmt>(AStmt);
  5506. // 1.2.2 OpenMP Language Terminology
  5507. // Structured block - An executable statement with a single entry at the
  5508. // top and a single exit at the bottom.
  5509. // The point of exit cannot be a branch out of the structured block.
  5510. // longjmp() and throw() must not violate the entry/exit criteria.
  5511. CS->getCapturedDecl()->setNothrow();
  5512. setFunctionHasBranchProtectedScope();
  5513. return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
  5514. DSAStack->isCancelRegion());
  5515. }
  5516. StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
  5517. SourceLocation EndLoc) {
  5518. return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
  5519. }
  5520. StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
  5521. SourceLocation EndLoc) {
  5522. return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
  5523. }
  5524. StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
  5525. SourceLocation EndLoc) {
  5526. return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
  5527. }
  5528. StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
  5529. Stmt *AStmt,
  5530. SourceLocation StartLoc,
  5531. SourceLocation EndLoc) {
  5532. if (!AStmt)
  5533. return StmtError();
  5534. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  5535. setFunctionHasBranchProtectedScope();
  5536. return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
  5537. AStmt,
  5538. DSAStack->getTaskgroupReductionRef());
  5539. }
  5540. StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
  5541. SourceLocation StartLoc,
  5542. SourceLocation EndLoc) {
  5543. assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
  5544. return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
  5545. }
  5546. StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
  5547. Stmt *AStmt,
  5548. SourceLocation StartLoc,
  5549. SourceLocation EndLoc) {
  5550. const OMPClause *DependFound = nullptr;
  5551. const OMPClause *DependSourceClause = nullptr;
  5552. const OMPClause *DependSinkClause = nullptr;
  5553. bool ErrorFound = false;
  5554. const OMPThreadsClause *TC = nullptr;
  5555. const OMPSIMDClause *SC = nullptr;
  5556. for (const OMPClause *C : Clauses) {
  5557. if (auto *DC = dyn_cast<OMPDependClause>(C)) {
  5558. DependFound = C;
  5559. if (DC->getDependencyKind() == OMPC_DEPEND_source) {
  5560. if (DependSourceClause) {
  5561. Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
  5562. << getOpenMPDirectiveName(OMPD_ordered)
  5563. << getOpenMPClauseName(OMPC_depend) << 2;
  5564. ErrorFound = true;
  5565. } else {
  5566. DependSourceClause = C;
  5567. }
  5568. if (DependSinkClause) {
  5569. Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
  5570. << 0;
  5571. ErrorFound = true;
  5572. }
  5573. } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
  5574. if (DependSourceClause) {
  5575. Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
  5576. << 1;
  5577. ErrorFound = true;
  5578. }
  5579. DependSinkClause = C;
  5580. }
  5581. } else if (C->getClauseKind() == OMPC_threads) {
  5582. TC = cast<OMPThreadsClause>(C);
  5583. } else if (C->getClauseKind() == OMPC_simd) {
  5584. SC = cast<OMPSIMDClause>(C);
  5585. }
  5586. }
  5587. if (!ErrorFound && !SC &&
  5588. isOpenMPSimdDirective(DSAStack->getParentDirective())) {
  5589. // OpenMP [2.8.1,simd Construct, Restrictions]
  5590. // An ordered construct with the simd clause is the only OpenMP construct
  5591. // that can appear in the simd region.
  5592. Diag(StartLoc, diag::err_omp_prohibited_region_simd);
  5593. ErrorFound = true;
  5594. } else if (DependFound && (TC || SC)) {
  5595. Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
  5596. << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
  5597. ErrorFound = true;
  5598. } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
  5599. Diag(DependFound->getBeginLoc(),
  5600. diag::err_omp_ordered_directive_without_param);
  5601. ErrorFound = true;
  5602. } else if (TC || Clauses.empty()) {
  5603. if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
  5604. SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
  5605. Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
  5606. << (TC != nullptr);
  5607. Diag(Param->getBeginLoc(), diag::note_omp_ordered_param);
  5608. ErrorFound = true;
  5609. }
  5610. }
  5611. if ((!AStmt && !DependFound) || ErrorFound)
  5612. return StmtError();
  5613. if (AStmt) {
  5614. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  5615. setFunctionHasBranchProtectedScope();
  5616. }
  5617. return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
  5618. }
  5619. namespace {
  5620. /// Helper class for checking expression in 'omp atomic [update]'
  5621. /// construct.
  5622. class OpenMPAtomicUpdateChecker {
  5623. /// Error results for atomic update expressions.
  5624. enum ExprAnalysisErrorCode {
  5625. /// A statement is not an expression statement.
  5626. NotAnExpression,
  5627. /// Expression is not builtin binary or unary operation.
  5628. NotABinaryOrUnaryExpression,
  5629. /// Unary operation is not post-/pre- increment/decrement operation.
  5630. NotAnUnaryIncDecExpression,
  5631. /// An expression is not of scalar type.
  5632. NotAScalarType,
  5633. /// A binary operation is not an assignment operation.
  5634. NotAnAssignmentOp,
  5635. /// RHS part of the binary operation is not a binary expression.
  5636. NotABinaryExpression,
  5637. /// RHS part is not additive/multiplicative/shift/biwise binary
  5638. /// expression.
  5639. NotABinaryOperator,
  5640. /// RHS binary operation does not have reference to the updated LHS
  5641. /// part.
  5642. NotAnUpdateExpression,
  5643. /// No errors is found.
  5644. NoError
  5645. };
  5646. /// Reference to Sema.
  5647. Sema &SemaRef;
  5648. /// A location for note diagnostics (when error is found).
  5649. SourceLocation NoteLoc;
  5650. /// 'x' lvalue part of the source atomic expression.
  5651. Expr *X;
  5652. /// 'expr' rvalue part of the source atomic expression.
  5653. Expr *E;
  5654. /// Helper expression of the form
  5655. /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
  5656. /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
  5657. Expr *UpdateExpr;
  5658. /// Is 'x' a LHS in a RHS part of full update expression. It is
  5659. /// important for non-associative operations.
  5660. bool IsXLHSInRHSPart;
  5661. BinaryOperatorKind Op;
  5662. SourceLocation OpLoc;
  5663. /// true if the source expression is a postfix unary operation, false
  5664. /// if it is a prefix unary operation.
  5665. bool IsPostfixUpdate;
  5666. public:
  5667. OpenMPAtomicUpdateChecker(Sema &SemaRef)
  5668. : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
  5669. IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
  5670. /// Check specified statement that it is suitable for 'atomic update'
  5671. /// constructs and extract 'x', 'expr' and Operation from the original
  5672. /// expression. If DiagId and NoteId == 0, then only check is performed
  5673. /// without error notification.
  5674. /// \param DiagId Diagnostic which should be emitted if error is found.
  5675. /// \param NoteId Diagnostic note for the main error message.
  5676. /// \return true if statement is not an update expression, false otherwise.
  5677. bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
  5678. /// Return the 'x' lvalue part of the source atomic expression.
  5679. Expr *getX() const { return X; }
  5680. /// Return the 'expr' rvalue part of the source atomic expression.
  5681. Expr *getExpr() const { return E; }
  5682. /// Return the update expression used in calculation of the updated
  5683. /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
  5684. /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
  5685. Expr *getUpdateExpr() const { return UpdateExpr; }
  5686. /// Return true if 'x' is LHS in RHS part of full update expression,
  5687. /// false otherwise.
  5688. bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
  5689. /// true if the source expression is a postfix unary operation, false
  5690. /// if it is a prefix unary operation.
  5691. bool isPostfixUpdate() const { return IsPostfixUpdate; }
  5692. private:
  5693. bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
  5694. unsigned NoteId = 0);
  5695. };
  5696. } // namespace
  5697. bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
  5698. BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
  5699. ExprAnalysisErrorCode ErrorFound = NoError;
  5700. SourceLocation ErrorLoc, NoteLoc;
  5701. SourceRange ErrorRange, NoteRange;
  5702. // Allowed constructs are:
  5703. // x = x binop expr;
  5704. // x = expr binop x;
  5705. if (AtomicBinOp->getOpcode() == BO_Assign) {
  5706. X = AtomicBinOp->getLHS();
  5707. if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
  5708. AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
  5709. if (AtomicInnerBinOp->isMultiplicativeOp() ||
  5710. AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
  5711. AtomicInnerBinOp->isBitwiseOp()) {
  5712. Op = AtomicInnerBinOp->getOpcode();
  5713. OpLoc = AtomicInnerBinOp->getOperatorLoc();
  5714. Expr *LHS = AtomicInnerBinOp->getLHS();
  5715. Expr *RHS = AtomicInnerBinOp->getRHS();
  5716. llvm::FoldingSetNodeID XId, LHSId, RHSId;
  5717. X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
  5718. /*Canonical=*/true);
  5719. LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
  5720. /*Canonical=*/true);
  5721. RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
  5722. /*Canonical=*/true);
  5723. if (XId == LHSId) {
  5724. E = RHS;
  5725. IsXLHSInRHSPart = true;
  5726. } else if (XId == RHSId) {
  5727. E = LHS;
  5728. IsXLHSInRHSPart = false;
  5729. } else {
  5730. ErrorLoc = AtomicInnerBinOp->getExprLoc();
  5731. ErrorRange = AtomicInnerBinOp->getSourceRange();
  5732. NoteLoc = X->getExprLoc();
  5733. NoteRange = X->getSourceRange();
  5734. ErrorFound = NotAnUpdateExpression;
  5735. }
  5736. } else {
  5737. ErrorLoc = AtomicInnerBinOp->getExprLoc();
  5738. ErrorRange = AtomicInnerBinOp->getSourceRange();
  5739. NoteLoc = AtomicInnerBinOp->getOperatorLoc();
  5740. NoteRange = SourceRange(NoteLoc, NoteLoc);
  5741. ErrorFound = NotABinaryOperator;
  5742. }
  5743. } else {
  5744. NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
  5745. NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
  5746. ErrorFound = NotABinaryExpression;
  5747. }
  5748. } else {
  5749. ErrorLoc = AtomicBinOp->getExprLoc();
  5750. ErrorRange = AtomicBinOp->getSourceRange();
  5751. NoteLoc = AtomicBinOp->getOperatorLoc();
  5752. NoteRange = SourceRange(NoteLoc, NoteLoc);
  5753. ErrorFound = NotAnAssignmentOp;
  5754. }
  5755. if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
  5756. SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
  5757. SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
  5758. return true;
  5759. }
  5760. if (SemaRef.CurContext->isDependentContext())
  5761. E = X = UpdateExpr = nullptr;
  5762. return ErrorFound != NoError;
  5763. }
  5764. bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
  5765. unsigned NoteId) {
  5766. ExprAnalysisErrorCode ErrorFound = NoError;
  5767. SourceLocation ErrorLoc, NoteLoc;
  5768. SourceRange ErrorRange, NoteRange;
  5769. // Allowed constructs are:
  5770. // x++;
  5771. // x--;
  5772. // ++x;
  5773. // --x;
  5774. // x binop= expr;
  5775. // x = x binop expr;
  5776. // x = expr binop x;
  5777. if (auto *AtomicBody = dyn_cast<Expr>(S)) {
  5778. AtomicBody = AtomicBody->IgnoreParenImpCasts();
  5779. if (AtomicBody->getType()->isScalarType() ||
  5780. AtomicBody->isInstantiationDependent()) {
  5781. if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
  5782. AtomicBody->IgnoreParenImpCasts())) {
  5783. // Check for Compound Assignment Operation
  5784. Op = BinaryOperator::getOpForCompoundAssignment(
  5785. AtomicCompAssignOp->getOpcode());
  5786. OpLoc = AtomicCompAssignOp->getOperatorLoc();
  5787. E = AtomicCompAssignOp->getRHS();
  5788. X = AtomicCompAssignOp->getLHS()->IgnoreParens();
  5789. IsXLHSInRHSPart = true;
  5790. } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
  5791. AtomicBody->IgnoreParenImpCasts())) {
  5792. // Check for Binary Operation
  5793. if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
  5794. return true;
  5795. } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
  5796. AtomicBody->IgnoreParenImpCasts())) {
  5797. // Check for Unary Operation
  5798. if (AtomicUnaryOp->isIncrementDecrementOp()) {
  5799. IsPostfixUpdate = AtomicUnaryOp->isPostfix();
  5800. Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
  5801. OpLoc = AtomicUnaryOp->getOperatorLoc();
  5802. X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
  5803. E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
  5804. IsXLHSInRHSPart = true;
  5805. } else {
  5806. ErrorFound = NotAnUnaryIncDecExpression;
  5807. ErrorLoc = AtomicUnaryOp->getExprLoc();
  5808. ErrorRange = AtomicUnaryOp->getSourceRange();
  5809. NoteLoc = AtomicUnaryOp->getOperatorLoc();
  5810. NoteRange = SourceRange(NoteLoc, NoteLoc);
  5811. }
  5812. } else if (!AtomicBody->isInstantiationDependent()) {
  5813. ErrorFound = NotABinaryOrUnaryExpression;
  5814. NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
  5815. NoteRange = ErrorRange = AtomicBody->getSourceRange();
  5816. }
  5817. } else {
  5818. ErrorFound = NotAScalarType;
  5819. NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
  5820. NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
  5821. }
  5822. } else {
  5823. ErrorFound = NotAnExpression;
  5824. NoteLoc = ErrorLoc = S->getBeginLoc();
  5825. NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
  5826. }
  5827. if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
  5828. SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
  5829. SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
  5830. return true;
  5831. }
  5832. if (SemaRef.CurContext->isDependentContext())
  5833. E = X = UpdateExpr = nullptr;
  5834. if (ErrorFound == NoError && E && X) {
  5835. // Build an update expression of form 'OpaqueValueExpr(x) binop
  5836. // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
  5837. // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
  5838. auto *OVEX = new (SemaRef.getASTContext())
  5839. OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
  5840. auto *OVEExpr = new (SemaRef.getASTContext())
  5841. OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
  5842. ExprResult Update =
  5843. SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
  5844. IsXLHSInRHSPart ? OVEExpr : OVEX);
  5845. if (Update.isInvalid())
  5846. return true;
  5847. Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
  5848. Sema::AA_Casting);
  5849. if (Update.isInvalid())
  5850. return true;
  5851. UpdateExpr = Update.get();
  5852. }
  5853. return ErrorFound != NoError;
  5854. }
  5855. StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
  5856. Stmt *AStmt,
  5857. SourceLocation StartLoc,
  5858. SourceLocation EndLoc) {
  5859. if (!AStmt)
  5860. return StmtError();
  5861. auto *CS = cast<CapturedStmt>(AStmt);
  5862. // 1.2.2 OpenMP Language Terminology
  5863. // Structured block - An executable statement with a single entry at the
  5864. // top and a single exit at the bottom.
  5865. // The point of exit cannot be a branch out of the structured block.
  5866. // longjmp() and throw() must not violate the entry/exit criteria.
  5867. OpenMPClauseKind AtomicKind = OMPC_unknown;
  5868. SourceLocation AtomicKindLoc;
  5869. for (const OMPClause *C : Clauses) {
  5870. if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
  5871. C->getClauseKind() == OMPC_update ||
  5872. C->getClauseKind() == OMPC_capture) {
  5873. if (AtomicKind != OMPC_unknown) {
  5874. Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
  5875. << SourceRange(C->getBeginLoc(), C->getEndLoc());
  5876. Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
  5877. << getOpenMPClauseName(AtomicKind);
  5878. } else {
  5879. AtomicKind = C->getClauseKind();
  5880. AtomicKindLoc = C->getBeginLoc();
  5881. }
  5882. }
  5883. }
  5884. Stmt *Body = CS->getCapturedStmt();
  5885. if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
  5886. Body = EWC->getSubExpr();
  5887. Expr *X = nullptr;
  5888. Expr *V = nullptr;
  5889. Expr *E = nullptr;
  5890. Expr *UE = nullptr;
  5891. bool IsXLHSInRHSPart = false;
  5892. bool IsPostfixUpdate = false;
  5893. // OpenMP [2.12.6, atomic Construct]
  5894. // In the next expressions:
  5895. // * x and v (as applicable) are both l-value expressions with scalar type.
  5896. // * During the execution of an atomic region, multiple syntactic
  5897. // occurrences of x must designate the same storage location.
  5898. // * Neither of v and expr (as applicable) may access the storage location
  5899. // designated by x.
  5900. // * Neither of x and expr (as applicable) may access the storage location
  5901. // designated by v.
  5902. // * expr is an expression with scalar type.
  5903. // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
  5904. // * binop, binop=, ++, and -- are not overloaded operators.
  5905. // * The expression x binop expr must be numerically equivalent to x binop
  5906. // (expr). This requirement is satisfied if the operators in expr have
  5907. // precedence greater than binop, or by using parentheses around expr or
  5908. // subexpressions of expr.
  5909. // * The expression expr binop x must be numerically equivalent to (expr)
  5910. // binop x. This requirement is satisfied if the operators in expr have
  5911. // precedence equal to or greater than binop, or by using parentheses around
  5912. // expr or subexpressions of expr.
  5913. // * For forms that allow multiple occurrences of x, the number of times
  5914. // that x is evaluated is unspecified.
  5915. if (AtomicKind == OMPC_read) {
  5916. enum {
  5917. NotAnExpression,
  5918. NotAnAssignmentOp,
  5919. NotAScalarType,
  5920. NotAnLValue,
  5921. NoError
  5922. } ErrorFound = NoError;
  5923. SourceLocation ErrorLoc, NoteLoc;
  5924. SourceRange ErrorRange, NoteRange;
  5925. // If clause is read:
  5926. // v = x;
  5927. if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
  5928. const auto *AtomicBinOp =
  5929. dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
  5930. if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
  5931. X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
  5932. V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
  5933. if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
  5934. (V->isInstantiationDependent() || V->getType()->isScalarType())) {
  5935. if (!X->isLValue() || !V->isLValue()) {
  5936. const Expr *NotLValueExpr = X->isLValue() ? V : X;
  5937. ErrorFound = NotAnLValue;
  5938. ErrorLoc = AtomicBinOp->getExprLoc();
  5939. ErrorRange = AtomicBinOp->getSourceRange();
  5940. NoteLoc = NotLValueExpr->getExprLoc();
  5941. NoteRange = NotLValueExpr->getSourceRange();
  5942. }
  5943. } else if (!X->isInstantiationDependent() ||
  5944. !V->isInstantiationDependent()) {
  5945. const Expr *NotScalarExpr =
  5946. (X->isInstantiationDependent() || X->getType()->isScalarType())
  5947. ? V
  5948. : X;
  5949. ErrorFound = NotAScalarType;
  5950. ErrorLoc = AtomicBinOp->getExprLoc();
  5951. ErrorRange = AtomicBinOp->getSourceRange();
  5952. NoteLoc = NotScalarExpr->getExprLoc();
  5953. NoteRange = NotScalarExpr->getSourceRange();
  5954. }
  5955. } else if (!AtomicBody->isInstantiationDependent()) {
  5956. ErrorFound = NotAnAssignmentOp;
  5957. ErrorLoc = AtomicBody->getExprLoc();
  5958. ErrorRange = AtomicBody->getSourceRange();
  5959. NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
  5960. : AtomicBody->getExprLoc();
  5961. NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
  5962. : AtomicBody->getSourceRange();
  5963. }
  5964. } else {
  5965. ErrorFound = NotAnExpression;
  5966. NoteLoc = ErrorLoc = Body->getBeginLoc();
  5967. NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
  5968. }
  5969. if (ErrorFound != NoError) {
  5970. Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
  5971. << ErrorRange;
  5972. Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
  5973. << NoteRange;
  5974. return StmtError();
  5975. }
  5976. if (CurContext->isDependentContext())
  5977. V = X = nullptr;
  5978. } else if (AtomicKind == OMPC_write) {
  5979. enum {
  5980. NotAnExpression,
  5981. NotAnAssignmentOp,
  5982. NotAScalarType,
  5983. NotAnLValue,
  5984. NoError
  5985. } ErrorFound = NoError;
  5986. SourceLocation ErrorLoc, NoteLoc;
  5987. SourceRange ErrorRange, NoteRange;
  5988. // If clause is write:
  5989. // x = expr;
  5990. if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
  5991. const auto *AtomicBinOp =
  5992. dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
  5993. if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
  5994. X = AtomicBinOp->getLHS();
  5995. E = AtomicBinOp->getRHS();
  5996. if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
  5997. (E->isInstantiationDependent() || E->getType()->isScalarType())) {
  5998. if (!X->isLValue()) {
  5999. ErrorFound = NotAnLValue;
  6000. ErrorLoc = AtomicBinOp->getExprLoc();
  6001. ErrorRange = AtomicBinOp->getSourceRange();
  6002. NoteLoc = X->getExprLoc();
  6003. NoteRange = X->getSourceRange();
  6004. }
  6005. } else if (!X->isInstantiationDependent() ||
  6006. !E->isInstantiationDependent()) {
  6007. const Expr *NotScalarExpr =
  6008. (X->isInstantiationDependent() || X->getType()->isScalarType())
  6009. ? E
  6010. : X;
  6011. ErrorFound = NotAScalarType;
  6012. ErrorLoc = AtomicBinOp->getExprLoc();
  6013. ErrorRange = AtomicBinOp->getSourceRange();
  6014. NoteLoc = NotScalarExpr->getExprLoc();
  6015. NoteRange = NotScalarExpr->getSourceRange();
  6016. }
  6017. } else if (!AtomicBody->isInstantiationDependent()) {
  6018. ErrorFound = NotAnAssignmentOp;
  6019. ErrorLoc = AtomicBody->getExprLoc();
  6020. ErrorRange = AtomicBody->getSourceRange();
  6021. NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
  6022. : AtomicBody->getExprLoc();
  6023. NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
  6024. : AtomicBody->getSourceRange();
  6025. }
  6026. } else {
  6027. ErrorFound = NotAnExpression;
  6028. NoteLoc = ErrorLoc = Body->getBeginLoc();
  6029. NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
  6030. }
  6031. if (ErrorFound != NoError) {
  6032. Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
  6033. << ErrorRange;
  6034. Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
  6035. << NoteRange;
  6036. return StmtError();
  6037. }
  6038. if (CurContext->isDependentContext())
  6039. E = X = nullptr;
  6040. } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
  6041. // If clause is update:
  6042. // x++;
  6043. // x--;
  6044. // ++x;
  6045. // --x;
  6046. // x binop= expr;
  6047. // x = x binop expr;
  6048. // x = expr binop x;
  6049. OpenMPAtomicUpdateChecker Checker(*this);
  6050. if (Checker.checkStatement(
  6051. Body, (AtomicKind == OMPC_update)
  6052. ? diag::err_omp_atomic_update_not_expression_statement
  6053. : diag::err_omp_atomic_not_expression_statement,
  6054. diag::note_omp_atomic_update))
  6055. return StmtError();
  6056. if (!CurContext->isDependentContext()) {
  6057. E = Checker.getExpr();
  6058. X = Checker.getX();
  6059. UE = Checker.getUpdateExpr();
  6060. IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
  6061. }
  6062. } else if (AtomicKind == OMPC_capture) {
  6063. enum {
  6064. NotAnAssignmentOp,
  6065. NotACompoundStatement,
  6066. NotTwoSubstatements,
  6067. NotASpecificExpression,
  6068. NoError
  6069. } ErrorFound = NoError;
  6070. SourceLocation ErrorLoc, NoteLoc;
  6071. SourceRange ErrorRange, NoteRange;
  6072. if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
  6073. // If clause is a capture:
  6074. // v = x++;
  6075. // v = x--;
  6076. // v = ++x;
  6077. // v = --x;
  6078. // v = x binop= expr;
  6079. // v = x = x binop expr;
  6080. // v = x = expr binop x;
  6081. const auto *AtomicBinOp =
  6082. dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
  6083. if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
  6084. V = AtomicBinOp->getLHS();
  6085. Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
  6086. OpenMPAtomicUpdateChecker Checker(*this);
  6087. if (Checker.checkStatement(
  6088. Body, diag::err_omp_atomic_capture_not_expression_statement,
  6089. diag::note_omp_atomic_update))
  6090. return StmtError();
  6091. E = Checker.getExpr();
  6092. X = Checker.getX();
  6093. UE = Checker.getUpdateExpr();
  6094. IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
  6095. IsPostfixUpdate = Checker.isPostfixUpdate();
  6096. } else if (!AtomicBody->isInstantiationDependent()) {
  6097. ErrorLoc = AtomicBody->getExprLoc();
  6098. ErrorRange = AtomicBody->getSourceRange();
  6099. NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
  6100. : AtomicBody->getExprLoc();
  6101. NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
  6102. : AtomicBody->getSourceRange();
  6103. ErrorFound = NotAnAssignmentOp;
  6104. }
  6105. if (ErrorFound != NoError) {
  6106. Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
  6107. << ErrorRange;
  6108. Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
  6109. return StmtError();
  6110. }
  6111. if (CurContext->isDependentContext())
  6112. UE = V = E = X = nullptr;
  6113. } else {
  6114. // If clause is a capture:
  6115. // { v = x; x = expr; }
  6116. // { v = x; x++; }
  6117. // { v = x; x--; }
  6118. // { v = x; ++x; }
  6119. // { v = x; --x; }
  6120. // { v = x; x binop= expr; }
  6121. // { v = x; x = x binop expr; }
  6122. // { v = x; x = expr binop x; }
  6123. // { x++; v = x; }
  6124. // { x--; v = x; }
  6125. // { ++x; v = x; }
  6126. // { --x; v = x; }
  6127. // { x binop= expr; v = x; }
  6128. // { x = x binop expr; v = x; }
  6129. // { x = expr binop x; v = x; }
  6130. if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
  6131. // Check that this is { expr1; expr2; }
  6132. if (CS->size() == 2) {
  6133. Stmt *First = CS->body_front();
  6134. Stmt *Second = CS->body_back();
  6135. if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
  6136. First = EWC->getSubExpr()->IgnoreParenImpCasts();
  6137. if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
  6138. Second = EWC->getSubExpr()->IgnoreParenImpCasts();
  6139. // Need to find what subexpression is 'v' and what is 'x'.
  6140. OpenMPAtomicUpdateChecker Checker(*this);
  6141. bool IsUpdateExprFound = !Checker.checkStatement(Second);
  6142. BinaryOperator *BinOp = nullptr;
  6143. if (IsUpdateExprFound) {
  6144. BinOp = dyn_cast<BinaryOperator>(First);
  6145. IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
  6146. }
  6147. if (IsUpdateExprFound && !CurContext->isDependentContext()) {
  6148. // { v = x; x++; }
  6149. // { v = x; x--; }
  6150. // { v = x; ++x; }
  6151. // { v = x; --x; }
  6152. // { v = x; x binop= expr; }
  6153. // { v = x; x = x binop expr; }
  6154. // { v = x; x = expr binop x; }
  6155. // Check that the first expression has form v = x.
  6156. Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
  6157. llvm::FoldingSetNodeID XId, PossibleXId;
  6158. Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
  6159. PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
  6160. IsUpdateExprFound = XId == PossibleXId;
  6161. if (IsUpdateExprFound) {
  6162. V = BinOp->getLHS();
  6163. X = Checker.getX();
  6164. E = Checker.getExpr();
  6165. UE = Checker.getUpdateExpr();
  6166. IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
  6167. IsPostfixUpdate = true;
  6168. }
  6169. }
  6170. if (!IsUpdateExprFound) {
  6171. IsUpdateExprFound = !Checker.checkStatement(First);
  6172. BinOp = nullptr;
  6173. if (IsUpdateExprFound) {
  6174. BinOp = dyn_cast<BinaryOperator>(Second);
  6175. IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
  6176. }
  6177. if (IsUpdateExprFound && !CurContext->isDependentContext()) {
  6178. // { x++; v = x; }
  6179. // { x--; v = x; }
  6180. // { ++x; v = x; }
  6181. // { --x; v = x; }
  6182. // { x binop= expr; v = x; }
  6183. // { x = x binop expr; v = x; }
  6184. // { x = expr binop x; v = x; }
  6185. // Check that the second expression has form v = x.
  6186. Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
  6187. llvm::FoldingSetNodeID XId, PossibleXId;
  6188. Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
  6189. PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
  6190. IsUpdateExprFound = XId == PossibleXId;
  6191. if (IsUpdateExprFound) {
  6192. V = BinOp->getLHS();
  6193. X = Checker.getX();
  6194. E = Checker.getExpr();
  6195. UE = Checker.getUpdateExpr();
  6196. IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
  6197. IsPostfixUpdate = false;
  6198. }
  6199. }
  6200. }
  6201. if (!IsUpdateExprFound) {
  6202. // { v = x; x = expr; }
  6203. auto *FirstExpr = dyn_cast<Expr>(First);
  6204. auto *SecondExpr = dyn_cast<Expr>(Second);
  6205. if (!FirstExpr || !SecondExpr ||
  6206. !(FirstExpr->isInstantiationDependent() ||
  6207. SecondExpr->isInstantiationDependent())) {
  6208. auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
  6209. if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
  6210. ErrorFound = NotAnAssignmentOp;
  6211. NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
  6212. : First->getBeginLoc();
  6213. NoteRange = ErrorRange = FirstBinOp
  6214. ? FirstBinOp->getSourceRange()
  6215. : SourceRange(ErrorLoc, ErrorLoc);
  6216. } else {
  6217. auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
  6218. if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
  6219. ErrorFound = NotAnAssignmentOp;
  6220. NoteLoc = ErrorLoc = SecondBinOp
  6221. ? SecondBinOp->getOperatorLoc()
  6222. : Second->getBeginLoc();
  6223. NoteRange = ErrorRange =
  6224. SecondBinOp ? SecondBinOp->getSourceRange()
  6225. : SourceRange(ErrorLoc, ErrorLoc);
  6226. } else {
  6227. Expr *PossibleXRHSInFirst =
  6228. FirstBinOp->getRHS()->IgnoreParenImpCasts();
  6229. Expr *PossibleXLHSInSecond =
  6230. SecondBinOp->getLHS()->IgnoreParenImpCasts();
  6231. llvm::FoldingSetNodeID X1Id, X2Id;
  6232. PossibleXRHSInFirst->Profile(X1Id, Context,
  6233. /*Canonical=*/true);
  6234. PossibleXLHSInSecond->Profile(X2Id, Context,
  6235. /*Canonical=*/true);
  6236. IsUpdateExprFound = X1Id == X2Id;
  6237. if (IsUpdateExprFound) {
  6238. V = FirstBinOp->getLHS();
  6239. X = SecondBinOp->getLHS();
  6240. E = SecondBinOp->getRHS();
  6241. UE = nullptr;
  6242. IsXLHSInRHSPart = false;
  6243. IsPostfixUpdate = true;
  6244. } else {
  6245. ErrorFound = NotASpecificExpression;
  6246. ErrorLoc = FirstBinOp->getExprLoc();
  6247. ErrorRange = FirstBinOp->getSourceRange();
  6248. NoteLoc = SecondBinOp->getLHS()->getExprLoc();
  6249. NoteRange = SecondBinOp->getRHS()->getSourceRange();
  6250. }
  6251. }
  6252. }
  6253. }
  6254. }
  6255. } else {
  6256. NoteLoc = ErrorLoc = Body->getBeginLoc();
  6257. NoteRange = ErrorRange =
  6258. SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
  6259. ErrorFound = NotTwoSubstatements;
  6260. }
  6261. } else {
  6262. NoteLoc = ErrorLoc = Body->getBeginLoc();
  6263. NoteRange = ErrorRange =
  6264. SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
  6265. ErrorFound = NotACompoundStatement;
  6266. }
  6267. if (ErrorFound != NoError) {
  6268. Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
  6269. << ErrorRange;
  6270. Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
  6271. return StmtError();
  6272. }
  6273. if (CurContext->isDependentContext())
  6274. UE = V = E = X = nullptr;
  6275. }
  6276. }
  6277. setFunctionHasBranchProtectedScope();
  6278. return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
  6279. X, V, E, UE, IsXLHSInRHSPart,
  6280. IsPostfixUpdate);
  6281. }
  6282. StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
  6283. Stmt *AStmt,
  6284. SourceLocation StartLoc,
  6285. SourceLocation EndLoc) {
  6286. if (!AStmt)
  6287. return StmtError();
  6288. auto *CS = cast<CapturedStmt>(AStmt);
  6289. // 1.2.2 OpenMP Language Terminology
  6290. // Structured block - An executable statement with a single entry at the
  6291. // top and a single exit at the bottom.
  6292. // The point of exit cannot be a branch out of the structured block.
  6293. // longjmp() and throw() must not violate the entry/exit criteria.
  6294. CS->getCapturedDecl()->setNothrow();
  6295. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
  6296. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  6297. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  6298. // 1.2.2 OpenMP Language Terminology
  6299. // Structured block - An executable statement with a single entry at the
  6300. // top and a single exit at the bottom.
  6301. // The point of exit cannot be a branch out of the structured block.
  6302. // longjmp() and throw() must not violate the entry/exit criteria.
  6303. CS->getCapturedDecl()->setNothrow();
  6304. }
  6305. // OpenMP [2.16, Nesting of Regions]
  6306. // If specified, a teams construct must be contained within a target
  6307. // construct. That target construct must contain no statements or directives
  6308. // outside of the teams construct.
  6309. if (DSAStack->hasInnerTeamsRegion()) {
  6310. const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
  6311. bool OMPTeamsFound = true;
  6312. if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
  6313. auto I = CS->body_begin();
  6314. while (I != CS->body_end()) {
  6315. const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
  6316. if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
  6317. OMPTeamsFound = false;
  6318. break;
  6319. }
  6320. ++I;
  6321. }
  6322. assert(I != CS->body_end() && "Not found statement");
  6323. S = *I;
  6324. } else {
  6325. const auto *OED = dyn_cast<OMPExecutableDirective>(S);
  6326. OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
  6327. }
  6328. if (!OMPTeamsFound) {
  6329. Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
  6330. Diag(DSAStack->getInnerTeamsRegionLoc(),
  6331. diag::note_omp_nested_teams_construct_here);
  6332. Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
  6333. << isa<OMPExecutableDirective>(S);
  6334. return StmtError();
  6335. }
  6336. }
  6337. setFunctionHasBranchProtectedScope();
  6338. return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
  6339. }
  6340. StmtResult
  6341. Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
  6342. Stmt *AStmt, SourceLocation StartLoc,
  6343. SourceLocation EndLoc) {
  6344. if (!AStmt)
  6345. return StmtError();
  6346. auto *CS = cast<CapturedStmt>(AStmt);
  6347. // 1.2.2 OpenMP Language Terminology
  6348. // Structured block - An executable statement with a single entry at the
  6349. // top and a single exit at the bottom.
  6350. // The point of exit cannot be a branch out of the structured block.
  6351. // longjmp() and throw() must not violate the entry/exit criteria.
  6352. CS->getCapturedDecl()->setNothrow();
  6353. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
  6354. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  6355. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  6356. // 1.2.2 OpenMP Language Terminology
  6357. // Structured block - An executable statement with a single entry at the
  6358. // top and a single exit at the bottom.
  6359. // The point of exit cannot be a branch out of the structured block.
  6360. // longjmp() and throw() must not violate the entry/exit criteria.
  6361. CS->getCapturedDecl()->setNothrow();
  6362. }
  6363. setFunctionHasBranchProtectedScope();
  6364. return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
  6365. AStmt);
  6366. }
  6367. StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
  6368. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  6369. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  6370. if (!AStmt)
  6371. return StmtError();
  6372. auto *CS = cast<CapturedStmt>(AStmt);
  6373. // 1.2.2 OpenMP Language Terminology
  6374. // Structured block - An executable statement with a single entry at the
  6375. // top and a single exit at the bottom.
  6376. // The point of exit cannot be a branch out of the structured block.
  6377. // longjmp() and throw() must not violate the entry/exit criteria.
  6378. CS->getCapturedDecl()->setNothrow();
  6379. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
  6380. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  6381. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  6382. // 1.2.2 OpenMP Language Terminology
  6383. // Structured block - An executable statement with a single entry at the
  6384. // top and a single exit at the bottom.
  6385. // The point of exit cannot be a branch out of the structured block.
  6386. // longjmp() and throw() must not violate the entry/exit criteria.
  6387. CS->getCapturedDecl()->setNothrow();
  6388. }
  6389. OMPLoopDirective::HelperExprs B;
  6390. // In presence of clause 'collapse' or 'ordered' with number of loops, it will
  6391. // define the nested loops number.
  6392. unsigned NestedLoopCount =
  6393. checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
  6394. getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
  6395. VarsWithImplicitDSA, B);
  6396. if (NestedLoopCount == 0)
  6397. return StmtError();
  6398. assert((CurContext->isDependentContext() || B.builtAll()) &&
  6399. "omp target parallel for loop exprs were not built");
  6400. if (!CurContext->isDependentContext()) {
  6401. // Finalize the clauses that need pre-built expressions for CodeGen.
  6402. for (OMPClause *C : Clauses) {
  6403. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  6404. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  6405. B.NumIterations, *this, CurScope,
  6406. DSAStack))
  6407. return StmtError();
  6408. }
  6409. }
  6410. setFunctionHasBranchProtectedScope();
  6411. return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
  6412. NestedLoopCount, Clauses, AStmt,
  6413. B, DSAStack->isCancelRegion());
  6414. }
  6415. /// Check for existence of a map clause in the list of clauses.
  6416. static bool hasClauses(ArrayRef<OMPClause *> Clauses,
  6417. const OpenMPClauseKind K) {
  6418. return llvm::any_of(
  6419. Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
  6420. }
  6421. template <typename... Params>
  6422. static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
  6423. const Params... ClauseTypes) {
  6424. return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
  6425. }
  6426. StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
  6427. Stmt *AStmt,
  6428. SourceLocation StartLoc,
  6429. SourceLocation EndLoc) {
  6430. if (!AStmt)
  6431. return StmtError();
  6432. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  6433. // OpenMP [2.10.1, Restrictions, p. 97]
  6434. // At least one map clause must appear on the directive.
  6435. if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
  6436. Diag(StartLoc, diag::err_omp_no_clause_for_directive)
  6437. << "'map' or 'use_device_ptr'"
  6438. << getOpenMPDirectiveName(OMPD_target_data);
  6439. return StmtError();
  6440. }
  6441. setFunctionHasBranchProtectedScope();
  6442. return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
  6443. AStmt);
  6444. }
  6445. StmtResult
  6446. Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
  6447. SourceLocation StartLoc,
  6448. SourceLocation EndLoc, Stmt *AStmt) {
  6449. if (!AStmt)
  6450. return StmtError();
  6451. auto *CS = cast<CapturedStmt>(AStmt);
  6452. // 1.2.2 OpenMP Language Terminology
  6453. // Structured block - An executable statement with a single entry at the
  6454. // top and a single exit at the bottom.
  6455. // The point of exit cannot be a branch out of the structured block.
  6456. // longjmp() and throw() must not violate the entry/exit criteria.
  6457. CS->getCapturedDecl()->setNothrow();
  6458. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
  6459. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  6460. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  6461. // 1.2.2 OpenMP Language Terminology
  6462. // Structured block - An executable statement with a single entry at the
  6463. // top and a single exit at the bottom.
  6464. // The point of exit cannot be a branch out of the structured block.
  6465. // longjmp() and throw() must not violate the entry/exit criteria.
  6466. CS->getCapturedDecl()->setNothrow();
  6467. }
  6468. // OpenMP [2.10.2, Restrictions, p. 99]
  6469. // At least one map clause must appear on the directive.
  6470. if (!hasClauses(Clauses, OMPC_map)) {
  6471. Diag(StartLoc, diag::err_omp_no_clause_for_directive)
  6472. << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
  6473. return StmtError();
  6474. }
  6475. return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
  6476. AStmt);
  6477. }
  6478. StmtResult
  6479. Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
  6480. SourceLocation StartLoc,
  6481. SourceLocation EndLoc, Stmt *AStmt) {
  6482. if (!AStmt)
  6483. return StmtError();
  6484. auto *CS = cast<CapturedStmt>(AStmt);
  6485. // 1.2.2 OpenMP Language Terminology
  6486. // Structured block - An executable statement with a single entry at the
  6487. // top and a single exit at the bottom.
  6488. // The point of exit cannot be a branch out of the structured block.
  6489. // longjmp() and throw() must not violate the entry/exit criteria.
  6490. CS->getCapturedDecl()->setNothrow();
  6491. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
  6492. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  6493. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  6494. // 1.2.2 OpenMP Language Terminology
  6495. // Structured block - An executable statement with a single entry at the
  6496. // top and a single exit at the bottom.
  6497. // The point of exit cannot be a branch out of the structured block.
  6498. // longjmp() and throw() must not violate the entry/exit criteria.
  6499. CS->getCapturedDecl()->setNothrow();
  6500. }
  6501. // OpenMP [2.10.3, Restrictions, p. 102]
  6502. // At least one map clause must appear on the directive.
  6503. if (!hasClauses(Clauses, OMPC_map)) {
  6504. Diag(StartLoc, diag::err_omp_no_clause_for_directive)
  6505. << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
  6506. return StmtError();
  6507. }
  6508. return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
  6509. AStmt);
  6510. }
  6511. StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
  6512. SourceLocation StartLoc,
  6513. SourceLocation EndLoc,
  6514. Stmt *AStmt) {
  6515. if (!AStmt)
  6516. return StmtError();
  6517. auto *CS = cast<CapturedStmt>(AStmt);
  6518. // 1.2.2 OpenMP Language Terminology
  6519. // Structured block - An executable statement with a single entry at the
  6520. // top and a single exit at the bottom.
  6521. // The point of exit cannot be a branch out of the structured block.
  6522. // longjmp() and throw() must not violate the entry/exit criteria.
  6523. CS->getCapturedDecl()->setNothrow();
  6524. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
  6525. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  6526. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  6527. // 1.2.2 OpenMP Language Terminology
  6528. // Structured block - An executable statement with a single entry at the
  6529. // top and a single exit at the bottom.
  6530. // The point of exit cannot be a branch out of the structured block.
  6531. // longjmp() and throw() must not violate the entry/exit criteria.
  6532. CS->getCapturedDecl()->setNothrow();
  6533. }
  6534. if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
  6535. Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
  6536. return StmtError();
  6537. }
  6538. return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
  6539. AStmt);
  6540. }
  6541. StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
  6542. Stmt *AStmt, SourceLocation StartLoc,
  6543. SourceLocation EndLoc) {
  6544. if (!AStmt)
  6545. return StmtError();
  6546. auto *CS = cast<CapturedStmt>(AStmt);
  6547. // 1.2.2 OpenMP Language Terminology
  6548. // Structured block - An executable statement with a single entry at the
  6549. // top and a single exit at the bottom.
  6550. // The point of exit cannot be a branch out of the structured block.
  6551. // longjmp() and throw() must not violate the entry/exit criteria.
  6552. CS->getCapturedDecl()->setNothrow();
  6553. setFunctionHasBranchProtectedScope();
  6554. DSAStack->setParentTeamsRegionLoc(StartLoc);
  6555. return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
  6556. }
  6557. StmtResult
  6558. Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
  6559. SourceLocation EndLoc,
  6560. OpenMPDirectiveKind CancelRegion) {
  6561. if (DSAStack->isParentNowaitRegion()) {
  6562. Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
  6563. return StmtError();
  6564. }
  6565. if (DSAStack->isParentOrderedRegion()) {
  6566. Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
  6567. return StmtError();
  6568. }
  6569. return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
  6570. CancelRegion);
  6571. }
  6572. StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
  6573. SourceLocation StartLoc,
  6574. SourceLocation EndLoc,
  6575. OpenMPDirectiveKind CancelRegion) {
  6576. if (DSAStack->isParentNowaitRegion()) {
  6577. Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
  6578. return StmtError();
  6579. }
  6580. if (DSAStack->isParentOrderedRegion()) {
  6581. Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
  6582. return StmtError();
  6583. }
  6584. DSAStack->setParentCancelRegion(/*Cancel=*/true);
  6585. return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
  6586. CancelRegion);
  6587. }
  6588. static bool checkGrainsizeNumTasksClauses(Sema &S,
  6589. ArrayRef<OMPClause *> Clauses) {
  6590. const OMPClause *PrevClause = nullptr;
  6591. bool ErrorFound = false;
  6592. for (const OMPClause *C : Clauses) {
  6593. if (C->getClauseKind() == OMPC_grainsize ||
  6594. C->getClauseKind() == OMPC_num_tasks) {
  6595. if (!PrevClause)
  6596. PrevClause = C;
  6597. else if (PrevClause->getClauseKind() != C->getClauseKind()) {
  6598. S.Diag(C->getBeginLoc(),
  6599. diag::err_omp_grainsize_num_tasks_mutually_exclusive)
  6600. << getOpenMPClauseName(C->getClauseKind())
  6601. << getOpenMPClauseName(PrevClause->getClauseKind());
  6602. S.Diag(PrevClause->getBeginLoc(),
  6603. diag::note_omp_previous_grainsize_num_tasks)
  6604. << getOpenMPClauseName(PrevClause->getClauseKind());
  6605. ErrorFound = true;
  6606. }
  6607. }
  6608. }
  6609. return ErrorFound;
  6610. }
  6611. static bool checkReductionClauseWithNogroup(Sema &S,
  6612. ArrayRef<OMPClause *> Clauses) {
  6613. const OMPClause *ReductionClause = nullptr;
  6614. const OMPClause *NogroupClause = nullptr;
  6615. for (const OMPClause *C : Clauses) {
  6616. if (C->getClauseKind() == OMPC_reduction) {
  6617. ReductionClause = C;
  6618. if (NogroupClause)
  6619. break;
  6620. continue;
  6621. }
  6622. if (C->getClauseKind() == OMPC_nogroup) {
  6623. NogroupClause = C;
  6624. if (ReductionClause)
  6625. break;
  6626. continue;
  6627. }
  6628. }
  6629. if (ReductionClause && NogroupClause) {
  6630. S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
  6631. << SourceRange(NogroupClause->getBeginLoc(),
  6632. NogroupClause->getEndLoc());
  6633. return true;
  6634. }
  6635. return false;
  6636. }
  6637. StmtResult Sema::ActOnOpenMPTaskLoopDirective(
  6638. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  6639. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  6640. if (!AStmt)
  6641. return StmtError();
  6642. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  6643. OMPLoopDirective::HelperExprs B;
  6644. // In presence of clause 'collapse' or 'ordered' with number of loops, it will
  6645. // define the nested loops number.
  6646. unsigned NestedLoopCount =
  6647. checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
  6648. /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
  6649. VarsWithImplicitDSA, B);
  6650. if (NestedLoopCount == 0)
  6651. return StmtError();
  6652. assert((CurContext->isDependentContext() || B.builtAll()) &&
  6653. "omp for loop exprs were not built");
  6654. // OpenMP, [2.9.2 taskloop Construct, Restrictions]
  6655. // The grainsize clause and num_tasks clause are mutually exclusive and may
  6656. // not appear on the same taskloop directive.
  6657. if (checkGrainsizeNumTasksClauses(*this, Clauses))
  6658. return StmtError();
  6659. // OpenMP, [2.9.2 taskloop Construct, Restrictions]
  6660. // If a reduction clause is present on the taskloop directive, the nogroup
  6661. // clause must not be specified.
  6662. if (checkReductionClauseWithNogroup(*this, Clauses))
  6663. return StmtError();
  6664. setFunctionHasBranchProtectedScope();
  6665. return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
  6666. NestedLoopCount, Clauses, AStmt, B);
  6667. }
  6668. StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
  6669. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  6670. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  6671. if (!AStmt)
  6672. return StmtError();
  6673. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  6674. OMPLoopDirective::HelperExprs B;
  6675. // In presence of clause 'collapse' or 'ordered' with number of loops, it will
  6676. // define the nested loops number.
  6677. unsigned NestedLoopCount =
  6678. checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
  6679. /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
  6680. VarsWithImplicitDSA, B);
  6681. if (NestedLoopCount == 0)
  6682. return StmtError();
  6683. assert((CurContext->isDependentContext() || B.builtAll()) &&
  6684. "omp for loop exprs were not built");
  6685. if (!CurContext->isDependentContext()) {
  6686. // Finalize the clauses that need pre-built expressions for CodeGen.
  6687. for (OMPClause *C : Clauses) {
  6688. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  6689. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  6690. B.NumIterations, *this, CurScope,
  6691. DSAStack))
  6692. return StmtError();
  6693. }
  6694. }
  6695. // OpenMP, [2.9.2 taskloop Construct, Restrictions]
  6696. // The grainsize clause and num_tasks clause are mutually exclusive and may
  6697. // not appear on the same taskloop directive.
  6698. if (checkGrainsizeNumTasksClauses(*this, Clauses))
  6699. return StmtError();
  6700. // OpenMP, [2.9.2 taskloop Construct, Restrictions]
  6701. // If a reduction clause is present on the taskloop directive, the nogroup
  6702. // clause must not be specified.
  6703. if (checkReductionClauseWithNogroup(*this, Clauses))
  6704. return StmtError();
  6705. if (checkSimdlenSafelenSpecified(*this, Clauses))
  6706. return StmtError();
  6707. setFunctionHasBranchProtectedScope();
  6708. return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
  6709. NestedLoopCount, Clauses, AStmt, B);
  6710. }
  6711. StmtResult Sema::ActOnOpenMPDistributeDirective(
  6712. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  6713. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  6714. if (!AStmt)
  6715. return StmtError();
  6716. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  6717. OMPLoopDirective::HelperExprs B;
  6718. // In presence of clause 'collapse' with number of loops, it will
  6719. // define the nested loops number.
  6720. unsigned NestedLoopCount =
  6721. checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
  6722. nullptr /*ordered not a clause on distribute*/, AStmt,
  6723. *this, *DSAStack, VarsWithImplicitDSA, B);
  6724. if (NestedLoopCount == 0)
  6725. return StmtError();
  6726. assert((CurContext->isDependentContext() || B.builtAll()) &&
  6727. "omp for loop exprs were not built");
  6728. setFunctionHasBranchProtectedScope();
  6729. return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
  6730. NestedLoopCount, Clauses, AStmt, B);
  6731. }
  6732. StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
  6733. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  6734. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  6735. if (!AStmt)
  6736. return StmtError();
  6737. auto *CS = cast<CapturedStmt>(AStmt);
  6738. // 1.2.2 OpenMP Language Terminology
  6739. // Structured block - An executable statement with a single entry at the
  6740. // top and a single exit at the bottom.
  6741. // The point of exit cannot be a branch out of the structured block.
  6742. // longjmp() and throw() must not violate the entry/exit criteria.
  6743. CS->getCapturedDecl()->setNothrow();
  6744. for (int ThisCaptureLevel =
  6745. getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
  6746. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  6747. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  6748. // 1.2.2 OpenMP Language Terminology
  6749. // Structured block - An executable statement with a single entry at the
  6750. // top and a single exit at the bottom.
  6751. // The point of exit cannot be a branch out of the structured block.
  6752. // longjmp() and throw() must not violate the entry/exit criteria.
  6753. CS->getCapturedDecl()->setNothrow();
  6754. }
  6755. OMPLoopDirective::HelperExprs B;
  6756. // In presence of clause 'collapse' with number of loops, it will
  6757. // define the nested loops number.
  6758. unsigned NestedLoopCount = checkOpenMPLoop(
  6759. OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
  6760. nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
  6761. VarsWithImplicitDSA, B);
  6762. if (NestedLoopCount == 0)
  6763. return StmtError();
  6764. assert((CurContext->isDependentContext() || B.builtAll()) &&
  6765. "omp for loop exprs were not built");
  6766. setFunctionHasBranchProtectedScope();
  6767. return OMPDistributeParallelForDirective::Create(
  6768. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
  6769. DSAStack->isCancelRegion());
  6770. }
  6771. StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
  6772. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  6773. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  6774. if (!AStmt)
  6775. return StmtError();
  6776. auto *CS = cast<CapturedStmt>(AStmt);
  6777. // 1.2.2 OpenMP Language Terminology
  6778. // Structured block - An executable statement with a single entry at the
  6779. // top and a single exit at the bottom.
  6780. // The point of exit cannot be a branch out of the structured block.
  6781. // longjmp() and throw() must not violate the entry/exit criteria.
  6782. CS->getCapturedDecl()->setNothrow();
  6783. for (int ThisCaptureLevel =
  6784. getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
  6785. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  6786. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  6787. // 1.2.2 OpenMP Language Terminology
  6788. // Structured block - An executable statement with a single entry at the
  6789. // top and a single exit at the bottom.
  6790. // The point of exit cannot be a branch out of the structured block.
  6791. // longjmp() and throw() must not violate the entry/exit criteria.
  6792. CS->getCapturedDecl()->setNothrow();
  6793. }
  6794. OMPLoopDirective::HelperExprs B;
  6795. // In presence of clause 'collapse' with number of loops, it will
  6796. // define the nested loops number.
  6797. unsigned NestedLoopCount = checkOpenMPLoop(
  6798. OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
  6799. nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
  6800. VarsWithImplicitDSA, B);
  6801. if (NestedLoopCount == 0)
  6802. return StmtError();
  6803. assert((CurContext->isDependentContext() || B.builtAll()) &&
  6804. "omp for loop exprs were not built");
  6805. if (!CurContext->isDependentContext()) {
  6806. // Finalize the clauses that need pre-built expressions for CodeGen.
  6807. for (OMPClause *C : Clauses) {
  6808. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  6809. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  6810. B.NumIterations, *this, CurScope,
  6811. DSAStack))
  6812. return StmtError();
  6813. }
  6814. }
  6815. if (checkSimdlenSafelenSpecified(*this, Clauses))
  6816. return StmtError();
  6817. setFunctionHasBranchProtectedScope();
  6818. return OMPDistributeParallelForSimdDirective::Create(
  6819. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
  6820. }
  6821. StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
  6822. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  6823. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  6824. if (!AStmt)
  6825. return StmtError();
  6826. auto *CS = cast<CapturedStmt>(AStmt);
  6827. // 1.2.2 OpenMP Language Terminology
  6828. // Structured block - An executable statement with a single entry at the
  6829. // top and a single exit at the bottom.
  6830. // The point of exit cannot be a branch out of the structured block.
  6831. // longjmp() and throw() must not violate the entry/exit criteria.
  6832. CS->getCapturedDecl()->setNothrow();
  6833. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
  6834. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  6835. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  6836. // 1.2.2 OpenMP Language Terminology
  6837. // Structured block - An executable statement with a single entry at the
  6838. // top and a single exit at the bottom.
  6839. // The point of exit cannot be a branch out of the structured block.
  6840. // longjmp() and throw() must not violate the entry/exit criteria.
  6841. CS->getCapturedDecl()->setNothrow();
  6842. }
  6843. OMPLoopDirective::HelperExprs B;
  6844. // In presence of clause 'collapse' with number of loops, it will
  6845. // define the nested loops number.
  6846. unsigned NestedLoopCount =
  6847. checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
  6848. nullptr /*ordered not a clause on distribute*/, CS, *this,
  6849. *DSAStack, VarsWithImplicitDSA, B);
  6850. if (NestedLoopCount == 0)
  6851. return StmtError();
  6852. assert((CurContext->isDependentContext() || B.builtAll()) &&
  6853. "omp for loop exprs were not built");
  6854. if (!CurContext->isDependentContext()) {
  6855. // Finalize the clauses that need pre-built expressions for CodeGen.
  6856. for (OMPClause *C : Clauses) {
  6857. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  6858. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  6859. B.NumIterations, *this, CurScope,
  6860. DSAStack))
  6861. return StmtError();
  6862. }
  6863. }
  6864. if (checkSimdlenSafelenSpecified(*this, Clauses))
  6865. return StmtError();
  6866. setFunctionHasBranchProtectedScope();
  6867. return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
  6868. NestedLoopCount, Clauses, AStmt, B);
  6869. }
  6870. StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
  6871. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  6872. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  6873. if (!AStmt)
  6874. return StmtError();
  6875. auto *CS = cast<CapturedStmt>(AStmt);
  6876. // 1.2.2 OpenMP Language Terminology
  6877. // Structured block - An executable statement with a single entry at the
  6878. // top and a single exit at the bottom.
  6879. // The point of exit cannot be a branch out of the structured block.
  6880. // longjmp() and throw() must not violate the entry/exit criteria.
  6881. CS->getCapturedDecl()->setNothrow();
  6882. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
  6883. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  6884. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  6885. // 1.2.2 OpenMP Language Terminology
  6886. // Structured block - An executable statement with a single entry at the
  6887. // top and a single exit at the bottom.
  6888. // The point of exit cannot be a branch out of the structured block.
  6889. // longjmp() and throw() must not violate the entry/exit criteria.
  6890. CS->getCapturedDecl()->setNothrow();
  6891. }
  6892. OMPLoopDirective::HelperExprs B;
  6893. // In presence of clause 'collapse' or 'ordered' with number of loops, it will
  6894. // define the nested loops number.
  6895. unsigned NestedLoopCount = checkOpenMPLoop(
  6896. OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
  6897. getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
  6898. VarsWithImplicitDSA, B);
  6899. if (NestedLoopCount == 0)
  6900. return StmtError();
  6901. assert((CurContext->isDependentContext() || B.builtAll()) &&
  6902. "omp target parallel for simd loop exprs were not built");
  6903. if (!CurContext->isDependentContext()) {
  6904. // Finalize the clauses that need pre-built expressions for CodeGen.
  6905. for (OMPClause *C : Clauses) {
  6906. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  6907. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  6908. B.NumIterations, *this, CurScope,
  6909. DSAStack))
  6910. return StmtError();
  6911. }
  6912. }
  6913. if (checkSimdlenSafelenSpecified(*this, Clauses))
  6914. return StmtError();
  6915. setFunctionHasBranchProtectedScope();
  6916. return OMPTargetParallelForSimdDirective::Create(
  6917. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
  6918. }
  6919. StmtResult Sema::ActOnOpenMPTargetSimdDirective(
  6920. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  6921. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  6922. if (!AStmt)
  6923. return StmtError();
  6924. auto *CS = cast<CapturedStmt>(AStmt);
  6925. // 1.2.2 OpenMP Language Terminology
  6926. // Structured block - An executable statement with a single entry at the
  6927. // top and a single exit at the bottom.
  6928. // The point of exit cannot be a branch out of the structured block.
  6929. // longjmp() and throw() must not violate the entry/exit criteria.
  6930. CS->getCapturedDecl()->setNothrow();
  6931. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
  6932. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  6933. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  6934. // 1.2.2 OpenMP Language Terminology
  6935. // Structured block - An executable statement with a single entry at the
  6936. // top and a single exit at the bottom.
  6937. // The point of exit cannot be a branch out of the structured block.
  6938. // longjmp() and throw() must not violate the entry/exit criteria.
  6939. CS->getCapturedDecl()->setNothrow();
  6940. }
  6941. OMPLoopDirective::HelperExprs B;
  6942. // In presence of clause 'collapse' with number of loops, it will define the
  6943. // nested loops number.
  6944. unsigned NestedLoopCount =
  6945. checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
  6946. getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
  6947. VarsWithImplicitDSA, B);
  6948. if (NestedLoopCount == 0)
  6949. return StmtError();
  6950. assert((CurContext->isDependentContext() || B.builtAll()) &&
  6951. "omp target simd loop exprs were not built");
  6952. if (!CurContext->isDependentContext()) {
  6953. // Finalize the clauses that need pre-built expressions for CodeGen.
  6954. for (OMPClause *C : Clauses) {
  6955. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  6956. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  6957. B.NumIterations, *this, CurScope,
  6958. DSAStack))
  6959. return StmtError();
  6960. }
  6961. }
  6962. if (checkSimdlenSafelenSpecified(*this, Clauses))
  6963. return StmtError();
  6964. setFunctionHasBranchProtectedScope();
  6965. return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
  6966. NestedLoopCount, Clauses, AStmt, B);
  6967. }
  6968. StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
  6969. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  6970. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  6971. if (!AStmt)
  6972. return StmtError();
  6973. auto *CS = cast<CapturedStmt>(AStmt);
  6974. // 1.2.2 OpenMP Language Terminology
  6975. // Structured block - An executable statement with a single entry at the
  6976. // top and a single exit at the bottom.
  6977. // The point of exit cannot be a branch out of the structured block.
  6978. // longjmp() and throw() must not violate the entry/exit criteria.
  6979. CS->getCapturedDecl()->setNothrow();
  6980. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
  6981. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  6982. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  6983. // 1.2.2 OpenMP Language Terminology
  6984. // Structured block - An executable statement with a single entry at the
  6985. // top and a single exit at the bottom.
  6986. // The point of exit cannot be a branch out of the structured block.
  6987. // longjmp() and throw() must not violate the entry/exit criteria.
  6988. CS->getCapturedDecl()->setNothrow();
  6989. }
  6990. OMPLoopDirective::HelperExprs B;
  6991. // In presence of clause 'collapse' with number of loops, it will
  6992. // define the nested loops number.
  6993. unsigned NestedLoopCount =
  6994. checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
  6995. nullptr /*ordered not a clause on distribute*/, CS, *this,
  6996. *DSAStack, VarsWithImplicitDSA, B);
  6997. if (NestedLoopCount == 0)
  6998. return StmtError();
  6999. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7000. "omp teams distribute loop exprs were not built");
  7001. setFunctionHasBranchProtectedScope();
  7002. DSAStack->setParentTeamsRegionLoc(StartLoc);
  7003. return OMPTeamsDistributeDirective::Create(
  7004. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
  7005. }
  7006. StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
  7007. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7008. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7009. if (!AStmt)
  7010. return StmtError();
  7011. auto *CS = cast<CapturedStmt>(AStmt);
  7012. // 1.2.2 OpenMP Language Terminology
  7013. // Structured block - An executable statement with a single entry at the
  7014. // top and a single exit at the bottom.
  7015. // The point of exit cannot be a branch out of the structured block.
  7016. // longjmp() and throw() must not violate the entry/exit criteria.
  7017. CS->getCapturedDecl()->setNothrow();
  7018. for (int ThisCaptureLevel =
  7019. getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
  7020. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7021. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7022. // 1.2.2 OpenMP Language Terminology
  7023. // Structured block - An executable statement with a single entry at the
  7024. // top and a single exit at the bottom.
  7025. // The point of exit cannot be a branch out of the structured block.
  7026. // longjmp() and throw() must not violate the entry/exit criteria.
  7027. CS->getCapturedDecl()->setNothrow();
  7028. }
  7029. OMPLoopDirective::HelperExprs B;
  7030. // In presence of clause 'collapse' with number of loops, it will
  7031. // define the nested loops number.
  7032. unsigned NestedLoopCount = checkOpenMPLoop(
  7033. OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
  7034. nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
  7035. VarsWithImplicitDSA, B);
  7036. if (NestedLoopCount == 0)
  7037. return StmtError();
  7038. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7039. "omp teams distribute simd loop exprs were not built");
  7040. if (!CurContext->isDependentContext()) {
  7041. // Finalize the clauses that need pre-built expressions for CodeGen.
  7042. for (OMPClause *C : Clauses) {
  7043. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  7044. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  7045. B.NumIterations, *this, CurScope,
  7046. DSAStack))
  7047. return StmtError();
  7048. }
  7049. }
  7050. if (checkSimdlenSafelenSpecified(*this, Clauses))
  7051. return StmtError();
  7052. setFunctionHasBranchProtectedScope();
  7053. DSAStack->setParentTeamsRegionLoc(StartLoc);
  7054. return OMPTeamsDistributeSimdDirective::Create(
  7055. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
  7056. }
  7057. StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
  7058. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7059. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7060. if (!AStmt)
  7061. return StmtError();
  7062. auto *CS = cast<CapturedStmt>(AStmt);
  7063. // 1.2.2 OpenMP Language Terminology
  7064. // Structured block - An executable statement with a single entry at the
  7065. // top and a single exit at the bottom.
  7066. // The point of exit cannot be a branch out of the structured block.
  7067. // longjmp() and throw() must not violate the entry/exit criteria.
  7068. CS->getCapturedDecl()->setNothrow();
  7069. for (int ThisCaptureLevel =
  7070. getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
  7071. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7072. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7073. // 1.2.2 OpenMP Language Terminology
  7074. // Structured block - An executable statement with a single entry at the
  7075. // top and a single exit at the bottom.
  7076. // The point of exit cannot be a branch out of the structured block.
  7077. // longjmp() and throw() must not violate the entry/exit criteria.
  7078. CS->getCapturedDecl()->setNothrow();
  7079. }
  7080. OMPLoopDirective::HelperExprs B;
  7081. // In presence of clause 'collapse' with number of loops, it will
  7082. // define the nested loops number.
  7083. unsigned NestedLoopCount = checkOpenMPLoop(
  7084. OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
  7085. nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
  7086. VarsWithImplicitDSA, B);
  7087. if (NestedLoopCount == 0)
  7088. return StmtError();
  7089. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7090. "omp for loop exprs were not built");
  7091. if (!CurContext->isDependentContext()) {
  7092. // Finalize the clauses that need pre-built expressions for CodeGen.
  7093. for (OMPClause *C : Clauses) {
  7094. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  7095. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  7096. B.NumIterations, *this, CurScope,
  7097. DSAStack))
  7098. return StmtError();
  7099. }
  7100. }
  7101. if (checkSimdlenSafelenSpecified(*this, Clauses))
  7102. return StmtError();
  7103. setFunctionHasBranchProtectedScope();
  7104. DSAStack->setParentTeamsRegionLoc(StartLoc);
  7105. return OMPTeamsDistributeParallelForSimdDirective::Create(
  7106. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
  7107. }
  7108. StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
  7109. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7110. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7111. if (!AStmt)
  7112. return StmtError();
  7113. auto *CS = cast<CapturedStmt>(AStmt);
  7114. // 1.2.2 OpenMP Language Terminology
  7115. // Structured block - An executable statement with a single entry at the
  7116. // top and a single exit at the bottom.
  7117. // The point of exit cannot be a branch out of the structured block.
  7118. // longjmp() and throw() must not violate the entry/exit criteria.
  7119. CS->getCapturedDecl()->setNothrow();
  7120. for (int ThisCaptureLevel =
  7121. getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
  7122. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7123. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7124. // 1.2.2 OpenMP Language Terminology
  7125. // Structured block - An executable statement with a single entry at the
  7126. // top and a single exit at the bottom.
  7127. // The point of exit cannot be a branch out of the structured block.
  7128. // longjmp() and throw() must not violate the entry/exit criteria.
  7129. CS->getCapturedDecl()->setNothrow();
  7130. }
  7131. OMPLoopDirective::HelperExprs B;
  7132. // In presence of clause 'collapse' with number of loops, it will
  7133. // define the nested loops number.
  7134. unsigned NestedLoopCount = checkOpenMPLoop(
  7135. OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
  7136. nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
  7137. VarsWithImplicitDSA, B);
  7138. if (NestedLoopCount == 0)
  7139. return StmtError();
  7140. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7141. "omp for loop exprs were not built");
  7142. setFunctionHasBranchProtectedScope();
  7143. DSAStack->setParentTeamsRegionLoc(StartLoc);
  7144. return OMPTeamsDistributeParallelForDirective::Create(
  7145. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
  7146. DSAStack->isCancelRegion());
  7147. }
  7148. StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
  7149. Stmt *AStmt,
  7150. SourceLocation StartLoc,
  7151. SourceLocation EndLoc) {
  7152. if (!AStmt)
  7153. return StmtError();
  7154. auto *CS = cast<CapturedStmt>(AStmt);
  7155. // 1.2.2 OpenMP Language Terminology
  7156. // Structured block - An executable statement with a single entry at the
  7157. // top and a single exit at the bottom.
  7158. // The point of exit cannot be a branch out of the structured block.
  7159. // longjmp() and throw() must not violate the entry/exit criteria.
  7160. CS->getCapturedDecl()->setNothrow();
  7161. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
  7162. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7163. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7164. // 1.2.2 OpenMP Language Terminology
  7165. // Structured block - An executable statement with a single entry at the
  7166. // top and a single exit at the bottom.
  7167. // The point of exit cannot be a branch out of the structured block.
  7168. // longjmp() and throw() must not violate the entry/exit criteria.
  7169. CS->getCapturedDecl()->setNothrow();
  7170. }
  7171. setFunctionHasBranchProtectedScope();
  7172. return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
  7173. AStmt);
  7174. }
  7175. StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
  7176. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7177. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7178. if (!AStmt)
  7179. return StmtError();
  7180. auto *CS = cast<CapturedStmt>(AStmt);
  7181. // 1.2.2 OpenMP Language Terminology
  7182. // Structured block - An executable statement with a single entry at the
  7183. // top and a single exit at the bottom.
  7184. // The point of exit cannot be a branch out of the structured block.
  7185. // longjmp() and throw() must not violate the entry/exit criteria.
  7186. CS->getCapturedDecl()->setNothrow();
  7187. for (int ThisCaptureLevel =
  7188. getOpenMPCaptureLevels(OMPD_target_teams_distribute);
  7189. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7190. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7191. // 1.2.2 OpenMP Language Terminology
  7192. // Structured block - An executable statement with a single entry at the
  7193. // top and a single exit at the bottom.
  7194. // The point of exit cannot be a branch out of the structured block.
  7195. // longjmp() and throw() must not violate the entry/exit criteria.
  7196. CS->getCapturedDecl()->setNothrow();
  7197. }
  7198. OMPLoopDirective::HelperExprs B;
  7199. // In presence of clause 'collapse' with number of loops, it will
  7200. // define the nested loops number.
  7201. unsigned NestedLoopCount = checkOpenMPLoop(
  7202. OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
  7203. nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
  7204. VarsWithImplicitDSA, B);
  7205. if (NestedLoopCount == 0)
  7206. return StmtError();
  7207. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7208. "omp target teams distribute loop exprs were not built");
  7209. setFunctionHasBranchProtectedScope();
  7210. return OMPTargetTeamsDistributeDirective::Create(
  7211. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
  7212. }
  7213. StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
  7214. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7215. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7216. if (!AStmt)
  7217. return StmtError();
  7218. auto *CS = cast<CapturedStmt>(AStmt);
  7219. // 1.2.2 OpenMP Language Terminology
  7220. // Structured block - An executable statement with a single entry at the
  7221. // top and a single exit at the bottom.
  7222. // The point of exit cannot be a branch out of the structured block.
  7223. // longjmp() and throw() must not violate the entry/exit criteria.
  7224. CS->getCapturedDecl()->setNothrow();
  7225. for (int ThisCaptureLevel =
  7226. getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
  7227. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7228. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7229. // 1.2.2 OpenMP Language Terminology
  7230. // Structured block - An executable statement with a single entry at the
  7231. // top and a single exit at the bottom.
  7232. // The point of exit cannot be a branch out of the structured block.
  7233. // longjmp() and throw() must not violate the entry/exit criteria.
  7234. CS->getCapturedDecl()->setNothrow();
  7235. }
  7236. OMPLoopDirective::HelperExprs B;
  7237. // In presence of clause 'collapse' with number of loops, it will
  7238. // define the nested loops number.
  7239. unsigned NestedLoopCount = checkOpenMPLoop(
  7240. OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
  7241. nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
  7242. VarsWithImplicitDSA, B);
  7243. if (NestedLoopCount == 0)
  7244. return StmtError();
  7245. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7246. "omp target teams distribute parallel for loop exprs were not built");
  7247. if (!CurContext->isDependentContext()) {
  7248. // Finalize the clauses that need pre-built expressions for CodeGen.
  7249. for (OMPClause *C : Clauses) {
  7250. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  7251. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  7252. B.NumIterations, *this, CurScope,
  7253. DSAStack))
  7254. return StmtError();
  7255. }
  7256. }
  7257. setFunctionHasBranchProtectedScope();
  7258. return OMPTargetTeamsDistributeParallelForDirective::Create(
  7259. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
  7260. DSAStack->isCancelRegion());
  7261. }
  7262. StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
  7263. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7264. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7265. if (!AStmt)
  7266. return StmtError();
  7267. auto *CS = cast<CapturedStmt>(AStmt);
  7268. // 1.2.2 OpenMP Language Terminology
  7269. // Structured block - An executable statement with a single entry at the
  7270. // top and a single exit at the bottom.
  7271. // The point of exit cannot be a branch out of the structured block.
  7272. // longjmp() and throw() must not violate the entry/exit criteria.
  7273. CS->getCapturedDecl()->setNothrow();
  7274. for (int ThisCaptureLevel = getOpenMPCaptureLevels(
  7275. OMPD_target_teams_distribute_parallel_for_simd);
  7276. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7277. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7278. // 1.2.2 OpenMP Language Terminology
  7279. // Structured block - An executable statement with a single entry at the
  7280. // top and a single exit at the bottom.
  7281. // The point of exit cannot be a branch out of the structured block.
  7282. // longjmp() and throw() must not violate the entry/exit criteria.
  7283. CS->getCapturedDecl()->setNothrow();
  7284. }
  7285. OMPLoopDirective::HelperExprs B;
  7286. // In presence of clause 'collapse' with number of loops, it will
  7287. // define the nested loops number.
  7288. unsigned NestedLoopCount =
  7289. checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
  7290. getCollapseNumberExpr(Clauses),
  7291. nullptr /*ordered not a clause on distribute*/, CS, *this,
  7292. *DSAStack, VarsWithImplicitDSA, B);
  7293. if (NestedLoopCount == 0)
  7294. return StmtError();
  7295. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7296. "omp target teams distribute parallel for simd loop exprs were not "
  7297. "built");
  7298. if (!CurContext->isDependentContext()) {
  7299. // Finalize the clauses that need pre-built expressions for CodeGen.
  7300. for (OMPClause *C : Clauses) {
  7301. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  7302. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  7303. B.NumIterations, *this, CurScope,
  7304. DSAStack))
  7305. return StmtError();
  7306. }
  7307. }
  7308. if (checkSimdlenSafelenSpecified(*this, Clauses))
  7309. return StmtError();
  7310. setFunctionHasBranchProtectedScope();
  7311. return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
  7312. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
  7313. }
  7314. StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
  7315. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7316. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7317. if (!AStmt)
  7318. return StmtError();
  7319. auto *CS = cast<CapturedStmt>(AStmt);
  7320. // 1.2.2 OpenMP Language Terminology
  7321. // Structured block - An executable statement with a single entry at the
  7322. // top and a single exit at the bottom.
  7323. // The point of exit cannot be a branch out of the structured block.
  7324. // longjmp() and throw() must not violate the entry/exit criteria.
  7325. CS->getCapturedDecl()->setNothrow();
  7326. for (int ThisCaptureLevel =
  7327. getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
  7328. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7329. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7330. // 1.2.2 OpenMP Language Terminology
  7331. // Structured block - An executable statement with a single entry at the
  7332. // top and a single exit at the bottom.
  7333. // The point of exit cannot be a branch out of the structured block.
  7334. // longjmp() and throw() must not violate the entry/exit criteria.
  7335. CS->getCapturedDecl()->setNothrow();
  7336. }
  7337. OMPLoopDirective::HelperExprs B;
  7338. // In presence of clause 'collapse' with number of loops, it will
  7339. // define the nested loops number.
  7340. unsigned NestedLoopCount = checkOpenMPLoop(
  7341. OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
  7342. nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
  7343. VarsWithImplicitDSA, B);
  7344. if (NestedLoopCount == 0)
  7345. return StmtError();
  7346. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7347. "omp target teams distribute simd loop exprs were not built");
  7348. if (!CurContext->isDependentContext()) {
  7349. // Finalize the clauses that need pre-built expressions for CodeGen.
  7350. for (OMPClause *C : Clauses) {
  7351. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  7352. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  7353. B.NumIterations, *this, CurScope,
  7354. DSAStack))
  7355. return StmtError();
  7356. }
  7357. }
  7358. if (checkSimdlenSafelenSpecified(*this, Clauses))
  7359. return StmtError();
  7360. setFunctionHasBranchProtectedScope();
  7361. return OMPTargetTeamsDistributeSimdDirective::Create(
  7362. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
  7363. }
  7364. OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
  7365. SourceLocation StartLoc,
  7366. SourceLocation LParenLoc,
  7367. SourceLocation EndLoc) {
  7368. OMPClause *Res = nullptr;
  7369. switch (Kind) {
  7370. case OMPC_final:
  7371. Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
  7372. break;
  7373. case OMPC_num_threads:
  7374. Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
  7375. break;
  7376. case OMPC_safelen:
  7377. Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
  7378. break;
  7379. case OMPC_simdlen:
  7380. Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
  7381. break;
  7382. case OMPC_collapse:
  7383. Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
  7384. break;
  7385. case OMPC_ordered:
  7386. Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
  7387. break;
  7388. case OMPC_device:
  7389. Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
  7390. break;
  7391. case OMPC_num_teams:
  7392. Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
  7393. break;
  7394. case OMPC_thread_limit:
  7395. Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
  7396. break;
  7397. case OMPC_priority:
  7398. Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
  7399. break;
  7400. case OMPC_grainsize:
  7401. Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
  7402. break;
  7403. case OMPC_num_tasks:
  7404. Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
  7405. break;
  7406. case OMPC_hint:
  7407. Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
  7408. break;
  7409. case OMPC_if:
  7410. case OMPC_default:
  7411. case OMPC_proc_bind:
  7412. case OMPC_schedule:
  7413. case OMPC_private:
  7414. case OMPC_firstprivate:
  7415. case OMPC_lastprivate:
  7416. case OMPC_shared:
  7417. case OMPC_reduction:
  7418. case OMPC_task_reduction:
  7419. case OMPC_in_reduction:
  7420. case OMPC_linear:
  7421. case OMPC_aligned:
  7422. case OMPC_copyin:
  7423. case OMPC_copyprivate:
  7424. case OMPC_nowait:
  7425. case OMPC_untied:
  7426. case OMPC_mergeable:
  7427. case OMPC_threadprivate:
  7428. case OMPC_flush:
  7429. case OMPC_read:
  7430. case OMPC_write:
  7431. case OMPC_update:
  7432. case OMPC_capture:
  7433. case OMPC_seq_cst:
  7434. case OMPC_depend:
  7435. case OMPC_threads:
  7436. case OMPC_simd:
  7437. case OMPC_map:
  7438. case OMPC_nogroup:
  7439. case OMPC_dist_schedule:
  7440. case OMPC_defaultmap:
  7441. case OMPC_unknown:
  7442. case OMPC_uniform:
  7443. case OMPC_to:
  7444. case OMPC_from:
  7445. case OMPC_use_device_ptr:
  7446. case OMPC_is_device_ptr:
  7447. case OMPC_unified_address:
  7448. llvm_unreachable("Clause is not allowed.");
  7449. }
  7450. return Res;
  7451. }
  7452. // An OpenMP directive such as 'target parallel' has two captured regions:
  7453. // for the 'target' and 'parallel' respectively. This function returns
  7454. // the region in which to capture expressions associated with a clause.
  7455. // A return value of OMPD_unknown signifies that the expression should not
  7456. // be captured.
  7457. static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
  7458. OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
  7459. OpenMPDirectiveKind NameModifier = OMPD_unknown) {
  7460. OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
  7461. switch (CKind) {
  7462. case OMPC_if:
  7463. switch (DKind) {
  7464. case OMPD_target_parallel:
  7465. case OMPD_target_parallel_for:
  7466. case OMPD_target_parallel_for_simd:
  7467. // If this clause applies to the nested 'parallel' region, capture within
  7468. // the 'target' region, otherwise do not capture.
  7469. if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
  7470. CaptureRegion = OMPD_target;
  7471. break;
  7472. case OMPD_target_teams_distribute_parallel_for:
  7473. case OMPD_target_teams_distribute_parallel_for_simd:
  7474. // If this clause applies to the nested 'parallel' region, capture within
  7475. // the 'teams' region, otherwise do not capture.
  7476. if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
  7477. CaptureRegion = OMPD_teams;
  7478. break;
  7479. case OMPD_teams_distribute_parallel_for:
  7480. case OMPD_teams_distribute_parallel_for_simd:
  7481. CaptureRegion = OMPD_teams;
  7482. break;
  7483. case OMPD_target_update:
  7484. case OMPD_target_enter_data:
  7485. case OMPD_target_exit_data:
  7486. CaptureRegion = OMPD_task;
  7487. break;
  7488. case OMPD_cancel:
  7489. case OMPD_parallel:
  7490. case OMPD_parallel_sections:
  7491. case OMPD_parallel_for:
  7492. case OMPD_parallel_for_simd:
  7493. case OMPD_target:
  7494. case OMPD_target_simd:
  7495. case OMPD_target_teams:
  7496. case OMPD_target_teams_distribute:
  7497. case OMPD_target_teams_distribute_simd:
  7498. case OMPD_distribute_parallel_for:
  7499. case OMPD_distribute_parallel_for_simd:
  7500. case OMPD_task:
  7501. case OMPD_taskloop:
  7502. case OMPD_taskloop_simd:
  7503. case OMPD_target_data:
  7504. // Do not capture if-clause expressions.
  7505. break;
  7506. case OMPD_threadprivate:
  7507. case OMPD_taskyield:
  7508. case OMPD_barrier:
  7509. case OMPD_taskwait:
  7510. case OMPD_cancellation_point:
  7511. case OMPD_flush:
  7512. case OMPD_declare_reduction:
  7513. case OMPD_declare_simd:
  7514. case OMPD_declare_target:
  7515. case OMPD_end_declare_target:
  7516. case OMPD_teams:
  7517. case OMPD_simd:
  7518. case OMPD_for:
  7519. case OMPD_for_simd:
  7520. case OMPD_sections:
  7521. case OMPD_section:
  7522. case OMPD_single:
  7523. case OMPD_master:
  7524. case OMPD_critical:
  7525. case OMPD_taskgroup:
  7526. case OMPD_distribute:
  7527. case OMPD_ordered:
  7528. case OMPD_atomic:
  7529. case OMPD_distribute_simd:
  7530. case OMPD_teams_distribute:
  7531. case OMPD_teams_distribute_simd:
  7532. case OMPD_requires:
  7533. llvm_unreachable("Unexpected OpenMP directive with if-clause");
  7534. case OMPD_unknown:
  7535. llvm_unreachable("Unknown OpenMP directive");
  7536. }
  7537. break;
  7538. case OMPC_num_threads:
  7539. switch (DKind) {
  7540. case OMPD_target_parallel:
  7541. case OMPD_target_parallel_for:
  7542. case OMPD_target_parallel_for_simd:
  7543. CaptureRegion = OMPD_target;
  7544. break;
  7545. case OMPD_teams_distribute_parallel_for:
  7546. case OMPD_teams_distribute_parallel_for_simd:
  7547. case OMPD_target_teams_distribute_parallel_for:
  7548. case OMPD_target_teams_distribute_parallel_for_simd:
  7549. CaptureRegion = OMPD_teams;
  7550. break;
  7551. case OMPD_parallel:
  7552. case OMPD_parallel_sections:
  7553. case OMPD_parallel_for:
  7554. case OMPD_parallel_for_simd:
  7555. case OMPD_distribute_parallel_for:
  7556. case OMPD_distribute_parallel_for_simd:
  7557. // Do not capture num_threads-clause expressions.
  7558. break;
  7559. case OMPD_target_data:
  7560. case OMPD_target_enter_data:
  7561. case OMPD_target_exit_data:
  7562. case OMPD_target_update:
  7563. case OMPD_target:
  7564. case OMPD_target_simd:
  7565. case OMPD_target_teams:
  7566. case OMPD_target_teams_distribute:
  7567. case OMPD_target_teams_distribute_simd:
  7568. case OMPD_cancel:
  7569. case OMPD_task:
  7570. case OMPD_taskloop:
  7571. case OMPD_taskloop_simd:
  7572. case OMPD_threadprivate:
  7573. case OMPD_taskyield:
  7574. case OMPD_barrier:
  7575. case OMPD_taskwait:
  7576. case OMPD_cancellation_point:
  7577. case OMPD_flush:
  7578. case OMPD_declare_reduction:
  7579. case OMPD_declare_simd:
  7580. case OMPD_declare_target:
  7581. case OMPD_end_declare_target:
  7582. case OMPD_teams:
  7583. case OMPD_simd:
  7584. case OMPD_for:
  7585. case OMPD_for_simd:
  7586. case OMPD_sections:
  7587. case OMPD_section:
  7588. case OMPD_single:
  7589. case OMPD_master:
  7590. case OMPD_critical:
  7591. case OMPD_taskgroup:
  7592. case OMPD_distribute:
  7593. case OMPD_ordered:
  7594. case OMPD_atomic:
  7595. case OMPD_distribute_simd:
  7596. case OMPD_teams_distribute:
  7597. case OMPD_teams_distribute_simd:
  7598. case OMPD_requires:
  7599. llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
  7600. case OMPD_unknown:
  7601. llvm_unreachable("Unknown OpenMP directive");
  7602. }
  7603. break;
  7604. case OMPC_num_teams:
  7605. switch (DKind) {
  7606. case OMPD_target_teams:
  7607. case OMPD_target_teams_distribute:
  7608. case OMPD_target_teams_distribute_simd:
  7609. case OMPD_target_teams_distribute_parallel_for:
  7610. case OMPD_target_teams_distribute_parallel_for_simd:
  7611. CaptureRegion = OMPD_target;
  7612. break;
  7613. case OMPD_teams_distribute_parallel_for:
  7614. case OMPD_teams_distribute_parallel_for_simd:
  7615. case OMPD_teams:
  7616. case OMPD_teams_distribute:
  7617. case OMPD_teams_distribute_simd:
  7618. // Do not capture num_teams-clause expressions.
  7619. break;
  7620. case OMPD_distribute_parallel_for:
  7621. case OMPD_distribute_parallel_for_simd:
  7622. case OMPD_task:
  7623. case OMPD_taskloop:
  7624. case OMPD_taskloop_simd:
  7625. case OMPD_target_data:
  7626. case OMPD_target_enter_data:
  7627. case OMPD_target_exit_data:
  7628. case OMPD_target_update:
  7629. case OMPD_cancel:
  7630. case OMPD_parallel:
  7631. case OMPD_parallel_sections:
  7632. case OMPD_parallel_for:
  7633. case OMPD_parallel_for_simd:
  7634. case OMPD_target:
  7635. case OMPD_target_simd:
  7636. case OMPD_target_parallel:
  7637. case OMPD_target_parallel_for:
  7638. case OMPD_target_parallel_for_simd:
  7639. case OMPD_threadprivate:
  7640. case OMPD_taskyield:
  7641. case OMPD_barrier:
  7642. case OMPD_taskwait:
  7643. case OMPD_cancellation_point:
  7644. case OMPD_flush:
  7645. case OMPD_declare_reduction:
  7646. case OMPD_declare_simd:
  7647. case OMPD_declare_target:
  7648. case OMPD_end_declare_target:
  7649. case OMPD_simd:
  7650. case OMPD_for:
  7651. case OMPD_for_simd:
  7652. case OMPD_sections:
  7653. case OMPD_section:
  7654. case OMPD_single:
  7655. case OMPD_master:
  7656. case OMPD_critical:
  7657. case OMPD_taskgroup:
  7658. case OMPD_distribute:
  7659. case OMPD_ordered:
  7660. case OMPD_atomic:
  7661. case OMPD_distribute_simd:
  7662. case OMPD_requires:
  7663. llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
  7664. case OMPD_unknown:
  7665. llvm_unreachable("Unknown OpenMP directive");
  7666. }
  7667. break;
  7668. case OMPC_thread_limit:
  7669. switch (DKind) {
  7670. case OMPD_target_teams:
  7671. case OMPD_target_teams_distribute:
  7672. case OMPD_target_teams_distribute_simd:
  7673. case OMPD_target_teams_distribute_parallel_for:
  7674. case OMPD_target_teams_distribute_parallel_for_simd:
  7675. CaptureRegion = OMPD_target;
  7676. break;
  7677. case OMPD_teams_distribute_parallel_for:
  7678. case OMPD_teams_distribute_parallel_for_simd:
  7679. case OMPD_teams:
  7680. case OMPD_teams_distribute:
  7681. case OMPD_teams_distribute_simd:
  7682. // Do not capture thread_limit-clause expressions.
  7683. break;
  7684. case OMPD_distribute_parallel_for:
  7685. case OMPD_distribute_parallel_for_simd:
  7686. case OMPD_task:
  7687. case OMPD_taskloop:
  7688. case OMPD_taskloop_simd:
  7689. case OMPD_target_data:
  7690. case OMPD_target_enter_data:
  7691. case OMPD_target_exit_data:
  7692. case OMPD_target_update:
  7693. case OMPD_cancel:
  7694. case OMPD_parallel:
  7695. case OMPD_parallel_sections:
  7696. case OMPD_parallel_for:
  7697. case OMPD_parallel_for_simd:
  7698. case OMPD_target:
  7699. case OMPD_target_simd:
  7700. case OMPD_target_parallel:
  7701. case OMPD_target_parallel_for:
  7702. case OMPD_target_parallel_for_simd:
  7703. case OMPD_threadprivate:
  7704. case OMPD_taskyield:
  7705. case OMPD_barrier:
  7706. case OMPD_taskwait:
  7707. case OMPD_cancellation_point:
  7708. case OMPD_flush:
  7709. case OMPD_declare_reduction:
  7710. case OMPD_declare_simd:
  7711. case OMPD_declare_target:
  7712. case OMPD_end_declare_target:
  7713. case OMPD_simd:
  7714. case OMPD_for:
  7715. case OMPD_for_simd:
  7716. case OMPD_sections:
  7717. case OMPD_section:
  7718. case OMPD_single:
  7719. case OMPD_master:
  7720. case OMPD_critical:
  7721. case OMPD_taskgroup:
  7722. case OMPD_distribute:
  7723. case OMPD_ordered:
  7724. case OMPD_atomic:
  7725. case OMPD_distribute_simd:
  7726. case OMPD_requires:
  7727. llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
  7728. case OMPD_unknown:
  7729. llvm_unreachable("Unknown OpenMP directive");
  7730. }
  7731. break;
  7732. case OMPC_schedule:
  7733. switch (DKind) {
  7734. case OMPD_parallel_for:
  7735. case OMPD_parallel_for_simd:
  7736. case OMPD_distribute_parallel_for:
  7737. case OMPD_distribute_parallel_for_simd:
  7738. case OMPD_teams_distribute_parallel_for:
  7739. case OMPD_teams_distribute_parallel_for_simd:
  7740. case OMPD_target_parallel_for:
  7741. case OMPD_target_parallel_for_simd:
  7742. case OMPD_target_teams_distribute_parallel_for:
  7743. case OMPD_target_teams_distribute_parallel_for_simd:
  7744. CaptureRegion = OMPD_parallel;
  7745. break;
  7746. case OMPD_for:
  7747. case OMPD_for_simd:
  7748. // Do not capture schedule-clause expressions.
  7749. break;
  7750. case OMPD_task:
  7751. case OMPD_taskloop:
  7752. case OMPD_taskloop_simd:
  7753. case OMPD_target_data:
  7754. case OMPD_target_enter_data:
  7755. case OMPD_target_exit_data:
  7756. case OMPD_target_update:
  7757. case OMPD_teams:
  7758. case OMPD_teams_distribute:
  7759. case OMPD_teams_distribute_simd:
  7760. case OMPD_target_teams_distribute:
  7761. case OMPD_target_teams_distribute_simd:
  7762. case OMPD_target:
  7763. case OMPD_target_simd:
  7764. case OMPD_target_parallel:
  7765. case OMPD_cancel:
  7766. case OMPD_parallel:
  7767. case OMPD_parallel_sections:
  7768. case OMPD_threadprivate:
  7769. case OMPD_taskyield:
  7770. case OMPD_barrier:
  7771. case OMPD_taskwait:
  7772. case OMPD_cancellation_point:
  7773. case OMPD_flush:
  7774. case OMPD_declare_reduction:
  7775. case OMPD_declare_simd:
  7776. case OMPD_declare_target:
  7777. case OMPD_end_declare_target:
  7778. case OMPD_simd:
  7779. case OMPD_sections:
  7780. case OMPD_section:
  7781. case OMPD_single:
  7782. case OMPD_master:
  7783. case OMPD_critical:
  7784. case OMPD_taskgroup:
  7785. case OMPD_distribute:
  7786. case OMPD_ordered:
  7787. case OMPD_atomic:
  7788. case OMPD_distribute_simd:
  7789. case OMPD_target_teams:
  7790. case OMPD_requires:
  7791. llvm_unreachable("Unexpected OpenMP directive with schedule clause");
  7792. case OMPD_unknown:
  7793. llvm_unreachable("Unknown OpenMP directive");
  7794. }
  7795. break;
  7796. case OMPC_dist_schedule:
  7797. switch (DKind) {
  7798. case OMPD_teams_distribute_parallel_for:
  7799. case OMPD_teams_distribute_parallel_for_simd:
  7800. case OMPD_teams_distribute:
  7801. case OMPD_teams_distribute_simd:
  7802. case OMPD_target_teams_distribute_parallel_for:
  7803. case OMPD_target_teams_distribute_parallel_for_simd:
  7804. case OMPD_target_teams_distribute:
  7805. case OMPD_target_teams_distribute_simd:
  7806. CaptureRegion = OMPD_teams;
  7807. break;
  7808. case OMPD_distribute_parallel_for:
  7809. case OMPD_distribute_parallel_for_simd:
  7810. case OMPD_distribute:
  7811. case OMPD_distribute_simd:
  7812. // Do not capture thread_limit-clause expressions.
  7813. break;
  7814. case OMPD_parallel_for:
  7815. case OMPD_parallel_for_simd:
  7816. case OMPD_target_parallel_for_simd:
  7817. case OMPD_target_parallel_for:
  7818. case OMPD_task:
  7819. case OMPD_taskloop:
  7820. case OMPD_taskloop_simd:
  7821. case OMPD_target_data:
  7822. case OMPD_target_enter_data:
  7823. case OMPD_target_exit_data:
  7824. case OMPD_target_update:
  7825. case OMPD_teams:
  7826. case OMPD_target:
  7827. case OMPD_target_simd:
  7828. case OMPD_target_parallel:
  7829. case OMPD_cancel:
  7830. case OMPD_parallel:
  7831. case OMPD_parallel_sections:
  7832. case OMPD_threadprivate:
  7833. case OMPD_taskyield:
  7834. case OMPD_barrier:
  7835. case OMPD_taskwait:
  7836. case OMPD_cancellation_point:
  7837. case OMPD_flush:
  7838. case OMPD_declare_reduction:
  7839. case OMPD_declare_simd:
  7840. case OMPD_declare_target:
  7841. case OMPD_end_declare_target:
  7842. case OMPD_simd:
  7843. case OMPD_for:
  7844. case OMPD_for_simd:
  7845. case OMPD_sections:
  7846. case OMPD_section:
  7847. case OMPD_single:
  7848. case OMPD_master:
  7849. case OMPD_critical:
  7850. case OMPD_taskgroup:
  7851. case OMPD_ordered:
  7852. case OMPD_atomic:
  7853. case OMPD_target_teams:
  7854. case OMPD_requires:
  7855. llvm_unreachable("Unexpected OpenMP directive with schedule clause");
  7856. case OMPD_unknown:
  7857. llvm_unreachable("Unknown OpenMP directive");
  7858. }
  7859. break;
  7860. case OMPC_device:
  7861. switch (DKind) {
  7862. case OMPD_target_update:
  7863. case OMPD_target_enter_data:
  7864. case OMPD_target_exit_data:
  7865. case OMPD_target:
  7866. case OMPD_target_simd:
  7867. case OMPD_target_teams:
  7868. case OMPD_target_parallel:
  7869. case OMPD_target_teams_distribute:
  7870. case OMPD_target_teams_distribute_simd:
  7871. case OMPD_target_parallel_for:
  7872. case OMPD_target_parallel_for_simd:
  7873. case OMPD_target_teams_distribute_parallel_for:
  7874. case OMPD_target_teams_distribute_parallel_for_simd:
  7875. CaptureRegion = OMPD_task;
  7876. break;
  7877. case OMPD_target_data:
  7878. // Do not capture device-clause expressions.
  7879. break;
  7880. case OMPD_teams_distribute_parallel_for:
  7881. case OMPD_teams_distribute_parallel_for_simd:
  7882. case OMPD_teams:
  7883. case OMPD_teams_distribute:
  7884. case OMPD_teams_distribute_simd:
  7885. case OMPD_distribute_parallel_for:
  7886. case OMPD_distribute_parallel_for_simd:
  7887. case OMPD_task:
  7888. case OMPD_taskloop:
  7889. case OMPD_taskloop_simd:
  7890. case OMPD_cancel:
  7891. case OMPD_parallel:
  7892. case OMPD_parallel_sections:
  7893. case OMPD_parallel_for:
  7894. case OMPD_parallel_for_simd:
  7895. case OMPD_threadprivate:
  7896. case OMPD_taskyield:
  7897. case OMPD_barrier:
  7898. case OMPD_taskwait:
  7899. case OMPD_cancellation_point:
  7900. case OMPD_flush:
  7901. case OMPD_declare_reduction:
  7902. case OMPD_declare_simd:
  7903. case OMPD_declare_target:
  7904. case OMPD_end_declare_target:
  7905. case OMPD_simd:
  7906. case OMPD_for:
  7907. case OMPD_for_simd:
  7908. case OMPD_sections:
  7909. case OMPD_section:
  7910. case OMPD_single:
  7911. case OMPD_master:
  7912. case OMPD_critical:
  7913. case OMPD_taskgroup:
  7914. case OMPD_distribute:
  7915. case OMPD_ordered:
  7916. case OMPD_atomic:
  7917. case OMPD_distribute_simd:
  7918. case OMPD_requires:
  7919. llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
  7920. case OMPD_unknown:
  7921. llvm_unreachable("Unknown OpenMP directive");
  7922. }
  7923. break;
  7924. case OMPC_firstprivate:
  7925. case OMPC_lastprivate:
  7926. case OMPC_reduction:
  7927. case OMPC_task_reduction:
  7928. case OMPC_in_reduction:
  7929. case OMPC_linear:
  7930. case OMPC_default:
  7931. case OMPC_proc_bind:
  7932. case OMPC_final:
  7933. case OMPC_safelen:
  7934. case OMPC_simdlen:
  7935. case OMPC_collapse:
  7936. case OMPC_private:
  7937. case OMPC_shared:
  7938. case OMPC_aligned:
  7939. case OMPC_copyin:
  7940. case OMPC_copyprivate:
  7941. case OMPC_ordered:
  7942. case OMPC_nowait:
  7943. case OMPC_untied:
  7944. case OMPC_mergeable:
  7945. case OMPC_threadprivate:
  7946. case OMPC_flush:
  7947. case OMPC_read:
  7948. case OMPC_write:
  7949. case OMPC_update:
  7950. case OMPC_capture:
  7951. case OMPC_seq_cst:
  7952. case OMPC_depend:
  7953. case OMPC_threads:
  7954. case OMPC_simd:
  7955. case OMPC_map:
  7956. case OMPC_priority:
  7957. case OMPC_grainsize:
  7958. case OMPC_nogroup:
  7959. case OMPC_num_tasks:
  7960. case OMPC_hint:
  7961. case OMPC_defaultmap:
  7962. case OMPC_unknown:
  7963. case OMPC_uniform:
  7964. case OMPC_to:
  7965. case OMPC_from:
  7966. case OMPC_use_device_ptr:
  7967. case OMPC_is_device_ptr:
  7968. case OMPC_unified_address:
  7969. llvm_unreachable("Unexpected OpenMP clause.");
  7970. }
  7971. return CaptureRegion;
  7972. }
  7973. OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
  7974. Expr *Condition, SourceLocation StartLoc,
  7975. SourceLocation LParenLoc,
  7976. SourceLocation NameModifierLoc,
  7977. SourceLocation ColonLoc,
  7978. SourceLocation EndLoc) {
  7979. Expr *ValExpr = Condition;
  7980. Stmt *HelperValStmt = nullptr;
  7981. OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
  7982. if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
  7983. !Condition->isInstantiationDependent() &&
  7984. !Condition->containsUnexpandedParameterPack()) {
  7985. ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
  7986. if (Val.isInvalid())
  7987. return nullptr;
  7988. ValExpr = Val.get();
  7989. OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
  7990. CaptureRegion =
  7991. getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
  7992. if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
  7993. ValExpr = MakeFullExpr(ValExpr).get();
  7994. llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
  7995. ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
  7996. HelperValStmt = buildPreInits(Context, Captures);
  7997. }
  7998. }
  7999. return new (Context)
  8000. OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
  8001. LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
  8002. }
  8003. OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
  8004. SourceLocation StartLoc,
  8005. SourceLocation LParenLoc,
  8006. SourceLocation EndLoc) {
  8007. Expr *ValExpr = Condition;
  8008. if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
  8009. !Condition->isInstantiationDependent() &&
  8010. !Condition->containsUnexpandedParameterPack()) {
  8011. ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
  8012. if (Val.isInvalid())
  8013. return nullptr;
  8014. ValExpr = MakeFullExpr(Val.get()).get();
  8015. }
  8016. return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
  8017. }
  8018. ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
  8019. Expr *Op) {
  8020. if (!Op)
  8021. return ExprError();
  8022. class IntConvertDiagnoser : public ICEConvertDiagnoser {
  8023. public:
  8024. IntConvertDiagnoser()
  8025. : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
  8026. SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
  8027. QualType T) override {
  8028. return S.Diag(Loc, diag::err_omp_not_integral) << T;
  8029. }
  8030. SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
  8031. QualType T) override {
  8032. return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
  8033. }
  8034. SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
  8035. QualType T,
  8036. QualType ConvTy) override {
  8037. return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
  8038. }
  8039. SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
  8040. QualType ConvTy) override {
  8041. return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
  8042. << ConvTy->isEnumeralType() << ConvTy;
  8043. }
  8044. SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
  8045. QualType T) override {
  8046. return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
  8047. }
  8048. SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
  8049. QualType ConvTy) override {
  8050. return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
  8051. << ConvTy->isEnumeralType() << ConvTy;
  8052. }
  8053. SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
  8054. QualType) override {
  8055. llvm_unreachable("conversion functions are permitted");
  8056. }
  8057. } ConvertDiagnoser;
  8058. return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
  8059. }
  8060. static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
  8061. OpenMPClauseKind CKind,
  8062. bool StrictlyPositive) {
  8063. if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
  8064. !ValExpr->isInstantiationDependent()) {
  8065. SourceLocation Loc = ValExpr->getExprLoc();
  8066. ExprResult Value =
  8067. SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
  8068. if (Value.isInvalid())
  8069. return false;
  8070. ValExpr = Value.get();
  8071. // The expression must evaluate to a non-negative integer value.
  8072. llvm::APSInt Result;
  8073. if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
  8074. Result.isSigned() &&
  8075. !((!StrictlyPositive && Result.isNonNegative()) ||
  8076. (StrictlyPositive && Result.isStrictlyPositive()))) {
  8077. SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
  8078. << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
  8079. << ValExpr->getSourceRange();
  8080. return false;
  8081. }
  8082. }
  8083. return true;
  8084. }
  8085. OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
  8086. SourceLocation StartLoc,
  8087. SourceLocation LParenLoc,
  8088. SourceLocation EndLoc) {
  8089. Expr *ValExpr = NumThreads;
  8090. Stmt *HelperValStmt = nullptr;
  8091. // OpenMP [2.5, Restrictions]
  8092. // The num_threads expression must evaluate to a positive integer value.
  8093. if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
  8094. /*StrictlyPositive=*/true))
  8095. return nullptr;
  8096. OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
  8097. OpenMPDirectiveKind CaptureRegion =
  8098. getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
  8099. if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
  8100. ValExpr = MakeFullExpr(ValExpr).get();
  8101. llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
  8102. ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
  8103. HelperValStmt = buildPreInits(Context, Captures);
  8104. }
  8105. return new (Context) OMPNumThreadsClause(
  8106. ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
  8107. }
  8108. ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
  8109. OpenMPClauseKind CKind,
  8110. bool StrictlyPositive) {
  8111. if (!E)
  8112. return ExprError();
  8113. if (E->isValueDependent() || E->isTypeDependent() ||
  8114. E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
  8115. return E;
  8116. llvm::APSInt Result;
  8117. ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
  8118. if (ICE.isInvalid())
  8119. return ExprError();
  8120. if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
  8121. (!StrictlyPositive && !Result.isNonNegative())) {
  8122. Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
  8123. << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
  8124. << E->getSourceRange();
  8125. return ExprError();
  8126. }
  8127. if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
  8128. Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
  8129. << E->getSourceRange();
  8130. return ExprError();
  8131. }
  8132. if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
  8133. DSAStack->setAssociatedLoops(Result.getExtValue());
  8134. else if (CKind == OMPC_ordered)
  8135. DSAStack->setAssociatedLoops(Result.getExtValue());
  8136. return ICE;
  8137. }
  8138. OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
  8139. SourceLocation LParenLoc,
  8140. SourceLocation EndLoc) {
  8141. // OpenMP [2.8.1, simd construct, Description]
  8142. // The parameter of the safelen clause must be a constant
  8143. // positive integer expression.
  8144. ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
  8145. if (Safelen.isInvalid())
  8146. return nullptr;
  8147. return new (Context)
  8148. OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
  8149. }
  8150. OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
  8151. SourceLocation LParenLoc,
  8152. SourceLocation EndLoc) {
  8153. // OpenMP [2.8.1, simd construct, Description]
  8154. // The parameter of the simdlen clause must be a constant
  8155. // positive integer expression.
  8156. ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
  8157. if (Simdlen.isInvalid())
  8158. return nullptr;
  8159. return new (Context)
  8160. OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
  8161. }
  8162. OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
  8163. SourceLocation StartLoc,
  8164. SourceLocation LParenLoc,
  8165. SourceLocation EndLoc) {
  8166. // OpenMP [2.7.1, loop construct, Description]
  8167. // OpenMP [2.8.1, simd construct, Description]
  8168. // OpenMP [2.9.6, distribute construct, Description]
  8169. // The parameter of the collapse clause must be a constant
  8170. // positive integer expression.
  8171. ExprResult NumForLoopsResult =
  8172. VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
  8173. if (NumForLoopsResult.isInvalid())
  8174. return nullptr;
  8175. return new (Context)
  8176. OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
  8177. }
  8178. OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
  8179. SourceLocation EndLoc,
  8180. SourceLocation LParenLoc,
  8181. Expr *NumForLoops) {
  8182. // OpenMP [2.7.1, loop construct, Description]
  8183. // OpenMP [2.8.1, simd construct, Description]
  8184. // OpenMP [2.9.6, distribute construct, Description]
  8185. // The parameter of the ordered clause must be a constant
  8186. // positive integer expression if any.
  8187. if (NumForLoops && LParenLoc.isValid()) {
  8188. ExprResult NumForLoopsResult =
  8189. VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
  8190. if (NumForLoopsResult.isInvalid())
  8191. return nullptr;
  8192. NumForLoops = NumForLoopsResult.get();
  8193. } else {
  8194. NumForLoops = nullptr;
  8195. }
  8196. auto *Clause = OMPOrderedClause::Create(
  8197. Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
  8198. StartLoc, LParenLoc, EndLoc);
  8199. DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
  8200. return Clause;
  8201. }
  8202. OMPClause *Sema::ActOnOpenMPSimpleClause(
  8203. OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
  8204. SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
  8205. OMPClause *Res = nullptr;
  8206. switch (Kind) {
  8207. case OMPC_default:
  8208. Res =
  8209. ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
  8210. ArgumentLoc, StartLoc, LParenLoc, EndLoc);
  8211. break;
  8212. case OMPC_proc_bind:
  8213. Res = ActOnOpenMPProcBindClause(
  8214. static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
  8215. LParenLoc, EndLoc);
  8216. break;
  8217. case OMPC_if:
  8218. case OMPC_final:
  8219. case OMPC_num_threads:
  8220. case OMPC_safelen:
  8221. case OMPC_simdlen:
  8222. case OMPC_collapse:
  8223. case OMPC_schedule:
  8224. case OMPC_private:
  8225. case OMPC_firstprivate:
  8226. case OMPC_lastprivate:
  8227. case OMPC_shared:
  8228. case OMPC_reduction:
  8229. case OMPC_task_reduction:
  8230. case OMPC_in_reduction:
  8231. case OMPC_linear:
  8232. case OMPC_aligned:
  8233. case OMPC_copyin:
  8234. case OMPC_copyprivate:
  8235. case OMPC_ordered:
  8236. case OMPC_nowait:
  8237. case OMPC_untied:
  8238. case OMPC_mergeable:
  8239. case OMPC_threadprivate:
  8240. case OMPC_flush:
  8241. case OMPC_read:
  8242. case OMPC_write:
  8243. case OMPC_update:
  8244. case OMPC_capture:
  8245. case OMPC_seq_cst:
  8246. case OMPC_depend:
  8247. case OMPC_device:
  8248. case OMPC_threads:
  8249. case OMPC_simd:
  8250. case OMPC_map:
  8251. case OMPC_num_teams:
  8252. case OMPC_thread_limit:
  8253. case OMPC_priority:
  8254. case OMPC_grainsize:
  8255. case OMPC_nogroup:
  8256. case OMPC_num_tasks:
  8257. case OMPC_hint:
  8258. case OMPC_dist_schedule:
  8259. case OMPC_defaultmap:
  8260. case OMPC_unknown:
  8261. case OMPC_uniform:
  8262. case OMPC_to:
  8263. case OMPC_from:
  8264. case OMPC_use_device_ptr:
  8265. case OMPC_is_device_ptr:
  8266. case OMPC_unified_address:
  8267. llvm_unreachable("Clause is not allowed.");
  8268. }
  8269. return Res;
  8270. }
  8271. static std::string
  8272. getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
  8273. ArrayRef<unsigned> Exclude = llvm::None) {
  8274. SmallString<256> Buffer;
  8275. llvm::raw_svector_ostream Out(Buffer);
  8276. unsigned Bound = Last >= 2 ? Last - 2 : 0;
  8277. unsigned Skipped = Exclude.size();
  8278. auto S = Exclude.begin(), E = Exclude.end();
  8279. for (unsigned I = First; I < Last; ++I) {
  8280. if (std::find(S, E, I) != E) {
  8281. --Skipped;
  8282. continue;
  8283. }
  8284. Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
  8285. if (I == Bound - Skipped)
  8286. Out << " or ";
  8287. else if (I != Bound + 1 - Skipped)
  8288. Out << ", ";
  8289. }
  8290. return Out.str();
  8291. }
  8292. OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
  8293. SourceLocation KindKwLoc,
  8294. SourceLocation StartLoc,
  8295. SourceLocation LParenLoc,
  8296. SourceLocation EndLoc) {
  8297. if (Kind == OMPC_DEFAULT_unknown) {
  8298. static_assert(OMPC_DEFAULT_unknown > 0,
  8299. "OMPC_DEFAULT_unknown not greater than 0");
  8300. Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
  8301. << getListOfPossibleValues(OMPC_default, /*First=*/0,
  8302. /*Last=*/OMPC_DEFAULT_unknown)
  8303. << getOpenMPClauseName(OMPC_default);
  8304. return nullptr;
  8305. }
  8306. switch (Kind) {
  8307. case OMPC_DEFAULT_none:
  8308. DSAStack->setDefaultDSANone(KindKwLoc);
  8309. break;
  8310. case OMPC_DEFAULT_shared:
  8311. DSAStack->setDefaultDSAShared(KindKwLoc);
  8312. break;
  8313. case OMPC_DEFAULT_unknown:
  8314. llvm_unreachable("Clause kind is not allowed.");
  8315. break;
  8316. }
  8317. return new (Context)
  8318. OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
  8319. }
  8320. OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
  8321. SourceLocation KindKwLoc,
  8322. SourceLocation StartLoc,
  8323. SourceLocation LParenLoc,
  8324. SourceLocation EndLoc) {
  8325. if (Kind == OMPC_PROC_BIND_unknown) {
  8326. Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
  8327. << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
  8328. /*Last=*/OMPC_PROC_BIND_unknown)
  8329. << getOpenMPClauseName(OMPC_proc_bind);
  8330. return nullptr;
  8331. }
  8332. return new (Context)
  8333. OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
  8334. }
  8335. OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
  8336. OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
  8337. SourceLocation StartLoc, SourceLocation LParenLoc,
  8338. ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
  8339. SourceLocation EndLoc) {
  8340. OMPClause *Res = nullptr;
  8341. switch (Kind) {
  8342. case OMPC_schedule:
  8343. enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
  8344. assert(Argument.size() == NumberOfElements &&
  8345. ArgumentLoc.size() == NumberOfElements);
  8346. Res = ActOnOpenMPScheduleClause(
  8347. static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
  8348. static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
  8349. static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
  8350. StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
  8351. ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
  8352. break;
  8353. case OMPC_if:
  8354. assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
  8355. Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
  8356. Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
  8357. DelimLoc, EndLoc);
  8358. break;
  8359. case OMPC_dist_schedule:
  8360. Res = ActOnOpenMPDistScheduleClause(
  8361. static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
  8362. StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
  8363. break;
  8364. case OMPC_defaultmap:
  8365. enum { Modifier, DefaultmapKind };
  8366. Res = ActOnOpenMPDefaultmapClause(
  8367. static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
  8368. static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
  8369. StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
  8370. EndLoc);
  8371. break;
  8372. case OMPC_final:
  8373. case OMPC_num_threads:
  8374. case OMPC_safelen:
  8375. case OMPC_simdlen:
  8376. case OMPC_collapse:
  8377. case OMPC_default:
  8378. case OMPC_proc_bind:
  8379. case OMPC_private:
  8380. case OMPC_firstprivate:
  8381. case OMPC_lastprivate:
  8382. case OMPC_shared:
  8383. case OMPC_reduction:
  8384. case OMPC_task_reduction:
  8385. case OMPC_in_reduction:
  8386. case OMPC_linear:
  8387. case OMPC_aligned:
  8388. case OMPC_copyin:
  8389. case OMPC_copyprivate:
  8390. case OMPC_ordered:
  8391. case OMPC_nowait:
  8392. case OMPC_untied:
  8393. case OMPC_mergeable:
  8394. case OMPC_threadprivate:
  8395. case OMPC_flush:
  8396. case OMPC_read:
  8397. case OMPC_write:
  8398. case OMPC_update:
  8399. case OMPC_capture:
  8400. case OMPC_seq_cst:
  8401. case OMPC_depend:
  8402. case OMPC_device:
  8403. case OMPC_threads:
  8404. case OMPC_simd:
  8405. case OMPC_map:
  8406. case OMPC_num_teams:
  8407. case OMPC_thread_limit:
  8408. case OMPC_priority:
  8409. case OMPC_grainsize:
  8410. case OMPC_nogroup:
  8411. case OMPC_num_tasks:
  8412. case OMPC_hint:
  8413. case OMPC_unknown:
  8414. case OMPC_uniform:
  8415. case OMPC_to:
  8416. case OMPC_from:
  8417. case OMPC_use_device_ptr:
  8418. case OMPC_is_device_ptr:
  8419. case OMPC_unified_address:
  8420. llvm_unreachable("Clause is not allowed.");
  8421. }
  8422. return Res;
  8423. }
  8424. static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
  8425. OpenMPScheduleClauseModifier M2,
  8426. SourceLocation M1Loc, SourceLocation M2Loc) {
  8427. if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
  8428. SmallVector<unsigned, 2> Excluded;
  8429. if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
  8430. Excluded.push_back(M2);
  8431. if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
  8432. Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
  8433. if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
  8434. Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
  8435. S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
  8436. << getListOfPossibleValues(OMPC_schedule,
  8437. /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
  8438. /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
  8439. Excluded)
  8440. << getOpenMPClauseName(OMPC_schedule);
  8441. return true;
  8442. }
  8443. return false;
  8444. }
  8445. OMPClause *Sema::ActOnOpenMPScheduleClause(
  8446. OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
  8447. OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
  8448. SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
  8449. SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
  8450. if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
  8451. checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
  8452. return nullptr;
  8453. // OpenMP, 2.7.1, Loop Construct, Restrictions
  8454. // Either the monotonic modifier or the nonmonotonic modifier can be specified
  8455. // but not both.
  8456. if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
  8457. (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
  8458. M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
  8459. (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
  8460. M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
  8461. Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
  8462. << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
  8463. << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
  8464. return nullptr;
  8465. }
  8466. if (Kind == OMPC_SCHEDULE_unknown) {
  8467. std::string Values;
  8468. if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
  8469. unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
  8470. Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
  8471. /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
  8472. Exclude);
  8473. } else {
  8474. Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
  8475. /*Last=*/OMPC_SCHEDULE_unknown);
  8476. }
  8477. Diag(KindLoc, diag::err_omp_unexpected_clause_value)
  8478. << Values << getOpenMPClauseName(OMPC_schedule);
  8479. return nullptr;
  8480. }
  8481. // OpenMP, 2.7.1, Loop Construct, Restrictions
  8482. // The nonmonotonic modifier can only be specified with schedule(dynamic) or
  8483. // schedule(guided).
  8484. if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
  8485. M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
  8486. Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
  8487. Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
  8488. diag::err_omp_schedule_nonmonotonic_static);
  8489. return nullptr;
  8490. }
  8491. Expr *ValExpr = ChunkSize;
  8492. Stmt *HelperValStmt = nullptr;
  8493. if (ChunkSize) {
  8494. if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
  8495. !ChunkSize->isInstantiationDependent() &&
  8496. !ChunkSize->containsUnexpandedParameterPack()) {
  8497. SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
  8498. ExprResult Val =
  8499. PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
  8500. if (Val.isInvalid())
  8501. return nullptr;
  8502. ValExpr = Val.get();
  8503. // OpenMP [2.7.1, Restrictions]
  8504. // chunk_size must be a loop invariant integer expression with a positive
  8505. // value.
  8506. llvm::APSInt Result;
  8507. if (ValExpr->isIntegerConstantExpr(Result, Context)) {
  8508. if (Result.isSigned() && !Result.isStrictlyPositive()) {
  8509. Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
  8510. << "schedule" << 1 << ChunkSize->getSourceRange();
  8511. return nullptr;
  8512. }
  8513. } else if (getOpenMPCaptureRegionForClause(
  8514. DSAStack->getCurrentDirective(), OMPC_schedule) !=
  8515. OMPD_unknown &&
  8516. !CurContext->isDependentContext()) {
  8517. ValExpr = MakeFullExpr(ValExpr).get();
  8518. llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
  8519. ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
  8520. HelperValStmt = buildPreInits(Context, Captures);
  8521. }
  8522. }
  8523. }
  8524. return new (Context)
  8525. OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
  8526. ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
  8527. }
  8528. OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
  8529. SourceLocation StartLoc,
  8530. SourceLocation EndLoc) {
  8531. OMPClause *Res = nullptr;
  8532. switch (Kind) {
  8533. case OMPC_ordered:
  8534. Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
  8535. break;
  8536. case OMPC_nowait:
  8537. Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
  8538. break;
  8539. case OMPC_untied:
  8540. Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
  8541. break;
  8542. case OMPC_mergeable:
  8543. Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
  8544. break;
  8545. case OMPC_read:
  8546. Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
  8547. break;
  8548. case OMPC_write:
  8549. Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
  8550. break;
  8551. case OMPC_update:
  8552. Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
  8553. break;
  8554. case OMPC_capture:
  8555. Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
  8556. break;
  8557. case OMPC_seq_cst:
  8558. Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
  8559. break;
  8560. case OMPC_threads:
  8561. Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
  8562. break;
  8563. case OMPC_simd:
  8564. Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
  8565. break;
  8566. case OMPC_nogroup:
  8567. Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
  8568. break;
  8569. case OMPC_unified_address:
  8570. Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
  8571. break;
  8572. case OMPC_if:
  8573. case OMPC_final:
  8574. case OMPC_num_threads:
  8575. case OMPC_safelen:
  8576. case OMPC_simdlen:
  8577. case OMPC_collapse:
  8578. case OMPC_schedule:
  8579. case OMPC_private:
  8580. case OMPC_firstprivate:
  8581. case OMPC_lastprivate:
  8582. case OMPC_shared:
  8583. case OMPC_reduction:
  8584. case OMPC_task_reduction:
  8585. case OMPC_in_reduction:
  8586. case OMPC_linear:
  8587. case OMPC_aligned:
  8588. case OMPC_copyin:
  8589. case OMPC_copyprivate:
  8590. case OMPC_default:
  8591. case OMPC_proc_bind:
  8592. case OMPC_threadprivate:
  8593. case OMPC_flush:
  8594. case OMPC_depend:
  8595. case OMPC_device:
  8596. case OMPC_map:
  8597. case OMPC_num_teams:
  8598. case OMPC_thread_limit:
  8599. case OMPC_priority:
  8600. case OMPC_grainsize:
  8601. case OMPC_num_tasks:
  8602. case OMPC_hint:
  8603. case OMPC_dist_schedule:
  8604. case OMPC_defaultmap:
  8605. case OMPC_unknown:
  8606. case OMPC_uniform:
  8607. case OMPC_to:
  8608. case OMPC_from:
  8609. case OMPC_use_device_ptr:
  8610. case OMPC_is_device_ptr:
  8611. llvm_unreachable("Clause is not allowed.");
  8612. }
  8613. return Res;
  8614. }
  8615. OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
  8616. SourceLocation EndLoc) {
  8617. DSAStack->setNowaitRegion();
  8618. return new (Context) OMPNowaitClause(StartLoc, EndLoc);
  8619. }
  8620. OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
  8621. SourceLocation EndLoc) {
  8622. return new (Context) OMPUntiedClause(StartLoc, EndLoc);
  8623. }
  8624. OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
  8625. SourceLocation EndLoc) {
  8626. return new (Context) OMPMergeableClause(StartLoc, EndLoc);
  8627. }
  8628. OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
  8629. SourceLocation EndLoc) {
  8630. return new (Context) OMPReadClause(StartLoc, EndLoc);
  8631. }
  8632. OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
  8633. SourceLocation EndLoc) {
  8634. return new (Context) OMPWriteClause(StartLoc, EndLoc);
  8635. }
  8636. OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
  8637. SourceLocation EndLoc) {
  8638. return new (Context) OMPUpdateClause(StartLoc, EndLoc);
  8639. }
  8640. OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
  8641. SourceLocation EndLoc) {
  8642. return new (Context) OMPCaptureClause(StartLoc, EndLoc);
  8643. }
  8644. OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
  8645. SourceLocation EndLoc) {
  8646. return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
  8647. }
  8648. OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
  8649. SourceLocation EndLoc) {
  8650. return new (Context) OMPThreadsClause(StartLoc, EndLoc);
  8651. }
  8652. OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
  8653. SourceLocation EndLoc) {
  8654. return new (Context) OMPSIMDClause(StartLoc, EndLoc);
  8655. }
  8656. OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
  8657. SourceLocation EndLoc) {
  8658. return new (Context) OMPNogroupClause(StartLoc, EndLoc);
  8659. }
  8660. OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
  8661. SourceLocation EndLoc) {
  8662. return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
  8663. }
  8664. OMPClause *Sema::ActOnOpenMPVarListClause(
  8665. OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
  8666. SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
  8667. SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
  8668. const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
  8669. OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
  8670. OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
  8671. SourceLocation DepLinMapLoc) {
  8672. OMPClause *Res = nullptr;
  8673. switch (Kind) {
  8674. case OMPC_private:
  8675. Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
  8676. break;
  8677. case OMPC_firstprivate:
  8678. Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
  8679. break;
  8680. case OMPC_lastprivate:
  8681. Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
  8682. break;
  8683. case OMPC_shared:
  8684. Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
  8685. break;
  8686. case OMPC_reduction:
  8687. Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
  8688. EndLoc, ReductionIdScopeSpec, ReductionId);
  8689. break;
  8690. case OMPC_task_reduction:
  8691. Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
  8692. EndLoc, ReductionIdScopeSpec,
  8693. ReductionId);
  8694. break;
  8695. case OMPC_in_reduction:
  8696. Res =
  8697. ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
  8698. EndLoc, ReductionIdScopeSpec, ReductionId);
  8699. break;
  8700. case OMPC_linear:
  8701. Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
  8702. LinKind, DepLinMapLoc, ColonLoc, EndLoc);
  8703. break;
  8704. case OMPC_aligned:
  8705. Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
  8706. ColonLoc, EndLoc);
  8707. break;
  8708. case OMPC_copyin:
  8709. Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
  8710. break;
  8711. case OMPC_copyprivate:
  8712. Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
  8713. break;
  8714. case OMPC_flush:
  8715. Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
  8716. break;
  8717. case OMPC_depend:
  8718. Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
  8719. StartLoc, LParenLoc, EndLoc);
  8720. break;
  8721. case OMPC_map:
  8722. Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
  8723. DepLinMapLoc, ColonLoc, VarList, StartLoc,
  8724. LParenLoc, EndLoc);
  8725. break;
  8726. case OMPC_to:
  8727. Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
  8728. break;
  8729. case OMPC_from:
  8730. Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
  8731. break;
  8732. case OMPC_use_device_ptr:
  8733. Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
  8734. break;
  8735. case OMPC_is_device_ptr:
  8736. Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
  8737. break;
  8738. case OMPC_if:
  8739. case OMPC_final:
  8740. case OMPC_num_threads:
  8741. case OMPC_safelen:
  8742. case OMPC_simdlen:
  8743. case OMPC_collapse:
  8744. case OMPC_default:
  8745. case OMPC_proc_bind:
  8746. case OMPC_schedule:
  8747. case OMPC_ordered:
  8748. case OMPC_nowait:
  8749. case OMPC_untied:
  8750. case OMPC_mergeable:
  8751. case OMPC_threadprivate:
  8752. case OMPC_read:
  8753. case OMPC_write:
  8754. case OMPC_update:
  8755. case OMPC_capture:
  8756. case OMPC_seq_cst:
  8757. case OMPC_device:
  8758. case OMPC_threads:
  8759. case OMPC_simd:
  8760. case OMPC_num_teams:
  8761. case OMPC_thread_limit:
  8762. case OMPC_priority:
  8763. case OMPC_grainsize:
  8764. case OMPC_nogroup:
  8765. case OMPC_num_tasks:
  8766. case OMPC_hint:
  8767. case OMPC_dist_schedule:
  8768. case OMPC_defaultmap:
  8769. case OMPC_unknown:
  8770. case OMPC_uniform:
  8771. case OMPC_unified_address:
  8772. llvm_unreachable("Clause is not allowed.");
  8773. }
  8774. return Res;
  8775. }
  8776. ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
  8777. ExprObjectKind OK, SourceLocation Loc) {
  8778. ExprResult Res = BuildDeclRefExpr(
  8779. Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
  8780. if (!Res.isUsable())
  8781. return ExprError();
  8782. if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
  8783. Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
  8784. if (!Res.isUsable())
  8785. return ExprError();
  8786. }
  8787. if (VK != VK_LValue && Res.get()->isGLValue()) {
  8788. Res = DefaultLvalueConversion(Res.get());
  8789. if (!Res.isUsable())
  8790. return ExprError();
  8791. }
  8792. return Res;
  8793. }
  8794. static std::pair<ValueDecl *, bool>
  8795. getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
  8796. SourceRange &ERange, bool AllowArraySection = false) {
  8797. if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
  8798. RefExpr->containsUnexpandedParameterPack())
  8799. return std::make_pair(nullptr, true);
  8800. // OpenMP [3.1, C/C++]
  8801. // A list item is a variable name.
  8802. // OpenMP [2.9.3.3, Restrictions, p.1]
  8803. // A variable that is part of another variable (as an array or
  8804. // structure element) cannot appear in a private clause.
  8805. RefExpr = RefExpr->IgnoreParens();
  8806. enum {
  8807. NoArrayExpr = -1,
  8808. ArraySubscript = 0,
  8809. OMPArraySection = 1
  8810. } IsArrayExpr = NoArrayExpr;
  8811. if (AllowArraySection) {
  8812. if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
  8813. Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
  8814. while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
  8815. Base = TempASE->getBase()->IgnoreParenImpCasts();
  8816. RefExpr = Base;
  8817. IsArrayExpr = ArraySubscript;
  8818. } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
  8819. Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
  8820. while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
  8821. Base = TempOASE->getBase()->IgnoreParenImpCasts();
  8822. while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
  8823. Base = TempASE->getBase()->IgnoreParenImpCasts();
  8824. RefExpr = Base;
  8825. IsArrayExpr = OMPArraySection;
  8826. }
  8827. }
  8828. ELoc = RefExpr->getExprLoc();
  8829. ERange = RefExpr->getSourceRange();
  8830. RefExpr = RefExpr->IgnoreParenImpCasts();
  8831. auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
  8832. auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
  8833. if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
  8834. (S.getCurrentThisType().isNull() || !ME ||
  8835. !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
  8836. !isa<FieldDecl>(ME->getMemberDecl()))) {
  8837. if (IsArrayExpr != NoArrayExpr) {
  8838. S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
  8839. << ERange;
  8840. } else {
  8841. S.Diag(ELoc,
  8842. AllowArraySection
  8843. ? diag::err_omp_expected_var_name_member_expr_or_array_item
  8844. : diag::err_omp_expected_var_name_member_expr)
  8845. << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
  8846. }
  8847. return std::make_pair(nullptr, false);
  8848. }
  8849. return std::make_pair(
  8850. getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
  8851. }
  8852. OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
  8853. SourceLocation StartLoc,
  8854. SourceLocation LParenLoc,
  8855. SourceLocation EndLoc) {
  8856. SmallVector<Expr *, 8> Vars;
  8857. SmallVector<Expr *, 8> PrivateCopies;
  8858. for (Expr *RefExpr : VarList) {
  8859. assert(RefExpr && "NULL expr in OpenMP private clause.");
  8860. SourceLocation ELoc;
  8861. SourceRange ERange;
  8862. Expr *SimpleRefExpr = RefExpr;
  8863. auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
  8864. if (Res.second) {
  8865. // It will be analyzed later.
  8866. Vars.push_back(RefExpr);
  8867. PrivateCopies.push_back(nullptr);
  8868. }
  8869. ValueDecl *D = Res.first;
  8870. if (!D)
  8871. continue;
  8872. QualType Type = D->getType();
  8873. auto *VD = dyn_cast<VarDecl>(D);
  8874. // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
  8875. // A variable that appears in a private clause must not have an incomplete
  8876. // type or a reference type.
  8877. if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
  8878. continue;
  8879. Type = Type.getNonReferenceType();
  8880. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  8881. // in a Construct]
  8882. // Variables with the predetermined data-sharing attributes may not be
  8883. // listed in data-sharing attributes clauses, except for the cases
  8884. // listed below. For these exceptions only, listing a predetermined
  8885. // variable in a data-sharing attribute clause is allowed and overrides
  8886. // the variable's predetermined data-sharing attributes.
  8887. DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
  8888. if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
  8889. Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
  8890. << getOpenMPClauseName(OMPC_private);
  8891. reportOriginalDsa(*this, DSAStack, D, DVar);
  8892. continue;
  8893. }
  8894. OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
  8895. // Variably modified types are not supported for tasks.
  8896. if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
  8897. isOpenMPTaskingDirective(CurrDir)) {
  8898. Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
  8899. << getOpenMPClauseName(OMPC_private) << Type
  8900. << getOpenMPDirectiveName(CurrDir);
  8901. bool IsDecl =
  8902. !VD ||
  8903. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  8904. Diag(D->getLocation(),
  8905. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  8906. << D;
  8907. continue;
  8908. }
  8909. // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
  8910. // A list item cannot appear in both a map clause and a data-sharing
  8911. // attribute clause on the same construct
  8912. if (isOpenMPTargetExecutionDirective(CurrDir)) {
  8913. OpenMPClauseKind ConflictKind;
  8914. if (DSAStack->checkMappableExprComponentListsForDecl(
  8915. VD, /*CurrentRegionOnly=*/true,
  8916. [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
  8917. OpenMPClauseKind WhereFoundClauseKind) -> bool {
  8918. ConflictKind = WhereFoundClauseKind;
  8919. return true;
  8920. })) {
  8921. Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
  8922. << getOpenMPClauseName(OMPC_private)
  8923. << getOpenMPClauseName(ConflictKind)
  8924. << getOpenMPDirectiveName(CurrDir);
  8925. reportOriginalDsa(*this, DSAStack, D, DVar);
  8926. continue;
  8927. }
  8928. }
  8929. // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
  8930. // A variable of class type (or array thereof) that appears in a private
  8931. // clause requires an accessible, unambiguous default constructor for the
  8932. // class type.
  8933. // Generate helper private variable and initialize it with the default
  8934. // value. The address of the original variable is replaced by the address of
  8935. // the new private variable in CodeGen. This new variable is not added to
  8936. // IdResolver, so the code in the OpenMP region uses original variable for
  8937. // proper diagnostics.
  8938. Type = Type.getUnqualifiedType();
  8939. VarDecl *VDPrivate =
  8940. buildVarDecl(*this, ELoc, Type, D->getName(),
  8941. D->hasAttrs() ? &D->getAttrs() : nullptr,
  8942. VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
  8943. ActOnUninitializedDecl(VDPrivate);
  8944. if (VDPrivate->isInvalidDecl())
  8945. continue;
  8946. DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
  8947. *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
  8948. DeclRefExpr *Ref = nullptr;
  8949. if (!VD && !CurContext->isDependentContext())
  8950. Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
  8951. DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
  8952. Vars.push_back((VD || CurContext->isDependentContext())
  8953. ? RefExpr->IgnoreParens()
  8954. : Ref);
  8955. PrivateCopies.push_back(VDPrivateRefExpr);
  8956. }
  8957. if (Vars.empty())
  8958. return nullptr;
  8959. return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
  8960. PrivateCopies);
  8961. }
  8962. namespace {
  8963. class DiagsUninitializedSeveretyRAII {
  8964. private:
  8965. DiagnosticsEngine &Diags;
  8966. SourceLocation SavedLoc;
  8967. bool IsIgnored = false;
  8968. public:
  8969. DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
  8970. bool IsIgnored)
  8971. : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
  8972. if (!IsIgnored) {
  8973. Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
  8974. /*Map*/ diag::Severity::Ignored, Loc);
  8975. }
  8976. }
  8977. ~DiagsUninitializedSeveretyRAII() {
  8978. if (!IsIgnored)
  8979. Diags.popMappings(SavedLoc);
  8980. }
  8981. };
  8982. }
  8983. OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
  8984. SourceLocation StartLoc,
  8985. SourceLocation LParenLoc,
  8986. SourceLocation EndLoc) {
  8987. SmallVector<Expr *, 8> Vars;
  8988. SmallVector<Expr *, 8> PrivateCopies;
  8989. SmallVector<Expr *, 8> Inits;
  8990. SmallVector<Decl *, 4> ExprCaptures;
  8991. bool IsImplicitClause =
  8992. StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
  8993. SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
  8994. for (Expr *RefExpr : VarList) {
  8995. assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
  8996. SourceLocation ELoc;
  8997. SourceRange ERange;
  8998. Expr *SimpleRefExpr = RefExpr;
  8999. auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
  9000. if (Res.second) {
  9001. // It will be analyzed later.
  9002. Vars.push_back(RefExpr);
  9003. PrivateCopies.push_back(nullptr);
  9004. Inits.push_back(nullptr);
  9005. }
  9006. ValueDecl *D = Res.first;
  9007. if (!D)
  9008. continue;
  9009. ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
  9010. QualType Type = D->getType();
  9011. auto *VD = dyn_cast<VarDecl>(D);
  9012. // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
  9013. // A variable that appears in a private clause must not have an incomplete
  9014. // type or a reference type.
  9015. if (RequireCompleteType(ELoc, Type,
  9016. diag::err_omp_firstprivate_incomplete_type))
  9017. continue;
  9018. Type = Type.getNonReferenceType();
  9019. // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
  9020. // A variable of class type (or array thereof) that appears in a private
  9021. // clause requires an accessible, unambiguous copy constructor for the
  9022. // class type.
  9023. QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
  9024. // If an implicit firstprivate variable found it was checked already.
  9025. DSAStackTy::DSAVarData TopDVar;
  9026. if (!IsImplicitClause) {
  9027. DSAStackTy::DSAVarData DVar =
  9028. DSAStack->getTopDSA(D, /*FromParent=*/false);
  9029. TopDVar = DVar;
  9030. OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
  9031. bool IsConstant = ElemType.isConstant(Context);
  9032. // OpenMP [2.4.13, Data-sharing Attribute Clauses]
  9033. // A list item that specifies a given variable may not appear in more
  9034. // than one clause on the same directive, except that a variable may be
  9035. // specified in both firstprivate and lastprivate clauses.
  9036. // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
  9037. // A list item may appear in a firstprivate or lastprivate clause but not
  9038. // both.
  9039. if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
  9040. (isOpenMPDistributeDirective(CurrDir) ||
  9041. DVar.CKind != OMPC_lastprivate) &&
  9042. DVar.RefExpr) {
  9043. Diag(ELoc, diag::err_omp_wrong_dsa)
  9044. << getOpenMPClauseName(DVar.CKind)
  9045. << getOpenMPClauseName(OMPC_firstprivate);
  9046. reportOriginalDsa(*this, DSAStack, D, DVar);
  9047. continue;
  9048. }
  9049. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  9050. // in a Construct]
  9051. // Variables with the predetermined data-sharing attributes may not be
  9052. // listed in data-sharing attributes clauses, except for the cases
  9053. // listed below. For these exceptions only, listing a predetermined
  9054. // variable in a data-sharing attribute clause is allowed and overrides
  9055. // the variable's predetermined data-sharing attributes.
  9056. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  9057. // in a Construct, C/C++, p.2]
  9058. // Variables with const-qualified type having no mutable member may be
  9059. // listed in a firstprivate clause, even if they are static data members.
  9060. if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
  9061. DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
  9062. Diag(ELoc, diag::err_omp_wrong_dsa)
  9063. << getOpenMPClauseName(DVar.CKind)
  9064. << getOpenMPClauseName(OMPC_firstprivate);
  9065. reportOriginalDsa(*this, DSAStack, D, DVar);
  9066. continue;
  9067. }
  9068. // OpenMP [2.9.3.4, Restrictions, p.2]
  9069. // A list item that is private within a parallel region must not appear
  9070. // in a firstprivate clause on a worksharing construct if any of the
  9071. // worksharing regions arising from the worksharing construct ever bind
  9072. // to any of the parallel regions arising from the parallel construct.
  9073. // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
  9074. // A list item that is private within a teams region must not appear in a
  9075. // firstprivate clause on a distribute construct if any of the distribute
  9076. // regions arising from the distribute construct ever bind to any of the
  9077. // teams regions arising from the teams construct.
  9078. // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
  9079. // A list item that appears in a reduction clause of a teams construct
  9080. // must not appear in a firstprivate clause on a distribute construct if
  9081. // any of the distribute regions arising from the distribute construct
  9082. // ever bind to any of the teams regions arising from the teams construct.
  9083. if ((isOpenMPWorksharingDirective(CurrDir) ||
  9084. isOpenMPDistributeDirective(CurrDir)) &&
  9085. !isOpenMPParallelDirective(CurrDir) &&
  9086. !isOpenMPTeamsDirective(CurrDir)) {
  9087. DVar = DSAStack->getImplicitDSA(D, true);
  9088. if (DVar.CKind != OMPC_shared &&
  9089. (isOpenMPParallelDirective(DVar.DKind) ||
  9090. isOpenMPTeamsDirective(DVar.DKind) ||
  9091. DVar.DKind == OMPD_unknown)) {
  9092. Diag(ELoc, diag::err_omp_required_access)
  9093. << getOpenMPClauseName(OMPC_firstprivate)
  9094. << getOpenMPClauseName(OMPC_shared);
  9095. reportOriginalDsa(*this, DSAStack, D, DVar);
  9096. continue;
  9097. }
  9098. }
  9099. // OpenMP [2.9.3.4, Restrictions, p.3]
  9100. // A list item that appears in a reduction clause of a parallel construct
  9101. // must not appear in a firstprivate clause on a worksharing or task
  9102. // construct if any of the worksharing or task regions arising from the
  9103. // worksharing or task construct ever bind to any of the parallel regions
  9104. // arising from the parallel construct.
  9105. // OpenMP [2.9.3.4, Restrictions, p.4]
  9106. // A list item that appears in a reduction clause in worksharing
  9107. // construct must not appear in a firstprivate clause in a task construct
  9108. // encountered during execution of any of the worksharing regions arising
  9109. // from the worksharing construct.
  9110. if (isOpenMPTaskingDirective(CurrDir)) {
  9111. DVar = DSAStack->hasInnermostDSA(
  9112. D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
  9113. [](OpenMPDirectiveKind K) {
  9114. return isOpenMPParallelDirective(K) ||
  9115. isOpenMPWorksharingDirective(K) ||
  9116. isOpenMPTeamsDirective(K);
  9117. },
  9118. /*FromParent=*/true);
  9119. if (DVar.CKind == OMPC_reduction &&
  9120. (isOpenMPParallelDirective(DVar.DKind) ||
  9121. isOpenMPWorksharingDirective(DVar.DKind) ||
  9122. isOpenMPTeamsDirective(DVar.DKind))) {
  9123. Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
  9124. << getOpenMPDirectiveName(DVar.DKind);
  9125. reportOriginalDsa(*this, DSAStack, D, DVar);
  9126. continue;
  9127. }
  9128. }
  9129. // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
  9130. // A list item cannot appear in both a map clause and a data-sharing
  9131. // attribute clause on the same construct
  9132. if (isOpenMPTargetExecutionDirective(CurrDir)) {
  9133. OpenMPClauseKind ConflictKind;
  9134. if (DSAStack->checkMappableExprComponentListsForDecl(
  9135. VD, /*CurrentRegionOnly=*/true,
  9136. [&ConflictKind](
  9137. OMPClauseMappableExprCommon::MappableExprComponentListRef,
  9138. OpenMPClauseKind WhereFoundClauseKind) {
  9139. ConflictKind = WhereFoundClauseKind;
  9140. return true;
  9141. })) {
  9142. Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
  9143. << getOpenMPClauseName(OMPC_firstprivate)
  9144. << getOpenMPClauseName(ConflictKind)
  9145. << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
  9146. reportOriginalDsa(*this, DSAStack, D, DVar);
  9147. continue;
  9148. }
  9149. }
  9150. }
  9151. // Variably modified types are not supported for tasks.
  9152. if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
  9153. isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
  9154. Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
  9155. << getOpenMPClauseName(OMPC_firstprivate) << Type
  9156. << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
  9157. bool IsDecl =
  9158. !VD ||
  9159. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  9160. Diag(D->getLocation(),
  9161. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  9162. << D;
  9163. continue;
  9164. }
  9165. Type = Type.getUnqualifiedType();
  9166. VarDecl *VDPrivate =
  9167. buildVarDecl(*this, ELoc, Type, D->getName(),
  9168. D->hasAttrs() ? &D->getAttrs() : nullptr,
  9169. VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
  9170. // Generate helper private variable and initialize it with the value of the
  9171. // original variable. The address of the original variable is replaced by
  9172. // the address of the new private variable in the CodeGen. This new variable
  9173. // is not added to IdResolver, so the code in the OpenMP region uses
  9174. // original variable for proper diagnostics and variable capturing.
  9175. Expr *VDInitRefExpr = nullptr;
  9176. // For arrays generate initializer for single element and replace it by the
  9177. // original array element in CodeGen.
  9178. if (Type->isArrayType()) {
  9179. VarDecl *VDInit =
  9180. buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
  9181. VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
  9182. Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
  9183. ElemType = ElemType.getUnqualifiedType();
  9184. VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
  9185. ".firstprivate.temp");
  9186. InitializedEntity Entity =
  9187. InitializedEntity::InitializeVariable(VDInitTemp);
  9188. InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
  9189. InitializationSequence InitSeq(*this, Entity, Kind, Init);
  9190. ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
  9191. if (Result.isInvalid())
  9192. VDPrivate->setInvalidDecl();
  9193. else
  9194. VDPrivate->setInit(Result.getAs<Expr>());
  9195. // Remove temp variable declaration.
  9196. Context.Deallocate(VDInitTemp);
  9197. } else {
  9198. VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
  9199. ".firstprivate.temp");
  9200. VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
  9201. RefExpr->getExprLoc());
  9202. AddInitializerToDecl(VDPrivate,
  9203. DefaultLvalueConversion(VDInitRefExpr).get(),
  9204. /*DirectInit=*/false);
  9205. }
  9206. if (VDPrivate->isInvalidDecl()) {
  9207. if (IsImplicitClause) {
  9208. Diag(RefExpr->getExprLoc(),
  9209. diag::note_omp_task_predetermined_firstprivate_here);
  9210. }
  9211. continue;
  9212. }
  9213. CurContext->addDecl(VDPrivate);
  9214. DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
  9215. *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
  9216. RefExpr->getExprLoc());
  9217. DeclRefExpr *Ref = nullptr;
  9218. if (!VD && !CurContext->isDependentContext()) {
  9219. if (TopDVar.CKind == OMPC_lastprivate) {
  9220. Ref = TopDVar.PrivateCopy;
  9221. } else {
  9222. Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
  9223. if (!isOpenMPCapturedDecl(D))
  9224. ExprCaptures.push_back(Ref->getDecl());
  9225. }
  9226. }
  9227. DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
  9228. Vars.push_back((VD || CurContext->isDependentContext())
  9229. ? RefExpr->IgnoreParens()
  9230. : Ref);
  9231. PrivateCopies.push_back(VDPrivateRefExpr);
  9232. Inits.push_back(VDInitRefExpr);
  9233. }
  9234. if (Vars.empty())
  9235. return nullptr;
  9236. return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
  9237. Vars, PrivateCopies, Inits,
  9238. buildPreInits(Context, ExprCaptures));
  9239. }
  9240. OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
  9241. SourceLocation StartLoc,
  9242. SourceLocation LParenLoc,
  9243. SourceLocation EndLoc) {
  9244. SmallVector<Expr *, 8> Vars;
  9245. SmallVector<Expr *, 8> SrcExprs;
  9246. SmallVector<Expr *, 8> DstExprs;
  9247. SmallVector<Expr *, 8> AssignmentOps;
  9248. SmallVector<Decl *, 4> ExprCaptures;
  9249. SmallVector<Expr *, 4> ExprPostUpdates;
  9250. for (Expr *RefExpr : VarList) {
  9251. assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
  9252. SourceLocation ELoc;
  9253. SourceRange ERange;
  9254. Expr *SimpleRefExpr = RefExpr;
  9255. auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
  9256. if (Res.second) {
  9257. // It will be analyzed later.
  9258. Vars.push_back(RefExpr);
  9259. SrcExprs.push_back(nullptr);
  9260. DstExprs.push_back(nullptr);
  9261. AssignmentOps.push_back(nullptr);
  9262. }
  9263. ValueDecl *D = Res.first;
  9264. if (!D)
  9265. continue;
  9266. QualType Type = D->getType();
  9267. auto *VD = dyn_cast<VarDecl>(D);
  9268. // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
  9269. // A variable that appears in a lastprivate clause must not have an
  9270. // incomplete type or a reference type.
  9271. if (RequireCompleteType(ELoc, Type,
  9272. diag::err_omp_lastprivate_incomplete_type))
  9273. continue;
  9274. Type = Type.getNonReferenceType();
  9275. OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
  9276. // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
  9277. // in a Construct]
  9278. // Variables with the predetermined data-sharing attributes may not be
  9279. // listed in data-sharing attributes clauses, except for the cases
  9280. // listed below.
  9281. // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
  9282. // A list item may appear in a firstprivate or lastprivate clause but not
  9283. // both.
  9284. DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
  9285. if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
  9286. (isOpenMPDistributeDirective(CurrDir) ||
  9287. DVar.CKind != OMPC_firstprivate) &&
  9288. (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
  9289. Diag(ELoc, diag::err_omp_wrong_dsa)
  9290. << getOpenMPClauseName(DVar.CKind)
  9291. << getOpenMPClauseName(OMPC_lastprivate);
  9292. reportOriginalDsa(*this, DSAStack, D, DVar);
  9293. continue;
  9294. }
  9295. // OpenMP [2.14.3.5, Restrictions, p.2]
  9296. // A list item that is private within a parallel region, or that appears in
  9297. // the reduction clause of a parallel construct, must not appear in a
  9298. // lastprivate clause on a worksharing construct if any of the corresponding
  9299. // worksharing regions ever binds to any of the corresponding parallel
  9300. // regions.
  9301. DSAStackTy::DSAVarData TopDVar = DVar;
  9302. if (isOpenMPWorksharingDirective(CurrDir) &&
  9303. !isOpenMPParallelDirective(CurrDir) &&
  9304. !isOpenMPTeamsDirective(CurrDir)) {
  9305. DVar = DSAStack->getImplicitDSA(D, true);
  9306. if (DVar.CKind != OMPC_shared) {
  9307. Diag(ELoc, diag::err_omp_required_access)
  9308. << getOpenMPClauseName(OMPC_lastprivate)
  9309. << getOpenMPClauseName(OMPC_shared);
  9310. reportOriginalDsa(*this, DSAStack, D, DVar);
  9311. continue;
  9312. }
  9313. }
  9314. // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
  9315. // A variable of class type (or array thereof) that appears in a
  9316. // lastprivate clause requires an accessible, unambiguous default
  9317. // constructor for the class type, unless the list item is also specified
  9318. // in a firstprivate clause.
  9319. // A variable of class type (or array thereof) that appears in a
  9320. // lastprivate clause requires an accessible, unambiguous copy assignment
  9321. // operator for the class type.
  9322. Type = Context.getBaseElementType(Type).getNonReferenceType();
  9323. VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
  9324. Type.getUnqualifiedType(), ".lastprivate.src",
  9325. D->hasAttrs() ? &D->getAttrs() : nullptr);
  9326. DeclRefExpr *PseudoSrcExpr =
  9327. buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
  9328. VarDecl *DstVD =
  9329. buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
  9330. D->hasAttrs() ? &D->getAttrs() : nullptr);
  9331. DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
  9332. // For arrays generate assignment operation for single element and replace
  9333. // it by the original array element in CodeGen.
  9334. ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
  9335. PseudoDstExpr, PseudoSrcExpr);
  9336. if (AssignmentOp.isInvalid())
  9337. continue;
  9338. AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
  9339. /*DiscardedValue=*/true);
  9340. if (AssignmentOp.isInvalid())
  9341. continue;
  9342. DeclRefExpr *Ref = nullptr;
  9343. if (!VD && !CurContext->isDependentContext()) {
  9344. if (TopDVar.CKind == OMPC_firstprivate) {
  9345. Ref = TopDVar.PrivateCopy;
  9346. } else {
  9347. Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
  9348. if (!isOpenMPCapturedDecl(D))
  9349. ExprCaptures.push_back(Ref->getDecl());
  9350. }
  9351. if (TopDVar.CKind == OMPC_firstprivate ||
  9352. (!isOpenMPCapturedDecl(D) &&
  9353. Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
  9354. ExprResult RefRes = DefaultLvalueConversion(Ref);
  9355. if (!RefRes.isUsable())
  9356. continue;
  9357. ExprResult PostUpdateRes =
  9358. BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
  9359. RefRes.get());
  9360. if (!PostUpdateRes.isUsable())
  9361. continue;
  9362. ExprPostUpdates.push_back(
  9363. IgnoredValueConversions(PostUpdateRes.get()).get());
  9364. }
  9365. }
  9366. DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
  9367. Vars.push_back((VD || CurContext->isDependentContext())
  9368. ? RefExpr->IgnoreParens()
  9369. : Ref);
  9370. SrcExprs.push_back(PseudoSrcExpr);
  9371. DstExprs.push_back(PseudoDstExpr);
  9372. AssignmentOps.push_back(AssignmentOp.get());
  9373. }
  9374. if (Vars.empty())
  9375. return nullptr;
  9376. return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
  9377. Vars, SrcExprs, DstExprs, AssignmentOps,
  9378. buildPreInits(Context, ExprCaptures),
  9379. buildPostUpdate(*this, ExprPostUpdates));
  9380. }
  9381. OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
  9382. SourceLocation StartLoc,
  9383. SourceLocation LParenLoc,
  9384. SourceLocation EndLoc) {
  9385. SmallVector<Expr *, 8> Vars;
  9386. for (Expr *RefExpr : VarList) {
  9387. assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
  9388. SourceLocation ELoc;
  9389. SourceRange ERange;
  9390. Expr *SimpleRefExpr = RefExpr;
  9391. auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
  9392. if (Res.second) {
  9393. // It will be analyzed later.
  9394. Vars.push_back(RefExpr);
  9395. }
  9396. ValueDecl *D = Res.first;
  9397. if (!D)
  9398. continue;
  9399. auto *VD = dyn_cast<VarDecl>(D);
  9400. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  9401. // in a Construct]
  9402. // Variables with the predetermined data-sharing attributes may not be
  9403. // listed in data-sharing attributes clauses, except for the cases
  9404. // listed below. For these exceptions only, listing a predetermined
  9405. // variable in a data-sharing attribute clause is allowed and overrides
  9406. // the variable's predetermined data-sharing attributes.
  9407. DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
  9408. if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
  9409. DVar.RefExpr) {
  9410. Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
  9411. << getOpenMPClauseName(OMPC_shared);
  9412. reportOriginalDsa(*this, DSAStack, D, DVar);
  9413. continue;
  9414. }
  9415. DeclRefExpr *Ref = nullptr;
  9416. if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
  9417. Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
  9418. DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
  9419. Vars.push_back((VD || !Ref || CurContext->isDependentContext())
  9420. ? RefExpr->IgnoreParens()
  9421. : Ref);
  9422. }
  9423. if (Vars.empty())
  9424. return nullptr;
  9425. return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
  9426. }
  9427. namespace {
  9428. class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
  9429. DSAStackTy *Stack;
  9430. public:
  9431. bool VisitDeclRefExpr(DeclRefExpr *E) {
  9432. if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
  9433. DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
  9434. if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
  9435. return false;
  9436. if (DVar.CKind != OMPC_unknown)
  9437. return true;
  9438. DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
  9439. VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
  9440. /*FromParent=*/true);
  9441. return DVarPrivate.CKind != OMPC_unknown;
  9442. }
  9443. return false;
  9444. }
  9445. bool VisitStmt(Stmt *S) {
  9446. for (Stmt *Child : S->children()) {
  9447. if (Child && Visit(Child))
  9448. return true;
  9449. }
  9450. return false;
  9451. }
  9452. explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
  9453. };
  9454. } // namespace
  9455. namespace {
  9456. // Transform MemberExpression for specified FieldDecl of current class to
  9457. // DeclRefExpr to specified OMPCapturedExprDecl.
  9458. class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
  9459. typedef TreeTransform<TransformExprToCaptures> BaseTransform;
  9460. ValueDecl *Field = nullptr;
  9461. DeclRefExpr *CapturedExpr = nullptr;
  9462. public:
  9463. TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
  9464. : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
  9465. ExprResult TransformMemberExpr(MemberExpr *E) {
  9466. if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
  9467. E->getMemberDecl() == Field) {
  9468. CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
  9469. return CapturedExpr;
  9470. }
  9471. return BaseTransform::TransformMemberExpr(E);
  9472. }
  9473. DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
  9474. };
  9475. } // namespace
  9476. template <typename T, typename U>
  9477. static T filterLookupForUDR(SmallVectorImpl<U> &Lookups,
  9478. const llvm::function_ref<T(ValueDecl *)> Gen) {
  9479. for (U &Set : Lookups) {
  9480. for (auto *D : Set) {
  9481. if (T Res = Gen(cast<ValueDecl>(D)))
  9482. return Res;
  9483. }
  9484. }
  9485. return T();
  9486. }
  9487. static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
  9488. assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
  9489. for (auto RD : D->redecls()) {
  9490. // Don't bother with extra checks if we already know this one isn't visible.
  9491. if (RD == D)
  9492. continue;
  9493. auto ND = cast<NamedDecl>(RD);
  9494. if (LookupResult::isVisible(SemaRef, ND))
  9495. return ND;
  9496. }
  9497. return nullptr;
  9498. }
  9499. static void
  9500. argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &ReductionId,
  9501. SourceLocation Loc, QualType Ty,
  9502. SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
  9503. // Find all of the associated namespaces and classes based on the
  9504. // arguments we have.
  9505. Sema::AssociatedNamespaceSet AssociatedNamespaces;
  9506. Sema::AssociatedClassSet AssociatedClasses;
  9507. OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
  9508. SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
  9509. AssociatedClasses);
  9510. // C++ [basic.lookup.argdep]p3:
  9511. // Let X be the lookup set produced by unqualified lookup (3.4.1)
  9512. // and let Y be the lookup set produced by argument dependent
  9513. // lookup (defined as follows). If X contains [...] then Y is
  9514. // empty. Otherwise Y is the set of declarations found in the
  9515. // namespaces associated with the argument types as described
  9516. // below. The set of declarations found by the lookup of the name
  9517. // is the union of X and Y.
  9518. //
  9519. // Here, we compute Y and add its members to the overloaded
  9520. // candidate set.
  9521. for (auto *NS : AssociatedNamespaces) {
  9522. // When considering an associated namespace, the lookup is the
  9523. // same as the lookup performed when the associated namespace is
  9524. // used as a qualifier (3.4.3.2) except that:
  9525. //
  9526. // -- Any using-directives in the associated namespace are
  9527. // ignored.
  9528. //
  9529. // -- Any namespace-scope friend functions declared in
  9530. // associated classes are visible within their respective
  9531. // namespaces even if they are not visible during an ordinary
  9532. // lookup (11.4).
  9533. DeclContext::lookup_result R = NS->lookup(ReductionId.getName());
  9534. for (auto *D : R) {
  9535. auto *Underlying = D;
  9536. if (auto *USD = dyn_cast<UsingShadowDecl>(D))
  9537. Underlying = USD->getTargetDecl();
  9538. if (!isa<OMPDeclareReductionDecl>(Underlying))
  9539. continue;
  9540. if (!SemaRef.isVisible(D)) {
  9541. D = findAcceptableDecl(SemaRef, D);
  9542. if (!D)
  9543. continue;
  9544. if (auto *USD = dyn_cast<UsingShadowDecl>(D))
  9545. Underlying = USD->getTargetDecl();
  9546. }
  9547. Lookups.emplace_back();
  9548. Lookups.back().addDecl(Underlying);
  9549. }
  9550. }
  9551. }
  9552. static ExprResult
  9553. buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
  9554. Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
  9555. const DeclarationNameInfo &ReductionId, QualType Ty,
  9556. CXXCastPath &BasePath, Expr *UnresolvedReduction) {
  9557. if (ReductionIdScopeSpec.isInvalid())
  9558. return ExprError();
  9559. SmallVector<UnresolvedSet<8>, 4> Lookups;
  9560. if (S) {
  9561. LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
  9562. Lookup.suppressDiagnostics();
  9563. while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
  9564. NamedDecl *D = Lookup.getRepresentativeDecl();
  9565. do {
  9566. S = S->getParent();
  9567. } while (S && !S->isDeclScope(D));
  9568. if (S)
  9569. S = S->getParent();
  9570. Lookups.emplace_back();
  9571. Lookups.back().append(Lookup.begin(), Lookup.end());
  9572. Lookup.clear();
  9573. }
  9574. } else if (auto *ULE =
  9575. cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
  9576. Lookups.push_back(UnresolvedSet<8>());
  9577. Decl *PrevD = nullptr;
  9578. for (NamedDecl *D : ULE->decls()) {
  9579. if (D == PrevD)
  9580. Lookups.push_back(UnresolvedSet<8>());
  9581. else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
  9582. Lookups.back().addDecl(DRD);
  9583. PrevD = D;
  9584. }
  9585. }
  9586. if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
  9587. Ty->isInstantiationDependentType() ||
  9588. Ty->containsUnexpandedParameterPack() ||
  9589. filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) {
  9590. return !D->isInvalidDecl() &&
  9591. (D->getType()->isDependentType() ||
  9592. D->getType()->isInstantiationDependentType() ||
  9593. D->getType()->containsUnexpandedParameterPack());
  9594. })) {
  9595. UnresolvedSet<8> ResSet;
  9596. for (const UnresolvedSet<8> &Set : Lookups) {
  9597. if (Set.empty())
  9598. continue;
  9599. ResSet.append(Set.begin(), Set.end());
  9600. // The last item marks the end of all declarations at the specified scope.
  9601. ResSet.addDecl(Set[Set.size() - 1]);
  9602. }
  9603. return UnresolvedLookupExpr::Create(
  9604. SemaRef.Context, /*NamingClass=*/nullptr,
  9605. ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
  9606. /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
  9607. }
  9608. // Lookup inside the classes.
  9609. // C++ [over.match.oper]p3:
  9610. // For a unary operator @ with an operand of a type whose
  9611. // cv-unqualified version is T1, and for a binary operator @ with
  9612. // a left operand of a type whose cv-unqualified version is T1 and
  9613. // a right operand of a type whose cv-unqualified version is T2,
  9614. // three sets of candidate functions, designated member
  9615. // candidates, non-member candidates and built-in candidates, are
  9616. // constructed as follows:
  9617. // -- If T1 is a complete class type or a class currently being
  9618. // defined, the set of member candidates is the result of the
  9619. // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
  9620. // the set of member candidates is empty.
  9621. LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
  9622. Lookup.suppressDiagnostics();
  9623. if (const auto *TyRec = Ty->getAs<RecordType>()) {
  9624. // Complete the type if it can be completed.
  9625. // If the type is neither complete nor being defined, bail out now.
  9626. if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
  9627. TyRec->getDecl()->getDefinition()) {
  9628. Lookup.clear();
  9629. SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
  9630. if (Lookup.empty()) {
  9631. Lookups.emplace_back();
  9632. Lookups.back().append(Lookup.begin(), Lookup.end());
  9633. }
  9634. }
  9635. }
  9636. // Perform ADL.
  9637. argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
  9638. if (auto *VD = filterLookupForUDR<ValueDecl *>(
  9639. Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
  9640. if (!D->isInvalidDecl() &&
  9641. SemaRef.Context.hasSameType(D->getType(), Ty))
  9642. return D;
  9643. return nullptr;
  9644. }))
  9645. return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
  9646. if (auto *VD = filterLookupForUDR<ValueDecl *>(
  9647. Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
  9648. if (!D->isInvalidDecl() &&
  9649. SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
  9650. !Ty.isMoreQualifiedThan(D->getType()))
  9651. return D;
  9652. return nullptr;
  9653. })) {
  9654. CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
  9655. /*DetectVirtual=*/false);
  9656. if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
  9657. if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
  9658. VD->getType().getUnqualifiedType()))) {
  9659. if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
  9660. /*DiagID=*/0) !=
  9661. Sema::AR_inaccessible) {
  9662. SemaRef.BuildBasePathArray(Paths, BasePath);
  9663. return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
  9664. }
  9665. }
  9666. }
  9667. }
  9668. if (ReductionIdScopeSpec.isSet()) {
  9669. SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
  9670. return ExprError();
  9671. }
  9672. return ExprEmpty();
  9673. }
  9674. namespace {
  9675. /// Data for the reduction-based clauses.
  9676. struct ReductionData {
  9677. /// List of original reduction items.
  9678. SmallVector<Expr *, 8> Vars;
  9679. /// List of private copies of the reduction items.
  9680. SmallVector<Expr *, 8> Privates;
  9681. /// LHS expressions for the reduction_op expressions.
  9682. SmallVector<Expr *, 8> LHSs;
  9683. /// RHS expressions for the reduction_op expressions.
  9684. SmallVector<Expr *, 8> RHSs;
  9685. /// Reduction operation expression.
  9686. SmallVector<Expr *, 8> ReductionOps;
  9687. /// Taskgroup descriptors for the corresponding reduction items in
  9688. /// in_reduction clauses.
  9689. SmallVector<Expr *, 8> TaskgroupDescriptors;
  9690. /// List of captures for clause.
  9691. SmallVector<Decl *, 4> ExprCaptures;
  9692. /// List of postupdate expressions.
  9693. SmallVector<Expr *, 4> ExprPostUpdates;
  9694. ReductionData() = delete;
  9695. /// Reserves required memory for the reduction data.
  9696. ReductionData(unsigned Size) {
  9697. Vars.reserve(Size);
  9698. Privates.reserve(Size);
  9699. LHSs.reserve(Size);
  9700. RHSs.reserve(Size);
  9701. ReductionOps.reserve(Size);
  9702. TaskgroupDescriptors.reserve(Size);
  9703. ExprCaptures.reserve(Size);
  9704. ExprPostUpdates.reserve(Size);
  9705. }
  9706. /// Stores reduction item and reduction operation only (required for dependent
  9707. /// reduction item).
  9708. void push(Expr *Item, Expr *ReductionOp) {
  9709. Vars.emplace_back(Item);
  9710. Privates.emplace_back(nullptr);
  9711. LHSs.emplace_back(nullptr);
  9712. RHSs.emplace_back(nullptr);
  9713. ReductionOps.emplace_back(ReductionOp);
  9714. TaskgroupDescriptors.emplace_back(nullptr);
  9715. }
  9716. /// Stores reduction data.
  9717. void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
  9718. Expr *TaskgroupDescriptor) {
  9719. Vars.emplace_back(Item);
  9720. Privates.emplace_back(Private);
  9721. LHSs.emplace_back(LHS);
  9722. RHSs.emplace_back(RHS);
  9723. ReductionOps.emplace_back(ReductionOp);
  9724. TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
  9725. }
  9726. };
  9727. } // namespace
  9728. static bool checkOMPArraySectionConstantForReduction(
  9729. ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
  9730. SmallVectorImpl<llvm::APSInt> &ArraySizes) {
  9731. const Expr *Length = OASE->getLength();
  9732. if (Length == nullptr) {
  9733. // For array sections of the form [1:] or [:], we would need to analyze
  9734. // the lower bound...
  9735. if (OASE->getColonLoc().isValid())
  9736. return false;
  9737. // This is an array subscript which has implicit length 1!
  9738. SingleElement = true;
  9739. ArraySizes.push_back(llvm::APSInt::get(1));
  9740. } else {
  9741. llvm::APSInt ConstantLengthValue;
  9742. if (!Length->EvaluateAsInt(ConstantLengthValue, Context))
  9743. return false;
  9744. SingleElement = (ConstantLengthValue.getSExtValue() == 1);
  9745. ArraySizes.push_back(ConstantLengthValue);
  9746. }
  9747. // Get the base of this array section and walk up from there.
  9748. const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
  9749. // We require length = 1 for all array sections except the right-most to
  9750. // guarantee that the memory region is contiguous and has no holes in it.
  9751. while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
  9752. Length = TempOASE->getLength();
  9753. if (Length == nullptr) {
  9754. // For array sections of the form [1:] or [:], we would need to analyze
  9755. // the lower bound...
  9756. if (OASE->getColonLoc().isValid())
  9757. return false;
  9758. // This is an array subscript which has implicit length 1!
  9759. ArraySizes.push_back(llvm::APSInt::get(1));
  9760. } else {
  9761. llvm::APSInt ConstantLengthValue;
  9762. if (!Length->EvaluateAsInt(ConstantLengthValue, Context) ||
  9763. ConstantLengthValue.getSExtValue() != 1)
  9764. return false;
  9765. ArraySizes.push_back(ConstantLengthValue);
  9766. }
  9767. Base = TempOASE->getBase()->IgnoreParenImpCasts();
  9768. }
  9769. // If we have a single element, we don't need to add the implicit lengths.
  9770. if (!SingleElement) {
  9771. while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
  9772. // Has implicit length 1!
  9773. ArraySizes.push_back(llvm::APSInt::get(1));
  9774. Base = TempASE->getBase()->IgnoreParenImpCasts();
  9775. }
  9776. }
  9777. // This array section can be privatized as a single value or as a constant
  9778. // sized array.
  9779. return true;
  9780. }
  9781. static bool actOnOMPReductionKindClause(
  9782. Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
  9783. ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
  9784. SourceLocation ColonLoc, SourceLocation EndLoc,
  9785. CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
  9786. ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
  9787. DeclarationName DN = ReductionId.getName();
  9788. OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
  9789. BinaryOperatorKind BOK = BO_Comma;
  9790. ASTContext &Context = S.Context;
  9791. // OpenMP [2.14.3.6, reduction clause]
  9792. // C
  9793. // reduction-identifier is either an identifier or one of the following
  9794. // operators: +, -, *, &, |, ^, && and ||
  9795. // C++
  9796. // reduction-identifier is either an id-expression or one of the following
  9797. // operators: +, -, *, &, |, ^, && and ||
  9798. switch (OOK) {
  9799. case OO_Plus:
  9800. case OO_Minus:
  9801. BOK = BO_Add;
  9802. break;
  9803. case OO_Star:
  9804. BOK = BO_Mul;
  9805. break;
  9806. case OO_Amp:
  9807. BOK = BO_And;
  9808. break;
  9809. case OO_Pipe:
  9810. BOK = BO_Or;
  9811. break;
  9812. case OO_Caret:
  9813. BOK = BO_Xor;
  9814. break;
  9815. case OO_AmpAmp:
  9816. BOK = BO_LAnd;
  9817. break;
  9818. case OO_PipePipe:
  9819. BOK = BO_LOr;
  9820. break;
  9821. case OO_New:
  9822. case OO_Delete:
  9823. case OO_Array_New:
  9824. case OO_Array_Delete:
  9825. case OO_Slash:
  9826. case OO_Percent:
  9827. case OO_Tilde:
  9828. case OO_Exclaim:
  9829. case OO_Equal:
  9830. case OO_Less:
  9831. case OO_Greater:
  9832. case OO_LessEqual:
  9833. case OO_GreaterEqual:
  9834. case OO_PlusEqual:
  9835. case OO_MinusEqual:
  9836. case OO_StarEqual:
  9837. case OO_SlashEqual:
  9838. case OO_PercentEqual:
  9839. case OO_CaretEqual:
  9840. case OO_AmpEqual:
  9841. case OO_PipeEqual:
  9842. case OO_LessLess:
  9843. case OO_GreaterGreater:
  9844. case OO_LessLessEqual:
  9845. case OO_GreaterGreaterEqual:
  9846. case OO_EqualEqual:
  9847. case OO_ExclaimEqual:
  9848. case OO_Spaceship:
  9849. case OO_PlusPlus:
  9850. case OO_MinusMinus:
  9851. case OO_Comma:
  9852. case OO_ArrowStar:
  9853. case OO_Arrow:
  9854. case OO_Call:
  9855. case OO_Subscript:
  9856. case OO_Conditional:
  9857. case OO_Coawait:
  9858. case NUM_OVERLOADED_OPERATORS:
  9859. llvm_unreachable("Unexpected reduction identifier");
  9860. case OO_None:
  9861. if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
  9862. if (II->isStr("max"))
  9863. BOK = BO_GT;
  9864. else if (II->isStr("min"))
  9865. BOK = BO_LT;
  9866. }
  9867. break;
  9868. }
  9869. SourceRange ReductionIdRange;
  9870. if (ReductionIdScopeSpec.isValid())
  9871. ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
  9872. else
  9873. ReductionIdRange.setBegin(ReductionId.getBeginLoc());
  9874. ReductionIdRange.setEnd(ReductionId.getEndLoc());
  9875. auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
  9876. bool FirstIter = true;
  9877. for (Expr *RefExpr : VarList) {
  9878. assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
  9879. // OpenMP [2.1, C/C++]
  9880. // A list item is a variable or array section, subject to the restrictions
  9881. // specified in Section 2.4 on page 42 and in each of the sections
  9882. // describing clauses and directives for which a list appears.
  9883. // OpenMP [2.14.3.3, Restrictions, p.1]
  9884. // A variable that is part of another variable (as an array or
  9885. // structure element) cannot appear in a private clause.
  9886. if (!FirstIter && IR != ER)
  9887. ++IR;
  9888. FirstIter = false;
  9889. SourceLocation ELoc;
  9890. SourceRange ERange;
  9891. Expr *SimpleRefExpr = RefExpr;
  9892. auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
  9893. /*AllowArraySection=*/true);
  9894. if (Res.second) {
  9895. // Try to find 'declare reduction' corresponding construct before using
  9896. // builtin/overloaded operators.
  9897. QualType Type = Context.DependentTy;
  9898. CXXCastPath BasePath;
  9899. ExprResult DeclareReductionRef = buildDeclareReductionRef(
  9900. S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
  9901. ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
  9902. Expr *ReductionOp = nullptr;
  9903. if (S.CurContext->isDependentContext() &&
  9904. (DeclareReductionRef.isUnset() ||
  9905. isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
  9906. ReductionOp = DeclareReductionRef.get();
  9907. // It will be analyzed later.
  9908. RD.push(RefExpr, ReductionOp);
  9909. }
  9910. ValueDecl *D = Res.first;
  9911. if (!D)
  9912. continue;
  9913. Expr *TaskgroupDescriptor = nullptr;
  9914. QualType Type;
  9915. auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
  9916. auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
  9917. if (ASE) {
  9918. Type = ASE->getType().getNonReferenceType();
  9919. } else if (OASE) {
  9920. QualType BaseType =
  9921. OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
  9922. if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
  9923. Type = ATy->getElementType();
  9924. else
  9925. Type = BaseType->getPointeeType();
  9926. Type = Type.getNonReferenceType();
  9927. } else {
  9928. Type = Context.getBaseElementType(D->getType().getNonReferenceType());
  9929. }
  9930. auto *VD = dyn_cast<VarDecl>(D);
  9931. // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
  9932. // A variable that appears in a private clause must not have an incomplete
  9933. // type or a reference type.
  9934. if (S.RequireCompleteType(ELoc, D->getType(),
  9935. diag::err_omp_reduction_incomplete_type))
  9936. continue;
  9937. // OpenMP [2.14.3.6, reduction clause, Restrictions]
  9938. // A list item that appears in a reduction clause must not be
  9939. // const-qualified.
  9940. if (Type.getNonReferenceType().isConstant(Context)) {
  9941. S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange;
  9942. if (!ASE && !OASE) {
  9943. bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
  9944. VarDecl::DeclarationOnly;
  9945. S.Diag(D->getLocation(),
  9946. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  9947. << D;
  9948. }
  9949. continue;
  9950. }
  9951. // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
  9952. // If a list-item is a reference type then it must bind to the same object
  9953. // for all threads of the team.
  9954. if (!ASE && !OASE && VD) {
  9955. VarDecl *VDDef = VD->getDefinition();
  9956. if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
  9957. DSARefChecker Check(Stack);
  9958. if (Check.Visit(VDDef->getInit())) {
  9959. S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
  9960. << getOpenMPClauseName(ClauseKind) << ERange;
  9961. S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
  9962. continue;
  9963. }
  9964. }
  9965. }
  9966. // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
  9967. // in a Construct]
  9968. // Variables with the predetermined data-sharing attributes may not be
  9969. // listed in data-sharing attributes clauses, except for the cases
  9970. // listed below. For these exceptions only, listing a predetermined
  9971. // variable in a data-sharing attribute clause is allowed and overrides
  9972. // the variable's predetermined data-sharing attributes.
  9973. // OpenMP [2.14.3.6, Restrictions, p.3]
  9974. // Any number of reduction clauses can be specified on the directive,
  9975. // but a list item can appear only once in the reduction clauses for that
  9976. // directive.
  9977. DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
  9978. if (DVar.CKind == OMPC_reduction) {
  9979. S.Diag(ELoc, diag::err_omp_once_referenced)
  9980. << getOpenMPClauseName(ClauseKind);
  9981. if (DVar.RefExpr)
  9982. S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
  9983. continue;
  9984. }
  9985. if (DVar.CKind != OMPC_unknown) {
  9986. S.Diag(ELoc, diag::err_omp_wrong_dsa)
  9987. << getOpenMPClauseName(DVar.CKind)
  9988. << getOpenMPClauseName(OMPC_reduction);
  9989. reportOriginalDsa(S, Stack, D, DVar);
  9990. continue;
  9991. }
  9992. // OpenMP [2.14.3.6, Restrictions, p.1]
  9993. // A list item that appears in a reduction clause of a worksharing
  9994. // construct must be shared in the parallel regions to which any of the
  9995. // worksharing regions arising from the worksharing construct bind.
  9996. OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
  9997. if (isOpenMPWorksharingDirective(CurrDir) &&
  9998. !isOpenMPParallelDirective(CurrDir) &&
  9999. !isOpenMPTeamsDirective(CurrDir)) {
  10000. DVar = Stack->getImplicitDSA(D, true);
  10001. if (DVar.CKind != OMPC_shared) {
  10002. S.Diag(ELoc, diag::err_omp_required_access)
  10003. << getOpenMPClauseName(OMPC_reduction)
  10004. << getOpenMPClauseName(OMPC_shared);
  10005. reportOriginalDsa(S, Stack, D, DVar);
  10006. continue;
  10007. }
  10008. }
  10009. // Try to find 'declare reduction' corresponding construct before using
  10010. // builtin/overloaded operators.
  10011. CXXCastPath BasePath;
  10012. ExprResult DeclareReductionRef = buildDeclareReductionRef(
  10013. S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
  10014. ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
  10015. if (DeclareReductionRef.isInvalid())
  10016. continue;
  10017. if (S.CurContext->isDependentContext() &&
  10018. (DeclareReductionRef.isUnset() ||
  10019. isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
  10020. RD.push(RefExpr, DeclareReductionRef.get());
  10021. continue;
  10022. }
  10023. if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
  10024. // Not allowed reduction identifier is found.
  10025. S.Diag(ReductionId.getBeginLoc(),
  10026. diag::err_omp_unknown_reduction_identifier)
  10027. << Type << ReductionIdRange;
  10028. continue;
  10029. }
  10030. // OpenMP [2.14.3.6, reduction clause, Restrictions]
  10031. // The type of a list item that appears in a reduction clause must be valid
  10032. // for the reduction-identifier. For a max or min reduction in C, the type
  10033. // of the list item must be an allowed arithmetic data type: char, int,
  10034. // float, double, or _Bool, possibly modified with long, short, signed, or
  10035. // unsigned. For a max or min reduction in C++, the type of the list item
  10036. // must be an allowed arithmetic data type: char, wchar_t, int, float,
  10037. // double, or bool, possibly modified with long, short, signed, or unsigned.
  10038. if (DeclareReductionRef.isUnset()) {
  10039. if ((BOK == BO_GT || BOK == BO_LT) &&
  10040. !(Type->isScalarType() ||
  10041. (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
  10042. S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
  10043. << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
  10044. if (!ASE && !OASE) {
  10045. bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
  10046. VarDecl::DeclarationOnly;
  10047. S.Diag(D->getLocation(),
  10048. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  10049. << D;
  10050. }
  10051. continue;
  10052. }
  10053. if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
  10054. !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
  10055. S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
  10056. << getOpenMPClauseName(ClauseKind);
  10057. if (!ASE && !OASE) {
  10058. bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
  10059. VarDecl::DeclarationOnly;
  10060. S.Diag(D->getLocation(),
  10061. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  10062. << D;
  10063. }
  10064. continue;
  10065. }
  10066. }
  10067. Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
  10068. VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
  10069. D->hasAttrs() ? &D->getAttrs() : nullptr);
  10070. VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
  10071. D->hasAttrs() ? &D->getAttrs() : nullptr);
  10072. QualType PrivateTy = Type;
  10073. // Try if we can determine constant lengths for all array sections and avoid
  10074. // the VLA.
  10075. bool ConstantLengthOASE = false;
  10076. if (OASE) {
  10077. bool SingleElement;
  10078. llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
  10079. ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
  10080. Context, OASE, SingleElement, ArraySizes);
  10081. // If we don't have a single element, we must emit a constant array type.
  10082. if (ConstantLengthOASE && !SingleElement) {
  10083. for (llvm::APSInt &Size : ArraySizes)
  10084. PrivateTy = Context.getConstantArrayType(
  10085. PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
  10086. }
  10087. }
  10088. if ((OASE && !ConstantLengthOASE) ||
  10089. (!OASE && !ASE &&
  10090. D->getType().getNonReferenceType()->isVariablyModifiedType())) {
  10091. if (!Context.getTargetInfo().isVLASupported() &&
  10092. S.shouldDiagnoseTargetSupportFromOpenMP()) {
  10093. S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
  10094. S.Diag(ELoc, diag::note_vla_unsupported);
  10095. continue;
  10096. }
  10097. // For arrays/array sections only:
  10098. // Create pseudo array type for private copy. The size for this array will
  10099. // be generated during codegen.
  10100. // For array subscripts or single variables Private Ty is the same as Type
  10101. // (type of the variable or single array element).
  10102. PrivateTy = Context.getVariableArrayType(
  10103. Type,
  10104. new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
  10105. ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
  10106. } else if (!ASE && !OASE &&
  10107. Context.getAsArrayType(D->getType().getNonReferenceType())) {
  10108. PrivateTy = D->getType().getNonReferenceType();
  10109. }
  10110. // Private copy.
  10111. VarDecl *PrivateVD =
  10112. buildVarDecl(S, ELoc, PrivateTy, D->getName(),
  10113. D->hasAttrs() ? &D->getAttrs() : nullptr,
  10114. VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
  10115. // Add initializer for private variable.
  10116. Expr *Init = nullptr;
  10117. DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
  10118. DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
  10119. if (DeclareReductionRef.isUsable()) {
  10120. auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
  10121. auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
  10122. if (DRD->getInitializer()) {
  10123. Init = DRDRef;
  10124. RHSVD->setInit(DRDRef);
  10125. RHSVD->setInitStyle(VarDecl::CallInit);
  10126. }
  10127. } else {
  10128. switch (BOK) {
  10129. case BO_Add:
  10130. case BO_Xor:
  10131. case BO_Or:
  10132. case BO_LOr:
  10133. // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
  10134. if (Type->isScalarType() || Type->isAnyComplexType())
  10135. Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
  10136. break;
  10137. case BO_Mul:
  10138. case BO_LAnd:
  10139. if (Type->isScalarType() || Type->isAnyComplexType()) {
  10140. // '*' and '&&' reduction ops - initializer is '1'.
  10141. Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
  10142. }
  10143. break;
  10144. case BO_And: {
  10145. // '&' reduction op - initializer is '~0'.
  10146. QualType OrigType = Type;
  10147. if (auto *ComplexTy = OrigType->getAs<ComplexType>())
  10148. Type = ComplexTy->getElementType();
  10149. if (Type->isRealFloatingType()) {
  10150. llvm::APFloat InitValue =
  10151. llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
  10152. /*isIEEE=*/true);
  10153. Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
  10154. Type, ELoc);
  10155. } else if (Type->isScalarType()) {
  10156. uint64_t Size = Context.getTypeSize(Type);
  10157. QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
  10158. llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
  10159. Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
  10160. }
  10161. if (Init && OrigType->isAnyComplexType()) {
  10162. // Init = 0xFFFF + 0xFFFFi;
  10163. auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
  10164. Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
  10165. }
  10166. Type = OrigType;
  10167. break;
  10168. }
  10169. case BO_LT:
  10170. case BO_GT: {
  10171. // 'min' reduction op - initializer is 'Largest representable number in
  10172. // the reduction list item type'.
  10173. // 'max' reduction op - initializer is 'Least representable number in
  10174. // the reduction list item type'.
  10175. if (Type->isIntegerType() || Type->isPointerType()) {
  10176. bool IsSigned = Type->hasSignedIntegerRepresentation();
  10177. uint64_t Size = Context.getTypeSize(Type);
  10178. QualType IntTy =
  10179. Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
  10180. llvm::APInt InitValue =
  10181. (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
  10182. : llvm::APInt::getMinValue(Size)
  10183. : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
  10184. : llvm::APInt::getMaxValue(Size);
  10185. Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
  10186. if (Type->isPointerType()) {
  10187. // Cast to pointer type.
  10188. ExprResult CastExpr = S.BuildCStyleCastExpr(
  10189. ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
  10190. if (CastExpr.isInvalid())
  10191. continue;
  10192. Init = CastExpr.get();
  10193. }
  10194. } else if (Type->isRealFloatingType()) {
  10195. llvm::APFloat InitValue = llvm::APFloat::getLargest(
  10196. Context.getFloatTypeSemantics(Type), BOK != BO_LT);
  10197. Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
  10198. Type, ELoc);
  10199. }
  10200. break;
  10201. }
  10202. case BO_PtrMemD:
  10203. case BO_PtrMemI:
  10204. case BO_MulAssign:
  10205. case BO_Div:
  10206. case BO_Rem:
  10207. case BO_Sub:
  10208. case BO_Shl:
  10209. case BO_Shr:
  10210. case BO_LE:
  10211. case BO_GE:
  10212. case BO_EQ:
  10213. case BO_NE:
  10214. case BO_Cmp:
  10215. case BO_AndAssign:
  10216. case BO_XorAssign:
  10217. case BO_OrAssign:
  10218. case BO_Assign:
  10219. case BO_AddAssign:
  10220. case BO_SubAssign:
  10221. case BO_DivAssign:
  10222. case BO_RemAssign:
  10223. case BO_ShlAssign:
  10224. case BO_ShrAssign:
  10225. case BO_Comma:
  10226. llvm_unreachable("Unexpected reduction operation");
  10227. }
  10228. }
  10229. if (Init && DeclareReductionRef.isUnset())
  10230. S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
  10231. else if (!Init)
  10232. S.ActOnUninitializedDecl(RHSVD);
  10233. if (RHSVD->isInvalidDecl())
  10234. continue;
  10235. if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
  10236. S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
  10237. << Type << ReductionIdRange;
  10238. bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
  10239. VarDecl::DeclarationOnly;
  10240. S.Diag(D->getLocation(),
  10241. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  10242. << D;
  10243. continue;
  10244. }
  10245. // Store initializer for single element in private copy. Will be used during
  10246. // codegen.
  10247. PrivateVD->setInit(RHSVD->getInit());
  10248. PrivateVD->setInitStyle(RHSVD->getInitStyle());
  10249. DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
  10250. ExprResult ReductionOp;
  10251. if (DeclareReductionRef.isUsable()) {
  10252. QualType RedTy = DeclareReductionRef.get()->getType();
  10253. QualType PtrRedTy = Context.getPointerType(RedTy);
  10254. ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
  10255. ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
  10256. if (!BasePath.empty()) {
  10257. LHS = S.DefaultLvalueConversion(LHS.get());
  10258. RHS = S.DefaultLvalueConversion(RHS.get());
  10259. LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
  10260. CK_UncheckedDerivedToBase, LHS.get(),
  10261. &BasePath, LHS.get()->getValueKind());
  10262. RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
  10263. CK_UncheckedDerivedToBase, RHS.get(),
  10264. &BasePath, RHS.get()->getValueKind());
  10265. }
  10266. FunctionProtoType::ExtProtoInfo EPI;
  10267. QualType Params[] = {PtrRedTy, PtrRedTy};
  10268. QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
  10269. auto *OVE = new (Context) OpaqueValueExpr(
  10270. ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
  10271. S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
  10272. Expr *Args[] = {LHS.get(), RHS.get()};
  10273. ReductionOp = new (Context)
  10274. CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
  10275. } else {
  10276. ReductionOp = S.BuildBinOp(
  10277. Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
  10278. if (ReductionOp.isUsable()) {
  10279. if (BOK != BO_LT && BOK != BO_GT) {
  10280. ReductionOp =
  10281. S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
  10282. BO_Assign, LHSDRE, ReductionOp.get());
  10283. } else {
  10284. auto *ConditionalOp = new (Context)
  10285. ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
  10286. Type, VK_LValue, OK_Ordinary);
  10287. ReductionOp =
  10288. S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
  10289. BO_Assign, LHSDRE, ConditionalOp);
  10290. }
  10291. if (ReductionOp.isUsable())
  10292. ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get());
  10293. }
  10294. if (!ReductionOp.isUsable())
  10295. continue;
  10296. }
  10297. // OpenMP [2.15.4.6, Restrictions, p.2]
  10298. // A list item that appears in an in_reduction clause of a task construct
  10299. // must appear in a task_reduction clause of a construct associated with a
  10300. // taskgroup region that includes the participating task in its taskgroup
  10301. // set. The construct associated with the innermost region that meets this
  10302. // condition must specify the same reduction-identifier as the in_reduction
  10303. // clause.
  10304. if (ClauseKind == OMPC_in_reduction) {
  10305. SourceRange ParentSR;
  10306. BinaryOperatorKind ParentBOK;
  10307. const Expr *ParentReductionOp;
  10308. Expr *ParentBOKTD, *ParentReductionOpTD;
  10309. DSAStackTy::DSAVarData ParentBOKDSA =
  10310. Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
  10311. ParentBOKTD);
  10312. DSAStackTy::DSAVarData ParentReductionOpDSA =
  10313. Stack->getTopMostTaskgroupReductionData(
  10314. D, ParentSR, ParentReductionOp, ParentReductionOpTD);
  10315. bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
  10316. bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
  10317. if (!IsParentBOK && !IsParentReductionOp) {
  10318. S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
  10319. continue;
  10320. }
  10321. if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
  10322. (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
  10323. IsParentReductionOp) {
  10324. bool EmitError = true;
  10325. if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
  10326. llvm::FoldingSetNodeID RedId, ParentRedId;
  10327. ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
  10328. DeclareReductionRef.get()->Profile(RedId, Context,
  10329. /*Canonical=*/true);
  10330. EmitError = RedId != ParentRedId;
  10331. }
  10332. if (EmitError) {
  10333. S.Diag(ReductionId.getBeginLoc(),
  10334. diag::err_omp_reduction_identifier_mismatch)
  10335. << ReductionIdRange << RefExpr->getSourceRange();
  10336. S.Diag(ParentSR.getBegin(),
  10337. diag::note_omp_previous_reduction_identifier)
  10338. << ParentSR
  10339. << (IsParentBOK ? ParentBOKDSA.RefExpr
  10340. : ParentReductionOpDSA.RefExpr)
  10341. ->getSourceRange();
  10342. continue;
  10343. }
  10344. }
  10345. TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
  10346. assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
  10347. }
  10348. DeclRefExpr *Ref = nullptr;
  10349. Expr *VarsExpr = RefExpr->IgnoreParens();
  10350. if (!VD && !S.CurContext->isDependentContext()) {
  10351. if (ASE || OASE) {
  10352. TransformExprToCaptures RebuildToCapture(S, D);
  10353. VarsExpr =
  10354. RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
  10355. Ref = RebuildToCapture.getCapturedExpr();
  10356. } else {
  10357. VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
  10358. }
  10359. if (!S.isOpenMPCapturedDecl(D)) {
  10360. RD.ExprCaptures.emplace_back(Ref->getDecl());
  10361. if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
  10362. ExprResult RefRes = S.DefaultLvalueConversion(Ref);
  10363. if (!RefRes.isUsable())
  10364. continue;
  10365. ExprResult PostUpdateRes =
  10366. S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
  10367. RefRes.get());
  10368. if (!PostUpdateRes.isUsable())
  10369. continue;
  10370. if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
  10371. Stack->getCurrentDirective() == OMPD_taskgroup) {
  10372. S.Diag(RefExpr->getExprLoc(),
  10373. diag::err_omp_reduction_non_addressable_expression)
  10374. << RefExpr->getSourceRange();
  10375. continue;
  10376. }
  10377. RD.ExprPostUpdates.emplace_back(
  10378. S.IgnoredValueConversions(PostUpdateRes.get()).get());
  10379. }
  10380. }
  10381. }
  10382. // All reduction items are still marked as reduction (to do not increase
  10383. // code base size).
  10384. Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
  10385. if (CurrDir == OMPD_taskgroup) {
  10386. if (DeclareReductionRef.isUsable())
  10387. Stack->addTaskgroupReductionData(D, ReductionIdRange,
  10388. DeclareReductionRef.get());
  10389. else
  10390. Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
  10391. }
  10392. RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
  10393. TaskgroupDescriptor);
  10394. }
  10395. return RD.Vars.empty();
  10396. }
  10397. OMPClause *Sema::ActOnOpenMPReductionClause(
  10398. ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
  10399. SourceLocation ColonLoc, SourceLocation EndLoc,
  10400. CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
  10401. ArrayRef<Expr *> UnresolvedReductions) {
  10402. ReductionData RD(VarList.size());
  10403. if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
  10404. StartLoc, LParenLoc, ColonLoc, EndLoc,
  10405. ReductionIdScopeSpec, ReductionId,
  10406. UnresolvedReductions, RD))
  10407. return nullptr;
  10408. return OMPReductionClause::Create(
  10409. Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
  10410. ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
  10411. RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
  10412. buildPreInits(Context, RD.ExprCaptures),
  10413. buildPostUpdate(*this, RD.ExprPostUpdates));
  10414. }
  10415. OMPClause *Sema::ActOnOpenMPTaskReductionClause(
  10416. ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
  10417. SourceLocation ColonLoc, SourceLocation EndLoc,
  10418. CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
  10419. ArrayRef<Expr *> UnresolvedReductions) {
  10420. ReductionData RD(VarList.size());
  10421. if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
  10422. StartLoc, LParenLoc, ColonLoc, EndLoc,
  10423. ReductionIdScopeSpec, ReductionId,
  10424. UnresolvedReductions, RD))
  10425. return nullptr;
  10426. return OMPTaskReductionClause::Create(
  10427. Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
  10428. ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
  10429. RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
  10430. buildPreInits(Context, RD.ExprCaptures),
  10431. buildPostUpdate(*this, RD.ExprPostUpdates));
  10432. }
  10433. OMPClause *Sema::ActOnOpenMPInReductionClause(
  10434. ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
  10435. SourceLocation ColonLoc, SourceLocation EndLoc,
  10436. CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
  10437. ArrayRef<Expr *> UnresolvedReductions) {
  10438. ReductionData RD(VarList.size());
  10439. if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
  10440. StartLoc, LParenLoc, ColonLoc, EndLoc,
  10441. ReductionIdScopeSpec, ReductionId,
  10442. UnresolvedReductions, RD))
  10443. return nullptr;
  10444. return OMPInReductionClause::Create(
  10445. Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
  10446. ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
  10447. RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
  10448. buildPreInits(Context, RD.ExprCaptures),
  10449. buildPostUpdate(*this, RD.ExprPostUpdates));
  10450. }
  10451. bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
  10452. SourceLocation LinLoc) {
  10453. if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
  10454. LinKind == OMPC_LINEAR_unknown) {
  10455. Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
  10456. return true;
  10457. }
  10458. return false;
  10459. }
  10460. bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
  10461. OpenMPLinearClauseKind LinKind,
  10462. QualType Type) {
  10463. const auto *VD = dyn_cast_or_null<VarDecl>(D);
  10464. // A variable must not have an incomplete type or a reference type.
  10465. if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
  10466. return true;
  10467. if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
  10468. !Type->isReferenceType()) {
  10469. Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
  10470. << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
  10471. return true;
  10472. }
  10473. Type = Type.getNonReferenceType();
  10474. // A list item must not be const-qualified.
  10475. if (Type.isConstant(Context)) {
  10476. Diag(ELoc, diag::err_omp_const_variable)
  10477. << getOpenMPClauseName(OMPC_linear);
  10478. if (D) {
  10479. bool IsDecl =
  10480. !VD ||
  10481. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  10482. Diag(D->getLocation(),
  10483. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  10484. << D;
  10485. }
  10486. return true;
  10487. }
  10488. // A list item must be of integral or pointer type.
  10489. Type = Type.getUnqualifiedType().getCanonicalType();
  10490. const auto *Ty = Type.getTypePtrOrNull();
  10491. if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
  10492. !Ty->isPointerType())) {
  10493. Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
  10494. if (D) {
  10495. bool IsDecl =
  10496. !VD ||
  10497. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  10498. Diag(D->getLocation(),
  10499. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  10500. << D;
  10501. }
  10502. return true;
  10503. }
  10504. return false;
  10505. }
  10506. OMPClause *Sema::ActOnOpenMPLinearClause(
  10507. ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
  10508. SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
  10509. SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
  10510. SmallVector<Expr *, 8> Vars;
  10511. SmallVector<Expr *, 8> Privates;
  10512. SmallVector<Expr *, 8> Inits;
  10513. SmallVector<Decl *, 4> ExprCaptures;
  10514. SmallVector<Expr *, 4> ExprPostUpdates;
  10515. if (CheckOpenMPLinearModifier(LinKind, LinLoc))
  10516. LinKind = OMPC_LINEAR_val;
  10517. for (Expr *RefExpr : VarList) {
  10518. assert(RefExpr && "NULL expr in OpenMP linear clause.");
  10519. SourceLocation ELoc;
  10520. SourceRange ERange;
  10521. Expr *SimpleRefExpr = RefExpr;
  10522. auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
  10523. /*AllowArraySection=*/false);
  10524. if (Res.second) {
  10525. // It will be analyzed later.
  10526. Vars.push_back(RefExpr);
  10527. Privates.push_back(nullptr);
  10528. Inits.push_back(nullptr);
  10529. }
  10530. ValueDecl *D = Res.first;
  10531. if (!D)
  10532. continue;
  10533. QualType Type = D->getType();
  10534. auto *VD = dyn_cast<VarDecl>(D);
  10535. // OpenMP [2.14.3.7, linear clause]
  10536. // A list-item cannot appear in more than one linear clause.
  10537. // A list-item that appears in a linear clause cannot appear in any
  10538. // other data-sharing attribute clause.
  10539. DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
  10540. if (DVar.RefExpr) {
  10541. Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
  10542. << getOpenMPClauseName(OMPC_linear);
  10543. reportOriginalDsa(*this, DSAStack, D, DVar);
  10544. continue;
  10545. }
  10546. if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
  10547. continue;
  10548. Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
  10549. // Build private copy of original var.
  10550. VarDecl *Private =
  10551. buildVarDecl(*this, ELoc, Type, D->getName(),
  10552. D->hasAttrs() ? &D->getAttrs() : nullptr,
  10553. VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
  10554. DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
  10555. // Build var to save initial value.
  10556. VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
  10557. Expr *InitExpr;
  10558. DeclRefExpr *Ref = nullptr;
  10559. if (!VD && !CurContext->isDependentContext()) {
  10560. Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
  10561. if (!isOpenMPCapturedDecl(D)) {
  10562. ExprCaptures.push_back(Ref->getDecl());
  10563. if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
  10564. ExprResult RefRes = DefaultLvalueConversion(Ref);
  10565. if (!RefRes.isUsable())
  10566. continue;
  10567. ExprResult PostUpdateRes =
  10568. BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
  10569. SimpleRefExpr, RefRes.get());
  10570. if (!PostUpdateRes.isUsable())
  10571. continue;
  10572. ExprPostUpdates.push_back(
  10573. IgnoredValueConversions(PostUpdateRes.get()).get());
  10574. }
  10575. }
  10576. }
  10577. if (LinKind == OMPC_LINEAR_uval)
  10578. InitExpr = VD ? VD->getInit() : SimpleRefExpr;
  10579. else
  10580. InitExpr = VD ? SimpleRefExpr : Ref;
  10581. AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
  10582. /*DirectInit=*/false);
  10583. DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
  10584. DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
  10585. Vars.push_back((VD || CurContext->isDependentContext())
  10586. ? RefExpr->IgnoreParens()
  10587. : Ref);
  10588. Privates.push_back(PrivateRef);
  10589. Inits.push_back(InitRef);
  10590. }
  10591. if (Vars.empty())
  10592. return nullptr;
  10593. Expr *StepExpr = Step;
  10594. Expr *CalcStepExpr = nullptr;
  10595. if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
  10596. !Step->isInstantiationDependent() &&
  10597. !Step->containsUnexpandedParameterPack()) {
  10598. SourceLocation StepLoc = Step->getBeginLoc();
  10599. ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
  10600. if (Val.isInvalid())
  10601. return nullptr;
  10602. StepExpr = Val.get();
  10603. // Build var to save the step value.
  10604. VarDecl *SaveVar =
  10605. buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
  10606. ExprResult SaveRef =
  10607. buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
  10608. ExprResult CalcStep =
  10609. BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
  10610. CalcStep = ActOnFinishFullExpr(CalcStep.get());
  10611. // Warn about zero linear step (it would be probably better specified as
  10612. // making corresponding variables 'const').
  10613. llvm::APSInt Result;
  10614. bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
  10615. if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
  10616. Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
  10617. << (Vars.size() > 1);
  10618. if (!IsConstant && CalcStep.isUsable()) {
  10619. // Calculate the step beforehand instead of doing this on each iteration.
  10620. // (This is not used if the number of iterations may be kfold-ed).
  10621. CalcStepExpr = CalcStep.get();
  10622. }
  10623. }
  10624. return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
  10625. ColonLoc, EndLoc, Vars, Privates, Inits,
  10626. StepExpr, CalcStepExpr,
  10627. buildPreInits(Context, ExprCaptures),
  10628. buildPostUpdate(*this, ExprPostUpdates));
  10629. }
  10630. static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
  10631. Expr *NumIterations, Sema &SemaRef,
  10632. Scope *S, DSAStackTy *Stack) {
  10633. // Walk the vars and build update/final expressions for the CodeGen.
  10634. SmallVector<Expr *, 8> Updates;
  10635. SmallVector<Expr *, 8> Finals;
  10636. Expr *Step = Clause.getStep();
  10637. Expr *CalcStep = Clause.getCalcStep();
  10638. // OpenMP [2.14.3.7, linear clause]
  10639. // If linear-step is not specified it is assumed to be 1.
  10640. if (!Step)
  10641. Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
  10642. else if (CalcStep)
  10643. Step = cast<BinaryOperator>(CalcStep)->getLHS();
  10644. bool HasErrors = false;
  10645. auto CurInit = Clause.inits().begin();
  10646. auto CurPrivate = Clause.privates().begin();
  10647. OpenMPLinearClauseKind LinKind = Clause.getModifier();
  10648. for (Expr *RefExpr : Clause.varlists()) {
  10649. SourceLocation ELoc;
  10650. SourceRange ERange;
  10651. Expr *SimpleRefExpr = RefExpr;
  10652. auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
  10653. /*AllowArraySection=*/false);
  10654. ValueDecl *D = Res.first;
  10655. if (Res.second || !D) {
  10656. Updates.push_back(nullptr);
  10657. Finals.push_back(nullptr);
  10658. HasErrors = true;
  10659. continue;
  10660. }
  10661. auto &&Info = Stack->isLoopControlVariable(D);
  10662. // OpenMP [2.15.11, distribute simd Construct]
  10663. // A list item may not appear in a linear clause, unless it is the loop
  10664. // iteration variable.
  10665. if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
  10666. isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
  10667. SemaRef.Diag(ELoc,
  10668. diag::err_omp_linear_distribute_var_non_loop_iteration);
  10669. Updates.push_back(nullptr);
  10670. Finals.push_back(nullptr);
  10671. HasErrors = true;
  10672. continue;
  10673. }
  10674. Expr *InitExpr = *CurInit;
  10675. // Build privatized reference to the current linear var.
  10676. auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
  10677. Expr *CapturedRef;
  10678. if (LinKind == OMPC_LINEAR_uval)
  10679. CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
  10680. else
  10681. CapturedRef =
  10682. buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
  10683. DE->getType().getUnqualifiedType(), DE->getExprLoc(),
  10684. /*RefersToCapture=*/true);
  10685. // Build update: Var = InitExpr + IV * Step
  10686. ExprResult Update;
  10687. if (!Info.first)
  10688. Update =
  10689. buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
  10690. InitExpr, IV, Step, /* Subtract */ false);
  10691. else
  10692. Update = *CurPrivate;
  10693. Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
  10694. /*DiscardedValue=*/true);
  10695. // Build final: Var = InitExpr + NumIterations * Step
  10696. ExprResult Final;
  10697. if (!Info.first)
  10698. Final =
  10699. buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
  10700. InitExpr, NumIterations, Step, /*Subtract=*/false);
  10701. else
  10702. Final = *CurPrivate;
  10703. Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
  10704. /*DiscardedValue=*/true);
  10705. if (!Update.isUsable() || !Final.isUsable()) {
  10706. Updates.push_back(nullptr);
  10707. Finals.push_back(nullptr);
  10708. HasErrors = true;
  10709. } else {
  10710. Updates.push_back(Update.get());
  10711. Finals.push_back(Final.get());
  10712. }
  10713. ++CurInit;
  10714. ++CurPrivate;
  10715. }
  10716. Clause.setUpdates(Updates);
  10717. Clause.setFinals(Finals);
  10718. return HasErrors;
  10719. }
  10720. OMPClause *Sema::ActOnOpenMPAlignedClause(
  10721. ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
  10722. SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
  10723. SmallVector<Expr *, 8> Vars;
  10724. for (Expr *RefExpr : VarList) {
  10725. assert(RefExpr && "NULL expr in OpenMP linear clause.");
  10726. SourceLocation ELoc;
  10727. SourceRange ERange;
  10728. Expr *SimpleRefExpr = RefExpr;
  10729. auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
  10730. /*AllowArraySection=*/false);
  10731. if (Res.second) {
  10732. // It will be analyzed later.
  10733. Vars.push_back(RefExpr);
  10734. }
  10735. ValueDecl *D = Res.first;
  10736. if (!D)
  10737. continue;
  10738. QualType QType = D->getType();
  10739. auto *VD = dyn_cast<VarDecl>(D);
  10740. // OpenMP [2.8.1, simd construct, Restrictions]
  10741. // The type of list items appearing in the aligned clause must be
  10742. // array, pointer, reference to array, or reference to pointer.
  10743. QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
  10744. const Type *Ty = QType.getTypePtrOrNull();
  10745. if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
  10746. Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
  10747. << QType << getLangOpts().CPlusPlus << ERange;
  10748. bool IsDecl =
  10749. !VD ||
  10750. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  10751. Diag(D->getLocation(),
  10752. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  10753. << D;
  10754. continue;
  10755. }
  10756. // OpenMP [2.8.1, simd construct, Restrictions]
  10757. // A list-item cannot appear in more than one aligned clause.
  10758. if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
  10759. Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
  10760. Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
  10761. << getOpenMPClauseName(OMPC_aligned);
  10762. continue;
  10763. }
  10764. DeclRefExpr *Ref = nullptr;
  10765. if (!VD && isOpenMPCapturedDecl(D))
  10766. Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
  10767. Vars.push_back(DefaultFunctionArrayConversion(
  10768. (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
  10769. .get());
  10770. }
  10771. // OpenMP [2.8.1, simd construct, Description]
  10772. // The parameter of the aligned clause, alignment, must be a constant
  10773. // positive integer expression.
  10774. // If no optional parameter is specified, implementation-defined default
  10775. // alignments for SIMD instructions on the target platforms are assumed.
  10776. if (Alignment != nullptr) {
  10777. ExprResult AlignResult =
  10778. VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
  10779. if (AlignResult.isInvalid())
  10780. return nullptr;
  10781. Alignment = AlignResult.get();
  10782. }
  10783. if (Vars.empty())
  10784. return nullptr;
  10785. return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
  10786. EndLoc, Vars, Alignment);
  10787. }
  10788. OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
  10789. SourceLocation StartLoc,
  10790. SourceLocation LParenLoc,
  10791. SourceLocation EndLoc) {
  10792. SmallVector<Expr *, 8> Vars;
  10793. SmallVector<Expr *, 8> SrcExprs;
  10794. SmallVector<Expr *, 8> DstExprs;
  10795. SmallVector<Expr *, 8> AssignmentOps;
  10796. for (Expr *RefExpr : VarList) {
  10797. assert(RefExpr && "NULL expr in OpenMP copyin clause.");
  10798. if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
  10799. // It will be analyzed later.
  10800. Vars.push_back(RefExpr);
  10801. SrcExprs.push_back(nullptr);
  10802. DstExprs.push_back(nullptr);
  10803. AssignmentOps.push_back(nullptr);
  10804. continue;
  10805. }
  10806. SourceLocation ELoc = RefExpr->getExprLoc();
  10807. // OpenMP [2.1, C/C++]
  10808. // A list item is a variable name.
  10809. // OpenMP [2.14.4.1, Restrictions, p.1]
  10810. // A list item that appears in a copyin clause must be threadprivate.
  10811. auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
  10812. if (!DE || !isa<VarDecl>(DE->getDecl())) {
  10813. Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
  10814. << 0 << RefExpr->getSourceRange();
  10815. continue;
  10816. }
  10817. Decl *D = DE->getDecl();
  10818. auto *VD = cast<VarDecl>(D);
  10819. QualType Type = VD->getType();
  10820. if (Type->isDependentType() || Type->isInstantiationDependentType()) {
  10821. // It will be analyzed later.
  10822. Vars.push_back(DE);
  10823. SrcExprs.push_back(nullptr);
  10824. DstExprs.push_back(nullptr);
  10825. AssignmentOps.push_back(nullptr);
  10826. continue;
  10827. }
  10828. // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
  10829. // A list item that appears in a copyin clause must be threadprivate.
  10830. if (!DSAStack->isThreadPrivate(VD)) {
  10831. Diag(ELoc, diag::err_omp_required_access)
  10832. << getOpenMPClauseName(OMPC_copyin)
  10833. << getOpenMPDirectiveName(OMPD_threadprivate);
  10834. continue;
  10835. }
  10836. // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
  10837. // A variable of class type (or array thereof) that appears in a
  10838. // copyin clause requires an accessible, unambiguous copy assignment
  10839. // operator for the class type.
  10840. QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
  10841. VarDecl *SrcVD =
  10842. buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
  10843. ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
  10844. DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
  10845. *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
  10846. VarDecl *DstVD =
  10847. buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
  10848. VD->hasAttrs() ? &VD->getAttrs() : nullptr);
  10849. DeclRefExpr *PseudoDstExpr =
  10850. buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
  10851. // For arrays generate assignment operation for single element and replace
  10852. // it by the original array element in CodeGen.
  10853. ExprResult AssignmentOp =
  10854. BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
  10855. PseudoSrcExpr);
  10856. if (AssignmentOp.isInvalid())
  10857. continue;
  10858. AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
  10859. /*DiscardedValue=*/true);
  10860. if (AssignmentOp.isInvalid())
  10861. continue;
  10862. DSAStack->addDSA(VD, DE, OMPC_copyin);
  10863. Vars.push_back(DE);
  10864. SrcExprs.push_back(PseudoSrcExpr);
  10865. DstExprs.push_back(PseudoDstExpr);
  10866. AssignmentOps.push_back(AssignmentOp.get());
  10867. }
  10868. if (Vars.empty())
  10869. return nullptr;
  10870. return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
  10871. SrcExprs, DstExprs, AssignmentOps);
  10872. }
  10873. OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
  10874. SourceLocation StartLoc,
  10875. SourceLocation LParenLoc,
  10876. SourceLocation EndLoc) {
  10877. SmallVector<Expr *, 8> Vars;
  10878. SmallVector<Expr *, 8> SrcExprs;
  10879. SmallVector<Expr *, 8> DstExprs;
  10880. SmallVector<Expr *, 8> AssignmentOps;
  10881. for (Expr *RefExpr : VarList) {
  10882. assert(RefExpr && "NULL expr in OpenMP linear clause.");
  10883. SourceLocation ELoc;
  10884. SourceRange ERange;
  10885. Expr *SimpleRefExpr = RefExpr;
  10886. auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
  10887. /*AllowArraySection=*/false);
  10888. if (Res.second) {
  10889. // It will be analyzed later.
  10890. Vars.push_back(RefExpr);
  10891. SrcExprs.push_back(nullptr);
  10892. DstExprs.push_back(nullptr);
  10893. AssignmentOps.push_back(nullptr);
  10894. }
  10895. ValueDecl *D = Res.first;
  10896. if (!D)
  10897. continue;
  10898. QualType Type = D->getType();
  10899. auto *VD = dyn_cast<VarDecl>(D);
  10900. // OpenMP [2.14.4.2, Restrictions, p.2]
  10901. // A list item that appears in a copyprivate clause may not appear in a
  10902. // private or firstprivate clause on the single construct.
  10903. if (!VD || !DSAStack->isThreadPrivate(VD)) {
  10904. DSAStackTy::DSAVarData DVar =
  10905. DSAStack->getTopDSA(D, /*FromParent=*/false);
  10906. if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
  10907. DVar.RefExpr) {
  10908. Diag(ELoc, diag::err_omp_wrong_dsa)
  10909. << getOpenMPClauseName(DVar.CKind)
  10910. << getOpenMPClauseName(OMPC_copyprivate);
  10911. reportOriginalDsa(*this, DSAStack, D, DVar);
  10912. continue;
  10913. }
  10914. // OpenMP [2.11.4.2, Restrictions, p.1]
  10915. // All list items that appear in a copyprivate clause must be either
  10916. // threadprivate or private in the enclosing context.
  10917. if (DVar.CKind == OMPC_unknown) {
  10918. DVar = DSAStack->getImplicitDSA(D, false);
  10919. if (DVar.CKind == OMPC_shared) {
  10920. Diag(ELoc, diag::err_omp_required_access)
  10921. << getOpenMPClauseName(OMPC_copyprivate)
  10922. << "threadprivate or private in the enclosing context";
  10923. reportOriginalDsa(*this, DSAStack, D, DVar);
  10924. continue;
  10925. }
  10926. }
  10927. }
  10928. // Variably modified types are not supported.
  10929. if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
  10930. Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
  10931. << getOpenMPClauseName(OMPC_copyprivate) << Type
  10932. << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
  10933. bool IsDecl =
  10934. !VD ||
  10935. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  10936. Diag(D->getLocation(),
  10937. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  10938. << D;
  10939. continue;
  10940. }
  10941. // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
  10942. // A variable of class type (or array thereof) that appears in a
  10943. // copyin clause requires an accessible, unambiguous copy assignment
  10944. // operator for the class type.
  10945. Type = Context.getBaseElementType(Type.getNonReferenceType())
  10946. .getUnqualifiedType();
  10947. VarDecl *SrcVD =
  10948. buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
  10949. D->hasAttrs() ? &D->getAttrs() : nullptr);
  10950. DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
  10951. VarDecl *DstVD =
  10952. buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
  10953. D->hasAttrs() ? &D->getAttrs() : nullptr);
  10954. DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
  10955. ExprResult AssignmentOp = BuildBinOp(
  10956. DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
  10957. if (AssignmentOp.isInvalid())
  10958. continue;
  10959. AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
  10960. /*DiscardedValue=*/true);
  10961. if (AssignmentOp.isInvalid())
  10962. continue;
  10963. // No need to mark vars as copyprivate, they are already threadprivate or
  10964. // implicitly private.
  10965. assert(VD || isOpenMPCapturedDecl(D));
  10966. Vars.push_back(
  10967. VD ? RefExpr->IgnoreParens()
  10968. : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
  10969. SrcExprs.push_back(PseudoSrcExpr);
  10970. DstExprs.push_back(PseudoDstExpr);
  10971. AssignmentOps.push_back(AssignmentOp.get());
  10972. }
  10973. if (Vars.empty())
  10974. return nullptr;
  10975. return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
  10976. Vars, SrcExprs, DstExprs, AssignmentOps);
  10977. }
  10978. OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
  10979. SourceLocation StartLoc,
  10980. SourceLocation LParenLoc,
  10981. SourceLocation EndLoc) {
  10982. if (VarList.empty())
  10983. return nullptr;
  10984. return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
  10985. }
  10986. OMPClause *
  10987. Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
  10988. SourceLocation DepLoc, SourceLocation ColonLoc,
  10989. ArrayRef<Expr *> VarList, SourceLocation StartLoc,
  10990. SourceLocation LParenLoc, SourceLocation EndLoc) {
  10991. if (DSAStack->getCurrentDirective() == OMPD_ordered &&
  10992. DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
  10993. Diag(DepLoc, diag::err_omp_unexpected_clause_value)
  10994. << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
  10995. return nullptr;
  10996. }
  10997. if (DSAStack->getCurrentDirective() != OMPD_ordered &&
  10998. (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
  10999. DepKind == OMPC_DEPEND_sink)) {
  11000. unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
  11001. Diag(DepLoc, diag::err_omp_unexpected_clause_value)
  11002. << getListOfPossibleValues(OMPC_depend, /*First=*/0,
  11003. /*Last=*/OMPC_DEPEND_unknown, Except)
  11004. << getOpenMPClauseName(OMPC_depend);
  11005. return nullptr;
  11006. }
  11007. SmallVector<Expr *, 8> Vars;
  11008. DSAStackTy::OperatorOffsetTy OpsOffs;
  11009. llvm::APSInt DepCounter(/*BitWidth=*/32);
  11010. llvm::APSInt TotalDepCount(/*BitWidth=*/32);
  11011. if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
  11012. if (const Expr *OrderedCountExpr =
  11013. DSAStack->getParentOrderedRegionParam().first) {
  11014. TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
  11015. TotalDepCount.setIsUnsigned(/*Val=*/true);
  11016. }
  11017. }
  11018. for (Expr *RefExpr : VarList) {
  11019. assert(RefExpr && "NULL expr in OpenMP shared clause.");
  11020. if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
  11021. // It will be analyzed later.
  11022. Vars.push_back(RefExpr);
  11023. continue;
  11024. }
  11025. SourceLocation ELoc = RefExpr->getExprLoc();
  11026. Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
  11027. if (DepKind == OMPC_DEPEND_sink) {
  11028. if (DSAStack->getParentOrderedRegionParam().first &&
  11029. DepCounter >= TotalDepCount) {
  11030. Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
  11031. continue;
  11032. }
  11033. ++DepCounter;
  11034. // OpenMP [2.13.9, Summary]
  11035. // depend(dependence-type : vec), where dependence-type is:
  11036. // 'sink' and where vec is the iteration vector, which has the form:
  11037. // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
  11038. // where n is the value specified by the ordered clause in the loop
  11039. // directive, xi denotes the loop iteration variable of the i-th nested
  11040. // loop associated with the loop directive, and di is a constant
  11041. // non-negative integer.
  11042. if (CurContext->isDependentContext()) {
  11043. // It will be analyzed later.
  11044. Vars.push_back(RefExpr);
  11045. continue;
  11046. }
  11047. SimpleExpr = SimpleExpr->IgnoreImplicit();
  11048. OverloadedOperatorKind OOK = OO_None;
  11049. SourceLocation OOLoc;
  11050. Expr *LHS = SimpleExpr;
  11051. Expr *RHS = nullptr;
  11052. if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
  11053. OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
  11054. OOLoc = BO->getOperatorLoc();
  11055. LHS = BO->getLHS()->IgnoreParenImpCasts();
  11056. RHS = BO->getRHS()->IgnoreParenImpCasts();
  11057. } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
  11058. OOK = OCE->getOperator();
  11059. OOLoc = OCE->getOperatorLoc();
  11060. LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
  11061. RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
  11062. } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
  11063. OOK = MCE->getMethodDecl()
  11064. ->getNameInfo()
  11065. .getName()
  11066. .getCXXOverloadedOperator();
  11067. OOLoc = MCE->getCallee()->getExprLoc();
  11068. LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
  11069. RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
  11070. }
  11071. SourceLocation ELoc;
  11072. SourceRange ERange;
  11073. auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
  11074. /*AllowArraySection=*/false);
  11075. if (Res.second) {
  11076. // It will be analyzed later.
  11077. Vars.push_back(RefExpr);
  11078. }
  11079. ValueDecl *D = Res.first;
  11080. if (!D)
  11081. continue;
  11082. if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
  11083. Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
  11084. continue;
  11085. }
  11086. if (RHS) {
  11087. ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
  11088. RHS, OMPC_depend, /*StrictlyPositive=*/false);
  11089. if (RHSRes.isInvalid())
  11090. continue;
  11091. }
  11092. if (!CurContext->isDependentContext() &&
  11093. DSAStack->getParentOrderedRegionParam().first &&
  11094. DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
  11095. const ValueDecl *VD =
  11096. DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
  11097. if (VD)
  11098. Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
  11099. << 1 << VD;
  11100. else
  11101. Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
  11102. continue;
  11103. }
  11104. OpsOffs.emplace_back(RHS, OOK);
  11105. } else {
  11106. auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
  11107. if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
  11108. (ASE &&
  11109. !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
  11110. !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
  11111. Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
  11112. << RefExpr->getSourceRange();
  11113. continue;
  11114. }
  11115. bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
  11116. getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
  11117. ExprResult Res =
  11118. CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts());
  11119. getDiagnostics().setSuppressAllDiagnostics(Suppress);
  11120. if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
  11121. Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
  11122. << RefExpr->getSourceRange();
  11123. continue;
  11124. }
  11125. }
  11126. Vars.push_back(RefExpr->IgnoreParenImpCasts());
  11127. }
  11128. if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
  11129. TotalDepCount > VarList.size() &&
  11130. DSAStack->getParentOrderedRegionParam().first &&
  11131. DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
  11132. Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
  11133. << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
  11134. }
  11135. if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
  11136. Vars.empty())
  11137. return nullptr;
  11138. auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
  11139. DepKind, DepLoc, ColonLoc, Vars,
  11140. TotalDepCount.getZExtValue());
  11141. if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
  11142. DSAStack->isParentOrderedRegion())
  11143. DSAStack->addDoacrossDependClause(C, OpsOffs);
  11144. return C;
  11145. }
  11146. OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
  11147. SourceLocation LParenLoc,
  11148. SourceLocation EndLoc) {
  11149. Expr *ValExpr = Device;
  11150. Stmt *HelperValStmt = nullptr;
  11151. // OpenMP [2.9.1, Restrictions]
  11152. // The device expression must evaluate to a non-negative integer value.
  11153. if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
  11154. /*StrictlyPositive=*/false))
  11155. return nullptr;
  11156. OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
  11157. OpenMPDirectiveKind CaptureRegion =
  11158. getOpenMPCaptureRegionForClause(DKind, OMPC_device);
  11159. if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
  11160. ValExpr = MakeFullExpr(ValExpr).get();
  11161. llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
  11162. ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
  11163. HelperValStmt = buildPreInits(Context, Captures);
  11164. }
  11165. return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
  11166. StartLoc, LParenLoc, EndLoc);
  11167. }
  11168. static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
  11169. DSAStackTy *Stack, QualType QTy,
  11170. bool FullCheck = true) {
  11171. NamedDecl *ND;
  11172. if (QTy->isIncompleteType(&ND)) {
  11173. SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
  11174. return false;
  11175. }
  11176. if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
  11177. !QTy.isTrivialType(SemaRef.Context))
  11178. SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
  11179. return true;
  11180. }
  11181. /// Return true if it can be proven that the provided array expression
  11182. /// (array section or array subscript) does NOT specify the whole size of the
  11183. /// array whose base type is \a BaseQTy.
  11184. static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
  11185. const Expr *E,
  11186. QualType BaseQTy) {
  11187. const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
  11188. // If this is an array subscript, it refers to the whole size if the size of
  11189. // the dimension is constant and equals 1. Also, an array section assumes the
  11190. // format of an array subscript if no colon is used.
  11191. if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
  11192. if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
  11193. return ATy->getSize().getSExtValue() != 1;
  11194. // Size can't be evaluated statically.
  11195. return false;
  11196. }
  11197. assert(OASE && "Expecting array section if not an array subscript.");
  11198. const Expr *LowerBound = OASE->getLowerBound();
  11199. const Expr *Length = OASE->getLength();
  11200. // If there is a lower bound that does not evaluates to zero, we are not
  11201. // covering the whole dimension.
  11202. if (LowerBound) {
  11203. llvm::APSInt ConstLowerBound;
  11204. if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
  11205. return false; // Can't get the integer value as a constant.
  11206. if (ConstLowerBound.getSExtValue())
  11207. return true;
  11208. }
  11209. // If we don't have a length we covering the whole dimension.
  11210. if (!Length)
  11211. return false;
  11212. // If the base is a pointer, we don't have a way to get the size of the
  11213. // pointee.
  11214. if (BaseQTy->isPointerType())
  11215. return false;
  11216. // We can only check if the length is the same as the size of the dimension
  11217. // if we have a constant array.
  11218. const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
  11219. if (!CATy)
  11220. return false;
  11221. llvm::APSInt ConstLength;
  11222. if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
  11223. return false; // Can't get the integer value as a constant.
  11224. return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
  11225. }
  11226. // Return true if it can be proven that the provided array expression (array
  11227. // section or array subscript) does NOT specify a single element of the array
  11228. // whose base type is \a BaseQTy.
  11229. static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
  11230. const Expr *E,
  11231. QualType BaseQTy) {
  11232. const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
  11233. // An array subscript always refer to a single element. Also, an array section
  11234. // assumes the format of an array subscript if no colon is used.
  11235. if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
  11236. return false;
  11237. assert(OASE && "Expecting array section if not an array subscript.");
  11238. const Expr *Length = OASE->getLength();
  11239. // If we don't have a length we have to check if the array has unitary size
  11240. // for this dimension. Also, we should always expect a length if the base type
  11241. // is pointer.
  11242. if (!Length) {
  11243. if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
  11244. return ATy->getSize().getSExtValue() != 1;
  11245. // We cannot assume anything.
  11246. return false;
  11247. }
  11248. // Check if the length evaluates to 1.
  11249. llvm::APSInt ConstLength;
  11250. if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
  11251. return false; // Can't get the integer value as a constant.
  11252. return ConstLength.getSExtValue() != 1;
  11253. }
  11254. // Return the expression of the base of the mappable expression or null if it
  11255. // cannot be determined and do all the necessary checks to see if the expression
  11256. // is valid as a standalone mappable expression. In the process, record all the
  11257. // components of the expression.
  11258. static const Expr *checkMapClauseExpressionBase(
  11259. Sema &SemaRef, Expr *E,
  11260. OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
  11261. OpenMPClauseKind CKind, bool NoDiagnose) {
  11262. SourceLocation ELoc = E->getExprLoc();
  11263. SourceRange ERange = E->getSourceRange();
  11264. // The base of elements of list in a map clause have to be either:
  11265. // - a reference to variable or field.
  11266. // - a member expression.
  11267. // - an array expression.
  11268. //
  11269. // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
  11270. // reference to 'r'.
  11271. //
  11272. // If we have:
  11273. //
  11274. // struct SS {
  11275. // Bla S;
  11276. // foo() {
  11277. // #pragma omp target map (S.Arr[:12]);
  11278. // }
  11279. // }
  11280. //
  11281. // We want to retrieve the member expression 'this->S';
  11282. const Expr *RelevantExpr = nullptr;
  11283. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
  11284. // If a list item is an array section, it must specify contiguous storage.
  11285. //
  11286. // For this restriction it is sufficient that we make sure only references
  11287. // to variables or fields and array expressions, and that no array sections
  11288. // exist except in the rightmost expression (unless they cover the whole
  11289. // dimension of the array). E.g. these would be invalid:
  11290. //
  11291. // r.ArrS[3:5].Arr[6:7]
  11292. //
  11293. // r.ArrS[3:5].x
  11294. //
  11295. // but these would be valid:
  11296. // r.ArrS[3].Arr[6:7]
  11297. //
  11298. // r.ArrS[3].x
  11299. bool AllowUnitySizeArraySection = true;
  11300. bool AllowWholeSizeArraySection = true;
  11301. while (!RelevantExpr) {
  11302. E = E->IgnoreParenImpCasts();
  11303. if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
  11304. if (!isa<VarDecl>(CurE->getDecl()))
  11305. return nullptr;
  11306. RelevantExpr = CurE;
  11307. // If we got a reference to a declaration, we should not expect any array
  11308. // section before that.
  11309. AllowUnitySizeArraySection = false;
  11310. AllowWholeSizeArraySection = false;
  11311. // Record the component.
  11312. CurComponents.emplace_back(CurE, CurE->getDecl());
  11313. } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
  11314. Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
  11315. if (isa<CXXThisExpr>(BaseE))
  11316. // We found a base expression: this->Val.
  11317. RelevantExpr = CurE;
  11318. else
  11319. E = BaseE;
  11320. if (!isa<FieldDecl>(CurE->getMemberDecl())) {
  11321. if (!NoDiagnose) {
  11322. SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
  11323. << CurE->getSourceRange();
  11324. return nullptr;
  11325. }
  11326. if (RelevantExpr)
  11327. return nullptr;
  11328. continue;
  11329. }
  11330. auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
  11331. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
  11332. // A bit-field cannot appear in a map clause.
  11333. //
  11334. if (FD->isBitField()) {
  11335. if (!NoDiagnose) {
  11336. SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
  11337. << CurE->getSourceRange() << getOpenMPClauseName(CKind);
  11338. return nullptr;
  11339. }
  11340. if (RelevantExpr)
  11341. return nullptr;
  11342. continue;
  11343. }
  11344. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
  11345. // If the type of a list item is a reference to a type T then the type
  11346. // will be considered to be T for all purposes of this clause.
  11347. QualType CurType = BaseE->getType().getNonReferenceType();
  11348. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
  11349. // A list item cannot be a variable that is a member of a structure with
  11350. // a union type.
  11351. //
  11352. if (CurType->isUnionType()) {
  11353. if (!NoDiagnose) {
  11354. SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
  11355. << CurE->getSourceRange();
  11356. return nullptr;
  11357. }
  11358. continue;
  11359. }
  11360. // If we got a member expression, we should not expect any array section
  11361. // before that:
  11362. //
  11363. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
  11364. // If a list item is an element of a structure, only the rightmost symbol
  11365. // of the variable reference can be an array section.
  11366. //
  11367. AllowUnitySizeArraySection = false;
  11368. AllowWholeSizeArraySection = false;
  11369. // Record the component.
  11370. CurComponents.emplace_back(CurE, FD);
  11371. } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
  11372. E = CurE->getBase()->IgnoreParenImpCasts();
  11373. if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
  11374. if (!NoDiagnose) {
  11375. SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
  11376. << 0 << CurE->getSourceRange();
  11377. return nullptr;
  11378. }
  11379. continue;
  11380. }
  11381. // If we got an array subscript that express the whole dimension we
  11382. // can have any array expressions before. If it only expressing part of
  11383. // the dimension, we can only have unitary-size array expressions.
  11384. if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
  11385. E->getType()))
  11386. AllowWholeSizeArraySection = false;
  11387. // Record the component - we don't have any declaration associated.
  11388. CurComponents.emplace_back(CurE, nullptr);
  11389. } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
  11390. assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
  11391. E = CurE->getBase()->IgnoreParenImpCasts();
  11392. QualType CurType =
  11393. OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
  11394. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
  11395. // If the type of a list item is a reference to a type T then the type
  11396. // will be considered to be T for all purposes of this clause.
  11397. if (CurType->isReferenceType())
  11398. CurType = CurType->getPointeeType();
  11399. bool IsPointer = CurType->isAnyPointerType();
  11400. if (!IsPointer && !CurType->isArrayType()) {
  11401. SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
  11402. << 0 << CurE->getSourceRange();
  11403. return nullptr;
  11404. }
  11405. bool NotWhole =
  11406. checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
  11407. bool NotUnity =
  11408. checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
  11409. if (AllowWholeSizeArraySection) {
  11410. // Any array section is currently allowed. Allowing a whole size array
  11411. // section implies allowing a unity array section as well.
  11412. //
  11413. // If this array section refers to the whole dimension we can still
  11414. // accept other array sections before this one, except if the base is a
  11415. // pointer. Otherwise, only unitary sections are accepted.
  11416. if (NotWhole || IsPointer)
  11417. AllowWholeSizeArraySection = false;
  11418. } else if (AllowUnitySizeArraySection && NotUnity) {
  11419. // A unity or whole array section is not allowed and that is not
  11420. // compatible with the properties of the current array section.
  11421. SemaRef.Diag(
  11422. ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
  11423. << CurE->getSourceRange();
  11424. return nullptr;
  11425. }
  11426. // Record the component - we don't have any declaration associated.
  11427. CurComponents.emplace_back(CurE, nullptr);
  11428. } else {
  11429. if (!NoDiagnose) {
  11430. // If nothing else worked, this is not a valid map clause expression.
  11431. SemaRef.Diag(
  11432. ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
  11433. << ERange;
  11434. }
  11435. return nullptr;
  11436. }
  11437. }
  11438. return RelevantExpr;
  11439. }
  11440. // Return true if expression E associated with value VD has conflicts with other
  11441. // map information.
  11442. static bool checkMapConflicts(
  11443. Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
  11444. bool CurrentRegionOnly,
  11445. OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
  11446. OpenMPClauseKind CKind) {
  11447. assert(VD && E);
  11448. SourceLocation ELoc = E->getExprLoc();
  11449. SourceRange ERange = E->getSourceRange();
  11450. // In order to easily check the conflicts we need to match each component of
  11451. // the expression under test with the components of the expressions that are
  11452. // already in the stack.
  11453. assert(!CurComponents.empty() && "Map clause expression with no components!");
  11454. assert(CurComponents.back().getAssociatedDeclaration() == VD &&
  11455. "Map clause expression with unexpected base!");
  11456. // Variables to help detecting enclosing problems in data environment nests.
  11457. bool IsEnclosedByDataEnvironmentExpr = false;
  11458. const Expr *EnclosingExpr = nullptr;
  11459. bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
  11460. VD, CurrentRegionOnly,
  11461. [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
  11462. ERange, CKind, &EnclosingExpr,
  11463. CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
  11464. StackComponents,
  11465. OpenMPClauseKind) {
  11466. assert(!StackComponents.empty() &&
  11467. "Map clause expression with no components!");
  11468. assert(StackComponents.back().getAssociatedDeclaration() == VD &&
  11469. "Map clause expression with unexpected base!");
  11470. (void)VD;
  11471. // The whole expression in the stack.
  11472. const Expr *RE = StackComponents.front().getAssociatedExpression();
  11473. // Expressions must start from the same base. Here we detect at which
  11474. // point both expressions diverge from each other and see if we can
  11475. // detect if the memory referred to both expressions is contiguous and
  11476. // do not overlap.
  11477. auto CI = CurComponents.rbegin();
  11478. auto CE = CurComponents.rend();
  11479. auto SI = StackComponents.rbegin();
  11480. auto SE = StackComponents.rend();
  11481. for (; CI != CE && SI != SE; ++CI, ++SI) {
  11482. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
  11483. // At most one list item can be an array item derived from a given
  11484. // variable in map clauses of the same construct.
  11485. if (CurrentRegionOnly &&
  11486. (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
  11487. isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
  11488. (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
  11489. isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
  11490. SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
  11491. diag::err_omp_multiple_array_items_in_map_clause)
  11492. << CI->getAssociatedExpression()->getSourceRange();
  11493. SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
  11494. diag::note_used_here)
  11495. << SI->getAssociatedExpression()->getSourceRange();
  11496. return true;
  11497. }
  11498. // Do both expressions have the same kind?
  11499. if (CI->getAssociatedExpression()->getStmtClass() !=
  11500. SI->getAssociatedExpression()->getStmtClass())
  11501. break;
  11502. // Are we dealing with different variables/fields?
  11503. if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
  11504. break;
  11505. }
  11506. // Check if the extra components of the expressions in the enclosing
  11507. // data environment are redundant for the current base declaration.
  11508. // If they are, the maps completely overlap, which is legal.
  11509. for (; SI != SE; ++SI) {
  11510. QualType Type;
  11511. if (const auto *ASE =
  11512. dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
  11513. Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
  11514. } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
  11515. SI->getAssociatedExpression())) {
  11516. const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
  11517. Type =
  11518. OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
  11519. }
  11520. if (Type.isNull() || Type->isAnyPointerType() ||
  11521. checkArrayExpressionDoesNotReferToWholeSize(
  11522. SemaRef, SI->getAssociatedExpression(), Type))
  11523. break;
  11524. }
  11525. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
  11526. // List items of map clauses in the same construct must not share
  11527. // original storage.
  11528. //
  11529. // If the expressions are exactly the same or one is a subset of the
  11530. // other, it means they are sharing storage.
  11531. if (CI == CE && SI == SE) {
  11532. if (CurrentRegionOnly) {
  11533. if (CKind == OMPC_map) {
  11534. SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
  11535. } else {
  11536. assert(CKind == OMPC_to || CKind == OMPC_from);
  11537. SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
  11538. << ERange;
  11539. }
  11540. SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
  11541. << RE->getSourceRange();
  11542. return true;
  11543. }
  11544. // If we find the same expression in the enclosing data environment,
  11545. // that is legal.
  11546. IsEnclosedByDataEnvironmentExpr = true;
  11547. return false;
  11548. }
  11549. QualType DerivedType =
  11550. std::prev(CI)->getAssociatedDeclaration()->getType();
  11551. SourceLocation DerivedLoc =
  11552. std::prev(CI)->getAssociatedExpression()->getExprLoc();
  11553. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
  11554. // If the type of a list item is a reference to a type T then the type
  11555. // will be considered to be T for all purposes of this clause.
  11556. DerivedType = DerivedType.getNonReferenceType();
  11557. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
  11558. // A variable for which the type is pointer and an array section
  11559. // derived from that variable must not appear as list items of map
  11560. // clauses of the same construct.
  11561. //
  11562. // Also, cover one of the cases in:
  11563. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
  11564. // If any part of the original storage of a list item has corresponding
  11565. // storage in the device data environment, all of the original storage
  11566. // must have corresponding storage in the device data environment.
  11567. //
  11568. if (DerivedType->isAnyPointerType()) {
  11569. if (CI == CE || SI == SE) {
  11570. SemaRef.Diag(
  11571. DerivedLoc,
  11572. diag::err_omp_pointer_mapped_along_with_derived_section)
  11573. << DerivedLoc;
  11574. SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
  11575. << RE->getSourceRange();
  11576. return true;
  11577. }
  11578. if (CI->getAssociatedExpression()->getStmtClass() !=
  11579. SI->getAssociatedExpression()->getStmtClass() ||
  11580. CI->getAssociatedDeclaration()->getCanonicalDecl() ==
  11581. SI->getAssociatedDeclaration()->getCanonicalDecl()) {
  11582. assert(CI != CE && SI != SE);
  11583. SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
  11584. << DerivedLoc;
  11585. SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
  11586. << RE->getSourceRange();
  11587. return true;
  11588. }
  11589. }
  11590. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
  11591. // List items of map clauses in the same construct must not share
  11592. // original storage.
  11593. //
  11594. // An expression is a subset of the other.
  11595. if (CurrentRegionOnly && (CI == CE || SI == SE)) {
  11596. if (CKind == OMPC_map) {
  11597. if (CI != CE || SI != SE) {
  11598. // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
  11599. // a pointer.
  11600. auto Begin =
  11601. CI != CE ? CurComponents.begin() : StackComponents.begin();
  11602. auto End = CI != CE ? CurComponents.end() : StackComponents.end();
  11603. auto It = Begin;
  11604. while (It != End && !It->getAssociatedDeclaration())
  11605. std::advance(It, 1);
  11606. assert(It != End &&
  11607. "Expected at least one component with the declaration.");
  11608. if (It != Begin && It->getAssociatedDeclaration()
  11609. ->getType()
  11610. .getCanonicalType()
  11611. ->isAnyPointerType()) {
  11612. IsEnclosedByDataEnvironmentExpr = false;
  11613. EnclosingExpr = nullptr;
  11614. return false;
  11615. }
  11616. }
  11617. SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
  11618. } else {
  11619. assert(CKind == OMPC_to || CKind == OMPC_from);
  11620. SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
  11621. << ERange;
  11622. }
  11623. SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
  11624. << RE->getSourceRange();
  11625. return true;
  11626. }
  11627. // The current expression uses the same base as other expression in the
  11628. // data environment but does not contain it completely.
  11629. if (!CurrentRegionOnly && SI != SE)
  11630. EnclosingExpr = RE;
  11631. // The current expression is a subset of the expression in the data
  11632. // environment.
  11633. IsEnclosedByDataEnvironmentExpr |=
  11634. (!CurrentRegionOnly && CI != CE && SI == SE);
  11635. return false;
  11636. });
  11637. if (CurrentRegionOnly)
  11638. return FoundError;
  11639. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
  11640. // If any part of the original storage of a list item has corresponding
  11641. // storage in the device data environment, all of the original storage must
  11642. // have corresponding storage in the device data environment.
  11643. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
  11644. // If a list item is an element of a structure, and a different element of
  11645. // the structure has a corresponding list item in the device data environment
  11646. // prior to a task encountering the construct associated with the map clause,
  11647. // then the list item must also have a corresponding list item in the device
  11648. // data environment prior to the task encountering the construct.
  11649. //
  11650. if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
  11651. SemaRef.Diag(ELoc,
  11652. diag::err_omp_original_storage_is_shared_and_does_not_contain)
  11653. << ERange;
  11654. SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
  11655. << EnclosingExpr->getSourceRange();
  11656. return true;
  11657. }
  11658. return FoundError;
  11659. }
  11660. namespace {
  11661. // Utility struct that gathers all the related lists associated with a mappable
  11662. // expression.
  11663. struct MappableVarListInfo {
  11664. // The list of expressions.
  11665. ArrayRef<Expr *> VarList;
  11666. // The list of processed expressions.
  11667. SmallVector<Expr *, 16> ProcessedVarList;
  11668. // The mappble components for each expression.
  11669. OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
  11670. // The base declaration of the variable.
  11671. SmallVector<ValueDecl *, 16> VarBaseDeclarations;
  11672. MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
  11673. // We have a list of components and base declarations for each entry in the
  11674. // variable list.
  11675. VarComponents.reserve(VarList.size());
  11676. VarBaseDeclarations.reserve(VarList.size());
  11677. }
  11678. };
  11679. }
  11680. // Check the validity of the provided variable list for the provided clause kind
  11681. // \a CKind. In the check process the valid expressions, and mappable expression
  11682. // components and variables are extracted and used to fill \a Vars,
  11683. // \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
  11684. // \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
  11685. static void
  11686. checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
  11687. OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
  11688. SourceLocation StartLoc,
  11689. OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
  11690. bool IsMapTypeImplicit = false) {
  11691. // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
  11692. assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
  11693. "Unexpected clause kind with mappable expressions!");
  11694. // Keep track of the mappable components and base declarations in this clause.
  11695. // Each entry in the list is going to have a list of components associated. We
  11696. // record each set of the components so that we can build the clause later on.
  11697. // In the end we should have the same amount of declarations and component
  11698. // lists.
  11699. for (Expr *RE : MVLI.VarList) {
  11700. assert(RE && "Null expr in omp to/from/map clause");
  11701. SourceLocation ELoc = RE->getExprLoc();
  11702. const Expr *VE = RE->IgnoreParenLValueCasts();
  11703. if (VE->isValueDependent() || VE->isTypeDependent() ||
  11704. VE->isInstantiationDependent() ||
  11705. VE->containsUnexpandedParameterPack()) {
  11706. // We can only analyze this information once the missing information is
  11707. // resolved.
  11708. MVLI.ProcessedVarList.push_back(RE);
  11709. continue;
  11710. }
  11711. Expr *SimpleExpr = RE->IgnoreParenCasts();
  11712. if (!RE->IgnoreParenImpCasts()->isLValue()) {
  11713. SemaRef.Diag(ELoc,
  11714. diag::err_omp_expected_named_var_member_or_array_expression)
  11715. << RE->getSourceRange();
  11716. continue;
  11717. }
  11718. OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
  11719. ValueDecl *CurDeclaration = nullptr;
  11720. // Obtain the array or member expression bases if required. Also, fill the
  11721. // components array with all the components identified in the process.
  11722. const Expr *BE = checkMapClauseExpressionBase(
  11723. SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
  11724. if (!BE)
  11725. continue;
  11726. assert(!CurComponents.empty() &&
  11727. "Invalid mappable expression information.");
  11728. // For the following checks, we rely on the base declaration which is
  11729. // expected to be associated with the last component. The declaration is
  11730. // expected to be a variable or a field (if 'this' is being mapped).
  11731. CurDeclaration = CurComponents.back().getAssociatedDeclaration();
  11732. assert(CurDeclaration && "Null decl on map clause.");
  11733. assert(
  11734. CurDeclaration->isCanonicalDecl() &&
  11735. "Expecting components to have associated only canonical declarations.");
  11736. auto *VD = dyn_cast<VarDecl>(CurDeclaration);
  11737. const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
  11738. assert((VD || FD) && "Only variables or fields are expected here!");
  11739. (void)FD;
  11740. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
  11741. // threadprivate variables cannot appear in a map clause.
  11742. // OpenMP 4.5 [2.10.5, target update Construct]
  11743. // threadprivate variables cannot appear in a from clause.
  11744. if (VD && DSAS->isThreadPrivate(VD)) {
  11745. DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
  11746. SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
  11747. << getOpenMPClauseName(CKind);
  11748. reportOriginalDsa(SemaRef, DSAS, VD, DVar);
  11749. continue;
  11750. }
  11751. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
  11752. // A list item cannot appear in both a map clause and a data-sharing
  11753. // attribute clause on the same construct.
  11754. // Check conflicts with other map clause expressions. We check the conflicts
  11755. // with the current construct separately from the enclosing data
  11756. // environment, because the restrictions are different. We only have to
  11757. // check conflicts across regions for the map clauses.
  11758. if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
  11759. /*CurrentRegionOnly=*/true, CurComponents, CKind))
  11760. break;
  11761. if (CKind == OMPC_map &&
  11762. checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
  11763. /*CurrentRegionOnly=*/false, CurComponents, CKind))
  11764. break;
  11765. // OpenMP 4.5 [2.10.5, target update Construct]
  11766. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
  11767. // If the type of a list item is a reference to a type T then the type will
  11768. // be considered to be T for all purposes of this clause.
  11769. auto I = llvm::find_if(
  11770. CurComponents,
  11771. [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
  11772. return MC.getAssociatedDeclaration();
  11773. });
  11774. assert(I != CurComponents.end() && "Null decl on map clause.");
  11775. QualType Type =
  11776. I->getAssociatedDeclaration()->getType().getNonReferenceType();
  11777. // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
  11778. // A list item in a to or from clause must have a mappable type.
  11779. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
  11780. // A list item must have a mappable type.
  11781. if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
  11782. DSAS, Type))
  11783. continue;
  11784. if (CKind == OMPC_map) {
  11785. // target enter data
  11786. // OpenMP [2.10.2, Restrictions, p. 99]
  11787. // A map-type must be specified in all map clauses and must be either
  11788. // to or alloc.
  11789. OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
  11790. if (DKind == OMPD_target_enter_data &&
  11791. !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
  11792. SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
  11793. << (IsMapTypeImplicit ? 1 : 0)
  11794. << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
  11795. << getOpenMPDirectiveName(DKind);
  11796. continue;
  11797. }
  11798. // target exit_data
  11799. // OpenMP [2.10.3, Restrictions, p. 102]
  11800. // A map-type must be specified in all map clauses and must be either
  11801. // from, release, or delete.
  11802. if (DKind == OMPD_target_exit_data &&
  11803. !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
  11804. MapType == OMPC_MAP_delete)) {
  11805. SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
  11806. << (IsMapTypeImplicit ? 1 : 0)
  11807. << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
  11808. << getOpenMPDirectiveName(DKind);
  11809. continue;
  11810. }
  11811. // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
  11812. // A list item cannot appear in both a map clause and a data-sharing
  11813. // attribute clause on the same construct
  11814. if (VD && isOpenMPTargetExecutionDirective(DKind)) {
  11815. DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
  11816. if (isOpenMPPrivate(DVar.CKind)) {
  11817. SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
  11818. << getOpenMPClauseName(DVar.CKind)
  11819. << getOpenMPClauseName(OMPC_map)
  11820. << getOpenMPDirectiveName(DSAS->getCurrentDirective());
  11821. reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
  11822. continue;
  11823. }
  11824. }
  11825. }
  11826. // Save the current expression.
  11827. MVLI.ProcessedVarList.push_back(RE);
  11828. // Store the components in the stack so that they can be used to check
  11829. // against other clauses later on.
  11830. DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
  11831. /*WhereFoundClauseKind=*/OMPC_map);
  11832. // Save the components and declaration to create the clause. For purposes of
  11833. // the clause creation, any component list that has has base 'this' uses
  11834. // null as base declaration.
  11835. MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
  11836. MVLI.VarComponents.back().append(CurComponents.begin(),
  11837. CurComponents.end());
  11838. MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
  11839. : CurDeclaration);
  11840. }
  11841. }
  11842. OMPClause *
  11843. Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
  11844. OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
  11845. SourceLocation MapLoc, SourceLocation ColonLoc,
  11846. ArrayRef<Expr *> VarList, SourceLocation StartLoc,
  11847. SourceLocation LParenLoc, SourceLocation EndLoc) {
  11848. MappableVarListInfo MVLI(VarList);
  11849. checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
  11850. MapType, IsMapTypeImplicit);
  11851. // We need to produce a map clause even if we don't have variables so that
  11852. // other diagnostics related with non-existing map clauses are accurate.
  11853. return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
  11854. MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
  11855. MVLI.VarComponents, MapTypeModifier, MapType,
  11856. IsMapTypeImplicit, MapLoc);
  11857. }
  11858. QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
  11859. TypeResult ParsedType) {
  11860. assert(ParsedType.isUsable());
  11861. QualType ReductionType = GetTypeFromParser(ParsedType.get());
  11862. if (ReductionType.isNull())
  11863. return QualType();
  11864. // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
  11865. // A type name in a declare reduction directive cannot be a function type, an
  11866. // array type, a reference type, or a type qualified with const, volatile or
  11867. // restrict.
  11868. if (ReductionType.hasQualifiers()) {
  11869. Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
  11870. return QualType();
  11871. }
  11872. if (ReductionType->isFunctionType()) {
  11873. Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
  11874. return QualType();
  11875. }
  11876. if (ReductionType->isReferenceType()) {
  11877. Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
  11878. return QualType();
  11879. }
  11880. if (ReductionType->isArrayType()) {
  11881. Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
  11882. return QualType();
  11883. }
  11884. return ReductionType;
  11885. }
  11886. Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
  11887. Scope *S, DeclContext *DC, DeclarationName Name,
  11888. ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
  11889. AccessSpecifier AS, Decl *PrevDeclInScope) {
  11890. SmallVector<Decl *, 8> Decls;
  11891. Decls.reserve(ReductionTypes.size());
  11892. LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
  11893. forRedeclarationInCurContext());
  11894. // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
  11895. // A reduction-identifier may not be re-declared in the current scope for the
  11896. // same type or for a type that is compatible according to the base language
  11897. // rules.
  11898. llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
  11899. OMPDeclareReductionDecl *PrevDRD = nullptr;
  11900. bool InCompoundScope = true;
  11901. if (S != nullptr) {
  11902. // Find previous declaration with the same name not referenced in other
  11903. // declarations.
  11904. FunctionScopeInfo *ParentFn = getEnclosingFunction();
  11905. InCompoundScope =
  11906. (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
  11907. LookupName(Lookup, S);
  11908. FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
  11909. /*AllowInlineNamespace=*/false);
  11910. llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
  11911. LookupResult::Filter Filter = Lookup.makeFilter();
  11912. while (Filter.hasNext()) {
  11913. auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
  11914. if (InCompoundScope) {
  11915. auto I = UsedAsPrevious.find(PrevDecl);
  11916. if (I == UsedAsPrevious.end())
  11917. UsedAsPrevious[PrevDecl] = false;
  11918. if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
  11919. UsedAsPrevious[D] = true;
  11920. }
  11921. PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
  11922. PrevDecl->getLocation();
  11923. }
  11924. Filter.done();
  11925. if (InCompoundScope) {
  11926. for (const auto &PrevData : UsedAsPrevious) {
  11927. if (!PrevData.second) {
  11928. PrevDRD = PrevData.first;
  11929. break;
  11930. }
  11931. }
  11932. }
  11933. } else if (PrevDeclInScope != nullptr) {
  11934. auto *PrevDRDInScope = PrevDRD =
  11935. cast<OMPDeclareReductionDecl>(PrevDeclInScope);
  11936. do {
  11937. PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
  11938. PrevDRDInScope->getLocation();
  11939. PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
  11940. } while (PrevDRDInScope != nullptr);
  11941. }
  11942. for (const auto &TyData : ReductionTypes) {
  11943. const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
  11944. bool Invalid = false;
  11945. if (I != PreviousRedeclTypes.end()) {
  11946. Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
  11947. << TyData.first;
  11948. Diag(I->second, diag::note_previous_definition);
  11949. Invalid = true;
  11950. }
  11951. PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
  11952. auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
  11953. Name, TyData.first, PrevDRD);
  11954. DC->addDecl(DRD);
  11955. DRD->setAccess(AS);
  11956. Decls.push_back(DRD);
  11957. if (Invalid)
  11958. DRD->setInvalidDecl();
  11959. else
  11960. PrevDRD = DRD;
  11961. }
  11962. return DeclGroupPtrTy::make(
  11963. DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
  11964. }
  11965. void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
  11966. auto *DRD = cast<OMPDeclareReductionDecl>(D);
  11967. // Enter new function scope.
  11968. PushFunctionScope();
  11969. setFunctionHasBranchProtectedScope();
  11970. getCurFunction()->setHasOMPDeclareReductionCombiner();
  11971. if (S != nullptr)
  11972. PushDeclContext(S, DRD);
  11973. else
  11974. CurContext = DRD;
  11975. PushExpressionEvaluationContext(
  11976. ExpressionEvaluationContext::PotentiallyEvaluated);
  11977. QualType ReductionType = DRD->getType();
  11978. // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
  11979. // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
  11980. // uses semantics of argument handles by value, but it should be passed by
  11981. // reference. C lang does not support references, so pass all parameters as
  11982. // pointers.
  11983. // Create 'T omp_in;' variable.
  11984. VarDecl *OmpInParm =
  11985. buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
  11986. // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
  11987. // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
  11988. // uses semantics of argument handles by value, but it should be passed by
  11989. // reference. C lang does not support references, so pass all parameters as
  11990. // pointers.
  11991. // Create 'T omp_out;' variable.
  11992. VarDecl *OmpOutParm =
  11993. buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
  11994. if (S != nullptr) {
  11995. PushOnScopeChains(OmpInParm, S);
  11996. PushOnScopeChains(OmpOutParm, S);
  11997. } else {
  11998. DRD->addDecl(OmpInParm);
  11999. DRD->addDecl(OmpOutParm);
  12000. }
  12001. Expr *InE =
  12002. ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
  12003. Expr *OutE =
  12004. ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
  12005. DRD->setCombinerData(InE, OutE);
  12006. }
  12007. void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
  12008. auto *DRD = cast<OMPDeclareReductionDecl>(D);
  12009. DiscardCleanupsInEvaluationContext();
  12010. PopExpressionEvaluationContext();
  12011. PopDeclContext();
  12012. PopFunctionScopeInfo();
  12013. if (Combiner != nullptr)
  12014. DRD->setCombiner(Combiner);
  12015. else
  12016. DRD->setInvalidDecl();
  12017. }
  12018. VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
  12019. auto *DRD = cast<OMPDeclareReductionDecl>(D);
  12020. // Enter new function scope.
  12021. PushFunctionScope();
  12022. setFunctionHasBranchProtectedScope();
  12023. if (S != nullptr)
  12024. PushDeclContext(S, DRD);
  12025. else
  12026. CurContext = DRD;
  12027. PushExpressionEvaluationContext(
  12028. ExpressionEvaluationContext::PotentiallyEvaluated);
  12029. QualType ReductionType = DRD->getType();
  12030. // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
  12031. // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
  12032. // uses semantics of argument handles by value, but it should be passed by
  12033. // reference. C lang does not support references, so pass all parameters as
  12034. // pointers.
  12035. // Create 'T omp_priv;' variable.
  12036. VarDecl *OmpPrivParm =
  12037. buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
  12038. // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
  12039. // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
  12040. // uses semantics of argument handles by value, but it should be passed by
  12041. // reference. C lang does not support references, so pass all parameters as
  12042. // pointers.
  12043. // Create 'T omp_orig;' variable.
  12044. VarDecl *OmpOrigParm =
  12045. buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
  12046. if (S != nullptr) {
  12047. PushOnScopeChains(OmpPrivParm, S);
  12048. PushOnScopeChains(OmpOrigParm, S);
  12049. } else {
  12050. DRD->addDecl(OmpPrivParm);
  12051. DRD->addDecl(OmpOrigParm);
  12052. }
  12053. Expr *OrigE =
  12054. ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
  12055. Expr *PrivE =
  12056. ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
  12057. DRD->setInitializerData(OrigE, PrivE);
  12058. return OmpPrivParm;
  12059. }
  12060. void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
  12061. VarDecl *OmpPrivParm) {
  12062. auto *DRD = cast<OMPDeclareReductionDecl>(D);
  12063. DiscardCleanupsInEvaluationContext();
  12064. PopExpressionEvaluationContext();
  12065. PopDeclContext();
  12066. PopFunctionScopeInfo();
  12067. if (Initializer != nullptr) {
  12068. DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
  12069. } else if (OmpPrivParm->hasInit()) {
  12070. DRD->setInitializer(OmpPrivParm->getInit(),
  12071. OmpPrivParm->isDirectInit()
  12072. ? OMPDeclareReductionDecl::DirectInit
  12073. : OMPDeclareReductionDecl::CopyInit);
  12074. } else {
  12075. DRD->setInvalidDecl();
  12076. }
  12077. }
  12078. Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
  12079. Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
  12080. for (Decl *D : DeclReductions.get()) {
  12081. if (IsValid) {
  12082. if (S)
  12083. PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
  12084. /*AddToContext=*/false);
  12085. } else {
  12086. D->setInvalidDecl();
  12087. }
  12088. }
  12089. return DeclReductions;
  12090. }
  12091. OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
  12092. SourceLocation StartLoc,
  12093. SourceLocation LParenLoc,
  12094. SourceLocation EndLoc) {
  12095. Expr *ValExpr = NumTeams;
  12096. Stmt *HelperValStmt = nullptr;
  12097. // OpenMP [teams Constrcut, Restrictions]
  12098. // The num_teams expression must evaluate to a positive integer value.
  12099. if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
  12100. /*StrictlyPositive=*/true))
  12101. return nullptr;
  12102. OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
  12103. OpenMPDirectiveKind CaptureRegion =
  12104. getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
  12105. if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
  12106. ValExpr = MakeFullExpr(ValExpr).get();
  12107. llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
  12108. ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
  12109. HelperValStmt = buildPreInits(Context, Captures);
  12110. }
  12111. return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
  12112. StartLoc, LParenLoc, EndLoc);
  12113. }
  12114. OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
  12115. SourceLocation StartLoc,
  12116. SourceLocation LParenLoc,
  12117. SourceLocation EndLoc) {
  12118. Expr *ValExpr = ThreadLimit;
  12119. Stmt *HelperValStmt = nullptr;
  12120. // OpenMP [teams Constrcut, Restrictions]
  12121. // The thread_limit expression must evaluate to a positive integer value.
  12122. if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
  12123. /*StrictlyPositive=*/true))
  12124. return nullptr;
  12125. OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
  12126. OpenMPDirectiveKind CaptureRegion =
  12127. getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
  12128. if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
  12129. ValExpr = MakeFullExpr(ValExpr).get();
  12130. llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
  12131. ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
  12132. HelperValStmt = buildPreInits(Context, Captures);
  12133. }
  12134. return new (Context) OMPThreadLimitClause(
  12135. ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
  12136. }
  12137. OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
  12138. SourceLocation StartLoc,
  12139. SourceLocation LParenLoc,
  12140. SourceLocation EndLoc) {
  12141. Expr *ValExpr = Priority;
  12142. // OpenMP [2.9.1, task Constrcut]
  12143. // The priority-value is a non-negative numerical scalar expression.
  12144. if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
  12145. /*StrictlyPositive=*/false))
  12146. return nullptr;
  12147. return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
  12148. }
  12149. OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
  12150. SourceLocation StartLoc,
  12151. SourceLocation LParenLoc,
  12152. SourceLocation EndLoc) {
  12153. Expr *ValExpr = Grainsize;
  12154. // OpenMP [2.9.2, taskloop Constrcut]
  12155. // The parameter of the grainsize clause must be a positive integer
  12156. // expression.
  12157. if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
  12158. /*StrictlyPositive=*/true))
  12159. return nullptr;
  12160. return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
  12161. }
  12162. OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
  12163. SourceLocation StartLoc,
  12164. SourceLocation LParenLoc,
  12165. SourceLocation EndLoc) {
  12166. Expr *ValExpr = NumTasks;
  12167. // OpenMP [2.9.2, taskloop Constrcut]
  12168. // The parameter of the num_tasks clause must be a positive integer
  12169. // expression.
  12170. if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
  12171. /*StrictlyPositive=*/true))
  12172. return nullptr;
  12173. return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
  12174. }
  12175. OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
  12176. SourceLocation LParenLoc,
  12177. SourceLocation EndLoc) {
  12178. // OpenMP [2.13.2, critical construct, Description]
  12179. // ... where hint-expression is an integer constant expression that evaluates
  12180. // to a valid lock hint.
  12181. ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
  12182. if (HintExpr.isInvalid())
  12183. return nullptr;
  12184. return new (Context)
  12185. OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
  12186. }
  12187. OMPClause *Sema::ActOnOpenMPDistScheduleClause(
  12188. OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
  12189. SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
  12190. SourceLocation EndLoc) {
  12191. if (Kind == OMPC_DIST_SCHEDULE_unknown) {
  12192. std::string Values;
  12193. Values += "'";
  12194. Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
  12195. Values += "'";
  12196. Diag(KindLoc, diag::err_omp_unexpected_clause_value)
  12197. << Values << getOpenMPClauseName(OMPC_dist_schedule);
  12198. return nullptr;
  12199. }
  12200. Expr *ValExpr = ChunkSize;
  12201. Stmt *HelperValStmt = nullptr;
  12202. if (ChunkSize) {
  12203. if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
  12204. !ChunkSize->isInstantiationDependent() &&
  12205. !ChunkSize->containsUnexpandedParameterPack()) {
  12206. SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
  12207. ExprResult Val =
  12208. PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
  12209. if (Val.isInvalid())
  12210. return nullptr;
  12211. ValExpr = Val.get();
  12212. // OpenMP [2.7.1, Restrictions]
  12213. // chunk_size must be a loop invariant integer expression with a positive
  12214. // value.
  12215. llvm::APSInt Result;
  12216. if (ValExpr->isIntegerConstantExpr(Result, Context)) {
  12217. if (Result.isSigned() && !Result.isStrictlyPositive()) {
  12218. Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
  12219. << "dist_schedule" << ChunkSize->getSourceRange();
  12220. return nullptr;
  12221. }
  12222. } else if (getOpenMPCaptureRegionForClause(
  12223. DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
  12224. OMPD_unknown &&
  12225. !CurContext->isDependentContext()) {
  12226. ValExpr = MakeFullExpr(ValExpr).get();
  12227. llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
  12228. ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
  12229. HelperValStmt = buildPreInits(Context, Captures);
  12230. }
  12231. }
  12232. }
  12233. return new (Context)
  12234. OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
  12235. Kind, ValExpr, HelperValStmt);
  12236. }
  12237. OMPClause *Sema::ActOnOpenMPDefaultmapClause(
  12238. OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
  12239. SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
  12240. SourceLocation KindLoc, SourceLocation EndLoc) {
  12241. // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
  12242. if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
  12243. std::string Value;
  12244. SourceLocation Loc;
  12245. Value += "'";
  12246. if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
  12247. Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
  12248. OMPC_DEFAULTMAP_MODIFIER_tofrom);
  12249. Loc = MLoc;
  12250. } else {
  12251. Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
  12252. OMPC_DEFAULTMAP_scalar);
  12253. Loc = KindLoc;
  12254. }
  12255. Value += "'";
  12256. Diag(Loc, diag::err_omp_unexpected_clause_value)
  12257. << Value << getOpenMPClauseName(OMPC_defaultmap);
  12258. return nullptr;
  12259. }
  12260. DSAStack->setDefaultDMAToFromScalar(StartLoc);
  12261. return new (Context)
  12262. OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
  12263. }
  12264. bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
  12265. DeclContext *CurLexicalContext = getCurLexicalContext();
  12266. if (!CurLexicalContext->isFileContext() &&
  12267. !CurLexicalContext->isExternCContext() &&
  12268. !CurLexicalContext->isExternCXXContext() &&
  12269. !isa<CXXRecordDecl>(CurLexicalContext) &&
  12270. !isa<ClassTemplateDecl>(CurLexicalContext) &&
  12271. !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
  12272. !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
  12273. Diag(Loc, diag::err_omp_region_not_file_context);
  12274. return false;
  12275. }
  12276. ++DeclareTargetNestingLevel;
  12277. return true;
  12278. }
  12279. void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
  12280. assert(DeclareTargetNestingLevel > 0 &&
  12281. "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
  12282. --DeclareTargetNestingLevel;
  12283. }
  12284. void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
  12285. CXXScopeSpec &ScopeSpec,
  12286. const DeclarationNameInfo &Id,
  12287. OMPDeclareTargetDeclAttr::MapTypeTy MT,
  12288. NamedDeclSetType &SameDirectiveDecls) {
  12289. LookupResult Lookup(*this, Id, LookupOrdinaryName);
  12290. LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
  12291. if (Lookup.isAmbiguous())
  12292. return;
  12293. Lookup.suppressDiagnostics();
  12294. if (!Lookup.isSingleResult()) {
  12295. if (TypoCorrection Corrected =
  12296. CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
  12297. llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
  12298. CTK_ErrorRecovery)) {
  12299. diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
  12300. << Id.getName());
  12301. checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
  12302. return;
  12303. }
  12304. Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
  12305. return;
  12306. }
  12307. NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
  12308. if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
  12309. isa<FunctionTemplateDecl>(ND)) {
  12310. if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
  12311. Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
  12312. llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
  12313. OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
  12314. cast<ValueDecl>(ND));
  12315. if (!Res) {
  12316. auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
  12317. ND->addAttr(A);
  12318. if (ASTMutationListener *ML = Context.getASTMutationListener())
  12319. ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
  12320. checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
  12321. } else if (*Res != MT) {
  12322. Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
  12323. << Id.getName();
  12324. }
  12325. } else {
  12326. Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
  12327. }
  12328. }
  12329. static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
  12330. Sema &SemaRef, Decl *D) {
  12331. if (!D || !isa<VarDecl>(D))
  12332. return;
  12333. auto *VD = cast<VarDecl>(D);
  12334. if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
  12335. return;
  12336. SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
  12337. SemaRef.Diag(SL, diag::note_used_here) << SR;
  12338. }
  12339. static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
  12340. Sema &SemaRef, DSAStackTy *Stack,
  12341. ValueDecl *VD) {
  12342. return VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
  12343. checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
  12344. /*FullCheck=*/false);
  12345. }
  12346. void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
  12347. SourceLocation IdLoc) {
  12348. if (!D || D->isInvalidDecl())
  12349. return;
  12350. SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
  12351. SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
  12352. if (auto *VD = dyn_cast<VarDecl>(D)) {
  12353. // Only global variables can be marked as declare target.
  12354. if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
  12355. !VD->isStaticDataMember())
  12356. return;
  12357. // 2.10.6: threadprivate variable cannot appear in a declare target
  12358. // directive.
  12359. if (DSAStack->isThreadPrivate(VD)) {
  12360. Diag(SL, diag::err_omp_threadprivate_in_target);
  12361. reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
  12362. return;
  12363. }
  12364. }
  12365. if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
  12366. D = FTD->getTemplatedDecl();
  12367. if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
  12368. llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
  12369. OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
  12370. if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
  12371. assert(IdLoc.isValid() && "Source location is expected");
  12372. Diag(IdLoc, diag::err_omp_function_in_link_clause);
  12373. Diag(FD->getLocation(), diag::note_defined_here) << FD;
  12374. return;
  12375. }
  12376. }
  12377. if (auto *VD = dyn_cast<ValueDecl>(D)) {
  12378. // Problem if any with var declared with incomplete type will be reported
  12379. // as normal, so no need to check it here.
  12380. if ((E || !VD->getType()->isIncompleteType()) &&
  12381. !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
  12382. return;
  12383. if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
  12384. // Checking declaration inside declare target region.
  12385. if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
  12386. isa<FunctionTemplateDecl>(D)) {
  12387. auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
  12388. Context, OMPDeclareTargetDeclAttr::MT_To);
  12389. D->addAttr(A);
  12390. if (ASTMutationListener *ML = Context.getASTMutationListener())
  12391. ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
  12392. }
  12393. return;
  12394. }
  12395. }
  12396. if (!E)
  12397. return;
  12398. checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
  12399. }
  12400. OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
  12401. SourceLocation StartLoc,
  12402. SourceLocation LParenLoc,
  12403. SourceLocation EndLoc) {
  12404. MappableVarListInfo MVLI(VarList);
  12405. checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
  12406. if (MVLI.ProcessedVarList.empty())
  12407. return nullptr;
  12408. return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
  12409. MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
  12410. MVLI.VarComponents);
  12411. }
  12412. OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
  12413. SourceLocation StartLoc,
  12414. SourceLocation LParenLoc,
  12415. SourceLocation EndLoc) {
  12416. MappableVarListInfo MVLI(VarList);
  12417. checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
  12418. if (MVLI.ProcessedVarList.empty())
  12419. return nullptr;
  12420. return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
  12421. MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
  12422. MVLI.VarComponents);
  12423. }
  12424. OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
  12425. SourceLocation StartLoc,
  12426. SourceLocation LParenLoc,
  12427. SourceLocation EndLoc) {
  12428. MappableVarListInfo MVLI(VarList);
  12429. SmallVector<Expr *, 8> PrivateCopies;
  12430. SmallVector<Expr *, 8> Inits;
  12431. for (Expr *RefExpr : VarList) {
  12432. assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
  12433. SourceLocation ELoc;
  12434. SourceRange ERange;
  12435. Expr *SimpleRefExpr = RefExpr;
  12436. auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
  12437. if (Res.second) {
  12438. // It will be analyzed later.
  12439. MVLI.ProcessedVarList.push_back(RefExpr);
  12440. PrivateCopies.push_back(nullptr);
  12441. Inits.push_back(nullptr);
  12442. }
  12443. ValueDecl *D = Res.first;
  12444. if (!D)
  12445. continue;
  12446. QualType Type = D->getType();
  12447. Type = Type.getNonReferenceType().getUnqualifiedType();
  12448. auto *VD = dyn_cast<VarDecl>(D);
  12449. // Item should be a pointer or reference to pointer.
  12450. if (!Type->isPointerType()) {
  12451. Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
  12452. << 0 << RefExpr->getSourceRange();
  12453. continue;
  12454. }
  12455. // Build the private variable and the expression that refers to it.
  12456. auto VDPrivate =
  12457. buildVarDecl(*this, ELoc, Type, D->getName(),
  12458. D->hasAttrs() ? &D->getAttrs() : nullptr,
  12459. VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
  12460. if (VDPrivate->isInvalidDecl())
  12461. continue;
  12462. CurContext->addDecl(VDPrivate);
  12463. DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
  12464. *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
  12465. // Add temporary variable to initialize the private copy of the pointer.
  12466. VarDecl *VDInit =
  12467. buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
  12468. DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
  12469. *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
  12470. AddInitializerToDecl(VDPrivate,
  12471. DefaultLvalueConversion(VDInitRefExpr).get(),
  12472. /*DirectInit=*/false);
  12473. // If required, build a capture to implement the privatization initialized
  12474. // with the current list item value.
  12475. DeclRefExpr *Ref = nullptr;
  12476. if (!VD)
  12477. Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
  12478. MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
  12479. PrivateCopies.push_back(VDPrivateRefExpr);
  12480. Inits.push_back(VDInitRefExpr);
  12481. // We need to add a data sharing attribute for this variable to make sure it
  12482. // is correctly captured. A variable that shows up in a use_device_ptr has
  12483. // similar properties of a first private variable.
  12484. DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
  12485. // Create a mappable component for the list item. List items in this clause
  12486. // only need a component.
  12487. MVLI.VarBaseDeclarations.push_back(D);
  12488. MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
  12489. MVLI.VarComponents.back().push_back(
  12490. OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
  12491. }
  12492. if (MVLI.ProcessedVarList.empty())
  12493. return nullptr;
  12494. return OMPUseDevicePtrClause::Create(
  12495. Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
  12496. PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
  12497. }
  12498. OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
  12499. SourceLocation StartLoc,
  12500. SourceLocation LParenLoc,
  12501. SourceLocation EndLoc) {
  12502. MappableVarListInfo MVLI(VarList);
  12503. for (Expr *RefExpr : VarList) {
  12504. assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
  12505. SourceLocation ELoc;
  12506. SourceRange ERange;
  12507. Expr *SimpleRefExpr = RefExpr;
  12508. auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
  12509. if (Res.second) {
  12510. // It will be analyzed later.
  12511. MVLI.ProcessedVarList.push_back(RefExpr);
  12512. }
  12513. ValueDecl *D = Res.first;
  12514. if (!D)
  12515. continue;
  12516. QualType Type = D->getType();
  12517. // item should be a pointer or array or reference to pointer or array
  12518. if (!Type.getNonReferenceType()->isPointerType() &&
  12519. !Type.getNonReferenceType()->isArrayType()) {
  12520. Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
  12521. << 0 << RefExpr->getSourceRange();
  12522. continue;
  12523. }
  12524. // Check if the declaration in the clause does not show up in any data
  12525. // sharing attribute.
  12526. DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
  12527. if (isOpenMPPrivate(DVar.CKind)) {
  12528. Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
  12529. << getOpenMPClauseName(DVar.CKind)
  12530. << getOpenMPClauseName(OMPC_is_device_ptr)
  12531. << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
  12532. reportOriginalDsa(*this, DSAStack, D, DVar);
  12533. continue;
  12534. }
  12535. const Expr *ConflictExpr;
  12536. if (DSAStack->checkMappableExprComponentListsForDecl(
  12537. D, /*CurrentRegionOnly=*/true,
  12538. [&ConflictExpr](
  12539. OMPClauseMappableExprCommon::MappableExprComponentListRef R,
  12540. OpenMPClauseKind) -> bool {
  12541. ConflictExpr = R.front().getAssociatedExpression();
  12542. return true;
  12543. })) {
  12544. Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
  12545. Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
  12546. << ConflictExpr->getSourceRange();
  12547. continue;
  12548. }
  12549. // Store the components in the stack so that they can be used to check
  12550. // against other clauses later on.
  12551. OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
  12552. DSAStack->addMappableExpressionComponents(
  12553. D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
  12554. // Record the expression we've just processed.
  12555. MVLI.ProcessedVarList.push_back(SimpleRefExpr);
  12556. // Create a mappable component for the list item. List items in this clause
  12557. // only need a component. We use a null declaration to signal fields in
  12558. // 'this'.
  12559. assert((isa<DeclRefExpr>(SimpleRefExpr) ||
  12560. isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
  12561. "Unexpected device pointer expression!");
  12562. MVLI.VarBaseDeclarations.push_back(
  12563. isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
  12564. MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
  12565. MVLI.VarComponents.back().push_back(MC);
  12566. }
  12567. if (MVLI.ProcessedVarList.empty())
  12568. return nullptr;
  12569. return OMPIsDevicePtrClause::Create(
  12570. Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
  12571. MVLI.VarBaseDeclarations, MVLI.VarComponents);
  12572. }