SemaOpenMP.cpp 573 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426
  1. //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. /// \file
  9. /// This file implements semantic analysis for OpenMP directives and
  10. /// clauses.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "TreeTransform.h"
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/ASTMutationListener.h"
  16. #include "clang/AST/CXXInheritance.h"
  17. #include "clang/AST/Decl.h"
  18. #include "clang/AST/DeclCXX.h"
  19. #include "clang/AST/DeclOpenMP.h"
  20. #include "clang/AST/StmtCXX.h"
  21. #include "clang/AST/StmtOpenMP.h"
  22. #include "clang/AST/StmtVisitor.h"
  23. #include "clang/AST/TypeOrdering.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. unsigned AssociatedLoops = 1;
  134. const Decl *PossiblyLoopCounter = nullptr;
  135. bool NowaitRegion = false;
  136. bool CancelRegion = false;
  137. bool LoopStart = false;
  138. SourceLocation InnerTeamsRegionLoc;
  139. /// Reference to the taskgroup task_reduction reference expression.
  140. Expr *TaskgroupReductionRef = nullptr;
  141. llvm::DenseSet<QualType> MappedClassesQualTypes;
  142. SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
  143. Scope *CurScope, SourceLocation Loc)
  144. : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
  145. ConstructLoc(Loc) {}
  146. SharingMapTy() = default;
  147. };
  148. using StackTy = SmallVector<SharingMapTy, 4>;
  149. /// Stack of used declaration and their data-sharing attributes.
  150. DeclSAMapTy Threadprivates;
  151. const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
  152. SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
  153. /// true, if check for DSA must be from parent directive, false, if
  154. /// from current directive.
  155. OpenMPClauseKind ClauseKindMode = OMPC_unknown;
  156. Sema &SemaRef;
  157. bool ForceCapturing = false;
  158. /// true if all the vaiables in the target executable directives must be
  159. /// captured by reference.
  160. bool ForceCaptureByReferenceInTargetExecutable = false;
  161. CriticalsWithHintsTy Criticals;
  162. using iterator = StackTy::const_reverse_iterator;
  163. DSAVarData getDSA(iterator &Iter, ValueDecl *D) const;
  164. /// Checks if the variable is a local for OpenMP region.
  165. bool isOpenMPLocal(VarDecl *D, iterator Iter) const;
  166. bool isStackEmpty() const {
  167. return Stack.empty() ||
  168. Stack.back().second != CurrentNonCapturingFunctionScope ||
  169. Stack.back().first.empty();
  170. }
  171. /// Vector of previously declared requires directives
  172. SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
  173. public:
  174. explicit DSAStackTy(Sema &S) : SemaRef(S) {}
  175. bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
  176. OpenMPClauseKind getClauseParsingMode() const {
  177. assert(isClauseParsingMode() && "Must be in clause parsing mode.");
  178. return ClauseKindMode;
  179. }
  180. void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
  181. bool isForceVarCapturing() const { return ForceCapturing; }
  182. void setForceVarCapturing(bool V) { ForceCapturing = V; }
  183. void setForceCaptureByReferenceInTargetExecutable(bool V) {
  184. ForceCaptureByReferenceInTargetExecutable = V;
  185. }
  186. bool isForceCaptureByReferenceInTargetExecutable() const {
  187. return ForceCaptureByReferenceInTargetExecutable;
  188. }
  189. void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
  190. Scope *CurScope, SourceLocation Loc) {
  191. if (Stack.empty() ||
  192. Stack.back().second != CurrentNonCapturingFunctionScope)
  193. Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
  194. Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
  195. Stack.back().first.back().DefaultAttrLoc = Loc;
  196. }
  197. void pop() {
  198. assert(!Stack.back().first.empty() &&
  199. "Data-sharing attributes stack is empty!");
  200. Stack.back().first.pop_back();
  201. }
  202. /// Marks that we're started loop parsing.
  203. void loopInit() {
  204. assert(isOpenMPLoopDirective(getCurrentDirective()) &&
  205. "Expected loop-based directive.");
  206. Stack.back().first.back().LoopStart = true;
  207. }
  208. /// Start capturing of the variables in the loop context.
  209. void loopStart() {
  210. assert(isOpenMPLoopDirective(getCurrentDirective()) &&
  211. "Expected loop-based directive.");
  212. Stack.back().first.back().LoopStart = false;
  213. }
  214. /// true, if variables are captured, false otherwise.
  215. bool isLoopStarted() const {
  216. assert(isOpenMPLoopDirective(getCurrentDirective()) &&
  217. "Expected loop-based directive.");
  218. return !Stack.back().first.back().LoopStart;
  219. }
  220. /// Marks (or clears) declaration as possibly loop counter.
  221. void resetPossibleLoopCounter(const Decl *D = nullptr) {
  222. Stack.back().first.back().PossiblyLoopCounter =
  223. D ? D->getCanonicalDecl() : D;
  224. }
  225. /// Gets the possible loop counter decl.
  226. const Decl *getPossiblyLoopCunter() const {
  227. return Stack.back().first.back().PossiblyLoopCounter;
  228. }
  229. /// Start new OpenMP region stack in new non-capturing function.
  230. void pushFunction() {
  231. const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
  232. assert(!isa<CapturingScopeInfo>(CurFnScope));
  233. CurrentNonCapturingFunctionScope = CurFnScope;
  234. }
  235. /// Pop region stack for non-capturing function.
  236. void popFunction(const FunctionScopeInfo *OldFSI) {
  237. if (!Stack.empty() && Stack.back().second == OldFSI) {
  238. assert(Stack.back().first.empty());
  239. Stack.pop_back();
  240. }
  241. CurrentNonCapturingFunctionScope = nullptr;
  242. for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
  243. if (!isa<CapturingScopeInfo>(FSI)) {
  244. CurrentNonCapturingFunctionScope = FSI;
  245. break;
  246. }
  247. }
  248. }
  249. void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
  250. Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint);
  251. }
  252. const std::pair<const OMPCriticalDirective *, llvm::APSInt>
  253. getCriticalWithHint(const DeclarationNameInfo &Name) const {
  254. auto I = Criticals.find(Name.getAsString());
  255. if (I != Criticals.end())
  256. return I->second;
  257. return std::make_pair(nullptr, llvm::APSInt());
  258. }
  259. /// If 'aligned' declaration for given variable \a D was not seen yet,
  260. /// add it and return NULL; otherwise return previous occurrence's expression
  261. /// for diagnostics.
  262. const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
  263. /// Register specified variable as loop control variable.
  264. void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
  265. /// Check if the specified variable is a loop control variable for
  266. /// current region.
  267. /// \return The index of the loop control variable in the list of associated
  268. /// for-loops (from outer to inner).
  269. const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
  270. /// Check if the specified variable is a loop control variable for
  271. /// parent region.
  272. /// \return The index of the loop control variable in the list of associated
  273. /// for-loops (from outer to inner).
  274. const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
  275. /// Get the loop control variable for the I-th loop (or nullptr) in
  276. /// parent directive.
  277. const ValueDecl *getParentLoopControlVariable(unsigned I) const;
  278. /// Adds explicit data sharing attribute to the specified declaration.
  279. void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
  280. DeclRefExpr *PrivateCopy = nullptr);
  281. /// Adds additional information for the reduction items with the reduction id
  282. /// represented as an operator.
  283. void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
  284. BinaryOperatorKind BOK);
  285. /// Adds additional information for the reduction items with the reduction id
  286. /// represented as reduction identifier.
  287. void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
  288. const Expr *ReductionRef);
  289. /// Returns the location and reduction operation from the innermost parent
  290. /// region for the given \p D.
  291. const DSAVarData
  292. getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
  293. BinaryOperatorKind &BOK,
  294. Expr *&TaskgroupDescriptor) const;
  295. /// Returns the location and reduction operation from the innermost parent
  296. /// region for the given \p D.
  297. const DSAVarData
  298. getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
  299. const Expr *&ReductionRef,
  300. Expr *&TaskgroupDescriptor) const;
  301. /// Return reduction reference expression for the current taskgroup.
  302. Expr *getTaskgroupReductionRef() const {
  303. assert(Stack.back().first.back().Directive == OMPD_taskgroup &&
  304. "taskgroup reference expression requested for non taskgroup "
  305. "directive.");
  306. return Stack.back().first.back().TaskgroupReductionRef;
  307. }
  308. /// Checks if the given \p VD declaration is actually a taskgroup reduction
  309. /// descriptor variable at the \p Level of OpenMP regions.
  310. bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
  311. return Stack.back().first[Level].TaskgroupReductionRef &&
  312. cast<DeclRefExpr>(Stack.back().first[Level].TaskgroupReductionRef)
  313. ->getDecl() == VD;
  314. }
  315. /// Returns data sharing attributes from top of the stack for the
  316. /// specified declaration.
  317. const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
  318. /// Returns data-sharing attributes for the specified declaration.
  319. const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
  320. /// Checks if the specified variables has data-sharing attributes which
  321. /// match specified \a CPred predicate in any directive which matches \a DPred
  322. /// predicate.
  323. const DSAVarData
  324. hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
  325. const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
  326. bool FromParent) const;
  327. /// Checks if the specified variables has data-sharing attributes which
  328. /// match specified \a CPred predicate in any innermost directive which
  329. /// matches \a DPred predicate.
  330. const DSAVarData
  331. hasInnermostDSA(ValueDecl *D,
  332. const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
  333. const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
  334. bool FromParent) const;
  335. /// Checks if the specified variables has explicit data-sharing
  336. /// attributes which match specified \a CPred predicate at the specified
  337. /// OpenMP region.
  338. bool hasExplicitDSA(const ValueDecl *D,
  339. const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
  340. unsigned Level, bool NotLastprivate = false) const;
  341. /// Returns true if the directive at level \Level matches in the
  342. /// specified \a DPred predicate.
  343. bool hasExplicitDirective(
  344. const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
  345. unsigned Level) const;
  346. /// Finds a directive which matches specified \a DPred predicate.
  347. bool hasDirective(
  348. const llvm::function_ref<bool(
  349. OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
  350. DPred,
  351. bool FromParent) const;
  352. /// Returns currently analyzed directive.
  353. OpenMPDirectiveKind getCurrentDirective() const {
  354. return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive;
  355. }
  356. /// Returns directive kind at specified level.
  357. OpenMPDirectiveKind getDirective(unsigned Level) const {
  358. assert(!isStackEmpty() && "No directive at specified level.");
  359. return Stack.back().first[Level].Directive;
  360. }
  361. /// Returns parent directive.
  362. OpenMPDirectiveKind getParentDirective() const {
  363. if (isStackEmpty() || Stack.back().first.size() == 1)
  364. return OMPD_unknown;
  365. return std::next(Stack.back().first.rbegin())->Directive;
  366. }
  367. /// Add requires decl to internal vector
  368. void addRequiresDecl(OMPRequiresDecl *RD) {
  369. RequiresDecls.push_back(RD);
  370. }
  371. /// Checks for a duplicate clause amongst previously declared requires
  372. /// directives
  373. bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
  374. bool IsDuplicate = false;
  375. for (OMPClause *CNew : ClauseList) {
  376. for (const OMPRequiresDecl *D : RequiresDecls) {
  377. for (const OMPClause *CPrev : D->clauselists()) {
  378. if (CNew->getClauseKind() == CPrev->getClauseKind()) {
  379. SemaRef.Diag(CNew->getBeginLoc(),
  380. diag::err_omp_requires_clause_redeclaration)
  381. << getOpenMPClauseName(CNew->getClauseKind());
  382. SemaRef.Diag(CPrev->getBeginLoc(),
  383. diag::note_omp_requires_previous_clause)
  384. << getOpenMPClauseName(CPrev->getClauseKind());
  385. IsDuplicate = true;
  386. }
  387. }
  388. }
  389. }
  390. return IsDuplicate;
  391. }
  392. /// Set default data sharing attribute to none.
  393. void setDefaultDSANone(SourceLocation Loc) {
  394. assert(!isStackEmpty());
  395. Stack.back().first.back().DefaultAttr = DSA_none;
  396. Stack.back().first.back().DefaultAttrLoc = Loc;
  397. }
  398. /// Set default data sharing attribute to shared.
  399. void setDefaultDSAShared(SourceLocation Loc) {
  400. assert(!isStackEmpty());
  401. Stack.back().first.back().DefaultAttr = DSA_shared;
  402. Stack.back().first.back().DefaultAttrLoc = Loc;
  403. }
  404. /// Set default data mapping attribute to 'tofrom:scalar'.
  405. void setDefaultDMAToFromScalar(SourceLocation Loc) {
  406. assert(!isStackEmpty());
  407. Stack.back().first.back().DefaultMapAttr = DMA_tofrom_scalar;
  408. Stack.back().first.back().DefaultMapAttrLoc = Loc;
  409. }
  410. DefaultDataSharingAttributes getDefaultDSA() const {
  411. return isStackEmpty() ? DSA_unspecified
  412. : Stack.back().first.back().DefaultAttr;
  413. }
  414. SourceLocation getDefaultDSALocation() const {
  415. return isStackEmpty() ? SourceLocation()
  416. : Stack.back().first.back().DefaultAttrLoc;
  417. }
  418. DefaultMapAttributes getDefaultDMA() const {
  419. return isStackEmpty() ? DMA_unspecified
  420. : Stack.back().first.back().DefaultMapAttr;
  421. }
  422. DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const {
  423. return Stack.back().first[Level].DefaultMapAttr;
  424. }
  425. SourceLocation getDefaultDMALocation() const {
  426. return isStackEmpty() ? SourceLocation()
  427. : Stack.back().first.back().DefaultMapAttrLoc;
  428. }
  429. /// Checks if the specified variable is a threadprivate.
  430. bool isThreadPrivate(VarDecl *D) {
  431. const DSAVarData DVar = getTopDSA(D, false);
  432. return isOpenMPThreadPrivate(DVar.CKind);
  433. }
  434. /// Marks current region as ordered (it has an 'ordered' clause).
  435. void setOrderedRegion(bool IsOrdered, const Expr *Param,
  436. OMPOrderedClause *Clause) {
  437. assert(!isStackEmpty());
  438. if (IsOrdered)
  439. Stack.back().first.back().OrderedRegion.emplace(Param, Clause);
  440. else
  441. Stack.back().first.back().OrderedRegion.reset();
  442. }
  443. /// Returns true, if region is ordered (has associated 'ordered' clause),
  444. /// false - otherwise.
  445. bool isOrderedRegion() const {
  446. if (isStackEmpty())
  447. return false;
  448. return Stack.back().first.rbegin()->OrderedRegion.hasValue();
  449. }
  450. /// Returns optional parameter for the ordered region.
  451. std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
  452. if (isStackEmpty() ||
  453. !Stack.back().first.rbegin()->OrderedRegion.hasValue())
  454. return std::make_pair(nullptr, nullptr);
  455. return Stack.back().first.rbegin()->OrderedRegion.getValue();
  456. }
  457. /// Returns true, if parent region is ordered (has associated
  458. /// 'ordered' clause), false - otherwise.
  459. bool isParentOrderedRegion() const {
  460. if (isStackEmpty() || Stack.back().first.size() == 1)
  461. return false;
  462. return std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue();
  463. }
  464. /// Returns optional parameter for the ordered region.
  465. std::pair<const Expr *, OMPOrderedClause *>
  466. getParentOrderedRegionParam() const {
  467. if (isStackEmpty() || Stack.back().first.size() == 1 ||
  468. !std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue())
  469. return std::make_pair(nullptr, nullptr);
  470. return std::next(Stack.back().first.rbegin())->OrderedRegion.getValue();
  471. }
  472. /// Marks current region as nowait (it has a 'nowait' clause).
  473. void setNowaitRegion(bool IsNowait = true) {
  474. assert(!isStackEmpty());
  475. Stack.back().first.back().NowaitRegion = IsNowait;
  476. }
  477. /// Returns true, if parent region is nowait (has associated
  478. /// 'nowait' clause), false - otherwise.
  479. bool isParentNowaitRegion() const {
  480. if (isStackEmpty() || Stack.back().first.size() == 1)
  481. return false;
  482. return std::next(Stack.back().first.rbegin())->NowaitRegion;
  483. }
  484. /// Marks parent region as cancel region.
  485. void setParentCancelRegion(bool Cancel = true) {
  486. if (!isStackEmpty() && Stack.back().first.size() > 1) {
  487. auto &StackElemRef = *std::next(Stack.back().first.rbegin());
  488. StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel;
  489. }
  490. }
  491. /// Return true if current region has inner cancel construct.
  492. bool isCancelRegion() const {
  493. return isStackEmpty() ? false : Stack.back().first.back().CancelRegion;
  494. }
  495. /// Set collapse value for the region.
  496. void setAssociatedLoops(unsigned Val) {
  497. assert(!isStackEmpty());
  498. Stack.back().first.back().AssociatedLoops = Val;
  499. }
  500. /// Return collapse value for region.
  501. unsigned getAssociatedLoops() const {
  502. return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops;
  503. }
  504. /// Marks current target region as one with closely nested teams
  505. /// region.
  506. void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
  507. if (!isStackEmpty() && Stack.back().first.size() > 1) {
  508. std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc =
  509. TeamsRegionLoc;
  510. }
  511. }
  512. /// Returns true, if current region has closely nested teams region.
  513. bool hasInnerTeamsRegion() const {
  514. return getInnerTeamsRegionLoc().isValid();
  515. }
  516. /// Returns location of the nested teams region (if any).
  517. SourceLocation getInnerTeamsRegionLoc() const {
  518. return isStackEmpty() ? SourceLocation()
  519. : Stack.back().first.back().InnerTeamsRegionLoc;
  520. }
  521. Scope *getCurScope() const {
  522. return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
  523. }
  524. SourceLocation getConstructLoc() const {
  525. return isStackEmpty() ? SourceLocation()
  526. : Stack.back().first.back().ConstructLoc;
  527. }
  528. /// Do the check specified in \a Check to all component lists and return true
  529. /// if any issue is found.
  530. bool checkMappableExprComponentListsForDecl(
  531. const ValueDecl *VD, bool CurrentRegionOnly,
  532. const llvm::function_ref<
  533. bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
  534. OpenMPClauseKind)>
  535. Check) const {
  536. if (isStackEmpty())
  537. return false;
  538. auto SI = Stack.back().first.rbegin();
  539. auto SE = Stack.back().first.rend();
  540. if (SI == SE)
  541. return false;
  542. if (CurrentRegionOnly)
  543. SE = std::next(SI);
  544. else
  545. std::advance(SI, 1);
  546. for (; SI != SE; ++SI) {
  547. auto MI = SI->MappedExprComponents.find(VD);
  548. if (MI != SI->MappedExprComponents.end())
  549. for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
  550. MI->second.Components)
  551. if (Check(L, MI->second.Kind))
  552. return true;
  553. }
  554. return false;
  555. }
  556. /// Do the check specified in \a Check to all component lists at a given level
  557. /// and return true if any issue is found.
  558. bool checkMappableExprComponentListsForDeclAtLevel(
  559. const ValueDecl *VD, unsigned Level,
  560. const llvm::function_ref<
  561. bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
  562. OpenMPClauseKind)>
  563. Check) const {
  564. if (isStackEmpty())
  565. return false;
  566. auto StartI = Stack.back().first.begin();
  567. auto EndI = Stack.back().first.end();
  568. if (std::distance(StartI, EndI) <= (int)Level)
  569. return false;
  570. std::advance(StartI, Level);
  571. auto MI = StartI->MappedExprComponents.find(VD);
  572. if (MI != StartI->MappedExprComponents.end())
  573. for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
  574. MI->second.Components)
  575. if (Check(L, MI->second.Kind))
  576. return true;
  577. return false;
  578. }
  579. /// Create a new mappable expression component list associated with a given
  580. /// declaration and initialize it with the provided list of components.
  581. void addMappableExpressionComponents(
  582. const ValueDecl *VD,
  583. OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
  584. OpenMPClauseKind WhereFoundClauseKind) {
  585. assert(!isStackEmpty() &&
  586. "Not expecting to retrieve components from a empty stack!");
  587. MappedExprComponentTy &MEC =
  588. Stack.back().first.back().MappedExprComponents[VD];
  589. // Create new entry and append the new components there.
  590. MEC.Components.resize(MEC.Components.size() + 1);
  591. MEC.Components.back().append(Components.begin(), Components.end());
  592. MEC.Kind = WhereFoundClauseKind;
  593. }
  594. unsigned getNestingLevel() const {
  595. assert(!isStackEmpty());
  596. return Stack.back().first.size() - 1;
  597. }
  598. void addDoacrossDependClause(OMPDependClause *C,
  599. const OperatorOffsetTy &OpsOffs) {
  600. assert(!isStackEmpty() && Stack.back().first.size() > 1);
  601. SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
  602. assert(isOpenMPWorksharingDirective(StackElem.Directive));
  603. StackElem.DoacrossDepends.try_emplace(C, OpsOffs);
  604. }
  605. llvm::iterator_range<DoacrossDependMapTy::const_iterator>
  606. getDoacrossDependClauses() const {
  607. assert(!isStackEmpty());
  608. const SharingMapTy &StackElem = Stack.back().first.back();
  609. if (isOpenMPWorksharingDirective(StackElem.Directive)) {
  610. const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
  611. return llvm::make_range(Ref.begin(), Ref.end());
  612. }
  613. return llvm::make_range(StackElem.DoacrossDepends.end(),
  614. StackElem.DoacrossDepends.end());
  615. }
  616. // Store types of classes which have been explicitly mapped
  617. void addMappedClassesQualTypes(QualType QT) {
  618. SharingMapTy &StackElem = Stack.back().first.back();
  619. StackElem.MappedClassesQualTypes.insert(QT);
  620. }
  621. // Return set of mapped classes types
  622. bool isClassPreviouslyMapped(QualType QT) const {
  623. const SharingMapTy &StackElem = Stack.back().first.back();
  624. return StackElem.MappedClassesQualTypes.count(QT) != 0;
  625. }
  626. };
  627. bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) {
  628. return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind);
  629. }
  630. bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) {
  631. return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) || DKind == OMPD_unknown;
  632. }
  633. } // namespace
  634. static const Expr *getExprAsWritten(const Expr *E) {
  635. if (const auto *FE = dyn_cast<FullExpr>(E))
  636. E = FE->getSubExpr();
  637. if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
  638. E = MTE->GetTemporaryExpr();
  639. while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
  640. E = Binder->getSubExpr();
  641. if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
  642. E = ICE->getSubExprAsWritten();
  643. return E->IgnoreParens();
  644. }
  645. static Expr *getExprAsWritten(Expr *E) {
  646. return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
  647. }
  648. static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
  649. if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
  650. if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
  651. D = ME->getMemberDecl();
  652. const auto *VD = dyn_cast<VarDecl>(D);
  653. const auto *FD = dyn_cast<FieldDecl>(D);
  654. if (VD != nullptr) {
  655. VD = VD->getCanonicalDecl();
  656. D = VD;
  657. } else {
  658. assert(FD);
  659. FD = FD->getCanonicalDecl();
  660. D = FD;
  661. }
  662. return D;
  663. }
  664. static ValueDecl *getCanonicalDecl(ValueDecl *D) {
  665. return const_cast<ValueDecl *>(
  666. getCanonicalDecl(const_cast<const ValueDecl *>(D)));
  667. }
  668. DSAStackTy::DSAVarData DSAStackTy::getDSA(iterator &Iter,
  669. ValueDecl *D) const {
  670. D = getCanonicalDecl(D);
  671. auto *VD = dyn_cast<VarDecl>(D);
  672. const auto *FD = dyn_cast<FieldDecl>(D);
  673. DSAVarData DVar;
  674. if (isStackEmpty() || Iter == Stack.back().first.rend()) {
  675. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  676. // in a region but not in construct]
  677. // File-scope or namespace-scope variables referenced in called routines
  678. // in the region are shared unless they appear in a threadprivate
  679. // directive.
  680. if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
  681. DVar.CKind = OMPC_shared;
  682. // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
  683. // in a region but not in construct]
  684. // Variables with static storage duration that are declared in called
  685. // routines in the region are shared.
  686. if (VD && VD->hasGlobalStorage())
  687. DVar.CKind = OMPC_shared;
  688. // Non-static data members are shared by default.
  689. if (FD)
  690. DVar.CKind = OMPC_shared;
  691. return DVar;
  692. }
  693. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  694. // in a Construct, C/C++, predetermined, p.1]
  695. // Variables with automatic storage duration that are declared in a scope
  696. // inside the construct are private.
  697. if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
  698. (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
  699. DVar.CKind = OMPC_private;
  700. return DVar;
  701. }
  702. DVar.DKind = Iter->Directive;
  703. // Explicitly specified attributes and local variables with predetermined
  704. // attributes.
  705. if (Iter->SharingMap.count(D)) {
  706. const DSAInfo &Data = Iter->SharingMap.lookup(D);
  707. DVar.RefExpr = Data.RefExpr.getPointer();
  708. DVar.PrivateCopy = Data.PrivateCopy;
  709. DVar.CKind = Data.Attributes;
  710. DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
  711. return DVar;
  712. }
  713. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  714. // in a Construct, C/C++, implicitly determined, p.1]
  715. // In a parallel or task construct, the data-sharing attributes of these
  716. // variables are determined by the default clause, if present.
  717. switch (Iter->DefaultAttr) {
  718. case DSA_shared:
  719. DVar.CKind = OMPC_shared;
  720. DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
  721. return DVar;
  722. case DSA_none:
  723. return DVar;
  724. case DSA_unspecified:
  725. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  726. // in a Construct, implicitly determined, p.2]
  727. // In a parallel construct, if no default clause is present, these
  728. // variables are shared.
  729. DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
  730. if (isOpenMPParallelDirective(DVar.DKind) ||
  731. isOpenMPTeamsDirective(DVar.DKind)) {
  732. DVar.CKind = OMPC_shared;
  733. return DVar;
  734. }
  735. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  736. // in a Construct, implicitly determined, p.4]
  737. // In a task construct, if no default clause is present, a variable that in
  738. // the enclosing context is determined to be shared by all implicit tasks
  739. // bound to the current team is shared.
  740. if (isOpenMPTaskingDirective(DVar.DKind)) {
  741. DSAVarData DVarTemp;
  742. iterator I = Iter, E = Stack.back().first.rend();
  743. do {
  744. ++I;
  745. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
  746. // Referenced in a Construct, implicitly determined, p.6]
  747. // In a task construct, if no default clause is present, a variable
  748. // whose data-sharing attribute is not determined by the rules above is
  749. // firstprivate.
  750. DVarTemp = getDSA(I, D);
  751. if (DVarTemp.CKind != OMPC_shared) {
  752. DVar.RefExpr = nullptr;
  753. DVar.CKind = OMPC_firstprivate;
  754. return DVar;
  755. }
  756. } while (I != E && !isImplicitTaskingRegion(I->Directive));
  757. DVar.CKind =
  758. (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
  759. return DVar;
  760. }
  761. }
  762. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  763. // in a Construct, implicitly determined, p.3]
  764. // For constructs other than task, if no default clause is present, these
  765. // variables inherit their data-sharing attributes from the enclosing
  766. // context.
  767. return getDSA(++Iter, D);
  768. }
  769. const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
  770. const Expr *NewDE) {
  771. assert(!isStackEmpty() && "Data sharing attributes stack is empty");
  772. D = getCanonicalDecl(D);
  773. SharingMapTy &StackElem = Stack.back().first.back();
  774. auto It = StackElem.AlignedMap.find(D);
  775. if (It == StackElem.AlignedMap.end()) {
  776. assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
  777. StackElem.AlignedMap[D] = NewDE;
  778. return nullptr;
  779. }
  780. assert(It->second && "Unexpected nullptr expr in the aligned map");
  781. return It->second;
  782. }
  783. void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
  784. assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
  785. D = getCanonicalDecl(D);
  786. SharingMapTy &StackElem = Stack.back().first.back();
  787. StackElem.LCVMap.try_emplace(
  788. D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
  789. }
  790. const DSAStackTy::LCDeclInfo
  791. DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
  792. assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
  793. D = getCanonicalDecl(D);
  794. const SharingMapTy &StackElem = Stack.back().first.back();
  795. auto It = StackElem.LCVMap.find(D);
  796. if (It != StackElem.LCVMap.end())
  797. return It->second;
  798. return {0, nullptr};
  799. }
  800. const DSAStackTy::LCDeclInfo
  801. DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
  802. assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
  803. "Data-sharing attributes stack is empty");
  804. D = getCanonicalDecl(D);
  805. const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
  806. auto It = StackElem.LCVMap.find(D);
  807. if (It != StackElem.LCVMap.end())
  808. return It->second;
  809. return {0, nullptr};
  810. }
  811. const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
  812. assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
  813. "Data-sharing attributes stack is empty");
  814. const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
  815. if (StackElem.LCVMap.size() < I)
  816. return nullptr;
  817. for (const auto &Pair : StackElem.LCVMap)
  818. if (Pair.second.first == I)
  819. return Pair.first;
  820. return nullptr;
  821. }
  822. void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
  823. DeclRefExpr *PrivateCopy) {
  824. D = getCanonicalDecl(D);
  825. if (A == OMPC_threadprivate) {
  826. DSAInfo &Data = Threadprivates[D];
  827. Data.Attributes = A;
  828. Data.RefExpr.setPointer(E);
  829. Data.PrivateCopy = nullptr;
  830. } else {
  831. assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
  832. DSAInfo &Data = Stack.back().first.back().SharingMap[D];
  833. assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
  834. (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
  835. (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
  836. (isLoopControlVariable(D).first && A == OMPC_private));
  837. if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
  838. Data.RefExpr.setInt(/*IntVal=*/true);
  839. return;
  840. }
  841. const bool IsLastprivate =
  842. A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
  843. Data.Attributes = A;
  844. Data.RefExpr.setPointerAndInt(E, IsLastprivate);
  845. Data.PrivateCopy = PrivateCopy;
  846. if (PrivateCopy) {
  847. DSAInfo &Data =
  848. Stack.back().first.back().SharingMap[PrivateCopy->getDecl()];
  849. Data.Attributes = A;
  850. Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
  851. Data.PrivateCopy = nullptr;
  852. }
  853. }
  854. }
  855. /// Build a variable declaration for OpenMP loop iteration variable.
  856. static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
  857. StringRef Name, const AttrVec *Attrs = nullptr,
  858. DeclRefExpr *OrigRef = nullptr) {
  859. DeclContext *DC = SemaRef.CurContext;
  860. IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
  861. TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
  862. auto *Decl =
  863. VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
  864. if (Attrs) {
  865. for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
  866. I != E; ++I)
  867. Decl->addAttr(*I);
  868. }
  869. Decl->setImplicit();
  870. if (OrigRef) {
  871. Decl->addAttr(
  872. OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
  873. }
  874. return Decl;
  875. }
  876. static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
  877. SourceLocation Loc,
  878. bool RefersToCapture = false) {
  879. D->setReferenced();
  880. D->markUsed(S.Context);
  881. return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
  882. SourceLocation(), D, RefersToCapture, Loc, Ty,
  883. VK_LValue);
  884. }
  885. void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
  886. BinaryOperatorKind BOK) {
  887. D = getCanonicalDecl(D);
  888. assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
  889. assert(
  890. Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
  891. "Additional reduction info may be specified only for reduction items.");
  892. ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
  893. assert(ReductionData.ReductionRange.isInvalid() &&
  894. Stack.back().first.back().Directive == OMPD_taskgroup &&
  895. "Additional reduction info may be specified only once for reduction "
  896. "items.");
  897. ReductionData.set(BOK, SR);
  898. Expr *&TaskgroupReductionRef =
  899. Stack.back().first.back().TaskgroupReductionRef;
  900. if (!TaskgroupReductionRef) {
  901. VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
  902. SemaRef.Context.VoidPtrTy, ".task_red.");
  903. TaskgroupReductionRef =
  904. buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
  905. }
  906. }
  907. void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
  908. const Expr *ReductionRef) {
  909. D = getCanonicalDecl(D);
  910. assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
  911. assert(
  912. Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
  913. "Additional reduction info may be specified only for reduction items.");
  914. ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
  915. assert(ReductionData.ReductionRange.isInvalid() &&
  916. Stack.back().first.back().Directive == OMPD_taskgroup &&
  917. "Additional reduction info may be specified only once for reduction "
  918. "items.");
  919. ReductionData.set(ReductionRef, SR);
  920. Expr *&TaskgroupReductionRef =
  921. Stack.back().first.back().TaskgroupReductionRef;
  922. if (!TaskgroupReductionRef) {
  923. VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
  924. SemaRef.Context.VoidPtrTy, ".task_red.");
  925. TaskgroupReductionRef =
  926. buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
  927. }
  928. }
  929. const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
  930. const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
  931. Expr *&TaskgroupDescriptor) const {
  932. D = getCanonicalDecl(D);
  933. assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
  934. if (Stack.back().first.empty())
  935. return DSAVarData();
  936. for (iterator I = std::next(Stack.back().first.rbegin(), 1),
  937. E = Stack.back().first.rend();
  938. I != E; std::advance(I, 1)) {
  939. const DSAInfo &Data = I->SharingMap.lookup(D);
  940. if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
  941. continue;
  942. const ReductionData &ReductionData = I->ReductionMap.lookup(D);
  943. if (!ReductionData.ReductionOp ||
  944. ReductionData.ReductionOp.is<const Expr *>())
  945. return DSAVarData();
  946. SR = ReductionData.ReductionRange;
  947. BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
  948. assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
  949. "expression for the descriptor is not "
  950. "set.");
  951. TaskgroupDescriptor = I->TaskgroupReductionRef;
  952. return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
  953. Data.PrivateCopy, I->DefaultAttrLoc);
  954. }
  955. return DSAVarData();
  956. }
  957. const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
  958. const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
  959. Expr *&TaskgroupDescriptor) const {
  960. D = getCanonicalDecl(D);
  961. assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
  962. if (Stack.back().first.empty())
  963. return DSAVarData();
  964. for (iterator I = std::next(Stack.back().first.rbegin(), 1),
  965. E = Stack.back().first.rend();
  966. I != E; std::advance(I, 1)) {
  967. const DSAInfo &Data = I->SharingMap.lookup(D);
  968. if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
  969. continue;
  970. const ReductionData &ReductionData = I->ReductionMap.lookup(D);
  971. if (!ReductionData.ReductionOp ||
  972. !ReductionData.ReductionOp.is<const Expr *>())
  973. return DSAVarData();
  974. SR = ReductionData.ReductionRange;
  975. ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
  976. assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
  977. "expression for the descriptor is not "
  978. "set.");
  979. TaskgroupDescriptor = I->TaskgroupReductionRef;
  980. return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
  981. Data.PrivateCopy, I->DefaultAttrLoc);
  982. }
  983. return DSAVarData();
  984. }
  985. bool DSAStackTy::isOpenMPLocal(VarDecl *D, iterator Iter) const {
  986. D = D->getCanonicalDecl();
  987. if (!isStackEmpty()) {
  988. iterator I = Iter, E = Stack.back().first.rend();
  989. Scope *TopScope = nullptr;
  990. while (I != E && !isImplicitOrExplicitTaskingRegion(I->Directive) &&
  991. !isOpenMPTargetExecutionDirective(I->Directive))
  992. ++I;
  993. if (I == E)
  994. return false;
  995. TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
  996. Scope *CurScope = getCurScope();
  997. while (CurScope != TopScope && !CurScope->isDeclScope(D))
  998. CurScope = CurScope->getParent();
  999. return CurScope != TopScope;
  1000. }
  1001. return false;
  1002. }
  1003. static bool isConstNotMutableType(Sema &SemaRef, QualType Type,
  1004. bool AcceptIfMutable = true,
  1005. bool *IsClassType = nullptr) {
  1006. ASTContext &Context = SemaRef.getASTContext();
  1007. Type = Type.getNonReferenceType().getCanonicalType();
  1008. bool IsConstant = Type.isConstant(Context);
  1009. Type = Context.getBaseElementType(Type);
  1010. const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus
  1011. ? Type->getAsCXXRecordDecl()
  1012. : nullptr;
  1013. if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
  1014. if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
  1015. RD = CTD->getTemplatedDecl();
  1016. if (IsClassType)
  1017. *IsClassType = RD;
  1018. return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD &&
  1019. RD->hasDefinition() && RD->hasMutableFields());
  1020. }
  1021. static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D,
  1022. QualType Type, OpenMPClauseKind CKind,
  1023. SourceLocation ELoc,
  1024. bool AcceptIfMutable = true,
  1025. bool ListItemNotVar = false) {
  1026. ASTContext &Context = SemaRef.getASTContext();
  1027. bool IsClassType;
  1028. if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) {
  1029. unsigned Diag = ListItemNotVar
  1030. ? diag::err_omp_const_list_item
  1031. : IsClassType ? diag::err_omp_const_not_mutable_variable
  1032. : diag::err_omp_const_variable;
  1033. SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind);
  1034. if (!ListItemNotVar && D) {
  1035. const VarDecl *VD = dyn_cast<VarDecl>(D);
  1036. bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
  1037. VarDecl::DeclarationOnly;
  1038. SemaRef.Diag(D->getLocation(),
  1039. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  1040. << D;
  1041. }
  1042. return true;
  1043. }
  1044. return false;
  1045. }
  1046. const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
  1047. bool FromParent) {
  1048. D = getCanonicalDecl(D);
  1049. DSAVarData DVar;
  1050. auto *VD = dyn_cast<VarDecl>(D);
  1051. auto TI = Threadprivates.find(D);
  1052. if (TI != Threadprivates.end()) {
  1053. DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
  1054. DVar.CKind = OMPC_threadprivate;
  1055. return DVar;
  1056. }
  1057. if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
  1058. DVar.RefExpr = buildDeclRefExpr(
  1059. SemaRef, VD, D->getType().getNonReferenceType(),
  1060. VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
  1061. DVar.CKind = OMPC_threadprivate;
  1062. addDSA(D, DVar.RefExpr, OMPC_threadprivate);
  1063. return DVar;
  1064. }
  1065. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  1066. // in a Construct, C/C++, predetermined, p.1]
  1067. // Variables appearing in threadprivate directives are threadprivate.
  1068. if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
  1069. !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
  1070. SemaRef.getLangOpts().OpenMPUseTLS &&
  1071. SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
  1072. (VD && VD->getStorageClass() == SC_Register &&
  1073. VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
  1074. DVar.RefExpr = buildDeclRefExpr(
  1075. SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
  1076. DVar.CKind = OMPC_threadprivate;
  1077. addDSA(D, DVar.RefExpr, OMPC_threadprivate);
  1078. return DVar;
  1079. }
  1080. if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
  1081. VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
  1082. !isLoopControlVariable(D).first) {
  1083. iterator IterTarget =
  1084. std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(),
  1085. [](const SharingMapTy &Data) {
  1086. return isOpenMPTargetExecutionDirective(Data.Directive);
  1087. });
  1088. if (IterTarget != Stack.back().first.rend()) {
  1089. iterator ParentIterTarget = std::next(IterTarget, 1);
  1090. for (iterator Iter = Stack.back().first.rbegin();
  1091. Iter != ParentIterTarget; std::advance(Iter, 1)) {
  1092. if (isOpenMPLocal(VD, Iter)) {
  1093. DVar.RefExpr =
  1094. buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
  1095. D->getLocation());
  1096. DVar.CKind = OMPC_threadprivate;
  1097. return DVar;
  1098. }
  1099. }
  1100. if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) {
  1101. auto DSAIter = IterTarget->SharingMap.find(D);
  1102. if (DSAIter != IterTarget->SharingMap.end() &&
  1103. isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
  1104. DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
  1105. DVar.CKind = OMPC_threadprivate;
  1106. return DVar;
  1107. }
  1108. iterator End = Stack.back().first.rend();
  1109. if (!SemaRef.isOpenMPCapturedByRef(
  1110. D, std::distance(ParentIterTarget, End))) {
  1111. DVar.RefExpr =
  1112. buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
  1113. IterTarget->ConstructLoc);
  1114. DVar.CKind = OMPC_threadprivate;
  1115. return DVar;
  1116. }
  1117. }
  1118. }
  1119. }
  1120. if (isStackEmpty())
  1121. // Not in OpenMP execution region and top scope was already checked.
  1122. return DVar;
  1123. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  1124. // in a Construct, C/C++, predetermined, p.4]
  1125. // Static data members are shared.
  1126. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  1127. // in a Construct, C/C++, predetermined, p.7]
  1128. // Variables with static storage duration that are declared in a scope
  1129. // inside the construct are shared.
  1130. auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
  1131. if (VD && VD->isStaticDataMember()) {
  1132. DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
  1133. if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
  1134. return DVar;
  1135. DVar.CKind = OMPC_shared;
  1136. return DVar;
  1137. }
  1138. // The predetermined shared attribute for const-qualified types having no
  1139. // mutable members was removed after OpenMP 3.1.
  1140. if (SemaRef.LangOpts.OpenMP <= 31) {
  1141. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  1142. // in a Construct, C/C++, predetermined, p.6]
  1143. // Variables with const qualified type having no mutable member are
  1144. // shared.
  1145. if (isConstNotMutableType(SemaRef, D->getType())) {
  1146. // Variables with const-qualified type having no mutable member may be
  1147. // listed in a firstprivate clause, even if they are static data members.
  1148. DSAVarData DVarTemp = hasInnermostDSA(
  1149. D,
  1150. [](OpenMPClauseKind C) {
  1151. return C == OMPC_firstprivate || C == OMPC_shared;
  1152. },
  1153. MatchesAlways, FromParent);
  1154. if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
  1155. return DVarTemp;
  1156. DVar.CKind = OMPC_shared;
  1157. return DVar;
  1158. }
  1159. }
  1160. // Explicitly specified attributes and local variables with predetermined
  1161. // attributes.
  1162. iterator I = Stack.back().first.rbegin();
  1163. iterator EndI = Stack.back().first.rend();
  1164. if (FromParent && I != EndI)
  1165. std::advance(I, 1);
  1166. auto It = I->SharingMap.find(D);
  1167. if (It != I->SharingMap.end()) {
  1168. const DSAInfo &Data = It->getSecond();
  1169. DVar.RefExpr = Data.RefExpr.getPointer();
  1170. DVar.PrivateCopy = Data.PrivateCopy;
  1171. DVar.CKind = Data.Attributes;
  1172. DVar.ImplicitDSALoc = I->DefaultAttrLoc;
  1173. DVar.DKind = I->Directive;
  1174. }
  1175. return DVar;
  1176. }
  1177. const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
  1178. bool FromParent) const {
  1179. if (isStackEmpty()) {
  1180. iterator I;
  1181. return getDSA(I, D);
  1182. }
  1183. D = getCanonicalDecl(D);
  1184. iterator StartI = Stack.back().first.rbegin();
  1185. iterator EndI = Stack.back().first.rend();
  1186. if (FromParent && StartI != EndI)
  1187. std::advance(StartI, 1);
  1188. return getDSA(StartI, D);
  1189. }
  1190. const DSAStackTy::DSAVarData
  1191. DSAStackTy::hasDSA(ValueDecl *D,
  1192. const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
  1193. const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
  1194. bool FromParent) const {
  1195. if (isStackEmpty())
  1196. return {};
  1197. D = getCanonicalDecl(D);
  1198. iterator I = Stack.back().first.rbegin();
  1199. iterator EndI = Stack.back().first.rend();
  1200. if (FromParent && I != EndI)
  1201. std::advance(I, 1);
  1202. for (; I != EndI; std::advance(I, 1)) {
  1203. if (!DPred(I->Directive) && !isImplicitOrExplicitTaskingRegion(I->Directive))
  1204. continue;
  1205. iterator NewI = I;
  1206. DSAVarData DVar = getDSA(NewI, D);
  1207. if (I == NewI && CPred(DVar.CKind))
  1208. return DVar;
  1209. }
  1210. return {};
  1211. }
  1212. const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
  1213. ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
  1214. const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
  1215. bool FromParent) const {
  1216. if (isStackEmpty())
  1217. return {};
  1218. D = getCanonicalDecl(D);
  1219. iterator StartI = Stack.back().first.rbegin();
  1220. iterator EndI = Stack.back().first.rend();
  1221. if (FromParent && StartI != EndI)
  1222. std::advance(StartI, 1);
  1223. if (StartI == EndI || !DPred(StartI->Directive))
  1224. return {};
  1225. iterator NewI = StartI;
  1226. DSAVarData DVar = getDSA(NewI, D);
  1227. return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
  1228. }
  1229. bool DSAStackTy::hasExplicitDSA(
  1230. const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
  1231. unsigned Level, bool NotLastprivate) const {
  1232. if (isStackEmpty())
  1233. return false;
  1234. D = getCanonicalDecl(D);
  1235. auto StartI = Stack.back().first.begin();
  1236. auto EndI = Stack.back().first.end();
  1237. if (std::distance(StartI, EndI) <= (int)Level)
  1238. return false;
  1239. std::advance(StartI, Level);
  1240. auto I = StartI->SharingMap.find(D);
  1241. if ((I != StartI->SharingMap.end()) &&
  1242. I->getSecond().RefExpr.getPointer() &&
  1243. CPred(I->getSecond().Attributes) &&
  1244. (!NotLastprivate || !I->getSecond().RefExpr.getInt()))
  1245. return true;
  1246. // Check predetermined rules for the loop control variables.
  1247. auto LI = StartI->LCVMap.find(D);
  1248. if (LI != StartI->LCVMap.end())
  1249. return CPred(OMPC_private);
  1250. return false;
  1251. }
  1252. bool DSAStackTy::hasExplicitDirective(
  1253. const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
  1254. unsigned Level) const {
  1255. if (isStackEmpty())
  1256. return false;
  1257. auto StartI = Stack.back().first.begin();
  1258. auto EndI = Stack.back().first.end();
  1259. if (std::distance(StartI, EndI) <= (int)Level)
  1260. return false;
  1261. std::advance(StartI, Level);
  1262. return DPred(StartI->Directive);
  1263. }
  1264. bool DSAStackTy::hasDirective(
  1265. const llvm::function_ref<bool(OpenMPDirectiveKind,
  1266. const DeclarationNameInfo &, SourceLocation)>
  1267. DPred,
  1268. bool FromParent) const {
  1269. // We look only in the enclosing region.
  1270. if (isStackEmpty())
  1271. return false;
  1272. auto StartI = std::next(Stack.back().first.rbegin());
  1273. auto EndI = Stack.back().first.rend();
  1274. if (FromParent && StartI != EndI)
  1275. StartI = std::next(StartI);
  1276. for (auto I = StartI, EE = EndI; I != EE; ++I) {
  1277. if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
  1278. return true;
  1279. }
  1280. return false;
  1281. }
  1282. void Sema::InitDataSharingAttributesStack() {
  1283. VarDataSharingAttributesStack = new DSAStackTy(*this);
  1284. }
  1285. #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
  1286. void Sema::pushOpenMPFunctionRegion() {
  1287. DSAStack->pushFunction();
  1288. }
  1289. void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
  1290. DSAStack->popFunction(OldFSI);
  1291. }
  1292. static bool isOpenMPDeviceDelayedContext(Sema &S) {
  1293. assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice &&
  1294. "Expected OpenMP device compilation.");
  1295. return !S.isInOpenMPTargetExecutionDirective() &&
  1296. !S.isInOpenMPDeclareTargetContext();
  1297. }
  1298. /// Do we know that we will eventually codegen the given function?
  1299. static bool isKnownEmitted(Sema &S, FunctionDecl *FD) {
  1300. assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice &&
  1301. "Expected OpenMP device compilation.");
  1302. // Templates are emitted when they're instantiated.
  1303. if (FD->isDependentContext())
  1304. return false;
  1305. if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
  1306. FD->getCanonicalDecl()))
  1307. return true;
  1308. // Otherwise, the function is known-emitted if it's in our set of
  1309. // known-emitted functions.
  1310. return S.DeviceKnownEmittedFns.count(FD) > 0;
  1311. }
  1312. Sema::DeviceDiagBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc,
  1313. unsigned DiagID) {
  1314. assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
  1315. "Expected OpenMP device compilation.");
  1316. return DeviceDiagBuilder((isOpenMPDeviceDelayedContext(*this) &&
  1317. !isKnownEmitted(*this, getCurFunctionDecl()))
  1318. ? DeviceDiagBuilder::K_Deferred
  1319. : DeviceDiagBuilder::K_Immediate,
  1320. Loc, DiagID, getCurFunctionDecl(), *this);
  1321. }
  1322. void Sema::checkOpenMPDeviceFunction(SourceLocation Loc, FunctionDecl *Callee) {
  1323. assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
  1324. "Expected OpenMP device compilation.");
  1325. assert(Callee && "Callee may not be null.");
  1326. FunctionDecl *Caller = getCurFunctionDecl();
  1327. // If the caller is known-emitted, mark the callee as known-emitted.
  1328. // Otherwise, mark the call in our call graph so we can traverse it later.
  1329. if (!isOpenMPDeviceDelayedContext(*this) ||
  1330. (Caller && isKnownEmitted(*this, Caller)))
  1331. markKnownEmitted(*this, Caller, Callee, Loc, isKnownEmitted);
  1332. else if (Caller)
  1333. DeviceCallGraph[Caller].insert({Callee, Loc});
  1334. }
  1335. void Sema::checkOpenMPDeviceExpr(const Expr *E) {
  1336. assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice &&
  1337. "OpenMP device compilation mode is expected.");
  1338. QualType Ty = E->getType();
  1339. if ((Ty->isFloat16Type() && !Context.getTargetInfo().hasFloat16Type()) ||
  1340. (Ty->isFloat128Type() && !Context.getTargetInfo().hasFloat128Type()) ||
  1341. (Ty->isIntegerType() && Context.getTypeSize(Ty) == 128 &&
  1342. !Context.getTargetInfo().hasInt128Type()))
  1343. targetDiag(E->getExprLoc(), diag::err_type_unsupported)
  1344. << Ty << E->getSourceRange();
  1345. }
  1346. bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const {
  1347. assert(LangOpts.OpenMP && "OpenMP is not allowed");
  1348. ASTContext &Ctx = getASTContext();
  1349. bool IsByRef = true;
  1350. // Find the directive that is associated with the provided scope.
  1351. D = cast<ValueDecl>(D->getCanonicalDecl());
  1352. QualType Ty = D->getType();
  1353. if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
  1354. // This table summarizes how a given variable should be passed to the device
  1355. // given its type and the clauses where it appears. This table is based on
  1356. // the description in OpenMP 4.5 [2.10.4, target Construct] and
  1357. // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
  1358. //
  1359. // =========================================================================
  1360. // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
  1361. // | |(tofrom:scalar)| | pvt | | | |
  1362. // =========================================================================
  1363. // | scl | | | | - | | bycopy|
  1364. // | scl | | - | x | - | - | bycopy|
  1365. // | scl | | x | - | - | - | null |
  1366. // | scl | x | | | - | | byref |
  1367. // | scl | x | - | x | - | - | bycopy|
  1368. // | scl | x | x | - | - | - | null |
  1369. // | scl | | - | - | - | x | byref |
  1370. // | scl | x | - | - | - | x | byref |
  1371. //
  1372. // | agg | n.a. | | | - | | byref |
  1373. // | agg | n.a. | - | x | - | - | byref |
  1374. // | agg | n.a. | x | - | - | - | null |
  1375. // | agg | n.a. | - | - | - | x | byref |
  1376. // | agg | n.a. | - | - | - | x[] | byref |
  1377. //
  1378. // | ptr | n.a. | | | - | | bycopy|
  1379. // | ptr | n.a. | - | x | - | - | bycopy|
  1380. // | ptr | n.a. | x | - | - | - | null |
  1381. // | ptr | n.a. | - | - | - | x | byref |
  1382. // | ptr | n.a. | - | - | - | x[] | bycopy|
  1383. // | ptr | n.a. | - | - | x | | bycopy|
  1384. // | ptr | n.a. | - | - | x | x | bycopy|
  1385. // | ptr | n.a. | - | - | x | x[] | bycopy|
  1386. // =========================================================================
  1387. // Legend:
  1388. // scl - scalar
  1389. // ptr - pointer
  1390. // agg - aggregate
  1391. // x - applies
  1392. // - - invalid in this combination
  1393. // [] - mapped with an array section
  1394. // byref - should be mapped by reference
  1395. // byval - should be mapped by value
  1396. // null - initialize a local variable to null on the device
  1397. //
  1398. // Observations:
  1399. // - All scalar declarations that show up in a map clause have to be passed
  1400. // by reference, because they may have been mapped in the enclosing data
  1401. // environment.
  1402. // - If the scalar value does not fit the size of uintptr, it has to be
  1403. // passed by reference, regardless the result in the table above.
  1404. // - For pointers mapped by value that have either an implicit map or an
  1405. // array section, the runtime library may pass the NULL value to the
  1406. // device instead of the value passed to it by the compiler.
  1407. if (Ty->isReferenceType())
  1408. Ty = Ty->castAs<ReferenceType>()->getPointeeType();
  1409. // Locate map clauses and see if the variable being captured is referred to
  1410. // in any of those clauses. Here we only care about variables, not fields,
  1411. // because fields are part of aggregates.
  1412. bool IsVariableUsedInMapClause = false;
  1413. bool IsVariableAssociatedWithSection = false;
  1414. DSAStack->checkMappableExprComponentListsForDeclAtLevel(
  1415. D, Level,
  1416. [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
  1417. OMPClauseMappableExprCommon::MappableExprComponentListRef
  1418. MapExprComponents,
  1419. OpenMPClauseKind WhereFoundClauseKind) {
  1420. // Only the map clause information influences how a variable is
  1421. // captured. E.g. is_device_ptr does not require changing the default
  1422. // behavior.
  1423. if (WhereFoundClauseKind != OMPC_map)
  1424. return false;
  1425. auto EI = MapExprComponents.rbegin();
  1426. auto EE = MapExprComponents.rend();
  1427. assert(EI != EE && "Invalid map expression!");
  1428. if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
  1429. IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
  1430. ++EI;
  1431. if (EI == EE)
  1432. return false;
  1433. if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
  1434. isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
  1435. isa<MemberExpr>(EI->getAssociatedExpression())) {
  1436. IsVariableAssociatedWithSection = true;
  1437. // There is nothing more we need to know about this variable.
  1438. return true;
  1439. }
  1440. // Keep looking for more map info.
  1441. return false;
  1442. });
  1443. if (IsVariableUsedInMapClause) {
  1444. // If variable is identified in a map clause it is always captured by
  1445. // reference except if it is a pointer that is dereferenced somehow.
  1446. IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
  1447. } else {
  1448. // By default, all the data that has a scalar type is mapped by copy
  1449. // (except for reduction variables).
  1450. IsByRef =
  1451. (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
  1452. !Ty->isAnyPointerType()) ||
  1453. !Ty->isScalarType() ||
  1454. DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
  1455. DSAStack->hasExplicitDSA(
  1456. D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
  1457. }
  1458. }
  1459. if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
  1460. IsByRef =
  1461. ((DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
  1462. !Ty->isAnyPointerType()) ||
  1463. !DSAStack->hasExplicitDSA(
  1464. D,
  1465. [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
  1466. Level, /*NotLastprivate=*/true)) &&
  1467. // If the variable is artificial and must be captured by value - try to
  1468. // capture by value.
  1469. !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
  1470. !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
  1471. }
  1472. // When passing data by copy, we need to make sure it fits the uintptr size
  1473. // and alignment, because the runtime library only deals with uintptr types.
  1474. // If it does not fit the uintptr size, we need to pass the data by reference
  1475. // instead.
  1476. if (!IsByRef &&
  1477. (Ctx.getTypeSizeInChars(Ty) >
  1478. Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
  1479. Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
  1480. IsByRef = true;
  1481. }
  1482. return IsByRef;
  1483. }
  1484. unsigned Sema::getOpenMPNestingLevel() const {
  1485. assert(getLangOpts().OpenMP);
  1486. return DSAStack->getNestingLevel();
  1487. }
  1488. bool Sema::isInOpenMPTargetExecutionDirective() const {
  1489. return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
  1490. !DSAStack->isClauseParsingMode()) ||
  1491. DSAStack->hasDirective(
  1492. [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
  1493. SourceLocation) -> bool {
  1494. return isOpenMPTargetExecutionDirective(K);
  1495. },
  1496. false);
  1497. }
  1498. VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D) {
  1499. assert(LangOpts.OpenMP && "OpenMP is not allowed");
  1500. D = getCanonicalDecl(D);
  1501. // If we are attempting to capture a global variable in a directive with
  1502. // 'target' we return true so that this global is also mapped to the device.
  1503. //
  1504. auto *VD = dyn_cast<VarDecl>(D);
  1505. if (VD && !VD->hasLocalStorage()) {
  1506. if (isInOpenMPDeclareTargetContext() &&
  1507. (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
  1508. // Try to mark variable as declare target if it is used in capturing
  1509. // regions.
  1510. if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
  1511. checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
  1512. return nullptr;
  1513. } else if (isInOpenMPTargetExecutionDirective()) {
  1514. // If the declaration is enclosed in a 'declare target' directive,
  1515. // then it should not be captured.
  1516. //
  1517. if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
  1518. return nullptr;
  1519. return VD;
  1520. }
  1521. }
  1522. // Capture variables captured by reference in lambdas for target-based
  1523. // directives.
  1524. if (VD && !DSAStack->isClauseParsingMode()) {
  1525. if (const auto *RD = VD->getType()
  1526. .getCanonicalType()
  1527. .getNonReferenceType()
  1528. ->getAsCXXRecordDecl()) {
  1529. bool SavedForceCaptureByReferenceInTargetExecutable =
  1530. DSAStack->isForceCaptureByReferenceInTargetExecutable();
  1531. DSAStack->setForceCaptureByReferenceInTargetExecutable(/*V=*/true);
  1532. if (RD->isLambda()) {
  1533. llvm::DenseMap<const VarDecl *, FieldDecl *> Captures;
  1534. FieldDecl *ThisCapture;
  1535. RD->getCaptureFields(Captures, ThisCapture);
  1536. for (const LambdaCapture &LC : RD->captures()) {
  1537. if (LC.getCaptureKind() == LCK_ByRef) {
  1538. VarDecl *VD = LC.getCapturedVar();
  1539. DeclContext *VDC = VD->getDeclContext();
  1540. if (!VDC->Encloses(CurContext))
  1541. continue;
  1542. DSAStackTy::DSAVarData DVarPrivate =
  1543. DSAStack->getTopDSA(VD, /*FromParent=*/false);
  1544. // Do not capture already captured variables.
  1545. if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) &&
  1546. DVarPrivate.CKind == OMPC_unknown &&
  1547. !DSAStack->checkMappableExprComponentListsForDecl(
  1548. D, /*CurrentRegionOnly=*/true,
  1549. [](OMPClauseMappableExprCommon::
  1550. MappableExprComponentListRef,
  1551. OpenMPClauseKind) { return true; }))
  1552. MarkVariableReferenced(LC.getLocation(), LC.getCapturedVar());
  1553. } else if (LC.getCaptureKind() == LCK_This) {
  1554. QualType ThisTy = getCurrentThisType();
  1555. if (!ThisTy.isNull() &&
  1556. Context.typesAreCompatible(ThisTy, ThisCapture->getType()))
  1557. CheckCXXThisCapture(LC.getLocation());
  1558. }
  1559. }
  1560. }
  1561. DSAStack->setForceCaptureByReferenceInTargetExecutable(
  1562. SavedForceCaptureByReferenceInTargetExecutable);
  1563. }
  1564. }
  1565. if (DSAStack->getCurrentDirective() != OMPD_unknown &&
  1566. (!DSAStack->isClauseParsingMode() ||
  1567. DSAStack->getParentDirective() != OMPD_unknown)) {
  1568. auto &&Info = DSAStack->isLoopControlVariable(D);
  1569. if (Info.first ||
  1570. (VD && VD->hasLocalStorage() &&
  1571. isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) ||
  1572. (VD && DSAStack->isForceVarCapturing()))
  1573. return VD ? VD : Info.second;
  1574. DSAStackTy::DSAVarData DVarPrivate =
  1575. DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
  1576. if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
  1577. return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
  1578. DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate,
  1579. [](OpenMPDirectiveKind) { return true; },
  1580. DSAStack->isClauseParsingMode());
  1581. if (DVarPrivate.CKind != OMPC_unknown)
  1582. return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
  1583. }
  1584. return nullptr;
  1585. }
  1586. void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
  1587. unsigned Level) const {
  1588. SmallVector<OpenMPDirectiveKind, 4> Regions;
  1589. getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
  1590. FunctionScopesIndex -= Regions.size();
  1591. }
  1592. void Sema::startOpenMPLoop() {
  1593. assert(LangOpts.OpenMP && "OpenMP must be enabled.");
  1594. if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
  1595. DSAStack->loopInit();
  1596. }
  1597. bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const {
  1598. assert(LangOpts.OpenMP && "OpenMP is not allowed");
  1599. if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
  1600. if (DSAStack->getAssociatedLoops() > 0 &&
  1601. !DSAStack->isLoopStarted()) {
  1602. DSAStack->resetPossibleLoopCounter(D);
  1603. DSAStack->loopStart();
  1604. return true;
  1605. }
  1606. if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
  1607. DSAStack->isLoopControlVariable(D).first) &&
  1608. !DSAStack->hasExplicitDSA(
  1609. D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) &&
  1610. !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
  1611. return true;
  1612. }
  1613. return DSAStack->hasExplicitDSA(
  1614. D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) ||
  1615. (DSAStack->isClauseParsingMode() &&
  1616. DSAStack->getClauseParsingMode() == OMPC_private) ||
  1617. // Consider taskgroup reduction descriptor variable a private to avoid
  1618. // possible capture in the region.
  1619. (DSAStack->hasExplicitDirective(
  1620. [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
  1621. Level) &&
  1622. DSAStack->isTaskgroupReductionRef(D, Level));
  1623. }
  1624. void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
  1625. unsigned Level) {
  1626. assert(LangOpts.OpenMP && "OpenMP is not allowed");
  1627. D = getCanonicalDecl(D);
  1628. OpenMPClauseKind OMPC = OMPC_unknown;
  1629. for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
  1630. const unsigned NewLevel = I - 1;
  1631. if (DSAStack->hasExplicitDSA(D,
  1632. [&OMPC](const OpenMPClauseKind K) {
  1633. if (isOpenMPPrivate(K)) {
  1634. OMPC = K;
  1635. return true;
  1636. }
  1637. return false;
  1638. },
  1639. NewLevel))
  1640. break;
  1641. if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
  1642. D, NewLevel,
  1643. [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
  1644. OpenMPClauseKind) { return true; })) {
  1645. OMPC = OMPC_map;
  1646. break;
  1647. }
  1648. if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
  1649. NewLevel)) {
  1650. OMPC = OMPC_map;
  1651. if (D->getType()->isScalarType() &&
  1652. DSAStack->getDefaultDMAAtLevel(NewLevel) !=
  1653. DefaultMapAttributes::DMA_tofrom_scalar)
  1654. OMPC = OMPC_firstprivate;
  1655. break;
  1656. }
  1657. }
  1658. if (OMPC != OMPC_unknown)
  1659. FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
  1660. }
  1661. bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D,
  1662. unsigned Level) const {
  1663. assert(LangOpts.OpenMP && "OpenMP is not allowed");
  1664. // Return true if the current level is no longer enclosed in a target region.
  1665. const auto *VD = dyn_cast<VarDecl>(D);
  1666. return VD && !VD->hasLocalStorage() &&
  1667. DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
  1668. Level);
  1669. }
  1670. void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
  1671. void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
  1672. const DeclarationNameInfo &DirName,
  1673. Scope *CurScope, SourceLocation Loc) {
  1674. DSAStack->push(DKind, DirName, CurScope, Loc);
  1675. PushExpressionEvaluationContext(
  1676. ExpressionEvaluationContext::PotentiallyEvaluated);
  1677. }
  1678. void Sema::StartOpenMPClause(OpenMPClauseKind K) {
  1679. DSAStack->setClauseParsingMode(K);
  1680. }
  1681. void Sema::EndOpenMPClause() {
  1682. DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
  1683. }
  1684. void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
  1685. // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
  1686. // A variable of class type (or array thereof) that appears in a lastprivate
  1687. // clause requires an accessible, unambiguous default constructor for the
  1688. // class type, unless the list item is also specified in a firstprivate
  1689. // clause.
  1690. if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
  1691. for (OMPClause *C : D->clauses()) {
  1692. if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
  1693. SmallVector<Expr *, 8> PrivateCopies;
  1694. for (Expr *DE : Clause->varlists()) {
  1695. if (DE->isValueDependent() || DE->isTypeDependent()) {
  1696. PrivateCopies.push_back(nullptr);
  1697. continue;
  1698. }
  1699. auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
  1700. auto *VD = cast<VarDecl>(DRE->getDecl());
  1701. QualType Type = VD->getType().getNonReferenceType();
  1702. const DSAStackTy::DSAVarData DVar =
  1703. DSAStack->getTopDSA(VD, /*FromParent=*/false);
  1704. if (DVar.CKind == OMPC_lastprivate) {
  1705. // Generate helper private variable and initialize it with the
  1706. // default value. The address of the original variable is replaced
  1707. // by the address of the new private variable in CodeGen. This new
  1708. // variable is not added to IdResolver, so the code in the OpenMP
  1709. // region uses original variable for proper diagnostics.
  1710. VarDecl *VDPrivate = buildVarDecl(
  1711. *this, DE->getExprLoc(), Type.getUnqualifiedType(),
  1712. VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
  1713. ActOnUninitializedDecl(VDPrivate);
  1714. if (VDPrivate->isInvalidDecl())
  1715. continue;
  1716. PrivateCopies.push_back(buildDeclRefExpr(
  1717. *this, VDPrivate, DE->getType(), DE->getExprLoc()));
  1718. } else {
  1719. // The variable is also a firstprivate, so initialization sequence
  1720. // for private copy is generated already.
  1721. PrivateCopies.push_back(nullptr);
  1722. }
  1723. }
  1724. // Set initializers to private copies if no errors were found.
  1725. if (PrivateCopies.size() == Clause->varlist_size())
  1726. Clause->setPrivateCopies(PrivateCopies);
  1727. }
  1728. }
  1729. }
  1730. DSAStack->pop();
  1731. DiscardCleanupsInEvaluationContext();
  1732. PopExpressionEvaluationContext();
  1733. }
  1734. static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
  1735. Expr *NumIterations, Sema &SemaRef,
  1736. Scope *S, DSAStackTy *Stack);
  1737. namespace {
  1738. class VarDeclFilterCCC final : public CorrectionCandidateCallback {
  1739. private:
  1740. Sema &SemaRef;
  1741. public:
  1742. explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
  1743. bool ValidateCandidate(const TypoCorrection &Candidate) override {
  1744. NamedDecl *ND = Candidate.getCorrectionDecl();
  1745. if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
  1746. return VD->hasGlobalStorage() &&
  1747. SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
  1748. SemaRef.getCurScope());
  1749. }
  1750. return false;
  1751. }
  1752. };
  1753. class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
  1754. private:
  1755. Sema &SemaRef;
  1756. public:
  1757. explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
  1758. bool ValidateCandidate(const TypoCorrection &Candidate) override {
  1759. NamedDecl *ND = Candidate.getCorrectionDecl();
  1760. if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) ||
  1761. isa<FunctionDecl>(ND))) {
  1762. return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
  1763. SemaRef.getCurScope());
  1764. }
  1765. return false;
  1766. }
  1767. };
  1768. } // namespace
  1769. ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
  1770. CXXScopeSpec &ScopeSpec,
  1771. const DeclarationNameInfo &Id,
  1772. OpenMPDirectiveKind Kind) {
  1773. LookupResult Lookup(*this, Id, LookupOrdinaryName);
  1774. LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
  1775. if (Lookup.isAmbiguous())
  1776. return ExprError();
  1777. VarDecl *VD;
  1778. if (!Lookup.isSingleResult()) {
  1779. if (TypoCorrection Corrected = CorrectTypo(
  1780. Id, LookupOrdinaryName, CurScope, nullptr,
  1781. llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
  1782. diagnoseTypo(Corrected,
  1783. PDiag(Lookup.empty()
  1784. ? diag::err_undeclared_var_use_suggest
  1785. : diag::err_omp_expected_var_arg_suggest)
  1786. << Id.getName());
  1787. VD = Corrected.getCorrectionDeclAs<VarDecl>();
  1788. } else {
  1789. Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
  1790. : diag::err_omp_expected_var_arg)
  1791. << Id.getName();
  1792. return ExprError();
  1793. }
  1794. } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
  1795. Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
  1796. Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
  1797. return ExprError();
  1798. }
  1799. Lookup.suppressDiagnostics();
  1800. // OpenMP [2.9.2, Syntax, C/C++]
  1801. // Variables must be file-scope, namespace-scope, or static block-scope.
  1802. if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) {
  1803. Diag(Id.getLoc(), diag::err_omp_global_var_arg)
  1804. << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal();
  1805. bool IsDecl =
  1806. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  1807. Diag(VD->getLocation(),
  1808. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  1809. << VD;
  1810. return ExprError();
  1811. }
  1812. VarDecl *CanonicalVD = VD->getCanonicalDecl();
  1813. NamedDecl *ND = CanonicalVD;
  1814. // OpenMP [2.9.2, Restrictions, C/C++, p.2]
  1815. // A threadprivate directive for file-scope variables must appear outside
  1816. // any definition or declaration.
  1817. if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
  1818. !getCurLexicalContext()->isTranslationUnit()) {
  1819. Diag(Id.getLoc(), diag::err_omp_var_scope)
  1820. << getOpenMPDirectiveName(Kind) << VD;
  1821. bool IsDecl =
  1822. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  1823. Diag(VD->getLocation(),
  1824. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  1825. << VD;
  1826. return ExprError();
  1827. }
  1828. // OpenMP [2.9.2, Restrictions, C/C++, p.3]
  1829. // A threadprivate directive for static class member variables must appear
  1830. // in the class definition, in the same scope in which the member
  1831. // variables are declared.
  1832. if (CanonicalVD->isStaticDataMember() &&
  1833. !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
  1834. Diag(Id.getLoc(), diag::err_omp_var_scope)
  1835. << getOpenMPDirectiveName(Kind) << VD;
  1836. bool IsDecl =
  1837. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  1838. Diag(VD->getLocation(),
  1839. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  1840. << VD;
  1841. return ExprError();
  1842. }
  1843. // OpenMP [2.9.2, Restrictions, C/C++, p.4]
  1844. // A threadprivate directive for namespace-scope variables must appear
  1845. // outside any definition or declaration other than the namespace
  1846. // definition itself.
  1847. if (CanonicalVD->getDeclContext()->isNamespace() &&
  1848. (!getCurLexicalContext()->isFileContext() ||
  1849. !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
  1850. Diag(Id.getLoc(), diag::err_omp_var_scope)
  1851. << getOpenMPDirectiveName(Kind) << VD;
  1852. bool IsDecl =
  1853. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  1854. Diag(VD->getLocation(),
  1855. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  1856. << VD;
  1857. return ExprError();
  1858. }
  1859. // OpenMP [2.9.2, Restrictions, C/C++, p.6]
  1860. // A threadprivate directive for static block-scope variables must appear
  1861. // in the scope of the variable and not in a nested scope.
  1862. if (CanonicalVD->isLocalVarDecl() && CurScope &&
  1863. !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
  1864. Diag(Id.getLoc(), diag::err_omp_var_scope)
  1865. << getOpenMPDirectiveName(Kind) << VD;
  1866. bool IsDecl =
  1867. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  1868. Diag(VD->getLocation(),
  1869. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  1870. << VD;
  1871. return ExprError();
  1872. }
  1873. // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
  1874. // A threadprivate directive must lexically precede all references to any
  1875. // of the variables in its list.
  1876. if (Kind == OMPD_threadprivate && VD->isUsed() &&
  1877. !DSAStack->isThreadPrivate(VD)) {
  1878. Diag(Id.getLoc(), diag::err_omp_var_used)
  1879. << getOpenMPDirectiveName(Kind) << VD;
  1880. return ExprError();
  1881. }
  1882. QualType ExprType = VD->getType().getNonReferenceType();
  1883. return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
  1884. SourceLocation(), VD,
  1885. /*RefersToEnclosingVariableOrCapture=*/false,
  1886. Id.getLoc(), ExprType, VK_LValue);
  1887. }
  1888. Sema::DeclGroupPtrTy
  1889. Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
  1890. ArrayRef<Expr *> VarList) {
  1891. if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
  1892. CurContext->addDecl(D);
  1893. return DeclGroupPtrTy::make(DeclGroupRef(D));
  1894. }
  1895. return nullptr;
  1896. }
  1897. namespace {
  1898. class LocalVarRefChecker final
  1899. : public ConstStmtVisitor<LocalVarRefChecker, bool> {
  1900. Sema &SemaRef;
  1901. public:
  1902. bool VisitDeclRefExpr(const DeclRefExpr *E) {
  1903. if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
  1904. if (VD->hasLocalStorage()) {
  1905. SemaRef.Diag(E->getBeginLoc(),
  1906. diag::err_omp_local_var_in_threadprivate_init)
  1907. << E->getSourceRange();
  1908. SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
  1909. << VD << VD->getSourceRange();
  1910. return true;
  1911. }
  1912. }
  1913. return false;
  1914. }
  1915. bool VisitStmt(const Stmt *S) {
  1916. for (const Stmt *Child : S->children()) {
  1917. if (Child && Visit(Child))
  1918. return true;
  1919. }
  1920. return false;
  1921. }
  1922. explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
  1923. };
  1924. } // namespace
  1925. OMPThreadPrivateDecl *
  1926. Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
  1927. SmallVector<Expr *, 8> Vars;
  1928. for (Expr *RefExpr : VarList) {
  1929. auto *DE = cast<DeclRefExpr>(RefExpr);
  1930. auto *VD = cast<VarDecl>(DE->getDecl());
  1931. SourceLocation ILoc = DE->getExprLoc();
  1932. // Mark variable as used.
  1933. VD->setReferenced();
  1934. VD->markUsed(Context);
  1935. QualType QType = VD->getType();
  1936. if (QType->isDependentType() || QType->isInstantiationDependentType()) {
  1937. // It will be analyzed later.
  1938. Vars.push_back(DE);
  1939. continue;
  1940. }
  1941. // OpenMP [2.9.2, Restrictions, C/C++, p.10]
  1942. // A threadprivate variable must not have an incomplete type.
  1943. if (RequireCompleteType(ILoc, VD->getType(),
  1944. diag::err_omp_threadprivate_incomplete_type)) {
  1945. continue;
  1946. }
  1947. // OpenMP [2.9.2, Restrictions, C/C++, p.10]
  1948. // A threadprivate variable must not have a reference type.
  1949. if (VD->getType()->isReferenceType()) {
  1950. Diag(ILoc, diag::err_omp_ref_type_arg)
  1951. << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
  1952. bool IsDecl =
  1953. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  1954. Diag(VD->getLocation(),
  1955. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  1956. << VD;
  1957. continue;
  1958. }
  1959. // Check if this is a TLS variable. If TLS is not being supported, produce
  1960. // the corresponding diagnostic.
  1961. if ((VD->getTLSKind() != VarDecl::TLS_None &&
  1962. !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
  1963. getLangOpts().OpenMPUseTLS &&
  1964. getASTContext().getTargetInfo().isTLSSupported())) ||
  1965. (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
  1966. !VD->isLocalVarDecl())) {
  1967. Diag(ILoc, diag::err_omp_var_thread_local)
  1968. << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
  1969. bool IsDecl =
  1970. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  1971. Diag(VD->getLocation(),
  1972. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  1973. << VD;
  1974. continue;
  1975. }
  1976. // Check if initial value of threadprivate variable reference variable with
  1977. // local storage (it is not supported by runtime).
  1978. if (const Expr *Init = VD->getAnyInitializer()) {
  1979. LocalVarRefChecker Checker(*this);
  1980. if (Checker.Visit(Init))
  1981. continue;
  1982. }
  1983. Vars.push_back(RefExpr);
  1984. DSAStack->addDSA(VD, DE, OMPC_threadprivate);
  1985. VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
  1986. Context, SourceRange(Loc, Loc)));
  1987. if (ASTMutationListener *ML = Context.getASTMutationListener())
  1988. ML->DeclarationMarkedOpenMPThreadPrivate(VD);
  1989. }
  1990. OMPThreadPrivateDecl *D = nullptr;
  1991. if (!Vars.empty()) {
  1992. D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
  1993. Vars);
  1994. D->setAccess(AS_public);
  1995. }
  1996. return D;
  1997. }
  1998. Sema::DeclGroupPtrTy
  1999. Sema::ActOnOpenMPAllocateDirective(SourceLocation Loc, ArrayRef<Expr *> VarList,
  2000. DeclContext *Owner) {
  2001. SmallVector<Expr *, 8> Vars;
  2002. for (Expr *RefExpr : VarList) {
  2003. auto *DE = cast<DeclRefExpr>(RefExpr);
  2004. auto *VD = cast<VarDecl>(DE->getDecl());
  2005. // Check if this is a TLS variable or global register.
  2006. if (VD->getTLSKind() != VarDecl::TLS_None ||
  2007. VD->hasAttr<OMPThreadPrivateDeclAttr>() ||
  2008. (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
  2009. !VD->isLocalVarDecl()))
  2010. continue;
  2011. // Do not apply for parameters.
  2012. if (isa<ParmVarDecl>(VD))
  2013. continue;
  2014. Vars.push_back(RefExpr);
  2015. VD->addAttr(
  2016. OMPAllocateDeclAttr::CreateImplicit(Context, DE->getSourceRange()));
  2017. if (ASTMutationListener *ML = Context.getASTMutationListener())
  2018. ML->DeclarationMarkedOpenMPAllocate(VD,
  2019. VD->getAttr<OMPAllocateDeclAttr>());
  2020. }
  2021. if (Vars.empty())
  2022. return nullptr;
  2023. if (!Owner)
  2024. Owner = getCurLexicalContext();
  2025. OMPAllocateDecl *D = OMPAllocateDecl::Create(Context, Owner, Loc, Vars);
  2026. D->setAccess(AS_public);
  2027. Owner->addDecl(D);
  2028. return DeclGroupPtrTy::make(DeclGroupRef(D));
  2029. }
  2030. Sema::DeclGroupPtrTy
  2031. Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
  2032. ArrayRef<OMPClause *> ClauseList) {
  2033. OMPRequiresDecl *D = nullptr;
  2034. if (!CurContext->isFileContext()) {
  2035. Diag(Loc, diag::err_omp_invalid_scope) << "requires";
  2036. } else {
  2037. D = CheckOMPRequiresDecl(Loc, ClauseList);
  2038. if (D) {
  2039. CurContext->addDecl(D);
  2040. DSAStack->addRequiresDecl(D);
  2041. }
  2042. }
  2043. return DeclGroupPtrTy::make(DeclGroupRef(D));
  2044. }
  2045. OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
  2046. ArrayRef<OMPClause *> ClauseList) {
  2047. if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
  2048. return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
  2049. ClauseList);
  2050. return nullptr;
  2051. }
  2052. static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
  2053. const ValueDecl *D,
  2054. const DSAStackTy::DSAVarData &DVar,
  2055. bool IsLoopIterVar = false) {
  2056. if (DVar.RefExpr) {
  2057. SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
  2058. << getOpenMPClauseName(DVar.CKind);
  2059. return;
  2060. }
  2061. enum {
  2062. PDSA_StaticMemberShared,
  2063. PDSA_StaticLocalVarShared,
  2064. PDSA_LoopIterVarPrivate,
  2065. PDSA_LoopIterVarLinear,
  2066. PDSA_LoopIterVarLastprivate,
  2067. PDSA_ConstVarShared,
  2068. PDSA_GlobalVarShared,
  2069. PDSA_TaskVarFirstprivate,
  2070. PDSA_LocalVarPrivate,
  2071. PDSA_Implicit
  2072. } Reason = PDSA_Implicit;
  2073. bool ReportHint = false;
  2074. auto ReportLoc = D->getLocation();
  2075. auto *VD = dyn_cast<VarDecl>(D);
  2076. if (IsLoopIterVar) {
  2077. if (DVar.CKind == OMPC_private)
  2078. Reason = PDSA_LoopIterVarPrivate;
  2079. else if (DVar.CKind == OMPC_lastprivate)
  2080. Reason = PDSA_LoopIterVarLastprivate;
  2081. else
  2082. Reason = PDSA_LoopIterVarLinear;
  2083. } else if (isOpenMPTaskingDirective(DVar.DKind) &&
  2084. DVar.CKind == OMPC_firstprivate) {
  2085. Reason = PDSA_TaskVarFirstprivate;
  2086. ReportLoc = DVar.ImplicitDSALoc;
  2087. } else if (VD && VD->isStaticLocal())
  2088. Reason = PDSA_StaticLocalVarShared;
  2089. else if (VD && VD->isStaticDataMember())
  2090. Reason = PDSA_StaticMemberShared;
  2091. else if (VD && VD->isFileVarDecl())
  2092. Reason = PDSA_GlobalVarShared;
  2093. else if (D->getType().isConstant(SemaRef.getASTContext()))
  2094. Reason = PDSA_ConstVarShared;
  2095. else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
  2096. ReportHint = true;
  2097. Reason = PDSA_LocalVarPrivate;
  2098. }
  2099. if (Reason != PDSA_Implicit) {
  2100. SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
  2101. << Reason << ReportHint
  2102. << getOpenMPDirectiveName(Stack->getCurrentDirective());
  2103. } else if (DVar.ImplicitDSALoc.isValid()) {
  2104. SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
  2105. << getOpenMPClauseName(DVar.CKind);
  2106. }
  2107. }
  2108. namespace {
  2109. class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
  2110. DSAStackTy *Stack;
  2111. Sema &SemaRef;
  2112. bool ErrorFound = false;
  2113. CapturedStmt *CS = nullptr;
  2114. llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
  2115. llvm::SmallVector<Expr *, 4> ImplicitMap;
  2116. Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
  2117. llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
  2118. void VisitSubCaptures(OMPExecutableDirective *S) {
  2119. // Check implicitly captured variables.
  2120. if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
  2121. return;
  2122. for (const CapturedStmt::Capture &Cap :
  2123. S->getInnermostCapturedStmt()->captures()) {
  2124. if (!Cap.capturesVariable())
  2125. continue;
  2126. VarDecl *VD = Cap.getCapturedVar();
  2127. // Do not try to map the variable if it or its sub-component was mapped
  2128. // already.
  2129. if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
  2130. Stack->checkMappableExprComponentListsForDecl(
  2131. VD, /*CurrentRegionOnly=*/true,
  2132. [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
  2133. OpenMPClauseKind) { return true; }))
  2134. continue;
  2135. DeclRefExpr *DRE = buildDeclRefExpr(
  2136. SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
  2137. Cap.getLocation(), /*RefersToCapture=*/true);
  2138. Visit(DRE);
  2139. }
  2140. }
  2141. public:
  2142. void VisitDeclRefExpr(DeclRefExpr *E) {
  2143. if (E->isTypeDependent() || E->isValueDependent() ||
  2144. E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
  2145. return;
  2146. if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
  2147. VD = VD->getCanonicalDecl();
  2148. // Skip internally declared variables.
  2149. if (VD->hasLocalStorage() && !CS->capturesVariable(VD))
  2150. return;
  2151. DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
  2152. // Check if the variable has explicit DSA set and stop analysis if it so.
  2153. if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
  2154. return;
  2155. // Skip internally declared static variables.
  2156. llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
  2157. OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
  2158. if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) &&
  2159. (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
  2160. return;
  2161. SourceLocation ELoc = E->getExprLoc();
  2162. OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
  2163. // The default(none) clause requires that each variable that is referenced
  2164. // in the construct, and does not have a predetermined data-sharing
  2165. // attribute, must have its data-sharing attribute explicitly determined
  2166. // by being listed in a data-sharing attribute clause.
  2167. if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
  2168. isImplicitOrExplicitTaskingRegion(DKind) &&
  2169. VarsWithInheritedDSA.count(VD) == 0) {
  2170. VarsWithInheritedDSA[VD] = E;
  2171. return;
  2172. }
  2173. if (isOpenMPTargetExecutionDirective(DKind) &&
  2174. !Stack->isLoopControlVariable(VD).first) {
  2175. if (!Stack->checkMappableExprComponentListsForDecl(
  2176. VD, /*CurrentRegionOnly=*/true,
  2177. [](OMPClauseMappableExprCommon::MappableExprComponentListRef
  2178. StackComponents,
  2179. OpenMPClauseKind) {
  2180. // Variable is used if it has been marked as an array, array
  2181. // section or the variable iself.
  2182. return StackComponents.size() == 1 ||
  2183. std::all_of(
  2184. std::next(StackComponents.rbegin()),
  2185. StackComponents.rend(),
  2186. [](const OMPClauseMappableExprCommon::
  2187. MappableComponent &MC) {
  2188. return MC.getAssociatedDeclaration() ==
  2189. nullptr &&
  2190. (isa<OMPArraySectionExpr>(
  2191. MC.getAssociatedExpression()) ||
  2192. isa<ArraySubscriptExpr>(
  2193. MC.getAssociatedExpression()));
  2194. });
  2195. })) {
  2196. bool IsFirstprivate = false;
  2197. // By default lambdas are captured as firstprivates.
  2198. if (const auto *RD =
  2199. VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
  2200. IsFirstprivate = RD->isLambda();
  2201. IsFirstprivate =
  2202. IsFirstprivate ||
  2203. (VD->getType().getNonReferenceType()->isScalarType() &&
  2204. Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res);
  2205. if (IsFirstprivate)
  2206. ImplicitFirstprivate.emplace_back(E);
  2207. else
  2208. ImplicitMap.emplace_back(E);
  2209. return;
  2210. }
  2211. }
  2212. // OpenMP [2.9.3.6, Restrictions, p.2]
  2213. // A list item that appears in a reduction clause of the innermost
  2214. // enclosing worksharing or parallel construct may not be accessed in an
  2215. // explicit task.
  2216. DVar = Stack->hasInnermostDSA(
  2217. VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
  2218. [](OpenMPDirectiveKind K) {
  2219. return isOpenMPParallelDirective(K) ||
  2220. isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
  2221. },
  2222. /*FromParent=*/true);
  2223. if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
  2224. ErrorFound = true;
  2225. SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
  2226. reportOriginalDsa(SemaRef, Stack, VD, DVar);
  2227. return;
  2228. }
  2229. // Define implicit data-sharing attributes for task.
  2230. DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
  2231. if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
  2232. !Stack->isLoopControlVariable(VD).first)
  2233. ImplicitFirstprivate.push_back(E);
  2234. }
  2235. }
  2236. void VisitMemberExpr(MemberExpr *E) {
  2237. if (E->isTypeDependent() || E->isValueDependent() ||
  2238. E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
  2239. return;
  2240. auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
  2241. OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
  2242. if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParens())) {
  2243. if (!FD)
  2244. return;
  2245. DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
  2246. // Check if the variable has explicit DSA set and stop analysis if it
  2247. // so.
  2248. if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
  2249. return;
  2250. if (isOpenMPTargetExecutionDirective(DKind) &&
  2251. !Stack->isLoopControlVariable(FD).first &&
  2252. !Stack->checkMappableExprComponentListsForDecl(
  2253. FD, /*CurrentRegionOnly=*/true,
  2254. [](OMPClauseMappableExprCommon::MappableExprComponentListRef
  2255. StackComponents,
  2256. OpenMPClauseKind) {
  2257. return isa<CXXThisExpr>(
  2258. cast<MemberExpr>(
  2259. StackComponents.back().getAssociatedExpression())
  2260. ->getBase()
  2261. ->IgnoreParens());
  2262. })) {
  2263. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
  2264. // A bit-field cannot appear in a map clause.
  2265. //
  2266. if (FD->isBitField())
  2267. return;
  2268. // Check to see if the member expression is referencing a class that
  2269. // has already been explicitly mapped
  2270. if (Stack->isClassPreviouslyMapped(TE->getType()))
  2271. return;
  2272. ImplicitMap.emplace_back(E);
  2273. return;
  2274. }
  2275. SourceLocation ELoc = E->getExprLoc();
  2276. // OpenMP [2.9.3.6, Restrictions, p.2]
  2277. // A list item that appears in a reduction clause of the innermost
  2278. // enclosing worksharing or parallel construct may not be accessed in
  2279. // an explicit task.
  2280. DVar = Stack->hasInnermostDSA(
  2281. FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
  2282. [](OpenMPDirectiveKind K) {
  2283. return isOpenMPParallelDirective(K) ||
  2284. isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
  2285. },
  2286. /*FromParent=*/true);
  2287. if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
  2288. ErrorFound = true;
  2289. SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
  2290. reportOriginalDsa(SemaRef, Stack, FD, DVar);
  2291. return;
  2292. }
  2293. // Define implicit data-sharing attributes for task.
  2294. DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
  2295. if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
  2296. !Stack->isLoopControlVariable(FD).first) {
  2297. // Check if there is a captured expression for the current field in the
  2298. // region. Do not mark it as firstprivate unless there is no captured
  2299. // expression.
  2300. // TODO: try to make it firstprivate.
  2301. if (DVar.CKind != OMPC_unknown)
  2302. ImplicitFirstprivate.push_back(E);
  2303. }
  2304. return;
  2305. }
  2306. if (isOpenMPTargetExecutionDirective(DKind)) {
  2307. OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
  2308. if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
  2309. /*NoDiagnose=*/true))
  2310. return;
  2311. const auto *VD = cast<ValueDecl>(
  2312. CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
  2313. if (!Stack->checkMappableExprComponentListsForDecl(
  2314. VD, /*CurrentRegionOnly=*/true,
  2315. [&CurComponents](
  2316. OMPClauseMappableExprCommon::MappableExprComponentListRef
  2317. StackComponents,
  2318. OpenMPClauseKind) {
  2319. auto CCI = CurComponents.rbegin();
  2320. auto CCE = CurComponents.rend();
  2321. for (const auto &SC : llvm::reverse(StackComponents)) {
  2322. // Do both expressions have the same kind?
  2323. if (CCI->getAssociatedExpression()->getStmtClass() !=
  2324. SC.getAssociatedExpression()->getStmtClass())
  2325. if (!(isa<OMPArraySectionExpr>(
  2326. SC.getAssociatedExpression()) &&
  2327. isa<ArraySubscriptExpr>(
  2328. CCI->getAssociatedExpression())))
  2329. return false;
  2330. const Decl *CCD = CCI->getAssociatedDeclaration();
  2331. const Decl *SCD = SC.getAssociatedDeclaration();
  2332. CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
  2333. SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
  2334. if (SCD != CCD)
  2335. return false;
  2336. std::advance(CCI, 1);
  2337. if (CCI == CCE)
  2338. break;
  2339. }
  2340. return true;
  2341. })) {
  2342. Visit(E->getBase());
  2343. }
  2344. } else {
  2345. Visit(E->getBase());
  2346. }
  2347. }
  2348. void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
  2349. for (OMPClause *C : S->clauses()) {
  2350. // Skip analysis of arguments of implicitly defined firstprivate clause
  2351. // for task|target directives.
  2352. // Skip analysis of arguments of implicitly defined map clause for target
  2353. // directives.
  2354. if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
  2355. C->isImplicit())) {
  2356. for (Stmt *CC : C->children()) {
  2357. if (CC)
  2358. Visit(CC);
  2359. }
  2360. }
  2361. }
  2362. // Check implicitly captured variables.
  2363. VisitSubCaptures(S);
  2364. }
  2365. void VisitStmt(Stmt *S) {
  2366. for (Stmt *C : S->children()) {
  2367. if (C) {
  2368. // Check implicitly captured variables in the task-based directives to
  2369. // check if they must be firstprivatized.
  2370. Visit(C);
  2371. }
  2372. }
  2373. }
  2374. bool isErrorFound() const { return ErrorFound; }
  2375. ArrayRef<Expr *> getImplicitFirstprivate() const {
  2376. return ImplicitFirstprivate;
  2377. }
  2378. ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
  2379. const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
  2380. return VarsWithInheritedDSA;
  2381. }
  2382. DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
  2383. : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
  2384. };
  2385. } // namespace
  2386. void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
  2387. switch (DKind) {
  2388. case OMPD_parallel:
  2389. case OMPD_parallel_for:
  2390. case OMPD_parallel_for_simd:
  2391. case OMPD_parallel_sections:
  2392. case OMPD_teams:
  2393. case OMPD_teams_distribute:
  2394. case OMPD_teams_distribute_simd: {
  2395. QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
  2396. QualType KmpInt32PtrTy =
  2397. Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
  2398. Sema::CapturedParamNameType Params[] = {
  2399. std::make_pair(".global_tid.", KmpInt32PtrTy),
  2400. std::make_pair(".bound_tid.", KmpInt32PtrTy),
  2401. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2402. };
  2403. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2404. Params);
  2405. break;
  2406. }
  2407. case OMPD_target_teams:
  2408. case OMPD_target_parallel:
  2409. case OMPD_target_parallel_for:
  2410. case OMPD_target_parallel_for_simd:
  2411. case OMPD_target_teams_distribute:
  2412. case OMPD_target_teams_distribute_simd: {
  2413. QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
  2414. QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
  2415. QualType KmpInt32PtrTy =
  2416. Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
  2417. QualType Args[] = {VoidPtrTy};
  2418. FunctionProtoType::ExtProtoInfo EPI;
  2419. EPI.Variadic = true;
  2420. QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
  2421. Sema::CapturedParamNameType Params[] = {
  2422. std::make_pair(".global_tid.", KmpInt32Ty),
  2423. std::make_pair(".part_id.", KmpInt32PtrTy),
  2424. std::make_pair(".privates.", VoidPtrTy),
  2425. std::make_pair(
  2426. ".copy_fn.",
  2427. Context.getPointerType(CopyFnType).withConst().withRestrict()),
  2428. std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
  2429. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2430. };
  2431. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2432. Params);
  2433. // Mark this captured region as inlined, because we don't use outlined
  2434. // function directly.
  2435. getCurCapturedRegion()->TheCapturedDecl->addAttr(
  2436. AlwaysInlineAttr::CreateImplicit(
  2437. Context, AlwaysInlineAttr::Keyword_forceinline));
  2438. Sema::CapturedParamNameType ParamsTarget[] = {
  2439. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2440. };
  2441. // Start a captured region for 'target' with no implicit parameters.
  2442. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2443. ParamsTarget);
  2444. Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
  2445. std::make_pair(".global_tid.", KmpInt32PtrTy),
  2446. std::make_pair(".bound_tid.", KmpInt32PtrTy),
  2447. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2448. };
  2449. // Start a captured region for 'teams' or 'parallel'. Both regions have
  2450. // the same implicit parameters.
  2451. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2452. ParamsTeamsOrParallel);
  2453. break;
  2454. }
  2455. case OMPD_target:
  2456. case OMPD_target_simd: {
  2457. QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
  2458. QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
  2459. QualType KmpInt32PtrTy =
  2460. Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
  2461. QualType Args[] = {VoidPtrTy};
  2462. FunctionProtoType::ExtProtoInfo EPI;
  2463. EPI.Variadic = true;
  2464. QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
  2465. Sema::CapturedParamNameType Params[] = {
  2466. std::make_pair(".global_tid.", KmpInt32Ty),
  2467. std::make_pair(".part_id.", KmpInt32PtrTy),
  2468. std::make_pair(".privates.", VoidPtrTy),
  2469. std::make_pair(
  2470. ".copy_fn.",
  2471. Context.getPointerType(CopyFnType).withConst().withRestrict()),
  2472. std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
  2473. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2474. };
  2475. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2476. Params);
  2477. // Mark this captured region as inlined, because we don't use outlined
  2478. // function directly.
  2479. getCurCapturedRegion()->TheCapturedDecl->addAttr(
  2480. AlwaysInlineAttr::CreateImplicit(
  2481. Context, AlwaysInlineAttr::Keyword_forceinline));
  2482. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2483. std::make_pair(StringRef(), QualType()));
  2484. break;
  2485. }
  2486. case OMPD_simd:
  2487. case OMPD_for:
  2488. case OMPD_for_simd:
  2489. case OMPD_sections:
  2490. case OMPD_section:
  2491. case OMPD_single:
  2492. case OMPD_master:
  2493. case OMPD_critical:
  2494. case OMPD_taskgroup:
  2495. case OMPD_distribute:
  2496. case OMPD_distribute_simd:
  2497. case OMPD_ordered:
  2498. case OMPD_atomic:
  2499. case OMPD_target_data: {
  2500. Sema::CapturedParamNameType Params[] = {
  2501. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2502. };
  2503. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2504. Params);
  2505. break;
  2506. }
  2507. case OMPD_task: {
  2508. QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
  2509. QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
  2510. QualType KmpInt32PtrTy =
  2511. Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
  2512. QualType Args[] = {VoidPtrTy};
  2513. FunctionProtoType::ExtProtoInfo EPI;
  2514. EPI.Variadic = true;
  2515. QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
  2516. Sema::CapturedParamNameType Params[] = {
  2517. std::make_pair(".global_tid.", KmpInt32Ty),
  2518. std::make_pair(".part_id.", KmpInt32PtrTy),
  2519. std::make_pair(".privates.", VoidPtrTy),
  2520. std::make_pair(
  2521. ".copy_fn.",
  2522. Context.getPointerType(CopyFnType).withConst().withRestrict()),
  2523. std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
  2524. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2525. };
  2526. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2527. Params);
  2528. // Mark this captured region as inlined, because we don't use outlined
  2529. // function directly.
  2530. getCurCapturedRegion()->TheCapturedDecl->addAttr(
  2531. AlwaysInlineAttr::CreateImplicit(
  2532. Context, AlwaysInlineAttr::Keyword_forceinline));
  2533. break;
  2534. }
  2535. case OMPD_taskloop:
  2536. case OMPD_taskloop_simd: {
  2537. QualType KmpInt32Ty =
  2538. Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
  2539. .withConst();
  2540. QualType KmpUInt64Ty =
  2541. Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
  2542. .withConst();
  2543. QualType KmpInt64Ty =
  2544. Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
  2545. .withConst();
  2546. QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
  2547. QualType KmpInt32PtrTy =
  2548. Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
  2549. QualType Args[] = {VoidPtrTy};
  2550. FunctionProtoType::ExtProtoInfo EPI;
  2551. EPI.Variadic = true;
  2552. QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
  2553. Sema::CapturedParamNameType Params[] = {
  2554. std::make_pair(".global_tid.", KmpInt32Ty),
  2555. std::make_pair(".part_id.", KmpInt32PtrTy),
  2556. std::make_pair(".privates.", VoidPtrTy),
  2557. std::make_pair(
  2558. ".copy_fn.",
  2559. Context.getPointerType(CopyFnType).withConst().withRestrict()),
  2560. std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
  2561. std::make_pair(".lb.", KmpUInt64Ty),
  2562. std::make_pair(".ub.", KmpUInt64Ty),
  2563. std::make_pair(".st.", KmpInt64Ty),
  2564. std::make_pair(".liter.", KmpInt32Ty),
  2565. std::make_pair(".reductions.", VoidPtrTy),
  2566. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2567. };
  2568. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2569. Params);
  2570. // Mark this captured region as inlined, because we don't use outlined
  2571. // function directly.
  2572. getCurCapturedRegion()->TheCapturedDecl->addAttr(
  2573. AlwaysInlineAttr::CreateImplicit(
  2574. Context, AlwaysInlineAttr::Keyword_forceinline));
  2575. break;
  2576. }
  2577. case OMPD_distribute_parallel_for_simd:
  2578. case OMPD_distribute_parallel_for: {
  2579. QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
  2580. QualType KmpInt32PtrTy =
  2581. Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
  2582. Sema::CapturedParamNameType Params[] = {
  2583. std::make_pair(".global_tid.", KmpInt32PtrTy),
  2584. std::make_pair(".bound_tid.", KmpInt32PtrTy),
  2585. std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
  2586. std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
  2587. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2588. };
  2589. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2590. Params);
  2591. break;
  2592. }
  2593. case OMPD_target_teams_distribute_parallel_for:
  2594. case OMPD_target_teams_distribute_parallel_for_simd: {
  2595. QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
  2596. QualType KmpInt32PtrTy =
  2597. Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
  2598. QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
  2599. QualType Args[] = {VoidPtrTy};
  2600. FunctionProtoType::ExtProtoInfo EPI;
  2601. EPI.Variadic = true;
  2602. QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
  2603. Sema::CapturedParamNameType Params[] = {
  2604. std::make_pair(".global_tid.", KmpInt32Ty),
  2605. std::make_pair(".part_id.", KmpInt32PtrTy),
  2606. std::make_pair(".privates.", VoidPtrTy),
  2607. std::make_pair(
  2608. ".copy_fn.",
  2609. Context.getPointerType(CopyFnType).withConst().withRestrict()),
  2610. std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
  2611. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2612. };
  2613. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2614. Params);
  2615. // Mark this captured region as inlined, because we don't use outlined
  2616. // function directly.
  2617. getCurCapturedRegion()->TheCapturedDecl->addAttr(
  2618. AlwaysInlineAttr::CreateImplicit(
  2619. Context, AlwaysInlineAttr::Keyword_forceinline));
  2620. Sema::CapturedParamNameType ParamsTarget[] = {
  2621. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2622. };
  2623. // Start a captured region for 'target' with no implicit parameters.
  2624. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2625. ParamsTarget);
  2626. Sema::CapturedParamNameType ParamsTeams[] = {
  2627. std::make_pair(".global_tid.", KmpInt32PtrTy),
  2628. std::make_pair(".bound_tid.", KmpInt32PtrTy),
  2629. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2630. };
  2631. // Start a captured region for 'target' with no implicit parameters.
  2632. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2633. ParamsTeams);
  2634. Sema::CapturedParamNameType ParamsParallel[] = {
  2635. std::make_pair(".global_tid.", KmpInt32PtrTy),
  2636. std::make_pair(".bound_tid.", KmpInt32PtrTy),
  2637. std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
  2638. std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
  2639. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2640. };
  2641. // Start a captured region for 'teams' or 'parallel'. Both regions have
  2642. // the same implicit parameters.
  2643. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2644. ParamsParallel);
  2645. break;
  2646. }
  2647. case OMPD_teams_distribute_parallel_for:
  2648. case OMPD_teams_distribute_parallel_for_simd: {
  2649. QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
  2650. QualType KmpInt32PtrTy =
  2651. Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
  2652. Sema::CapturedParamNameType ParamsTeams[] = {
  2653. std::make_pair(".global_tid.", KmpInt32PtrTy),
  2654. std::make_pair(".bound_tid.", KmpInt32PtrTy),
  2655. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2656. };
  2657. // Start a captured region for 'target' with no implicit parameters.
  2658. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2659. ParamsTeams);
  2660. Sema::CapturedParamNameType ParamsParallel[] = {
  2661. std::make_pair(".global_tid.", KmpInt32PtrTy),
  2662. std::make_pair(".bound_tid.", KmpInt32PtrTy),
  2663. std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
  2664. std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
  2665. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2666. };
  2667. // Start a captured region for 'teams' or 'parallel'. Both regions have
  2668. // the same implicit parameters.
  2669. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2670. ParamsParallel);
  2671. break;
  2672. }
  2673. case OMPD_target_update:
  2674. case OMPD_target_enter_data:
  2675. case OMPD_target_exit_data: {
  2676. QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
  2677. QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
  2678. QualType KmpInt32PtrTy =
  2679. Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
  2680. QualType Args[] = {VoidPtrTy};
  2681. FunctionProtoType::ExtProtoInfo EPI;
  2682. EPI.Variadic = true;
  2683. QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
  2684. Sema::CapturedParamNameType Params[] = {
  2685. std::make_pair(".global_tid.", KmpInt32Ty),
  2686. std::make_pair(".part_id.", KmpInt32PtrTy),
  2687. std::make_pair(".privates.", VoidPtrTy),
  2688. std::make_pair(
  2689. ".copy_fn.",
  2690. Context.getPointerType(CopyFnType).withConst().withRestrict()),
  2691. std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
  2692. std::make_pair(StringRef(), QualType()) // __context with shared vars
  2693. };
  2694. ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
  2695. Params);
  2696. // Mark this captured region as inlined, because we don't use outlined
  2697. // function directly.
  2698. getCurCapturedRegion()->TheCapturedDecl->addAttr(
  2699. AlwaysInlineAttr::CreateImplicit(
  2700. Context, AlwaysInlineAttr::Keyword_forceinline));
  2701. break;
  2702. }
  2703. case OMPD_threadprivate:
  2704. case OMPD_allocate:
  2705. case OMPD_taskyield:
  2706. case OMPD_barrier:
  2707. case OMPD_taskwait:
  2708. case OMPD_cancellation_point:
  2709. case OMPD_cancel:
  2710. case OMPD_flush:
  2711. case OMPD_declare_reduction:
  2712. case OMPD_declare_mapper:
  2713. case OMPD_declare_simd:
  2714. case OMPD_declare_target:
  2715. case OMPD_end_declare_target:
  2716. case OMPD_requires:
  2717. llvm_unreachable("OpenMP Directive is not allowed");
  2718. case OMPD_unknown:
  2719. llvm_unreachable("Unknown OpenMP directive");
  2720. }
  2721. }
  2722. int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
  2723. SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
  2724. getOpenMPCaptureRegions(CaptureRegions, DKind);
  2725. return CaptureRegions.size();
  2726. }
  2727. static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
  2728. Expr *CaptureExpr, bool WithInit,
  2729. bool AsExpression) {
  2730. assert(CaptureExpr);
  2731. ASTContext &C = S.getASTContext();
  2732. Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
  2733. QualType Ty = Init->getType();
  2734. if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
  2735. if (S.getLangOpts().CPlusPlus) {
  2736. Ty = C.getLValueReferenceType(Ty);
  2737. } else {
  2738. Ty = C.getPointerType(Ty);
  2739. ExprResult Res =
  2740. S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
  2741. if (!Res.isUsable())
  2742. return nullptr;
  2743. Init = Res.get();
  2744. }
  2745. WithInit = true;
  2746. }
  2747. auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
  2748. CaptureExpr->getBeginLoc());
  2749. if (!WithInit)
  2750. CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
  2751. S.CurContext->addHiddenDecl(CED);
  2752. S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
  2753. return CED;
  2754. }
  2755. static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
  2756. bool WithInit) {
  2757. OMPCapturedExprDecl *CD;
  2758. if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
  2759. CD = cast<OMPCapturedExprDecl>(VD);
  2760. else
  2761. CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
  2762. /*AsExpression=*/false);
  2763. return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
  2764. CaptureExpr->getExprLoc());
  2765. }
  2766. static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
  2767. CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
  2768. if (!Ref) {
  2769. OMPCapturedExprDecl *CD = buildCaptureDecl(
  2770. S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
  2771. /*WithInit=*/true, /*AsExpression=*/true);
  2772. Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
  2773. CaptureExpr->getExprLoc());
  2774. }
  2775. ExprResult Res = Ref;
  2776. if (!S.getLangOpts().CPlusPlus &&
  2777. CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
  2778. Ref->getType()->isPointerType()) {
  2779. Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
  2780. if (!Res.isUsable())
  2781. return ExprError();
  2782. }
  2783. return S.DefaultLvalueConversion(Res.get());
  2784. }
  2785. namespace {
  2786. // OpenMP directives parsed in this section are represented as a
  2787. // CapturedStatement with an associated statement. If a syntax error
  2788. // is detected during the parsing of the associated statement, the
  2789. // compiler must abort processing and close the CapturedStatement.
  2790. //
  2791. // Combined directives such as 'target parallel' have more than one
  2792. // nested CapturedStatements. This RAII ensures that we unwind out
  2793. // of all the nested CapturedStatements when an error is found.
  2794. class CaptureRegionUnwinderRAII {
  2795. private:
  2796. Sema &S;
  2797. bool &ErrorFound;
  2798. OpenMPDirectiveKind DKind = OMPD_unknown;
  2799. public:
  2800. CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
  2801. OpenMPDirectiveKind DKind)
  2802. : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
  2803. ~CaptureRegionUnwinderRAII() {
  2804. if (ErrorFound) {
  2805. int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
  2806. while (--ThisCaptureLevel >= 0)
  2807. S.ActOnCapturedRegionError();
  2808. }
  2809. }
  2810. };
  2811. } // namespace
  2812. StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
  2813. ArrayRef<OMPClause *> Clauses) {
  2814. bool ErrorFound = false;
  2815. CaptureRegionUnwinderRAII CaptureRegionUnwinder(
  2816. *this, ErrorFound, DSAStack->getCurrentDirective());
  2817. if (!S.isUsable()) {
  2818. ErrorFound = true;
  2819. return StmtError();
  2820. }
  2821. SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
  2822. getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
  2823. OMPOrderedClause *OC = nullptr;
  2824. OMPScheduleClause *SC = nullptr;
  2825. SmallVector<const OMPLinearClause *, 4> LCs;
  2826. SmallVector<const OMPClauseWithPreInit *, 4> PICs;
  2827. // This is required for proper codegen.
  2828. for (OMPClause *Clause : Clauses) {
  2829. if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
  2830. Clause->getClauseKind() == OMPC_in_reduction) {
  2831. // Capture taskgroup task_reduction descriptors inside the tasking regions
  2832. // with the corresponding in_reduction items.
  2833. auto *IRC = cast<OMPInReductionClause>(Clause);
  2834. for (Expr *E : IRC->taskgroup_descriptors())
  2835. if (E)
  2836. MarkDeclarationsReferencedInExpr(E);
  2837. }
  2838. if (isOpenMPPrivate(Clause->getClauseKind()) ||
  2839. Clause->getClauseKind() == OMPC_copyprivate ||
  2840. (getLangOpts().OpenMPUseTLS &&
  2841. getASTContext().getTargetInfo().isTLSSupported() &&
  2842. Clause->getClauseKind() == OMPC_copyin)) {
  2843. DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
  2844. // Mark all variables in private list clauses as used in inner region.
  2845. for (Stmt *VarRef : Clause->children()) {
  2846. if (auto *E = cast_or_null<Expr>(VarRef)) {
  2847. MarkDeclarationsReferencedInExpr(E);
  2848. }
  2849. }
  2850. DSAStack->setForceVarCapturing(/*V=*/false);
  2851. } else if (CaptureRegions.size() > 1 ||
  2852. CaptureRegions.back() != OMPD_unknown) {
  2853. if (auto *C = OMPClauseWithPreInit::get(Clause))
  2854. PICs.push_back(C);
  2855. if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
  2856. if (Expr *E = C->getPostUpdateExpr())
  2857. MarkDeclarationsReferencedInExpr(E);
  2858. }
  2859. }
  2860. if (Clause->getClauseKind() == OMPC_schedule)
  2861. SC = cast<OMPScheduleClause>(Clause);
  2862. else if (Clause->getClauseKind() == OMPC_ordered)
  2863. OC = cast<OMPOrderedClause>(Clause);
  2864. else if (Clause->getClauseKind() == OMPC_linear)
  2865. LCs.push_back(cast<OMPLinearClause>(Clause));
  2866. }
  2867. // OpenMP, 2.7.1 Loop Construct, Restrictions
  2868. // The nonmonotonic modifier cannot be specified if an ordered clause is
  2869. // specified.
  2870. if (SC &&
  2871. (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
  2872. SC->getSecondScheduleModifier() ==
  2873. OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
  2874. OC) {
  2875. Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
  2876. ? SC->getFirstScheduleModifierLoc()
  2877. : SC->getSecondScheduleModifierLoc(),
  2878. diag::err_omp_schedule_nonmonotonic_ordered)
  2879. << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
  2880. ErrorFound = true;
  2881. }
  2882. if (!LCs.empty() && OC && OC->getNumForLoops()) {
  2883. for (const OMPLinearClause *C : LCs) {
  2884. Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
  2885. << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
  2886. }
  2887. ErrorFound = true;
  2888. }
  2889. if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
  2890. isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
  2891. OC->getNumForLoops()) {
  2892. Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
  2893. << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
  2894. ErrorFound = true;
  2895. }
  2896. if (ErrorFound) {
  2897. return StmtError();
  2898. }
  2899. StmtResult SR = S;
  2900. for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
  2901. // Mark all variables in private list clauses as used in inner region.
  2902. // Required for proper codegen of combined directives.
  2903. // TODO: add processing for other clauses.
  2904. if (ThisCaptureRegion != OMPD_unknown) {
  2905. for (const clang::OMPClauseWithPreInit *C : PICs) {
  2906. OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
  2907. // Find the particular capture region for the clause if the
  2908. // directive is a combined one with multiple capture regions.
  2909. // If the directive is not a combined one, the capture region
  2910. // associated with the clause is OMPD_unknown and is generated
  2911. // only once.
  2912. if (CaptureRegion == ThisCaptureRegion ||
  2913. CaptureRegion == OMPD_unknown) {
  2914. if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
  2915. for (Decl *D : DS->decls())
  2916. MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
  2917. }
  2918. }
  2919. }
  2920. }
  2921. SR = ActOnCapturedRegionEnd(SR.get());
  2922. }
  2923. return SR;
  2924. }
  2925. static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
  2926. OpenMPDirectiveKind CancelRegion,
  2927. SourceLocation StartLoc) {
  2928. // CancelRegion is only needed for cancel and cancellation_point.
  2929. if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
  2930. return false;
  2931. if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
  2932. CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
  2933. return false;
  2934. SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
  2935. << getOpenMPDirectiveName(CancelRegion);
  2936. return true;
  2937. }
  2938. static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
  2939. OpenMPDirectiveKind CurrentRegion,
  2940. const DeclarationNameInfo &CurrentName,
  2941. OpenMPDirectiveKind CancelRegion,
  2942. SourceLocation StartLoc) {
  2943. if (Stack->getCurScope()) {
  2944. OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
  2945. OpenMPDirectiveKind OffendingRegion = ParentRegion;
  2946. bool NestingProhibited = false;
  2947. bool CloseNesting = true;
  2948. bool OrphanSeen = false;
  2949. enum {
  2950. NoRecommend,
  2951. ShouldBeInParallelRegion,
  2952. ShouldBeInOrderedRegion,
  2953. ShouldBeInTargetRegion,
  2954. ShouldBeInTeamsRegion
  2955. } Recommend = NoRecommend;
  2956. if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
  2957. // OpenMP [2.16, Nesting of Regions]
  2958. // OpenMP constructs may not be nested inside a simd region.
  2959. // OpenMP [2.8.1,simd Construct, Restrictions]
  2960. // An ordered construct with the simd clause is the only OpenMP
  2961. // construct that can appear in the simd region.
  2962. // Allowing a SIMD construct nested in another SIMD construct is an
  2963. // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
  2964. // message.
  2965. SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
  2966. ? diag::err_omp_prohibited_region_simd
  2967. : diag::warn_omp_nesting_simd);
  2968. return CurrentRegion != OMPD_simd;
  2969. }
  2970. if (ParentRegion == OMPD_atomic) {
  2971. // OpenMP [2.16, Nesting of Regions]
  2972. // OpenMP constructs may not be nested inside an atomic region.
  2973. SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
  2974. return true;
  2975. }
  2976. if (CurrentRegion == OMPD_section) {
  2977. // OpenMP [2.7.2, sections Construct, Restrictions]
  2978. // Orphaned section directives are prohibited. That is, the section
  2979. // directives must appear within the sections construct and must not be
  2980. // encountered elsewhere in the sections region.
  2981. if (ParentRegion != OMPD_sections &&
  2982. ParentRegion != OMPD_parallel_sections) {
  2983. SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
  2984. << (ParentRegion != OMPD_unknown)
  2985. << getOpenMPDirectiveName(ParentRegion);
  2986. return true;
  2987. }
  2988. return false;
  2989. }
  2990. // Allow some constructs (except teams and cancellation constructs) to be
  2991. // orphaned (they could be used in functions, called from OpenMP regions
  2992. // with the required preconditions).
  2993. if (ParentRegion == OMPD_unknown &&
  2994. !isOpenMPNestingTeamsDirective(CurrentRegion) &&
  2995. CurrentRegion != OMPD_cancellation_point &&
  2996. CurrentRegion != OMPD_cancel)
  2997. return false;
  2998. if (CurrentRegion == OMPD_cancellation_point ||
  2999. CurrentRegion == OMPD_cancel) {
  3000. // OpenMP [2.16, Nesting of Regions]
  3001. // A cancellation point construct for which construct-type-clause is
  3002. // taskgroup must be nested inside a task construct. A cancellation
  3003. // point construct for which construct-type-clause is not taskgroup must
  3004. // be closely nested inside an OpenMP construct that matches the type
  3005. // specified in construct-type-clause.
  3006. // A cancel construct for which construct-type-clause is taskgroup must be
  3007. // nested inside a task construct. A cancel construct for which
  3008. // construct-type-clause is not taskgroup must be closely nested inside an
  3009. // OpenMP construct that matches the type specified in
  3010. // construct-type-clause.
  3011. NestingProhibited =
  3012. !((CancelRegion == OMPD_parallel &&
  3013. (ParentRegion == OMPD_parallel ||
  3014. ParentRegion == OMPD_target_parallel)) ||
  3015. (CancelRegion == OMPD_for &&
  3016. (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
  3017. ParentRegion == OMPD_target_parallel_for ||
  3018. ParentRegion == OMPD_distribute_parallel_for ||
  3019. ParentRegion == OMPD_teams_distribute_parallel_for ||
  3020. ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
  3021. (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
  3022. (CancelRegion == OMPD_sections &&
  3023. (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
  3024. ParentRegion == OMPD_parallel_sections)));
  3025. OrphanSeen = ParentRegion == OMPD_unknown;
  3026. } else if (CurrentRegion == OMPD_master) {
  3027. // OpenMP [2.16, Nesting of Regions]
  3028. // A master region may not be closely nested inside a worksharing,
  3029. // atomic, or explicit task region.
  3030. NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
  3031. isOpenMPTaskingDirective(ParentRegion);
  3032. } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
  3033. // OpenMP [2.16, Nesting of Regions]
  3034. // A critical region may not be nested (closely or otherwise) inside a
  3035. // critical region with the same name. Note that this restriction is not
  3036. // sufficient to prevent deadlock.
  3037. SourceLocation PreviousCriticalLoc;
  3038. bool DeadLock = Stack->hasDirective(
  3039. [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
  3040. const DeclarationNameInfo &DNI,
  3041. SourceLocation Loc) {
  3042. if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
  3043. PreviousCriticalLoc = Loc;
  3044. return true;
  3045. }
  3046. return false;
  3047. },
  3048. false /* skip top directive */);
  3049. if (DeadLock) {
  3050. SemaRef.Diag(StartLoc,
  3051. diag::err_omp_prohibited_region_critical_same_name)
  3052. << CurrentName.getName();
  3053. if (PreviousCriticalLoc.isValid())
  3054. SemaRef.Diag(PreviousCriticalLoc,
  3055. diag::note_omp_previous_critical_region);
  3056. return true;
  3057. }
  3058. } else if (CurrentRegion == OMPD_barrier) {
  3059. // OpenMP [2.16, Nesting of Regions]
  3060. // A barrier region may not be closely nested inside a worksharing,
  3061. // explicit task, critical, ordered, atomic, or master region.
  3062. NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
  3063. isOpenMPTaskingDirective(ParentRegion) ||
  3064. ParentRegion == OMPD_master ||
  3065. ParentRegion == OMPD_critical ||
  3066. ParentRegion == OMPD_ordered;
  3067. } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
  3068. !isOpenMPParallelDirective(CurrentRegion) &&
  3069. !isOpenMPTeamsDirective(CurrentRegion)) {
  3070. // OpenMP [2.16, Nesting of Regions]
  3071. // A worksharing region may not be closely nested inside a worksharing,
  3072. // explicit task, critical, ordered, atomic, or master region.
  3073. NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
  3074. isOpenMPTaskingDirective(ParentRegion) ||
  3075. ParentRegion == OMPD_master ||
  3076. ParentRegion == OMPD_critical ||
  3077. ParentRegion == OMPD_ordered;
  3078. Recommend = ShouldBeInParallelRegion;
  3079. } else if (CurrentRegion == OMPD_ordered) {
  3080. // OpenMP [2.16, Nesting of Regions]
  3081. // An ordered region may not be closely nested inside a critical,
  3082. // atomic, or explicit task region.
  3083. // An ordered region must be closely nested inside a loop region (or
  3084. // parallel loop region) with an ordered clause.
  3085. // OpenMP [2.8.1,simd Construct, Restrictions]
  3086. // An ordered construct with the simd clause is the only OpenMP construct
  3087. // that can appear in the simd region.
  3088. NestingProhibited = ParentRegion == OMPD_critical ||
  3089. isOpenMPTaskingDirective(ParentRegion) ||
  3090. !(isOpenMPSimdDirective(ParentRegion) ||
  3091. Stack->isParentOrderedRegion());
  3092. Recommend = ShouldBeInOrderedRegion;
  3093. } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
  3094. // OpenMP [2.16, Nesting of Regions]
  3095. // If specified, a teams construct must be contained within a target
  3096. // construct.
  3097. NestingProhibited = ParentRegion != OMPD_target;
  3098. OrphanSeen = ParentRegion == OMPD_unknown;
  3099. Recommend = ShouldBeInTargetRegion;
  3100. }
  3101. if (!NestingProhibited &&
  3102. !isOpenMPTargetExecutionDirective(CurrentRegion) &&
  3103. !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
  3104. (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
  3105. // OpenMP [2.16, Nesting of Regions]
  3106. // distribute, parallel, parallel sections, parallel workshare, and the
  3107. // parallel loop and parallel loop SIMD constructs are the only OpenMP
  3108. // constructs that can be closely nested in the teams region.
  3109. NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
  3110. !isOpenMPDistributeDirective(CurrentRegion);
  3111. Recommend = ShouldBeInParallelRegion;
  3112. }
  3113. if (!NestingProhibited &&
  3114. isOpenMPNestingDistributeDirective(CurrentRegion)) {
  3115. // OpenMP 4.5 [2.17 Nesting of Regions]
  3116. // The region associated with the distribute construct must be strictly
  3117. // nested inside a teams region
  3118. NestingProhibited =
  3119. (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
  3120. Recommend = ShouldBeInTeamsRegion;
  3121. }
  3122. if (!NestingProhibited &&
  3123. (isOpenMPTargetExecutionDirective(CurrentRegion) ||
  3124. isOpenMPTargetDataManagementDirective(CurrentRegion))) {
  3125. // OpenMP 4.5 [2.17 Nesting of Regions]
  3126. // If a target, target update, target data, target enter data, or
  3127. // target exit data construct is encountered during execution of a
  3128. // target region, the behavior is unspecified.
  3129. NestingProhibited = Stack->hasDirective(
  3130. [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
  3131. SourceLocation) {
  3132. if (isOpenMPTargetExecutionDirective(K)) {
  3133. OffendingRegion = K;
  3134. return true;
  3135. }
  3136. return false;
  3137. },
  3138. false /* don't skip top directive */);
  3139. CloseNesting = false;
  3140. }
  3141. if (NestingProhibited) {
  3142. if (OrphanSeen) {
  3143. SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
  3144. << getOpenMPDirectiveName(CurrentRegion) << Recommend;
  3145. } else {
  3146. SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
  3147. << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
  3148. << Recommend << getOpenMPDirectiveName(CurrentRegion);
  3149. }
  3150. return true;
  3151. }
  3152. }
  3153. return false;
  3154. }
  3155. static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
  3156. ArrayRef<OMPClause *> Clauses,
  3157. ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
  3158. bool ErrorFound = false;
  3159. unsigned NamedModifiersNumber = 0;
  3160. SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
  3161. OMPD_unknown + 1);
  3162. SmallVector<SourceLocation, 4> NameModifierLoc;
  3163. for (const OMPClause *C : Clauses) {
  3164. if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
  3165. // At most one if clause without a directive-name-modifier can appear on
  3166. // the directive.
  3167. OpenMPDirectiveKind CurNM = IC->getNameModifier();
  3168. if (FoundNameModifiers[CurNM]) {
  3169. S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
  3170. << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
  3171. << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
  3172. ErrorFound = true;
  3173. } else if (CurNM != OMPD_unknown) {
  3174. NameModifierLoc.push_back(IC->getNameModifierLoc());
  3175. ++NamedModifiersNumber;
  3176. }
  3177. FoundNameModifiers[CurNM] = IC;
  3178. if (CurNM == OMPD_unknown)
  3179. continue;
  3180. // Check if the specified name modifier is allowed for the current
  3181. // directive.
  3182. // At most one if clause with the particular directive-name-modifier can
  3183. // appear on the directive.
  3184. bool MatchFound = false;
  3185. for (auto NM : AllowedNameModifiers) {
  3186. if (CurNM == NM) {
  3187. MatchFound = true;
  3188. break;
  3189. }
  3190. }
  3191. if (!MatchFound) {
  3192. S.Diag(IC->getNameModifierLoc(),
  3193. diag::err_omp_wrong_if_directive_name_modifier)
  3194. << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
  3195. ErrorFound = true;
  3196. }
  3197. }
  3198. }
  3199. // If any if clause on the directive includes a directive-name-modifier then
  3200. // all if clauses on the directive must include a directive-name-modifier.
  3201. if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
  3202. if (NamedModifiersNumber == AllowedNameModifiers.size()) {
  3203. S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
  3204. diag::err_omp_no_more_if_clause);
  3205. } else {
  3206. std::string Values;
  3207. std::string Sep(", ");
  3208. unsigned AllowedCnt = 0;
  3209. unsigned TotalAllowedNum =
  3210. AllowedNameModifiers.size() - NamedModifiersNumber;
  3211. for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
  3212. ++Cnt) {
  3213. OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
  3214. if (!FoundNameModifiers[NM]) {
  3215. Values += "'";
  3216. Values += getOpenMPDirectiveName(NM);
  3217. Values += "'";
  3218. if (AllowedCnt + 2 == TotalAllowedNum)
  3219. Values += " or ";
  3220. else if (AllowedCnt + 1 != TotalAllowedNum)
  3221. Values += Sep;
  3222. ++AllowedCnt;
  3223. }
  3224. }
  3225. S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
  3226. diag::err_omp_unnamed_if_clause)
  3227. << (TotalAllowedNum > 1) << Values;
  3228. }
  3229. for (SourceLocation Loc : NameModifierLoc) {
  3230. S.Diag(Loc, diag::note_omp_previous_named_if_clause);
  3231. }
  3232. ErrorFound = true;
  3233. }
  3234. return ErrorFound;
  3235. }
  3236. StmtResult Sema::ActOnOpenMPExecutableDirective(
  3237. OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
  3238. OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
  3239. Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
  3240. StmtResult Res = StmtError();
  3241. // First check CancelRegion which is then used in checkNestingOfRegions.
  3242. if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
  3243. checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
  3244. StartLoc))
  3245. return StmtError();
  3246. llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
  3247. VarsWithInheritedDSAType VarsWithInheritedDSA;
  3248. bool ErrorFound = false;
  3249. ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
  3250. if (AStmt && !CurContext->isDependentContext()) {
  3251. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  3252. // Check default data sharing attributes for referenced variables.
  3253. DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
  3254. int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
  3255. Stmt *S = AStmt;
  3256. while (--ThisCaptureLevel >= 0)
  3257. S = cast<CapturedStmt>(S)->getCapturedStmt();
  3258. DSAChecker.Visit(S);
  3259. if (DSAChecker.isErrorFound())
  3260. return StmtError();
  3261. // Generate list of implicitly defined firstprivate variables.
  3262. VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
  3263. SmallVector<Expr *, 4> ImplicitFirstprivates(
  3264. DSAChecker.getImplicitFirstprivate().begin(),
  3265. DSAChecker.getImplicitFirstprivate().end());
  3266. SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
  3267. DSAChecker.getImplicitMap().end());
  3268. // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
  3269. for (OMPClause *C : Clauses) {
  3270. if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
  3271. for (Expr *E : IRC->taskgroup_descriptors())
  3272. if (E)
  3273. ImplicitFirstprivates.emplace_back(E);
  3274. }
  3275. }
  3276. if (!ImplicitFirstprivates.empty()) {
  3277. if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
  3278. ImplicitFirstprivates, SourceLocation(), SourceLocation(),
  3279. SourceLocation())) {
  3280. ClausesWithImplicit.push_back(Implicit);
  3281. ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
  3282. ImplicitFirstprivates.size();
  3283. } else {
  3284. ErrorFound = true;
  3285. }
  3286. }
  3287. if (!ImplicitMaps.empty()) {
  3288. CXXScopeSpec MapperIdScopeSpec;
  3289. DeclarationNameInfo MapperId;
  3290. if (OMPClause *Implicit = ActOnOpenMPMapClause(
  3291. llvm::None, llvm::None, MapperIdScopeSpec, MapperId,
  3292. OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true, SourceLocation(),
  3293. SourceLocation(), ImplicitMaps, OMPVarListLocTy())) {
  3294. ClausesWithImplicit.emplace_back(Implicit);
  3295. ErrorFound |=
  3296. cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
  3297. } else {
  3298. ErrorFound = true;
  3299. }
  3300. }
  3301. }
  3302. llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
  3303. switch (Kind) {
  3304. case OMPD_parallel:
  3305. Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
  3306. EndLoc);
  3307. AllowedNameModifiers.push_back(OMPD_parallel);
  3308. break;
  3309. case OMPD_simd:
  3310. Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
  3311. VarsWithInheritedDSA);
  3312. break;
  3313. case OMPD_for:
  3314. Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
  3315. VarsWithInheritedDSA);
  3316. break;
  3317. case OMPD_for_simd:
  3318. Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
  3319. EndLoc, VarsWithInheritedDSA);
  3320. break;
  3321. case OMPD_sections:
  3322. Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
  3323. EndLoc);
  3324. break;
  3325. case OMPD_section:
  3326. assert(ClausesWithImplicit.empty() &&
  3327. "No clauses are allowed for 'omp section' directive");
  3328. Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
  3329. break;
  3330. case OMPD_single:
  3331. Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
  3332. EndLoc);
  3333. break;
  3334. case OMPD_master:
  3335. assert(ClausesWithImplicit.empty() &&
  3336. "No clauses are allowed for 'omp master' directive");
  3337. Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
  3338. break;
  3339. case OMPD_critical:
  3340. Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
  3341. StartLoc, EndLoc);
  3342. break;
  3343. case OMPD_parallel_for:
  3344. Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
  3345. EndLoc, VarsWithInheritedDSA);
  3346. AllowedNameModifiers.push_back(OMPD_parallel);
  3347. break;
  3348. case OMPD_parallel_for_simd:
  3349. Res = ActOnOpenMPParallelForSimdDirective(
  3350. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3351. AllowedNameModifiers.push_back(OMPD_parallel);
  3352. break;
  3353. case OMPD_parallel_sections:
  3354. Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
  3355. StartLoc, EndLoc);
  3356. AllowedNameModifiers.push_back(OMPD_parallel);
  3357. break;
  3358. case OMPD_task:
  3359. Res =
  3360. ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
  3361. AllowedNameModifiers.push_back(OMPD_task);
  3362. break;
  3363. case OMPD_taskyield:
  3364. assert(ClausesWithImplicit.empty() &&
  3365. "No clauses are allowed for 'omp taskyield' directive");
  3366. assert(AStmt == nullptr &&
  3367. "No associated statement allowed for 'omp taskyield' directive");
  3368. Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
  3369. break;
  3370. case OMPD_barrier:
  3371. assert(ClausesWithImplicit.empty() &&
  3372. "No clauses are allowed for 'omp barrier' directive");
  3373. assert(AStmt == nullptr &&
  3374. "No associated statement allowed for 'omp barrier' directive");
  3375. Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
  3376. break;
  3377. case OMPD_taskwait:
  3378. assert(ClausesWithImplicit.empty() &&
  3379. "No clauses are allowed for 'omp taskwait' directive");
  3380. assert(AStmt == nullptr &&
  3381. "No associated statement allowed for 'omp taskwait' directive");
  3382. Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
  3383. break;
  3384. case OMPD_taskgroup:
  3385. Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
  3386. EndLoc);
  3387. break;
  3388. case OMPD_flush:
  3389. assert(AStmt == nullptr &&
  3390. "No associated statement allowed for 'omp flush' directive");
  3391. Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
  3392. break;
  3393. case OMPD_ordered:
  3394. Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
  3395. EndLoc);
  3396. break;
  3397. case OMPD_atomic:
  3398. Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
  3399. EndLoc);
  3400. break;
  3401. case OMPD_teams:
  3402. Res =
  3403. ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
  3404. break;
  3405. case OMPD_target:
  3406. Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
  3407. EndLoc);
  3408. AllowedNameModifiers.push_back(OMPD_target);
  3409. break;
  3410. case OMPD_target_parallel:
  3411. Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
  3412. StartLoc, EndLoc);
  3413. AllowedNameModifiers.push_back(OMPD_target);
  3414. AllowedNameModifiers.push_back(OMPD_parallel);
  3415. break;
  3416. case OMPD_target_parallel_for:
  3417. Res = ActOnOpenMPTargetParallelForDirective(
  3418. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3419. AllowedNameModifiers.push_back(OMPD_target);
  3420. AllowedNameModifiers.push_back(OMPD_parallel);
  3421. break;
  3422. case OMPD_cancellation_point:
  3423. assert(ClausesWithImplicit.empty() &&
  3424. "No clauses are allowed for 'omp cancellation point' directive");
  3425. assert(AStmt == nullptr && "No associated statement allowed for 'omp "
  3426. "cancellation point' directive");
  3427. Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
  3428. break;
  3429. case OMPD_cancel:
  3430. assert(AStmt == nullptr &&
  3431. "No associated statement allowed for 'omp cancel' directive");
  3432. Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
  3433. CancelRegion);
  3434. AllowedNameModifiers.push_back(OMPD_cancel);
  3435. break;
  3436. case OMPD_target_data:
  3437. Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
  3438. EndLoc);
  3439. AllowedNameModifiers.push_back(OMPD_target_data);
  3440. break;
  3441. case OMPD_target_enter_data:
  3442. Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
  3443. EndLoc, AStmt);
  3444. AllowedNameModifiers.push_back(OMPD_target_enter_data);
  3445. break;
  3446. case OMPD_target_exit_data:
  3447. Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
  3448. EndLoc, AStmt);
  3449. AllowedNameModifiers.push_back(OMPD_target_exit_data);
  3450. break;
  3451. case OMPD_taskloop:
  3452. Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
  3453. EndLoc, VarsWithInheritedDSA);
  3454. AllowedNameModifiers.push_back(OMPD_taskloop);
  3455. break;
  3456. case OMPD_taskloop_simd:
  3457. Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
  3458. EndLoc, VarsWithInheritedDSA);
  3459. AllowedNameModifiers.push_back(OMPD_taskloop);
  3460. break;
  3461. case OMPD_distribute:
  3462. Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
  3463. EndLoc, VarsWithInheritedDSA);
  3464. break;
  3465. case OMPD_target_update:
  3466. Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
  3467. EndLoc, AStmt);
  3468. AllowedNameModifiers.push_back(OMPD_target_update);
  3469. break;
  3470. case OMPD_distribute_parallel_for:
  3471. Res = ActOnOpenMPDistributeParallelForDirective(
  3472. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3473. AllowedNameModifiers.push_back(OMPD_parallel);
  3474. break;
  3475. case OMPD_distribute_parallel_for_simd:
  3476. Res = ActOnOpenMPDistributeParallelForSimdDirective(
  3477. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3478. AllowedNameModifiers.push_back(OMPD_parallel);
  3479. break;
  3480. case OMPD_distribute_simd:
  3481. Res = ActOnOpenMPDistributeSimdDirective(
  3482. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3483. break;
  3484. case OMPD_target_parallel_for_simd:
  3485. Res = ActOnOpenMPTargetParallelForSimdDirective(
  3486. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3487. AllowedNameModifiers.push_back(OMPD_target);
  3488. AllowedNameModifiers.push_back(OMPD_parallel);
  3489. break;
  3490. case OMPD_target_simd:
  3491. Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
  3492. EndLoc, VarsWithInheritedDSA);
  3493. AllowedNameModifiers.push_back(OMPD_target);
  3494. break;
  3495. case OMPD_teams_distribute:
  3496. Res = ActOnOpenMPTeamsDistributeDirective(
  3497. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3498. break;
  3499. case OMPD_teams_distribute_simd:
  3500. Res = ActOnOpenMPTeamsDistributeSimdDirective(
  3501. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3502. break;
  3503. case OMPD_teams_distribute_parallel_for_simd:
  3504. Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
  3505. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3506. AllowedNameModifiers.push_back(OMPD_parallel);
  3507. break;
  3508. case OMPD_teams_distribute_parallel_for:
  3509. Res = ActOnOpenMPTeamsDistributeParallelForDirective(
  3510. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3511. AllowedNameModifiers.push_back(OMPD_parallel);
  3512. break;
  3513. case OMPD_target_teams:
  3514. Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
  3515. EndLoc);
  3516. AllowedNameModifiers.push_back(OMPD_target);
  3517. break;
  3518. case OMPD_target_teams_distribute:
  3519. Res = ActOnOpenMPTargetTeamsDistributeDirective(
  3520. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3521. AllowedNameModifiers.push_back(OMPD_target);
  3522. break;
  3523. case OMPD_target_teams_distribute_parallel_for:
  3524. Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
  3525. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3526. AllowedNameModifiers.push_back(OMPD_target);
  3527. AllowedNameModifiers.push_back(OMPD_parallel);
  3528. break;
  3529. case OMPD_target_teams_distribute_parallel_for_simd:
  3530. Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
  3531. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3532. AllowedNameModifiers.push_back(OMPD_target);
  3533. AllowedNameModifiers.push_back(OMPD_parallel);
  3534. break;
  3535. case OMPD_target_teams_distribute_simd:
  3536. Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
  3537. ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
  3538. AllowedNameModifiers.push_back(OMPD_target);
  3539. break;
  3540. case OMPD_declare_target:
  3541. case OMPD_end_declare_target:
  3542. case OMPD_threadprivate:
  3543. case OMPD_allocate:
  3544. case OMPD_declare_reduction:
  3545. case OMPD_declare_mapper:
  3546. case OMPD_declare_simd:
  3547. case OMPD_requires:
  3548. llvm_unreachable("OpenMP Directive is not allowed");
  3549. case OMPD_unknown:
  3550. llvm_unreachable("Unknown OpenMP directive");
  3551. }
  3552. for (const auto &P : VarsWithInheritedDSA) {
  3553. Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
  3554. << P.first << P.second->getSourceRange();
  3555. }
  3556. ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
  3557. if (!AllowedNameModifiers.empty())
  3558. ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
  3559. ErrorFound;
  3560. if (ErrorFound)
  3561. return StmtError();
  3562. return Res;
  3563. }
  3564. Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
  3565. DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
  3566. ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
  3567. ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
  3568. ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
  3569. assert(Aligneds.size() == Alignments.size());
  3570. assert(Linears.size() == LinModifiers.size());
  3571. assert(Linears.size() == Steps.size());
  3572. if (!DG || DG.get().isNull())
  3573. return DeclGroupPtrTy();
  3574. if (!DG.get().isSingleDecl()) {
  3575. Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
  3576. return DG;
  3577. }
  3578. Decl *ADecl = DG.get().getSingleDecl();
  3579. if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
  3580. ADecl = FTD->getTemplatedDecl();
  3581. auto *FD = dyn_cast<FunctionDecl>(ADecl);
  3582. if (!FD) {
  3583. Diag(ADecl->getLocation(), diag::err_omp_function_expected);
  3584. return DeclGroupPtrTy();
  3585. }
  3586. // OpenMP [2.8.2, declare simd construct, Description]
  3587. // The parameter of the simdlen clause must be a constant positive integer
  3588. // expression.
  3589. ExprResult SL;
  3590. if (Simdlen)
  3591. SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
  3592. // OpenMP [2.8.2, declare simd construct, Description]
  3593. // The special this pointer can be used as if was one of the arguments to the
  3594. // function in any of the linear, aligned, or uniform clauses.
  3595. // The uniform clause declares one or more arguments to have an invariant
  3596. // value for all concurrent invocations of the function in the execution of a
  3597. // single SIMD loop.
  3598. llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
  3599. const Expr *UniformedLinearThis = nullptr;
  3600. for (const Expr *E : Uniforms) {
  3601. E = E->IgnoreParenImpCasts();
  3602. if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
  3603. if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
  3604. if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
  3605. FD->getParamDecl(PVD->getFunctionScopeIndex())
  3606. ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
  3607. UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
  3608. continue;
  3609. }
  3610. if (isa<CXXThisExpr>(E)) {
  3611. UniformedLinearThis = E;
  3612. continue;
  3613. }
  3614. Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
  3615. << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
  3616. }
  3617. // OpenMP [2.8.2, declare simd construct, Description]
  3618. // The aligned clause declares that the object to which each list item points
  3619. // is aligned to the number of bytes expressed in the optional parameter of
  3620. // the aligned clause.
  3621. // The special this pointer can be used as if was one of the arguments to the
  3622. // function in any of the linear, aligned, or uniform clauses.
  3623. // The type of list items appearing in the aligned clause must be array,
  3624. // pointer, reference to array, or reference to pointer.
  3625. llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
  3626. const Expr *AlignedThis = nullptr;
  3627. for (const Expr *E : Aligneds) {
  3628. E = E->IgnoreParenImpCasts();
  3629. if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
  3630. if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
  3631. const VarDecl *CanonPVD = PVD->getCanonicalDecl();
  3632. if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
  3633. FD->getParamDecl(PVD->getFunctionScopeIndex())
  3634. ->getCanonicalDecl() == CanonPVD) {
  3635. // OpenMP [2.8.1, simd construct, Restrictions]
  3636. // A list-item cannot appear in more than one aligned clause.
  3637. if (AlignedArgs.count(CanonPVD) > 0) {
  3638. Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
  3639. << 1 << E->getSourceRange();
  3640. Diag(AlignedArgs[CanonPVD]->getExprLoc(),
  3641. diag::note_omp_explicit_dsa)
  3642. << getOpenMPClauseName(OMPC_aligned);
  3643. continue;
  3644. }
  3645. AlignedArgs[CanonPVD] = E;
  3646. QualType QTy = PVD->getType()
  3647. .getNonReferenceType()
  3648. .getUnqualifiedType()
  3649. .getCanonicalType();
  3650. const Type *Ty = QTy.getTypePtrOrNull();
  3651. if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
  3652. Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
  3653. << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
  3654. Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
  3655. }
  3656. continue;
  3657. }
  3658. }
  3659. if (isa<CXXThisExpr>(E)) {
  3660. if (AlignedThis) {
  3661. Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
  3662. << 2 << E->getSourceRange();
  3663. Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
  3664. << getOpenMPClauseName(OMPC_aligned);
  3665. }
  3666. AlignedThis = E;
  3667. continue;
  3668. }
  3669. Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
  3670. << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
  3671. }
  3672. // The optional parameter of the aligned clause, alignment, must be a constant
  3673. // positive integer expression. If no optional parameter is specified,
  3674. // implementation-defined default alignments for SIMD instructions on the
  3675. // target platforms are assumed.
  3676. SmallVector<const Expr *, 4> NewAligns;
  3677. for (Expr *E : Alignments) {
  3678. ExprResult Align;
  3679. if (E)
  3680. Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
  3681. NewAligns.push_back(Align.get());
  3682. }
  3683. // OpenMP [2.8.2, declare simd construct, Description]
  3684. // The linear clause declares one or more list items to be private to a SIMD
  3685. // lane and to have a linear relationship with respect to the iteration space
  3686. // of a loop.
  3687. // The special this pointer can be used as if was one of the arguments to the
  3688. // function in any of the linear, aligned, or uniform clauses.
  3689. // When a linear-step expression is specified in a linear clause it must be
  3690. // either a constant integer expression or an integer-typed parameter that is
  3691. // specified in a uniform clause on the directive.
  3692. llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
  3693. const bool IsUniformedThis = UniformedLinearThis != nullptr;
  3694. auto MI = LinModifiers.begin();
  3695. for (const Expr *E : Linears) {
  3696. auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
  3697. ++MI;
  3698. E = E->IgnoreParenImpCasts();
  3699. if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
  3700. if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
  3701. const VarDecl *CanonPVD = PVD->getCanonicalDecl();
  3702. if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
  3703. FD->getParamDecl(PVD->getFunctionScopeIndex())
  3704. ->getCanonicalDecl() == CanonPVD) {
  3705. // OpenMP [2.15.3.7, linear Clause, Restrictions]
  3706. // A list-item cannot appear in more than one linear clause.
  3707. if (LinearArgs.count(CanonPVD) > 0) {
  3708. Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
  3709. << getOpenMPClauseName(OMPC_linear)
  3710. << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
  3711. Diag(LinearArgs[CanonPVD]->getExprLoc(),
  3712. diag::note_omp_explicit_dsa)
  3713. << getOpenMPClauseName(OMPC_linear);
  3714. continue;
  3715. }
  3716. // Each argument can appear in at most one uniform or linear clause.
  3717. if (UniformedArgs.count(CanonPVD) > 0) {
  3718. Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
  3719. << getOpenMPClauseName(OMPC_linear)
  3720. << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
  3721. Diag(UniformedArgs[CanonPVD]->getExprLoc(),
  3722. diag::note_omp_explicit_dsa)
  3723. << getOpenMPClauseName(OMPC_uniform);
  3724. continue;
  3725. }
  3726. LinearArgs[CanonPVD] = E;
  3727. if (E->isValueDependent() || E->isTypeDependent() ||
  3728. E->isInstantiationDependent() ||
  3729. E->containsUnexpandedParameterPack())
  3730. continue;
  3731. (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
  3732. PVD->getOriginalType());
  3733. continue;
  3734. }
  3735. }
  3736. if (isa<CXXThisExpr>(E)) {
  3737. if (UniformedLinearThis) {
  3738. Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
  3739. << getOpenMPClauseName(OMPC_linear)
  3740. << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
  3741. << E->getSourceRange();
  3742. Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
  3743. << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
  3744. : OMPC_linear);
  3745. continue;
  3746. }
  3747. UniformedLinearThis = E;
  3748. if (E->isValueDependent() || E->isTypeDependent() ||
  3749. E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
  3750. continue;
  3751. (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
  3752. E->getType());
  3753. continue;
  3754. }
  3755. Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
  3756. << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
  3757. }
  3758. Expr *Step = nullptr;
  3759. Expr *NewStep = nullptr;
  3760. SmallVector<Expr *, 4> NewSteps;
  3761. for (Expr *E : Steps) {
  3762. // Skip the same step expression, it was checked already.
  3763. if (Step == E || !E) {
  3764. NewSteps.push_back(E ? NewStep : nullptr);
  3765. continue;
  3766. }
  3767. Step = E;
  3768. if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
  3769. if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
  3770. const VarDecl *CanonPVD = PVD->getCanonicalDecl();
  3771. if (UniformedArgs.count(CanonPVD) == 0) {
  3772. Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
  3773. << Step->getSourceRange();
  3774. } else if (E->isValueDependent() || E->isTypeDependent() ||
  3775. E->isInstantiationDependent() ||
  3776. E->containsUnexpandedParameterPack() ||
  3777. CanonPVD->getType()->hasIntegerRepresentation()) {
  3778. NewSteps.push_back(Step);
  3779. } else {
  3780. Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
  3781. << Step->getSourceRange();
  3782. }
  3783. continue;
  3784. }
  3785. NewStep = Step;
  3786. if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
  3787. !Step->isInstantiationDependent() &&
  3788. !Step->containsUnexpandedParameterPack()) {
  3789. NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
  3790. .get();
  3791. if (NewStep)
  3792. NewStep = VerifyIntegerConstantExpression(NewStep).get();
  3793. }
  3794. NewSteps.push_back(NewStep);
  3795. }
  3796. auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
  3797. Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
  3798. Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
  3799. const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
  3800. const_cast<Expr **>(Linears.data()), Linears.size(),
  3801. const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
  3802. NewSteps.data(), NewSteps.size(), SR);
  3803. ADecl->addAttr(NewAttr);
  3804. return ConvertDeclToDeclGroup(ADecl);
  3805. }
  3806. StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
  3807. Stmt *AStmt,
  3808. SourceLocation StartLoc,
  3809. SourceLocation EndLoc) {
  3810. if (!AStmt)
  3811. return StmtError();
  3812. auto *CS = cast<CapturedStmt>(AStmt);
  3813. // 1.2.2 OpenMP Language Terminology
  3814. // Structured block - An executable statement with a single entry at the
  3815. // top and a single exit at the bottom.
  3816. // The point of exit cannot be a branch out of the structured block.
  3817. // longjmp() and throw() must not violate the entry/exit criteria.
  3818. CS->getCapturedDecl()->setNothrow();
  3819. setFunctionHasBranchProtectedScope();
  3820. return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
  3821. DSAStack->isCancelRegion());
  3822. }
  3823. namespace {
  3824. /// Helper class for checking canonical form of the OpenMP loops and
  3825. /// extracting iteration space of each loop in the loop nest, that will be used
  3826. /// for IR generation.
  3827. class OpenMPIterationSpaceChecker {
  3828. /// Reference to Sema.
  3829. Sema &SemaRef;
  3830. /// A location for diagnostics (when there is no some better location).
  3831. SourceLocation DefaultLoc;
  3832. /// A location for diagnostics (when increment is not compatible).
  3833. SourceLocation ConditionLoc;
  3834. /// A source location for referring to loop init later.
  3835. SourceRange InitSrcRange;
  3836. /// A source location for referring to condition later.
  3837. SourceRange ConditionSrcRange;
  3838. /// A source location for referring to increment later.
  3839. SourceRange IncrementSrcRange;
  3840. /// Loop variable.
  3841. ValueDecl *LCDecl = nullptr;
  3842. /// Reference to loop variable.
  3843. Expr *LCRef = nullptr;
  3844. /// Lower bound (initializer for the var).
  3845. Expr *LB = nullptr;
  3846. /// Upper bound.
  3847. Expr *UB = nullptr;
  3848. /// Loop step (increment).
  3849. Expr *Step = nullptr;
  3850. /// This flag is true when condition is one of:
  3851. /// Var < UB
  3852. /// Var <= UB
  3853. /// UB > Var
  3854. /// UB >= Var
  3855. /// This will have no value when the condition is !=
  3856. llvm::Optional<bool> TestIsLessOp;
  3857. /// This flag is true when condition is strict ( < or > ).
  3858. bool TestIsStrictOp = false;
  3859. /// This flag is true when step is subtracted on each iteration.
  3860. bool SubtractStep = false;
  3861. public:
  3862. OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
  3863. : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
  3864. /// Check init-expr for canonical loop form and save loop counter
  3865. /// variable - #Var and its initialization value - #LB.
  3866. bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
  3867. /// Check test-expr for canonical form, save upper-bound (#UB), flags
  3868. /// for less/greater and for strict/non-strict comparison.
  3869. bool checkAndSetCond(Expr *S);
  3870. /// Check incr-expr for canonical loop form and return true if it
  3871. /// does not conform, otherwise save loop step (#Step).
  3872. bool checkAndSetInc(Expr *S);
  3873. /// Return the loop counter variable.
  3874. ValueDecl *getLoopDecl() const { return LCDecl; }
  3875. /// Return the reference expression to loop counter variable.
  3876. Expr *getLoopDeclRefExpr() const { return LCRef; }
  3877. /// Source range of the loop init.
  3878. SourceRange getInitSrcRange() const { return InitSrcRange; }
  3879. /// Source range of the loop condition.
  3880. SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
  3881. /// Source range of the loop increment.
  3882. SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
  3883. /// True if the step should be subtracted.
  3884. bool shouldSubtractStep() const { return SubtractStep; }
  3885. /// True, if the compare operator is strict (<, > or !=).
  3886. bool isStrictTestOp() const { return TestIsStrictOp; }
  3887. /// Build the expression to calculate the number of iterations.
  3888. Expr *buildNumIterations(
  3889. Scope *S, const bool LimitedType,
  3890. llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
  3891. /// Build the precondition expression for the loops.
  3892. Expr *
  3893. buildPreCond(Scope *S, Expr *Cond,
  3894. llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
  3895. /// Build reference expression to the counter be used for codegen.
  3896. DeclRefExpr *
  3897. buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
  3898. DSAStackTy &DSA) const;
  3899. /// Build reference expression to the private counter be used for
  3900. /// codegen.
  3901. Expr *buildPrivateCounterVar() const;
  3902. /// Build initialization of the counter be used for codegen.
  3903. Expr *buildCounterInit() const;
  3904. /// Build step of the counter be used for codegen.
  3905. Expr *buildCounterStep() const;
  3906. /// Build loop data with counter value for depend clauses in ordered
  3907. /// directives.
  3908. Expr *
  3909. buildOrderedLoopData(Scope *S, Expr *Counter,
  3910. llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
  3911. SourceLocation Loc, Expr *Inc = nullptr,
  3912. OverloadedOperatorKind OOK = OO_Amp);
  3913. /// Return true if any expression is dependent.
  3914. bool dependent() const;
  3915. private:
  3916. /// Check the right-hand side of an assignment in the increment
  3917. /// expression.
  3918. bool checkAndSetIncRHS(Expr *RHS);
  3919. /// Helper to set loop counter variable and its initializer.
  3920. bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
  3921. /// Helper to set upper bound.
  3922. bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp,
  3923. SourceRange SR, SourceLocation SL);
  3924. /// Helper to set loop increment.
  3925. bool setStep(Expr *NewStep, bool Subtract);
  3926. };
  3927. bool OpenMPIterationSpaceChecker::dependent() const {
  3928. if (!LCDecl) {
  3929. assert(!LB && !UB && !Step);
  3930. return false;
  3931. }
  3932. return LCDecl->getType()->isDependentType() ||
  3933. (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
  3934. (Step && Step->isValueDependent());
  3935. }
  3936. bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
  3937. Expr *NewLCRefExpr,
  3938. Expr *NewLB) {
  3939. // State consistency checking to ensure correct usage.
  3940. assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
  3941. UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
  3942. if (!NewLCDecl || !NewLB)
  3943. return true;
  3944. LCDecl = getCanonicalDecl(NewLCDecl);
  3945. LCRef = NewLCRefExpr;
  3946. if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
  3947. if (const CXXConstructorDecl *Ctor = CE->getConstructor())
  3948. if ((Ctor->isCopyOrMoveConstructor() ||
  3949. Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
  3950. CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
  3951. NewLB = CE->getArg(0)->IgnoreParenImpCasts();
  3952. LB = NewLB;
  3953. return false;
  3954. }
  3955. bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB,
  3956. llvm::Optional<bool> LessOp,
  3957. bool StrictOp, SourceRange SR,
  3958. SourceLocation SL) {
  3959. // State consistency checking to ensure correct usage.
  3960. assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
  3961. Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
  3962. if (!NewUB)
  3963. return true;
  3964. UB = NewUB;
  3965. if (LessOp)
  3966. TestIsLessOp = LessOp;
  3967. TestIsStrictOp = StrictOp;
  3968. ConditionSrcRange = SR;
  3969. ConditionLoc = SL;
  3970. return false;
  3971. }
  3972. bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
  3973. // State consistency checking to ensure correct usage.
  3974. assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
  3975. if (!NewStep)
  3976. return true;
  3977. if (!NewStep->isValueDependent()) {
  3978. // Check that the step is integer expression.
  3979. SourceLocation StepLoc = NewStep->getBeginLoc();
  3980. ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
  3981. StepLoc, getExprAsWritten(NewStep));
  3982. if (Val.isInvalid())
  3983. return true;
  3984. NewStep = Val.get();
  3985. // OpenMP [2.6, Canonical Loop Form, Restrictions]
  3986. // If test-expr is of form var relational-op b and relational-op is < or
  3987. // <= then incr-expr must cause var to increase on each iteration of the
  3988. // loop. If test-expr is of form var relational-op b and relational-op is
  3989. // > or >= then incr-expr must cause var to decrease on each iteration of
  3990. // the loop.
  3991. // If test-expr is of form b relational-op var and relational-op is < or
  3992. // <= then incr-expr must cause var to decrease on each iteration of the
  3993. // loop. If test-expr is of form b relational-op var and relational-op is
  3994. // > or >= then incr-expr must cause var to increase on each iteration of
  3995. // the loop.
  3996. llvm::APSInt Result;
  3997. bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
  3998. bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
  3999. bool IsConstNeg =
  4000. IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
  4001. bool IsConstPos =
  4002. IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
  4003. bool IsConstZero = IsConstant && !Result.getBoolValue();
  4004. // != with increment is treated as <; != with decrement is treated as >
  4005. if (!TestIsLessOp.hasValue())
  4006. TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract);
  4007. if (UB && (IsConstZero ||
  4008. (TestIsLessOp.getValue() ?
  4009. (IsConstNeg || (IsUnsigned && Subtract)) :
  4010. (IsConstPos || (IsUnsigned && !Subtract))))) {
  4011. SemaRef.Diag(NewStep->getExprLoc(),
  4012. diag::err_omp_loop_incr_not_compatible)
  4013. << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange();
  4014. SemaRef.Diag(ConditionLoc,
  4015. diag::note_omp_loop_cond_requres_compatible_incr)
  4016. << TestIsLessOp.getValue() << ConditionSrcRange;
  4017. return true;
  4018. }
  4019. if (TestIsLessOp.getValue() == Subtract) {
  4020. NewStep =
  4021. SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
  4022. .get();
  4023. Subtract = !Subtract;
  4024. }
  4025. }
  4026. Step = NewStep;
  4027. SubtractStep = Subtract;
  4028. return false;
  4029. }
  4030. bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
  4031. // Check init-expr for canonical loop form and save loop counter
  4032. // variable - #Var and its initialization value - #LB.
  4033. // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
  4034. // var = lb
  4035. // integer-type var = lb
  4036. // random-access-iterator-type var = lb
  4037. // pointer-type var = lb
  4038. //
  4039. if (!S) {
  4040. if (EmitDiags) {
  4041. SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
  4042. }
  4043. return true;
  4044. }
  4045. if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
  4046. if (!ExprTemp->cleanupsHaveSideEffects())
  4047. S = ExprTemp->getSubExpr();
  4048. InitSrcRange = S->getSourceRange();
  4049. if (Expr *E = dyn_cast<Expr>(S))
  4050. S = E->IgnoreParens();
  4051. if (auto *BO = dyn_cast<BinaryOperator>(S)) {
  4052. if (BO->getOpcode() == BO_Assign) {
  4053. Expr *LHS = BO->getLHS()->IgnoreParens();
  4054. if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
  4055. if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
  4056. if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
  4057. return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
  4058. return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
  4059. }
  4060. if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
  4061. if (ME->isArrow() &&
  4062. isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
  4063. return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
  4064. }
  4065. }
  4066. } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
  4067. if (DS->isSingleDecl()) {
  4068. if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
  4069. if (Var->hasInit() && !Var->getType()->isReferenceType()) {
  4070. // Accept non-canonical init form here but emit ext. warning.
  4071. if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
  4072. SemaRef.Diag(S->getBeginLoc(),
  4073. diag::ext_omp_loop_not_canonical_init)
  4074. << S->getSourceRange();
  4075. return setLCDeclAndLB(
  4076. Var,
  4077. buildDeclRefExpr(SemaRef, Var,
  4078. Var->getType().getNonReferenceType(),
  4079. DS->getBeginLoc()),
  4080. Var->getInit());
  4081. }
  4082. }
  4083. }
  4084. } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
  4085. if (CE->getOperator() == OO_Equal) {
  4086. Expr *LHS = CE->getArg(0);
  4087. if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
  4088. if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
  4089. if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
  4090. return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
  4091. return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
  4092. }
  4093. if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
  4094. if (ME->isArrow() &&
  4095. isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
  4096. return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
  4097. }
  4098. }
  4099. }
  4100. if (dependent() || SemaRef.CurContext->isDependentContext())
  4101. return false;
  4102. if (EmitDiags) {
  4103. SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
  4104. << S->getSourceRange();
  4105. }
  4106. return true;
  4107. }
  4108. /// Ignore parenthesizes, implicit casts, copy constructor and return the
  4109. /// variable (which may be the loop variable) if possible.
  4110. static const ValueDecl *getInitLCDecl(const Expr *E) {
  4111. if (!E)
  4112. return nullptr;
  4113. E = getExprAsWritten(E);
  4114. if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
  4115. if (const CXXConstructorDecl *Ctor = CE->getConstructor())
  4116. if ((Ctor->isCopyOrMoveConstructor() ||
  4117. Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
  4118. CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
  4119. E = CE->getArg(0)->IgnoreParenImpCasts();
  4120. if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
  4121. if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
  4122. return getCanonicalDecl(VD);
  4123. }
  4124. if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
  4125. if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
  4126. return getCanonicalDecl(ME->getMemberDecl());
  4127. return nullptr;
  4128. }
  4129. bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
  4130. // Check test-expr for canonical form, save upper-bound UB, flags for
  4131. // less/greater and for strict/non-strict comparison.
  4132. // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
  4133. // var relational-op b
  4134. // b relational-op var
  4135. //
  4136. if (!S) {
  4137. SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
  4138. return true;
  4139. }
  4140. S = getExprAsWritten(S);
  4141. SourceLocation CondLoc = S->getBeginLoc();
  4142. if (auto *BO = dyn_cast<BinaryOperator>(S)) {
  4143. if (BO->isRelationalOp()) {
  4144. if (getInitLCDecl(BO->getLHS()) == LCDecl)
  4145. return setUB(BO->getRHS(),
  4146. (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
  4147. (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
  4148. BO->getSourceRange(), BO->getOperatorLoc());
  4149. if (getInitLCDecl(BO->getRHS()) == LCDecl)
  4150. return setUB(BO->getLHS(),
  4151. (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
  4152. (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
  4153. BO->getSourceRange(), BO->getOperatorLoc());
  4154. } else if (BO->getOpcode() == BO_NE)
  4155. return setUB(getInitLCDecl(BO->getLHS()) == LCDecl ?
  4156. BO->getRHS() : BO->getLHS(),
  4157. /*LessOp=*/llvm::None,
  4158. /*StrictOp=*/true,
  4159. BO->getSourceRange(), BO->getOperatorLoc());
  4160. } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
  4161. if (CE->getNumArgs() == 2) {
  4162. auto Op = CE->getOperator();
  4163. switch (Op) {
  4164. case OO_Greater:
  4165. case OO_GreaterEqual:
  4166. case OO_Less:
  4167. case OO_LessEqual:
  4168. if (getInitLCDecl(CE->getArg(0)) == LCDecl)
  4169. return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
  4170. Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
  4171. CE->getOperatorLoc());
  4172. if (getInitLCDecl(CE->getArg(1)) == LCDecl)
  4173. return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
  4174. Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
  4175. CE->getOperatorLoc());
  4176. break;
  4177. case OO_ExclaimEqual:
  4178. return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ?
  4179. CE->getArg(1) : CE->getArg(0),
  4180. /*LessOp=*/llvm::None,
  4181. /*StrictOp=*/true,
  4182. CE->getSourceRange(),
  4183. CE->getOperatorLoc());
  4184. break;
  4185. default:
  4186. break;
  4187. }
  4188. }
  4189. }
  4190. if (dependent() || SemaRef.CurContext->isDependentContext())
  4191. return false;
  4192. SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
  4193. << S->getSourceRange() << LCDecl;
  4194. return true;
  4195. }
  4196. bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
  4197. // RHS of canonical loop form increment can be:
  4198. // var + incr
  4199. // incr + var
  4200. // var - incr
  4201. //
  4202. RHS = RHS->IgnoreParenImpCasts();
  4203. if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
  4204. if (BO->isAdditiveOp()) {
  4205. bool IsAdd = BO->getOpcode() == BO_Add;
  4206. if (getInitLCDecl(BO->getLHS()) == LCDecl)
  4207. return setStep(BO->getRHS(), !IsAdd);
  4208. if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
  4209. return setStep(BO->getLHS(), /*Subtract=*/false);
  4210. }
  4211. } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
  4212. bool IsAdd = CE->getOperator() == OO_Plus;
  4213. if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
  4214. if (getInitLCDecl(CE->getArg(0)) == LCDecl)
  4215. return setStep(CE->getArg(1), !IsAdd);
  4216. if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
  4217. return setStep(CE->getArg(0), /*Subtract=*/false);
  4218. }
  4219. }
  4220. if (dependent() || SemaRef.CurContext->isDependentContext())
  4221. return false;
  4222. SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
  4223. << RHS->getSourceRange() << LCDecl;
  4224. return true;
  4225. }
  4226. bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
  4227. // Check incr-expr for canonical loop form and return true if it
  4228. // does not conform.
  4229. // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
  4230. // ++var
  4231. // var++
  4232. // --var
  4233. // var--
  4234. // var += incr
  4235. // var -= incr
  4236. // var = var + incr
  4237. // var = incr + var
  4238. // var = var - incr
  4239. //
  4240. if (!S) {
  4241. SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
  4242. return true;
  4243. }
  4244. if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
  4245. if (!ExprTemp->cleanupsHaveSideEffects())
  4246. S = ExprTemp->getSubExpr();
  4247. IncrementSrcRange = S->getSourceRange();
  4248. S = S->IgnoreParens();
  4249. if (auto *UO = dyn_cast<UnaryOperator>(S)) {
  4250. if (UO->isIncrementDecrementOp() &&
  4251. getInitLCDecl(UO->getSubExpr()) == LCDecl)
  4252. return setStep(SemaRef
  4253. .ActOnIntegerConstant(UO->getBeginLoc(),
  4254. (UO->isDecrementOp() ? -1 : 1))
  4255. .get(),
  4256. /*Subtract=*/false);
  4257. } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
  4258. switch (BO->getOpcode()) {
  4259. case BO_AddAssign:
  4260. case BO_SubAssign:
  4261. if (getInitLCDecl(BO->getLHS()) == LCDecl)
  4262. return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
  4263. break;
  4264. case BO_Assign:
  4265. if (getInitLCDecl(BO->getLHS()) == LCDecl)
  4266. return checkAndSetIncRHS(BO->getRHS());
  4267. break;
  4268. default:
  4269. break;
  4270. }
  4271. } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
  4272. switch (CE->getOperator()) {
  4273. case OO_PlusPlus:
  4274. case OO_MinusMinus:
  4275. if (getInitLCDecl(CE->getArg(0)) == LCDecl)
  4276. return setStep(SemaRef
  4277. .ActOnIntegerConstant(
  4278. CE->getBeginLoc(),
  4279. ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
  4280. .get(),
  4281. /*Subtract=*/false);
  4282. break;
  4283. case OO_PlusEqual:
  4284. case OO_MinusEqual:
  4285. if (getInitLCDecl(CE->getArg(0)) == LCDecl)
  4286. return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
  4287. break;
  4288. case OO_Equal:
  4289. if (getInitLCDecl(CE->getArg(0)) == LCDecl)
  4290. return checkAndSetIncRHS(CE->getArg(1));
  4291. break;
  4292. default:
  4293. break;
  4294. }
  4295. }
  4296. if (dependent() || SemaRef.CurContext->isDependentContext())
  4297. return false;
  4298. SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
  4299. << S->getSourceRange() << LCDecl;
  4300. return true;
  4301. }
  4302. static ExprResult
  4303. tryBuildCapture(Sema &SemaRef, Expr *Capture,
  4304. llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
  4305. if (SemaRef.CurContext->isDependentContext())
  4306. return ExprResult(Capture);
  4307. if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
  4308. return SemaRef.PerformImplicitConversion(
  4309. Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
  4310. /*AllowExplicit=*/true);
  4311. auto I = Captures.find(Capture);
  4312. if (I != Captures.end())
  4313. return buildCapture(SemaRef, Capture, I->second);
  4314. DeclRefExpr *Ref = nullptr;
  4315. ExprResult Res = buildCapture(SemaRef, Capture, Ref);
  4316. Captures[Capture] = Ref;
  4317. return Res;
  4318. }
  4319. /// Build the expression to calculate the number of iterations.
  4320. Expr *OpenMPIterationSpaceChecker::buildNumIterations(
  4321. Scope *S, const bool LimitedType,
  4322. llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
  4323. ExprResult Diff;
  4324. QualType VarType = LCDecl->getType().getNonReferenceType();
  4325. if (VarType->isIntegerType() || VarType->isPointerType() ||
  4326. SemaRef.getLangOpts().CPlusPlus) {
  4327. // Upper - Lower
  4328. Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB;
  4329. Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB;
  4330. Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
  4331. Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
  4332. if (!Upper || !Lower)
  4333. return nullptr;
  4334. Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
  4335. if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
  4336. // BuildBinOp already emitted error, this one is to point user to upper
  4337. // and lower bound, and to tell what is passed to 'operator-'.
  4338. SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
  4339. << Upper->getSourceRange() << Lower->getSourceRange();
  4340. return nullptr;
  4341. }
  4342. }
  4343. if (!Diff.isUsable())
  4344. return nullptr;
  4345. // Upper - Lower [- 1]
  4346. if (TestIsStrictOp)
  4347. Diff = SemaRef.BuildBinOp(
  4348. S, DefaultLoc, BO_Sub, Diff.get(),
  4349. SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
  4350. if (!Diff.isUsable())
  4351. return nullptr;
  4352. // Upper - Lower [- 1] + Step
  4353. ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
  4354. if (!NewStep.isUsable())
  4355. return nullptr;
  4356. Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
  4357. if (!Diff.isUsable())
  4358. return nullptr;
  4359. // Parentheses (for dumping/debugging purposes only).
  4360. Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
  4361. if (!Diff.isUsable())
  4362. return nullptr;
  4363. // (Upper - Lower [- 1] + Step) / Step
  4364. Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
  4365. if (!Diff.isUsable())
  4366. return nullptr;
  4367. // OpenMP runtime requires 32-bit or 64-bit loop variables.
  4368. QualType Type = Diff.get()->getType();
  4369. ASTContext &C = SemaRef.Context;
  4370. bool UseVarType = VarType->hasIntegerRepresentation() &&
  4371. C.getTypeSize(Type) > C.getTypeSize(VarType);
  4372. if (!Type->isIntegerType() || UseVarType) {
  4373. unsigned NewSize =
  4374. UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
  4375. bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
  4376. : Type->hasSignedIntegerRepresentation();
  4377. Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
  4378. if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
  4379. Diff = SemaRef.PerformImplicitConversion(
  4380. Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
  4381. if (!Diff.isUsable())
  4382. return nullptr;
  4383. }
  4384. }
  4385. if (LimitedType) {
  4386. unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
  4387. if (NewSize != C.getTypeSize(Type)) {
  4388. if (NewSize < C.getTypeSize(Type)) {
  4389. assert(NewSize == 64 && "incorrect loop var size");
  4390. SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
  4391. << InitSrcRange << ConditionSrcRange;
  4392. }
  4393. QualType NewType = C.getIntTypeForBitwidth(
  4394. NewSize, Type->hasSignedIntegerRepresentation() ||
  4395. C.getTypeSize(Type) < NewSize);
  4396. if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
  4397. Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
  4398. Sema::AA_Converting, true);
  4399. if (!Diff.isUsable())
  4400. return nullptr;
  4401. }
  4402. }
  4403. }
  4404. return Diff.get();
  4405. }
  4406. Expr *OpenMPIterationSpaceChecker::buildPreCond(
  4407. Scope *S, Expr *Cond,
  4408. llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
  4409. // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
  4410. bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
  4411. SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
  4412. ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
  4413. ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
  4414. if (!NewLB.isUsable() || !NewUB.isUsable())
  4415. return nullptr;
  4416. ExprResult CondExpr =
  4417. SemaRef.BuildBinOp(S, DefaultLoc,
  4418. TestIsLessOp.getValue() ?
  4419. (TestIsStrictOp ? BO_LT : BO_LE) :
  4420. (TestIsStrictOp ? BO_GT : BO_GE),
  4421. NewLB.get(), NewUB.get());
  4422. if (CondExpr.isUsable()) {
  4423. if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
  4424. SemaRef.Context.BoolTy))
  4425. CondExpr = SemaRef.PerformImplicitConversion(
  4426. CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
  4427. /*AllowExplicit=*/true);
  4428. }
  4429. SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
  4430. // Otherwise use original loop condition and evaluate it in runtime.
  4431. return CondExpr.isUsable() ? CondExpr.get() : Cond;
  4432. }
  4433. /// Build reference expression to the counter be used for codegen.
  4434. DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
  4435. llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
  4436. DSAStackTy &DSA) const {
  4437. auto *VD = dyn_cast<VarDecl>(LCDecl);
  4438. if (!VD) {
  4439. VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
  4440. DeclRefExpr *Ref = buildDeclRefExpr(
  4441. SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
  4442. const DSAStackTy::DSAVarData Data =
  4443. DSA.getTopDSA(LCDecl, /*FromParent=*/false);
  4444. // If the loop control decl is explicitly marked as private, do not mark it
  4445. // as captured again.
  4446. if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
  4447. Captures.insert(std::make_pair(LCRef, Ref));
  4448. return Ref;
  4449. }
  4450. return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
  4451. DefaultLoc);
  4452. }
  4453. Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
  4454. if (LCDecl && !LCDecl->isInvalidDecl()) {
  4455. QualType Type = LCDecl->getType().getNonReferenceType();
  4456. VarDecl *PrivateVar = buildVarDecl(
  4457. SemaRef, DefaultLoc, Type, LCDecl->getName(),
  4458. LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
  4459. isa<VarDecl>(LCDecl)
  4460. ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
  4461. : nullptr);
  4462. if (PrivateVar->isInvalidDecl())
  4463. return nullptr;
  4464. return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
  4465. }
  4466. return nullptr;
  4467. }
  4468. /// Build initialization of the counter to be used for codegen.
  4469. Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
  4470. /// Build step of the counter be used for codegen.
  4471. Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
  4472. Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
  4473. Scope *S, Expr *Counter,
  4474. llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
  4475. Expr *Inc, OverloadedOperatorKind OOK) {
  4476. Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
  4477. if (!Cnt)
  4478. return nullptr;
  4479. if (Inc) {
  4480. assert((OOK == OO_Plus || OOK == OO_Minus) &&
  4481. "Expected only + or - operations for depend clauses.");
  4482. BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
  4483. Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
  4484. if (!Cnt)
  4485. return nullptr;
  4486. }
  4487. ExprResult Diff;
  4488. QualType VarType = LCDecl->getType().getNonReferenceType();
  4489. if (VarType->isIntegerType() || VarType->isPointerType() ||
  4490. SemaRef.getLangOpts().CPlusPlus) {
  4491. // Upper - Lower
  4492. Expr *Upper = TestIsLessOp.getValue()
  4493. ? Cnt
  4494. : tryBuildCapture(SemaRef, UB, Captures).get();
  4495. Expr *Lower = TestIsLessOp.getValue()
  4496. ? tryBuildCapture(SemaRef, LB, Captures).get()
  4497. : Cnt;
  4498. if (!Upper || !Lower)
  4499. return nullptr;
  4500. Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
  4501. if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
  4502. // BuildBinOp already emitted error, this one is to point user to upper
  4503. // and lower bound, and to tell what is passed to 'operator-'.
  4504. SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
  4505. << Upper->getSourceRange() << Lower->getSourceRange();
  4506. return nullptr;
  4507. }
  4508. }
  4509. if (!Diff.isUsable())
  4510. return nullptr;
  4511. // Parentheses (for dumping/debugging purposes only).
  4512. Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
  4513. if (!Diff.isUsable())
  4514. return nullptr;
  4515. ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
  4516. if (!NewStep.isUsable())
  4517. return nullptr;
  4518. // (Upper - Lower) / Step
  4519. Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
  4520. if (!Diff.isUsable())
  4521. return nullptr;
  4522. return Diff.get();
  4523. }
  4524. /// Iteration space of a single for loop.
  4525. struct LoopIterationSpace final {
  4526. /// True if the condition operator is the strict compare operator (<, > or
  4527. /// !=).
  4528. bool IsStrictCompare = false;
  4529. /// Condition of the loop.
  4530. Expr *PreCond = nullptr;
  4531. /// This expression calculates the number of iterations in the loop.
  4532. /// It is always possible to calculate it before starting the loop.
  4533. Expr *NumIterations = nullptr;
  4534. /// The loop counter variable.
  4535. Expr *CounterVar = nullptr;
  4536. /// Private loop counter variable.
  4537. Expr *PrivateCounterVar = nullptr;
  4538. /// This is initializer for the initial value of #CounterVar.
  4539. Expr *CounterInit = nullptr;
  4540. /// This is step for the #CounterVar used to generate its update:
  4541. /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
  4542. Expr *CounterStep = nullptr;
  4543. /// Should step be subtracted?
  4544. bool Subtract = false;
  4545. /// Source range of the loop init.
  4546. SourceRange InitSrcRange;
  4547. /// Source range of the loop condition.
  4548. SourceRange CondSrcRange;
  4549. /// Source range of the loop increment.
  4550. SourceRange IncSrcRange;
  4551. };
  4552. } // namespace
  4553. void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
  4554. assert(getLangOpts().OpenMP && "OpenMP is not active.");
  4555. assert(Init && "Expected loop in canonical form.");
  4556. unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
  4557. if (AssociatedLoops > 0 &&
  4558. isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
  4559. DSAStack->loopStart();
  4560. OpenMPIterationSpaceChecker ISC(*this, ForLoc);
  4561. if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
  4562. if (ValueDecl *D = ISC.getLoopDecl()) {
  4563. auto *VD = dyn_cast<VarDecl>(D);
  4564. if (!VD) {
  4565. if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
  4566. VD = Private;
  4567. } else {
  4568. DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
  4569. /*WithInit=*/false);
  4570. VD = cast<VarDecl>(Ref->getDecl());
  4571. }
  4572. }
  4573. DSAStack->addLoopControlVariable(D, VD);
  4574. const Decl *LD = DSAStack->getPossiblyLoopCunter();
  4575. if (LD != D->getCanonicalDecl()) {
  4576. DSAStack->resetPossibleLoopCounter();
  4577. if (auto *Var = dyn_cast_or_null<VarDecl>(LD))
  4578. MarkDeclarationsReferencedInExpr(
  4579. buildDeclRefExpr(*this, const_cast<VarDecl *>(Var),
  4580. Var->getType().getNonLValueExprType(Context),
  4581. ForLoc, /*RefersToCapture=*/true));
  4582. }
  4583. }
  4584. }
  4585. DSAStack->setAssociatedLoops(AssociatedLoops - 1);
  4586. }
  4587. }
  4588. /// Called on a for stmt to check and extract its iteration space
  4589. /// for further processing (such as collapsing).
  4590. static bool checkOpenMPIterationSpace(
  4591. OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
  4592. unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
  4593. unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
  4594. Expr *OrderedLoopCountExpr,
  4595. Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
  4596. LoopIterationSpace &ResultIterSpace,
  4597. llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
  4598. // OpenMP [2.6, Canonical Loop Form]
  4599. // for (init-expr; test-expr; incr-expr) structured-block
  4600. auto *For = dyn_cast_or_null<ForStmt>(S);
  4601. if (!For) {
  4602. SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
  4603. << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
  4604. << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
  4605. << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
  4606. if (TotalNestedLoopCount > 1) {
  4607. if (CollapseLoopCountExpr && OrderedLoopCountExpr)
  4608. SemaRef.Diag(DSA.getConstructLoc(),
  4609. diag::note_omp_collapse_ordered_expr)
  4610. << 2 << CollapseLoopCountExpr->getSourceRange()
  4611. << OrderedLoopCountExpr->getSourceRange();
  4612. else if (CollapseLoopCountExpr)
  4613. SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
  4614. diag::note_omp_collapse_ordered_expr)
  4615. << 0 << CollapseLoopCountExpr->getSourceRange();
  4616. else
  4617. SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
  4618. diag::note_omp_collapse_ordered_expr)
  4619. << 1 << OrderedLoopCountExpr->getSourceRange();
  4620. }
  4621. return true;
  4622. }
  4623. assert(For->getBody());
  4624. OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
  4625. // Check init.
  4626. Stmt *Init = For->getInit();
  4627. if (ISC.checkAndSetInit(Init))
  4628. return true;
  4629. bool HasErrors = false;
  4630. // Check loop variable's type.
  4631. if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
  4632. Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
  4633. // OpenMP [2.6, Canonical Loop Form]
  4634. // Var is one of the following:
  4635. // A variable of signed or unsigned integer type.
  4636. // For C++, a variable of a random access iterator type.
  4637. // For C, a variable of a pointer type.
  4638. QualType VarType = LCDecl->getType().getNonReferenceType();
  4639. if (!VarType->isDependentType() && !VarType->isIntegerType() &&
  4640. !VarType->isPointerType() &&
  4641. !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
  4642. SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
  4643. << SemaRef.getLangOpts().CPlusPlus;
  4644. HasErrors = true;
  4645. }
  4646. // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
  4647. // a Construct
  4648. // The loop iteration variable(s) in the associated for-loop(s) of a for or
  4649. // parallel for construct is (are) private.
  4650. // The loop iteration variable in the associated for-loop of a simd
  4651. // construct with just one associated for-loop is linear with a
  4652. // constant-linear-step that is the increment of the associated for-loop.
  4653. // Exclude loop var from the list of variables with implicitly defined data
  4654. // sharing attributes.
  4655. VarsWithImplicitDSA.erase(LCDecl);
  4656. // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
  4657. // in a Construct, C/C++].
  4658. // The loop iteration variable in the associated for-loop of a simd
  4659. // construct with just one associated for-loop may be listed in a linear
  4660. // clause with a constant-linear-step that is the increment of the
  4661. // associated for-loop.
  4662. // The loop iteration variable(s) in the associated for-loop(s) of a for or
  4663. // parallel for construct may be listed in a private or lastprivate clause.
  4664. DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
  4665. // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
  4666. // declared in the loop and it is predetermined as a private.
  4667. OpenMPClauseKind PredeterminedCKind =
  4668. isOpenMPSimdDirective(DKind)
  4669. ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
  4670. : OMPC_private;
  4671. if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
  4672. DVar.CKind != PredeterminedCKind) ||
  4673. ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
  4674. isOpenMPDistributeDirective(DKind)) &&
  4675. !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
  4676. DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
  4677. (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
  4678. SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
  4679. << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
  4680. << getOpenMPClauseName(PredeterminedCKind);
  4681. if (DVar.RefExpr == nullptr)
  4682. DVar.CKind = PredeterminedCKind;
  4683. reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
  4684. HasErrors = true;
  4685. } else if (LoopDeclRefExpr != nullptr) {
  4686. // Make the loop iteration variable private (for worksharing constructs),
  4687. // linear (for simd directives with the only one associated loop) or
  4688. // lastprivate (for simd directives with several collapsed or ordered
  4689. // loops).
  4690. if (DVar.CKind == OMPC_unknown)
  4691. DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
  4692. }
  4693. assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
  4694. // Check test-expr.
  4695. HasErrors |= ISC.checkAndSetCond(For->getCond());
  4696. // Check incr-expr.
  4697. HasErrors |= ISC.checkAndSetInc(For->getInc());
  4698. }
  4699. if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
  4700. return HasErrors;
  4701. // Build the loop's iteration space representation.
  4702. ResultIterSpace.PreCond =
  4703. ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures);
  4704. ResultIterSpace.NumIterations = ISC.buildNumIterations(
  4705. DSA.getCurScope(),
  4706. (isOpenMPWorksharingDirective(DKind) ||
  4707. isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
  4708. Captures);
  4709. ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA);
  4710. ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar();
  4711. ResultIterSpace.CounterInit = ISC.buildCounterInit();
  4712. ResultIterSpace.CounterStep = ISC.buildCounterStep();
  4713. ResultIterSpace.InitSrcRange = ISC.getInitSrcRange();
  4714. ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange();
  4715. ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange();
  4716. ResultIterSpace.Subtract = ISC.shouldSubtractStep();
  4717. ResultIterSpace.IsStrictCompare = ISC.isStrictTestOp();
  4718. HasErrors |= (ResultIterSpace.PreCond == nullptr ||
  4719. ResultIterSpace.NumIterations == nullptr ||
  4720. ResultIterSpace.CounterVar == nullptr ||
  4721. ResultIterSpace.PrivateCounterVar == nullptr ||
  4722. ResultIterSpace.CounterInit == nullptr ||
  4723. ResultIterSpace.CounterStep == nullptr);
  4724. if (!HasErrors && DSA.isOrderedRegion()) {
  4725. if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
  4726. if (CurrentNestedLoopCount <
  4727. DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
  4728. DSA.getOrderedRegionParam().second->setLoopNumIterations(
  4729. CurrentNestedLoopCount, ResultIterSpace.NumIterations);
  4730. DSA.getOrderedRegionParam().second->setLoopCounter(
  4731. CurrentNestedLoopCount, ResultIterSpace.CounterVar);
  4732. }
  4733. }
  4734. for (auto &Pair : DSA.getDoacrossDependClauses()) {
  4735. if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
  4736. // Erroneous case - clause has some problems.
  4737. continue;
  4738. }
  4739. if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
  4740. Pair.second.size() <= CurrentNestedLoopCount) {
  4741. // Erroneous case - clause has some problems.
  4742. Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
  4743. continue;
  4744. }
  4745. Expr *CntValue;
  4746. if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
  4747. CntValue = ISC.buildOrderedLoopData(
  4748. DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
  4749. Pair.first->getDependencyLoc());
  4750. else
  4751. CntValue = ISC.buildOrderedLoopData(
  4752. DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
  4753. Pair.first->getDependencyLoc(),
  4754. Pair.second[CurrentNestedLoopCount].first,
  4755. Pair.second[CurrentNestedLoopCount].second);
  4756. Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
  4757. }
  4758. }
  4759. return HasErrors;
  4760. }
  4761. /// Build 'VarRef = Start.
  4762. static ExprResult
  4763. buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
  4764. ExprResult Start,
  4765. llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
  4766. // Build 'VarRef = Start.
  4767. ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
  4768. if (!NewStart.isUsable())
  4769. return ExprError();
  4770. if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
  4771. VarRef.get()->getType())) {
  4772. NewStart = SemaRef.PerformImplicitConversion(
  4773. NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
  4774. /*AllowExplicit=*/true);
  4775. if (!NewStart.isUsable())
  4776. return ExprError();
  4777. }
  4778. ExprResult Init =
  4779. SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
  4780. return Init;
  4781. }
  4782. /// Build 'VarRef = Start + Iter * Step'.
  4783. static ExprResult buildCounterUpdate(
  4784. Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
  4785. ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
  4786. llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
  4787. // Add parentheses (for debugging purposes only).
  4788. Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
  4789. if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
  4790. !Step.isUsable())
  4791. return ExprError();
  4792. ExprResult NewStep = Step;
  4793. if (Captures)
  4794. NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
  4795. if (NewStep.isInvalid())
  4796. return ExprError();
  4797. ExprResult Update =
  4798. SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
  4799. if (!Update.isUsable())
  4800. return ExprError();
  4801. // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
  4802. // 'VarRef = Start (+|-) Iter * Step'.
  4803. ExprResult NewStart = Start;
  4804. if (Captures)
  4805. NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
  4806. if (NewStart.isInvalid())
  4807. return ExprError();
  4808. // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
  4809. ExprResult SavedUpdate = Update;
  4810. ExprResult UpdateVal;
  4811. if (VarRef.get()->getType()->isOverloadableType() ||
  4812. NewStart.get()->getType()->isOverloadableType() ||
  4813. Update.get()->getType()->isOverloadableType()) {
  4814. bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
  4815. SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
  4816. Update =
  4817. SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
  4818. if (Update.isUsable()) {
  4819. UpdateVal =
  4820. SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
  4821. VarRef.get(), SavedUpdate.get());
  4822. if (UpdateVal.isUsable()) {
  4823. Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
  4824. UpdateVal.get());
  4825. }
  4826. }
  4827. SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
  4828. }
  4829. // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
  4830. if (!Update.isUsable() || !UpdateVal.isUsable()) {
  4831. Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
  4832. NewStart.get(), SavedUpdate.get());
  4833. if (!Update.isUsable())
  4834. return ExprError();
  4835. if (!SemaRef.Context.hasSameType(Update.get()->getType(),
  4836. VarRef.get()->getType())) {
  4837. Update = SemaRef.PerformImplicitConversion(
  4838. Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
  4839. if (!Update.isUsable())
  4840. return ExprError();
  4841. }
  4842. Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
  4843. }
  4844. return Update;
  4845. }
  4846. /// Convert integer expression \a E to make it have at least \a Bits
  4847. /// bits.
  4848. static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
  4849. if (E == nullptr)
  4850. return ExprError();
  4851. ASTContext &C = SemaRef.Context;
  4852. QualType OldType = E->getType();
  4853. unsigned HasBits = C.getTypeSize(OldType);
  4854. if (HasBits >= Bits)
  4855. return ExprResult(E);
  4856. // OK to convert to signed, because new type has more bits than old.
  4857. QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
  4858. return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
  4859. true);
  4860. }
  4861. /// Check if the given expression \a E is a constant integer that fits
  4862. /// into \a Bits bits.
  4863. static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
  4864. if (E == nullptr)
  4865. return false;
  4866. llvm::APSInt Result;
  4867. if (E->isIntegerConstantExpr(Result, SemaRef.Context))
  4868. return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
  4869. return false;
  4870. }
  4871. /// Build preinits statement for the given declarations.
  4872. static Stmt *buildPreInits(ASTContext &Context,
  4873. MutableArrayRef<Decl *> PreInits) {
  4874. if (!PreInits.empty()) {
  4875. return new (Context) DeclStmt(
  4876. DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
  4877. SourceLocation(), SourceLocation());
  4878. }
  4879. return nullptr;
  4880. }
  4881. /// Build preinits statement for the given declarations.
  4882. static Stmt *
  4883. buildPreInits(ASTContext &Context,
  4884. const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
  4885. if (!Captures.empty()) {
  4886. SmallVector<Decl *, 16> PreInits;
  4887. for (const auto &Pair : Captures)
  4888. PreInits.push_back(Pair.second->getDecl());
  4889. return buildPreInits(Context, PreInits);
  4890. }
  4891. return nullptr;
  4892. }
  4893. /// Build postupdate expression for the given list of postupdates expressions.
  4894. static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
  4895. Expr *PostUpdate = nullptr;
  4896. if (!PostUpdates.empty()) {
  4897. for (Expr *E : PostUpdates) {
  4898. Expr *ConvE = S.BuildCStyleCastExpr(
  4899. E->getExprLoc(),
  4900. S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
  4901. E->getExprLoc(), E)
  4902. .get();
  4903. PostUpdate = PostUpdate
  4904. ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
  4905. PostUpdate, ConvE)
  4906. .get()
  4907. : ConvE;
  4908. }
  4909. }
  4910. return PostUpdate;
  4911. }
  4912. /// Called on a for stmt to check itself and nested loops (if any).
  4913. /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
  4914. /// number of collapsed loops otherwise.
  4915. static unsigned
  4916. checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
  4917. Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
  4918. DSAStackTy &DSA,
  4919. Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
  4920. OMPLoopDirective::HelperExprs &Built) {
  4921. unsigned NestedLoopCount = 1;
  4922. if (CollapseLoopCountExpr) {
  4923. // Found 'collapse' clause - calculate collapse number.
  4924. Expr::EvalResult Result;
  4925. if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
  4926. NestedLoopCount = Result.Val.getInt().getLimitedValue();
  4927. }
  4928. unsigned OrderedLoopCount = 1;
  4929. if (OrderedLoopCountExpr) {
  4930. // Found 'ordered' clause - calculate collapse number.
  4931. Expr::EvalResult EVResult;
  4932. if (OrderedLoopCountExpr->EvaluateAsInt(EVResult, SemaRef.getASTContext())) {
  4933. llvm::APSInt Result = EVResult.Val.getInt();
  4934. if (Result.getLimitedValue() < NestedLoopCount) {
  4935. SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
  4936. diag::err_omp_wrong_ordered_loop_count)
  4937. << OrderedLoopCountExpr->getSourceRange();
  4938. SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
  4939. diag::note_collapse_loop_count)
  4940. << CollapseLoopCountExpr->getSourceRange();
  4941. }
  4942. OrderedLoopCount = Result.getLimitedValue();
  4943. }
  4944. }
  4945. // This is helper routine for loop directives (e.g., 'for', 'simd',
  4946. // 'for simd', etc.).
  4947. llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
  4948. SmallVector<LoopIterationSpace, 4> IterSpaces(
  4949. std::max(OrderedLoopCount, NestedLoopCount));
  4950. Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
  4951. for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
  4952. if (checkOpenMPIterationSpace(
  4953. DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
  4954. std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
  4955. OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
  4956. Captures))
  4957. return 0;
  4958. // Move on to the next nested for loop, or to the loop body.
  4959. // OpenMP [2.8.1, simd construct, Restrictions]
  4960. // All loops associated with the construct must be perfectly nested; that
  4961. // is, there must be no intervening code nor any OpenMP directive between
  4962. // any two loops.
  4963. CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
  4964. }
  4965. for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
  4966. if (checkOpenMPIterationSpace(
  4967. DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
  4968. std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
  4969. OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
  4970. Captures))
  4971. return 0;
  4972. if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
  4973. // Handle initialization of captured loop iterator variables.
  4974. auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
  4975. if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
  4976. Captures[DRE] = DRE;
  4977. }
  4978. }
  4979. // Move on to the next nested for loop, or to the loop body.
  4980. // OpenMP [2.8.1, simd construct, Restrictions]
  4981. // All loops associated with the construct must be perfectly nested; that
  4982. // is, there must be no intervening code nor any OpenMP directive between
  4983. // any two loops.
  4984. CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
  4985. }
  4986. Built.clear(/* size */ NestedLoopCount);
  4987. if (SemaRef.CurContext->isDependentContext())
  4988. return NestedLoopCount;
  4989. // An example of what is generated for the following code:
  4990. //
  4991. // #pragma omp simd collapse(2) ordered(2)
  4992. // for (i = 0; i < NI; ++i)
  4993. // for (k = 0; k < NK; ++k)
  4994. // for (j = J0; j < NJ; j+=2) {
  4995. // <loop body>
  4996. // }
  4997. //
  4998. // We generate the code below.
  4999. // Note: the loop body may be outlined in CodeGen.
  5000. // Note: some counters may be C++ classes, operator- is used to find number of
  5001. // iterations and operator+= to calculate counter value.
  5002. // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
  5003. // or i64 is currently supported).
  5004. //
  5005. // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
  5006. // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
  5007. // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
  5008. // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
  5009. // // similar updates for vars in clauses (e.g. 'linear')
  5010. // <loop body (using local i and j)>
  5011. // }
  5012. // i = NI; // assign final values of counters
  5013. // j = NJ;
  5014. //
  5015. // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
  5016. // the iteration counts of the collapsed for loops.
  5017. // Precondition tests if there is at least one iteration (all conditions are
  5018. // true).
  5019. auto PreCond = ExprResult(IterSpaces[0].PreCond);
  5020. Expr *N0 = IterSpaces[0].NumIterations;
  5021. ExprResult LastIteration32 =
  5022. widenIterationCount(/*Bits=*/32,
  5023. SemaRef
  5024. .PerformImplicitConversion(
  5025. N0->IgnoreImpCasts(), N0->getType(),
  5026. Sema::AA_Converting, /*AllowExplicit=*/true)
  5027. .get(),
  5028. SemaRef);
  5029. ExprResult LastIteration64 = widenIterationCount(
  5030. /*Bits=*/64,
  5031. SemaRef
  5032. .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
  5033. Sema::AA_Converting,
  5034. /*AllowExplicit=*/true)
  5035. .get(),
  5036. SemaRef);
  5037. if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
  5038. return NestedLoopCount;
  5039. ASTContext &C = SemaRef.Context;
  5040. bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
  5041. Scope *CurScope = DSA.getCurScope();
  5042. for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
  5043. if (PreCond.isUsable()) {
  5044. PreCond =
  5045. SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
  5046. PreCond.get(), IterSpaces[Cnt].PreCond);
  5047. }
  5048. Expr *N = IterSpaces[Cnt].NumIterations;
  5049. SourceLocation Loc = N->getExprLoc();
  5050. AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
  5051. if (LastIteration32.isUsable())
  5052. LastIteration32 = SemaRef.BuildBinOp(
  5053. CurScope, Loc, BO_Mul, LastIteration32.get(),
  5054. SemaRef
  5055. .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
  5056. Sema::AA_Converting,
  5057. /*AllowExplicit=*/true)
  5058. .get());
  5059. if (LastIteration64.isUsable())
  5060. LastIteration64 = SemaRef.BuildBinOp(
  5061. CurScope, Loc, BO_Mul, LastIteration64.get(),
  5062. SemaRef
  5063. .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
  5064. Sema::AA_Converting,
  5065. /*AllowExplicit=*/true)
  5066. .get());
  5067. }
  5068. // Choose either the 32-bit or 64-bit version.
  5069. ExprResult LastIteration = LastIteration64;
  5070. if (SemaRef.getLangOpts().OpenMPOptimisticCollapse ||
  5071. (LastIteration32.isUsable() &&
  5072. C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
  5073. (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
  5074. fitsInto(
  5075. /*Bits=*/32,
  5076. LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
  5077. LastIteration64.get(), SemaRef))))
  5078. LastIteration = LastIteration32;
  5079. QualType VType = LastIteration.get()->getType();
  5080. QualType RealVType = VType;
  5081. QualType StrideVType = VType;
  5082. if (isOpenMPTaskLoopDirective(DKind)) {
  5083. VType =
  5084. SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
  5085. StrideVType =
  5086. SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
  5087. }
  5088. if (!LastIteration.isUsable())
  5089. return 0;
  5090. // Save the number of iterations.
  5091. ExprResult NumIterations = LastIteration;
  5092. {
  5093. LastIteration = SemaRef.BuildBinOp(
  5094. CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
  5095. LastIteration.get(),
  5096. SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
  5097. if (!LastIteration.isUsable())
  5098. return 0;
  5099. }
  5100. // Calculate the last iteration number beforehand instead of doing this on
  5101. // each iteration. Do not do this if the number of iterations may be kfold-ed.
  5102. llvm::APSInt Result;
  5103. bool IsConstant =
  5104. LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
  5105. ExprResult CalcLastIteration;
  5106. if (!IsConstant) {
  5107. ExprResult SaveRef =
  5108. tryBuildCapture(SemaRef, LastIteration.get(), Captures);
  5109. LastIteration = SaveRef;
  5110. // Prepare SaveRef + 1.
  5111. NumIterations = SemaRef.BuildBinOp(
  5112. CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
  5113. SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
  5114. if (!NumIterations.isUsable())
  5115. return 0;
  5116. }
  5117. SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
  5118. // Build variables passed into runtime, necessary for worksharing directives.
  5119. ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
  5120. if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
  5121. isOpenMPDistributeDirective(DKind)) {
  5122. // Lower bound variable, initialized with zero.
  5123. VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
  5124. LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
  5125. SemaRef.AddInitializerToDecl(LBDecl,
  5126. SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
  5127. /*DirectInit*/ false);
  5128. // Upper bound variable, initialized with last iteration number.
  5129. VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
  5130. UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
  5131. SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
  5132. /*DirectInit*/ false);
  5133. // A 32-bit variable-flag where runtime returns 1 for the last iteration.
  5134. // This will be used to implement clause 'lastprivate'.
  5135. QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
  5136. VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
  5137. IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
  5138. SemaRef.AddInitializerToDecl(ILDecl,
  5139. SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
  5140. /*DirectInit*/ false);
  5141. // Stride variable returned by runtime (we initialize it to 1 by default).
  5142. VarDecl *STDecl =
  5143. buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
  5144. ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
  5145. SemaRef.AddInitializerToDecl(STDecl,
  5146. SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
  5147. /*DirectInit*/ false);
  5148. // Build expression: UB = min(UB, LastIteration)
  5149. // It is necessary for CodeGen of directives with static scheduling.
  5150. ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
  5151. UB.get(), LastIteration.get());
  5152. ExprResult CondOp = SemaRef.ActOnConditionalOp(
  5153. LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
  5154. LastIteration.get(), UB.get());
  5155. EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
  5156. CondOp.get());
  5157. EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false);
  5158. // If we have a combined directive that combines 'distribute', 'for' or
  5159. // 'simd' we need to be able to access the bounds of the schedule of the
  5160. // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
  5161. // by scheduling 'distribute' have to be passed to the schedule of 'for'.
  5162. if (isOpenMPLoopBoundSharingDirective(DKind)) {
  5163. // Lower bound variable, initialized with zero.
  5164. VarDecl *CombLBDecl =
  5165. buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
  5166. CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
  5167. SemaRef.AddInitializerToDecl(
  5168. CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
  5169. /*DirectInit*/ false);
  5170. // Upper bound variable, initialized with last iteration number.
  5171. VarDecl *CombUBDecl =
  5172. buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
  5173. CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
  5174. SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
  5175. /*DirectInit*/ false);
  5176. ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
  5177. CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
  5178. ExprResult CombCondOp =
  5179. SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
  5180. LastIteration.get(), CombUB.get());
  5181. CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
  5182. CombCondOp.get());
  5183. CombEUB =
  5184. SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false);
  5185. const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
  5186. // We expect to have at least 2 more parameters than the 'parallel'
  5187. // directive does - the lower and upper bounds of the previous schedule.
  5188. assert(CD->getNumParams() >= 4 &&
  5189. "Unexpected number of parameters in loop combined directive");
  5190. // Set the proper type for the bounds given what we learned from the
  5191. // enclosed loops.
  5192. ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
  5193. ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
  5194. // Previous lower and upper bounds are obtained from the region
  5195. // parameters.
  5196. PrevLB =
  5197. buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
  5198. PrevUB =
  5199. buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
  5200. }
  5201. }
  5202. // Build the iteration variable and its initialization before loop.
  5203. ExprResult IV;
  5204. ExprResult Init, CombInit;
  5205. {
  5206. VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
  5207. IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
  5208. Expr *RHS =
  5209. (isOpenMPWorksharingDirective(DKind) ||
  5210. isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
  5211. ? LB.get()
  5212. : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
  5213. Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
  5214. Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false);
  5215. if (isOpenMPLoopBoundSharingDirective(DKind)) {
  5216. Expr *CombRHS =
  5217. (isOpenMPWorksharingDirective(DKind) ||
  5218. isOpenMPTaskLoopDirective(DKind) ||
  5219. isOpenMPDistributeDirective(DKind))
  5220. ? CombLB.get()
  5221. : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
  5222. CombInit =
  5223. SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
  5224. CombInit =
  5225. SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false);
  5226. }
  5227. }
  5228. bool UseStrictCompare =
  5229. RealVType->hasUnsignedIntegerRepresentation() &&
  5230. llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) {
  5231. return LIS.IsStrictCompare;
  5232. });
  5233. // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for
  5234. // unsigned IV)) for worksharing loops.
  5235. SourceLocation CondLoc = AStmt->getBeginLoc();
  5236. Expr *BoundUB = UB.get();
  5237. if (UseStrictCompare) {
  5238. BoundUB =
  5239. SemaRef
  5240. .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB,
  5241. SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
  5242. .get();
  5243. BoundUB =
  5244. SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get();
  5245. }
  5246. ExprResult Cond =
  5247. (isOpenMPWorksharingDirective(DKind) ||
  5248. isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
  5249. ? SemaRef.BuildBinOp(CurScope, CondLoc,
  5250. UseStrictCompare ? BO_LT : BO_LE, IV.get(),
  5251. BoundUB)
  5252. : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
  5253. NumIterations.get());
  5254. ExprResult CombDistCond;
  5255. if (isOpenMPLoopBoundSharingDirective(DKind)) {
  5256. CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
  5257. NumIterations.get());
  5258. }
  5259. ExprResult CombCond;
  5260. if (isOpenMPLoopBoundSharingDirective(DKind)) {
  5261. Expr *BoundCombUB = CombUB.get();
  5262. if (UseStrictCompare) {
  5263. BoundCombUB =
  5264. SemaRef
  5265. .BuildBinOp(
  5266. CurScope, CondLoc, BO_Add, BoundCombUB,
  5267. SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
  5268. .get();
  5269. BoundCombUB =
  5270. SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false)
  5271. .get();
  5272. }
  5273. CombCond =
  5274. SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
  5275. IV.get(), BoundCombUB);
  5276. }
  5277. // Loop increment (IV = IV + 1)
  5278. SourceLocation IncLoc = AStmt->getBeginLoc();
  5279. ExprResult Inc =
  5280. SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
  5281. SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
  5282. if (!Inc.isUsable())
  5283. return 0;
  5284. Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
  5285. Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false);
  5286. if (!Inc.isUsable())
  5287. return 0;
  5288. // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
  5289. // Used for directives with static scheduling.
  5290. // In combined construct, add combined version that use CombLB and CombUB
  5291. // base variables for the update
  5292. ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
  5293. if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
  5294. isOpenMPDistributeDirective(DKind)) {
  5295. // LB + ST
  5296. NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
  5297. if (!NextLB.isUsable())
  5298. return 0;
  5299. // LB = LB + ST
  5300. NextLB =
  5301. SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
  5302. NextLB =
  5303. SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false);
  5304. if (!NextLB.isUsable())
  5305. return 0;
  5306. // UB + ST
  5307. NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
  5308. if (!NextUB.isUsable())
  5309. return 0;
  5310. // UB = UB + ST
  5311. NextUB =
  5312. SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
  5313. NextUB =
  5314. SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false);
  5315. if (!NextUB.isUsable())
  5316. return 0;
  5317. if (isOpenMPLoopBoundSharingDirective(DKind)) {
  5318. CombNextLB =
  5319. SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
  5320. if (!NextLB.isUsable())
  5321. return 0;
  5322. // LB = LB + ST
  5323. CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
  5324. CombNextLB.get());
  5325. CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(),
  5326. /*DiscardedValue*/ false);
  5327. if (!CombNextLB.isUsable())
  5328. return 0;
  5329. // UB + ST
  5330. CombNextUB =
  5331. SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
  5332. if (!CombNextUB.isUsable())
  5333. return 0;
  5334. // UB = UB + ST
  5335. CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
  5336. CombNextUB.get());
  5337. CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(),
  5338. /*DiscardedValue*/ false);
  5339. if (!CombNextUB.isUsable())
  5340. return 0;
  5341. }
  5342. }
  5343. // Create increment expression for distribute loop when combined in a same
  5344. // directive with for as IV = IV + ST; ensure upper bound expression based
  5345. // on PrevUB instead of NumIterations - used to implement 'for' when found
  5346. // in combination with 'distribute', like in 'distribute parallel for'
  5347. SourceLocation DistIncLoc = AStmt->getBeginLoc();
  5348. ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
  5349. if (isOpenMPLoopBoundSharingDirective(DKind)) {
  5350. DistCond = SemaRef.BuildBinOp(
  5351. CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB);
  5352. assert(DistCond.isUsable() && "distribute cond expr was not built");
  5353. DistInc =
  5354. SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
  5355. assert(DistInc.isUsable() && "distribute inc expr was not built");
  5356. DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
  5357. DistInc.get());
  5358. DistInc =
  5359. SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false);
  5360. assert(DistInc.isUsable() && "distribute inc expr was not built");
  5361. // Build expression: UB = min(UB, prevUB) for #for in composite or combined
  5362. // construct
  5363. SourceLocation DistEUBLoc = AStmt->getBeginLoc();
  5364. ExprResult IsUBGreater =
  5365. SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
  5366. ExprResult CondOp = SemaRef.ActOnConditionalOp(
  5367. DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
  5368. PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
  5369. CondOp.get());
  5370. PrevEUB =
  5371. SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false);
  5372. // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in
  5373. // parallel for is in combination with a distribute directive with
  5374. // schedule(static, 1)
  5375. Expr *BoundPrevUB = PrevUB.get();
  5376. if (UseStrictCompare) {
  5377. BoundPrevUB =
  5378. SemaRef
  5379. .BuildBinOp(
  5380. CurScope, CondLoc, BO_Add, BoundPrevUB,
  5381. SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
  5382. .get();
  5383. BoundPrevUB =
  5384. SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false)
  5385. .get();
  5386. }
  5387. ParForInDistCond =
  5388. SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
  5389. IV.get(), BoundPrevUB);
  5390. }
  5391. // Build updates and final values of the loop counters.
  5392. bool HasErrors = false;
  5393. Built.Counters.resize(NestedLoopCount);
  5394. Built.Inits.resize(NestedLoopCount);
  5395. Built.Updates.resize(NestedLoopCount);
  5396. Built.Finals.resize(NestedLoopCount);
  5397. {
  5398. // We implement the following algorithm for obtaining the
  5399. // original loop iteration variable values based on the
  5400. // value of the collapsed loop iteration variable IV.
  5401. //
  5402. // Let n+1 be the number of collapsed loops in the nest.
  5403. // Iteration variables (I0, I1, .... In)
  5404. // Iteration counts (N0, N1, ... Nn)
  5405. //
  5406. // Acc = IV;
  5407. //
  5408. // To compute Ik for loop k, 0 <= k <= n, generate:
  5409. // Prod = N(k+1) * N(k+2) * ... * Nn;
  5410. // Ik = Acc / Prod;
  5411. // Acc -= Ik * Prod;
  5412. //
  5413. ExprResult Acc = IV;
  5414. for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
  5415. LoopIterationSpace &IS = IterSpaces[Cnt];
  5416. SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
  5417. ExprResult Iter;
  5418. // Compute prod
  5419. ExprResult Prod =
  5420. SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
  5421. for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K)
  5422. Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(),
  5423. IterSpaces[K].NumIterations);
  5424. // Iter = Acc / Prod
  5425. // If there is at least one more inner loop to avoid
  5426. // multiplication by 1.
  5427. if (Cnt + 1 < NestedLoopCount)
  5428. Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div,
  5429. Acc.get(), Prod.get());
  5430. else
  5431. Iter = Acc;
  5432. if (!Iter.isUsable()) {
  5433. HasErrors = true;
  5434. break;
  5435. }
  5436. // Update Acc:
  5437. // Acc -= Iter * Prod
  5438. // Check if there is at least one more inner loop to avoid
  5439. // multiplication by 1.
  5440. if (Cnt + 1 < NestedLoopCount)
  5441. Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul,
  5442. Iter.get(), Prod.get());
  5443. else
  5444. Prod = Iter;
  5445. Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub,
  5446. Acc.get(), Prod.get());
  5447. // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
  5448. auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
  5449. DeclRefExpr *CounterVar = buildDeclRefExpr(
  5450. SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
  5451. /*RefersToCapture=*/true);
  5452. ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
  5453. IS.CounterInit, Captures);
  5454. if (!Init.isUsable()) {
  5455. HasErrors = true;
  5456. break;
  5457. }
  5458. ExprResult Update = buildCounterUpdate(
  5459. SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
  5460. IS.CounterStep, IS.Subtract, &Captures);
  5461. if (!Update.isUsable()) {
  5462. HasErrors = true;
  5463. break;
  5464. }
  5465. // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
  5466. ExprResult Final = buildCounterUpdate(
  5467. SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
  5468. IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
  5469. if (!Final.isUsable()) {
  5470. HasErrors = true;
  5471. break;
  5472. }
  5473. if (!Update.isUsable() || !Final.isUsable()) {
  5474. HasErrors = true;
  5475. break;
  5476. }
  5477. // Save results
  5478. Built.Counters[Cnt] = IS.CounterVar;
  5479. Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
  5480. Built.Inits[Cnt] = Init.get();
  5481. Built.Updates[Cnt] = Update.get();
  5482. Built.Finals[Cnt] = Final.get();
  5483. }
  5484. }
  5485. if (HasErrors)
  5486. return 0;
  5487. // Save results
  5488. Built.IterationVarRef = IV.get();
  5489. Built.LastIteration = LastIteration.get();
  5490. Built.NumIterations = NumIterations.get();
  5491. Built.CalcLastIteration = SemaRef
  5492. .ActOnFinishFullExpr(CalcLastIteration.get(),
  5493. /*DiscardedValue*/ false)
  5494. .get();
  5495. Built.PreCond = PreCond.get();
  5496. Built.PreInits = buildPreInits(C, Captures);
  5497. Built.Cond = Cond.get();
  5498. Built.Init = Init.get();
  5499. Built.Inc = Inc.get();
  5500. Built.LB = LB.get();
  5501. Built.UB = UB.get();
  5502. Built.IL = IL.get();
  5503. Built.ST = ST.get();
  5504. Built.EUB = EUB.get();
  5505. Built.NLB = NextLB.get();
  5506. Built.NUB = NextUB.get();
  5507. Built.PrevLB = PrevLB.get();
  5508. Built.PrevUB = PrevUB.get();
  5509. Built.DistInc = DistInc.get();
  5510. Built.PrevEUB = PrevEUB.get();
  5511. Built.DistCombinedFields.LB = CombLB.get();
  5512. Built.DistCombinedFields.UB = CombUB.get();
  5513. Built.DistCombinedFields.EUB = CombEUB.get();
  5514. Built.DistCombinedFields.Init = CombInit.get();
  5515. Built.DistCombinedFields.Cond = CombCond.get();
  5516. Built.DistCombinedFields.NLB = CombNextLB.get();
  5517. Built.DistCombinedFields.NUB = CombNextUB.get();
  5518. Built.DistCombinedFields.DistCond = CombDistCond.get();
  5519. Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
  5520. return NestedLoopCount;
  5521. }
  5522. static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
  5523. auto CollapseClauses =
  5524. OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
  5525. if (CollapseClauses.begin() != CollapseClauses.end())
  5526. return (*CollapseClauses.begin())->getNumForLoops();
  5527. return nullptr;
  5528. }
  5529. static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
  5530. auto OrderedClauses =
  5531. OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
  5532. if (OrderedClauses.begin() != OrderedClauses.end())
  5533. return (*OrderedClauses.begin())->getNumForLoops();
  5534. return nullptr;
  5535. }
  5536. static bool checkSimdlenSafelenSpecified(Sema &S,
  5537. const ArrayRef<OMPClause *> Clauses) {
  5538. const OMPSafelenClause *Safelen = nullptr;
  5539. const OMPSimdlenClause *Simdlen = nullptr;
  5540. for (const OMPClause *Clause : Clauses) {
  5541. if (Clause->getClauseKind() == OMPC_safelen)
  5542. Safelen = cast<OMPSafelenClause>(Clause);
  5543. else if (Clause->getClauseKind() == OMPC_simdlen)
  5544. Simdlen = cast<OMPSimdlenClause>(Clause);
  5545. if (Safelen && Simdlen)
  5546. break;
  5547. }
  5548. if (Simdlen && Safelen) {
  5549. const Expr *SimdlenLength = Simdlen->getSimdlen();
  5550. const Expr *SafelenLength = Safelen->getSafelen();
  5551. if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
  5552. SimdlenLength->isInstantiationDependent() ||
  5553. SimdlenLength->containsUnexpandedParameterPack())
  5554. return false;
  5555. if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
  5556. SafelenLength->isInstantiationDependent() ||
  5557. SafelenLength->containsUnexpandedParameterPack())
  5558. return false;
  5559. Expr::EvalResult SimdlenResult, SafelenResult;
  5560. SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context);
  5561. SafelenLength->EvaluateAsInt(SafelenResult, S.Context);
  5562. llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt();
  5563. llvm::APSInt SafelenRes = SafelenResult.Val.getInt();
  5564. // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
  5565. // If both simdlen and safelen clauses are specified, the value of the
  5566. // simdlen parameter must be less than or equal to the value of the safelen
  5567. // parameter.
  5568. if (SimdlenRes > SafelenRes) {
  5569. S.Diag(SimdlenLength->getExprLoc(),
  5570. diag::err_omp_wrong_simdlen_safelen_values)
  5571. << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
  5572. return true;
  5573. }
  5574. }
  5575. return false;
  5576. }
  5577. StmtResult
  5578. Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
  5579. SourceLocation StartLoc, SourceLocation EndLoc,
  5580. VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  5581. if (!AStmt)
  5582. return StmtError();
  5583. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  5584. OMPLoopDirective::HelperExprs B;
  5585. // In presence of clause 'collapse' or 'ordered' with number of loops, it will
  5586. // define the nested loops number.
  5587. unsigned NestedLoopCount = checkOpenMPLoop(
  5588. OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
  5589. AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
  5590. if (NestedLoopCount == 0)
  5591. return StmtError();
  5592. assert((CurContext->isDependentContext() || B.builtAll()) &&
  5593. "omp simd loop exprs were not built");
  5594. if (!CurContext->isDependentContext()) {
  5595. // Finalize the clauses that need pre-built expressions for CodeGen.
  5596. for (OMPClause *C : Clauses) {
  5597. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  5598. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  5599. B.NumIterations, *this, CurScope,
  5600. DSAStack))
  5601. return StmtError();
  5602. }
  5603. }
  5604. if (checkSimdlenSafelenSpecified(*this, Clauses))
  5605. return StmtError();
  5606. setFunctionHasBranchProtectedScope();
  5607. return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
  5608. Clauses, AStmt, B);
  5609. }
  5610. StmtResult
  5611. Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
  5612. SourceLocation StartLoc, SourceLocation EndLoc,
  5613. VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  5614. if (!AStmt)
  5615. return StmtError();
  5616. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  5617. OMPLoopDirective::HelperExprs B;
  5618. // In presence of clause 'collapse' or 'ordered' with number of loops, it will
  5619. // define the nested loops number.
  5620. unsigned NestedLoopCount = checkOpenMPLoop(
  5621. OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
  5622. AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
  5623. if (NestedLoopCount == 0)
  5624. return StmtError();
  5625. assert((CurContext->isDependentContext() || B.builtAll()) &&
  5626. "omp for loop exprs were not built");
  5627. if (!CurContext->isDependentContext()) {
  5628. // Finalize the clauses that need pre-built expressions for CodeGen.
  5629. for (OMPClause *C : Clauses) {
  5630. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  5631. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  5632. B.NumIterations, *this, CurScope,
  5633. DSAStack))
  5634. return StmtError();
  5635. }
  5636. }
  5637. setFunctionHasBranchProtectedScope();
  5638. return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
  5639. Clauses, AStmt, B, DSAStack->isCancelRegion());
  5640. }
  5641. StmtResult Sema::ActOnOpenMPForSimdDirective(
  5642. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  5643. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  5644. if (!AStmt)
  5645. return StmtError();
  5646. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  5647. OMPLoopDirective::HelperExprs B;
  5648. // In presence of clause 'collapse' or 'ordered' with number of loops, it will
  5649. // define the nested loops number.
  5650. unsigned NestedLoopCount =
  5651. checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
  5652. getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
  5653. VarsWithImplicitDSA, B);
  5654. if (NestedLoopCount == 0)
  5655. return StmtError();
  5656. assert((CurContext->isDependentContext() || B.builtAll()) &&
  5657. "omp for simd loop exprs were not built");
  5658. if (!CurContext->isDependentContext()) {
  5659. // Finalize the clauses that need pre-built expressions for CodeGen.
  5660. for (OMPClause *C : Clauses) {
  5661. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  5662. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  5663. B.NumIterations, *this, CurScope,
  5664. DSAStack))
  5665. return StmtError();
  5666. }
  5667. }
  5668. if (checkSimdlenSafelenSpecified(*this, Clauses))
  5669. return StmtError();
  5670. setFunctionHasBranchProtectedScope();
  5671. return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
  5672. Clauses, AStmt, B);
  5673. }
  5674. StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
  5675. Stmt *AStmt,
  5676. SourceLocation StartLoc,
  5677. SourceLocation EndLoc) {
  5678. if (!AStmt)
  5679. return StmtError();
  5680. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  5681. auto BaseStmt = AStmt;
  5682. while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
  5683. BaseStmt = CS->getCapturedStmt();
  5684. if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
  5685. auto S = C->children();
  5686. if (S.begin() == S.end())
  5687. return StmtError();
  5688. // All associated statements must be '#pragma omp section' except for
  5689. // the first one.
  5690. for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
  5691. if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
  5692. if (SectionStmt)
  5693. Diag(SectionStmt->getBeginLoc(),
  5694. diag::err_omp_sections_substmt_not_section);
  5695. return StmtError();
  5696. }
  5697. cast<OMPSectionDirective>(SectionStmt)
  5698. ->setHasCancel(DSAStack->isCancelRegion());
  5699. }
  5700. } else {
  5701. Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
  5702. return StmtError();
  5703. }
  5704. setFunctionHasBranchProtectedScope();
  5705. return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
  5706. DSAStack->isCancelRegion());
  5707. }
  5708. StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
  5709. SourceLocation StartLoc,
  5710. SourceLocation EndLoc) {
  5711. if (!AStmt)
  5712. return StmtError();
  5713. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  5714. setFunctionHasBranchProtectedScope();
  5715. DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
  5716. return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
  5717. DSAStack->isCancelRegion());
  5718. }
  5719. StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
  5720. Stmt *AStmt,
  5721. SourceLocation StartLoc,
  5722. SourceLocation EndLoc) {
  5723. if (!AStmt)
  5724. return StmtError();
  5725. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  5726. setFunctionHasBranchProtectedScope();
  5727. // OpenMP [2.7.3, single Construct, Restrictions]
  5728. // The copyprivate clause must not be used with the nowait clause.
  5729. const OMPClause *Nowait = nullptr;
  5730. const OMPClause *Copyprivate = nullptr;
  5731. for (const OMPClause *Clause : Clauses) {
  5732. if (Clause->getClauseKind() == OMPC_nowait)
  5733. Nowait = Clause;
  5734. else if (Clause->getClauseKind() == OMPC_copyprivate)
  5735. Copyprivate = Clause;
  5736. if (Copyprivate && Nowait) {
  5737. Diag(Copyprivate->getBeginLoc(),
  5738. diag::err_omp_single_copyprivate_with_nowait);
  5739. Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
  5740. return StmtError();
  5741. }
  5742. }
  5743. return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
  5744. }
  5745. StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
  5746. SourceLocation StartLoc,
  5747. SourceLocation EndLoc) {
  5748. if (!AStmt)
  5749. return StmtError();
  5750. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  5751. setFunctionHasBranchProtectedScope();
  5752. return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
  5753. }
  5754. StmtResult Sema::ActOnOpenMPCriticalDirective(
  5755. const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
  5756. Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
  5757. if (!AStmt)
  5758. return StmtError();
  5759. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  5760. bool ErrorFound = false;
  5761. llvm::APSInt Hint;
  5762. SourceLocation HintLoc;
  5763. bool DependentHint = false;
  5764. for (const OMPClause *C : Clauses) {
  5765. if (C->getClauseKind() == OMPC_hint) {
  5766. if (!DirName.getName()) {
  5767. Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
  5768. ErrorFound = true;
  5769. }
  5770. Expr *E = cast<OMPHintClause>(C)->getHint();
  5771. if (E->isTypeDependent() || E->isValueDependent() ||
  5772. E->isInstantiationDependent()) {
  5773. DependentHint = true;
  5774. } else {
  5775. Hint = E->EvaluateKnownConstInt(Context);
  5776. HintLoc = C->getBeginLoc();
  5777. }
  5778. }
  5779. }
  5780. if (ErrorFound)
  5781. return StmtError();
  5782. const auto Pair = DSAStack->getCriticalWithHint(DirName);
  5783. if (Pair.first && DirName.getName() && !DependentHint) {
  5784. if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
  5785. Diag(StartLoc, diag::err_omp_critical_with_hint);
  5786. if (HintLoc.isValid())
  5787. Diag(HintLoc, diag::note_omp_critical_hint_here)
  5788. << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
  5789. else
  5790. Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
  5791. if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
  5792. Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
  5793. << 1
  5794. << C->getHint()->EvaluateKnownConstInt(Context).toString(
  5795. /*Radix=*/10, /*Signed=*/false);
  5796. } else {
  5797. Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
  5798. }
  5799. }
  5800. }
  5801. setFunctionHasBranchProtectedScope();
  5802. auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
  5803. Clauses, AStmt);
  5804. if (!Pair.first && DirName.getName() && !DependentHint)
  5805. DSAStack->addCriticalWithHint(Dir, Hint);
  5806. return Dir;
  5807. }
  5808. StmtResult Sema::ActOnOpenMPParallelForDirective(
  5809. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  5810. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  5811. if (!AStmt)
  5812. return StmtError();
  5813. auto *CS = cast<CapturedStmt>(AStmt);
  5814. // 1.2.2 OpenMP Language Terminology
  5815. // Structured block - An executable statement with a single entry at the
  5816. // top and a single exit at the bottom.
  5817. // The point of exit cannot be a branch out of the structured block.
  5818. // longjmp() and throw() must not violate the entry/exit criteria.
  5819. CS->getCapturedDecl()->setNothrow();
  5820. OMPLoopDirective::HelperExprs B;
  5821. // In presence of clause 'collapse' or 'ordered' with number of loops, it will
  5822. // define the nested loops number.
  5823. unsigned NestedLoopCount =
  5824. checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
  5825. getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
  5826. VarsWithImplicitDSA, B);
  5827. if (NestedLoopCount == 0)
  5828. return StmtError();
  5829. assert((CurContext->isDependentContext() || B.builtAll()) &&
  5830. "omp parallel for loop exprs were not built");
  5831. if (!CurContext->isDependentContext()) {
  5832. // Finalize the clauses that need pre-built expressions for CodeGen.
  5833. for (OMPClause *C : Clauses) {
  5834. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  5835. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  5836. B.NumIterations, *this, CurScope,
  5837. DSAStack))
  5838. return StmtError();
  5839. }
  5840. }
  5841. setFunctionHasBranchProtectedScope();
  5842. return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
  5843. NestedLoopCount, Clauses, AStmt, B,
  5844. DSAStack->isCancelRegion());
  5845. }
  5846. StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
  5847. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  5848. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  5849. if (!AStmt)
  5850. return StmtError();
  5851. auto *CS = cast<CapturedStmt>(AStmt);
  5852. // 1.2.2 OpenMP Language Terminology
  5853. // Structured block - An executable statement with a single entry at the
  5854. // top and a single exit at the bottom.
  5855. // The point of exit cannot be a branch out of the structured block.
  5856. // longjmp() and throw() must not violate the entry/exit criteria.
  5857. CS->getCapturedDecl()->setNothrow();
  5858. OMPLoopDirective::HelperExprs B;
  5859. // In presence of clause 'collapse' or 'ordered' with number of loops, it will
  5860. // define the nested loops number.
  5861. unsigned NestedLoopCount =
  5862. checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
  5863. getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
  5864. VarsWithImplicitDSA, B);
  5865. if (NestedLoopCount == 0)
  5866. return StmtError();
  5867. if (!CurContext->isDependentContext()) {
  5868. // Finalize the clauses that need pre-built expressions for CodeGen.
  5869. for (OMPClause *C : Clauses) {
  5870. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  5871. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  5872. B.NumIterations, *this, CurScope,
  5873. DSAStack))
  5874. return StmtError();
  5875. }
  5876. }
  5877. if (checkSimdlenSafelenSpecified(*this, Clauses))
  5878. return StmtError();
  5879. setFunctionHasBranchProtectedScope();
  5880. return OMPParallelForSimdDirective::Create(
  5881. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
  5882. }
  5883. StmtResult
  5884. Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
  5885. Stmt *AStmt, SourceLocation StartLoc,
  5886. SourceLocation EndLoc) {
  5887. if (!AStmt)
  5888. return StmtError();
  5889. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  5890. auto BaseStmt = AStmt;
  5891. while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
  5892. BaseStmt = CS->getCapturedStmt();
  5893. if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
  5894. auto S = C->children();
  5895. if (S.begin() == S.end())
  5896. return StmtError();
  5897. // All associated statements must be '#pragma omp section' except for
  5898. // the first one.
  5899. for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
  5900. if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
  5901. if (SectionStmt)
  5902. Diag(SectionStmt->getBeginLoc(),
  5903. diag::err_omp_parallel_sections_substmt_not_section);
  5904. return StmtError();
  5905. }
  5906. cast<OMPSectionDirective>(SectionStmt)
  5907. ->setHasCancel(DSAStack->isCancelRegion());
  5908. }
  5909. } else {
  5910. Diag(AStmt->getBeginLoc(),
  5911. diag::err_omp_parallel_sections_not_compound_stmt);
  5912. return StmtError();
  5913. }
  5914. setFunctionHasBranchProtectedScope();
  5915. return OMPParallelSectionsDirective::Create(
  5916. Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
  5917. }
  5918. StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
  5919. Stmt *AStmt, SourceLocation StartLoc,
  5920. SourceLocation EndLoc) {
  5921. if (!AStmt)
  5922. return StmtError();
  5923. auto *CS = cast<CapturedStmt>(AStmt);
  5924. // 1.2.2 OpenMP Language Terminology
  5925. // Structured block - An executable statement with a single entry at the
  5926. // top and a single exit at the bottom.
  5927. // The point of exit cannot be a branch out of the structured block.
  5928. // longjmp() and throw() must not violate the entry/exit criteria.
  5929. CS->getCapturedDecl()->setNothrow();
  5930. setFunctionHasBranchProtectedScope();
  5931. return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
  5932. DSAStack->isCancelRegion());
  5933. }
  5934. StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
  5935. SourceLocation EndLoc) {
  5936. return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
  5937. }
  5938. StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
  5939. SourceLocation EndLoc) {
  5940. return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
  5941. }
  5942. StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
  5943. SourceLocation EndLoc) {
  5944. return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
  5945. }
  5946. StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
  5947. Stmt *AStmt,
  5948. SourceLocation StartLoc,
  5949. SourceLocation EndLoc) {
  5950. if (!AStmt)
  5951. return StmtError();
  5952. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  5953. setFunctionHasBranchProtectedScope();
  5954. return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
  5955. AStmt,
  5956. DSAStack->getTaskgroupReductionRef());
  5957. }
  5958. StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
  5959. SourceLocation StartLoc,
  5960. SourceLocation EndLoc) {
  5961. assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
  5962. return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
  5963. }
  5964. StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
  5965. Stmt *AStmt,
  5966. SourceLocation StartLoc,
  5967. SourceLocation EndLoc) {
  5968. const OMPClause *DependFound = nullptr;
  5969. const OMPClause *DependSourceClause = nullptr;
  5970. const OMPClause *DependSinkClause = nullptr;
  5971. bool ErrorFound = false;
  5972. const OMPThreadsClause *TC = nullptr;
  5973. const OMPSIMDClause *SC = nullptr;
  5974. for (const OMPClause *C : Clauses) {
  5975. if (auto *DC = dyn_cast<OMPDependClause>(C)) {
  5976. DependFound = C;
  5977. if (DC->getDependencyKind() == OMPC_DEPEND_source) {
  5978. if (DependSourceClause) {
  5979. Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
  5980. << getOpenMPDirectiveName(OMPD_ordered)
  5981. << getOpenMPClauseName(OMPC_depend) << 2;
  5982. ErrorFound = true;
  5983. } else {
  5984. DependSourceClause = C;
  5985. }
  5986. if (DependSinkClause) {
  5987. Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
  5988. << 0;
  5989. ErrorFound = true;
  5990. }
  5991. } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
  5992. if (DependSourceClause) {
  5993. Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
  5994. << 1;
  5995. ErrorFound = true;
  5996. }
  5997. DependSinkClause = C;
  5998. }
  5999. } else if (C->getClauseKind() == OMPC_threads) {
  6000. TC = cast<OMPThreadsClause>(C);
  6001. } else if (C->getClauseKind() == OMPC_simd) {
  6002. SC = cast<OMPSIMDClause>(C);
  6003. }
  6004. }
  6005. if (!ErrorFound && !SC &&
  6006. isOpenMPSimdDirective(DSAStack->getParentDirective())) {
  6007. // OpenMP [2.8.1,simd Construct, Restrictions]
  6008. // An ordered construct with the simd clause is the only OpenMP construct
  6009. // that can appear in the simd region.
  6010. Diag(StartLoc, diag::err_omp_prohibited_region_simd);
  6011. ErrorFound = true;
  6012. } else if (DependFound && (TC || SC)) {
  6013. Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
  6014. << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
  6015. ErrorFound = true;
  6016. } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
  6017. Diag(DependFound->getBeginLoc(),
  6018. diag::err_omp_ordered_directive_without_param);
  6019. ErrorFound = true;
  6020. } else if (TC || Clauses.empty()) {
  6021. if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
  6022. SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
  6023. Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
  6024. << (TC != nullptr);
  6025. Diag(Param->getBeginLoc(), diag::note_omp_ordered_param);
  6026. ErrorFound = true;
  6027. }
  6028. }
  6029. if ((!AStmt && !DependFound) || ErrorFound)
  6030. return StmtError();
  6031. if (AStmt) {
  6032. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  6033. setFunctionHasBranchProtectedScope();
  6034. }
  6035. return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
  6036. }
  6037. namespace {
  6038. /// Helper class for checking expression in 'omp atomic [update]'
  6039. /// construct.
  6040. class OpenMPAtomicUpdateChecker {
  6041. /// Error results for atomic update expressions.
  6042. enum ExprAnalysisErrorCode {
  6043. /// A statement is not an expression statement.
  6044. NotAnExpression,
  6045. /// Expression is not builtin binary or unary operation.
  6046. NotABinaryOrUnaryExpression,
  6047. /// Unary operation is not post-/pre- increment/decrement operation.
  6048. NotAnUnaryIncDecExpression,
  6049. /// An expression is not of scalar type.
  6050. NotAScalarType,
  6051. /// A binary operation is not an assignment operation.
  6052. NotAnAssignmentOp,
  6053. /// RHS part of the binary operation is not a binary expression.
  6054. NotABinaryExpression,
  6055. /// RHS part is not additive/multiplicative/shift/biwise binary
  6056. /// expression.
  6057. NotABinaryOperator,
  6058. /// RHS binary operation does not have reference to the updated LHS
  6059. /// part.
  6060. NotAnUpdateExpression,
  6061. /// No errors is found.
  6062. NoError
  6063. };
  6064. /// Reference to Sema.
  6065. Sema &SemaRef;
  6066. /// A location for note diagnostics (when error is found).
  6067. SourceLocation NoteLoc;
  6068. /// 'x' lvalue part of the source atomic expression.
  6069. Expr *X;
  6070. /// 'expr' rvalue part of the source atomic expression.
  6071. Expr *E;
  6072. /// Helper expression of the form
  6073. /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
  6074. /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
  6075. Expr *UpdateExpr;
  6076. /// Is 'x' a LHS in a RHS part of full update expression. It is
  6077. /// important for non-associative operations.
  6078. bool IsXLHSInRHSPart;
  6079. BinaryOperatorKind Op;
  6080. SourceLocation OpLoc;
  6081. /// true if the source expression is a postfix unary operation, false
  6082. /// if it is a prefix unary operation.
  6083. bool IsPostfixUpdate;
  6084. public:
  6085. OpenMPAtomicUpdateChecker(Sema &SemaRef)
  6086. : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
  6087. IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
  6088. /// Check specified statement that it is suitable for 'atomic update'
  6089. /// constructs and extract 'x', 'expr' and Operation from the original
  6090. /// expression. If DiagId and NoteId == 0, then only check is performed
  6091. /// without error notification.
  6092. /// \param DiagId Diagnostic which should be emitted if error is found.
  6093. /// \param NoteId Diagnostic note for the main error message.
  6094. /// \return true if statement is not an update expression, false otherwise.
  6095. bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
  6096. /// Return the 'x' lvalue part of the source atomic expression.
  6097. Expr *getX() const { return X; }
  6098. /// Return the 'expr' rvalue part of the source atomic expression.
  6099. Expr *getExpr() const { return E; }
  6100. /// Return the update expression used in calculation of the updated
  6101. /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
  6102. /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
  6103. Expr *getUpdateExpr() const { return UpdateExpr; }
  6104. /// Return true if 'x' is LHS in RHS part of full update expression,
  6105. /// false otherwise.
  6106. bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
  6107. /// true if the source expression is a postfix unary operation, false
  6108. /// if it is a prefix unary operation.
  6109. bool isPostfixUpdate() const { return IsPostfixUpdate; }
  6110. private:
  6111. bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
  6112. unsigned NoteId = 0);
  6113. };
  6114. } // namespace
  6115. bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
  6116. BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
  6117. ExprAnalysisErrorCode ErrorFound = NoError;
  6118. SourceLocation ErrorLoc, NoteLoc;
  6119. SourceRange ErrorRange, NoteRange;
  6120. // Allowed constructs are:
  6121. // x = x binop expr;
  6122. // x = expr binop x;
  6123. if (AtomicBinOp->getOpcode() == BO_Assign) {
  6124. X = AtomicBinOp->getLHS();
  6125. if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
  6126. AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
  6127. if (AtomicInnerBinOp->isMultiplicativeOp() ||
  6128. AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
  6129. AtomicInnerBinOp->isBitwiseOp()) {
  6130. Op = AtomicInnerBinOp->getOpcode();
  6131. OpLoc = AtomicInnerBinOp->getOperatorLoc();
  6132. Expr *LHS = AtomicInnerBinOp->getLHS();
  6133. Expr *RHS = AtomicInnerBinOp->getRHS();
  6134. llvm::FoldingSetNodeID XId, LHSId, RHSId;
  6135. X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
  6136. /*Canonical=*/true);
  6137. LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
  6138. /*Canonical=*/true);
  6139. RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
  6140. /*Canonical=*/true);
  6141. if (XId == LHSId) {
  6142. E = RHS;
  6143. IsXLHSInRHSPart = true;
  6144. } else if (XId == RHSId) {
  6145. E = LHS;
  6146. IsXLHSInRHSPart = false;
  6147. } else {
  6148. ErrorLoc = AtomicInnerBinOp->getExprLoc();
  6149. ErrorRange = AtomicInnerBinOp->getSourceRange();
  6150. NoteLoc = X->getExprLoc();
  6151. NoteRange = X->getSourceRange();
  6152. ErrorFound = NotAnUpdateExpression;
  6153. }
  6154. } else {
  6155. ErrorLoc = AtomicInnerBinOp->getExprLoc();
  6156. ErrorRange = AtomicInnerBinOp->getSourceRange();
  6157. NoteLoc = AtomicInnerBinOp->getOperatorLoc();
  6158. NoteRange = SourceRange(NoteLoc, NoteLoc);
  6159. ErrorFound = NotABinaryOperator;
  6160. }
  6161. } else {
  6162. NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
  6163. NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
  6164. ErrorFound = NotABinaryExpression;
  6165. }
  6166. } else {
  6167. ErrorLoc = AtomicBinOp->getExprLoc();
  6168. ErrorRange = AtomicBinOp->getSourceRange();
  6169. NoteLoc = AtomicBinOp->getOperatorLoc();
  6170. NoteRange = SourceRange(NoteLoc, NoteLoc);
  6171. ErrorFound = NotAnAssignmentOp;
  6172. }
  6173. if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
  6174. SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
  6175. SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
  6176. return true;
  6177. }
  6178. if (SemaRef.CurContext->isDependentContext())
  6179. E = X = UpdateExpr = nullptr;
  6180. return ErrorFound != NoError;
  6181. }
  6182. bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
  6183. unsigned NoteId) {
  6184. ExprAnalysisErrorCode ErrorFound = NoError;
  6185. SourceLocation ErrorLoc, NoteLoc;
  6186. SourceRange ErrorRange, NoteRange;
  6187. // Allowed constructs are:
  6188. // x++;
  6189. // x--;
  6190. // ++x;
  6191. // --x;
  6192. // x binop= expr;
  6193. // x = x binop expr;
  6194. // x = expr binop x;
  6195. if (auto *AtomicBody = dyn_cast<Expr>(S)) {
  6196. AtomicBody = AtomicBody->IgnoreParenImpCasts();
  6197. if (AtomicBody->getType()->isScalarType() ||
  6198. AtomicBody->isInstantiationDependent()) {
  6199. if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
  6200. AtomicBody->IgnoreParenImpCasts())) {
  6201. // Check for Compound Assignment Operation
  6202. Op = BinaryOperator::getOpForCompoundAssignment(
  6203. AtomicCompAssignOp->getOpcode());
  6204. OpLoc = AtomicCompAssignOp->getOperatorLoc();
  6205. E = AtomicCompAssignOp->getRHS();
  6206. X = AtomicCompAssignOp->getLHS()->IgnoreParens();
  6207. IsXLHSInRHSPart = true;
  6208. } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
  6209. AtomicBody->IgnoreParenImpCasts())) {
  6210. // Check for Binary Operation
  6211. if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
  6212. return true;
  6213. } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
  6214. AtomicBody->IgnoreParenImpCasts())) {
  6215. // Check for Unary Operation
  6216. if (AtomicUnaryOp->isIncrementDecrementOp()) {
  6217. IsPostfixUpdate = AtomicUnaryOp->isPostfix();
  6218. Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
  6219. OpLoc = AtomicUnaryOp->getOperatorLoc();
  6220. X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
  6221. E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
  6222. IsXLHSInRHSPart = true;
  6223. } else {
  6224. ErrorFound = NotAnUnaryIncDecExpression;
  6225. ErrorLoc = AtomicUnaryOp->getExprLoc();
  6226. ErrorRange = AtomicUnaryOp->getSourceRange();
  6227. NoteLoc = AtomicUnaryOp->getOperatorLoc();
  6228. NoteRange = SourceRange(NoteLoc, NoteLoc);
  6229. }
  6230. } else if (!AtomicBody->isInstantiationDependent()) {
  6231. ErrorFound = NotABinaryOrUnaryExpression;
  6232. NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
  6233. NoteRange = ErrorRange = AtomicBody->getSourceRange();
  6234. }
  6235. } else {
  6236. ErrorFound = NotAScalarType;
  6237. NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
  6238. NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
  6239. }
  6240. } else {
  6241. ErrorFound = NotAnExpression;
  6242. NoteLoc = ErrorLoc = S->getBeginLoc();
  6243. NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
  6244. }
  6245. if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
  6246. SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
  6247. SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
  6248. return true;
  6249. }
  6250. if (SemaRef.CurContext->isDependentContext())
  6251. E = X = UpdateExpr = nullptr;
  6252. if (ErrorFound == NoError && E && X) {
  6253. // Build an update expression of form 'OpaqueValueExpr(x) binop
  6254. // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
  6255. // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
  6256. auto *OVEX = new (SemaRef.getASTContext())
  6257. OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
  6258. auto *OVEExpr = new (SemaRef.getASTContext())
  6259. OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
  6260. ExprResult Update =
  6261. SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
  6262. IsXLHSInRHSPart ? OVEExpr : OVEX);
  6263. if (Update.isInvalid())
  6264. return true;
  6265. Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
  6266. Sema::AA_Casting);
  6267. if (Update.isInvalid())
  6268. return true;
  6269. UpdateExpr = Update.get();
  6270. }
  6271. return ErrorFound != NoError;
  6272. }
  6273. StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
  6274. Stmt *AStmt,
  6275. SourceLocation StartLoc,
  6276. SourceLocation EndLoc) {
  6277. if (!AStmt)
  6278. return StmtError();
  6279. auto *CS = cast<CapturedStmt>(AStmt);
  6280. // 1.2.2 OpenMP Language Terminology
  6281. // Structured block - An executable statement with a single entry at the
  6282. // top and a single exit at the bottom.
  6283. // The point of exit cannot be a branch out of the structured block.
  6284. // longjmp() and throw() must not violate the entry/exit criteria.
  6285. OpenMPClauseKind AtomicKind = OMPC_unknown;
  6286. SourceLocation AtomicKindLoc;
  6287. for (const OMPClause *C : Clauses) {
  6288. if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
  6289. C->getClauseKind() == OMPC_update ||
  6290. C->getClauseKind() == OMPC_capture) {
  6291. if (AtomicKind != OMPC_unknown) {
  6292. Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
  6293. << SourceRange(C->getBeginLoc(), C->getEndLoc());
  6294. Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
  6295. << getOpenMPClauseName(AtomicKind);
  6296. } else {
  6297. AtomicKind = C->getClauseKind();
  6298. AtomicKindLoc = C->getBeginLoc();
  6299. }
  6300. }
  6301. }
  6302. Stmt *Body = CS->getCapturedStmt();
  6303. if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
  6304. Body = EWC->getSubExpr();
  6305. Expr *X = nullptr;
  6306. Expr *V = nullptr;
  6307. Expr *E = nullptr;
  6308. Expr *UE = nullptr;
  6309. bool IsXLHSInRHSPart = false;
  6310. bool IsPostfixUpdate = false;
  6311. // OpenMP [2.12.6, atomic Construct]
  6312. // In the next expressions:
  6313. // * x and v (as applicable) are both l-value expressions with scalar type.
  6314. // * During the execution of an atomic region, multiple syntactic
  6315. // occurrences of x must designate the same storage location.
  6316. // * Neither of v and expr (as applicable) may access the storage location
  6317. // designated by x.
  6318. // * Neither of x and expr (as applicable) may access the storage location
  6319. // designated by v.
  6320. // * expr is an expression with scalar type.
  6321. // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
  6322. // * binop, binop=, ++, and -- are not overloaded operators.
  6323. // * The expression x binop expr must be numerically equivalent to x binop
  6324. // (expr). This requirement is satisfied if the operators in expr have
  6325. // precedence greater than binop, or by using parentheses around expr or
  6326. // subexpressions of expr.
  6327. // * The expression expr binop x must be numerically equivalent to (expr)
  6328. // binop x. This requirement is satisfied if the operators in expr have
  6329. // precedence equal to or greater than binop, or by using parentheses around
  6330. // expr or subexpressions of expr.
  6331. // * For forms that allow multiple occurrences of x, the number of times
  6332. // that x is evaluated is unspecified.
  6333. if (AtomicKind == OMPC_read) {
  6334. enum {
  6335. NotAnExpression,
  6336. NotAnAssignmentOp,
  6337. NotAScalarType,
  6338. NotAnLValue,
  6339. NoError
  6340. } ErrorFound = NoError;
  6341. SourceLocation ErrorLoc, NoteLoc;
  6342. SourceRange ErrorRange, NoteRange;
  6343. // If clause is read:
  6344. // v = x;
  6345. if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
  6346. const auto *AtomicBinOp =
  6347. dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
  6348. if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
  6349. X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
  6350. V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
  6351. if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
  6352. (V->isInstantiationDependent() || V->getType()->isScalarType())) {
  6353. if (!X->isLValue() || !V->isLValue()) {
  6354. const Expr *NotLValueExpr = X->isLValue() ? V : X;
  6355. ErrorFound = NotAnLValue;
  6356. ErrorLoc = AtomicBinOp->getExprLoc();
  6357. ErrorRange = AtomicBinOp->getSourceRange();
  6358. NoteLoc = NotLValueExpr->getExprLoc();
  6359. NoteRange = NotLValueExpr->getSourceRange();
  6360. }
  6361. } else if (!X->isInstantiationDependent() ||
  6362. !V->isInstantiationDependent()) {
  6363. const Expr *NotScalarExpr =
  6364. (X->isInstantiationDependent() || X->getType()->isScalarType())
  6365. ? V
  6366. : X;
  6367. ErrorFound = NotAScalarType;
  6368. ErrorLoc = AtomicBinOp->getExprLoc();
  6369. ErrorRange = AtomicBinOp->getSourceRange();
  6370. NoteLoc = NotScalarExpr->getExprLoc();
  6371. NoteRange = NotScalarExpr->getSourceRange();
  6372. }
  6373. } else if (!AtomicBody->isInstantiationDependent()) {
  6374. ErrorFound = NotAnAssignmentOp;
  6375. ErrorLoc = AtomicBody->getExprLoc();
  6376. ErrorRange = AtomicBody->getSourceRange();
  6377. NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
  6378. : AtomicBody->getExprLoc();
  6379. NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
  6380. : AtomicBody->getSourceRange();
  6381. }
  6382. } else {
  6383. ErrorFound = NotAnExpression;
  6384. NoteLoc = ErrorLoc = Body->getBeginLoc();
  6385. NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
  6386. }
  6387. if (ErrorFound != NoError) {
  6388. Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
  6389. << ErrorRange;
  6390. Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
  6391. << NoteRange;
  6392. return StmtError();
  6393. }
  6394. if (CurContext->isDependentContext())
  6395. V = X = nullptr;
  6396. } else if (AtomicKind == OMPC_write) {
  6397. enum {
  6398. NotAnExpression,
  6399. NotAnAssignmentOp,
  6400. NotAScalarType,
  6401. NotAnLValue,
  6402. NoError
  6403. } ErrorFound = NoError;
  6404. SourceLocation ErrorLoc, NoteLoc;
  6405. SourceRange ErrorRange, NoteRange;
  6406. // If clause is write:
  6407. // x = expr;
  6408. if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
  6409. const auto *AtomicBinOp =
  6410. dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
  6411. if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
  6412. X = AtomicBinOp->getLHS();
  6413. E = AtomicBinOp->getRHS();
  6414. if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
  6415. (E->isInstantiationDependent() || E->getType()->isScalarType())) {
  6416. if (!X->isLValue()) {
  6417. ErrorFound = NotAnLValue;
  6418. ErrorLoc = AtomicBinOp->getExprLoc();
  6419. ErrorRange = AtomicBinOp->getSourceRange();
  6420. NoteLoc = X->getExprLoc();
  6421. NoteRange = X->getSourceRange();
  6422. }
  6423. } else if (!X->isInstantiationDependent() ||
  6424. !E->isInstantiationDependent()) {
  6425. const Expr *NotScalarExpr =
  6426. (X->isInstantiationDependent() || X->getType()->isScalarType())
  6427. ? E
  6428. : X;
  6429. ErrorFound = NotAScalarType;
  6430. ErrorLoc = AtomicBinOp->getExprLoc();
  6431. ErrorRange = AtomicBinOp->getSourceRange();
  6432. NoteLoc = NotScalarExpr->getExprLoc();
  6433. NoteRange = NotScalarExpr->getSourceRange();
  6434. }
  6435. } else if (!AtomicBody->isInstantiationDependent()) {
  6436. ErrorFound = NotAnAssignmentOp;
  6437. ErrorLoc = AtomicBody->getExprLoc();
  6438. ErrorRange = AtomicBody->getSourceRange();
  6439. NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
  6440. : AtomicBody->getExprLoc();
  6441. NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
  6442. : AtomicBody->getSourceRange();
  6443. }
  6444. } else {
  6445. ErrorFound = NotAnExpression;
  6446. NoteLoc = ErrorLoc = Body->getBeginLoc();
  6447. NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
  6448. }
  6449. if (ErrorFound != NoError) {
  6450. Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
  6451. << ErrorRange;
  6452. Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
  6453. << NoteRange;
  6454. return StmtError();
  6455. }
  6456. if (CurContext->isDependentContext())
  6457. E = X = nullptr;
  6458. } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
  6459. // If clause is update:
  6460. // x++;
  6461. // x--;
  6462. // ++x;
  6463. // --x;
  6464. // x binop= expr;
  6465. // x = x binop expr;
  6466. // x = expr binop x;
  6467. OpenMPAtomicUpdateChecker Checker(*this);
  6468. if (Checker.checkStatement(
  6469. Body, (AtomicKind == OMPC_update)
  6470. ? diag::err_omp_atomic_update_not_expression_statement
  6471. : diag::err_omp_atomic_not_expression_statement,
  6472. diag::note_omp_atomic_update))
  6473. return StmtError();
  6474. if (!CurContext->isDependentContext()) {
  6475. E = Checker.getExpr();
  6476. X = Checker.getX();
  6477. UE = Checker.getUpdateExpr();
  6478. IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
  6479. }
  6480. } else if (AtomicKind == OMPC_capture) {
  6481. enum {
  6482. NotAnAssignmentOp,
  6483. NotACompoundStatement,
  6484. NotTwoSubstatements,
  6485. NotASpecificExpression,
  6486. NoError
  6487. } ErrorFound = NoError;
  6488. SourceLocation ErrorLoc, NoteLoc;
  6489. SourceRange ErrorRange, NoteRange;
  6490. if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
  6491. // If clause is a capture:
  6492. // v = x++;
  6493. // v = x--;
  6494. // v = ++x;
  6495. // v = --x;
  6496. // v = x binop= expr;
  6497. // v = x = x binop expr;
  6498. // v = x = expr binop x;
  6499. const auto *AtomicBinOp =
  6500. dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
  6501. if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
  6502. V = AtomicBinOp->getLHS();
  6503. Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
  6504. OpenMPAtomicUpdateChecker Checker(*this);
  6505. if (Checker.checkStatement(
  6506. Body, diag::err_omp_atomic_capture_not_expression_statement,
  6507. diag::note_omp_atomic_update))
  6508. return StmtError();
  6509. E = Checker.getExpr();
  6510. X = Checker.getX();
  6511. UE = Checker.getUpdateExpr();
  6512. IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
  6513. IsPostfixUpdate = Checker.isPostfixUpdate();
  6514. } else if (!AtomicBody->isInstantiationDependent()) {
  6515. ErrorLoc = AtomicBody->getExprLoc();
  6516. ErrorRange = AtomicBody->getSourceRange();
  6517. NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
  6518. : AtomicBody->getExprLoc();
  6519. NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
  6520. : AtomicBody->getSourceRange();
  6521. ErrorFound = NotAnAssignmentOp;
  6522. }
  6523. if (ErrorFound != NoError) {
  6524. Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
  6525. << ErrorRange;
  6526. Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
  6527. return StmtError();
  6528. }
  6529. if (CurContext->isDependentContext())
  6530. UE = V = E = X = nullptr;
  6531. } else {
  6532. // If clause is a capture:
  6533. // { v = x; x = expr; }
  6534. // { v = x; x++; }
  6535. // { v = x; x--; }
  6536. // { v = x; ++x; }
  6537. // { v = x; --x; }
  6538. // { v = x; x binop= expr; }
  6539. // { v = x; x = x binop expr; }
  6540. // { v = x; x = expr binop x; }
  6541. // { x++; v = x; }
  6542. // { x--; v = x; }
  6543. // { ++x; v = x; }
  6544. // { --x; v = x; }
  6545. // { x binop= expr; v = x; }
  6546. // { x = x binop expr; v = x; }
  6547. // { x = expr binop x; v = x; }
  6548. if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
  6549. // Check that this is { expr1; expr2; }
  6550. if (CS->size() == 2) {
  6551. Stmt *First = CS->body_front();
  6552. Stmt *Second = CS->body_back();
  6553. if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
  6554. First = EWC->getSubExpr()->IgnoreParenImpCasts();
  6555. if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
  6556. Second = EWC->getSubExpr()->IgnoreParenImpCasts();
  6557. // Need to find what subexpression is 'v' and what is 'x'.
  6558. OpenMPAtomicUpdateChecker Checker(*this);
  6559. bool IsUpdateExprFound = !Checker.checkStatement(Second);
  6560. BinaryOperator *BinOp = nullptr;
  6561. if (IsUpdateExprFound) {
  6562. BinOp = dyn_cast<BinaryOperator>(First);
  6563. IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
  6564. }
  6565. if (IsUpdateExprFound && !CurContext->isDependentContext()) {
  6566. // { v = x; x++; }
  6567. // { v = x; x--; }
  6568. // { v = x; ++x; }
  6569. // { v = x; --x; }
  6570. // { v = x; x binop= expr; }
  6571. // { v = x; x = x binop expr; }
  6572. // { v = x; x = expr binop x; }
  6573. // Check that the first expression has form v = x.
  6574. Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
  6575. llvm::FoldingSetNodeID XId, PossibleXId;
  6576. Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
  6577. PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
  6578. IsUpdateExprFound = XId == PossibleXId;
  6579. if (IsUpdateExprFound) {
  6580. V = BinOp->getLHS();
  6581. X = Checker.getX();
  6582. E = Checker.getExpr();
  6583. UE = Checker.getUpdateExpr();
  6584. IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
  6585. IsPostfixUpdate = true;
  6586. }
  6587. }
  6588. if (!IsUpdateExprFound) {
  6589. IsUpdateExprFound = !Checker.checkStatement(First);
  6590. BinOp = nullptr;
  6591. if (IsUpdateExprFound) {
  6592. BinOp = dyn_cast<BinaryOperator>(Second);
  6593. IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
  6594. }
  6595. if (IsUpdateExprFound && !CurContext->isDependentContext()) {
  6596. // { x++; v = x; }
  6597. // { x--; v = x; }
  6598. // { ++x; v = x; }
  6599. // { --x; v = x; }
  6600. // { x binop= expr; v = x; }
  6601. // { x = x binop expr; v = x; }
  6602. // { x = expr binop x; v = x; }
  6603. // Check that the second expression has form v = x.
  6604. Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
  6605. llvm::FoldingSetNodeID XId, PossibleXId;
  6606. Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
  6607. PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
  6608. IsUpdateExprFound = XId == PossibleXId;
  6609. if (IsUpdateExprFound) {
  6610. V = BinOp->getLHS();
  6611. X = Checker.getX();
  6612. E = Checker.getExpr();
  6613. UE = Checker.getUpdateExpr();
  6614. IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
  6615. IsPostfixUpdate = false;
  6616. }
  6617. }
  6618. }
  6619. if (!IsUpdateExprFound) {
  6620. // { v = x; x = expr; }
  6621. auto *FirstExpr = dyn_cast<Expr>(First);
  6622. auto *SecondExpr = dyn_cast<Expr>(Second);
  6623. if (!FirstExpr || !SecondExpr ||
  6624. !(FirstExpr->isInstantiationDependent() ||
  6625. SecondExpr->isInstantiationDependent())) {
  6626. auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
  6627. if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
  6628. ErrorFound = NotAnAssignmentOp;
  6629. NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
  6630. : First->getBeginLoc();
  6631. NoteRange = ErrorRange = FirstBinOp
  6632. ? FirstBinOp->getSourceRange()
  6633. : SourceRange(ErrorLoc, ErrorLoc);
  6634. } else {
  6635. auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
  6636. if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
  6637. ErrorFound = NotAnAssignmentOp;
  6638. NoteLoc = ErrorLoc = SecondBinOp
  6639. ? SecondBinOp->getOperatorLoc()
  6640. : Second->getBeginLoc();
  6641. NoteRange = ErrorRange =
  6642. SecondBinOp ? SecondBinOp->getSourceRange()
  6643. : SourceRange(ErrorLoc, ErrorLoc);
  6644. } else {
  6645. Expr *PossibleXRHSInFirst =
  6646. FirstBinOp->getRHS()->IgnoreParenImpCasts();
  6647. Expr *PossibleXLHSInSecond =
  6648. SecondBinOp->getLHS()->IgnoreParenImpCasts();
  6649. llvm::FoldingSetNodeID X1Id, X2Id;
  6650. PossibleXRHSInFirst->Profile(X1Id, Context,
  6651. /*Canonical=*/true);
  6652. PossibleXLHSInSecond->Profile(X2Id, Context,
  6653. /*Canonical=*/true);
  6654. IsUpdateExprFound = X1Id == X2Id;
  6655. if (IsUpdateExprFound) {
  6656. V = FirstBinOp->getLHS();
  6657. X = SecondBinOp->getLHS();
  6658. E = SecondBinOp->getRHS();
  6659. UE = nullptr;
  6660. IsXLHSInRHSPart = false;
  6661. IsPostfixUpdate = true;
  6662. } else {
  6663. ErrorFound = NotASpecificExpression;
  6664. ErrorLoc = FirstBinOp->getExprLoc();
  6665. ErrorRange = FirstBinOp->getSourceRange();
  6666. NoteLoc = SecondBinOp->getLHS()->getExprLoc();
  6667. NoteRange = SecondBinOp->getRHS()->getSourceRange();
  6668. }
  6669. }
  6670. }
  6671. }
  6672. }
  6673. } else {
  6674. NoteLoc = ErrorLoc = Body->getBeginLoc();
  6675. NoteRange = ErrorRange =
  6676. SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
  6677. ErrorFound = NotTwoSubstatements;
  6678. }
  6679. } else {
  6680. NoteLoc = ErrorLoc = Body->getBeginLoc();
  6681. NoteRange = ErrorRange =
  6682. SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
  6683. ErrorFound = NotACompoundStatement;
  6684. }
  6685. if (ErrorFound != NoError) {
  6686. Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
  6687. << ErrorRange;
  6688. Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
  6689. return StmtError();
  6690. }
  6691. if (CurContext->isDependentContext())
  6692. UE = V = E = X = nullptr;
  6693. }
  6694. }
  6695. setFunctionHasBranchProtectedScope();
  6696. return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
  6697. X, V, E, UE, IsXLHSInRHSPart,
  6698. IsPostfixUpdate);
  6699. }
  6700. StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
  6701. Stmt *AStmt,
  6702. SourceLocation StartLoc,
  6703. SourceLocation EndLoc) {
  6704. if (!AStmt)
  6705. return StmtError();
  6706. auto *CS = cast<CapturedStmt>(AStmt);
  6707. // 1.2.2 OpenMP Language Terminology
  6708. // Structured block - An executable statement with a single entry at the
  6709. // top and a single exit at the bottom.
  6710. // The point of exit cannot be a branch out of the structured block.
  6711. // longjmp() and throw() must not violate the entry/exit criteria.
  6712. CS->getCapturedDecl()->setNothrow();
  6713. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
  6714. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  6715. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  6716. // 1.2.2 OpenMP Language Terminology
  6717. // Structured block - An executable statement with a single entry at the
  6718. // top and a single exit at the bottom.
  6719. // The point of exit cannot be a branch out of the structured block.
  6720. // longjmp() and throw() must not violate the entry/exit criteria.
  6721. CS->getCapturedDecl()->setNothrow();
  6722. }
  6723. // OpenMP [2.16, Nesting of Regions]
  6724. // If specified, a teams construct must be contained within a target
  6725. // construct. That target construct must contain no statements or directives
  6726. // outside of the teams construct.
  6727. if (DSAStack->hasInnerTeamsRegion()) {
  6728. const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
  6729. bool OMPTeamsFound = true;
  6730. if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
  6731. auto I = CS->body_begin();
  6732. while (I != CS->body_end()) {
  6733. const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
  6734. if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) ||
  6735. OMPTeamsFound) {
  6736. OMPTeamsFound = false;
  6737. break;
  6738. }
  6739. ++I;
  6740. }
  6741. assert(I != CS->body_end() && "Not found statement");
  6742. S = *I;
  6743. } else {
  6744. const auto *OED = dyn_cast<OMPExecutableDirective>(S);
  6745. OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
  6746. }
  6747. if (!OMPTeamsFound) {
  6748. Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
  6749. Diag(DSAStack->getInnerTeamsRegionLoc(),
  6750. diag::note_omp_nested_teams_construct_here);
  6751. Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
  6752. << isa<OMPExecutableDirective>(S);
  6753. return StmtError();
  6754. }
  6755. }
  6756. setFunctionHasBranchProtectedScope();
  6757. return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
  6758. }
  6759. StmtResult
  6760. Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
  6761. Stmt *AStmt, SourceLocation StartLoc,
  6762. SourceLocation EndLoc) {
  6763. if (!AStmt)
  6764. return StmtError();
  6765. auto *CS = cast<CapturedStmt>(AStmt);
  6766. // 1.2.2 OpenMP Language Terminology
  6767. // Structured block - An executable statement with a single entry at the
  6768. // top and a single exit at the bottom.
  6769. // The point of exit cannot be a branch out of the structured block.
  6770. // longjmp() and throw() must not violate the entry/exit criteria.
  6771. CS->getCapturedDecl()->setNothrow();
  6772. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
  6773. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  6774. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  6775. // 1.2.2 OpenMP Language Terminology
  6776. // Structured block - An executable statement with a single entry at the
  6777. // top and a single exit at the bottom.
  6778. // The point of exit cannot be a branch out of the structured block.
  6779. // longjmp() and throw() must not violate the entry/exit criteria.
  6780. CS->getCapturedDecl()->setNothrow();
  6781. }
  6782. setFunctionHasBranchProtectedScope();
  6783. return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
  6784. AStmt);
  6785. }
  6786. StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
  6787. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  6788. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  6789. if (!AStmt)
  6790. return StmtError();
  6791. auto *CS = cast<CapturedStmt>(AStmt);
  6792. // 1.2.2 OpenMP Language Terminology
  6793. // Structured block - An executable statement with a single entry at the
  6794. // top and a single exit at the bottom.
  6795. // The point of exit cannot be a branch out of the structured block.
  6796. // longjmp() and throw() must not violate the entry/exit criteria.
  6797. CS->getCapturedDecl()->setNothrow();
  6798. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
  6799. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  6800. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  6801. // 1.2.2 OpenMP Language Terminology
  6802. // Structured block - An executable statement with a single entry at the
  6803. // top and a single exit at the bottom.
  6804. // The point of exit cannot be a branch out of the structured block.
  6805. // longjmp() and throw() must not violate the entry/exit criteria.
  6806. CS->getCapturedDecl()->setNothrow();
  6807. }
  6808. OMPLoopDirective::HelperExprs B;
  6809. // In presence of clause 'collapse' or 'ordered' with number of loops, it will
  6810. // define the nested loops number.
  6811. unsigned NestedLoopCount =
  6812. checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
  6813. getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
  6814. VarsWithImplicitDSA, B);
  6815. if (NestedLoopCount == 0)
  6816. return StmtError();
  6817. assert((CurContext->isDependentContext() || B.builtAll()) &&
  6818. "omp target parallel for loop exprs were not built");
  6819. if (!CurContext->isDependentContext()) {
  6820. // Finalize the clauses that need pre-built expressions for CodeGen.
  6821. for (OMPClause *C : Clauses) {
  6822. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  6823. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  6824. B.NumIterations, *this, CurScope,
  6825. DSAStack))
  6826. return StmtError();
  6827. }
  6828. }
  6829. setFunctionHasBranchProtectedScope();
  6830. return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
  6831. NestedLoopCount, Clauses, AStmt,
  6832. B, DSAStack->isCancelRegion());
  6833. }
  6834. /// Check for existence of a map clause in the list of clauses.
  6835. static bool hasClauses(ArrayRef<OMPClause *> Clauses,
  6836. const OpenMPClauseKind K) {
  6837. return llvm::any_of(
  6838. Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
  6839. }
  6840. template <typename... Params>
  6841. static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
  6842. const Params... ClauseTypes) {
  6843. return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
  6844. }
  6845. StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
  6846. Stmt *AStmt,
  6847. SourceLocation StartLoc,
  6848. SourceLocation EndLoc) {
  6849. if (!AStmt)
  6850. return StmtError();
  6851. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  6852. // OpenMP [2.10.1, Restrictions, p. 97]
  6853. // At least one map clause must appear on the directive.
  6854. if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
  6855. Diag(StartLoc, diag::err_omp_no_clause_for_directive)
  6856. << "'map' or 'use_device_ptr'"
  6857. << getOpenMPDirectiveName(OMPD_target_data);
  6858. return StmtError();
  6859. }
  6860. setFunctionHasBranchProtectedScope();
  6861. return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
  6862. AStmt);
  6863. }
  6864. StmtResult
  6865. Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
  6866. SourceLocation StartLoc,
  6867. SourceLocation EndLoc, Stmt *AStmt) {
  6868. if (!AStmt)
  6869. return StmtError();
  6870. auto *CS = cast<CapturedStmt>(AStmt);
  6871. // 1.2.2 OpenMP Language Terminology
  6872. // Structured block - An executable statement with a single entry at the
  6873. // top and a single exit at the bottom.
  6874. // The point of exit cannot be a branch out of the structured block.
  6875. // longjmp() and throw() must not violate the entry/exit criteria.
  6876. CS->getCapturedDecl()->setNothrow();
  6877. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
  6878. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  6879. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  6880. // 1.2.2 OpenMP Language Terminology
  6881. // Structured block - An executable statement with a single entry at the
  6882. // top and a single exit at the bottom.
  6883. // The point of exit cannot be a branch out of the structured block.
  6884. // longjmp() and throw() must not violate the entry/exit criteria.
  6885. CS->getCapturedDecl()->setNothrow();
  6886. }
  6887. // OpenMP [2.10.2, Restrictions, p. 99]
  6888. // At least one map clause must appear on the directive.
  6889. if (!hasClauses(Clauses, OMPC_map)) {
  6890. Diag(StartLoc, diag::err_omp_no_clause_for_directive)
  6891. << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
  6892. return StmtError();
  6893. }
  6894. return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
  6895. AStmt);
  6896. }
  6897. StmtResult
  6898. Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
  6899. SourceLocation StartLoc,
  6900. SourceLocation EndLoc, Stmt *AStmt) {
  6901. if (!AStmt)
  6902. return StmtError();
  6903. auto *CS = cast<CapturedStmt>(AStmt);
  6904. // 1.2.2 OpenMP Language Terminology
  6905. // Structured block - An executable statement with a single entry at the
  6906. // top and a single exit at the bottom.
  6907. // The point of exit cannot be a branch out of the structured block.
  6908. // longjmp() and throw() must not violate the entry/exit criteria.
  6909. CS->getCapturedDecl()->setNothrow();
  6910. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
  6911. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  6912. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  6913. // 1.2.2 OpenMP Language Terminology
  6914. // Structured block - An executable statement with a single entry at the
  6915. // top and a single exit at the bottom.
  6916. // The point of exit cannot be a branch out of the structured block.
  6917. // longjmp() and throw() must not violate the entry/exit criteria.
  6918. CS->getCapturedDecl()->setNothrow();
  6919. }
  6920. // OpenMP [2.10.3, Restrictions, p. 102]
  6921. // At least one map clause must appear on the directive.
  6922. if (!hasClauses(Clauses, OMPC_map)) {
  6923. Diag(StartLoc, diag::err_omp_no_clause_for_directive)
  6924. << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
  6925. return StmtError();
  6926. }
  6927. return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
  6928. AStmt);
  6929. }
  6930. StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
  6931. SourceLocation StartLoc,
  6932. SourceLocation EndLoc,
  6933. Stmt *AStmt) {
  6934. if (!AStmt)
  6935. return StmtError();
  6936. auto *CS = cast<CapturedStmt>(AStmt);
  6937. // 1.2.2 OpenMP Language Terminology
  6938. // Structured block - An executable statement with a single entry at the
  6939. // top and a single exit at the bottom.
  6940. // The point of exit cannot be a branch out of the structured block.
  6941. // longjmp() and throw() must not violate the entry/exit criteria.
  6942. CS->getCapturedDecl()->setNothrow();
  6943. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
  6944. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  6945. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  6946. // 1.2.2 OpenMP Language Terminology
  6947. // Structured block - An executable statement with a single entry at the
  6948. // top and a single exit at the bottom.
  6949. // The point of exit cannot be a branch out of the structured block.
  6950. // longjmp() and throw() must not violate the entry/exit criteria.
  6951. CS->getCapturedDecl()->setNothrow();
  6952. }
  6953. if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
  6954. Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
  6955. return StmtError();
  6956. }
  6957. return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
  6958. AStmt);
  6959. }
  6960. StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
  6961. Stmt *AStmt, SourceLocation StartLoc,
  6962. SourceLocation EndLoc) {
  6963. if (!AStmt)
  6964. return StmtError();
  6965. auto *CS = cast<CapturedStmt>(AStmt);
  6966. // 1.2.2 OpenMP Language Terminology
  6967. // Structured block - An executable statement with a single entry at the
  6968. // top and a single exit at the bottom.
  6969. // The point of exit cannot be a branch out of the structured block.
  6970. // longjmp() and throw() must not violate the entry/exit criteria.
  6971. CS->getCapturedDecl()->setNothrow();
  6972. setFunctionHasBranchProtectedScope();
  6973. DSAStack->setParentTeamsRegionLoc(StartLoc);
  6974. return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
  6975. }
  6976. StmtResult
  6977. Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
  6978. SourceLocation EndLoc,
  6979. OpenMPDirectiveKind CancelRegion) {
  6980. if (DSAStack->isParentNowaitRegion()) {
  6981. Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
  6982. return StmtError();
  6983. }
  6984. if (DSAStack->isParentOrderedRegion()) {
  6985. Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
  6986. return StmtError();
  6987. }
  6988. return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
  6989. CancelRegion);
  6990. }
  6991. StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
  6992. SourceLocation StartLoc,
  6993. SourceLocation EndLoc,
  6994. OpenMPDirectiveKind CancelRegion) {
  6995. if (DSAStack->isParentNowaitRegion()) {
  6996. Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
  6997. return StmtError();
  6998. }
  6999. if (DSAStack->isParentOrderedRegion()) {
  7000. Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
  7001. return StmtError();
  7002. }
  7003. DSAStack->setParentCancelRegion(/*Cancel=*/true);
  7004. return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
  7005. CancelRegion);
  7006. }
  7007. static bool checkGrainsizeNumTasksClauses(Sema &S,
  7008. ArrayRef<OMPClause *> Clauses) {
  7009. const OMPClause *PrevClause = nullptr;
  7010. bool ErrorFound = false;
  7011. for (const OMPClause *C : Clauses) {
  7012. if (C->getClauseKind() == OMPC_grainsize ||
  7013. C->getClauseKind() == OMPC_num_tasks) {
  7014. if (!PrevClause)
  7015. PrevClause = C;
  7016. else if (PrevClause->getClauseKind() != C->getClauseKind()) {
  7017. S.Diag(C->getBeginLoc(),
  7018. diag::err_omp_grainsize_num_tasks_mutually_exclusive)
  7019. << getOpenMPClauseName(C->getClauseKind())
  7020. << getOpenMPClauseName(PrevClause->getClauseKind());
  7021. S.Diag(PrevClause->getBeginLoc(),
  7022. diag::note_omp_previous_grainsize_num_tasks)
  7023. << getOpenMPClauseName(PrevClause->getClauseKind());
  7024. ErrorFound = true;
  7025. }
  7026. }
  7027. }
  7028. return ErrorFound;
  7029. }
  7030. static bool checkReductionClauseWithNogroup(Sema &S,
  7031. ArrayRef<OMPClause *> Clauses) {
  7032. const OMPClause *ReductionClause = nullptr;
  7033. const OMPClause *NogroupClause = nullptr;
  7034. for (const OMPClause *C : Clauses) {
  7035. if (C->getClauseKind() == OMPC_reduction) {
  7036. ReductionClause = C;
  7037. if (NogroupClause)
  7038. break;
  7039. continue;
  7040. }
  7041. if (C->getClauseKind() == OMPC_nogroup) {
  7042. NogroupClause = C;
  7043. if (ReductionClause)
  7044. break;
  7045. continue;
  7046. }
  7047. }
  7048. if (ReductionClause && NogroupClause) {
  7049. S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
  7050. << SourceRange(NogroupClause->getBeginLoc(),
  7051. NogroupClause->getEndLoc());
  7052. return true;
  7053. }
  7054. return false;
  7055. }
  7056. StmtResult Sema::ActOnOpenMPTaskLoopDirective(
  7057. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7058. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7059. if (!AStmt)
  7060. return StmtError();
  7061. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  7062. OMPLoopDirective::HelperExprs B;
  7063. // In presence of clause 'collapse' or 'ordered' with number of loops, it will
  7064. // define the nested loops number.
  7065. unsigned NestedLoopCount =
  7066. checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
  7067. /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
  7068. VarsWithImplicitDSA, B);
  7069. if (NestedLoopCount == 0)
  7070. return StmtError();
  7071. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7072. "omp for loop exprs were not built");
  7073. // OpenMP, [2.9.2 taskloop Construct, Restrictions]
  7074. // The grainsize clause and num_tasks clause are mutually exclusive and may
  7075. // not appear on the same taskloop directive.
  7076. if (checkGrainsizeNumTasksClauses(*this, Clauses))
  7077. return StmtError();
  7078. // OpenMP, [2.9.2 taskloop Construct, Restrictions]
  7079. // If a reduction clause is present on the taskloop directive, the nogroup
  7080. // clause must not be specified.
  7081. if (checkReductionClauseWithNogroup(*this, Clauses))
  7082. return StmtError();
  7083. setFunctionHasBranchProtectedScope();
  7084. return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
  7085. NestedLoopCount, Clauses, AStmt, B);
  7086. }
  7087. StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
  7088. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7089. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7090. if (!AStmt)
  7091. return StmtError();
  7092. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  7093. OMPLoopDirective::HelperExprs B;
  7094. // In presence of clause 'collapse' or 'ordered' with number of loops, it will
  7095. // define the nested loops number.
  7096. unsigned NestedLoopCount =
  7097. checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
  7098. /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
  7099. VarsWithImplicitDSA, B);
  7100. if (NestedLoopCount == 0)
  7101. return StmtError();
  7102. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7103. "omp for loop exprs were not built");
  7104. if (!CurContext->isDependentContext()) {
  7105. // Finalize the clauses that need pre-built expressions for CodeGen.
  7106. for (OMPClause *C : Clauses) {
  7107. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  7108. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  7109. B.NumIterations, *this, CurScope,
  7110. DSAStack))
  7111. return StmtError();
  7112. }
  7113. }
  7114. // OpenMP, [2.9.2 taskloop Construct, Restrictions]
  7115. // The grainsize clause and num_tasks clause are mutually exclusive and may
  7116. // not appear on the same taskloop directive.
  7117. if (checkGrainsizeNumTasksClauses(*this, Clauses))
  7118. return StmtError();
  7119. // OpenMP, [2.9.2 taskloop Construct, Restrictions]
  7120. // If a reduction clause is present on the taskloop directive, the nogroup
  7121. // clause must not be specified.
  7122. if (checkReductionClauseWithNogroup(*this, Clauses))
  7123. return StmtError();
  7124. if (checkSimdlenSafelenSpecified(*this, Clauses))
  7125. return StmtError();
  7126. setFunctionHasBranchProtectedScope();
  7127. return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
  7128. NestedLoopCount, Clauses, AStmt, B);
  7129. }
  7130. StmtResult Sema::ActOnOpenMPDistributeDirective(
  7131. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7132. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7133. if (!AStmt)
  7134. return StmtError();
  7135. assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
  7136. OMPLoopDirective::HelperExprs B;
  7137. // In presence of clause 'collapse' with number of loops, it will
  7138. // define the nested loops number.
  7139. unsigned NestedLoopCount =
  7140. checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
  7141. nullptr /*ordered not a clause on distribute*/, AStmt,
  7142. *this, *DSAStack, VarsWithImplicitDSA, B);
  7143. if (NestedLoopCount == 0)
  7144. return StmtError();
  7145. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7146. "omp for loop exprs were not built");
  7147. setFunctionHasBranchProtectedScope();
  7148. return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
  7149. NestedLoopCount, Clauses, AStmt, B);
  7150. }
  7151. StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
  7152. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7153. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7154. if (!AStmt)
  7155. return StmtError();
  7156. auto *CS = cast<CapturedStmt>(AStmt);
  7157. // 1.2.2 OpenMP Language Terminology
  7158. // Structured block - An executable statement with a single entry at the
  7159. // top and a single exit at the bottom.
  7160. // The point of exit cannot be a branch out of the structured block.
  7161. // longjmp() and throw() must not violate the entry/exit criteria.
  7162. CS->getCapturedDecl()->setNothrow();
  7163. for (int ThisCaptureLevel =
  7164. getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
  7165. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7166. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7167. // 1.2.2 OpenMP Language Terminology
  7168. // Structured block - An executable statement with a single entry at the
  7169. // top and a single exit at the bottom.
  7170. // The point of exit cannot be a branch out of the structured block.
  7171. // longjmp() and throw() must not violate the entry/exit criteria.
  7172. CS->getCapturedDecl()->setNothrow();
  7173. }
  7174. OMPLoopDirective::HelperExprs B;
  7175. // In presence of clause 'collapse' with number of loops, it will
  7176. // define the nested loops number.
  7177. unsigned NestedLoopCount = checkOpenMPLoop(
  7178. OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
  7179. nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
  7180. VarsWithImplicitDSA, B);
  7181. if (NestedLoopCount == 0)
  7182. return StmtError();
  7183. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7184. "omp for loop exprs were not built");
  7185. setFunctionHasBranchProtectedScope();
  7186. return OMPDistributeParallelForDirective::Create(
  7187. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
  7188. DSAStack->isCancelRegion());
  7189. }
  7190. StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
  7191. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7192. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7193. if (!AStmt)
  7194. return StmtError();
  7195. auto *CS = cast<CapturedStmt>(AStmt);
  7196. // 1.2.2 OpenMP Language Terminology
  7197. // Structured block - An executable statement with a single entry at the
  7198. // top and a single exit at the bottom.
  7199. // The point of exit cannot be a branch out of the structured block.
  7200. // longjmp() and throw() must not violate the entry/exit criteria.
  7201. CS->getCapturedDecl()->setNothrow();
  7202. for (int ThisCaptureLevel =
  7203. getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
  7204. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7205. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7206. // 1.2.2 OpenMP Language Terminology
  7207. // Structured block - An executable statement with a single entry at the
  7208. // top and a single exit at the bottom.
  7209. // The point of exit cannot be a branch out of the structured block.
  7210. // longjmp() and throw() must not violate the entry/exit criteria.
  7211. CS->getCapturedDecl()->setNothrow();
  7212. }
  7213. OMPLoopDirective::HelperExprs B;
  7214. // In presence of clause 'collapse' with number of loops, it will
  7215. // define the nested loops number.
  7216. unsigned NestedLoopCount = checkOpenMPLoop(
  7217. OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
  7218. nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
  7219. VarsWithImplicitDSA, B);
  7220. if (NestedLoopCount == 0)
  7221. return StmtError();
  7222. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7223. "omp for loop exprs were not built");
  7224. if (!CurContext->isDependentContext()) {
  7225. // Finalize the clauses that need pre-built expressions for CodeGen.
  7226. for (OMPClause *C : Clauses) {
  7227. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  7228. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  7229. B.NumIterations, *this, CurScope,
  7230. DSAStack))
  7231. return StmtError();
  7232. }
  7233. }
  7234. if (checkSimdlenSafelenSpecified(*this, Clauses))
  7235. return StmtError();
  7236. setFunctionHasBranchProtectedScope();
  7237. return OMPDistributeParallelForSimdDirective::Create(
  7238. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
  7239. }
  7240. StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
  7241. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7242. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7243. if (!AStmt)
  7244. return StmtError();
  7245. auto *CS = cast<CapturedStmt>(AStmt);
  7246. // 1.2.2 OpenMP Language Terminology
  7247. // Structured block - An executable statement with a single entry at the
  7248. // top and a single exit at the bottom.
  7249. // The point of exit cannot be a branch out of the structured block.
  7250. // longjmp() and throw() must not violate the entry/exit criteria.
  7251. CS->getCapturedDecl()->setNothrow();
  7252. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
  7253. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7254. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7255. // 1.2.2 OpenMP Language Terminology
  7256. // Structured block - An executable statement with a single entry at the
  7257. // top and a single exit at the bottom.
  7258. // The point of exit cannot be a branch out of the structured block.
  7259. // longjmp() and throw() must not violate the entry/exit criteria.
  7260. CS->getCapturedDecl()->setNothrow();
  7261. }
  7262. OMPLoopDirective::HelperExprs B;
  7263. // In presence of clause 'collapse' with number of loops, it will
  7264. // define the nested loops number.
  7265. unsigned NestedLoopCount =
  7266. checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
  7267. nullptr /*ordered not a clause on distribute*/, CS, *this,
  7268. *DSAStack, VarsWithImplicitDSA, B);
  7269. if (NestedLoopCount == 0)
  7270. return StmtError();
  7271. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7272. "omp for loop exprs were not built");
  7273. if (!CurContext->isDependentContext()) {
  7274. // Finalize the clauses that need pre-built expressions for CodeGen.
  7275. for (OMPClause *C : Clauses) {
  7276. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  7277. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  7278. B.NumIterations, *this, CurScope,
  7279. DSAStack))
  7280. return StmtError();
  7281. }
  7282. }
  7283. if (checkSimdlenSafelenSpecified(*this, Clauses))
  7284. return StmtError();
  7285. setFunctionHasBranchProtectedScope();
  7286. return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
  7287. NestedLoopCount, Clauses, AStmt, B);
  7288. }
  7289. StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
  7290. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7291. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7292. if (!AStmt)
  7293. return StmtError();
  7294. auto *CS = cast<CapturedStmt>(AStmt);
  7295. // 1.2.2 OpenMP Language Terminology
  7296. // Structured block - An executable statement with a single entry at the
  7297. // top and a single exit at the bottom.
  7298. // The point of exit cannot be a branch out of the structured block.
  7299. // longjmp() and throw() must not violate the entry/exit criteria.
  7300. CS->getCapturedDecl()->setNothrow();
  7301. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
  7302. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7303. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7304. // 1.2.2 OpenMP Language Terminology
  7305. // Structured block - An executable statement with a single entry at the
  7306. // top and a single exit at the bottom.
  7307. // The point of exit cannot be a branch out of the structured block.
  7308. // longjmp() and throw() must not violate the entry/exit criteria.
  7309. CS->getCapturedDecl()->setNothrow();
  7310. }
  7311. OMPLoopDirective::HelperExprs B;
  7312. // In presence of clause 'collapse' or 'ordered' with number of loops, it will
  7313. // define the nested loops number.
  7314. unsigned NestedLoopCount = checkOpenMPLoop(
  7315. OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
  7316. getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
  7317. VarsWithImplicitDSA, B);
  7318. if (NestedLoopCount == 0)
  7319. return StmtError();
  7320. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7321. "omp target parallel for simd loop exprs were not built");
  7322. if (!CurContext->isDependentContext()) {
  7323. // Finalize the clauses that need pre-built expressions for CodeGen.
  7324. for (OMPClause *C : Clauses) {
  7325. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  7326. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  7327. B.NumIterations, *this, CurScope,
  7328. DSAStack))
  7329. return StmtError();
  7330. }
  7331. }
  7332. if (checkSimdlenSafelenSpecified(*this, Clauses))
  7333. return StmtError();
  7334. setFunctionHasBranchProtectedScope();
  7335. return OMPTargetParallelForSimdDirective::Create(
  7336. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
  7337. }
  7338. StmtResult Sema::ActOnOpenMPTargetSimdDirective(
  7339. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7340. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7341. if (!AStmt)
  7342. return StmtError();
  7343. auto *CS = cast<CapturedStmt>(AStmt);
  7344. // 1.2.2 OpenMP Language Terminology
  7345. // Structured block - An executable statement with a single entry at the
  7346. // top and a single exit at the bottom.
  7347. // The point of exit cannot be a branch out of the structured block.
  7348. // longjmp() and throw() must not violate the entry/exit criteria.
  7349. CS->getCapturedDecl()->setNothrow();
  7350. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
  7351. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7352. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7353. // 1.2.2 OpenMP Language Terminology
  7354. // Structured block - An executable statement with a single entry at the
  7355. // top and a single exit at the bottom.
  7356. // The point of exit cannot be a branch out of the structured block.
  7357. // longjmp() and throw() must not violate the entry/exit criteria.
  7358. CS->getCapturedDecl()->setNothrow();
  7359. }
  7360. OMPLoopDirective::HelperExprs B;
  7361. // In presence of clause 'collapse' with number of loops, it will define the
  7362. // nested loops number.
  7363. unsigned NestedLoopCount =
  7364. checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
  7365. getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
  7366. VarsWithImplicitDSA, B);
  7367. if (NestedLoopCount == 0)
  7368. return StmtError();
  7369. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7370. "omp target simd loop exprs were not built");
  7371. if (!CurContext->isDependentContext()) {
  7372. // Finalize the clauses that need pre-built expressions for CodeGen.
  7373. for (OMPClause *C : Clauses) {
  7374. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  7375. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  7376. B.NumIterations, *this, CurScope,
  7377. DSAStack))
  7378. return StmtError();
  7379. }
  7380. }
  7381. if (checkSimdlenSafelenSpecified(*this, Clauses))
  7382. return StmtError();
  7383. setFunctionHasBranchProtectedScope();
  7384. return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
  7385. NestedLoopCount, Clauses, AStmt, B);
  7386. }
  7387. StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
  7388. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7389. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7390. if (!AStmt)
  7391. return StmtError();
  7392. auto *CS = cast<CapturedStmt>(AStmt);
  7393. // 1.2.2 OpenMP Language Terminology
  7394. // Structured block - An executable statement with a single entry at the
  7395. // top and a single exit at the bottom.
  7396. // The point of exit cannot be a branch out of the structured block.
  7397. // longjmp() and throw() must not violate the entry/exit criteria.
  7398. CS->getCapturedDecl()->setNothrow();
  7399. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
  7400. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7401. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7402. // 1.2.2 OpenMP Language Terminology
  7403. // Structured block - An executable statement with a single entry at the
  7404. // top and a single exit at the bottom.
  7405. // The point of exit cannot be a branch out of the structured block.
  7406. // longjmp() and throw() must not violate the entry/exit criteria.
  7407. CS->getCapturedDecl()->setNothrow();
  7408. }
  7409. OMPLoopDirective::HelperExprs B;
  7410. // In presence of clause 'collapse' with number of loops, it will
  7411. // define the nested loops number.
  7412. unsigned NestedLoopCount =
  7413. checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
  7414. nullptr /*ordered not a clause on distribute*/, CS, *this,
  7415. *DSAStack, VarsWithImplicitDSA, B);
  7416. if (NestedLoopCount == 0)
  7417. return StmtError();
  7418. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7419. "omp teams distribute loop exprs were not built");
  7420. setFunctionHasBranchProtectedScope();
  7421. DSAStack->setParentTeamsRegionLoc(StartLoc);
  7422. return OMPTeamsDistributeDirective::Create(
  7423. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
  7424. }
  7425. StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
  7426. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7427. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7428. if (!AStmt)
  7429. return StmtError();
  7430. auto *CS = cast<CapturedStmt>(AStmt);
  7431. // 1.2.2 OpenMP Language Terminology
  7432. // Structured block - An executable statement with a single entry at the
  7433. // top and a single exit at the bottom.
  7434. // The point of exit cannot be a branch out of the structured block.
  7435. // longjmp() and throw() must not violate the entry/exit criteria.
  7436. CS->getCapturedDecl()->setNothrow();
  7437. for (int ThisCaptureLevel =
  7438. getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
  7439. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7440. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7441. // 1.2.2 OpenMP Language Terminology
  7442. // Structured block - An executable statement with a single entry at the
  7443. // top and a single exit at the bottom.
  7444. // The point of exit cannot be a branch out of the structured block.
  7445. // longjmp() and throw() must not violate the entry/exit criteria.
  7446. CS->getCapturedDecl()->setNothrow();
  7447. }
  7448. OMPLoopDirective::HelperExprs B;
  7449. // In presence of clause 'collapse' with number of loops, it will
  7450. // define the nested loops number.
  7451. unsigned NestedLoopCount = checkOpenMPLoop(
  7452. OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
  7453. nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
  7454. VarsWithImplicitDSA, B);
  7455. if (NestedLoopCount == 0)
  7456. return StmtError();
  7457. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7458. "omp teams distribute simd loop exprs were not built");
  7459. if (!CurContext->isDependentContext()) {
  7460. // Finalize the clauses that need pre-built expressions for CodeGen.
  7461. for (OMPClause *C : Clauses) {
  7462. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  7463. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  7464. B.NumIterations, *this, CurScope,
  7465. DSAStack))
  7466. return StmtError();
  7467. }
  7468. }
  7469. if (checkSimdlenSafelenSpecified(*this, Clauses))
  7470. return StmtError();
  7471. setFunctionHasBranchProtectedScope();
  7472. DSAStack->setParentTeamsRegionLoc(StartLoc);
  7473. return OMPTeamsDistributeSimdDirective::Create(
  7474. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
  7475. }
  7476. StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
  7477. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7478. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7479. if (!AStmt)
  7480. return StmtError();
  7481. auto *CS = cast<CapturedStmt>(AStmt);
  7482. // 1.2.2 OpenMP Language Terminology
  7483. // Structured block - An executable statement with a single entry at the
  7484. // top and a single exit at the bottom.
  7485. // The point of exit cannot be a branch out of the structured block.
  7486. // longjmp() and throw() must not violate the entry/exit criteria.
  7487. CS->getCapturedDecl()->setNothrow();
  7488. for (int ThisCaptureLevel =
  7489. getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
  7490. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7491. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7492. // 1.2.2 OpenMP Language Terminology
  7493. // Structured block - An executable statement with a single entry at the
  7494. // top and a single exit at the bottom.
  7495. // The point of exit cannot be a branch out of the structured block.
  7496. // longjmp() and throw() must not violate the entry/exit criteria.
  7497. CS->getCapturedDecl()->setNothrow();
  7498. }
  7499. OMPLoopDirective::HelperExprs B;
  7500. // In presence of clause 'collapse' with number of loops, it will
  7501. // define the nested loops number.
  7502. unsigned NestedLoopCount = checkOpenMPLoop(
  7503. OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
  7504. nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
  7505. VarsWithImplicitDSA, B);
  7506. if (NestedLoopCount == 0)
  7507. return StmtError();
  7508. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7509. "omp for loop exprs were not built");
  7510. if (!CurContext->isDependentContext()) {
  7511. // Finalize the clauses that need pre-built expressions for CodeGen.
  7512. for (OMPClause *C : Clauses) {
  7513. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  7514. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  7515. B.NumIterations, *this, CurScope,
  7516. DSAStack))
  7517. return StmtError();
  7518. }
  7519. }
  7520. if (checkSimdlenSafelenSpecified(*this, Clauses))
  7521. return StmtError();
  7522. setFunctionHasBranchProtectedScope();
  7523. DSAStack->setParentTeamsRegionLoc(StartLoc);
  7524. return OMPTeamsDistributeParallelForSimdDirective::Create(
  7525. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
  7526. }
  7527. StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
  7528. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7529. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7530. if (!AStmt)
  7531. return StmtError();
  7532. auto *CS = cast<CapturedStmt>(AStmt);
  7533. // 1.2.2 OpenMP Language Terminology
  7534. // Structured block - An executable statement with a single entry at the
  7535. // top and a single exit at the bottom.
  7536. // The point of exit cannot be a branch out of the structured block.
  7537. // longjmp() and throw() must not violate the entry/exit criteria.
  7538. CS->getCapturedDecl()->setNothrow();
  7539. for (int ThisCaptureLevel =
  7540. getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
  7541. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7542. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7543. // 1.2.2 OpenMP Language Terminology
  7544. // Structured block - An executable statement with a single entry at the
  7545. // top and a single exit at the bottom.
  7546. // The point of exit cannot be a branch out of the structured block.
  7547. // longjmp() and throw() must not violate the entry/exit criteria.
  7548. CS->getCapturedDecl()->setNothrow();
  7549. }
  7550. OMPLoopDirective::HelperExprs B;
  7551. // In presence of clause 'collapse' with number of loops, it will
  7552. // define the nested loops number.
  7553. unsigned NestedLoopCount = checkOpenMPLoop(
  7554. OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
  7555. nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
  7556. VarsWithImplicitDSA, B);
  7557. if (NestedLoopCount == 0)
  7558. return StmtError();
  7559. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7560. "omp for loop exprs were not built");
  7561. setFunctionHasBranchProtectedScope();
  7562. DSAStack->setParentTeamsRegionLoc(StartLoc);
  7563. return OMPTeamsDistributeParallelForDirective::Create(
  7564. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
  7565. DSAStack->isCancelRegion());
  7566. }
  7567. StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
  7568. Stmt *AStmt,
  7569. SourceLocation StartLoc,
  7570. SourceLocation EndLoc) {
  7571. if (!AStmt)
  7572. return StmtError();
  7573. auto *CS = cast<CapturedStmt>(AStmt);
  7574. // 1.2.2 OpenMP Language Terminology
  7575. // Structured block - An executable statement with a single entry at the
  7576. // top and a single exit at the bottom.
  7577. // The point of exit cannot be a branch out of the structured block.
  7578. // longjmp() and throw() must not violate the entry/exit criteria.
  7579. CS->getCapturedDecl()->setNothrow();
  7580. for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
  7581. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7582. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7583. // 1.2.2 OpenMP Language Terminology
  7584. // Structured block - An executable statement with a single entry at the
  7585. // top and a single exit at the bottom.
  7586. // The point of exit cannot be a branch out of the structured block.
  7587. // longjmp() and throw() must not violate the entry/exit criteria.
  7588. CS->getCapturedDecl()->setNothrow();
  7589. }
  7590. setFunctionHasBranchProtectedScope();
  7591. return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
  7592. AStmt);
  7593. }
  7594. StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
  7595. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7596. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7597. if (!AStmt)
  7598. return StmtError();
  7599. auto *CS = cast<CapturedStmt>(AStmt);
  7600. // 1.2.2 OpenMP Language Terminology
  7601. // Structured block - An executable statement with a single entry at the
  7602. // top and a single exit at the bottom.
  7603. // The point of exit cannot be a branch out of the structured block.
  7604. // longjmp() and throw() must not violate the entry/exit criteria.
  7605. CS->getCapturedDecl()->setNothrow();
  7606. for (int ThisCaptureLevel =
  7607. getOpenMPCaptureLevels(OMPD_target_teams_distribute);
  7608. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7609. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7610. // 1.2.2 OpenMP Language Terminology
  7611. // Structured block - An executable statement with a single entry at the
  7612. // top and a single exit at the bottom.
  7613. // The point of exit cannot be a branch out of the structured block.
  7614. // longjmp() and throw() must not violate the entry/exit criteria.
  7615. CS->getCapturedDecl()->setNothrow();
  7616. }
  7617. OMPLoopDirective::HelperExprs B;
  7618. // In presence of clause 'collapse' with number of loops, it will
  7619. // define the nested loops number.
  7620. unsigned NestedLoopCount = checkOpenMPLoop(
  7621. OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
  7622. nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
  7623. VarsWithImplicitDSA, B);
  7624. if (NestedLoopCount == 0)
  7625. return StmtError();
  7626. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7627. "omp target teams distribute loop exprs were not built");
  7628. setFunctionHasBranchProtectedScope();
  7629. return OMPTargetTeamsDistributeDirective::Create(
  7630. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
  7631. }
  7632. StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
  7633. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7634. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7635. if (!AStmt)
  7636. return StmtError();
  7637. auto *CS = cast<CapturedStmt>(AStmt);
  7638. // 1.2.2 OpenMP Language Terminology
  7639. // Structured block - An executable statement with a single entry at the
  7640. // top and a single exit at the bottom.
  7641. // The point of exit cannot be a branch out of the structured block.
  7642. // longjmp() and throw() must not violate the entry/exit criteria.
  7643. CS->getCapturedDecl()->setNothrow();
  7644. for (int ThisCaptureLevel =
  7645. getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
  7646. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7647. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7648. // 1.2.2 OpenMP Language Terminology
  7649. // Structured block - An executable statement with a single entry at the
  7650. // top and a single exit at the bottom.
  7651. // The point of exit cannot be a branch out of the structured block.
  7652. // longjmp() and throw() must not violate the entry/exit criteria.
  7653. CS->getCapturedDecl()->setNothrow();
  7654. }
  7655. OMPLoopDirective::HelperExprs B;
  7656. // In presence of clause 'collapse' with number of loops, it will
  7657. // define the nested loops number.
  7658. unsigned NestedLoopCount = checkOpenMPLoop(
  7659. OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
  7660. nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
  7661. VarsWithImplicitDSA, B);
  7662. if (NestedLoopCount == 0)
  7663. return StmtError();
  7664. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7665. "omp target teams distribute parallel for loop exprs were not built");
  7666. if (!CurContext->isDependentContext()) {
  7667. // Finalize the clauses that need pre-built expressions for CodeGen.
  7668. for (OMPClause *C : Clauses) {
  7669. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  7670. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  7671. B.NumIterations, *this, CurScope,
  7672. DSAStack))
  7673. return StmtError();
  7674. }
  7675. }
  7676. setFunctionHasBranchProtectedScope();
  7677. return OMPTargetTeamsDistributeParallelForDirective::Create(
  7678. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
  7679. DSAStack->isCancelRegion());
  7680. }
  7681. StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
  7682. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7683. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7684. if (!AStmt)
  7685. return StmtError();
  7686. auto *CS = cast<CapturedStmt>(AStmt);
  7687. // 1.2.2 OpenMP Language Terminology
  7688. // Structured block - An executable statement with a single entry at the
  7689. // top and a single exit at the bottom.
  7690. // The point of exit cannot be a branch out of the structured block.
  7691. // longjmp() and throw() must not violate the entry/exit criteria.
  7692. CS->getCapturedDecl()->setNothrow();
  7693. for (int ThisCaptureLevel = getOpenMPCaptureLevels(
  7694. OMPD_target_teams_distribute_parallel_for_simd);
  7695. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7696. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7697. // 1.2.2 OpenMP Language Terminology
  7698. // Structured block - An executable statement with a single entry at the
  7699. // top and a single exit at the bottom.
  7700. // The point of exit cannot be a branch out of the structured block.
  7701. // longjmp() and throw() must not violate the entry/exit criteria.
  7702. CS->getCapturedDecl()->setNothrow();
  7703. }
  7704. OMPLoopDirective::HelperExprs B;
  7705. // In presence of clause 'collapse' with number of loops, it will
  7706. // define the nested loops number.
  7707. unsigned NestedLoopCount =
  7708. checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
  7709. getCollapseNumberExpr(Clauses),
  7710. nullptr /*ordered not a clause on distribute*/, CS, *this,
  7711. *DSAStack, VarsWithImplicitDSA, B);
  7712. if (NestedLoopCount == 0)
  7713. return StmtError();
  7714. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7715. "omp target teams distribute parallel for simd loop exprs were not "
  7716. "built");
  7717. if (!CurContext->isDependentContext()) {
  7718. // Finalize the clauses that need pre-built expressions for CodeGen.
  7719. for (OMPClause *C : Clauses) {
  7720. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  7721. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  7722. B.NumIterations, *this, CurScope,
  7723. DSAStack))
  7724. return StmtError();
  7725. }
  7726. }
  7727. if (checkSimdlenSafelenSpecified(*this, Clauses))
  7728. return StmtError();
  7729. setFunctionHasBranchProtectedScope();
  7730. return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
  7731. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
  7732. }
  7733. StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
  7734. ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
  7735. SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
  7736. if (!AStmt)
  7737. return StmtError();
  7738. auto *CS = cast<CapturedStmt>(AStmt);
  7739. // 1.2.2 OpenMP Language Terminology
  7740. // Structured block - An executable statement with a single entry at the
  7741. // top and a single exit at the bottom.
  7742. // The point of exit cannot be a branch out of the structured block.
  7743. // longjmp() and throw() must not violate the entry/exit criteria.
  7744. CS->getCapturedDecl()->setNothrow();
  7745. for (int ThisCaptureLevel =
  7746. getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
  7747. ThisCaptureLevel > 1; --ThisCaptureLevel) {
  7748. CS = cast<CapturedStmt>(CS->getCapturedStmt());
  7749. // 1.2.2 OpenMP Language Terminology
  7750. // Structured block - An executable statement with a single entry at the
  7751. // top and a single exit at the bottom.
  7752. // The point of exit cannot be a branch out of the structured block.
  7753. // longjmp() and throw() must not violate the entry/exit criteria.
  7754. CS->getCapturedDecl()->setNothrow();
  7755. }
  7756. OMPLoopDirective::HelperExprs B;
  7757. // In presence of clause 'collapse' with number of loops, it will
  7758. // define the nested loops number.
  7759. unsigned NestedLoopCount = checkOpenMPLoop(
  7760. OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
  7761. nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
  7762. VarsWithImplicitDSA, B);
  7763. if (NestedLoopCount == 0)
  7764. return StmtError();
  7765. assert((CurContext->isDependentContext() || B.builtAll()) &&
  7766. "omp target teams distribute simd loop exprs were not built");
  7767. if (!CurContext->isDependentContext()) {
  7768. // Finalize the clauses that need pre-built expressions for CodeGen.
  7769. for (OMPClause *C : Clauses) {
  7770. if (auto *LC = dyn_cast<OMPLinearClause>(C))
  7771. if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
  7772. B.NumIterations, *this, CurScope,
  7773. DSAStack))
  7774. return StmtError();
  7775. }
  7776. }
  7777. if (checkSimdlenSafelenSpecified(*this, Clauses))
  7778. return StmtError();
  7779. setFunctionHasBranchProtectedScope();
  7780. return OMPTargetTeamsDistributeSimdDirective::Create(
  7781. Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
  7782. }
  7783. OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
  7784. SourceLocation StartLoc,
  7785. SourceLocation LParenLoc,
  7786. SourceLocation EndLoc) {
  7787. OMPClause *Res = nullptr;
  7788. switch (Kind) {
  7789. case OMPC_final:
  7790. Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
  7791. break;
  7792. case OMPC_num_threads:
  7793. Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
  7794. break;
  7795. case OMPC_safelen:
  7796. Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
  7797. break;
  7798. case OMPC_simdlen:
  7799. Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
  7800. break;
  7801. case OMPC_collapse:
  7802. Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
  7803. break;
  7804. case OMPC_ordered:
  7805. Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
  7806. break;
  7807. case OMPC_device:
  7808. Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
  7809. break;
  7810. case OMPC_num_teams:
  7811. Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
  7812. break;
  7813. case OMPC_thread_limit:
  7814. Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
  7815. break;
  7816. case OMPC_priority:
  7817. Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
  7818. break;
  7819. case OMPC_grainsize:
  7820. Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
  7821. break;
  7822. case OMPC_num_tasks:
  7823. Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
  7824. break;
  7825. case OMPC_hint:
  7826. Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
  7827. break;
  7828. case OMPC_if:
  7829. case OMPC_default:
  7830. case OMPC_proc_bind:
  7831. case OMPC_schedule:
  7832. case OMPC_private:
  7833. case OMPC_firstprivate:
  7834. case OMPC_lastprivate:
  7835. case OMPC_shared:
  7836. case OMPC_reduction:
  7837. case OMPC_task_reduction:
  7838. case OMPC_in_reduction:
  7839. case OMPC_linear:
  7840. case OMPC_aligned:
  7841. case OMPC_copyin:
  7842. case OMPC_copyprivate:
  7843. case OMPC_nowait:
  7844. case OMPC_untied:
  7845. case OMPC_mergeable:
  7846. case OMPC_threadprivate:
  7847. case OMPC_allocate:
  7848. case OMPC_flush:
  7849. case OMPC_read:
  7850. case OMPC_write:
  7851. case OMPC_update:
  7852. case OMPC_capture:
  7853. case OMPC_seq_cst:
  7854. case OMPC_depend:
  7855. case OMPC_threads:
  7856. case OMPC_simd:
  7857. case OMPC_map:
  7858. case OMPC_nogroup:
  7859. case OMPC_dist_schedule:
  7860. case OMPC_defaultmap:
  7861. case OMPC_unknown:
  7862. case OMPC_uniform:
  7863. case OMPC_to:
  7864. case OMPC_from:
  7865. case OMPC_use_device_ptr:
  7866. case OMPC_is_device_ptr:
  7867. case OMPC_unified_address:
  7868. case OMPC_unified_shared_memory:
  7869. case OMPC_reverse_offload:
  7870. case OMPC_dynamic_allocators:
  7871. case OMPC_atomic_default_mem_order:
  7872. llvm_unreachable("Clause is not allowed.");
  7873. }
  7874. return Res;
  7875. }
  7876. // An OpenMP directive such as 'target parallel' has two captured regions:
  7877. // for the 'target' and 'parallel' respectively. This function returns
  7878. // the region in which to capture expressions associated with a clause.
  7879. // A return value of OMPD_unknown signifies that the expression should not
  7880. // be captured.
  7881. static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
  7882. OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
  7883. OpenMPDirectiveKind NameModifier = OMPD_unknown) {
  7884. OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
  7885. switch (CKind) {
  7886. case OMPC_if:
  7887. switch (DKind) {
  7888. case OMPD_target_parallel:
  7889. case OMPD_target_parallel_for:
  7890. case OMPD_target_parallel_for_simd:
  7891. // If this clause applies to the nested 'parallel' region, capture within
  7892. // the 'target' region, otherwise do not capture.
  7893. if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
  7894. CaptureRegion = OMPD_target;
  7895. break;
  7896. case OMPD_target_teams_distribute_parallel_for:
  7897. case OMPD_target_teams_distribute_parallel_for_simd:
  7898. // If this clause applies to the nested 'parallel' region, capture within
  7899. // the 'teams' region, otherwise do not capture.
  7900. if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
  7901. CaptureRegion = OMPD_teams;
  7902. break;
  7903. case OMPD_teams_distribute_parallel_for:
  7904. case OMPD_teams_distribute_parallel_for_simd:
  7905. CaptureRegion = OMPD_teams;
  7906. break;
  7907. case OMPD_target_update:
  7908. case OMPD_target_enter_data:
  7909. case OMPD_target_exit_data:
  7910. CaptureRegion = OMPD_task;
  7911. break;
  7912. case OMPD_cancel:
  7913. case OMPD_parallel:
  7914. case OMPD_parallel_sections:
  7915. case OMPD_parallel_for:
  7916. case OMPD_parallel_for_simd:
  7917. case OMPD_target:
  7918. case OMPD_target_simd:
  7919. case OMPD_target_teams:
  7920. case OMPD_target_teams_distribute:
  7921. case OMPD_target_teams_distribute_simd:
  7922. case OMPD_distribute_parallel_for:
  7923. case OMPD_distribute_parallel_for_simd:
  7924. case OMPD_task:
  7925. case OMPD_taskloop:
  7926. case OMPD_taskloop_simd:
  7927. case OMPD_target_data:
  7928. // Do not capture if-clause expressions.
  7929. break;
  7930. case OMPD_threadprivate:
  7931. case OMPD_allocate:
  7932. case OMPD_taskyield:
  7933. case OMPD_barrier:
  7934. case OMPD_taskwait:
  7935. case OMPD_cancellation_point:
  7936. case OMPD_flush:
  7937. case OMPD_declare_reduction:
  7938. case OMPD_declare_mapper:
  7939. case OMPD_declare_simd:
  7940. case OMPD_declare_target:
  7941. case OMPD_end_declare_target:
  7942. case OMPD_teams:
  7943. case OMPD_simd:
  7944. case OMPD_for:
  7945. case OMPD_for_simd:
  7946. case OMPD_sections:
  7947. case OMPD_section:
  7948. case OMPD_single:
  7949. case OMPD_master:
  7950. case OMPD_critical:
  7951. case OMPD_taskgroup:
  7952. case OMPD_distribute:
  7953. case OMPD_ordered:
  7954. case OMPD_atomic:
  7955. case OMPD_distribute_simd:
  7956. case OMPD_teams_distribute:
  7957. case OMPD_teams_distribute_simd:
  7958. case OMPD_requires:
  7959. llvm_unreachable("Unexpected OpenMP directive with if-clause");
  7960. case OMPD_unknown:
  7961. llvm_unreachable("Unknown OpenMP directive");
  7962. }
  7963. break;
  7964. case OMPC_num_threads:
  7965. switch (DKind) {
  7966. case OMPD_target_parallel:
  7967. case OMPD_target_parallel_for:
  7968. case OMPD_target_parallel_for_simd:
  7969. CaptureRegion = OMPD_target;
  7970. break;
  7971. case OMPD_teams_distribute_parallel_for:
  7972. case OMPD_teams_distribute_parallel_for_simd:
  7973. case OMPD_target_teams_distribute_parallel_for:
  7974. case OMPD_target_teams_distribute_parallel_for_simd:
  7975. CaptureRegion = OMPD_teams;
  7976. break;
  7977. case OMPD_parallel:
  7978. case OMPD_parallel_sections:
  7979. case OMPD_parallel_for:
  7980. case OMPD_parallel_for_simd:
  7981. case OMPD_distribute_parallel_for:
  7982. case OMPD_distribute_parallel_for_simd:
  7983. // Do not capture num_threads-clause expressions.
  7984. break;
  7985. case OMPD_target_data:
  7986. case OMPD_target_enter_data:
  7987. case OMPD_target_exit_data:
  7988. case OMPD_target_update:
  7989. case OMPD_target:
  7990. case OMPD_target_simd:
  7991. case OMPD_target_teams:
  7992. case OMPD_target_teams_distribute:
  7993. case OMPD_target_teams_distribute_simd:
  7994. case OMPD_cancel:
  7995. case OMPD_task:
  7996. case OMPD_taskloop:
  7997. case OMPD_taskloop_simd:
  7998. case OMPD_threadprivate:
  7999. case OMPD_allocate:
  8000. case OMPD_taskyield:
  8001. case OMPD_barrier:
  8002. case OMPD_taskwait:
  8003. case OMPD_cancellation_point:
  8004. case OMPD_flush:
  8005. case OMPD_declare_reduction:
  8006. case OMPD_declare_mapper:
  8007. case OMPD_declare_simd:
  8008. case OMPD_declare_target:
  8009. case OMPD_end_declare_target:
  8010. case OMPD_teams:
  8011. case OMPD_simd:
  8012. case OMPD_for:
  8013. case OMPD_for_simd:
  8014. case OMPD_sections:
  8015. case OMPD_section:
  8016. case OMPD_single:
  8017. case OMPD_master:
  8018. case OMPD_critical:
  8019. case OMPD_taskgroup:
  8020. case OMPD_distribute:
  8021. case OMPD_ordered:
  8022. case OMPD_atomic:
  8023. case OMPD_distribute_simd:
  8024. case OMPD_teams_distribute:
  8025. case OMPD_teams_distribute_simd:
  8026. case OMPD_requires:
  8027. llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
  8028. case OMPD_unknown:
  8029. llvm_unreachable("Unknown OpenMP directive");
  8030. }
  8031. break;
  8032. case OMPC_num_teams:
  8033. switch (DKind) {
  8034. case OMPD_target_teams:
  8035. case OMPD_target_teams_distribute:
  8036. case OMPD_target_teams_distribute_simd:
  8037. case OMPD_target_teams_distribute_parallel_for:
  8038. case OMPD_target_teams_distribute_parallel_for_simd:
  8039. CaptureRegion = OMPD_target;
  8040. break;
  8041. case OMPD_teams_distribute_parallel_for:
  8042. case OMPD_teams_distribute_parallel_for_simd:
  8043. case OMPD_teams:
  8044. case OMPD_teams_distribute:
  8045. case OMPD_teams_distribute_simd:
  8046. // Do not capture num_teams-clause expressions.
  8047. break;
  8048. case OMPD_distribute_parallel_for:
  8049. case OMPD_distribute_parallel_for_simd:
  8050. case OMPD_task:
  8051. case OMPD_taskloop:
  8052. case OMPD_taskloop_simd:
  8053. case OMPD_target_data:
  8054. case OMPD_target_enter_data:
  8055. case OMPD_target_exit_data:
  8056. case OMPD_target_update:
  8057. case OMPD_cancel:
  8058. case OMPD_parallel:
  8059. case OMPD_parallel_sections:
  8060. case OMPD_parallel_for:
  8061. case OMPD_parallel_for_simd:
  8062. case OMPD_target:
  8063. case OMPD_target_simd:
  8064. case OMPD_target_parallel:
  8065. case OMPD_target_parallel_for:
  8066. case OMPD_target_parallel_for_simd:
  8067. case OMPD_threadprivate:
  8068. case OMPD_allocate:
  8069. case OMPD_taskyield:
  8070. case OMPD_barrier:
  8071. case OMPD_taskwait:
  8072. case OMPD_cancellation_point:
  8073. case OMPD_flush:
  8074. case OMPD_declare_reduction:
  8075. case OMPD_declare_mapper:
  8076. case OMPD_declare_simd:
  8077. case OMPD_declare_target:
  8078. case OMPD_end_declare_target:
  8079. case OMPD_simd:
  8080. case OMPD_for:
  8081. case OMPD_for_simd:
  8082. case OMPD_sections:
  8083. case OMPD_section:
  8084. case OMPD_single:
  8085. case OMPD_master:
  8086. case OMPD_critical:
  8087. case OMPD_taskgroup:
  8088. case OMPD_distribute:
  8089. case OMPD_ordered:
  8090. case OMPD_atomic:
  8091. case OMPD_distribute_simd:
  8092. case OMPD_requires:
  8093. llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
  8094. case OMPD_unknown:
  8095. llvm_unreachable("Unknown OpenMP directive");
  8096. }
  8097. break;
  8098. case OMPC_thread_limit:
  8099. switch (DKind) {
  8100. case OMPD_target_teams:
  8101. case OMPD_target_teams_distribute:
  8102. case OMPD_target_teams_distribute_simd:
  8103. case OMPD_target_teams_distribute_parallel_for:
  8104. case OMPD_target_teams_distribute_parallel_for_simd:
  8105. CaptureRegion = OMPD_target;
  8106. break;
  8107. case OMPD_teams_distribute_parallel_for:
  8108. case OMPD_teams_distribute_parallel_for_simd:
  8109. case OMPD_teams:
  8110. case OMPD_teams_distribute:
  8111. case OMPD_teams_distribute_simd:
  8112. // Do not capture thread_limit-clause expressions.
  8113. break;
  8114. case OMPD_distribute_parallel_for:
  8115. case OMPD_distribute_parallel_for_simd:
  8116. case OMPD_task:
  8117. case OMPD_taskloop:
  8118. case OMPD_taskloop_simd:
  8119. case OMPD_target_data:
  8120. case OMPD_target_enter_data:
  8121. case OMPD_target_exit_data:
  8122. case OMPD_target_update:
  8123. case OMPD_cancel:
  8124. case OMPD_parallel:
  8125. case OMPD_parallel_sections:
  8126. case OMPD_parallel_for:
  8127. case OMPD_parallel_for_simd:
  8128. case OMPD_target:
  8129. case OMPD_target_simd:
  8130. case OMPD_target_parallel:
  8131. case OMPD_target_parallel_for:
  8132. case OMPD_target_parallel_for_simd:
  8133. case OMPD_threadprivate:
  8134. case OMPD_allocate:
  8135. case OMPD_taskyield:
  8136. case OMPD_barrier:
  8137. case OMPD_taskwait:
  8138. case OMPD_cancellation_point:
  8139. case OMPD_flush:
  8140. case OMPD_declare_reduction:
  8141. case OMPD_declare_mapper:
  8142. case OMPD_declare_simd:
  8143. case OMPD_declare_target:
  8144. case OMPD_end_declare_target:
  8145. case OMPD_simd:
  8146. case OMPD_for:
  8147. case OMPD_for_simd:
  8148. case OMPD_sections:
  8149. case OMPD_section:
  8150. case OMPD_single:
  8151. case OMPD_master:
  8152. case OMPD_critical:
  8153. case OMPD_taskgroup:
  8154. case OMPD_distribute:
  8155. case OMPD_ordered:
  8156. case OMPD_atomic:
  8157. case OMPD_distribute_simd:
  8158. case OMPD_requires:
  8159. llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
  8160. case OMPD_unknown:
  8161. llvm_unreachable("Unknown OpenMP directive");
  8162. }
  8163. break;
  8164. case OMPC_schedule:
  8165. switch (DKind) {
  8166. case OMPD_parallel_for:
  8167. case OMPD_parallel_for_simd:
  8168. case OMPD_distribute_parallel_for:
  8169. case OMPD_distribute_parallel_for_simd:
  8170. case OMPD_teams_distribute_parallel_for:
  8171. case OMPD_teams_distribute_parallel_for_simd:
  8172. case OMPD_target_parallel_for:
  8173. case OMPD_target_parallel_for_simd:
  8174. case OMPD_target_teams_distribute_parallel_for:
  8175. case OMPD_target_teams_distribute_parallel_for_simd:
  8176. CaptureRegion = OMPD_parallel;
  8177. break;
  8178. case OMPD_for:
  8179. case OMPD_for_simd:
  8180. // Do not capture schedule-clause expressions.
  8181. break;
  8182. case OMPD_task:
  8183. case OMPD_taskloop:
  8184. case OMPD_taskloop_simd:
  8185. case OMPD_target_data:
  8186. case OMPD_target_enter_data:
  8187. case OMPD_target_exit_data:
  8188. case OMPD_target_update:
  8189. case OMPD_teams:
  8190. case OMPD_teams_distribute:
  8191. case OMPD_teams_distribute_simd:
  8192. case OMPD_target_teams_distribute:
  8193. case OMPD_target_teams_distribute_simd:
  8194. case OMPD_target:
  8195. case OMPD_target_simd:
  8196. case OMPD_target_parallel:
  8197. case OMPD_cancel:
  8198. case OMPD_parallel:
  8199. case OMPD_parallel_sections:
  8200. case OMPD_threadprivate:
  8201. case OMPD_allocate:
  8202. case OMPD_taskyield:
  8203. case OMPD_barrier:
  8204. case OMPD_taskwait:
  8205. case OMPD_cancellation_point:
  8206. case OMPD_flush:
  8207. case OMPD_declare_reduction:
  8208. case OMPD_declare_mapper:
  8209. case OMPD_declare_simd:
  8210. case OMPD_declare_target:
  8211. case OMPD_end_declare_target:
  8212. case OMPD_simd:
  8213. case OMPD_sections:
  8214. case OMPD_section:
  8215. case OMPD_single:
  8216. case OMPD_master:
  8217. case OMPD_critical:
  8218. case OMPD_taskgroup:
  8219. case OMPD_distribute:
  8220. case OMPD_ordered:
  8221. case OMPD_atomic:
  8222. case OMPD_distribute_simd:
  8223. case OMPD_target_teams:
  8224. case OMPD_requires:
  8225. llvm_unreachable("Unexpected OpenMP directive with schedule clause");
  8226. case OMPD_unknown:
  8227. llvm_unreachable("Unknown OpenMP directive");
  8228. }
  8229. break;
  8230. case OMPC_dist_schedule:
  8231. switch (DKind) {
  8232. case OMPD_teams_distribute_parallel_for:
  8233. case OMPD_teams_distribute_parallel_for_simd:
  8234. case OMPD_teams_distribute:
  8235. case OMPD_teams_distribute_simd:
  8236. case OMPD_target_teams_distribute_parallel_for:
  8237. case OMPD_target_teams_distribute_parallel_for_simd:
  8238. case OMPD_target_teams_distribute:
  8239. case OMPD_target_teams_distribute_simd:
  8240. CaptureRegion = OMPD_teams;
  8241. break;
  8242. case OMPD_distribute_parallel_for:
  8243. case OMPD_distribute_parallel_for_simd:
  8244. case OMPD_distribute:
  8245. case OMPD_distribute_simd:
  8246. // Do not capture thread_limit-clause expressions.
  8247. break;
  8248. case OMPD_parallel_for:
  8249. case OMPD_parallel_for_simd:
  8250. case OMPD_target_parallel_for_simd:
  8251. case OMPD_target_parallel_for:
  8252. case OMPD_task:
  8253. case OMPD_taskloop:
  8254. case OMPD_taskloop_simd:
  8255. case OMPD_target_data:
  8256. case OMPD_target_enter_data:
  8257. case OMPD_target_exit_data:
  8258. case OMPD_target_update:
  8259. case OMPD_teams:
  8260. case OMPD_target:
  8261. case OMPD_target_simd:
  8262. case OMPD_target_parallel:
  8263. case OMPD_cancel:
  8264. case OMPD_parallel:
  8265. case OMPD_parallel_sections:
  8266. case OMPD_threadprivate:
  8267. case OMPD_allocate:
  8268. case OMPD_taskyield:
  8269. case OMPD_barrier:
  8270. case OMPD_taskwait:
  8271. case OMPD_cancellation_point:
  8272. case OMPD_flush:
  8273. case OMPD_declare_reduction:
  8274. case OMPD_declare_mapper:
  8275. case OMPD_declare_simd:
  8276. case OMPD_declare_target:
  8277. case OMPD_end_declare_target:
  8278. case OMPD_simd:
  8279. case OMPD_for:
  8280. case OMPD_for_simd:
  8281. case OMPD_sections:
  8282. case OMPD_section:
  8283. case OMPD_single:
  8284. case OMPD_master:
  8285. case OMPD_critical:
  8286. case OMPD_taskgroup:
  8287. case OMPD_ordered:
  8288. case OMPD_atomic:
  8289. case OMPD_target_teams:
  8290. case OMPD_requires:
  8291. llvm_unreachable("Unexpected OpenMP directive with schedule clause");
  8292. case OMPD_unknown:
  8293. llvm_unreachable("Unknown OpenMP directive");
  8294. }
  8295. break;
  8296. case OMPC_device:
  8297. switch (DKind) {
  8298. case OMPD_target_update:
  8299. case OMPD_target_enter_data:
  8300. case OMPD_target_exit_data:
  8301. case OMPD_target:
  8302. case OMPD_target_simd:
  8303. case OMPD_target_teams:
  8304. case OMPD_target_parallel:
  8305. case OMPD_target_teams_distribute:
  8306. case OMPD_target_teams_distribute_simd:
  8307. case OMPD_target_parallel_for:
  8308. case OMPD_target_parallel_for_simd:
  8309. case OMPD_target_teams_distribute_parallel_for:
  8310. case OMPD_target_teams_distribute_parallel_for_simd:
  8311. CaptureRegion = OMPD_task;
  8312. break;
  8313. case OMPD_target_data:
  8314. // Do not capture device-clause expressions.
  8315. break;
  8316. case OMPD_teams_distribute_parallel_for:
  8317. case OMPD_teams_distribute_parallel_for_simd:
  8318. case OMPD_teams:
  8319. case OMPD_teams_distribute:
  8320. case OMPD_teams_distribute_simd:
  8321. case OMPD_distribute_parallel_for:
  8322. case OMPD_distribute_parallel_for_simd:
  8323. case OMPD_task:
  8324. case OMPD_taskloop:
  8325. case OMPD_taskloop_simd:
  8326. case OMPD_cancel:
  8327. case OMPD_parallel:
  8328. case OMPD_parallel_sections:
  8329. case OMPD_parallel_for:
  8330. case OMPD_parallel_for_simd:
  8331. case OMPD_threadprivate:
  8332. case OMPD_allocate:
  8333. case OMPD_taskyield:
  8334. case OMPD_barrier:
  8335. case OMPD_taskwait:
  8336. case OMPD_cancellation_point:
  8337. case OMPD_flush:
  8338. case OMPD_declare_reduction:
  8339. case OMPD_declare_mapper:
  8340. case OMPD_declare_simd:
  8341. case OMPD_declare_target:
  8342. case OMPD_end_declare_target:
  8343. case OMPD_simd:
  8344. case OMPD_for:
  8345. case OMPD_for_simd:
  8346. case OMPD_sections:
  8347. case OMPD_section:
  8348. case OMPD_single:
  8349. case OMPD_master:
  8350. case OMPD_critical:
  8351. case OMPD_taskgroup:
  8352. case OMPD_distribute:
  8353. case OMPD_ordered:
  8354. case OMPD_atomic:
  8355. case OMPD_distribute_simd:
  8356. case OMPD_requires:
  8357. llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
  8358. case OMPD_unknown:
  8359. llvm_unreachable("Unknown OpenMP directive");
  8360. }
  8361. break;
  8362. case OMPC_firstprivate:
  8363. case OMPC_lastprivate:
  8364. case OMPC_reduction:
  8365. case OMPC_task_reduction:
  8366. case OMPC_in_reduction:
  8367. case OMPC_linear:
  8368. case OMPC_default:
  8369. case OMPC_proc_bind:
  8370. case OMPC_final:
  8371. case OMPC_safelen:
  8372. case OMPC_simdlen:
  8373. case OMPC_collapse:
  8374. case OMPC_private:
  8375. case OMPC_shared:
  8376. case OMPC_aligned:
  8377. case OMPC_copyin:
  8378. case OMPC_copyprivate:
  8379. case OMPC_ordered:
  8380. case OMPC_nowait:
  8381. case OMPC_untied:
  8382. case OMPC_mergeable:
  8383. case OMPC_threadprivate:
  8384. case OMPC_allocate:
  8385. case OMPC_flush:
  8386. case OMPC_read:
  8387. case OMPC_write:
  8388. case OMPC_update:
  8389. case OMPC_capture:
  8390. case OMPC_seq_cst:
  8391. case OMPC_depend:
  8392. case OMPC_threads:
  8393. case OMPC_simd:
  8394. case OMPC_map:
  8395. case OMPC_priority:
  8396. case OMPC_grainsize:
  8397. case OMPC_nogroup:
  8398. case OMPC_num_tasks:
  8399. case OMPC_hint:
  8400. case OMPC_defaultmap:
  8401. case OMPC_unknown:
  8402. case OMPC_uniform:
  8403. case OMPC_to:
  8404. case OMPC_from:
  8405. case OMPC_use_device_ptr:
  8406. case OMPC_is_device_ptr:
  8407. case OMPC_unified_address:
  8408. case OMPC_unified_shared_memory:
  8409. case OMPC_reverse_offload:
  8410. case OMPC_dynamic_allocators:
  8411. case OMPC_atomic_default_mem_order:
  8412. llvm_unreachable("Unexpected OpenMP clause.");
  8413. }
  8414. return CaptureRegion;
  8415. }
  8416. OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
  8417. Expr *Condition, SourceLocation StartLoc,
  8418. SourceLocation LParenLoc,
  8419. SourceLocation NameModifierLoc,
  8420. SourceLocation ColonLoc,
  8421. SourceLocation EndLoc) {
  8422. Expr *ValExpr = Condition;
  8423. Stmt *HelperValStmt = nullptr;
  8424. OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
  8425. if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
  8426. !Condition->isInstantiationDependent() &&
  8427. !Condition->containsUnexpandedParameterPack()) {
  8428. ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
  8429. if (Val.isInvalid())
  8430. return nullptr;
  8431. ValExpr = Val.get();
  8432. OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
  8433. CaptureRegion =
  8434. getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
  8435. if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
  8436. ValExpr = MakeFullExpr(ValExpr).get();
  8437. llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
  8438. ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
  8439. HelperValStmt = buildPreInits(Context, Captures);
  8440. }
  8441. }
  8442. return new (Context)
  8443. OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
  8444. LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
  8445. }
  8446. OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
  8447. SourceLocation StartLoc,
  8448. SourceLocation LParenLoc,
  8449. SourceLocation EndLoc) {
  8450. Expr *ValExpr = Condition;
  8451. if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
  8452. !Condition->isInstantiationDependent() &&
  8453. !Condition->containsUnexpandedParameterPack()) {
  8454. ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
  8455. if (Val.isInvalid())
  8456. return nullptr;
  8457. ValExpr = MakeFullExpr(Val.get()).get();
  8458. }
  8459. return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
  8460. }
  8461. ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
  8462. Expr *Op) {
  8463. if (!Op)
  8464. return ExprError();
  8465. class IntConvertDiagnoser : public ICEConvertDiagnoser {
  8466. public:
  8467. IntConvertDiagnoser()
  8468. : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
  8469. SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
  8470. QualType T) override {
  8471. return S.Diag(Loc, diag::err_omp_not_integral) << T;
  8472. }
  8473. SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
  8474. QualType T) override {
  8475. return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
  8476. }
  8477. SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
  8478. QualType T,
  8479. QualType ConvTy) override {
  8480. return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
  8481. }
  8482. SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
  8483. QualType ConvTy) override {
  8484. return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
  8485. << ConvTy->isEnumeralType() << ConvTy;
  8486. }
  8487. SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
  8488. QualType T) override {
  8489. return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
  8490. }
  8491. SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
  8492. QualType ConvTy) override {
  8493. return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
  8494. << ConvTy->isEnumeralType() << ConvTy;
  8495. }
  8496. SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
  8497. QualType) override {
  8498. llvm_unreachable("conversion functions are permitted");
  8499. }
  8500. } ConvertDiagnoser;
  8501. return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
  8502. }
  8503. static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
  8504. OpenMPClauseKind CKind,
  8505. bool StrictlyPositive) {
  8506. if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
  8507. !ValExpr->isInstantiationDependent()) {
  8508. SourceLocation Loc = ValExpr->getExprLoc();
  8509. ExprResult Value =
  8510. SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
  8511. if (Value.isInvalid())
  8512. return false;
  8513. ValExpr = Value.get();
  8514. // The expression must evaluate to a non-negative integer value.
  8515. llvm::APSInt Result;
  8516. if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
  8517. Result.isSigned() &&
  8518. !((!StrictlyPositive && Result.isNonNegative()) ||
  8519. (StrictlyPositive && Result.isStrictlyPositive()))) {
  8520. SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
  8521. << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
  8522. << ValExpr->getSourceRange();
  8523. return false;
  8524. }
  8525. }
  8526. return true;
  8527. }
  8528. OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
  8529. SourceLocation StartLoc,
  8530. SourceLocation LParenLoc,
  8531. SourceLocation EndLoc) {
  8532. Expr *ValExpr = NumThreads;
  8533. Stmt *HelperValStmt = nullptr;
  8534. // OpenMP [2.5, Restrictions]
  8535. // The num_threads expression must evaluate to a positive integer value.
  8536. if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
  8537. /*StrictlyPositive=*/true))
  8538. return nullptr;
  8539. OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
  8540. OpenMPDirectiveKind CaptureRegion =
  8541. getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
  8542. if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
  8543. ValExpr = MakeFullExpr(ValExpr).get();
  8544. llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
  8545. ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
  8546. HelperValStmt = buildPreInits(Context, Captures);
  8547. }
  8548. return new (Context) OMPNumThreadsClause(
  8549. ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
  8550. }
  8551. ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
  8552. OpenMPClauseKind CKind,
  8553. bool StrictlyPositive) {
  8554. if (!E)
  8555. return ExprError();
  8556. if (E->isValueDependent() || E->isTypeDependent() ||
  8557. E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
  8558. return E;
  8559. llvm::APSInt Result;
  8560. ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
  8561. if (ICE.isInvalid())
  8562. return ExprError();
  8563. if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
  8564. (!StrictlyPositive && !Result.isNonNegative())) {
  8565. Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
  8566. << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
  8567. << E->getSourceRange();
  8568. return ExprError();
  8569. }
  8570. if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
  8571. Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
  8572. << E->getSourceRange();
  8573. return ExprError();
  8574. }
  8575. if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
  8576. DSAStack->setAssociatedLoops(Result.getExtValue());
  8577. else if (CKind == OMPC_ordered)
  8578. DSAStack->setAssociatedLoops(Result.getExtValue());
  8579. return ICE;
  8580. }
  8581. OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
  8582. SourceLocation LParenLoc,
  8583. SourceLocation EndLoc) {
  8584. // OpenMP [2.8.1, simd construct, Description]
  8585. // The parameter of the safelen clause must be a constant
  8586. // positive integer expression.
  8587. ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
  8588. if (Safelen.isInvalid())
  8589. return nullptr;
  8590. return new (Context)
  8591. OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
  8592. }
  8593. OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
  8594. SourceLocation LParenLoc,
  8595. SourceLocation EndLoc) {
  8596. // OpenMP [2.8.1, simd construct, Description]
  8597. // The parameter of the simdlen clause must be a constant
  8598. // positive integer expression.
  8599. ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
  8600. if (Simdlen.isInvalid())
  8601. return nullptr;
  8602. return new (Context)
  8603. OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
  8604. }
  8605. OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
  8606. SourceLocation StartLoc,
  8607. SourceLocation LParenLoc,
  8608. SourceLocation EndLoc) {
  8609. // OpenMP [2.7.1, loop construct, Description]
  8610. // OpenMP [2.8.1, simd construct, Description]
  8611. // OpenMP [2.9.6, distribute construct, Description]
  8612. // The parameter of the collapse clause must be a constant
  8613. // positive integer expression.
  8614. ExprResult NumForLoopsResult =
  8615. VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
  8616. if (NumForLoopsResult.isInvalid())
  8617. return nullptr;
  8618. return new (Context)
  8619. OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
  8620. }
  8621. OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
  8622. SourceLocation EndLoc,
  8623. SourceLocation LParenLoc,
  8624. Expr *NumForLoops) {
  8625. // OpenMP [2.7.1, loop construct, Description]
  8626. // OpenMP [2.8.1, simd construct, Description]
  8627. // OpenMP [2.9.6, distribute construct, Description]
  8628. // The parameter of the ordered clause must be a constant
  8629. // positive integer expression if any.
  8630. if (NumForLoops && LParenLoc.isValid()) {
  8631. ExprResult NumForLoopsResult =
  8632. VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
  8633. if (NumForLoopsResult.isInvalid())
  8634. return nullptr;
  8635. NumForLoops = NumForLoopsResult.get();
  8636. } else {
  8637. NumForLoops = nullptr;
  8638. }
  8639. auto *Clause = OMPOrderedClause::Create(
  8640. Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
  8641. StartLoc, LParenLoc, EndLoc);
  8642. DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
  8643. return Clause;
  8644. }
  8645. OMPClause *Sema::ActOnOpenMPSimpleClause(
  8646. OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
  8647. SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
  8648. OMPClause *Res = nullptr;
  8649. switch (Kind) {
  8650. case OMPC_default:
  8651. Res =
  8652. ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
  8653. ArgumentLoc, StartLoc, LParenLoc, EndLoc);
  8654. break;
  8655. case OMPC_proc_bind:
  8656. Res = ActOnOpenMPProcBindClause(
  8657. static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
  8658. LParenLoc, EndLoc);
  8659. break;
  8660. case OMPC_atomic_default_mem_order:
  8661. Res = ActOnOpenMPAtomicDefaultMemOrderClause(
  8662. static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
  8663. ArgumentLoc, StartLoc, LParenLoc, EndLoc);
  8664. break;
  8665. case OMPC_if:
  8666. case OMPC_final:
  8667. case OMPC_num_threads:
  8668. case OMPC_safelen:
  8669. case OMPC_simdlen:
  8670. case OMPC_collapse:
  8671. case OMPC_schedule:
  8672. case OMPC_private:
  8673. case OMPC_firstprivate:
  8674. case OMPC_lastprivate:
  8675. case OMPC_shared:
  8676. case OMPC_reduction:
  8677. case OMPC_task_reduction:
  8678. case OMPC_in_reduction:
  8679. case OMPC_linear:
  8680. case OMPC_aligned:
  8681. case OMPC_copyin:
  8682. case OMPC_copyprivate:
  8683. case OMPC_ordered:
  8684. case OMPC_nowait:
  8685. case OMPC_untied:
  8686. case OMPC_mergeable:
  8687. case OMPC_threadprivate:
  8688. case OMPC_allocate:
  8689. case OMPC_flush:
  8690. case OMPC_read:
  8691. case OMPC_write:
  8692. case OMPC_update:
  8693. case OMPC_capture:
  8694. case OMPC_seq_cst:
  8695. case OMPC_depend:
  8696. case OMPC_device:
  8697. case OMPC_threads:
  8698. case OMPC_simd:
  8699. case OMPC_map:
  8700. case OMPC_num_teams:
  8701. case OMPC_thread_limit:
  8702. case OMPC_priority:
  8703. case OMPC_grainsize:
  8704. case OMPC_nogroup:
  8705. case OMPC_num_tasks:
  8706. case OMPC_hint:
  8707. case OMPC_dist_schedule:
  8708. case OMPC_defaultmap:
  8709. case OMPC_unknown:
  8710. case OMPC_uniform:
  8711. case OMPC_to:
  8712. case OMPC_from:
  8713. case OMPC_use_device_ptr:
  8714. case OMPC_is_device_ptr:
  8715. case OMPC_unified_address:
  8716. case OMPC_unified_shared_memory:
  8717. case OMPC_reverse_offload:
  8718. case OMPC_dynamic_allocators:
  8719. llvm_unreachable("Clause is not allowed.");
  8720. }
  8721. return Res;
  8722. }
  8723. static std::string
  8724. getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
  8725. ArrayRef<unsigned> Exclude = llvm::None) {
  8726. SmallString<256> Buffer;
  8727. llvm::raw_svector_ostream Out(Buffer);
  8728. unsigned Bound = Last >= 2 ? Last - 2 : 0;
  8729. unsigned Skipped = Exclude.size();
  8730. auto S = Exclude.begin(), E = Exclude.end();
  8731. for (unsigned I = First; I < Last; ++I) {
  8732. if (std::find(S, E, I) != E) {
  8733. --Skipped;
  8734. continue;
  8735. }
  8736. Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
  8737. if (I == Bound - Skipped)
  8738. Out << " or ";
  8739. else if (I != Bound + 1 - Skipped)
  8740. Out << ", ";
  8741. }
  8742. return Out.str();
  8743. }
  8744. OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
  8745. SourceLocation KindKwLoc,
  8746. SourceLocation StartLoc,
  8747. SourceLocation LParenLoc,
  8748. SourceLocation EndLoc) {
  8749. if (Kind == OMPC_DEFAULT_unknown) {
  8750. static_assert(OMPC_DEFAULT_unknown > 0,
  8751. "OMPC_DEFAULT_unknown not greater than 0");
  8752. Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
  8753. << getListOfPossibleValues(OMPC_default, /*First=*/0,
  8754. /*Last=*/OMPC_DEFAULT_unknown)
  8755. << getOpenMPClauseName(OMPC_default);
  8756. return nullptr;
  8757. }
  8758. switch (Kind) {
  8759. case OMPC_DEFAULT_none:
  8760. DSAStack->setDefaultDSANone(KindKwLoc);
  8761. break;
  8762. case OMPC_DEFAULT_shared:
  8763. DSAStack->setDefaultDSAShared(KindKwLoc);
  8764. break;
  8765. case OMPC_DEFAULT_unknown:
  8766. llvm_unreachable("Clause kind is not allowed.");
  8767. break;
  8768. }
  8769. return new (Context)
  8770. OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
  8771. }
  8772. OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
  8773. SourceLocation KindKwLoc,
  8774. SourceLocation StartLoc,
  8775. SourceLocation LParenLoc,
  8776. SourceLocation EndLoc) {
  8777. if (Kind == OMPC_PROC_BIND_unknown) {
  8778. Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
  8779. << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
  8780. /*Last=*/OMPC_PROC_BIND_unknown)
  8781. << getOpenMPClauseName(OMPC_proc_bind);
  8782. return nullptr;
  8783. }
  8784. return new (Context)
  8785. OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
  8786. }
  8787. OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause(
  8788. OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
  8789. SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
  8790. if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
  8791. Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
  8792. << getListOfPossibleValues(
  8793. OMPC_atomic_default_mem_order, /*First=*/0,
  8794. /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
  8795. << getOpenMPClauseName(OMPC_atomic_default_mem_order);
  8796. return nullptr;
  8797. }
  8798. return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc,
  8799. LParenLoc, EndLoc);
  8800. }
  8801. OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
  8802. OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
  8803. SourceLocation StartLoc, SourceLocation LParenLoc,
  8804. ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
  8805. SourceLocation EndLoc) {
  8806. OMPClause *Res = nullptr;
  8807. switch (Kind) {
  8808. case OMPC_schedule:
  8809. enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
  8810. assert(Argument.size() == NumberOfElements &&
  8811. ArgumentLoc.size() == NumberOfElements);
  8812. Res = ActOnOpenMPScheduleClause(
  8813. static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
  8814. static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
  8815. static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
  8816. StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
  8817. ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
  8818. break;
  8819. case OMPC_if:
  8820. assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
  8821. Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
  8822. Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
  8823. DelimLoc, EndLoc);
  8824. break;
  8825. case OMPC_dist_schedule:
  8826. Res = ActOnOpenMPDistScheduleClause(
  8827. static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
  8828. StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
  8829. break;
  8830. case OMPC_defaultmap:
  8831. enum { Modifier, DefaultmapKind };
  8832. Res = ActOnOpenMPDefaultmapClause(
  8833. static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
  8834. static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
  8835. StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
  8836. EndLoc);
  8837. break;
  8838. case OMPC_final:
  8839. case OMPC_num_threads:
  8840. case OMPC_safelen:
  8841. case OMPC_simdlen:
  8842. case OMPC_collapse:
  8843. case OMPC_default:
  8844. case OMPC_proc_bind:
  8845. case OMPC_private:
  8846. case OMPC_firstprivate:
  8847. case OMPC_lastprivate:
  8848. case OMPC_shared:
  8849. case OMPC_reduction:
  8850. case OMPC_task_reduction:
  8851. case OMPC_in_reduction:
  8852. case OMPC_linear:
  8853. case OMPC_aligned:
  8854. case OMPC_copyin:
  8855. case OMPC_copyprivate:
  8856. case OMPC_ordered:
  8857. case OMPC_nowait:
  8858. case OMPC_untied:
  8859. case OMPC_mergeable:
  8860. case OMPC_threadprivate:
  8861. case OMPC_allocate:
  8862. case OMPC_flush:
  8863. case OMPC_read:
  8864. case OMPC_write:
  8865. case OMPC_update:
  8866. case OMPC_capture:
  8867. case OMPC_seq_cst:
  8868. case OMPC_depend:
  8869. case OMPC_device:
  8870. case OMPC_threads:
  8871. case OMPC_simd:
  8872. case OMPC_map:
  8873. case OMPC_num_teams:
  8874. case OMPC_thread_limit:
  8875. case OMPC_priority:
  8876. case OMPC_grainsize:
  8877. case OMPC_nogroup:
  8878. case OMPC_num_tasks:
  8879. case OMPC_hint:
  8880. case OMPC_unknown:
  8881. case OMPC_uniform:
  8882. case OMPC_to:
  8883. case OMPC_from:
  8884. case OMPC_use_device_ptr:
  8885. case OMPC_is_device_ptr:
  8886. case OMPC_unified_address:
  8887. case OMPC_unified_shared_memory:
  8888. case OMPC_reverse_offload:
  8889. case OMPC_dynamic_allocators:
  8890. case OMPC_atomic_default_mem_order:
  8891. llvm_unreachable("Clause is not allowed.");
  8892. }
  8893. return Res;
  8894. }
  8895. static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
  8896. OpenMPScheduleClauseModifier M2,
  8897. SourceLocation M1Loc, SourceLocation M2Loc) {
  8898. if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
  8899. SmallVector<unsigned, 2> Excluded;
  8900. if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
  8901. Excluded.push_back(M2);
  8902. if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
  8903. Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
  8904. if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
  8905. Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
  8906. S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
  8907. << getListOfPossibleValues(OMPC_schedule,
  8908. /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
  8909. /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
  8910. Excluded)
  8911. << getOpenMPClauseName(OMPC_schedule);
  8912. return true;
  8913. }
  8914. return false;
  8915. }
  8916. OMPClause *Sema::ActOnOpenMPScheduleClause(
  8917. OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
  8918. OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
  8919. SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
  8920. SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
  8921. if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
  8922. checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
  8923. return nullptr;
  8924. // OpenMP, 2.7.1, Loop Construct, Restrictions
  8925. // Either the monotonic modifier or the nonmonotonic modifier can be specified
  8926. // but not both.
  8927. if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
  8928. (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
  8929. M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
  8930. (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
  8931. M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
  8932. Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
  8933. << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
  8934. << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
  8935. return nullptr;
  8936. }
  8937. if (Kind == OMPC_SCHEDULE_unknown) {
  8938. std::string Values;
  8939. if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
  8940. unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
  8941. Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
  8942. /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
  8943. Exclude);
  8944. } else {
  8945. Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
  8946. /*Last=*/OMPC_SCHEDULE_unknown);
  8947. }
  8948. Diag(KindLoc, diag::err_omp_unexpected_clause_value)
  8949. << Values << getOpenMPClauseName(OMPC_schedule);
  8950. return nullptr;
  8951. }
  8952. // OpenMP, 2.7.1, Loop Construct, Restrictions
  8953. // The nonmonotonic modifier can only be specified with schedule(dynamic) or
  8954. // schedule(guided).
  8955. if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
  8956. M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
  8957. Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
  8958. Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
  8959. diag::err_omp_schedule_nonmonotonic_static);
  8960. return nullptr;
  8961. }
  8962. Expr *ValExpr = ChunkSize;
  8963. Stmt *HelperValStmt = nullptr;
  8964. if (ChunkSize) {
  8965. if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
  8966. !ChunkSize->isInstantiationDependent() &&
  8967. !ChunkSize->containsUnexpandedParameterPack()) {
  8968. SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
  8969. ExprResult Val =
  8970. PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
  8971. if (Val.isInvalid())
  8972. return nullptr;
  8973. ValExpr = Val.get();
  8974. // OpenMP [2.7.1, Restrictions]
  8975. // chunk_size must be a loop invariant integer expression with a positive
  8976. // value.
  8977. llvm::APSInt Result;
  8978. if (ValExpr->isIntegerConstantExpr(Result, Context)) {
  8979. if (Result.isSigned() && !Result.isStrictlyPositive()) {
  8980. Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
  8981. << "schedule" << 1 << ChunkSize->getSourceRange();
  8982. return nullptr;
  8983. }
  8984. } else if (getOpenMPCaptureRegionForClause(
  8985. DSAStack->getCurrentDirective(), OMPC_schedule) !=
  8986. OMPD_unknown &&
  8987. !CurContext->isDependentContext()) {
  8988. ValExpr = MakeFullExpr(ValExpr).get();
  8989. llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
  8990. ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
  8991. HelperValStmt = buildPreInits(Context, Captures);
  8992. }
  8993. }
  8994. }
  8995. return new (Context)
  8996. OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
  8997. ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
  8998. }
  8999. OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
  9000. SourceLocation StartLoc,
  9001. SourceLocation EndLoc) {
  9002. OMPClause *Res = nullptr;
  9003. switch (Kind) {
  9004. case OMPC_ordered:
  9005. Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
  9006. break;
  9007. case OMPC_nowait:
  9008. Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
  9009. break;
  9010. case OMPC_untied:
  9011. Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
  9012. break;
  9013. case OMPC_mergeable:
  9014. Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
  9015. break;
  9016. case OMPC_read:
  9017. Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
  9018. break;
  9019. case OMPC_write:
  9020. Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
  9021. break;
  9022. case OMPC_update:
  9023. Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
  9024. break;
  9025. case OMPC_capture:
  9026. Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
  9027. break;
  9028. case OMPC_seq_cst:
  9029. Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
  9030. break;
  9031. case OMPC_threads:
  9032. Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
  9033. break;
  9034. case OMPC_simd:
  9035. Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
  9036. break;
  9037. case OMPC_nogroup:
  9038. Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
  9039. break;
  9040. case OMPC_unified_address:
  9041. Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
  9042. break;
  9043. case OMPC_unified_shared_memory:
  9044. Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
  9045. break;
  9046. case OMPC_reverse_offload:
  9047. Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
  9048. break;
  9049. case OMPC_dynamic_allocators:
  9050. Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
  9051. break;
  9052. case OMPC_if:
  9053. case OMPC_final:
  9054. case OMPC_num_threads:
  9055. case OMPC_safelen:
  9056. case OMPC_simdlen:
  9057. case OMPC_collapse:
  9058. case OMPC_schedule:
  9059. case OMPC_private:
  9060. case OMPC_firstprivate:
  9061. case OMPC_lastprivate:
  9062. case OMPC_shared:
  9063. case OMPC_reduction:
  9064. case OMPC_task_reduction:
  9065. case OMPC_in_reduction:
  9066. case OMPC_linear:
  9067. case OMPC_aligned:
  9068. case OMPC_copyin:
  9069. case OMPC_copyprivate:
  9070. case OMPC_default:
  9071. case OMPC_proc_bind:
  9072. case OMPC_threadprivate:
  9073. case OMPC_allocate:
  9074. case OMPC_flush:
  9075. case OMPC_depend:
  9076. case OMPC_device:
  9077. case OMPC_map:
  9078. case OMPC_num_teams:
  9079. case OMPC_thread_limit:
  9080. case OMPC_priority:
  9081. case OMPC_grainsize:
  9082. case OMPC_num_tasks:
  9083. case OMPC_hint:
  9084. case OMPC_dist_schedule:
  9085. case OMPC_defaultmap:
  9086. case OMPC_unknown:
  9087. case OMPC_uniform:
  9088. case OMPC_to:
  9089. case OMPC_from:
  9090. case OMPC_use_device_ptr:
  9091. case OMPC_is_device_ptr:
  9092. case OMPC_atomic_default_mem_order:
  9093. llvm_unreachable("Clause is not allowed.");
  9094. }
  9095. return Res;
  9096. }
  9097. OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
  9098. SourceLocation EndLoc) {
  9099. DSAStack->setNowaitRegion();
  9100. return new (Context) OMPNowaitClause(StartLoc, EndLoc);
  9101. }
  9102. OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
  9103. SourceLocation EndLoc) {
  9104. return new (Context) OMPUntiedClause(StartLoc, EndLoc);
  9105. }
  9106. OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
  9107. SourceLocation EndLoc) {
  9108. return new (Context) OMPMergeableClause(StartLoc, EndLoc);
  9109. }
  9110. OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
  9111. SourceLocation EndLoc) {
  9112. return new (Context) OMPReadClause(StartLoc, EndLoc);
  9113. }
  9114. OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
  9115. SourceLocation EndLoc) {
  9116. return new (Context) OMPWriteClause(StartLoc, EndLoc);
  9117. }
  9118. OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
  9119. SourceLocation EndLoc) {
  9120. return new (Context) OMPUpdateClause(StartLoc, EndLoc);
  9121. }
  9122. OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
  9123. SourceLocation EndLoc) {
  9124. return new (Context) OMPCaptureClause(StartLoc, EndLoc);
  9125. }
  9126. OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
  9127. SourceLocation EndLoc) {
  9128. return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
  9129. }
  9130. OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
  9131. SourceLocation EndLoc) {
  9132. return new (Context) OMPThreadsClause(StartLoc, EndLoc);
  9133. }
  9134. OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
  9135. SourceLocation EndLoc) {
  9136. return new (Context) OMPSIMDClause(StartLoc, EndLoc);
  9137. }
  9138. OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
  9139. SourceLocation EndLoc) {
  9140. return new (Context) OMPNogroupClause(StartLoc, EndLoc);
  9141. }
  9142. OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
  9143. SourceLocation EndLoc) {
  9144. return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
  9145. }
  9146. OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
  9147. SourceLocation EndLoc) {
  9148. return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
  9149. }
  9150. OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
  9151. SourceLocation EndLoc) {
  9152. return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc);
  9153. }
  9154. OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
  9155. SourceLocation EndLoc) {
  9156. return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
  9157. }
  9158. OMPClause *Sema::ActOnOpenMPVarListClause(
  9159. OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
  9160. const OMPVarListLocTy &Locs, SourceLocation ColonLoc,
  9161. CXXScopeSpec &ReductionOrMapperIdScopeSpec,
  9162. DeclarationNameInfo &ReductionOrMapperId, OpenMPDependClauseKind DepKind,
  9163. OpenMPLinearClauseKind LinKind,
  9164. ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
  9165. ArrayRef<SourceLocation> MapTypeModifiersLoc, OpenMPMapClauseKind MapType,
  9166. bool IsMapTypeImplicit, SourceLocation DepLinMapLoc) {
  9167. SourceLocation StartLoc = Locs.StartLoc;
  9168. SourceLocation LParenLoc = Locs.LParenLoc;
  9169. SourceLocation EndLoc = Locs.EndLoc;
  9170. OMPClause *Res = nullptr;
  9171. switch (Kind) {
  9172. case OMPC_private:
  9173. Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
  9174. break;
  9175. case OMPC_firstprivate:
  9176. Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
  9177. break;
  9178. case OMPC_lastprivate:
  9179. Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
  9180. break;
  9181. case OMPC_shared:
  9182. Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
  9183. break;
  9184. case OMPC_reduction:
  9185. Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
  9186. EndLoc, ReductionOrMapperIdScopeSpec,
  9187. ReductionOrMapperId);
  9188. break;
  9189. case OMPC_task_reduction:
  9190. Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
  9191. EndLoc, ReductionOrMapperIdScopeSpec,
  9192. ReductionOrMapperId);
  9193. break;
  9194. case OMPC_in_reduction:
  9195. Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
  9196. EndLoc, ReductionOrMapperIdScopeSpec,
  9197. ReductionOrMapperId);
  9198. break;
  9199. case OMPC_linear:
  9200. Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
  9201. LinKind, DepLinMapLoc, ColonLoc, EndLoc);
  9202. break;
  9203. case OMPC_aligned:
  9204. Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
  9205. ColonLoc, EndLoc);
  9206. break;
  9207. case OMPC_copyin:
  9208. Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
  9209. break;
  9210. case OMPC_copyprivate:
  9211. Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
  9212. break;
  9213. case OMPC_flush:
  9214. Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
  9215. break;
  9216. case OMPC_depend:
  9217. Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
  9218. StartLoc, LParenLoc, EndLoc);
  9219. break;
  9220. case OMPC_map:
  9221. Res = ActOnOpenMPMapClause(MapTypeModifiers, MapTypeModifiersLoc,
  9222. ReductionOrMapperIdScopeSpec,
  9223. ReductionOrMapperId, MapType, IsMapTypeImplicit,
  9224. DepLinMapLoc, ColonLoc, VarList, Locs);
  9225. break;
  9226. case OMPC_to:
  9227. Res = ActOnOpenMPToClause(VarList, ReductionOrMapperIdScopeSpec,
  9228. ReductionOrMapperId, Locs);
  9229. break;
  9230. case OMPC_from:
  9231. Res = ActOnOpenMPFromClause(VarList, ReductionOrMapperIdScopeSpec,
  9232. ReductionOrMapperId, Locs);
  9233. break;
  9234. case OMPC_use_device_ptr:
  9235. Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs);
  9236. break;
  9237. case OMPC_is_device_ptr:
  9238. Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs);
  9239. break;
  9240. case OMPC_if:
  9241. case OMPC_final:
  9242. case OMPC_num_threads:
  9243. case OMPC_safelen:
  9244. case OMPC_simdlen:
  9245. case OMPC_collapse:
  9246. case OMPC_default:
  9247. case OMPC_proc_bind:
  9248. case OMPC_schedule:
  9249. case OMPC_ordered:
  9250. case OMPC_nowait:
  9251. case OMPC_untied:
  9252. case OMPC_mergeable:
  9253. case OMPC_threadprivate:
  9254. case OMPC_allocate:
  9255. case OMPC_read:
  9256. case OMPC_write:
  9257. case OMPC_update:
  9258. case OMPC_capture:
  9259. case OMPC_seq_cst:
  9260. case OMPC_device:
  9261. case OMPC_threads:
  9262. case OMPC_simd:
  9263. case OMPC_num_teams:
  9264. case OMPC_thread_limit:
  9265. case OMPC_priority:
  9266. case OMPC_grainsize:
  9267. case OMPC_nogroup:
  9268. case OMPC_num_tasks:
  9269. case OMPC_hint:
  9270. case OMPC_dist_schedule:
  9271. case OMPC_defaultmap:
  9272. case OMPC_unknown:
  9273. case OMPC_uniform:
  9274. case OMPC_unified_address:
  9275. case OMPC_unified_shared_memory:
  9276. case OMPC_reverse_offload:
  9277. case OMPC_dynamic_allocators:
  9278. case OMPC_atomic_default_mem_order:
  9279. llvm_unreachable("Clause is not allowed.");
  9280. }
  9281. return Res;
  9282. }
  9283. ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
  9284. ExprObjectKind OK, SourceLocation Loc) {
  9285. ExprResult Res = BuildDeclRefExpr(
  9286. Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
  9287. if (!Res.isUsable())
  9288. return ExprError();
  9289. if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
  9290. Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
  9291. if (!Res.isUsable())
  9292. return ExprError();
  9293. }
  9294. if (VK != VK_LValue && Res.get()->isGLValue()) {
  9295. Res = DefaultLvalueConversion(Res.get());
  9296. if (!Res.isUsable())
  9297. return ExprError();
  9298. }
  9299. return Res;
  9300. }
  9301. static std::pair<ValueDecl *, bool>
  9302. getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
  9303. SourceRange &ERange, bool AllowArraySection = false) {
  9304. if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
  9305. RefExpr->containsUnexpandedParameterPack())
  9306. return std::make_pair(nullptr, true);
  9307. // OpenMP [3.1, C/C++]
  9308. // A list item is a variable name.
  9309. // OpenMP [2.9.3.3, Restrictions, p.1]
  9310. // A variable that is part of another variable (as an array or
  9311. // structure element) cannot appear in a private clause.
  9312. RefExpr = RefExpr->IgnoreParens();
  9313. enum {
  9314. NoArrayExpr = -1,
  9315. ArraySubscript = 0,
  9316. OMPArraySection = 1
  9317. } IsArrayExpr = NoArrayExpr;
  9318. if (AllowArraySection) {
  9319. if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
  9320. Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
  9321. while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
  9322. Base = TempASE->getBase()->IgnoreParenImpCasts();
  9323. RefExpr = Base;
  9324. IsArrayExpr = ArraySubscript;
  9325. } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
  9326. Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
  9327. while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
  9328. Base = TempOASE->getBase()->IgnoreParenImpCasts();
  9329. while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
  9330. Base = TempASE->getBase()->IgnoreParenImpCasts();
  9331. RefExpr = Base;
  9332. IsArrayExpr = OMPArraySection;
  9333. }
  9334. }
  9335. ELoc = RefExpr->getExprLoc();
  9336. ERange = RefExpr->getSourceRange();
  9337. RefExpr = RefExpr->IgnoreParenImpCasts();
  9338. auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
  9339. auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
  9340. if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
  9341. (S.getCurrentThisType().isNull() || !ME ||
  9342. !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
  9343. !isa<FieldDecl>(ME->getMemberDecl()))) {
  9344. if (IsArrayExpr != NoArrayExpr) {
  9345. S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
  9346. << ERange;
  9347. } else {
  9348. S.Diag(ELoc,
  9349. AllowArraySection
  9350. ? diag::err_omp_expected_var_name_member_expr_or_array_item
  9351. : diag::err_omp_expected_var_name_member_expr)
  9352. << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
  9353. }
  9354. return std::make_pair(nullptr, false);
  9355. }
  9356. return std::make_pair(
  9357. getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
  9358. }
  9359. OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
  9360. SourceLocation StartLoc,
  9361. SourceLocation LParenLoc,
  9362. SourceLocation EndLoc) {
  9363. SmallVector<Expr *, 8> Vars;
  9364. SmallVector<Expr *, 8> PrivateCopies;
  9365. for (Expr *RefExpr : VarList) {
  9366. assert(RefExpr && "NULL expr in OpenMP private clause.");
  9367. SourceLocation ELoc;
  9368. SourceRange ERange;
  9369. Expr *SimpleRefExpr = RefExpr;
  9370. auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
  9371. if (Res.second) {
  9372. // It will be analyzed later.
  9373. Vars.push_back(RefExpr);
  9374. PrivateCopies.push_back(nullptr);
  9375. }
  9376. ValueDecl *D = Res.first;
  9377. if (!D)
  9378. continue;
  9379. QualType Type = D->getType();
  9380. auto *VD = dyn_cast<VarDecl>(D);
  9381. // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
  9382. // A variable that appears in a private clause must not have an incomplete
  9383. // type or a reference type.
  9384. if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
  9385. continue;
  9386. Type = Type.getNonReferenceType();
  9387. // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
  9388. // A variable that is privatized must not have a const-qualified type
  9389. // unless it is of class type with a mutable member. This restriction does
  9390. // not apply to the firstprivate clause.
  9391. //
  9392. // OpenMP 3.1 [2.9.3.3, private clause, Restrictions]
  9393. // A variable that appears in a private clause must not have a
  9394. // const-qualified type unless it is of class type with a mutable member.
  9395. if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc))
  9396. continue;
  9397. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  9398. // in a Construct]
  9399. // Variables with the predetermined data-sharing attributes may not be
  9400. // listed in data-sharing attributes clauses, except for the cases
  9401. // listed below. For these exceptions only, listing a predetermined
  9402. // variable in a data-sharing attribute clause is allowed and overrides
  9403. // the variable's predetermined data-sharing attributes.
  9404. DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
  9405. if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
  9406. Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
  9407. << getOpenMPClauseName(OMPC_private);
  9408. reportOriginalDsa(*this, DSAStack, D, DVar);
  9409. continue;
  9410. }
  9411. OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
  9412. // Variably modified types are not supported for tasks.
  9413. if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
  9414. isOpenMPTaskingDirective(CurrDir)) {
  9415. Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
  9416. << getOpenMPClauseName(OMPC_private) << Type
  9417. << getOpenMPDirectiveName(CurrDir);
  9418. bool IsDecl =
  9419. !VD ||
  9420. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  9421. Diag(D->getLocation(),
  9422. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  9423. << D;
  9424. continue;
  9425. }
  9426. // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
  9427. // A list item cannot appear in both a map clause and a data-sharing
  9428. // attribute clause on the same construct
  9429. if (isOpenMPTargetExecutionDirective(CurrDir)) {
  9430. OpenMPClauseKind ConflictKind;
  9431. if (DSAStack->checkMappableExprComponentListsForDecl(
  9432. VD, /*CurrentRegionOnly=*/true,
  9433. [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
  9434. OpenMPClauseKind WhereFoundClauseKind) -> bool {
  9435. ConflictKind = WhereFoundClauseKind;
  9436. return true;
  9437. })) {
  9438. Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
  9439. << getOpenMPClauseName(OMPC_private)
  9440. << getOpenMPClauseName(ConflictKind)
  9441. << getOpenMPDirectiveName(CurrDir);
  9442. reportOriginalDsa(*this, DSAStack, D, DVar);
  9443. continue;
  9444. }
  9445. }
  9446. // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
  9447. // A variable of class type (or array thereof) that appears in a private
  9448. // clause requires an accessible, unambiguous default constructor for the
  9449. // class type.
  9450. // Generate helper private variable and initialize it with the default
  9451. // value. The address of the original variable is replaced by the address of
  9452. // the new private variable in CodeGen. This new variable is not added to
  9453. // IdResolver, so the code in the OpenMP region uses original variable for
  9454. // proper diagnostics.
  9455. Type = Type.getUnqualifiedType();
  9456. VarDecl *VDPrivate =
  9457. buildVarDecl(*this, ELoc, Type, D->getName(),
  9458. D->hasAttrs() ? &D->getAttrs() : nullptr,
  9459. VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
  9460. ActOnUninitializedDecl(VDPrivate);
  9461. if (VDPrivate->isInvalidDecl())
  9462. continue;
  9463. DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
  9464. *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
  9465. DeclRefExpr *Ref = nullptr;
  9466. if (!VD && !CurContext->isDependentContext())
  9467. Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
  9468. DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
  9469. Vars.push_back((VD || CurContext->isDependentContext())
  9470. ? RefExpr->IgnoreParens()
  9471. : Ref);
  9472. PrivateCopies.push_back(VDPrivateRefExpr);
  9473. }
  9474. if (Vars.empty())
  9475. return nullptr;
  9476. return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
  9477. PrivateCopies);
  9478. }
  9479. namespace {
  9480. class DiagsUninitializedSeveretyRAII {
  9481. private:
  9482. DiagnosticsEngine &Diags;
  9483. SourceLocation SavedLoc;
  9484. bool IsIgnored = false;
  9485. public:
  9486. DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
  9487. bool IsIgnored)
  9488. : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
  9489. if (!IsIgnored) {
  9490. Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
  9491. /*Map*/ diag::Severity::Ignored, Loc);
  9492. }
  9493. }
  9494. ~DiagsUninitializedSeveretyRAII() {
  9495. if (!IsIgnored)
  9496. Diags.popMappings(SavedLoc);
  9497. }
  9498. };
  9499. }
  9500. OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
  9501. SourceLocation StartLoc,
  9502. SourceLocation LParenLoc,
  9503. SourceLocation EndLoc) {
  9504. SmallVector<Expr *, 8> Vars;
  9505. SmallVector<Expr *, 8> PrivateCopies;
  9506. SmallVector<Expr *, 8> Inits;
  9507. SmallVector<Decl *, 4> ExprCaptures;
  9508. bool IsImplicitClause =
  9509. StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
  9510. SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
  9511. for (Expr *RefExpr : VarList) {
  9512. assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
  9513. SourceLocation ELoc;
  9514. SourceRange ERange;
  9515. Expr *SimpleRefExpr = RefExpr;
  9516. auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
  9517. if (Res.second) {
  9518. // It will be analyzed later.
  9519. Vars.push_back(RefExpr);
  9520. PrivateCopies.push_back(nullptr);
  9521. Inits.push_back(nullptr);
  9522. }
  9523. ValueDecl *D = Res.first;
  9524. if (!D)
  9525. continue;
  9526. ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
  9527. QualType Type = D->getType();
  9528. auto *VD = dyn_cast<VarDecl>(D);
  9529. // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
  9530. // A variable that appears in a private clause must not have an incomplete
  9531. // type or a reference type.
  9532. if (RequireCompleteType(ELoc, Type,
  9533. diag::err_omp_firstprivate_incomplete_type))
  9534. continue;
  9535. Type = Type.getNonReferenceType();
  9536. // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
  9537. // A variable of class type (or array thereof) that appears in a private
  9538. // clause requires an accessible, unambiguous copy constructor for the
  9539. // class type.
  9540. QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
  9541. // If an implicit firstprivate variable found it was checked already.
  9542. DSAStackTy::DSAVarData TopDVar;
  9543. if (!IsImplicitClause) {
  9544. DSAStackTy::DSAVarData DVar =
  9545. DSAStack->getTopDSA(D, /*FromParent=*/false);
  9546. TopDVar = DVar;
  9547. OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
  9548. bool IsConstant = ElemType.isConstant(Context);
  9549. // OpenMP [2.4.13, Data-sharing Attribute Clauses]
  9550. // A list item that specifies a given variable may not appear in more
  9551. // than one clause on the same directive, except that a variable may be
  9552. // specified in both firstprivate and lastprivate clauses.
  9553. // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
  9554. // A list item may appear in a firstprivate or lastprivate clause but not
  9555. // both.
  9556. if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
  9557. (isOpenMPDistributeDirective(CurrDir) ||
  9558. DVar.CKind != OMPC_lastprivate) &&
  9559. DVar.RefExpr) {
  9560. Diag(ELoc, diag::err_omp_wrong_dsa)
  9561. << getOpenMPClauseName(DVar.CKind)
  9562. << getOpenMPClauseName(OMPC_firstprivate);
  9563. reportOriginalDsa(*this, DSAStack, D, DVar);
  9564. continue;
  9565. }
  9566. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  9567. // in a Construct]
  9568. // Variables with the predetermined data-sharing attributes may not be
  9569. // listed in data-sharing attributes clauses, except for the cases
  9570. // listed below. For these exceptions only, listing a predetermined
  9571. // variable in a data-sharing attribute clause is allowed and overrides
  9572. // the variable's predetermined data-sharing attributes.
  9573. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  9574. // in a Construct, C/C++, p.2]
  9575. // Variables with const-qualified type having no mutable member may be
  9576. // listed in a firstprivate clause, even if they are static data members.
  9577. if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
  9578. DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
  9579. Diag(ELoc, diag::err_omp_wrong_dsa)
  9580. << getOpenMPClauseName(DVar.CKind)
  9581. << getOpenMPClauseName(OMPC_firstprivate);
  9582. reportOriginalDsa(*this, DSAStack, D, DVar);
  9583. continue;
  9584. }
  9585. // OpenMP [2.9.3.4, Restrictions, p.2]
  9586. // A list item that is private within a parallel region must not appear
  9587. // in a firstprivate clause on a worksharing construct if any of the
  9588. // worksharing regions arising from the worksharing construct ever bind
  9589. // to any of the parallel regions arising from the parallel construct.
  9590. // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
  9591. // A list item that is private within a teams region must not appear in a
  9592. // firstprivate clause on a distribute construct if any of the distribute
  9593. // regions arising from the distribute construct ever bind to any of the
  9594. // teams regions arising from the teams construct.
  9595. // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
  9596. // A list item that appears in a reduction clause of a teams construct
  9597. // must not appear in a firstprivate clause on a distribute construct if
  9598. // any of the distribute regions arising from the distribute construct
  9599. // ever bind to any of the teams regions arising from the teams construct.
  9600. if ((isOpenMPWorksharingDirective(CurrDir) ||
  9601. isOpenMPDistributeDirective(CurrDir)) &&
  9602. !isOpenMPParallelDirective(CurrDir) &&
  9603. !isOpenMPTeamsDirective(CurrDir)) {
  9604. DVar = DSAStack->getImplicitDSA(D, true);
  9605. if (DVar.CKind != OMPC_shared &&
  9606. (isOpenMPParallelDirective(DVar.DKind) ||
  9607. isOpenMPTeamsDirective(DVar.DKind) ||
  9608. DVar.DKind == OMPD_unknown)) {
  9609. Diag(ELoc, diag::err_omp_required_access)
  9610. << getOpenMPClauseName(OMPC_firstprivate)
  9611. << getOpenMPClauseName(OMPC_shared);
  9612. reportOriginalDsa(*this, DSAStack, D, DVar);
  9613. continue;
  9614. }
  9615. }
  9616. // OpenMP [2.9.3.4, Restrictions, p.3]
  9617. // A list item that appears in a reduction clause of a parallel construct
  9618. // must not appear in a firstprivate clause on a worksharing or task
  9619. // construct if any of the worksharing or task regions arising from the
  9620. // worksharing or task construct ever bind to any of the parallel regions
  9621. // arising from the parallel construct.
  9622. // OpenMP [2.9.3.4, Restrictions, p.4]
  9623. // A list item that appears in a reduction clause in worksharing
  9624. // construct must not appear in a firstprivate clause in a task construct
  9625. // encountered during execution of any of the worksharing regions arising
  9626. // from the worksharing construct.
  9627. if (isOpenMPTaskingDirective(CurrDir)) {
  9628. DVar = DSAStack->hasInnermostDSA(
  9629. D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
  9630. [](OpenMPDirectiveKind K) {
  9631. return isOpenMPParallelDirective(K) ||
  9632. isOpenMPWorksharingDirective(K) ||
  9633. isOpenMPTeamsDirective(K);
  9634. },
  9635. /*FromParent=*/true);
  9636. if (DVar.CKind == OMPC_reduction &&
  9637. (isOpenMPParallelDirective(DVar.DKind) ||
  9638. isOpenMPWorksharingDirective(DVar.DKind) ||
  9639. isOpenMPTeamsDirective(DVar.DKind))) {
  9640. Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
  9641. << getOpenMPDirectiveName(DVar.DKind);
  9642. reportOriginalDsa(*this, DSAStack, D, DVar);
  9643. continue;
  9644. }
  9645. }
  9646. // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
  9647. // A list item cannot appear in both a map clause and a data-sharing
  9648. // attribute clause on the same construct
  9649. if (isOpenMPTargetExecutionDirective(CurrDir)) {
  9650. OpenMPClauseKind ConflictKind;
  9651. if (DSAStack->checkMappableExprComponentListsForDecl(
  9652. VD, /*CurrentRegionOnly=*/true,
  9653. [&ConflictKind](
  9654. OMPClauseMappableExprCommon::MappableExprComponentListRef,
  9655. OpenMPClauseKind WhereFoundClauseKind) {
  9656. ConflictKind = WhereFoundClauseKind;
  9657. return true;
  9658. })) {
  9659. Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
  9660. << getOpenMPClauseName(OMPC_firstprivate)
  9661. << getOpenMPClauseName(ConflictKind)
  9662. << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
  9663. reportOriginalDsa(*this, DSAStack, D, DVar);
  9664. continue;
  9665. }
  9666. }
  9667. }
  9668. // Variably modified types are not supported for tasks.
  9669. if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
  9670. isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
  9671. Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
  9672. << getOpenMPClauseName(OMPC_firstprivate) << Type
  9673. << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
  9674. bool IsDecl =
  9675. !VD ||
  9676. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  9677. Diag(D->getLocation(),
  9678. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  9679. << D;
  9680. continue;
  9681. }
  9682. Type = Type.getUnqualifiedType();
  9683. VarDecl *VDPrivate =
  9684. buildVarDecl(*this, ELoc, Type, D->getName(),
  9685. D->hasAttrs() ? &D->getAttrs() : nullptr,
  9686. VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
  9687. // Generate helper private variable and initialize it with the value of the
  9688. // original variable. The address of the original variable is replaced by
  9689. // the address of the new private variable in the CodeGen. This new variable
  9690. // is not added to IdResolver, so the code in the OpenMP region uses
  9691. // original variable for proper diagnostics and variable capturing.
  9692. Expr *VDInitRefExpr = nullptr;
  9693. // For arrays generate initializer for single element and replace it by the
  9694. // original array element in CodeGen.
  9695. if (Type->isArrayType()) {
  9696. VarDecl *VDInit =
  9697. buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
  9698. VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
  9699. Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
  9700. ElemType = ElemType.getUnqualifiedType();
  9701. VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
  9702. ".firstprivate.temp");
  9703. InitializedEntity Entity =
  9704. InitializedEntity::InitializeVariable(VDInitTemp);
  9705. InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
  9706. InitializationSequence InitSeq(*this, Entity, Kind, Init);
  9707. ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
  9708. if (Result.isInvalid())
  9709. VDPrivate->setInvalidDecl();
  9710. else
  9711. VDPrivate->setInit(Result.getAs<Expr>());
  9712. // Remove temp variable declaration.
  9713. Context.Deallocate(VDInitTemp);
  9714. } else {
  9715. VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
  9716. ".firstprivate.temp");
  9717. VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
  9718. RefExpr->getExprLoc());
  9719. AddInitializerToDecl(VDPrivate,
  9720. DefaultLvalueConversion(VDInitRefExpr).get(),
  9721. /*DirectInit=*/false);
  9722. }
  9723. if (VDPrivate->isInvalidDecl()) {
  9724. if (IsImplicitClause) {
  9725. Diag(RefExpr->getExprLoc(),
  9726. diag::note_omp_task_predetermined_firstprivate_here);
  9727. }
  9728. continue;
  9729. }
  9730. CurContext->addDecl(VDPrivate);
  9731. DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
  9732. *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
  9733. RefExpr->getExprLoc());
  9734. DeclRefExpr *Ref = nullptr;
  9735. if (!VD && !CurContext->isDependentContext()) {
  9736. if (TopDVar.CKind == OMPC_lastprivate) {
  9737. Ref = TopDVar.PrivateCopy;
  9738. } else {
  9739. Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
  9740. if (!isOpenMPCapturedDecl(D))
  9741. ExprCaptures.push_back(Ref->getDecl());
  9742. }
  9743. }
  9744. DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
  9745. Vars.push_back((VD || CurContext->isDependentContext())
  9746. ? RefExpr->IgnoreParens()
  9747. : Ref);
  9748. PrivateCopies.push_back(VDPrivateRefExpr);
  9749. Inits.push_back(VDInitRefExpr);
  9750. }
  9751. if (Vars.empty())
  9752. return nullptr;
  9753. return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
  9754. Vars, PrivateCopies, Inits,
  9755. buildPreInits(Context, ExprCaptures));
  9756. }
  9757. OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
  9758. SourceLocation StartLoc,
  9759. SourceLocation LParenLoc,
  9760. SourceLocation EndLoc) {
  9761. SmallVector<Expr *, 8> Vars;
  9762. SmallVector<Expr *, 8> SrcExprs;
  9763. SmallVector<Expr *, 8> DstExprs;
  9764. SmallVector<Expr *, 8> AssignmentOps;
  9765. SmallVector<Decl *, 4> ExprCaptures;
  9766. SmallVector<Expr *, 4> ExprPostUpdates;
  9767. for (Expr *RefExpr : VarList) {
  9768. assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
  9769. SourceLocation ELoc;
  9770. SourceRange ERange;
  9771. Expr *SimpleRefExpr = RefExpr;
  9772. auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
  9773. if (Res.second) {
  9774. // It will be analyzed later.
  9775. Vars.push_back(RefExpr);
  9776. SrcExprs.push_back(nullptr);
  9777. DstExprs.push_back(nullptr);
  9778. AssignmentOps.push_back(nullptr);
  9779. }
  9780. ValueDecl *D = Res.first;
  9781. if (!D)
  9782. continue;
  9783. QualType Type = D->getType();
  9784. auto *VD = dyn_cast<VarDecl>(D);
  9785. // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
  9786. // A variable that appears in a lastprivate clause must not have an
  9787. // incomplete type or a reference type.
  9788. if (RequireCompleteType(ELoc, Type,
  9789. diag::err_omp_lastprivate_incomplete_type))
  9790. continue;
  9791. Type = Type.getNonReferenceType();
  9792. // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
  9793. // A variable that is privatized must not have a const-qualified type
  9794. // unless it is of class type with a mutable member. This restriction does
  9795. // not apply to the firstprivate clause.
  9796. //
  9797. // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions]
  9798. // A variable that appears in a lastprivate clause must not have a
  9799. // const-qualified type unless it is of class type with a mutable member.
  9800. if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc))
  9801. continue;
  9802. OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
  9803. // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
  9804. // in a Construct]
  9805. // Variables with the predetermined data-sharing attributes may not be
  9806. // listed in data-sharing attributes clauses, except for the cases
  9807. // listed below.
  9808. // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
  9809. // A list item may appear in a firstprivate or lastprivate clause but not
  9810. // both.
  9811. DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
  9812. if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
  9813. (isOpenMPDistributeDirective(CurrDir) ||
  9814. DVar.CKind != OMPC_firstprivate) &&
  9815. (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
  9816. Diag(ELoc, diag::err_omp_wrong_dsa)
  9817. << getOpenMPClauseName(DVar.CKind)
  9818. << getOpenMPClauseName(OMPC_lastprivate);
  9819. reportOriginalDsa(*this, DSAStack, D, DVar);
  9820. continue;
  9821. }
  9822. // OpenMP [2.14.3.5, Restrictions, p.2]
  9823. // A list item that is private within a parallel region, or that appears in
  9824. // the reduction clause of a parallel construct, must not appear in a
  9825. // lastprivate clause on a worksharing construct if any of the corresponding
  9826. // worksharing regions ever binds to any of the corresponding parallel
  9827. // regions.
  9828. DSAStackTy::DSAVarData TopDVar = DVar;
  9829. if (isOpenMPWorksharingDirective(CurrDir) &&
  9830. !isOpenMPParallelDirective(CurrDir) &&
  9831. !isOpenMPTeamsDirective(CurrDir)) {
  9832. DVar = DSAStack->getImplicitDSA(D, true);
  9833. if (DVar.CKind != OMPC_shared) {
  9834. Diag(ELoc, diag::err_omp_required_access)
  9835. << getOpenMPClauseName(OMPC_lastprivate)
  9836. << getOpenMPClauseName(OMPC_shared);
  9837. reportOriginalDsa(*this, DSAStack, D, DVar);
  9838. continue;
  9839. }
  9840. }
  9841. // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
  9842. // A variable of class type (or array thereof) that appears in a
  9843. // lastprivate clause requires an accessible, unambiguous default
  9844. // constructor for the class type, unless the list item is also specified
  9845. // in a firstprivate clause.
  9846. // A variable of class type (or array thereof) that appears in a
  9847. // lastprivate clause requires an accessible, unambiguous copy assignment
  9848. // operator for the class type.
  9849. Type = Context.getBaseElementType(Type).getNonReferenceType();
  9850. VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
  9851. Type.getUnqualifiedType(), ".lastprivate.src",
  9852. D->hasAttrs() ? &D->getAttrs() : nullptr);
  9853. DeclRefExpr *PseudoSrcExpr =
  9854. buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
  9855. VarDecl *DstVD =
  9856. buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
  9857. D->hasAttrs() ? &D->getAttrs() : nullptr);
  9858. DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
  9859. // For arrays generate assignment operation for single element and replace
  9860. // it by the original array element in CodeGen.
  9861. ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
  9862. PseudoDstExpr, PseudoSrcExpr);
  9863. if (AssignmentOp.isInvalid())
  9864. continue;
  9865. AssignmentOp =
  9866. ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
  9867. if (AssignmentOp.isInvalid())
  9868. continue;
  9869. DeclRefExpr *Ref = nullptr;
  9870. if (!VD && !CurContext->isDependentContext()) {
  9871. if (TopDVar.CKind == OMPC_firstprivate) {
  9872. Ref = TopDVar.PrivateCopy;
  9873. } else {
  9874. Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
  9875. if (!isOpenMPCapturedDecl(D))
  9876. ExprCaptures.push_back(Ref->getDecl());
  9877. }
  9878. if (TopDVar.CKind == OMPC_firstprivate ||
  9879. (!isOpenMPCapturedDecl(D) &&
  9880. Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
  9881. ExprResult RefRes = DefaultLvalueConversion(Ref);
  9882. if (!RefRes.isUsable())
  9883. continue;
  9884. ExprResult PostUpdateRes =
  9885. BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
  9886. RefRes.get());
  9887. if (!PostUpdateRes.isUsable())
  9888. continue;
  9889. ExprPostUpdates.push_back(
  9890. IgnoredValueConversions(PostUpdateRes.get()).get());
  9891. }
  9892. }
  9893. DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
  9894. Vars.push_back((VD || CurContext->isDependentContext())
  9895. ? RefExpr->IgnoreParens()
  9896. : Ref);
  9897. SrcExprs.push_back(PseudoSrcExpr);
  9898. DstExprs.push_back(PseudoDstExpr);
  9899. AssignmentOps.push_back(AssignmentOp.get());
  9900. }
  9901. if (Vars.empty())
  9902. return nullptr;
  9903. return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
  9904. Vars, SrcExprs, DstExprs, AssignmentOps,
  9905. buildPreInits(Context, ExprCaptures),
  9906. buildPostUpdate(*this, ExprPostUpdates));
  9907. }
  9908. OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
  9909. SourceLocation StartLoc,
  9910. SourceLocation LParenLoc,
  9911. SourceLocation EndLoc) {
  9912. SmallVector<Expr *, 8> Vars;
  9913. for (Expr *RefExpr : VarList) {
  9914. assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
  9915. SourceLocation ELoc;
  9916. SourceRange ERange;
  9917. Expr *SimpleRefExpr = RefExpr;
  9918. auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
  9919. if (Res.second) {
  9920. // It will be analyzed later.
  9921. Vars.push_back(RefExpr);
  9922. }
  9923. ValueDecl *D = Res.first;
  9924. if (!D)
  9925. continue;
  9926. auto *VD = dyn_cast<VarDecl>(D);
  9927. // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
  9928. // in a Construct]
  9929. // Variables with the predetermined data-sharing attributes may not be
  9930. // listed in data-sharing attributes clauses, except for the cases
  9931. // listed below. For these exceptions only, listing a predetermined
  9932. // variable in a data-sharing attribute clause is allowed and overrides
  9933. // the variable's predetermined data-sharing attributes.
  9934. DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
  9935. if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
  9936. DVar.RefExpr) {
  9937. Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
  9938. << getOpenMPClauseName(OMPC_shared);
  9939. reportOriginalDsa(*this, DSAStack, D, DVar);
  9940. continue;
  9941. }
  9942. DeclRefExpr *Ref = nullptr;
  9943. if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
  9944. Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
  9945. DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
  9946. Vars.push_back((VD || !Ref || CurContext->isDependentContext())
  9947. ? RefExpr->IgnoreParens()
  9948. : Ref);
  9949. }
  9950. if (Vars.empty())
  9951. return nullptr;
  9952. return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
  9953. }
  9954. namespace {
  9955. class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
  9956. DSAStackTy *Stack;
  9957. public:
  9958. bool VisitDeclRefExpr(DeclRefExpr *E) {
  9959. if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
  9960. DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
  9961. if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
  9962. return false;
  9963. if (DVar.CKind != OMPC_unknown)
  9964. return true;
  9965. DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
  9966. VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
  9967. /*FromParent=*/true);
  9968. return DVarPrivate.CKind != OMPC_unknown;
  9969. }
  9970. return false;
  9971. }
  9972. bool VisitStmt(Stmt *S) {
  9973. for (Stmt *Child : S->children()) {
  9974. if (Child && Visit(Child))
  9975. return true;
  9976. }
  9977. return false;
  9978. }
  9979. explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
  9980. };
  9981. } // namespace
  9982. namespace {
  9983. // Transform MemberExpression for specified FieldDecl of current class to
  9984. // DeclRefExpr to specified OMPCapturedExprDecl.
  9985. class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
  9986. typedef TreeTransform<TransformExprToCaptures> BaseTransform;
  9987. ValueDecl *Field = nullptr;
  9988. DeclRefExpr *CapturedExpr = nullptr;
  9989. public:
  9990. TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
  9991. : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
  9992. ExprResult TransformMemberExpr(MemberExpr *E) {
  9993. if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
  9994. E->getMemberDecl() == Field) {
  9995. CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
  9996. return CapturedExpr;
  9997. }
  9998. return BaseTransform::TransformMemberExpr(E);
  9999. }
  10000. DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
  10001. };
  10002. } // namespace
  10003. template <typename T, typename U>
  10004. static T filterLookupForUDReductionAndMapper(
  10005. SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) {
  10006. for (U &Set : Lookups) {
  10007. for (auto *D : Set) {
  10008. if (T Res = Gen(cast<ValueDecl>(D)))
  10009. return Res;
  10010. }
  10011. }
  10012. return T();
  10013. }
  10014. static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
  10015. assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
  10016. for (auto RD : D->redecls()) {
  10017. // Don't bother with extra checks if we already know this one isn't visible.
  10018. if (RD == D)
  10019. continue;
  10020. auto ND = cast<NamedDecl>(RD);
  10021. if (LookupResult::isVisible(SemaRef, ND))
  10022. return ND;
  10023. }
  10024. return nullptr;
  10025. }
  10026. static void
  10027. argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id,
  10028. SourceLocation Loc, QualType Ty,
  10029. SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
  10030. // Find all of the associated namespaces and classes based on the
  10031. // arguments we have.
  10032. Sema::AssociatedNamespaceSet AssociatedNamespaces;
  10033. Sema::AssociatedClassSet AssociatedClasses;
  10034. OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
  10035. SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
  10036. AssociatedClasses);
  10037. // C++ [basic.lookup.argdep]p3:
  10038. // Let X be the lookup set produced by unqualified lookup (3.4.1)
  10039. // and let Y be the lookup set produced by argument dependent
  10040. // lookup (defined as follows). If X contains [...] then Y is
  10041. // empty. Otherwise Y is the set of declarations found in the
  10042. // namespaces associated with the argument types as described
  10043. // below. The set of declarations found by the lookup of the name
  10044. // is the union of X and Y.
  10045. //
  10046. // Here, we compute Y and add its members to the overloaded
  10047. // candidate set.
  10048. for (auto *NS : AssociatedNamespaces) {
  10049. // When considering an associated namespace, the lookup is the
  10050. // same as the lookup performed when the associated namespace is
  10051. // used as a qualifier (3.4.3.2) except that:
  10052. //
  10053. // -- Any using-directives in the associated namespace are
  10054. // ignored.
  10055. //
  10056. // -- Any namespace-scope friend functions declared in
  10057. // associated classes are visible within their respective
  10058. // namespaces even if they are not visible during an ordinary
  10059. // lookup (11.4).
  10060. DeclContext::lookup_result R = NS->lookup(Id.getName());
  10061. for (auto *D : R) {
  10062. auto *Underlying = D;
  10063. if (auto *USD = dyn_cast<UsingShadowDecl>(D))
  10064. Underlying = USD->getTargetDecl();
  10065. if (!isa<OMPDeclareReductionDecl>(Underlying) &&
  10066. !isa<OMPDeclareMapperDecl>(Underlying))
  10067. continue;
  10068. if (!SemaRef.isVisible(D)) {
  10069. D = findAcceptableDecl(SemaRef, D);
  10070. if (!D)
  10071. continue;
  10072. if (auto *USD = dyn_cast<UsingShadowDecl>(D))
  10073. Underlying = USD->getTargetDecl();
  10074. }
  10075. Lookups.emplace_back();
  10076. Lookups.back().addDecl(Underlying);
  10077. }
  10078. }
  10079. }
  10080. static ExprResult
  10081. buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
  10082. Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
  10083. const DeclarationNameInfo &ReductionId, QualType Ty,
  10084. CXXCastPath &BasePath, Expr *UnresolvedReduction) {
  10085. if (ReductionIdScopeSpec.isInvalid())
  10086. return ExprError();
  10087. SmallVector<UnresolvedSet<8>, 4> Lookups;
  10088. if (S) {
  10089. LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
  10090. Lookup.suppressDiagnostics();
  10091. while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
  10092. NamedDecl *D = Lookup.getRepresentativeDecl();
  10093. do {
  10094. S = S->getParent();
  10095. } while (S && !S->isDeclScope(D));
  10096. if (S)
  10097. S = S->getParent();
  10098. Lookups.emplace_back();
  10099. Lookups.back().append(Lookup.begin(), Lookup.end());
  10100. Lookup.clear();
  10101. }
  10102. } else if (auto *ULE =
  10103. cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
  10104. Lookups.push_back(UnresolvedSet<8>());
  10105. Decl *PrevD = nullptr;
  10106. for (NamedDecl *D : ULE->decls()) {
  10107. if (D == PrevD)
  10108. Lookups.push_back(UnresolvedSet<8>());
  10109. else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
  10110. Lookups.back().addDecl(DRD);
  10111. PrevD = D;
  10112. }
  10113. }
  10114. if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
  10115. Ty->isInstantiationDependentType() ||
  10116. Ty->containsUnexpandedParameterPack() ||
  10117. filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
  10118. return !D->isInvalidDecl() &&
  10119. (D->getType()->isDependentType() ||
  10120. D->getType()->isInstantiationDependentType() ||
  10121. D->getType()->containsUnexpandedParameterPack());
  10122. })) {
  10123. UnresolvedSet<8> ResSet;
  10124. for (const UnresolvedSet<8> &Set : Lookups) {
  10125. if (Set.empty())
  10126. continue;
  10127. ResSet.append(Set.begin(), Set.end());
  10128. // The last item marks the end of all declarations at the specified scope.
  10129. ResSet.addDecl(Set[Set.size() - 1]);
  10130. }
  10131. return UnresolvedLookupExpr::Create(
  10132. SemaRef.Context, /*NamingClass=*/nullptr,
  10133. ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
  10134. /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
  10135. }
  10136. // Lookup inside the classes.
  10137. // C++ [over.match.oper]p3:
  10138. // For a unary operator @ with an operand of a type whose
  10139. // cv-unqualified version is T1, and for a binary operator @ with
  10140. // a left operand of a type whose cv-unqualified version is T1 and
  10141. // a right operand of a type whose cv-unqualified version is T2,
  10142. // three sets of candidate functions, designated member
  10143. // candidates, non-member candidates and built-in candidates, are
  10144. // constructed as follows:
  10145. // -- If T1 is a complete class type or a class currently being
  10146. // defined, the set of member candidates is the result of the
  10147. // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
  10148. // the set of member candidates is empty.
  10149. LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
  10150. Lookup.suppressDiagnostics();
  10151. if (const auto *TyRec = Ty->getAs<RecordType>()) {
  10152. // Complete the type if it can be completed.
  10153. // If the type is neither complete nor being defined, bail out now.
  10154. if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
  10155. TyRec->getDecl()->getDefinition()) {
  10156. Lookup.clear();
  10157. SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
  10158. if (Lookup.empty()) {
  10159. Lookups.emplace_back();
  10160. Lookups.back().append(Lookup.begin(), Lookup.end());
  10161. }
  10162. }
  10163. }
  10164. // Perform ADL.
  10165. argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
  10166. if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
  10167. Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
  10168. if (!D->isInvalidDecl() &&
  10169. SemaRef.Context.hasSameType(D->getType(), Ty))
  10170. return D;
  10171. return nullptr;
  10172. }))
  10173. return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(),
  10174. VK_LValue, Loc);
  10175. if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
  10176. Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
  10177. if (!D->isInvalidDecl() &&
  10178. SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
  10179. !Ty.isMoreQualifiedThan(D->getType()))
  10180. return D;
  10181. return nullptr;
  10182. })) {
  10183. CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
  10184. /*DetectVirtual=*/false);
  10185. if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
  10186. if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
  10187. VD->getType().getUnqualifiedType()))) {
  10188. if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
  10189. /*DiagID=*/0) !=
  10190. Sema::AR_inaccessible) {
  10191. SemaRef.BuildBasePathArray(Paths, BasePath);
  10192. return SemaRef.BuildDeclRefExpr(
  10193. VD, VD->getType().getNonReferenceType(), VK_LValue, Loc);
  10194. }
  10195. }
  10196. }
  10197. }
  10198. if (ReductionIdScopeSpec.isSet()) {
  10199. SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
  10200. return ExprError();
  10201. }
  10202. return ExprEmpty();
  10203. }
  10204. namespace {
  10205. /// Data for the reduction-based clauses.
  10206. struct ReductionData {
  10207. /// List of original reduction items.
  10208. SmallVector<Expr *, 8> Vars;
  10209. /// List of private copies of the reduction items.
  10210. SmallVector<Expr *, 8> Privates;
  10211. /// LHS expressions for the reduction_op expressions.
  10212. SmallVector<Expr *, 8> LHSs;
  10213. /// RHS expressions for the reduction_op expressions.
  10214. SmallVector<Expr *, 8> RHSs;
  10215. /// Reduction operation expression.
  10216. SmallVector<Expr *, 8> ReductionOps;
  10217. /// Taskgroup descriptors for the corresponding reduction items in
  10218. /// in_reduction clauses.
  10219. SmallVector<Expr *, 8> TaskgroupDescriptors;
  10220. /// List of captures for clause.
  10221. SmallVector<Decl *, 4> ExprCaptures;
  10222. /// List of postupdate expressions.
  10223. SmallVector<Expr *, 4> ExprPostUpdates;
  10224. ReductionData() = delete;
  10225. /// Reserves required memory for the reduction data.
  10226. ReductionData(unsigned Size) {
  10227. Vars.reserve(Size);
  10228. Privates.reserve(Size);
  10229. LHSs.reserve(Size);
  10230. RHSs.reserve(Size);
  10231. ReductionOps.reserve(Size);
  10232. TaskgroupDescriptors.reserve(Size);
  10233. ExprCaptures.reserve(Size);
  10234. ExprPostUpdates.reserve(Size);
  10235. }
  10236. /// Stores reduction item and reduction operation only (required for dependent
  10237. /// reduction item).
  10238. void push(Expr *Item, Expr *ReductionOp) {
  10239. Vars.emplace_back(Item);
  10240. Privates.emplace_back(nullptr);
  10241. LHSs.emplace_back(nullptr);
  10242. RHSs.emplace_back(nullptr);
  10243. ReductionOps.emplace_back(ReductionOp);
  10244. TaskgroupDescriptors.emplace_back(nullptr);
  10245. }
  10246. /// Stores reduction data.
  10247. void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
  10248. Expr *TaskgroupDescriptor) {
  10249. Vars.emplace_back(Item);
  10250. Privates.emplace_back(Private);
  10251. LHSs.emplace_back(LHS);
  10252. RHSs.emplace_back(RHS);
  10253. ReductionOps.emplace_back(ReductionOp);
  10254. TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
  10255. }
  10256. };
  10257. } // namespace
  10258. static bool checkOMPArraySectionConstantForReduction(
  10259. ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
  10260. SmallVectorImpl<llvm::APSInt> &ArraySizes) {
  10261. const Expr *Length = OASE->getLength();
  10262. if (Length == nullptr) {
  10263. // For array sections of the form [1:] or [:], we would need to analyze
  10264. // the lower bound...
  10265. if (OASE->getColonLoc().isValid())
  10266. return false;
  10267. // This is an array subscript which has implicit length 1!
  10268. SingleElement = true;
  10269. ArraySizes.push_back(llvm::APSInt::get(1));
  10270. } else {
  10271. Expr::EvalResult Result;
  10272. if (!Length->EvaluateAsInt(Result, Context))
  10273. return false;
  10274. llvm::APSInt ConstantLengthValue = Result.Val.getInt();
  10275. SingleElement = (ConstantLengthValue.getSExtValue() == 1);
  10276. ArraySizes.push_back(ConstantLengthValue);
  10277. }
  10278. // Get the base of this array section and walk up from there.
  10279. const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
  10280. // We require length = 1 for all array sections except the right-most to
  10281. // guarantee that the memory region is contiguous and has no holes in it.
  10282. while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
  10283. Length = TempOASE->getLength();
  10284. if (Length == nullptr) {
  10285. // For array sections of the form [1:] or [:], we would need to analyze
  10286. // the lower bound...
  10287. if (OASE->getColonLoc().isValid())
  10288. return false;
  10289. // This is an array subscript which has implicit length 1!
  10290. ArraySizes.push_back(llvm::APSInt::get(1));
  10291. } else {
  10292. Expr::EvalResult Result;
  10293. if (!Length->EvaluateAsInt(Result, Context))
  10294. return false;
  10295. llvm::APSInt ConstantLengthValue = Result.Val.getInt();
  10296. if (ConstantLengthValue.getSExtValue() != 1)
  10297. return false;
  10298. ArraySizes.push_back(ConstantLengthValue);
  10299. }
  10300. Base = TempOASE->getBase()->IgnoreParenImpCasts();
  10301. }
  10302. // If we have a single element, we don't need to add the implicit lengths.
  10303. if (!SingleElement) {
  10304. while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
  10305. // Has implicit length 1!
  10306. ArraySizes.push_back(llvm::APSInt::get(1));
  10307. Base = TempASE->getBase()->IgnoreParenImpCasts();
  10308. }
  10309. }
  10310. // This array section can be privatized as a single value or as a constant
  10311. // sized array.
  10312. return true;
  10313. }
  10314. static bool actOnOMPReductionKindClause(
  10315. Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
  10316. ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
  10317. SourceLocation ColonLoc, SourceLocation EndLoc,
  10318. CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
  10319. ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
  10320. DeclarationName DN = ReductionId.getName();
  10321. OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
  10322. BinaryOperatorKind BOK = BO_Comma;
  10323. ASTContext &Context = S.Context;
  10324. // OpenMP [2.14.3.6, reduction clause]
  10325. // C
  10326. // reduction-identifier is either an identifier or one of the following
  10327. // operators: +, -, *, &, |, ^, && and ||
  10328. // C++
  10329. // reduction-identifier is either an id-expression or one of the following
  10330. // operators: +, -, *, &, |, ^, && and ||
  10331. switch (OOK) {
  10332. case OO_Plus:
  10333. case OO_Minus:
  10334. BOK = BO_Add;
  10335. break;
  10336. case OO_Star:
  10337. BOK = BO_Mul;
  10338. break;
  10339. case OO_Amp:
  10340. BOK = BO_And;
  10341. break;
  10342. case OO_Pipe:
  10343. BOK = BO_Or;
  10344. break;
  10345. case OO_Caret:
  10346. BOK = BO_Xor;
  10347. break;
  10348. case OO_AmpAmp:
  10349. BOK = BO_LAnd;
  10350. break;
  10351. case OO_PipePipe:
  10352. BOK = BO_LOr;
  10353. break;
  10354. case OO_New:
  10355. case OO_Delete:
  10356. case OO_Array_New:
  10357. case OO_Array_Delete:
  10358. case OO_Slash:
  10359. case OO_Percent:
  10360. case OO_Tilde:
  10361. case OO_Exclaim:
  10362. case OO_Equal:
  10363. case OO_Less:
  10364. case OO_Greater:
  10365. case OO_LessEqual:
  10366. case OO_GreaterEqual:
  10367. case OO_PlusEqual:
  10368. case OO_MinusEqual:
  10369. case OO_StarEqual:
  10370. case OO_SlashEqual:
  10371. case OO_PercentEqual:
  10372. case OO_CaretEqual:
  10373. case OO_AmpEqual:
  10374. case OO_PipeEqual:
  10375. case OO_LessLess:
  10376. case OO_GreaterGreater:
  10377. case OO_LessLessEqual:
  10378. case OO_GreaterGreaterEqual:
  10379. case OO_EqualEqual:
  10380. case OO_ExclaimEqual:
  10381. case OO_Spaceship:
  10382. case OO_PlusPlus:
  10383. case OO_MinusMinus:
  10384. case OO_Comma:
  10385. case OO_ArrowStar:
  10386. case OO_Arrow:
  10387. case OO_Call:
  10388. case OO_Subscript:
  10389. case OO_Conditional:
  10390. case OO_Coawait:
  10391. case NUM_OVERLOADED_OPERATORS:
  10392. llvm_unreachable("Unexpected reduction identifier");
  10393. case OO_None:
  10394. if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
  10395. if (II->isStr("max"))
  10396. BOK = BO_GT;
  10397. else if (II->isStr("min"))
  10398. BOK = BO_LT;
  10399. }
  10400. break;
  10401. }
  10402. SourceRange ReductionIdRange;
  10403. if (ReductionIdScopeSpec.isValid())
  10404. ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
  10405. else
  10406. ReductionIdRange.setBegin(ReductionId.getBeginLoc());
  10407. ReductionIdRange.setEnd(ReductionId.getEndLoc());
  10408. auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
  10409. bool FirstIter = true;
  10410. for (Expr *RefExpr : VarList) {
  10411. assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
  10412. // OpenMP [2.1, C/C++]
  10413. // A list item is a variable or array section, subject to the restrictions
  10414. // specified in Section 2.4 on page 42 and in each of the sections
  10415. // describing clauses and directives for which a list appears.
  10416. // OpenMP [2.14.3.3, Restrictions, p.1]
  10417. // A variable that is part of another variable (as an array or
  10418. // structure element) cannot appear in a private clause.
  10419. if (!FirstIter && IR != ER)
  10420. ++IR;
  10421. FirstIter = false;
  10422. SourceLocation ELoc;
  10423. SourceRange ERange;
  10424. Expr *SimpleRefExpr = RefExpr;
  10425. auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
  10426. /*AllowArraySection=*/true);
  10427. if (Res.second) {
  10428. // Try to find 'declare reduction' corresponding construct before using
  10429. // builtin/overloaded operators.
  10430. QualType Type = Context.DependentTy;
  10431. CXXCastPath BasePath;
  10432. ExprResult DeclareReductionRef = buildDeclareReductionRef(
  10433. S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
  10434. ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
  10435. Expr *ReductionOp = nullptr;
  10436. if (S.CurContext->isDependentContext() &&
  10437. (DeclareReductionRef.isUnset() ||
  10438. isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
  10439. ReductionOp = DeclareReductionRef.get();
  10440. // It will be analyzed later.
  10441. RD.push(RefExpr, ReductionOp);
  10442. }
  10443. ValueDecl *D = Res.first;
  10444. if (!D)
  10445. continue;
  10446. Expr *TaskgroupDescriptor = nullptr;
  10447. QualType Type;
  10448. auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
  10449. auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
  10450. if (ASE) {
  10451. Type = ASE->getType().getNonReferenceType();
  10452. } else if (OASE) {
  10453. QualType BaseType =
  10454. OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
  10455. if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
  10456. Type = ATy->getElementType();
  10457. else
  10458. Type = BaseType->getPointeeType();
  10459. Type = Type.getNonReferenceType();
  10460. } else {
  10461. Type = Context.getBaseElementType(D->getType().getNonReferenceType());
  10462. }
  10463. auto *VD = dyn_cast<VarDecl>(D);
  10464. // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
  10465. // A variable that appears in a private clause must not have an incomplete
  10466. // type or a reference type.
  10467. if (S.RequireCompleteType(ELoc, D->getType(),
  10468. diag::err_omp_reduction_incomplete_type))
  10469. continue;
  10470. // OpenMP [2.14.3.6, reduction clause, Restrictions]
  10471. // A list item that appears in a reduction clause must not be
  10472. // const-qualified.
  10473. if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc,
  10474. /*AcceptIfMutable*/ false, ASE || OASE))
  10475. continue;
  10476. OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
  10477. // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
  10478. // If a list-item is a reference type then it must bind to the same object
  10479. // for all threads of the team.
  10480. if (!ASE && !OASE) {
  10481. if (VD) {
  10482. VarDecl *VDDef = VD->getDefinition();
  10483. if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
  10484. DSARefChecker Check(Stack);
  10485. if (Check.Visit(VDDef->getInit())) {
  10486. S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
  10487. << getOpenMPClauseName(ClauseKind) << ERange;
  10488. S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
  10489. continue;
  10490. }
  10491. }
  10492. }
  10493. // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
  10494. // in a Construct]
  10495. // Variables with the predetermined data-sharing attributes may not be
  10496. // listed in data-sharing attributes clauses, except for the cases
  10497. // listed below. For these exceptions only, listing a predetermined
  10498. // variable in a data-sharing attribute clause is allowed and overrides
  10499. // the variable's predetermined data-sharing attributes.
  10500. // OpenMP [2.14.3.6, Restrictions, p.3]
  10501. // Any number of reduction clauses can be specified on the directive,
  10502. // but a list item can appear only once in the reduction clauses for that
  10503. // directive.
  10504. DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
  10505. if (DVar.CKind == OMPC_reduction) {
  10506. S.Diag(ELoc, diag::err_omp_once_referenced)
  10507. << getOpenMPClauseName(ClauseKind);
  10508. if (DVar.RefExpr)
  10509. S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
  10510. continue;
  10511. }
  10512. if (DVar.CKind != OMPC_unknown) {
  10513. S.Diag(ELoc, diag::err_omp_wrong_dsa)
  10514. << getOpenMPClauseName(DVar.CKind)
  10515. << getOpenMPClauseName(OMPC_reduction);
  10516. reportOriginalDsa(S, Stack, D, DVar);
  10517. continue;
  10518. }
  10519. // OpenMP [2.14.3.6, Restrictions, p.1]
  10520. // A list item that appears in a reduction clause of a worksharing
  10521. // construct must be shared in the parallel regions to which any of the
  10522. // worksharing regions arising from the worksharing construct bind.
  10523. if (isOpenMPWorksharingDirective(CurrDir) &&
  10524. !isOpenMPParallelDirective(CurrDir) &&
  10525. !isOpenMPTeamsDirective(CurrDir)) {
  10526. DVar = Stack->getImplicitDSA(D, true);
  10527. if (DVar.CKind != OMPC_shared) {
  10528. S.Diag(ELoc, diag::err_omp_required_access)
  10529. << getOpenMPClauseName(OMPC_reduction)
  10530. << getOpenMPClauseName(OMPC_shared);
  10531. reportOriginalDsa(S, Stack, D, DVar);
  10532. continue;
  10533. }
  10534. }
  10535. }
  10536. // Try to find 'declare reduction' corresponding construct before using
  10537. // builtin/overloaded operators.
  10538. CXXCastPath BasePath;
  10539. ExprResult DeclareReductionRef = buildDeclareReductionRef(
  10540. S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
  10541. ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
  10542. if (DeclareReductionRef.isInvalid())
  10543. continue;
  10544. if (S.CurContext->isDependentContext() &&
  10545. (DeclareReductionRef.isUnset() ||
  10546. isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
  10547. RD.push(RefExpr, DeclareReductionRef.get());
  10548. continue;
  10549. }
  10550. if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
  10551. // Not allowed reduction identifier is found.
  10552. S.Diag(ReductionId.getBeginLoc(),
  10553. diag::err_omp_unknown_reduction_identifier)
  10554. << Type << ReductionIdRange;
  10555. continue;
  10556. }
  10557. // OpenMP [2.14.3.6, reduction clause, Restrictions]
  10558. // The type of a list item that appears in a reduction clause must be valid
  10559. // for the reduction-identifier. For a max or min reduction in C, the type
  10560. // of the list item must be an allowed arithmetic data type: char, int,
  10561. // float, double, or _Bool, possibly modified with long, short, signed, or
  10562. // unsigned. For a max or min reduction in C++, the type of the list item
  10563. // must be an allowed arithmetic data type: char, wchar_t, int, float,
  10564. // double, or bool, possibly modified with long, short, signed, or unsigned.
  10565. if (DeclareReductionRef.isUnset()) {
  10566. if ((BOK == BO_GT || BOK == BO_LT) &&
  10567. !(Type->isScalarType() ||
  10568. (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
  10569. S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
  10570. << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
  10571. if (!ASE && !OASE) {
  10572. bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
  10573. VarDecl::DeclarationOnly;
  10574. S.Diag(D->getLocation(),
  10575. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  10576. << D;
  10577. }
  10578. continue;
  10579. }
  10580. if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
  10581. !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
  10582. S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
  10583. << getOpenMPClauseName(ClauseKind);
  10584. if (!ASE && !OASE) {
  10585. bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
  10586. VarDecl::DeclarationOnly;
  10587. S.Diag(D->getLocation(),
  10588. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  10589. << D;
  10590. }
  10591. continue;
  10592. }
  10593. }
  10594. Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
  10595. VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
  10596. D->hasAttrs() ? &D->getAttrs() : nullptr);
  10597. VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
  10598. D->hasAttrs() ? &D->getAttrs() : nullptr);
  10599. QualType PrivateTy = Type;
  10600. // Try if we can determine constant lengths for all array sections and avoid
  10601. // the VLA.
  10602. bool ConstantLengthOASE = false;
  10603. if (OASE) {
  10604. bool SingleElement;
  10605. llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
  10606. ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
  10607. Context, OASE, SingleElement, ArraySizes);
  10608. // If we don't have a single element, we must emit a constant array type.
  10609. if (ConstantLengthOASE && !SingleElement) {
  10610. for (llvm::APSInt &Size : ArraySizes)
  10611. PrivateTy = Context.getConstantArrayType(
  10612. PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
  10613. }
  10614. }
  10615. if ((OASE && !ConstantLengthOASE) ||
  10616. (!OASE && !ASE &&
  10617. D->getType().getNonReferenceType()->isVariablyModifiedType())) {
  10618. if (!Context.getTargetInfo().isVLASupported() &&
  10619. S.shouldDiagnoseTargetSupportFromOpenMP()) {
  10620. S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
  10621. S.Diag(ELoc, diag::note_vla_unsupported);
  10622. continue;
  10623. }
  10624. // For arrays/array sections only:
  10625. // Create pseudo array type for private copy. The size for this array will
  10626. // be generated during codegen.
  10627. // For array subscripts or single variables Private Ty is the same as Type
  10628. // (type of the variable or single array element).
  10629. PrivateTy = Context.getVariableArrayType(
  10630. Type,
  10631. new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
  10632. ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
  10633. } else if (!ASE && !OASE &&
  10634. Context.getAsArrayType(D->getType().getNonReferenceType())) {
  10635. PrivateTy = D->getType().getNonReferenceType();
  10636. }
  10637. // Private copy.
  10638. VarDecl *PrivateVD =
  10639. buildVarDecl(S, ELoc, PrivateTy, D->getName(),
  10640. D->hasAttrs() ? &D->getAttrs() : nullptr,
  10641. VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
  10642. // Add initializer for private variable.
  10643. Expr *Init = nullptr;
  10644. DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
  10645. DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
  10646. if (DeclareReductionRef.isUsable()) {
  10647. auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
  10648. auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
  10649. if (DRD->getInitializer()) {
  10650. Init = DRDRef;
  10651. RHSVD->setInit(DRDRef);
  10652. RHSVD->setInitStyle(VarDecl::CallInit);
  10653. }
  10654. } else {
  10655. switch (BOK) {
  10656. case BO_Add:
  10657. case BO_Xor:
  10658. case BO_Or:
  10659. case BO_LOr:
  10660. // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
  10661. if (Type->isScalarType() || Type->isAnyComplexType())
  10662. Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
  10663. break;
  10664. case BO_Mul:
  10665. case BO_LAnd:
  10666. if (Type->isScalarType() || Type->isAnyComplexType()) {
  10667. // '*' and '&&' reduction ops - initializer is '1'.
  10668. Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
  10669. }
  10670. break;
  10671. case BO_And: {
  10672. // '&' reduction op - initializer is '~0'.
  10673. QualType OrigType = Type;
  10674. if (auto *ComplexTy = OrigType->getAs<ComplexType>())
  10675. Type = ComplexTy->getElementType();
  10676. if (Type->isRealFloatingType()) {
  10677. llvm::APFloat InitValue =
  10678. llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
  10679. /*isIEEE=*/true);
  10680. Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
  10681. Type, ELoc);
  10682. } else if (Type->isScalarType()) {
  10683. uint64_t Size = Context.getTypeSize(Type);
  10684. QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
  10685. llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
  10686. Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
  10687. }
  10688. if (Init && OrigType->isAnyComplexType()) {
  10689. // Init = 0xFFFF + 0xFFFFi;
  10690. auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
  10691. Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
  10692. }
  10693. Type = OrigType;
  10694. break;
  10695. }
  10696. case BO_LT:
  10697. case BO_GT: {
  10698. // 'min' reduction op - initializer is 'Largest representable number in
  10699. // the reduction list item type'.
  10700. // 'max' reduction op - initializer is 'Least representable number in
  10701. // the reduction list item type'.
  10702. if (Type->isIntegerType() || Type->isPointerType()) {
  10703. bool IsSigned = Type->hasSignedIntegerRepresentation();
  10704. uint64_t Size = Context.getTypeSize(Type);
  10705. QualType IntTy =
  10706. Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
  10707. llvm::APInt InitValue =
  10708. (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
  10709. : llvm::APInt::getMinValue(Size)
  10710. : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
  10711. : llvm::APInt::getMaxValue(Size);
  10712. Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
  10713. if (Type->isPointerType()) {
  10714. // Cast to pointer type.
  10715. ExprResult CastExpr = S.BuildCStyleCastExpr(
  10716. ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
  10717. if (CastExpr.isInvalid())
  10718. continue;
  10719. Init = CastExpr.get();
  10720. }
  10721. } else if (Type->isRealFloatingType()) {
  10722. llvm::APFloat InitValue = llvm::APFloat::getLargest(
  10723. Context.getFloatTypeSemantics(Type), BOK != BO_LT);
  10724. Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
  10725. Type, ELoc);
  10726. }
  10727. break;
  10728. }
  10729. case BO_PtrMemD:
  10730. case BO_PtrMemI:
  10731. case BO_MulAssign:
  10732. case BO_Div:
  10733. case BO_Rem:
  10734. case BO_Sub:
  10735. case BO_Shl:
  10736. case BO_Shr:
  10737. case BO_LE:
  10738. case BO_GE:
  10739. case BO_EQ:
  10740. case BO_NE:
  10741. case BO_Cmp:
  10742. case BO_AndAssign:
  10743. case BO_XorAssign:
  10744. case BO_OrAssign:
  10745. case BO_Assign:
  10746. case BO_AddAssign:
  10747. case BO_SubAssign:
  10748. case BO_DivAssign:
  10749. case BO_RemAssign:
  10750. case BO_ShlAssign:
  10751. case BO_ShrAssign:
  10752. case BO_Comma:
  10753. llvm_unreachable("Unexpected reduction operation");
  10754. }
  10755. }
  10756. if (Init && DeclareReductionRef.isUnset())
  10757. S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
  10758. else if (!Init)
  10759. S.ActOnUninitializedDecl(RHSVD);
  10760. if (RHSVD->isInvalidDecl())
  10761. continue;
  10762. if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
  10763. S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
  10764. << Type << ReductionIdRange;
  10765. bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
  10766. VarDecl::DeclarationOnly;
  10767. S.Diag(D->getLocation(),
  10768. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  10769. << D;
  10770. continue;
  10771. }
  10772. // Store initializer for single element in private copy. Will be used during
  10773. // codegen.
  10774. PrivateVD->setInit(RHSVD->getInit());
  10775. PrivateVD->setInitStyle(RHSVD->getInitStyle());
  10776. DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
  10777. ExprResult ReductionOp;
  10778. if (DeclareReductionRef.isUsable()) {
  10779. QualType RedTy = DeclareReductionRef.get()->getType();
  10780. QualType PtrRedTy = Context.getPointerType(RedTy);
  10781. ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
  10782. ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
  10783. if (!BasePath.empty()) {
  10784. LHS = S.DefaultLvalueConversion(LHS.get());
  10785. RHS = S.DefaultLvalueConversion(RHS.get());
  10786. LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
  10787. CK_UncheckedDerivedToBase, LHS.get(),
  10788. &BasePath, LHS.get()->getValueKind());
  10789. RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
  10790. CK_UncheckedDerivedToBase, RHS.get(),
  10791. &BasePath, RHS.get()->getValueKind());
  10792. }
  10793. FunctionProtoType::ExtProtoInfo EPI;
  10794. QualType Params[] = {PtrRedTy, PtrRedTy};
  10795. QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
  10796. auto *OVE = new (Context) OpaqueValueExpr(
  10797. ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
  10798. S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
  10799. Expr *Args[] = {LHS.get(), RHS.get()};
  10800. ReductionOp =
  10801. CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
  10802. } else {
  10803. ReductionOp = S.BuildBinOp(
  10804. Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
  10805. if (ReductionOp.isUsable()) {
  10806. if (BOK != BO_LT && BOK != BO_GT) {
  10807. ReductionOp =
  10808. S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
  10809. BO_Assign, LHSDRE, ReductionOp.get());
  10810. } else {
  10811. auto *ConditionalOp = new (Context)
  10812. ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
  10813. Type, VK_LValue, OK_Ordinary);
  10814. ReductionOp =
  10815. S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
  10816. BO_Assign, LHSDRE, ConditionalOp);
  10817. }
  10818. if (ReductionOp.isUsable())
  10819. ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(),
  10820. /*DiscardedValue*/ false);
  10821. }
  10822. if (!ReductionOp.isUsable())
  10823. continue;
  10824. }
  10825. // OpenMP [2.15.4.6, Restrictions, p.2]
  10826. // A list item that appears in an in_reduction clause of a task construct
  10827. // must appear in a task_reduction clause of a construct associated with a
  10828. // taskgroup region that includes the participating task in its taskgroup
  10829. // set. The construct associated with the innermost region that meets this
  10830. // condition must specify the same reduction-identifier as the in_reduction
  10831. // clause.
  10832. if (ClauseKind == OMPC_in_reduction) {
  10833. SourceRange ParentSR;
  10834. BinaryOperatorKind ParentBOK;
  10835. const Expr *ParentReductionOp;
  10836. Expr *ParentBOKTD, *ParentReductionOpTD;
  10837. DSAStackTy::DSAVarData ParentBOKDSA =
  10838. Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
  10839. ParentBOKTD);
  10840. DSAStackTy::DSAVarData ParentReductionOpDSA =
  10841. Stack->getTopMostTaskgroupReductionData(
  10842. D, ParentSR, ParentReductionOp, ParentReductionOpTD);
  10843. bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
  10844. bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
  10845. if (!IsParentBOK && !IsParentReductionOp) {
  10846. S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
  10847. continue;
  10848. }
  10849. if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
  10850. (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
  10851. IsParentReductionOp) {
  10852. bool EmitError = true;
  10853. if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
  10854. llvm::FoldingSetNodeID RedId, ParentRedId;
  10855. ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
  10856. DeclareReductionRef.get()->Profile(RedId, Context,
  10857. /*Canonical=*/true);
  10858. EmitError = RedId != ParentRedId;
  10859. }
  10860. if (EmitError) {
  10861. S.Diag(ReductionId.getBeginLoc(),
  10862. diag::err_omp_reduction_identifier_mismatch)
  10863. << ReductionIdRange << RefExpr->getSourceRange();
  10864. S.Diag(ParentSR.getBegin(),
  10865. diag::note_omp_previous_reduction_identifier)
  10866. << ParentSR
  10867. << (IsParentBOK ? ParentBOKDSA.RefExpr
  10868. : ParentReductionOpDSA.RefExpr)
  10869. ->getSourceRange();
  10870. continue;
  10871. }
  10872. }
  10873. TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
  10874. assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
  10875. }
  10876. DeclRefExpr *Ref = nullptr;
  10877. Expr *VarsExpr = RefExpr->IgnoreParens();
  10878. if (!VD && !S.CurContext->isDependentContext()) {
  10879. if (ASE || OASE) {
  10880. TransformExprToCaptures RebuildToCapture(S, D);
  10881. VarsExpr =
  10882. RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
  10883. Ref = RebuildToCapture.getCapturedExpr();
  10884. } else {
  10885. VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
  10886. }
  10887. if (!S.isOpenMPCapturedDecl(D)) {
  10888. RD.ExprCaptures.emplace_back(Ref->getDecl());
  10889. if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
  10890. ExprResult RefRes = S.DefaultLvalueConversion(Ref);
  10891. if (!RefRes.isUsable())
  10892. continue;
  10893. ExprResult PostUpdateRes =
  10894. S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
  10895. RefRes.get());
  10896. if (!PostUpdateRes.isUsable())
  10897. continue;
  10898. if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
  10899. Stack->getCurrentDirective() == OMPD_taskgroup) {
  10900. S.Diag(RefExpr->getExprLoc(),
  10901. diag::err_omp_reduction_non_addressable_expression)
  10902. << RefExpr->getSourceRange();
  10903. continue;
  10904. }
  10905. RD.ExprPostUpdates.emplace_back(
  10906. S.IgnoredValueConversions(PostUpdateRes.get()).get());
  10907. }
  10908. }
  10909. }
  10910. // All reduction items are still marked as reduction (to do not increase
  10911. // code base size).
  10912. Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
  10913. if (CurrDir == OMPD_taskgroup) {
  10914. if (DeclareReductionRef.isUsable())
  10915. Stack->addTaskgroupReductionData(D, ReductionIdRange,
  10916. DeclareReductionRef.get());
  10917. else
  10918. Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
  10919. }
  10920. RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
  10921. TaskgroupDescriptor);
  10922. }
  10923. return RD.Vars.empty();
  10924. }
  10925. OMPClause *Sema::ActOnOpenMPReductionClause(
  10926. ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
  10927. SourceLocation ColonLoc, SourceLocation EndLoc,
  10928. CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
  10929. ArrayRef<Expr *> UnresolvedReductions) {
  10930. ReductionData RD(VarList.size());
  10931. if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
  10932. StartLoc, LParenLoc, ColonLoc, EndLoc,
  10933. ReductionIdScopeSpec, ReductionId,
  10934. UnresolvedReductions, RD))
  10935. return nullptr;
  10936. return OMPReductionClause::Create(
  10937. Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
  10938. ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
  10939. RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
  10940. buildPreInits(Context, RD.ExprCaptures),
  10941. buildPostUpdate(*this, RD.ExprPostUpdates));
  10942. }
  10943. OMPClause *Sema::ActOnOpenMPTaskReductionClause(
  10944. ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
  10945. SourceLocation ColonLoc, SourceLocation EndLoc,
  10946. CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
  10947. ArrayRef<Expr *> UnresolvedReductions) {
  10948. ReductionData RD(VarList.size());
  10949. if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
  10950. StartLoc, LParenLoc, ColonLoc, EndLoc,
  10951. ReductionIdScopeSpec, ReductionId,
  10952. UnresolvedReductions, RD))
  10953. return nullptr;
  10954. return OMPTaskReductionClause::Create(
  10955. Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
  10956. ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
  10957. RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
  10958. buildPreInits(Context, RD.ExprCaptures),
  10959. buildPostUpdate(*this, RD.ExprPostUpdates));
  10960. }
  10961. OMPClause *Sema::ActOnOpenMPInReductionClause(
  10962. ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
  10963. SourceLocation ColonLoc, SourceLocation EndLoc,
  10964. CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
  10965. ArrayRef<Expr *> UnresolvedReductions) {
  10966. ReductionData RD(VarList.size());
  10967. if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
  10968. StartLoc, LParenLoc, ColonLoc, EndLoc,
  10969. ReductionIdScopeSpec, ReductionId,
  10970. UnresolvedReductions, RD))
  10971. return nullptr;
  10972. return OMPInReductionClause::Create(
  10973. Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
  10974. ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
  10975. RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
  10976. buildPreInits(Context, RD.ExprCaptures),
  10977. buildPostUpdate(*this, RD.ExprPostUpdates));
  10978. }
  10979. bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
  10980. SourceLocation LinLoc) {
  10981. if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
  10982. LinKind == OMPC_LINEAR_unknown) {
  10983. Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
  10984. return true;
  10985. }
  10986. return false;
  10987. }
  10988. bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
  10989. OpenMPLinearClauseKind LinKind,
  10990. QualType Type) {
  10991. const auto *VD = dyn_cast_or_null<VarDecl>(D);
  10992. // A variable must not have an incomplete type or a reference type.
  10993. if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
  10994. return true;
  10995. if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
  10996. !Type->isReferenceType()) {
  10997. Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
  10998. << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
  10999. return true;
  11000. }
  11001. Type = Type.getNonReferenceType();
  11002. // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
  11003. // A variable that is privatized must not have a const-qualified type
  11004. // unless it is of class type with a mutable member. This restriction does
  11005. // not apply to the firstprivate clause.
  11006. if (rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc))
  11007. return true;
  11008. // A list item must be of integral or pointer type.
  11009. Type = Type.getUnqualifiedType().getCanonicalType();
  11010. const auto *Ty = Type.getTypePtrOrNull();
  11011. if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
  11012. !Ty->isPointerType())) {
  11013. Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
  11014. if (D) {
  11015. bool IsDecl =
  11016. !VD ||
  11017. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  11018. Diag(D->getLocation(),
  11019. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  11020. << D;
  11021. }
  11022. return true;
  11023. }
  11024. return false;
  11025. }
  11026. OMPClause *Sema::ActOnOpenMPLinearClause(
  11027. ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
  11028. SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
  11029. SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
  11030. SmallVector<Expr *, 8> Vars;
  11031. SmallVector<Expr *, 8> Privates;
  11032. SmallVector<Expr *, 8> Inits;
  11033. SmallVector<Decl *, 4> ExprCaptures;
  11034. SmallVector<Expr *, 4> ExprPostUpdates;
  11035. if (CheckOpenMPLinearModifier(LinKind, LinLoc))
  11036. LinKind = OMPC_LINEAR_val;
  11037. for (Expr *RefExpr : VarList) {
  11038. assert(RefExpr && "NULL expr in OpenMP linear clause.");
  11039. SourceLocation ELoc;
  11040. SourceRange ERange;
  11041. Expr *SimpleRefExpr = RefExpr;
  11042. auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
  11043. if (Res.second) {
  11044. // It will be analyzed later.
  11045. Vars.push_back(RefExpr);
  11046. Privates.push_back(nullptr);
  11047. Inits.push_back(nullptr);
  11048. }
  11049. ValueDecl *D = Res.first;
  11050. if (!D)
  11051. continue;
  11052. QualType Type = D->getType();
  11053. auto *VD = dyn_cast<VarDecl>(D);
  11054. // OpenMP [2.14.3.7, linear clause]
  11055. // A list-item cannot appear in more than one linear clause.
  11056. // A list-item that appears in a linear clause cannot appear in any
  11057. // other data-sharing attribute clause.
  11058. DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
  11059. if (DVar.RefExpr) {
  11060. Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
  11061. << getOpenMPClauseName(OMPC_linear);
  11062. reportOriginalDsa(*this, DSAStack, D, DVar);
  11063. continue;
  11064. }
  11065. if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
  11066. continue;
  11067. Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
  11068. // Build private copy of original var.
  11069. VarDecl *Private =
  11070. buildVarDecl(*this, ELoc, Type, D->getName(),
  11071. D->hasAttrs() ? &D->getAttrs() : nullptr,
  11072. VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
  11073. DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
  11074. // Build var to save initial value.
  11075. VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
  11076. Expr *InitExpr;
  11077. DeclRefExpr *Ref = nullptr;
  11078. if (!VD && !CurContext->isDependentContext()) {
  11079. Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
  11080. if (!isOpenMPCapturedDecl(D)) {
  11081. ExprCaptures.push_back(Ref->getDecl());
  11082. if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
  11083. ExprResult RefRes = DefaultLvalueConversion(Ref);
  11084. if (!RefRes.isUsable())
  11085. continue;
  11086. ExprResult PostUpdateRes =
  11087. BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
  11088. SimpleRefExpr, RefRes.get());
  11089. if (!PostUpdateRes.isUsable())
  11090. continue;
  11091. ExprPostUpdates.push_back(
  11092. IgnoredValueConversions(PostUpdateRes.get()).get());
  11093. }
  11094. }
  11095. }
  11096. if (LinKind == OMPC_LINEAR_uval)
  11097. InitExpr = VD ? VD->getInit() : SimpleRefExpr;
  11098. else
  11099. InitExpr = VD ? SimpleRefExpr : Ref;
  11100. AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
  11101. /*DirectInit=*/false);
  11102. DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
  11103. DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
  11104. Vars.push_back((VD || CurContext->isDependentContext())
  11105. ? RefExpr->IgnoreParens()
  11106. : Ref);
  11107. Privates.push_back(PrivateRef);
  11108. Inits.push_back(InitRef);
  11109. }
  11110. if (Vars.empty())
  11111. return nullptr;
  11112. Expr *StepExpr = Step;
  11113. Expr *CalcStepExpr = nullptr;
  11114. if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
  11115. !Step->isInstantiationDependent() &&
  11116. !Step->containsUnexpandedParameterPack()) {
  11117. SourceLocation StepLoc = Step->getBeginLoc();
  11118. ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
  11119. if (Val.isInvalid())
  11120. return nullptr;
  11121. StepExpr = Val.get();
  11122. // Build var to save the step value.
  11123. VarDecl *SaveVar =
  11124. buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
  11125. ExprResult SaveRef =
  11126. buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
  11127. ExprResult CalcStep =
  11128. BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
  11129. CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false);
  11130. // Warn about zero linear step (it would be probably better specified as
  11131. // making corresponding variables 'const').
  11132. llvm::APSInt Result;
  11133. bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
  11134. if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
  11135. Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
  11136. << (Vars.size() > 1);
  11137. if (!IsConstant && CalcStep.isUsable()) {
  11138. // Calculate the step beforehand instead of doing this on each iteration.
  11139. // (This is not used if the number of iterations may be kfold-ed).
  11140. CalcStepExpr = CalcStep.get();
  11141. }
  11142. }
  11143. return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
  11144. ColonLoc, EndLoc, Vars, Privates, Inits,
  11145. StepExpr, CalcStepExpr,
  11146. buildPreInits(Context, ExprCaptures),
  11147. buildPostUpdate(*this, ExprPostUpdates));
  11148. }
  11149. static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
  11150. Expr *NumIterations, Sema &SemaRef,
  11151. Scope *S, DSAStackTy *Stack) {
  11152. // Walk the vars and build update/final expressions for the CodeGen.
  11153. SmallVector<Expr *, 8> Updates;
  11154. SmallVector<Expr *, 8> Finals;
  11155. Expr *Step = Clause.getStep();
  11156. Expr *CalcStep = Clause.getCalcStep();
  11157. // OpenMP [2.14.3.7, linear clause]
  11158. // If linear-step is not specified it is assumed to be 1.
  11159. if (!Step)
  11160. Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
  11161. else if (CalcStep)
  11162. Step = cast<BinaryOperator>(CalcStep)->getLHS();
  11163. bool HasErrors = false;
  11164. auto CurInit = Clause.inits().begin();
  11165. auto CurPrivate = Clause.privates().begin();
  11166. OpenMPLinearClauseKind LinKind = Clause.getModifier();
  11167. for (Expr *RefExpr : Clause.varlists()) {
  11168. SourceLocation ELoc;
  11169. SourceRange ERange;
  11170. Expr *SimpleRefExpr = RefExpr;
  11171. auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
  11172. ValueDecl *D = Res.first;
  11173. if (Res.second || !D) {
  11174. Updates.push_back(nullptr);
  11175. Finals.push_back(nullptr);
  11176. HasErrors = true;
  11177. continue;
  11178. }
  11179. auto &&Info = Stack->isLoopControlVariable(D);
  11180. // OpenMP [2.15.11, distribute simd Construct]
  11181. // A list item may not appear in a linear clause, unless it is the loop
  11182. // iteration variable.
  11183. if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
  11184. isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
  11185. SemaRef.Diag(ELoc,
  11186. diag::err_omp_linear_distribute_var_non_loop_iteration);
  11187. Updates.push_back(nullptr);
  11188. Finals.push_back(nullptr);
  11189. HasErrors = true;
  11190. continue;
  11191. }
  11192. Expr *InitExpr = *CurInit;
  11193. // Build privatized reference to the current linear var.
  11194. auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
  11195. Expr *CapturedRef;
  11196. if (LinKind == OMPC_LINEAR_uval)
  11197. CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
  11198. else
  11199. CapturedRef =
  11200. buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
  11201. DE->getType().getUnqualifiedType(), DE->getExprLoc(),
  11202. /*RefersToCapture=*/true);
  11203. // Build update: Var = InitExpr + IV * Step
  11204. ExprResult Update;
  11205. if (!Info.first)
  11206. Update =
  11207. buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
  11208. InitExpr, IV, Step, /* Subtract */ false);
  11209. else
  11210. Update = *CurPrivate;
  11211. Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
  11212. /*DiscardedValue*/ false);
  11213. // Build final: Var = InitExpr + NumIterations * Step
  11214. ExprResult Final;
  11215. if (!Info.first)
  11216. Final =
  11217. buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
  11218. InitExpr, NumIterations, Step, /*Subtract=*/false);
  11219. else
  11220. Final = *CurPrivate;
  11221. Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
  11222. /*DiscardedValue*/ false);
  11223. if (!Update.isUsable() || !Final.isUsable()) {
  11224. Updates.push_back(nullptr);
  11225. Finals.push_back(nullptr);
  11226. HasErrors = true;
  11227. } else {
  11228. Updates.push_back(Update.get());
  11229. Finals.push_back(Final.get());
  11230. }
  11231. ++CurInit;
  11232. ++CurPrivate;
  11233. }
  11234. Clause.setUpdates(Updates);
  11235. Clause.setFinals(Finals);
  11236. return HasErrors;
  11237. }
  11238. OMPClause *Sema::ActOnOpenMPAlignedClause(
  11239. ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
  11240. SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
  11241. SmallVector<Expr *, 8> Vars;
  11242. for (Expr *RefExpr : VarList) {
  11243. assert(RefExpr && "NULL expr in OpenMP linear clause.");
  11244. SourceLocation ELoc;
  11245. SourceRange ERange;
  11246. Expr *SimpleRefExpr = RefExpr;
  11247. auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
  11248. if (Res.second) {
  11249. // It will be analyzed later.
  11250. Vars.push_back(RefExpr);
  11251. }
  11252. ValueDecl *D = Res.first;
  11253. if (!D)
  11254. continue;
  11255. QualType QType = D->getType();
  11256. auto *VD = dyn_cast<VarDecl>(D);
  11257. // OpenMP [2.8.1, simd construct, Restrictions]
  11258. // The type of list items appearing in the aligned clause must be
  11259. // array, pointer, reference to array, or reference to pointer.
  11260. QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
  11261. const Type *Ty = QType.getTypePtrOrNull();
  11262. if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
  11263. Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
  11264. << QType << getLangOpts().CPlusPlus << ERange;
  11265. bool IsDecl =
  11266. !VD ||
  11267. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  11268. Diag(D->getLocation(),
  11269. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  11270. << D;
  11271. continue;
  11272. }
  11273. // OpenMP [2.8.1, simd construct, Restrictions]
  11274. // A list-item cannot appear in more than one aligned clause.
  11275. if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
  11276. Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
  11277. Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
  11278. << getOpenMPClauseName(OMPC_aligned);
  11279. continue;
  11280. }
  11281. DeclRefExpr *Ref = nullptr;
  11282. if (!VD && isOpenMPCapturedDecl(D))
  11283. Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
  11284. Vars.push_back(DefaultFunctionArrayConversion(
  11285. (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
  11286. .get());
  11287. }
  11288. // OpenMP [2.8.1, simd construct, Description]
  11289. // The parameter of the aligned clause, alignment, must be a constant
  11290. // positive integer expression.
  11291. // If no optional parameter is specified, implementation-defined default
  11292. // alignments for SIMD instructions on the target platforms are assumed.
  11293. if (Alignment != nullptr) {
  11294. ExprResult AlignResult =
  11295. VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
  11296. if (AlignResult.isInvalid())
  11297. return nullptr;
  11298. Alignment = AlignResult.get();
  11299. }
  11300. if (Vars.empty())
  11301. return nullptr;
  11302. return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
  11303. EndLoc, Vars, Alignment);
  11304. }
  11305. OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
  11306. SourceLocation StartLoc,
  11307. SourceLocation LParenLoc,
  11308. SourceLocation EndLoc) {
  11309. SmallVector<Expr *, 8> Vars;
  11310. SmallVector<Expr *, 8> SrcExprs;
  11311. SmallVector<Expr *, 8> DstExprs;
  11312. SmallVector<Expr *, 8> AssignmentOps;
  11313. for (Expr *RefExpr : VarList) {
  11314. assert(RefExpr && "NULL expr in OpenMP copyin clause.");
  11315. if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
  11316. // It will be analyzed later.
  11317. Vars.push_back(RefExpr);
  11318. SrcExprs.push_back(nullptr);
  11319. DstExprs.push_back(nullptr);
  11320. AssignmentOps.push_back(nullptr);
  11321. continue;
  11322. }
  11323. SourceLocation ELoc = RefExpr->getExprLoc();
  11324. // OpenMP [2.1, C/C++]
  11325. // A list item is a variable name.
  11326. // OpenMP [2.14.4.1, Restrictions, p.1]
  11327. // A list item that appears in a copyin clause must be threadprivate.
  11328. auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
  11329. if (!DE || !isa<VarDecl>(DE->getDecl())) {
  11330. Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
  11331. << 0 << RefExpr->getSourceRange();
  11332. continue;
  11333. }
  11334. Decl *D = DE->getDecl();
  11335. auto *VD = cast<VarDecl>(D);
  11336. QualType Type = VD->getType();
  11337. if (Type->isDependentType() || Type->isInstantiationDependentType()) {
  11338. // It will be analyzed later.
  11339. Vars.push_back(DE);
  11340. SrcExprs.push_back(nullptr);
  11341. DstExprs.push_back(nullptr);
  11342. AssignmentOps.push_back(nullptr);
  11343. continue;
  11344. }
  11345. // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
  11346. // A list item that appears in a copyin clause must be threadprivate.
  11347. if (!DSAStack->isThreadPrivate(VD)) {
  11348. Diag(ELoc, diag::err_omp_required_access)
  11349. << getOpenMPClauseName(OMPC_copyin)
  11350. << getOpenMPDirectiveName(OMPD_threadprivate);
  11351. continue;
  11352. }
  11353. // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
  11354. // A variable of class type (or array thereof) that appears in a
  11355. // copyin clause requires an accessible, unambiguous copy assignment
  11356. // operator for the class type.
  11357. QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
  11358. VarDecl *SrcVD =
  11359. buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
  11360. ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
  11361. DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
  11362. *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
  11363. VarDecl *DstVD =
  11364. buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
  11365. VD->hasAttrs() ? &VD->getAttrs() : nullptr);
  11366. DeclRefExpr *PseudoDstExpr =
  11367. buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
  11368. // For arrays generate assignment operation for single element and replace
  11369. // it by the original array element in CodeGen.
  11370. ExprResult AssignmentOp =
  11371. BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
  11372. PseudoSrcExpr);
  11373. if (AssignmentOp.isInvalid())
  11374. continue;
  11375. AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
  11376. /*DiscardedValue*/ false);
  11377. if (AssignmentOp.isInvalid())
  11378. continue;
  11379. DSAStack->addDSA(VD, DE, OMPC_copyin);
  11380. Vars.push_back(DE);
  11381. SrcExprs.push_back(PseudoSrcExpr);
  11382. DstExprs.push_back(PseudoDstExpr);
  11383. AssignmentOps.push_back(AssignmentOp.get());
  11384. }
  11385. if (Vars.empty())
  11386. return nullptr;
  11387. return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
  11388. SrcExprs, DstExprs, AssignmentOps);
  11389. }
  11390. OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
  11391. SourceLocation StartLoc,
  11392. SourceLocation LParenLoc,
  11393. SourceLocation EndLoc) {
  11394. SmallVector<Expr *, 8> Vars;
  11395. SmallVector<Expr *, 8> SrcExprs;
  11396. SmallVector<Expr *, 8> DstExprs;
  11397. SmallVector<Expr *, 8> AssignmentOps;
  11398. for (Expr *RefExpr : VarList) {
  11399. assert(RefExpr && "NULL expr in OpenMP linear clause.");
  11400. SourceLocation ELoc;
  11401. SourceRange ERange;
  11402. Expr *SimpleRefExpr = RefExpr;
  11403. auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
  11404. if (Res.second) {
  11405. // It will be analyzed later.
  11406. Vars.push_back(RefExpr);
  11407. SrcExprs.push_back(nullptr);
  11408. DstExprs.push_back(nullptr);
  11409. AssignmentOps.push_back(nullptr);
  11410. }
  11411. ValueDecl *D = Res.first;
  11412. if (!D)
  11413. continue;
  11414. QualType Type = D->getType();
  11415. auto *VD = dyn_cast<VarDecl>(D);
  11416. // OpenMP [2.14.4.2, Restrictions, p.2]
  11417. // A list item that appears in a copyprivate clause may not appear in a
  11418. // private or firstprivate clause on the single construct.
  11419. if (!VD || !DSAStack->isThreadPrivate(VD)) {
  11420. DSAStackTy::DSAVarData DVar =
  11421. DSAStack->getTopDSA(D, /*FromParent=*/false);
  11422. if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
  11423. DVar.RefExpr) {
  11424. Diag(ELoc, diag::err_omp_wrong_dsa)
  11425. << getOpenMPClauseName(DVar.CKind)
  11426. << getOpenMPClauseName(OMPC_copyprivate);
  11427. reportOriginalDsa(*this, DSAStack, D, DVar);
  11428. continue;
  11429. }
  11430. // OpenMP [2.11.4.2, Restrictions, p.1]
  11431. // All list items that appear in a copyprivate clause must be either
  11432. // threadprivate or private in the enclosing context.
  11433. if (DVar.CKind == OMPC_unknown) {
  11434. DVar = DSAStack->getImplicitDSA(D, false);
  11435. if (DVar.CKind == OMPC_shared) {
  11436. Diag(ELoc, diag::err_omp_required_access)
  11437. << getOpenMPClauseName(OMPC_copyprivate)
  11438. << "threadprivate or private in the enclosing context";
  11439. reportOriginalDsa(*this, DSAStack, D, DVar);
  11440. continue;
  11441. }
  11442. }
  11443. }
  11444. // Variably modified types are not supported.
  11445. if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
  11446. Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
  11447. << getOpenMPClauseName(OMPC_copyprivate) << Type
  11448. << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
  11449. bool IsDecl =
  11450. !VD ||
  11451. VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
  11452. Diag(D->getLocation(),
  11453. IsDecl ? diag::note_previous_decl : diag::note_defined_here)
  11454. << D;
  11455. continue;
  11456. }
  11457. // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
  11458. // A variable of class type (or array thereof) that appears in a
  11459. // copyin clause requires an accessible, unambiguous copy assignment
  11460. // operator for the class type.
  11461. Type = Context.getBaseElementType(Type.getNonReferenceType())
  11462. .getUnqualifiedType();
  11463. VarDecl *SrcVD =
  11464. buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
  11465. D->hasAttrs() ? &D->getAttrs() : nullptr);
  11466. DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
  11467. VarDecl *DstVD =
  11468. buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
  11469. D->hasAttrs() ? &D->getAttrs() : nullptr);
  11470. DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
  11471. ExprResult AssignmentOp = BuildBinOp(
  11472. DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
  11473. if (AssignmentOp.isInvalid())
  11474. continue;
  11475. AssignmentOp =
  11476. ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
  11477. if (AssignmentOp.isInvalid())
  11478. continue;
  11479. // No need to mark vars as copyprivate, they are already threadprivate or
  11480. // implicitly private.
  11481. assert(VD || isOpenMPCapturedDecl(D));
  11482. Vars.push_back(
  11483. VD ? RefExpr->IgnoreParens()
  11484. : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
  11485. SrcExprs.push_back(PseudoSrcExpr);
  11486. DstExprs.push_back(PseudoDstExpr);
  11487. AssignmentOps.push_back(AssignmentOp.get());
  11488. }
  11489. if (Vars.empty())
  11490. return nullptr;
  11491. return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
  11492. Vars, SrcExprs, DstExprs, AssignmentOps);
  11493. }
  11494. OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
  11495. SourceLocation StartLoc,
  11496. SourceLocation LParenLoc,
  11497. SourceLocation EndLoc) {
  11498. if (VarList.empty())
  11499. return nullptr;
  11500. return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
  11501. }
  11502. OMPClause *
  11503. Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
  11504. SourceLocation DepLoc, SourceLocation ColonLoc,
  11505. ArrayRef<Expr *> VarList, SourceLocation StartLoc,
  11506. SourceLocation LParenLoc, SourceLocation EndLoc) {
  11507. if (DSAStack->getCurrentDirective() == OMPD_ordered &&
  11508. DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
  11509. Diag(DepLoc, diag::err_omp_unexpected_clause_value)
  11510. << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
  11511. return nullptr;
  11512. }
  11513. if (DSAStack->getCurrentDirective() != OMPD_ordered &&
  11514. (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
  11515. DepKind == OMPC_DEPEND_sink)) {
  11516. unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
  11517. Diag(DepLoc, diag::err_omp_unexpected_clause_value)
  11518. << getListOfPossibleValues(OMPC_depend, /*First=*/0,
  11519. /*Last=*/OMPC_DEPEND_unknown, Except)
  11520. << getOpenMPClauseName(OMPC_depend);
  11521. return nullptr;
  11522. }
  11523. SmallVector<Expr *, 8> Vars;
  11524. DSAStackTy::OperatorOffsetTy OpsOffs;
  11525. llvm::APSInt DepCounter(/*BitWidth=*/32);
  11526. llvm::APSInt TotalDepCount(/*BitWidth=*/32);
  11527. if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
  11528. if (const Expr *OrderedCountExpr =
  11529. DSAStack->getParentOrderedRegionParam().first) {
  11530. TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
  11531. TotalDepCount.setIsUnsigned(/*Val=*/true);
  11532. }
  11533. }
  11534. for (Expr *RefExpr : VarList) {
  11535. assert(RefExpr && "NULL expr in OpenMP shared clause.");
  11536. if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
  11537. // It will be analyzed later.
  11538. Vars.push_back(RefExpr);
  11539. continue;
  11540. }
  11541. SourceLocation ELoc = RefExpr->getExprLoc();
  11542. Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
  11543. if (DepKind == OMPC_DEPEND_sink) {
  11544. if (DSAStack->getParentOrderedRegionParam().first &&
  11545. DepCounter >= TotalDepCount) {
  11546. Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
  11547. continue;
  11548. }
  11549. ++DepCounter;
  11550. // OpenMP [2.13.9, Summary]
  11551. // depend(dependence-type : vec), where dependence-type is:
  11552. // 'sink' and where vec is the iteration vector, which has the form:
  11553. // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
  11554. // where n is the value specified by the ordered clause in the loop
  11555. // directive, xi denotes the loop iteration variable of the i-th nested
  11556. // loop associated with the loop directive, and di is a constant
  11557. // non-negative integer.
  11558. if (CurContext->isDependentContext()) {
  11559. // It will be analyzed later.
  11560. Vars.push_back(RefExpr);
  11561. continue;
  11562. }
  11563. SimpleExpr = SimpleExpr->IgnoreImplicit();
  11564. OverloadedOperatorKind OOK = OO_None;
  11565. SourceLocation OOLoc;
  11566. Expr *LHS = SimpleExpr;
  11567. Expr *RHS = nullptr;
  11568. if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
  11569. OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
  11570. OOLoc = BO->getOperatorLoc();
  11571. LHS = BO->getLHS()->IgnoreParenImpCasts();
  11572. RHS = BO->getRHS()->IgnoreParenImpCasts();
  11573. } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
  11574. OOK = OCE->getOperator();
  11575. OOLoc = OCE->getOperatorLoc();
  11576. LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
  11577. RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
  11578. } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
  11579. OOK = MCE->getMethodDecl()
  11580. ->getNameInfo()
  11581. .getName()
  11582. .getCXXOverloadedOperator();
  11583. OOLoc = MCE->getCallee()->getExprLoc();
  11584. LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
  11585. RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
  11586. }
  11587. SourceLocation ELoc;
  11588. SourceRange ERange;
  11589. auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
  11590. if (Res.second) {
  11591. // It will be analyzed later.
  11592. Vars.push_back(RefExpr);
  11593. }
  11594. ValueDecl *D = Res.first;
  11595. if (!D)
  11596. continue;
  11597. if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
  11598. Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
  11599. continue;
  11600. }
  11601. if (RHS) {
  11602. ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
  11603. RHS, OMPC_depend, /*StrictlyPositive=*/false);
  11604. if (RHSRes.isInvalid())
  11605. continue;
  11606. }
  11607. if (!CurContext->isDependentContext() &&
  11608. DSAStack->getParentOrderedRegionParam().first &&
  11609. DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
  11610. const ValueDecl *VD =
  11611. DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
  11612. if (VD)
  11613. Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
  11614. << 1 << VD;
  11615. else
  11616. Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
  11617. continue;
  11618. }
  11619. OpsOffs.emplace_back(RHS, OOK);
  11620. } else {
  11621. auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
  11622. if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
  11623. (ASE &&
  11624. !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
  11625. !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
  11626. Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
  11627. << RefExpr->getSourceRange();
  11628. continue;
  11629. }
  11630. bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
  11631. getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
  11632. ExprResult Res =
  11633. CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts());
  11634. getDiagnostics().setSuppressAllDiagnostics(Suppress);
  11635. if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
  11636. Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
  11637. << RefExpr->getSourceRange();
  11638. continue;
  11639. }
  11640. }
  11641. Vars.push_back(RefExpr->IgnoreParenImpCasts());
  11642. }
  11643. if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
  11644. TotalDepCount > VarList.size() &&
  11645. DSAStack->getParentOrderedRegionParam().first &&
  11646. DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
  11647. Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
  11648. << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
  11649. }
  11650. if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
  11651. Vars.empty())
  11652. return nullptr;
  11653. auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
  11654. DepKind, DepLoc, ColonLoc, Vars,
  11655. TotalDepCount.getZExtValue());
  11656. if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
  11657. DSAStack->isParentOrderedRegion())
  11658. DSAStack->addDoacrossDependClause(C, OpsOffs);
  11659. return C;
  11660. }
  11661. OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
  11662. SourceLocation LParenLoc,
  11663. SourceLocation EndLoc) {
  11664. Expr *ValExpr = Device;
  11665. Stmt *HelperValStmt = nullptr;
  11666. // OpenMP [2.9.1, Restrictions]
  11667. // The device expression must evaluate to a non-negative integer value.
  11668. if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
  11669. /*StrictlyPositive=*/false))
  11670. return nullptr;
  11671. OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
  11672. OpenMPDirectiveKind CaptureRegion =
  11673. getOpenMPCaptureRegionForClause(DKind, OMPC_device);
  11674. if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
  11675. ValExpr = MakeFullExpr(ValExpr).get();
  11676. llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
  11677. ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
  11678. HelperValStmt = buildPreInits(Context, Captures);
  11679. }
  11680. return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
  11681. StartLoc, LParenLoc, EndLoc);
  11682. }
  11683. static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
  11684. DSAStackTy *Stack, QualType QTy,
  11685. bool FullCheck = true) {
  11686. NamedDecl *ND;
  11687. if (QTy->isIncompleteType(&ND)) {
  11688. SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
  11689. return false;
  11690. }
  11691. if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
  11692. !QTy.isTrivialType(SemaRef.Context))
  11693. SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
  11694. return true;
  11695. }
  11696. /// Return true if it can be proven that the provided array expression
  11697. /// (array section or array subscript) does NOT specify the whole size of the
  11698. /// array whose base type is \a BaseQTy.
  11699. static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
  11700. const Expr *E,
  11701. QualType BaseQTy) {
  11702. const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
  11703. // If this is an array subscript, it refers to the whole size if the size of
  11704. // the dimension is constant and equals 1. Also, an array section assumes the
  11705. // format of an array subscript if no colon is used.
  11706. if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
  11707. if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
  11708. return ATy->getSize().getSExtValue() != 1;
  11709. // Size can't be evaluated statically.
  11710. return false;
  11711. }
  11712. assert(OASE && "Expecting array section if not an array subscript.");
  11713. const Expr *LowerBound = OASE->getLowerBound();
  11714. const Expr *Length = OASE->getLength();
  11715. // If there is a lower bound that does not evaluates to zero, we are not
  11716. // covering the whole dimension.
  11717. if (LowerBound) {
  11718. Expr::EvalResult Result;
  11719. if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext()))
  11720. return false; // Can't get the integer value as a constant.
  11721. llvm::APSInt ConstLowerBound = Result.Val.getInt();
  11722. if (ConstLowerBound.getSExtValue())
  11723. return true;
  11724. }
  11725. // If we don't have a length we covering the whole dimension.
  11726. if (!Length)
  11727. return false;
  11728. // If the base is a pointer, we don't have a way to get the size of the
  11729. // pointee.
  11730. if (BaseQTy->isPointerType())
  11731. return false;
  11732. // We can only check if the length is the same as the size of the dimension
  11733. // if we have a constant array.
  11734. const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
  11735. if (!CATy)
  11736. return false;
  11737. Expr::EvalResult Result;
  11738. if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
  11739. return false; // Can't get the integer value as a constant.
  11740. llvm::APSInt ConstLength = Result.Val.getInt();
  11741. return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
  11742. }
  11743. // Return true if it can be proven that the provided array expression (array
  11744. // section or array subscript) does NOT specify a single element of the array
  11745. // whose base type is \a BaseQTy.
  11746. static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
  11747. const Expr *E,
  11748. QualType BaseQTy) {
  11749. const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
  11750. // An array subscript always refer to a single element. Also, an array section
  11751. // assumes the format of an array subscript if no colon is used.
  11752. if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
  11753. return false;
  11754. assert(OASE && "Expecting array section if not an array subscript.");
  11755. const Expr *Length = OASE->getLength();
  11756. // If we don't have a length we have to check if the array has unitary size
  11757. // for this dimension. Also, we should always expect a length if the base type
  11758. // is pointer.
  11759. if (!Length) {
  11760. if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
  11761. return ATy->getSize().getSExtValue() != 1;
  11762. // We cannot assume anything.
  11763. return false;
  11764. }
  11765. // Check if the length evaluates to 1.
  11766. Expr::EvalResult Result;
  11767. if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
  11768. return false; // Can't get the integer value as a constant.
  11769. llvm::APSInt ConstLength = Result.Val.getInt();
  11770. return ConstLength.getSExtValue() != 1;
  11771. }
  11772. // Return the expression of the base of the mappable expression or null if it
  11773. // cannot be determined and do all the necessary checks to see if the expression
  11774. // is valid as a standalone mappable expression. In the process, record all the
  11775. // components of the expression.
  11776. static const Expr *checkMapClauseExpressionBase(
  11777. Sema &SemaRef, Expr *E,
  11778. OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
  11779. OpenMPClauseKind CKind, bool NoDiagnose) {
  11780. SourceLocation ELoc = E->getExprLoc();
  11781. SourceRange ERange = E->getSourceRange();
  11782. // The base of elements of list in a map clause have to be either:
  11783. // - a reference to variable or field.
  11784. // - a member expression.
  11785. // - an array expression.
  11786. //
  11787. // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
  11788. // reference to 'r'.
  11789. //
  11790. // If we have:
  11791. //
  11792. // struct SS {
  11793. // Bla S;
  11794. // foo() {
  11795. // #pragma omp target map (S.Arr[:12]);
  11796. // }
  11797. // }
  11798. //
  11799. // We want to retrieve the member expression 'this->S';
  11800. const Expr *RelevantExpr = nullptr;
  11801. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
  11802. // If a list item is an array section, it must specify contiguous storage.
  11803. //
  11804. // For this restriction it is sufficient that we make sure only references
  11805. // to variables or fields and array expressions, and that no array sections
  11806. // exist except in the rightmost expression (unless they cover the whole
  11807. // dimension of the array). E.g. these would be invalid:
  11808. //
  11809. // r.ArrS[3:5].Arr[6:7]
  11810. //
  11811. // r.ArrS[3:5].x
  11812. //
  11813. // but these would be valid:
  11814. // r.ArrS[3].Arr[6:7]
  11815. //
  11816. // r.ArrS[3].x
  11817. bool AllowUnitySizeArraySection = true;
  11818. bool AllowWholeSizeArraySection = true;
  11819. while (!RelevantExpr) {
  11820. E = E->IgnoreParenImpCasts();
  11821. if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
  11822. if (!isa<VarDecl>(CurE->getDecl()))
  11823. return nullptr;
  11824. RelevantExpr = CurE;
  11825. // If we got a reference to a declaration, we should not expect any array
  11826. // section before that.
  11827. AllowUnitySizeArraySection = false;
  11828. AllowWholeSizeArraySection = false;
  11829. // Record the component.
  11830. CurComponents.emplace_back(CurE, CurE->getDecl());
  11831. } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
  11832. Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
  11833. if (isa<CXXThisExpr>(BaseE))
  11834. // We found a base expression: this->Val.
  11835. RelevantExpr = CurE;
  11836. else
  11837. E = BaseE;
  11838. if (!isa<FieldDecl>(CurE->getMemberDecl())) {
  11839. if (!NoDiagnose) {
  11840. SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
  11841. << CurE->getSourceRange();
  11842. return nullptr;
  11843. }
  11844. if (RelevantExpr)
  11845. return nullptr;
  11846. continue;
  11847. }
  11848. auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
  11849. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
  11850. // A bit-field cannot appear in a map clause.
  11851. //
  11852. if (FD->isBitField()) {
  11853. if (!NoDiagnose) {
  11854. SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
  11855. << CurE->getSourceRange() << getOpenMPClauseName(CKind);
  11856. return nullptr;
  11857. }
  11858. if (RelevantExpr)
  11859. return nullptr;
  11860. continue;
  11861. }
  11862. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
  11863. // If the type of a list item is a reference to a type T then the type
  11864. // will be considered to be T for all purposes of this clause.
  11865. QualType CurType = BaseE->getType().getNonReferenceType();
  11866. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
  11867. // A list item cannot be a variable that is a member of a structure with
  11868. // a union type.
  11869. //
  11870. if (CurType->isUnionType()) {
  11871. if (!NoDiagnose) {
  11872. SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
  11873. << CurE->getSourceRange();
  11874. return nullptr;
  11875. }
  11876. continue;
  11877. }
  11878. // If we got a member expression, we should not expect any array section
  11879. // before that:
  11880. //
  11881. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
  11882. // If a list item is an element of a structure, only the rightmost symbol
  11883. // of the variable reference can be an array section.
  11884. //
  11885. AllowUnitySizeArraySection = false;
  11886. AllowWholeSizeArraySection = false;
  11887. // Record the component.
  11888. CurComponents.emplace_back(CurE, FD);
  11889. } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
  11890. E = CurE->getBase()->IgnoreParenImpCasts();
  11891. if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
  11892. if (!NoDiagnose) {
  11893. SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
  11894. << 0 << CurE->getSourceRange();
  11895. return nullptr;
  11896. }
  11897. continue;
  11898. }
  11899. // If we got an array subscript that express the whole dimension we
  11900. // can have any array expressions before. If it only expressing part of
  11901. // the dimension, we can only have unitary-size array expressions.
  11902. if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
  11903. E->getType()))
  11904. AllowWholeSizeArraySection = false;
  11905. if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
  11906. Expr::EvalResult Result;
  11907. if (CurE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext())) {
  11908. if (!Result.Val.getInt().isNullValue()) {
  11909. SemaRef.Diag(CurE->getIdx()->getExprLoc(),
  11910. diag::err_omp_invalid_map_this_expr);
  11911. SemaRef.Diag(CurE->getIdx()->getExprLoc(),
  11912. diag::note_omp_invalid_subscript_on_this_ptr_map);
  11913. }
  11914. }
  11915. RelevantExpr = TE;
  11916. }
  11917. // Record the component - we don't have any declaration associated.
  11918. CurComponents.emplace_back(CurE, nullptr);
  11919. } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
  11920. assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
  11921. E = CurE->getBase()->IgnoreParenImpCasts();
  11922. QualType CurType =
  11923. OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
  11924. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
  11925. // If the type of a list item is a reference to a type T then the type
  11926. // will be considered to be T for all purposes of this clause.
  11927. if (CurType->isReferenceType())
  11928. CurType = CurType->getPointeeType();
  11929. bool IsPointer = CurType->isAnyPointerType();
  11930. if (!IsPointer && !CurType->isArrayType()) {
  11931. SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
  11932. << 0 << CurE->getSourceRange();
  11933. return nullptr;
  11934. }
  11935. bool NotWhole =
  11936. checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
  11937. bool NotUnity =
  11938. checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
  11939. if (AllowWholeSizeArraySection) {
  11940. // Any array section is currently allowed. Allowing a whole size array
  11941. // section implies allowing a unity array section as well.
  11942. //
  11943. // If this array section refers to the whole dimension we can still
  11944. // accept other array sections before this one, except if the base is a
  11945. // pointer. Otherwise, only unitary sections are accepted.
  11946. if (NotWhole || IsPointer)
  11947. AllowWholeSizeArraySection = false;
  11948. } else if (AllowUnitySizeArraySection && NotUnity) {
  11949. // A unity or whole array section is not allowed and that is not
  11950. // compatible with the properties of the current array section.
  11951. SemaRef.Diag(
  11952. ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
  11953. << CurE->getSourceRange();
  11954. return nullptr;
  11955. }
  11956. if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
  11957. Expr::EvalResult ResultR;
  11958. Expr::EvalResult ResultL;
  11959. if (CurE->getLength()->EvaluateAsInt(ResultR,
  11960. SemaRef.getASTContext())) {
  11961. if (!ResultR.Val.getInt().isOneValue()) {
  11962. SemaRef.Diag(CurE->getLength()->getExprLoc(),
  11963. diag::err_omp_invalid_map_this_expr);
  11964. SemaRef.Diag(CurE->getLength()->getExprLoc(),
  11965. diag::note_omp_invalid_length_on_this_ptr_mapping);
  11966. }
  11967. }
  11968. if (CurE->getLowerBound() && CurE->getLowerBound()->EvaluateAsInt(
  11969. ResultL, SemaRef.getASTContext())) {
  11970. if (!ResultL.Val.getInt().isNullValue()) {
  11971. SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
  11972. diag::err_omp_invalid_map_this_expr);
  11973. SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
  11974. diag::note_omp_invalid_lower_bound_on_this_ptr_mapping);
  11975. }
  11976. }
  11977. RelevantExpr = TE;
  11978. }
  11979. // Record the component - we don't have any declaration associated.
  11980. CurComponents.emplace_back(CurE, nullptr);
  11981. } else {
  11982. if (!NoDiagnose) {
  11983. // If nothing else worked, this is not a valid map clause expression.
  11984. SemaRef.Diag(
  11985. ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
  11986. << ERange;
  11987. }
  11988. return nullptr;
  11989. }
  11990. }
  11991. return RelevantExpr;
  11992. }
  11993. // Return true if expression E associated with value VD has conflicts with other
  11994. // map information.
  11995. static bool checkMapConflicts(
  11996. Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
  11997. bool CurrentRegionOnly,
  11998. OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
  11999. OpenMPClauseKind CKind) {
  12000. assert(VD && E);
  12001. SourceLocation ELoc = E->getExprLoc();
  12002. SourceRange ERange = E->getSourceRange();
  12003. // In order to easily check the conflicts we need to match each component of
  12004. // the expression under test with the components of the expressions that are
  12005. // already in the stack.
  12006. assert(!CurComponents.empty() && "Map clause expression with no components!");
  12007. assert(CurComponents.back().getAssociatedDeclaration() == VD &&
  12008. "Map clause expression with unexpected base!");
  12009. // Variables to help detecting enclosing problems in data environment nests.
  12010. bool IsEnclosedByDataEnvironmentExpr = false;
  12011. const Expr *EnclosingExpr = nullptr;
  12012. bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
  12013. VD, CurrentRegionOnly,
  12014. [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
  12015. ERange, CKind, &EnclosingExpr,
  12016. CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
  12017. StackComponents,
  12018. OpenMPClauseKind) {
  12019. assert(!StackComponents.empty() &&
  12020. "Map clause expression with no components!");
  12021. assert(StackComponents.back().getAssociatedDeclaration() == VD &&
  12022. "Map clause expression with unexpected base!");
  12023. (void)VD;
  12024. // The whole expression in the stack.
  12025. const Expr *RE = StackComponents.front().getAssociatedExpression();
  12026. // Expressions must start from the same base. Here we detect at which
  12027. // point both expressions diverge from each other and see if we can
  12028. // detect if the memory referred to both expressions is contiguous and
  12029. // do not overlap.
  12030. auto CI = CurComponents.rbegin();
  12031. auto CE = CurComponents.rend();
  12032. auto SI = StackComponents.rbegin();
  12033. auto SE = StackComponents.rend();
  12034. for (; CI != CE && SI != SE; ++CI, ++SI) {
  12035. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
  12036. // At most one list item can be an array item derived from a given
  12037. // variable in map clauses of the same construct.
  12038. if (CurrentRegionOnly &&
  12039. (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
  12040. isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
  12041. (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
  12042. isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
  12043. SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
  12044. diag::err_omp_multiple_array_items_in_map_clause)
  12045. << CI->getAssociatedExpression()->getSourceRange();
  12046. SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
  12047. diag::note_used_here)
  12048. << SI->getAssociatedExpression()->getSourceRange();
  12049. return true;
  12050. }
  12051. // Do both expressions have the same kind?
  12052. if (CI->getAssociatedExpression()->getStmtClass() !=
  12053. SI->getAssociatedExpression()->getStmtClass())
  12054. break;
  12055. // Are we dealing with different variables/fields?
  12056. if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
  12057. break;
  12058. }
  12059. // Check if the extra components of the expressions in the enclosing
  12060. // data environment are redundant for the current base declaration.
  12061. // If they are, the maps completely overlap, which is legal.
  12062. for (; SI != SE; ++SI) {
  12063. QualType Type;
  12064. if (const auto *ASE =
  12065. dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
  12066. Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
  12067. } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
  12068. SI->getAssociatedExpression())) {
  12069. const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
  12070. Type =
  12071. OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
  12072. }
  12073. if (Type.isNull() || Type->isAnyPointerType() ||
  12074. checkArrayExpressionDoesNotReferToWholeSize(
  12075. SemaRef, SI->getAssociatedExpression(), Type))
  12076. break;
  12077. }
  12078. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
  12079. // List items of map clauses in the same construct must not share
  12080. // original storage.
  12081. //
  12082. // If the expressions are exactly the same or one is a subset of the
  12083. // other, it means they are sharing storage.
  12084. if (CI == CE && SI == SE) {
  12085. if (CurrentRegionOnly) {
  12086. if (CKind == OMPC_map) {
  12087. SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
  12088. } else {
  12089. assert(CKind == OMPC_to || CKind == OMPC_from);
  12090. SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
  12091. << ERange;
  12092. }
  12093. SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
  12094. << RE->getSourceRange();
  12095. return true;
  12096. }
  12097. // If we find the same expression in the enclosing data environment,
  12098. // that is legal.
  12099. IsEnclosedByDataEnvironmentExpr = true;
  12100. return false;
  12101. }
  12102. QualType DerivedType =
  12103. std::prev(CI)->getAssociatedDeclaration()->getType();
  12104. SourceLocation DerivedLoc =
  12105. std::prev(CI)->getAssociatedExpression()->getExprLoc();
  12106. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
  12107. // If the type of a list item is a reference to a type T then the type
  12108. // will be considered to be T for all purposes of this clause.
  12109. DerivedType = DerivedType.getNonReferenceType();
  12110. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
  12111. // A variable for which the type is pointer and an array section
  12112. // derived from that variable must not appear as list items of map
  12113. // clauses of the same construct.
  12114. //
  12115. // Also, cover one of the cases in:
  12116. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
  12117. // If any part of the original storage of a list item has corresponding
  12118. // storage in the device data environment, all of the original storage
  12119. // must have corresponding storage in the device data environment.
  12120. //
  12121. if (DerivedType->isAnyPointerType()) {
  12122. if (CI == CE || SI == SE) {
  12123. SemaRef.Diag(
  12124. DerivedLoc,
  12125. diag::err_omp_pointer_mapped_along_with_derived_section)
  12126. << DerivedLoc;
  12127. SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
  12128. << RE->getSourceRange();
  12129. return true;
  12130. }
  12131. if (CI->getAssociatedExpression()->getStmtClass() !=
  12132. SI->getAssociatedExpression()->getStmtClass() ||
  12133. CI->getAssociatedDeclaration()->getCanonicalDecl() ==
  12134. SI->getAssociatedDeclaration()->getCanonicalDecl()) {
  12135. assert(CI != CE && SI != SE);
  12136. SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
  12137. << DerivedLoc;
  12138. SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
  12139. << RE->getSourceRange();
  12140. return true;
  12141. }
  12142. }
  12143. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
  12144. // List items of map clauses in the same construct must not share
  12145. // original storage.
  12146. //
  12147. // An expression is a subset of the other.
  12148. if (CurrentRegionOnly && (CI == CE || SI == SE)) {
  12149. if (CKind == OMPC_map) {
  12150. if (CI != CE || SI != SE) {
  12151. // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
  12152. // a pointer.
  12153. auto Begin =
  12154. CI != CE ? CurComponents.begin() : StackComponents.begin();
  12155. auto End = CI != CE ? CurComponents.end() : StackComponents.end();
  12156. auto It = Begin;
  12157. while (It != End && !It->getAssociatedDeclaration())
  12158. std::advance(It, 1);
  12159. assert(It != End &&
  12160. "Expected at least one component with the declaration.");
  12161. if (It != Begin && It->getAssociatedDeclaration()
  12162. ->getType()
  12163. .getCanonicalType()
  12164. ->isAnyPointerType()) {
  12165. IsEnclosedByDataEnvironmentExpr = false;
  12166. EnclosingExpr = nullptr;
  12167. return false;
  12168. }
  12169. }
  12170. SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
  12171. } else {
  12172. assert(CKind == OMPC_to || CKind == OMPC_from);
  12173. SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
  12174. << ERange;
  12175. }
  12176. SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
  12177. << RE->getSourceRange();
  12178. return true;
  12179. }
  12180. // The current expression uses the same base as other expression in the
  12181. // data environment but does not contain it completely.
  12182. if (!CurrentRegionOnly && SI != SE)
  12183. EnclosingExpr = RE;
  12184. // The current expression is a subset of the expression in the data
  12185. // environment.
  12186. IsEnclosedByDataEnvironmentExpr |=
  12187. (!CurrentRegionOnly && CI != CE && SI == SE);
  12188. return false;
  12189. });
  12190. if (CurrentRegionOnly)
  12191. return FoundError;
  12192. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
  12193. // If any part of the original storage of a list item has corresponding
  12194. // storage in the device data environment, all of the original storage must
  12195. // have corresponding storage in the device data environment.
  12196. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
  12197. // If a list item is an element of a structure, and a different element of
  12198. // the structure has a corresponding list item in the device data environment
  12199. // prior to a task encountering the construct associated with the map clause,
  12200. // then the list item must also have a corresponding list item in the device
  12201. // data environment prior to the task encountering the construct.
  12202. //
  12203. if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
  12204. SemaRef.Diag(ELoc,
  12205. diag::err_omp_original_storage_is_shared_and_does_not_contain)
  12206. << ERange;
  12207. SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
  12208. << EnclosingExpr->getSourceRange();
  12209. return true;
  12210. }
  12211. return FoundError;
  12212. }
  12213. // Look up the user-defined mapper given the mapper name and mapped type, and
  12214. // build a reference to it.
  12215. ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S,
  12216. CXXScopeSpec &MapperIdScopeSpec,
  12217. const DeclarationNameInfo &MapperId,
  12218. QualType Type, Expr *UnresolvedMapper) {
  12219. if (MapperIdScopeSpec.isInvalid())
  12220. return ExprError();
  12221. // Find all user-defined mappers with the given MapperId.
  12222. SmallVector<UnresolvedSet<8>, 4> Lookups;
  12223. LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName);
  12224. Lookup.suppressDiagnostics();
  12225. if (S) {
  12226. while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) {
  12227. NamedDecl *D = Lookup.getRepresentativeDecl();
  12228. while (S && !S->isDeclScope(D))
  12229. S = S->getParent();
  12230. if (S)
  12231. S = S->getParent();
  12232. Lookups.emplace_back();
  12233. Lookups.back().append(Lookup.begin(), Lookup.end());
  12234. Lookup.clear();
  12235. }
  12236. } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) {
  12237. // Extract the user-defined mappers with the given MapperId.
  12238. Lookups.push_back(UnresolvedSet<8>());
  12239. for (NamedDecl *D : ULE->decls()) {
  12240. auto *DMD = cast<OMPDeclareMapperDecl>(D);
  12241. assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation.");
  12242. Lookups.back().addDecl(DMD);
  12243. }
  12244. }
  12245. // Defer the lookup for dependent types. The results will be passed through
  12246. // UnresolvedMapper on instantiation.
  12247. if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() ||
  12248. Type->isInstantiationDependentType() ||
  12249. Type->containsUnexpandedParameterPack() ||
  12250. filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
  12251. return !D->isInvalidDecl() &&
  12252. (D->getType()->isDependentType() ||
  12253. D->getType()->isInstantiationDependentType() ||
  12254. D->getType()->containsUnexpandedParameterPack());
  12255. })) {
  12256. UnresolvedSet<8> URS;
  12257. for (const UnresolvedSet<8> &Set : Lookups) {
  12258. if (Set.empty())
  12259. continue;
  12260. URS.append(Set.begin(), Set.end());
  12261. }
  12262. return UnresolvedLookupExpr::Create(
  12263. SemaRef.Context, /*NamingClass=*/nullptr,
  12264. MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId,
  12265. /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end());
  12266. }
  12267. // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
  12268. // The type must be of struct, union or class type in C and C++
  12269. if (!Type->isStructureOrClassType() && !Type->isUnionType())
  12270. return ExprEmpty();
  12271. SourceLocation Loc = MapperId.getLoc();
  12272. // Perform argument dependent lookup.
  12273. if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet())
  12274. argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups);
  12275. // Return the first user-defined mapper with the desired type.
  12276. if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
  12277. Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * {
  12278. if (!D->isInvalidDecl() &&
  12279. SemaRef.Context.hasSameType(D->getType(), Type))
  12280. return D;
  12281. return nullptr;
  12282. }))
  12283. return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
  12284. // Find the first user-defined mapper with a type derived from the desired
  12285. // type.
  12286. if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
  12287. Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * {
  12288. if (!D->isInvalidDecl() &&
  12289. SemaRef.IsDerivedFrom(Loc, Type, D->getType()) &&
  12290. !Type.isMoreQualifiedThan(D->getType()))
  12291. return D;
  12292. return nullptr;
  12293. })) {
  12294. CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
  12295. /*DetectVirtual=*/false);
  12296. if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) {
  12297. if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
  12298. VD->getType().getUnqualifiedType()))) {
  12299. if (SemaRef.CheckBaseClassAccess(
  12300. Loc, VD->getType(), Type, Paths.front(),
  12301. /*DiagID=*/0) != Sema::AR_inaccessible) {
  12302. return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
  12303. }
  12304. }
  12305. }
  12306. }
  12307. // Report error if a mapper is specified, but cannot be found.
  12308. if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") {
  12309. SemaRef.Diag(Loc, diag::err_omp_invalid_mapper)
  12310. << Type << MapperId.getName();
  12311. return ExprError();
  12312. }
  12313. return ExprEmpty();
  12314. }
  12315. namespace {
  12316. // Utility struct that gathers all the related lists associated with a mappable
  12317. // expression.
  12318. struct MappableVarListInfo {
  12319. // The list of expressions.
  12320. ArrayRef<Expr *> VarList;
  12321. // The list of processed expressions.
  12322. SmallVector<Expr *, 16> ProcessedVarList;
  12323. // The mappble components for each expression.
  12324. OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
  12325. // The base declaration of the variable.
  12326. SmallVector<ValueDecl *, 16> VarBaseDeclarations;
  12327. // The reference to the user-defined mapper associated with every expression.
  12328. SmallVector<Expr *, 16> UDMapperList;
  12329. MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
  12330. // We have a list of components and base declarations for each entry in the
  12331. // variable list.
  12332. VarComponents.reserve(VarList.size());
  12333. VarBaseDeclarations.reserve(VarList.size());
  12334. }
  12335. };
  12336. }
  12337. // Check the validity of the provided variable list for the provided clause kind
  12338. // \a CKind. In the check process the valid expressions, mappable expression
  12339. // components, variables, and user-defined mappers are extracted and used to
  12340. // fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a
  12341. // UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec,
  12342. // and \a MapperId are expected to be valid if the clause kind is 'map'.
  12343. static void checkMappableExpressionList(
  12344. Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind,
  12345. MappableVarListInfo &MVLI, SourceLocation StartLoc,
  12346. CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId,
  12347. ArrayRef<Expr *> UnresolvedMappers,
  12348. OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
  12349. bool IsMapTypeImplicit = false) {
  12350. // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
  12351. assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
  12352. "Unexpected clause kind with mappable expressions!");
  12353. // If the identifier of user-defined mapper is not specified, it is "default".
  12354. // We do not change the actual name in this clause to distinguish whether a
  12355. // mapper is specified explicitly, i.e., it is not explicitly specified when
  12356. // MapperId.getName() is empty.
  12357. if (!MapperId.getName() || MapperId.getName().isEmpty()) {
  12358. auto &DeclNames = SemaRef.getASTContext().DeclarationNames;
  12359. MapperId.setName(DeclNames.getIdentifier(
  12360. &SemaRef.getASTContext().Idents.get("default")));
  12361. }
  12362. // Iterators to find the current unresolved mapper expression.
  12363. auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end();
  12364. bool UpdateUMIt = false;
  12365. Expr *UnresolvedMapper = nullptr;
  12366. // Keep track of the mappable components and base declarations in this clause.
  12367. // Each entry in the list is going to have a list of components associated. We
  12368. // record each set of the components so that we can build the clause later on.
  12369. // In the end we should have the same amount of declarations and component
  12370. // lists.
  12371. for (Expr *RE : MVLI.VarList) {
  12372. assert(RE && "Null expr in omp to/from/map clause");
  12373. SourceLocation ELoc = RE->getExprLoc();
  12374. // Find the current unresolved mapper expression.
  12375. if (UpdateUMIt && UMIt != UMEnd) {
  12376. UMIt++;
  12377. assert(
  12378. UMIt != UMEnd &&
  12379. "Expect the size of UnresolvedMappers to match with that of VarList");
  12380. }
  12381. UpdateUMIt = true;
  12382. if (UMIt != UMEnd)
  12383. UnresolvedMapper = *UMIt;
  12384. const Expr *VE = RE->IgnoreParenLValueCasts();
  12385. if (VE->isValueDependent() || VE->isTypeDependent() ||
  12386. VE->isInstantiationDependent() ||
  12387. VE->containsUnexpandedParameterPack()) {
  12388. // Try to find the associated user-defined mapper.
  12389. ExprResult ER = buildUserDefinedMapperRef(
  12390. SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
  12391. VE->getType().getCanonicalType(), UnresolvedMapper);
  12392. if (ER.isInvalid())
  12393. continue;
  12394. MVLI.UDMapperList.push_back(ER.get());
  12395. // We can only analyze this information once the missing information is
  12396. // resolved.
  12397. MVLI.ProcessedVarList.push_back(RE);
  12398. continue;
  12399. }
  12400. Expr *SimpleExpr = RE->IgnoreParenCasts();
  12401. if (!RE->IgnoreParenImpCasts()->isLValue()) {
  12402. SemaRef.Diag(ELoc,
  12403. diag::err_omp_expected_named_var_member_or_array_expression)
  12404. << RE->getSourceRange();
  12405. continue;
  12406. }
  12407. OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
  12408. ValueDecl *CurDeclaration = nullptr;
  12409. // Obtain the array or member expression bases if required. Also, fill the
  12410. // components array with all the components identified in the process.
  12411. const Expr *BE = checkMapClauseExpressionBase(
  12412. SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
  12413. if (!BE)
  12414. continue;
  12415. assert(!CurComponents.empty() &&
  12416. "Invalid mappable expression information.");
  12417. if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) {
  12418. // Add store "this" pointer to class in DSAStackTy for future checking
  12419. DSAS->addMappedClassesQualTypes(TE->getType());
  12420. // Try to find the associated user-defined mapper.
  12421. ExprResult ER = buildUserDefinedMapperRef(
  12422. SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
  12423. VE->getType().getCanonicalType(), UnresolvedMapper);
  12424. if (ER.isInvalid())
  12425. continue;
  12426. MVLI.UDMapperList.push_back(ER.get());
  12427. // Skip restriction checking for variable or field declarations
  12428. MVLI.ProcessedVarList.push_back(RE);
  12429. MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
  12430. MVLI.VarComponents.back().append(CurComponents.begin(),
  12431. CurComponents.end());
  12432. MVLI.VarBaseDeclarations.push_back(nullptr);
  12433. continue;
  12434. }
  12435. // For the following checks, we rely on the base declaration which is
  12436. // expected to be associated with the last component. The declaration is
  12437. // expected to be a variable or a field (if 'this' is being mapped).
  12438. CurDeclaration = CurComponents.back().getAssociatedDeclaration();
  12439. assert(CurDeclaration && "Null decl on map clause.");
  12440. assert(
  12441. CurDeclaration->isCanonicalDecl() &&
  12442. "Expecting components to have associated only canonical declarations.");
  12443. auto *VD = dyn_cast<VarDecl>(CurDeclaration);
  12444. const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
  12445. assert((VD || FD) && "Only variables or fields are expected here!");
  12446. (void)FD;
  12447. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
  12448. // threadprivate variables cannot appear in a map clause.
  12449. // OpenMP 4.5 [2.10.5, target update Construct]
  12450. // threadprivate variables cannot appear in a from clause.
  12451. if (VD && DSAS->isThreadPrivate(VD)) {
  12452. DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
  12453. SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
  12454. << getOpenMPClauseName(CKind);
  12455. reportOriginalDsa(SemaRef, DSAS, VD, DVar);
  12456. continue;
  12457. }
  12458. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
  12459. // A list item cannot appear in both a map clause and a data-sharing
  12460. // attribute clause on the same construct.
  12461. // Check conflicts with other map clause expressions. We check the conflicts
  12462. // with the current construct separately from the enclosing data
  12463. // environment, because the restrictions are different. We only have to
  12464. // check conflicts across regions for the map clauses.
  12465. if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
  12466. /*CurrentRegionOnly=*/true, CurComponents, CKind))
  12467. break;
  12468. if (CKind == OMPC_map &&
  12469. checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
  12470. /*CurrentRegionOnly=*/false, CurComponents, CKind))
  12471. break;
  12472. // OpenMP 4.5 [2.10.5, target update Construct]
  12473. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
  12474. // If the type of a list item is a reference to a type T then the type will
  12475. // be considered to be T for all purposes of this clause.
  12476. auto I = llvm::find_if(
  12477. CurComponents,
  12478. [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
  12479. return MC.getAssociatedDeclaration();
  12480. });
  12481. assert(I != CurComponents.end() && "Null decl on map clause.");
  12482. QualType Type =
  12483. I->getAssociatedDeclaration()->getType().getNonReferenceType();
  12484. // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
  12485. // A list item in a to or from clause must have a mappable type.
  12486. // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
  12487. // A list item must have a mappable type.
  12488. if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
  12489. DSAS, Type))
  12490. continue;
  12491. if (CKind == OMPC_map) {
  12492. // target enter data
  12493. // OpenMP [2.10.2, Restrictions, p. 99]
  12494. // A map-type must be specified in all map clauses and must be either
  12495. // to or alloc.
  12496. OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
  12497. if (DKind == OMPD_target_enter_data &&
  12498. !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
  12499. SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
  12500. << (IsMapTypeImplicit ? 1 : 0)
  12501. << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
  12502. << getOpenMPDirectiveName(DKind);
  12503. continue;
  12504. }
  12505. // target exit_data
  12506. // OpenMP [2.10.3, Restrictions, p. 102]
  12507. // A map-type must be specified in all map clauses and must be either
  12508. // from, release, or delete.
  12509. if (DKind == OMPD_target_exit_data &&
  12510. !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
  12511. MapType == OMPC_MAP_delete)) {
  12512. SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
  12513. << (IsMapTypeImplicit ? 1 : 0)
  12514. << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
  12515. << getOpenMPDirectiveName(DKind);
  12516. continue;
  12517. }
  12518. // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
  12519. // A list item cannot appear in both a map clause and a data-sharing
  12520. // attribute clause on the same construct
  12521. if (VD && isOpenMPTargetExecutionDirective(DKind)) {
  12522. DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
  12523. if (isOpenMPPrivate(DVar.CKind)) {
  12524. SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
  12525. << getOpenMPClauseName(DVar.CKind)
  12526. << getOpenMPClauseName(OMPC_map)
  12527. << getOpenMPDirectiveName(DSAS->getCurrentDirective());
  12528. reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
  12529. continue;
  12530. }
  12531. }
  12532. }
  12533. // Try to find the associated user-defined mapper.
  12534. ExprResult ER = buildUserDefinedMapperRef(
  12535. SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
  12536. Type.getCanonicalType(), UnresolvedMapper);
  12537. if (ER.isInvalid())
  12538. continue;
  12539. MVLI.UDMapperList.push_back(ER.get());
  12540. // Save the current expression.
  12541. MVLI.ProcessedVarList.push_back(RE);
  12542. // Store the components in the stack so that they can be used to check
  12543. // against other clauses later on.
  12544. DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
  12545. /*WhereFoundClauseKind=*/OMPC_map);
  12546. // Save the components and declaration to create the clause. For purposes of
  12547. // the clause creation, any component list that has has base 'this' uses
  12548. // null as base declaration.
  12549. MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
  12550. MVLI.VarComponents.back().append(CurComponents.begin(),
  12551. CurComponents.end());
  12552. MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
  12553. : CurDeclaration);
  12554. }
  12555. }
  12556. OMPClause *Sema::ActOnOpenMPMapClause(
  12557. ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
  12558. ArrayRef<SourceLocation> MapTypeModifiersLoc,
  12559. CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
  12560. OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc,
  12561. SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
  12562. const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
  12563. OpenMPMapModifierKind Modifiers[] = {OMPC_MAP_MODIFIER_unknown,
  12564. OMPC_MAP_MODIFIER_unknown,
  12565. OMPC_MAP_MODIFIER_unknown};
  12566. SourceLocation ModifiersLoc[OMPMapClause::NumberOfModifiers];
  12567. // Process map-type-modifiers, flag errors for duplicate modifiers.
  12568. unsigned Count = 0;
  12569. for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) {
  12570. if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown &&
  12571. llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) {
  12572. Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier);
  12573. continue;
  12574. }
  12575. assert(Count < OMPMapClause::NumberOfModifiers &&
  12576. "Modifiers exceed the allowed number of map type modifiers");
  12577. Modifiers[Count] = MapTypeModifiers[I];
  12578. ModifiersLoc[Count] = MapTypeModifiersLoc[I];
  12579. ++Count;
  12580. }
  12581. MappableVarListInfo MVLI(VarList);
  12582. checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc,
  12583. MapperIdScopeSpec, MapperId, UnresolvedMappers,
  12584. MapType, IsMapTypeImplicit);
  12585. // We need to produce a map clause even if we don't have variables so that
  12586. // other diagnostics related with non-existing map clauses are accurate.
  12587. return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList,
  12588. MVLI.VarBaseDeclarations, MVLI.VarComponents,
  12589. MVLI.UDMapperList, Modifiers, ModifiersLoc,
  12590. MapperIdScopeSpec.getWithLocInContext(Context),
  12591. MapperId, MapType, IsMapTypeImplicit, MapLoc);
  12592. }
  12593. QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
  12594. TypeResult ParsedType) {
  12595. assert(ParsedType.isUsable());
  12596. QualType ReductionType = GetTypeFromParser(ParsedType.get());
  12597. if (ReductionType.isNull())
  12598. return QualType();
  12599. // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
  12600. // A type name in a declare reduction directive cannot be a function type, an
  12601. // array type, a reference type, or a type qualified with const, volatile or
  12602. // restrict.
  12603. if (ReductionType.hasQualifiers()) {
  12604. Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
  12605. return QualType();
  12606. }
  12607. if (ReductionType->isFunctionType()) {
  12608. Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
  12609. return QualType();
  12610. }
  12611. if (ReductionType->isReferenceType()) {
  12612. Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
  12613. return QualType();
  12614. }
  12615. if (ReductionType->isArrayType()) {
  12616. Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
  12617. return QualType();
  12618. }
  12619. return ReductionType;
  12620. }
  12621. Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
  12622. Scope *S, DeclContext *DC, DeclarationName Name,
  12623. ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
  12624. AccessSpecifier AS, Decl *PrevDeclInScope) {
  12625. SmallVector<Decl *, 8> Decls;
  12626. Decls.reserve(ReductionTypes.size());
  12627. LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
  12628. forRedeclarationInCurContext());
  12629. // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
  12630. // A reduction-identifier may not be re-declared in the current scope for the
  12631. // same type or for a type that is compatible according to the base language
  12632. // rules.
  12633. llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
  12634. OMPDeclareReductionDecl *PrevDRD = nullptr;
  12635. bool InCompoundScope = true;
  12636. if (S != nullptr) {
  12637. // Find previous declaration with the same name not referenced in other
  12638. // declarations.
  12639. FunctionScopeInfo *ParentFn = getEnclosingFunction();
  12640. InCompoundScope =
  12641. (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
  12642. LookupName(Lookup, S);
  12643. FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
  12644. /*AllowInlineNamespace=*/false);
  12645. llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
  12646. LookupResult::Filter Filter = Lookup.makeFilter();
  12647. while (Filter.hasNext()) {
  12648. auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
  12649. if (InCompoundScope) {
  12650. auto I = UsedAsPrevious.find(PrevDecl);
  12651. if (I == UsedAsPrevious.end())
  12652. UsedAsPrevious[PrevDecl] = false;
  12653. if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
  12654. UsedAsPrevious[D] = true;
  12655. }
  12656. PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
  12657. PrevDecl->getLocation();
  12658. }
  12659. Filter.done();
  12660. if (InCompoundScope) {
  12661. for (const auto &PrevData : UsedAsPrevious) {
  12662. if (!PrevData.second) {
  12663. PrevDRD = PrevData.first;
  12664. break;
  12665. }
  12666. }
  12667. }
  12668. } else if (PrevDeclInScope != nullptr) {
  12669. auto *PrevDRDInScope = PrevDRD =
  12670. cast<OMPDeclareReductionDecl>(PrevDeclInScope);
  12671. do {
  12672. PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
  12673. PrevDRDInScope->getLocation();
  12674. PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
  12675. } while (PrevDRDInScope != nullptr);
  12676. }
  12677. for (const auto &TyData : ReductionTypes) {
  12678. const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
  12679. bool Invalid = false;
  12680. if (I != PreviousRedeclTypes.end()) {
  12681. Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
  12682. << TyData.first;
  12683. Diag(I->second, diag::note_previous_definition);
  12684. Invalid = true;
  12685. }
  12686. PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
  12687. auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
  12688. Name, TyData.first, PrevDRD);
  12689. DC->addDecl(DRD);
  12690. DRD->setAccess(AS);
  12691. Decls.push_back(DRD);
  12692. if (Invalid)
  12693. DRD->setInvalidDecl();
  12694. else
  12695. PrevDRD = DRD;
  12696. }
  12697. return DeclGroupPtrTy::make(
  12698. DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
  12699. }
  12700. void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
  12701. auto *DRD = cast<OMPDeclareReductionDecl>(D);
  12702. // Enter new function scope.
  12703. PushFunctionScope();
  12704. setFunctionHasBranchProtectedScope();
  12705. getCurFunction()->setHasOMPDeclareReductionCombiner();
  12706. if (S != nullptr)
  12707. PushDeclContext(S, DRD);
  12708. else
  12709. CurContext = DRD;
  12710. PushExpressionEvaluationContext(
  12711. ExpressionEvaluationContext::PotentiallyEvaluated);
  12712. QualType ReductionType = DRD->getType();
  12713. // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
  12714. // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
  12715. // uses semantics of argument handles by value, but it should be passed by
  12716. // reference. C lang does not support references, so pass all parameters as
  12717. // pointers.
  12718. // Create 'T omp_in;' variable.
  12719. VarDecl *OmpInParm =
  12720. buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
  12721. // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
  12722. // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
  12723. // uses semantics of argument handles by value, but it should be passed by
  12724. // reference. C lang does not support references, so pass all parameters as
  12725. // pointers.
  12726. // Create 'T omp_out;' variable.
  12727. VarDecl *OmpOutParm =
  12728. buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
  12729. if (S != nullptr) {
  12730. PushOnScopeChains(OmpInParm, S);
  12731. PushOnScopeChains(OmpOutParm, S);
  12732. } else {
  12733. DRD->addDecl(OmpInParm);
  12734. DRD->addDecl(OmpOutParm);
  12735. }
  12736. Expr *InE =
  12737. ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
  12738. Expr *OutE =
  12739. ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
  12740. DRD->setCombinerData(InE, OutE);
  12741. }
  12742. void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
  12743. auto *DRD = cast<OMPDeclareReductionDecl>(D);
  12744. DiscardCleanupsInEvaluationContext();
  12745. PopExpressionEvaluationContext();
  12746. PopDeclContext();
  12747. PopFunctionScopeInfo();
  12748. if (Combiner != nullptr)
  12749. DRD->setCombiner(Combiner);
  12750. else
  12751. DRD->setInvalidDecl();
  12752. }
  12753. VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
  12754. auto *DRD = cast<OMPDeclareReductionDecl>(D);
  12755. // Enter new function scope.
  12756. PushFunctionScope();
  12757. setFunctionHasBranchProtectedScope();
  12758. if (S != nullptr)
  12759. PushDeclContext(S, DRD);
  12760. else
  12761. CurContext = DRD;
  12762. PushExpressionEvaluationContext(
  12763. ExpressionEvaluationContext::PotentiallyEvaluated);
  12764. QualType ReductionType = DRD->getType();
  12765. // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
  12766. // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
  12767. // uses semantics of argument handles by value, but it should be passed by
  12768. // reference. C lang does not support references, so pass all parameters as
  12769. // pointers.
  12770. // Create 'T omp_priv;' variable.
  12771. VarDecl *OmpPrivParm =
  12772. buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
  12773. // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
  12774. // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
  12775. // uses semantics of argument handles by value, but it should be passed by
  12776. // reference. C lang does not support references, so pass all parameters as
  12777. // pointers.
  12778. // Create 'T omp_orig;' variable.
  12779. VarDecl *OmpOrigParm =
  12780. buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
  12781. if (S != nullptr) {
  12782. PushOnScopeChains(OmpPrivParm, S);
  12783. PushOnScopeChains(OmpOrigParm, S);
  12784. } else {
  12785. DRD->addDecl(OmpPrivParm);
  12786. DRD->addDecl(OmpOrigParm);
  12787. }
  12788. Expr *OrigE =
  12789. ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
  12790. Expr *PrivE =
  12791. ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
  12792. DRD->setInitializerData(OrigE, PrivE);
  12793. return OmpPrivParm;
  12794. }
  12795. void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
  12796. VarDecl *OmpPrivParm) {
  12797. auto *DRD = cast<OMPDeclareReductionDecl>(D);
  12798. DiscardCleanupsInEvaluationContext();
  12799. PopExpressionEvaluationContext();
  12800. PopDeclContext();
  12801. PopFunctionScopeInfo();
  12802. if (Initializer != nullptr) {
  12803. DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
  12804. } else if (OmpPrivParm->hasInit()) {
  12805. DRD->setInitializer(OmpPrivParm->getInit(),
  12806. OmpPrivParm->isDirectInit()
  12807. ? OMPDeclareReductionDecl::DirectInit
  12808. : OMPDeclareReductionDecl::CopyInit);
  12809. } else {
  12810. DRD->setInvalidDecl();
  12811. }
  12812. }
  12813. Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
  12814. Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
  12815. for (Decl *D : DeclReductions.get()) {
  12816. if (IsValid) {
  12817. if (S)
  12818. PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
  12819. /*AddToContext=*/false);
  12820. } else {
  12821. D->setInvalidDecl();
  12822. }
  12823. }
  12824. return DeclReductions;
  12825. }
  12826. TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) {
  12827. TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
  12828. QualType T = TInfo->getType();
  12829. if (D.isInvalidType())
  12830. return true;
  12831. if (getLangOpts().CPlusPlus) {
  12832. // Check that there are no default arguments (C++ only).
  12833. CheckExtraCXXDefaultArguments(D);
  12834. }
  12835. return CreateParsedType(T, TInfo);
  12836. }
  12837. QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc,
  12838. TypeResult ParsedType) {
  12839. assert(ParsedType.isUsable() && "Expect usable parsed mapper type");
  12840. QualType MapperType = GetTypeFromParser(ParsedType.get());
  12841. assert(!MapperType.isNull() && "Expect valid mapper type");
  12842. // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
  12843. // The type must be of struct, union or class type in C and C++
  12844. if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) {
  12845. Diag(TyLoc, diag::err_omp_mapper_wrong_type);
  12846. return QualType();
  12847. }
  12848. return MapperType;
  12849. }
  12850. OMPDeclareMapperDecl *Sema::ActOnOpenMPDeclareMapperDirectiveStart(
  12851. Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType,
  12852. SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS,
  12853. Decl *PrevDeclInScope) {
  12854. LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName,
  12855. forRedeclarationInCurContext());
  12856. // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
  12857. // A mapper-identifier may not be redeclared in the current scope for the
  12858. // same type or for a type that is compatible according to the base language
  12859. // rules.
  12860. llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
  12861. OMPDeclareMapperDecl *PrevDMD = nullptr;
  12862. bool InCompoundScope = true;
  12863. if (S != nullptr) {
  12864. // Find previous declaration with the same name not referenced in other
  12865. // declarations.
  12866. FunctionScopeInfo *ParentFn = getEnclosingFunction();
  12867. InCompoundScope =
  12868. (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
  12869. LookupName(Lookup, S);
  12870. FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
  12871. /*AllowInlineNamespace=*/false);
  12872. llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious;
  12873. LookupResult::Filter Filter = Lookup.makeFilter();
  12874. while (Filter.hasNext()) {
  12875. auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next());
  12876. if (InCompoundScope) {
  12877. auto I = UsedAsPrevious.find(PrevDecl);
  12878. if (I == UsedAsPrevious.end())
  12879. UsedAsPrevious[PrevDecl] = false;
  12880. if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope())
  12881. UsedAsPrevious[D] = true;
  12882. }
  12883. PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
  12884. PrevDecl->getLocation();
  12885. }
  12886. Filter.done();
  12887. if (InCompoundScope) {
  12888. for (const auto &PrevData : UsedAsPrevious) {
  12889. if (!PrevData.second) {
  12890. PrevDMD = PrevData.first;
  12891. break;
  12892. }
  12893. }
  12894. }
  12895. } else if (PrevDeclInScope) {
  12896. auto *PrevDMDInScope = PrevDMD =
  12897. cast<OMPDeclareMapperDecl>(PrevDeclInScope);
  12898. do {
  12899. PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] =
  12900. PrevDMDInScope->getLocation();
  12901. PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope();
  12902. } while (PrevDMDInScope != nullptr);
  12903. }
  12904. const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType());
  12905. bool Invalid = false;
  12906. if (I != PreviousRedeclTypes.end()) {
  12907. Diag(StartLoc, diag::err_omp_declare_mapper_redefinition)
  12908. << MapperType << Name;
  12909. Diag(I->second, diag::note_previous_definition);
  12910. Invalid = true;
  12911. }
  12912. auto *DMD = OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name,
  12913. MapperType, VN, PrevDMD);
  12914. DC->addDecl(DMD);
  12915. DMD->setAccess(AS);
  12916. if (Invalid)
  12917. DMD->setInvalidDecl();
  12918. // Enter new function scope.
  12919. PushFunctionScope();
  12920. setFunctionHasBranchProtectedScope();
  12921. CurContext = DMD;
  12922. return DMD;
  12923. }
  12924. void Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(OMPDeclareMapperDecl *DMD,
  12925. Scope *S,
  12926. QualType MapperType,
  12927. SourceLocation StartLoc,
  12928. DeclarationName VN) {
  12929. VarDecl *VD = buildVarDecl(*this, StartLoc, MapperType, VN.getAsString());
  12930. if (S)
  12931. PushOnScopeChains(VD, S);
  12932. else
  12933. DMD->addDecl(VD);
  12934. Expr *MapperVarRefExpr = buildDeclRefExpr(*this, VD, MapperType, StartLoc);
  12935. DMD->setMapperVarRef(MapperVarRefExpr);
  12936. }
  12937. Sema::DeclGroupPtrTy
  12938. Sema::ActOnOpenMPDeclareMapperDirectiveEnd(OMPDeclareMapperDecl *D, Scope *S,
  12939. ArrayRef<OMPClause *> ClauseList) {
  12940. PopDeclContext();
  12941. PopFunctionScopeInfo();
  12942. if (D) {
  12943. if (S)
  12944. PushOnScopeChains(D, S, /*AddToContext=*/false);
  12945. D->CreateClauses(Context, ClauseList);
  12946. }
  12947. return DeclGroupPtrTy::make(DeclGroupRef(D));
  12948. }
  12949. OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
  12950. SourceLocation StartLoc,
  12951. SourceLocation LParenLoc,
  12952. SourceLocation EndLoc) {
  12953. Expr *ValExpr = NumTeams;
  12954. Stmt *HelperValStmt = nullptr;
  12955. // OpenMP [teams Constrcut, Restrictions]
  12956. // The num_teams expression must evaluate to a positive integer value.
  12957. if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
  12958. /*StrictlyPositive=*/true))
  12959. return nullptr;
  12960. OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
  12961. OpenMPDirectiveKind CaptureRegion =
  12962. getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
  12963. if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
  12964. ValExpr = MakeFullExpr(ValExpr).get();
  12965. llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
  12966. ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
  12967. HelperValStmt = buildPreInits(Context, Captures);
  12968. }
  12969. return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
  12970. StartLoc, LParenLoc, EndLoc);
  12971. }
  12972. OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
  12973. SourceLocation StartLoc,
  12974. SourceLocation LParenLoc,
  12975. SourceLocation EndLoc) {
  12976. Expr *ValExpr = ThreadLimit;
  12977. Stmt *HelperValStmt = nullptr;
  12978. // OpenMP [teams Constrcut, Restrictions]
  12979. // The thread_limit expression must evaluate to a positive integer value.
  12980. if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
  12981. /*StrictlyPositive=*/true))
  12982. return nullptr;
  12983. OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
  12984. OpenMPDirectiveKind CaptureRegion =
  12985. getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
  12986. if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
  12987. ValExpr = MakeFullExpr(ValExpr).get();
  12988. llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
  12989. ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
  12990. HelperValStmt = buildPreInits(Context, Captures);
  12991. }
  12992. return new (Context) OMPThreadLimitClause(
  12993. ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
  12994. }
  12995. OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
  12996. SourceLocation StartLoc,
  12997. SourceLocation LParenLoc,
  12998. SourceLocation EndLoc) {
  12999. Expr *ValExpr = Priority;
  13000. // OpenMP [2.9.1, task Constrcut]
  13001. // The priority-value is a non-negative numerical scalar expression.
  13002. if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
  13003. /*StrictlyPositive=*/false))
  13004. return nullptr;
  13005. return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
  13006. }
  13007. OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
  13008. SourceLocation StartLoc,
  13009. SourceLocation LParenLoc,
  13010. SourceLocation EndLoc) {
  13011. Expr *ValExpr = Grainsize;
  13012. // OpenMP [2.9.2, taskloop Constrcut]
  13013. // The parameter of the grainsize clause must be a positive integer
  13014. // expression.
  13015. if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
  13016. /*StrictlyPositive=*/true))
  13017. return nullptr;
  13018. return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
  13019. }
  13020. OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
  13021. SourceLocation StartLoc,
  13022. SourceLocation LParenLoc,
  13023. SourceLocation EndLoc) {
  13024. Expr *ValExpr = NumTasks;
  13025. // OpenMP [2.9.2, taskloop Constrcut]
  13026. // The parameter of the num_tasks clause must be a positive integer
  13027. // expression.
  13028. if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
  13029. /*StrictlyPositive=*/true))
  13030. return nullptr;
  13031. return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
  13032. }
  13033. OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
  13034. SourceLocation LParenLoc,
  13035. SourceLocation EndLoc) {
  13036. // OpenMP [2.13.2, critical construct, Description]
  13037. // ... where hint-expression is an integer constant expression that evaluates
  13038. // to a valid lock hint.
  13039. ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
  13040. if (HintExpr.isInvalid())
  13041. return nullptr;
  13042. return new (Context)
  13043. OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
  13044. }
  13045. OMPClause *Sema::ActOnOpenMPDistScheduleClause(
  13046. OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
  13047. SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
  13048. SourceLocation EndLoc) {
  13049. if (Kind == OMPC_DIST_SCHEDULE_unknown) {
  13050. std::string Values;
  13051. Values += "'";
  13052. Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
  13053. Values += "'";
  13054. Diag(KindLoc, diag::err_omp_unexpected_clause_value)
  13055. << Values << getOpenMPClauseName(OMPC_dist_schedule);
  13056. return nullptr;
  13057. }
  13058. Expr *ValExpr = ChunkSize;
  13059. Stmt *HelperValStmt = nullptr;
  13060. if (ChunkSize) {
  13061. if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
  13062. !ChunkSize->isInstantiationDependent() &&
  13063. !ChunkSize->containsUnexpandedParameterPack()) {
  13064. SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
  13065. ExprResult Val =
  13066. PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
  13067. if (Val.isInvalid())
  13068. return nullptr;
  13069. ValExpr = Val.get();
  13070. // OpenMP [2.7.1, Restrictions]
  13071. // chunk_size must be a loop invariant integer expression with a positive
  13072. // value.
  13073. llvm::APSInt Result;
  13074. if (ValExpr->isIntegerConstantExpr(Result, Context)) {
  13075. if (Result.isSigned() && !Result.isStrictlyPositive()) {
  13076. Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
  13077. << "dist_schedule" << ChunkSize->getSourceRange();
  13078. return nullptr;
  13079. }
  13080. } else if (getOpenMPCaptureRegionForClause(
  13081. DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
  13082. OMPD_unknown &&
  13083. !CurContext->isDependentContext()) {
  13084. ValExpr = MakeFullExpr(ValExpr).get();
  13085. llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
  13086. ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
  13087. HelperValStmt = buildPreInits(Context, Captures);
  13088. }
  13089. }
  13090. }
  13091. return new (Context)
  13092. OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
  13093. Kind, ValExpr, HelperValStmt);
  13094. }
  13095. OMPClause *Sema::ActOnOpenMPDefaultmapClause(
  13096. OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
  13097. SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
  13098. SourceLocation KindLoc, SourceLocation EndLoc) {
  13099. // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
  13100. if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
  13101. std::string Value;
  13102. SourceLocation Loc;
  13103. Value += "'";
  13104. if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
  13105. Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
  13106. OMPC_DEFAULTMAP_MODIFIER_tofrom);
  13107. Loc = MLoc;
  13108. } else {
  13109. Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
  13110. OMPC_DEFAULTMAP_scalar);
  13111. Loc = KindLoc;
  13112. }
  13113. Value += "'";
  13114. Diag(Loc, diag::err_omp_unexpected_clause_value)
  13115. << Value << getOpenMPClauseName(OMPC_defaultmap);
  13116. return nullptr;
  13117. }
  13118. DSAStack->setDefaultDMAToFromScalar(StartLoc);
  13119. return new (Context)
  13120. OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
  13121. }
  13122. bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
  13123. DeclContext *CurLexicalContext = getCurLexicalContext();
  13124. if (!CurLexicalContext->isFileContext() &&
  13125. !CurLexicalContext->isExternCContext() &&
  13126. !CurLexicalContext->isExternCXXContext() &&
  13127. !isa<CXXRecordDecl>(CurLexicalContext) &&
  13128. !isa<ClassTemplateDecl>(CurLexicalContext) &&
  13129. !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
  13130. !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
  13131. Diag(Loc, diag::err_omp_region_not_file_context);
  13132. return false;
  13133. }
  13134. ++DeclareTargetNestingLevel;
  13135. return true;
  13136. }
  13137. void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
  13138. assert(DeclareTargetNestingLevel > 0 &&
  13139. "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
  13140. --DeclareTargetNestingLevel;
  13141. }
  13142. void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
  13143. CXXScopeSpec &ScopeSpec,
  13144. const DeclarationNameInfo &Id,
  13145. OMPDeclareTargetDeclAttr::MapTypeTy MT,
  13146. NamedDeclSetType &SameDirectiveDecls) {
  13147. LookupResult Lookup(*this, Id, LookupOrdinaryName);
  13148. LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
  13149. if (Lookup.isAmbiguous())
  13150. return;
  13151. Lookup.suppressDiagnostics();
  13152. if (!Lookup.isSingleResult()) {
  13153. if (TypoCorrection Corrected =
  13154. CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
  13155. llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
  13156. CTK_ErrorRecovery)) {
  13157. diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
  13158. << Id.getName());
  13159. checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
  13160. return;
  13161. }
  13162. Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
  13163. return;
  13164. }
  13165. NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
  13166. if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
  13167. isa<FunctionTemplateDecl>(ND)) {
  13168. if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
  13169. Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
  13170. llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
  13171. OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
  13172. cast<ValueDecl>(ND));
  13173. if (!Res) {
  13174. auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
  13175. ND->addAttr(A);
  13176. if (ASTMutationListener *ML = Context.getASTMutationListener())
  13177. ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
  13178. checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
  13179. } else if (*Res != MT) {
  13180. Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
  13181. << Id.getName();
  13182. }
  13183. } else {
  13184. Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
  13185. }
  13186. }
  13187. static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
  13188. Sema &SemaRef, Decl *D) {
  13189. if (!D || !isa<VarDecl>(D))
  13190. return;
  13191. auto *VD = cast<VarDecl>(D);
  13192. if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
  13193. return;
  13194. SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
  13195. SemaRef.Diag(SL, diag::note_used_here) << SR;
  13196. }
  13197. static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
  13198. Sema &SemaRef, DSAStackTy *Stack,
  13199. ValueDecl *VD) {
  13200. return VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
  13201. checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
  13202. /*FullCheck=*/false);
  13203. }
  13204. void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
  13205. SourceLocation IdLoc) {
  13206. if (!D || D->isInvalidDecl())
  13207. return;
  13208. SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
  13209. SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
  13210. if (auto *VD = dyn_cast<VarDecl>(D)) {
  13211. // Only global variables can be marked as declare target.
  13212. if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
  13213. !VD->isStaticDataMember())
  13214. return;
  13215. // 2.10.6: threadprivate variable cannot appear in a declare target
  13216. // directive.
  13217. if (DSAStack->isThreadPrivate(VD)) {
  13218. Diag(SL, diag::err_omp_threadprivate_in_target);
  13219. reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
  13220. return;
  13221. }
  13222. }
  13223. if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
  13224. D = FTD->getTemplatedDecl();
  13225. if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
  13226. llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
  13227. OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
  13228. if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
  13229. assert(IdLoc.isValid() && "Source location is expected");
  13230. Diag(IdLoc, diag::err_omp_function_in_link_clause);
  13231. Diag(FD->getLocation(), diag::note_defined_here) << FD;
  13232. return;
  13233. }
  13234. }
  13235. if (auto *VD = dyn_cast<ValueDecl>(D)) {
  13236. // Problem if any with var declared with incomplete type will be reported
  13237. // as normal, so no need to check it here.
  13238. if ((E || !VD->getType()->isIncompleteType()) &&
  13239. !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
  13240. return;
  13241. if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
  13242. // Checking declaration inside declare target region.
  13243. if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
  13244. isa<FunctionTemplateDecl>(D)) {
  13245. auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
  13246. Context, OMPDeclareTargetDeclAttr::MT_To);
  13247. D->addAttr(A);
  13248. if (ASTMutationListener *ML = Context.getASTMutationListener())
  13249. ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
  13250. }
  13251. return;
  13252. }
  13253. }
  13254. if (!E)
  13255. return;
  13256. checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
  13257. }
  13258. OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
  13259. CXXScopeSpec &MapperIdScopeSpec,
  13260. DeclarationNameInfo &MapperId,
  13261. const OMPVarListLocTy &Locs,
  13262. ArrayRef<Expr *> UnresolvedMappers) {
  13263. MappableVarListInfo MVLI(VarList);
  13264. checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc,
  13265. MapperIdScopeSpec, MapperId, UnresolvedMappers);
  13266. if (MVLI.ProcessedVarList.empty())
  13267. return nullptr;
  13268. return OMPToClause::Create(
  13269. Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
  13270. MVLI.VarComponents, MVLI.UDMapperList,
  13271. MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
  13272. }
  13273. OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
  13274. CXXScopeSpec &MapperIdScopeSpec,
  13275. DeclarationNameInfo &MapperId,
  13276. const OMPVarListLocTy &Locs,
  13277. ArrayRef<Expr *> UnresolvedMappers) {
  13278. MappableVarListInfo MVLI(VarList);
  13279. checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc,
  13280. MapperIdScopeSpec, MapperId, UnresolvedMappers);
  13281. if (MVLI.ProcessedVarList.empty())
  13282. return nullptr;
  13283. return OMPFromClause::Create(
  13284. Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
  13285. MVLI.VarComponents, MVLI.UDMapperList,
  13286. MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
  13287. }
  13288. OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
  13289. const OMPVarListLocTy &Locs) {
  13290. MappableVarListInfo MVLI(VarList);
  13291. SmallVector<Expr *, 8> PrivateCopies;
  13292. SmallVector<Expr *, 8> Inits;
  13293. for (Expr *RefExpr : VarList) {
  13294. assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
  13295. SourceLocation ELoc;
  13296. SourceRange ERange;
  13297. Expr *SimpleRefExpr = RefExpr;
  13298. auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
  13299. if (Res.second) {
  13300. // It will be analyzed later.
  13301. MVLI.ProcessedVarList.push_back(RefExpr);
  13302. PrivateCopies.push_back(nullptr);
  13303. Inits.push_back(nullptr);
  13304. }
  13305. ValueDecl *D = Res.first;
  13306. if (!D)
  13307. continue;
  13308. QualType Type = D->getType();
  13309. Type = Type.getNonReferenceType().getUnqualifiedType();
  13310. auto *VD = dyn_cast<VarDecl>(D);
  13311. // Item should be a pointer or reference to pointer.
  13312. if (!Type->isPointerType()) {
  13313. Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
  13314. << 0 << RefExpr->getSourceRange();
  13315. continue;
  13316. }
  13317. // Build the private variable and the expression that refers to it.
  13318. auto VDPrivate =
  13319. buildVarDecl(*this, ELoc, Type, D->getName(),
  13320. D->hasAttrs() ? &D->getAttrs() : nullptr,
  13321. VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
  13322. if (VDPrivate->isInvalidDecl())
  13323. continue;
  13324. CurContext->addDecl(VDPrivate);
  13325. DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
  13326. *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
  13327. // Add temporary variable to initialize the private copy of the pointer.
  13328. VarDecl *VDInit =
  13329. buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
  13330. DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
  13331. *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
  13332. AddInitializerToDecl(VDPrivate,
  13333. DefaultLvalueConversion(VDInitRefExpr).get(),
  13334. /*DirectInit=*/false);
  13335. // If required, build a capture to implement the privatization initialized
  13336. // with the current list item value.
  13337. DeclRefExpr *Ref = nullptr;
  13338. if (!VD)
  13339. Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
  13340. MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
  13341. PrivateCopies.push_back(VDPrivateRefExpr);
  13342. Inits.push_back(VDInitRefExpr);
  13343. // We need to add a data sharing attribute for this variable to make sure it
  13344. // is correctly captured. A variable that shows up in a use_device_ptr has
  13345. // similar properties of a first private variable.
  13346. DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
  13347. // Create a mappable component for the list item. List items in this clause
  13348. // only need a component.
  13349. MVLI.VarBaseDeclarations.push_back(D);
  13350. MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
  13351. MVLI.VarComponents.back().push_back(
  13352. OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
  13353. }
  13354. if (MVLI.ProcessedVarList.empty())
  13355. return nullptr;
  13356. return OMPUseDevicePtrClause::Create(
  13357. Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits,
  13358. MVLI.VarBaseDeclarations, MVLI.VarComponents);
  13359. }
  13360. OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
  13361. const OMPVarListLocTy &Locs) {
  13362. MappableVarListInfo MVLI(VarList);
  13363. for (Expr *RefExpr : VarList) {
  13364. assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
  13365. SourceLocation ELoc;
  13366. SourceRange ERange;
  13367. Expr *SimpleRefExpr = RefExpr;
  13368. auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
  13369. if (Res.second) {
  13370. // It will be analyzed later.
  13371. MVLI.ProcessedVarList.push_back(RefExpr);
  13372. }
  13373. ValueDecl *D = Res.first;
  13374. if (!D)
  13375. continue;
  13376. QualType Type = D->getType();
  13377. // item should be a pointer or array or reference to pointer or array
  13378. if (!Type.getNonReferenceType()->isPointerType() &&
  13379. !Type.getNonReferenceType()->isArrayType()) {
  13380. Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
  13381. << 0 << RefExpr->getSourceRange();
  13382. continue;
  13383. }
  13384. // Check if the declaration in the clause does not show up in any data
  13385. // sharing attribute.
  13386. DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
  13387. if (isOpenMPPrivate(DVar.CKind)) {
  13388. Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
  13389. << getOpenMPClauseName(DVar.CKind)
  13390. << getOpenMPClauseName(OMPC_is_device_ptr)
  13391. << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
  13392. reportOriginalDsa(*this, DSAStack, D, DVar);
  13393. continue;
  13394. }
  13395. const Expr *ConflictExpr;
  13396. if (DSAStack->checkMappableExprComponentListsForDecl(
  13397. D, /*CurrentRegionOnly=*/true,
  13398. [&ConflictExpr](
  13399. OMPClauseMappableExprCommon::MappableExprComponentListRef R,
  13400. OpenMPClauseKind) -> bool {
  13401. ConflictExpr = R.front().getAssociatedExpression();
  13402. return true;
  13403. })) {
  13404. Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
  13405. Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
  13406. << ConflictExpr->getSourceRange();
  13407. continue;
  13408. }
  13409. // Store the components in the stack so that they can be used to check
  13410. // against other clauses later on.
  13411. OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
  13412. DSAStack->addMappableExpressionComponents(
  13413. D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
  13414. // Record the expression we've just processed.
  13415. MVLI.ProcessedVarList.push_back(SimpleRefExpr);
  13416. // Create a mappable component for the list item. List items in this clause
  13417. // only need a component. We use a null declaration to signal fields in
  13418. // 'this'.
  13419. assert((isa<DeclRefExpr>(SimpleRefExpr) ||
  13420. isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
  13421. "Unexpected device pointer expression!");
  13422. MVLI.VarBaseDeclarations.push_back(
  13423. isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
  13424. MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
  13425. MVLI.VarComponents.back().push_back(MC);
  13426. }
  13427. if (MVLI.ProcessedVarList.empty())
  13428. return nullptr;
  13429. return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList,
  13430. MVLI.VarBaseDeclarations,
  13431. MVLI.VarComponents);
  13432. }