validate_builtins.cpp 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184
  1. // Copyright (c) 2018 Google LLC.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Validates correctness of built-in variables.
  15. #include "source/val/validate.h"
  16. #include <functional>
  17. #include <list>
  18. #include <map>
  19. #include <set>
  20. #include <sstream>
  21. #include <stack>
  22. #include <string>
  23. #include <unordered_map>
  24. #include <vector>
  25. #include "source/diagnostic.h"
  26. #include "source/opcode.h"
  27. #include "source/spirv_target_env.h"
  28. #include "source/util/bitutils.h"
  29. #include "source/val/instruction.h"
  30. #include "source/val/validation_state.h"
  31. namespace spvtools {
  32. namespace val {
  33. namespace {
  34. // Returns a short textual description of the id defined by the given
  35. // instruction.
  36. std::string GetIdDesc(const Instruction& inst) {
  37. std::ostringstream ss;
  38. ss << "ID <" << inst.id() << "> (Op" << spvOpcodeString(inst.opcode()) << ")";
  39. return ss.str();
  40. }
  41. // Gets underlying data type which is
  42. // - member type if instruction is OpTypeStruct
  43. // (member index is taken from decoration).
  44. // - data type if id creates a pointer.
  45. // - type of the constant if instruction is OpConst or OpSpecConst.
  46. //
  47. // Fails in any other case. The function is based on built-ins allowed by
  48. // the Vulkan spec.
  49. // TODO: If non-Vulkan validation rules are added then it might need
  50. // to be refactored.
  51. spv_result_t GetUnderlyingType(ValidationState_t& _,
  52. const Decoration& decoration,
  53. const Instruction& inst,
  54. uint32_t* underlying_type) {
  55. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  56. if (inst.opcode() != SpvOpTypeStruct) {
  57. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  58. << GetIdDesc(inst)
  59. << "Attempted to get underlying data type via member index for "
  60. "non-struct type.";
  61. }
  62. *underlying_type = inst.word(decoration.struct_member_index() + 2);
  63. return SPV_SUCCESS;
  64. }
  65. if (inst.opcode() == SpvOpTypeStruct) {
  66. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  67. << GetIdDesc(inst)
  68. << " did not find an member index to get underlying data type for "
  69. "struct type.";
  70. }
  71. if (spvOpcodeIsConstant(inst.opcode())) {
  72. *underlying_type = inst.type_id();
  73. return SPV_SUCCESS;
  74. }
  75. uint32_t storage_class = 0;
  76. if (!_.GetPointerTypeInfo(inst.type_id(), underlying_type, &storage_class)) {
  77. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  78. << GetIdDesc(inst)
  79. << " is decorated with BuiltIn. BuiltIn decoration should only be "
  80. "applied to struct types, variables and constants.";
  81. }
  82. return SPV_SUCCESS;
  83. }
  84. // Returns Storage Class used by the instruction if applicable.
  85. // Returns SpvStorageClassMax if not.
  86. SpvStorageClass GetStorageClass(const Instruction& inst) {
  87. switch (inst.opcode()) {
  88. case SpvOpTypePointer:
  89. case SpvOpTypeForwardPointer: {
  90. return SpvStorageClass(inst.word(2));
  91. }
  92. case SpvOpVariable: {
  93. return SpvStorageClass(inst.word(3));
  94. }
  95. case SpvOpGenericCastToPtrExplicit: {
  96. return SpvStorageClass(inst.word(4));
  97. }
  98. default: { break; }
  99. }
  100. return SpvStorageClassMax;
  101. }
  102. bool IsBuiltInValidForWebGPU(SpvBuiltIn label) {
  103. switch (label) {
  104. case SpvBuiltInPosition:
  105. case SpvBuiltInVertexIndex:
  106. case SpvBuiltInInstanceIndex:
  107. case SpvBuiltInFrontFacing:
  108. case SpvBuiltInFragCoord:
  109. case SpvBuiltInFragDepth:
  110. case SpvBuiltInNumWorkgroups:
  111. case SpvBuiltInWorkgroupSize:
  112. case SpvBuiltInLocalInvocationId:
  113. case SpvBuiltInGlobalInvocationId:
  114. case SpvBuiltInLocalInvocationIndex: {
  115. return true;
  116. }
  117. default:
  118. break;
  119. }
  120. return false;
  121. }
  122. // Helper class managing validation of built-ins.
  123. // TODO: Generic functionality of this class can be moved into
  124. // ValidationState_t to be made available to other users.
  125. class BuiltInsValidator {
  126. public:
  127. BuiltInsValidator(ValidationState_t& vstate) : _(vstate) {}
  128. // Run validation.
  129. spv_result_t Run();
  130. private:
  131. // Goes through all decorations in the module, if decoration is BuiltIn
  132. // calls ValidateSingleBuiltInAtDefinition().
  133. spv_result_t ValidateBuiltInsAtDefinition();
  134. // Validates the instruction defining an id with built-in decoration.
  135. // Can be called multiple times for the same id, if multiple built-ins are
  136. // specified. Seeds id_to_at_reference_checks_ with decorated ids if needed.
  137. spv_result_t ValidateSingleBuiltInAtDefinition(const Decoration& decoration,
  138. const Instruction& inst);
  139. // The following section contains functions which are called when id defined
  140. // by |inst| is decorated with BuiltIn |decoration|.
  141. // Most functions are specific to a single built-in and have naming scheme:
  142. // ValidateXYZAtDefinition. Some functions are common to multiple kinds of
  143. // BuiltIn.
  144. spv_result_t ValidateClipOrCullDistanceAtDefinition(
  145. const Decoration& decoration, const Instruction& inst);
  146. spv_result_t ValidateFragCoordAtDefinition(const Decoration& decoration,
  147. const Instruction& inst);
  148. spv_result_t ValidateFragDepthAtDefinition(const Decoration& decoration,
  149. const Instruction& inst);
  150. spv_result_t ValidateFrontFacingAtDefinition(const Decoration& decoration,
  151. const Instruction& inst);
  152. spv_result_t ValidateHelperInvocationAtDefinition(
  153. const Decoration& decoration, const Instruction& inst);
  154. spv_result_t ValidateInvocationIdAtDefinition(const Decoration& decoration,
  155. const Instruction& inst);
  156. spv_result_t ValidateInstanceIndexAtDefinition(const Decoration& decoration,
  157. const Instruction& inst);
  158. spv_result_t ValidateLayerOrViewportIndexAtDefinition(
  159. const Decoration& decoration, const Instruction& inst);
  160. spv_result_t ValidatePatchVerticesAtDefinition(const Decoration& decoration,
  161. const Instruction& inst);
  162. spv_result_t ValidatePointCoordAtDefinition(const Decoration& decoration,
  163. const Instruction& inst);
  164. spv_result_t ValidatePointSizeAtDefinition(const Decoration& decoration,
  165. const Instruction& inst);
  166. spv_result_t ValidatePositionAtDefinition(const Decoration& decoration,
  167. const Instruction& inst);
  168. spv_result_t ValidatePrimitiveIdAtDefinition(const Decoration& decoration,
  169. const Instruction& inst);
  170. spv_result_t ValidateSampleIdAtDefinition(const Decoration& decoration,
  171. const Instruction& inst);
  172. spv_result_t ValidateSampleMaskAtDefinition(const Decoration& decoration,
  173. const Instruction& inst);
  174. spv_result_t ValidateSamplePositionAtDefinition(const Decoration& decoration,
  175. const Instruction& inst);
  176. spv_result_t ValidateTessCoordAtDefinition(const Decoration& decoration,
  177. const Instruction& inst);
  178. spv_result_t ValidateTessLevelOuterAtDefinition(const Decoration& decoration,
  179. const Instruction& inst);
  180. spv_result_t ValidateTessLevelInnerAtDefinition(const Decoration& decoration,
  181. const Instruction& inst);
  182. spv_result_t ValidateVertexIndexAtDefinition(const Decoration& decoration,
  183. const Instruction& inst);
  184. spv_result_t ValidateVertexIdOrInstanceIdAtDefinition(
  185. const Decoration& decoration, const Instruction& inst);
  186. spv_result_t ValidateLocalInvocationIndexAtDefinition(
  187. const Decoration& decoration, const Instruction& inst);
  188. spv_result_t ValidateWorkgroupSizeAtDefinition(const Decoration& decoration,
  189. const Instruction& inst);
  190. // Used for GlobalInvocationId, LocalInvocationId, NumWorkgroups, WorkgroupId.
  191. spv_result_t ValidateComputeShaderI32Vec3InputAtDefinition(
  192. const Decoration& decoration, const Instruction& inst);
  193. spv_result_t ValidateSMBuiltinsAtDefinition(const Decoration& decoration,
  194. const Instruction& inst);
  195. // Used for SubgroupEqMask, SubgroupGeMask, SubgroupGtMask, SubgroupLtMask,
  196. // SubgroupLeMask.
  197. spv_result_t ValidateI32Vec4InputAtDefinition(const Decoration& decoration,
  198. const Instruction& inst);
  199. // Used for SubgroupLocalInvocationId, SubgroupSize.
  200. spv_result_t ValidateI32InputAtDefinition(const Decoration& decoration,
  201. const Instruction& inst);
  202. // Used for SubgroupId, NumSubgroups.
  203. spv_result_t ValidateComputeI32InputAtDefinition(const Decoration& decoration,
  204. const Instruction& inst);
  205. // The following section contains functions which are called when id defined
  206. // by |referenced_inst| is
  207. // 1. referenced by |referenced_from_inst|
  208. // 2. dependent on |built_in_inst| which is decorated with BuiltIn
  209. // |decoration|. Most functions are specific to a single built-in and have
  210. // naming scheme: ValidateXYZAtReference. Some functions are common to
  211. // multiple kinds of BuiltIn.
  212. spv_result_t ValidateFragCoordAtReference(
  213. const Decoration& decoration, const Instruction& built_in_inst,
  214. const Instruction& referenced_inst,
  215. const Instruction& referenced_from_inst);
  216. spv_result_t ValidateFragDepthAtReference(
  217. const Decoration& decoration, const Instruction& built_in_inst,
  218. const Instruction& referenced_inst,
  219. const Instruction& referenced_from_inst);
  220. spv_result_t ValidateFrontFacingAtReference(
  221. const Decoration& decoration, const Instruction& built_in_inst,
  222. const Instruction& referenced_inst,
  223. const Instruction& referenced_from_inst);
  224. spv_result_t ValidateHelperInvocationAtReference(
  225. const Decoration& decoration, const Instruction& built_in_inst,
  226. const Instruction& referenced_inst,
  227. const Instruction& referenced_from_inst);
  228. spv_result_t ValidateInvocationIdAtReference(
  229. const Decoration& decoration, const Instruction& built_in_inst,
  230. const Instruction& referenced_inst,
  231. const Instruction& referenced_from_inst);
  232. spv_result_t ValidateInstanceIdAtReference(
  233. const Decoration& decoration, const Instruction& built_in_inst,
  234. const Instruction& referenced_inst,
  235. const Instruction& referenced_from_inst);
  236. spv_result_t ValidateInstanceIndexAtReference(
  237. const Decoration& decoration, const Instruction& built_in_inst,
  238. const Instruction& referenced_inst,
  239. const Instruction& referenced_from_inst);
  240. spv_result_t ValidatePatchVerticesAtReference(
  241. const Decoration& decoration, const Instruction& built_in_inst,
  242. const Instruction& referenced_inst,
  243. const Instruction& referenced_from_inst);
  244. spv_result_t ValidatePointCoordAtReference(
  245. const Decoration& decoration, const Instruction& built_in_inst,
  246. const Instruction& referenced_inst,
  247. const Instruction& referenced_from_inst);
  248. spv_result_t ValidatePointSizeAtReference(
  249. const Decoration& decoration, const Instruction& built_in_inst,
  250. const Instruction& referenced_inst,
  251. const Instruction& referenced_from_inst);
  252. spv_result_t ValidatePositionAtReference(
  253. const Decoration& decoration, const Instruction& built_in_inst,
  254. const Instruction& referenced_inst,
  255. const Instruction& referenced_from_inst);
  256. spv_result_t ValidatePrimitiveIdAtReference(
  257. const Decoration& decoration, const Instruction& built_in_inst,
  258. const Instruction& referenced_inst,
  259. const Instruction& referenced_from_inst);
  260. spv_result_t ValidateSampleIdAtReference(
  261. const Decoration& decoration, const Instruction& built_in_inst,
  262. const Instruction& referenced_inst,
  263. const Instruction& referenced_from_inst);
  264. spv_result_t ValidateSampleMaskAtReference(
  265. const Decoration& decoration, const Instruction& built_in_inst,
  266. const Instruction& referenced_inst,
  267. const Instruction& referenced_from_inst);
  268. spv_result_t ValidateSamplePositionAtReference(
  269. const Decoration& decoration, const Instruction& built_in_inst,
  270. const Instruction& referenced_inst,
  271. const Instruction& referenced_from_inst);
  272. spv_result_t ValidateTessCoordAtReference(
  273. const Decoration& decoration, const Instruction& built_in_inst,
  274. const Instruction& referenced_inst,
  275. const Instruction& referenced_from_inst);
  276. spv_result_t ValidateTessLevelAtReference(
  277. const Decoration& decoration, const Instruction& built_in_inst,
  278. const Instruction& referenced_inst,
  279. const Instruction& referenced_from_inst);
  280. spv_result_t ValidateLocalInvocationIndexAtReference(
  281. const Decoration& decoration, const Instruction& built_in_inst,
  282. const Instruction& referenced_inst,
  283. const Instruction& referenced_from_inst);
  284. spv_result_t ValidateVertexIndexAtReference(
  285. const Decoration& decoration, const Instruction& built_in_inst,
  286. const Instruction& referenced_inst,
  287. const Instruction& referenced_from_inst);
  288. spv_result_t ValidateLayerOrViewportIndexAtReference(
  289. const Decoration& decoration, const Instruction& built_in_inst,
  290. const Instruction& referenced_inst,
  291. const Instruction& referenced_from_inst);
  292. spv_result_t ValidateWorkgroupSizeAtReference(
  293. const Decoration& decoration, const Instruction& built_in_inst,
  294. const Instruction& referenced_inst,
  295. const Instruction& referenced_from_inst);
  296. spv_result_t ValidateClipOrCullDistanceAtReference(
  297. const Decoration& decoration, const Instruction& built_in_inst,
  298. const Instruction& referenced_inst,
  299. const Instruction& referenced_from_inst);
  300. // Used for GlobalInvocationId, LocalInvocationId, NumWorkgroups, WorkgroupId.
  301. spv_result_t ValidateComputeShaderI32Vec3InputAtReference(
  302. const Decoration& decoration, const Instruction& built_in_inst,
  303. const Instruction& referenced_inst,
  304. const Instruction& referenced_from_inst);
  305. // Used for SubgroupId and NumSubgroups.
  306. spv_result_t ValidateComputeI32InputAtReference(
  307. const Decoration& decoration, const Instruction& built_in_inst,
  308. const Instruction& referenced_inst,
  309. const Instruction& referenced_from_inst);
  310. spv_result_t ValidateSMBuiltinsAtReference(
  311. const Decoration& decoration, const Instruction& built_in_inst,
  312. const Instruction& referenced_inst,
  313. const Instruction& referenced_from_inst);
  314. // Validates that |built_in_inst| is not (even indirectly) referenced from
  315. // within a function which can be called with |execution_model|.
  316. //
  317. // |comment| - text explaining why the restriction was imposed.
  318. // |decoration| - BuiltIn decoration which causes the restriction.
  319. // |referenced_inst| - instruction which is dependent on |built_in_inst| and
  320. // defines the id which was referenced.
  321. // |referenced_from_inst| - instruction which references id defined by
  322. // |referenced_inst| from within a function.
  323. spv_result_t ValidateNotCalledWithExecutionModel(
  324. const char* comment, SpvExecutionModel execution_model,
  325. const Decoration& decoration, const Instruction& built_in_inst,
  326. const Instruction& referenced_inst,
  327. const Instruction& referenced_from_inst);
  328. // The following section contains functions which check that the decorated
  329. // variable has the type specified in the function name. |diag| would be
  330. // called with a corresponding error message, if validation is not successful.
  331. spv_result_t ValidateBool(
  332. const Decoration& decoration, const Instruction& inst,
  333. const std::function<spv_result_t(const std::string& message)>& diag);
  334. spv_result_t ValidateI32(
  335. const Decoration& decoration, const Instruction& inst,
  336. const std::function<spv_result_t(const std::string& message)>& diag);
  337. spv_result_t ValidateI32Vec(
  338. const Decoration& decoration, const Instruction& inst,
  339. uint32_t num_components,
  340. const std::function<spv_result_t(const std::string& message)>& diag);
  341. spv_result_t ValidateI32Arr(
  342. const Decoration& decoration, const Instruction& inst,
  343. const std::function<spv_result_t(const std::string& message)>& diag);
  344. spv_result_t ValidateOptionalArrayedI32(
  345. const Decoration& decoration, const Instruction& inst,
  346. const std::function<spv_result_t(const std::string& message)>& diag);
  347. spv_result_t ValidateI32Helper(
  348. const Decoration& decoration, const Instruction& inst,
  349. const std::function<spv_result_t(const std::string& message)>& diag,
  350. uint32_t underlying_type);
  351. spv_result_t ValidateF32(
  352. const Decoration& decoration, const Instruction& inst,
  353. const std::function<spv_result_t(const std::string& message)>& diag);
  354. spv_result_t ValidateOptionalArrayedF32(
  355. const Decoration& decoration, const Instruction& inst,
  356. const std::function<spv_result_t(const std::string& message)>& diag);
  357. spv_result_t ValidateF32Helper(
  358. const Decoration& decoration, const Instruction& inst,
  359. const std::function<spv_result_t(const std::string& message)>& diag,
  360. uint32_t underlying_type);
  361. spv_result_t ValidateF32Vec(
  362. const Decoration& decoration, const Instruction& inst,
  363. uint32_t num_components,
  364. const std::function<spv_result_t(const std::string& message)>& diag);
  365. spv_result_t ValidateOptionalArrayedF32Vec(
  366. const Decoration& decoration, const Instruction& inst,
  367. uint32_t num_components,
  368. const std::function<spv_result_t(const std::string& message)>& diag);
  369. spv_result_t ValidateF32VecHelper(
  370. const Decoration& decoration, const Instruction& inst,
  371. uint32_t num_components,
  372. const std::function<spv_result_t(const std::string& message)>& diag,
  373. uint32_t underlying_type);
  374. // If |num_components| is zero, the number of components is not checked.
  375. spv_result_t ValidateF32Arr(
  376. const Decoration& decoration, const Instruction& inst,
  377. uint32_t num_components,
  378. const std::function<spv_result_t(const std::string& message)>& diag);
  379. spv_result_t ValidateOptionalArrayedF32Arr(
  380. const Decoration& decoration, const Instruction& inst,
  381. uint32_t num_components,
  382. const std::function<spv_result_t(const std::string& message)>& diag);
  383. spv_result_t ValidateF32ArrHelper(
  384. const Decoration& decoration, const Instruction& inst,
  385. uint32_t num_components,
  386. const std::function<spv_result_t(const std::string& message)>& diag,
  387. uint32_t underlying_type);
  388. // Generates strings like "Member #0 of struct ID <2>".
  389. std::string GetDefinitionDesc(const Decoration& decoration,
  390. const Instruction& inst) const;
  391. // Generates strings like "ID <51> (OpTypePointer) is referencing ID <2>
  392. // (OpTypeStruct) which is decorated with BuiltIn Position".
  393. std::string GetReferenceDesc(
  394. const Decoration& decoration, const Instruction& built_in_inst,
  395. const Instruction& referenced_inst,
  396. const Instruction& referenced_from_inst,
  397. SpvExecutionModel execution_model = SpvExecutionModelMax) const;
  398. // Generates strings like "ID <51> (OpTypePointer) uses storage class
  399. // UniformConstant".
  400. std::string GetStorageClassDesc(const Instruction& inst) const;
  401. // Updates inner working of the class. Is called sequentially for every
  402. // instruction.
  403. void Update(const Instruction& inst);
  404. ValidationState_t& _;
  405. // Mapping id -> list of rules which validate instruction referencing the
  406. // id. Rules can create new rules and add them to this container.
  407. // Using std::map, and not std::unordered_map to avoid iterator invalidation
  408. // during rehashing.
  409. std::map<uint32_t, std::list<std::function<spv_result_t(const Instruction&)>>>
  410. id_to_at_reference_checks_;
  411. // Id of the function we are currently inside. 0 if not inside a function.
  412. uint32_t function_id_ = 0;
  413. // Entry points which can (indirectly) call the current function.
  414. // The pointer either points to a vector inside to function_to_entry_points_
  415. // or to no_entry_points_. The pointer is guaranteed to never be null.
  416. const std::vector<uint32_t> no_entry_points;
  417. const std::vector<uint32_t>* entry_points_ = &no_entry_points;
  418. // Execution models with which the current function can be called.
  419. std::set<SpvExecutionModel> execution_models_;
  420. };
  421. void BuiltInsValidator::Update(const Instruction& inst) {
  422. const SpvOp opcode = inst.opcode();
  423. if (opcode == SpvOpFunction) {
  424. // Entering a function.
  425. assert(function_id_ == 0);
  426. function_id_ = inst.id();
  427. execution_models_.clear();
  428. entry_points_ = &_.FunctionEntryPoints(function_id_);
  429. // Collect execution models from all entry points from which the current
  430. // function can be called.
  431. for (const uint32_t entry_point : *entry_points_) {
  432. if (const auto* models = _.GetExecutionModels(entry_point)) {
  433. execution_models_.insert(models->begin(), models->end());
  434. }
  435. }
  436. }
  437. if (opcode == SpvOpFunctionEnd) {
  438. // Exiting a function.
  439. assert(function_id_ != 0);
  440. function_id_ = 0;
  441. entry_points_ = &no_entry_points;
  442. execution_models_.clear();
  443. }
  444. }
  445. std::string BuiltInsValidator::GetDefinitionDesc(
  446. const Decoration& decoration, const Instruction& inst) const {
  447. std::ostringstream ss;
  448. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  449. assert(inst.opcode() == SpvOpTypeStruct);
  450. ss << "Member #" << decoration.struct_member_index();
  451. ss << " of struct ID <" << inst.id() << ">";
  452. } else {
  453. ss << GetIdDesc(inst);
  454. }
  455. return ss.str();
  456. }
  457. std::string BuiltInsValidator::GetReferenceDesc(
  458. const Decoration& decoration, const Instruction& built_in_inst,
  459. const Instruction& referenced_inst, const Instruction& referenced_from_inst,
  460. SpvExecutionModel execution_model) const {
  461. std::ostringstream ss;
  462. ss << GetIdDesc(referenced_from_inst) << " is referencing "
  463. << GetIdDesc(referenced_inst);
  464. if (built_in_inst.id() != referenced_inst.id()) {
  465. ss << " which is dependent on " << GetIdDesc(built_in_inst);
  466. }
  467. ss << " which is decorated with BuiltIn ";
  468. ss << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  469. decoration.params()[0]);
  470. if (function_id_) {
  471. ss << " in function <" << function_id_ << ">";
  472. if (execution_model != SpvExecutionModelMax) {
  473. ss << " called with execution model ";
  474. ss << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_EXECUTION_MODEL,
  475. execution_model);
  476. }
  477. }
  478. ss << ".";
  479. return ss.str();
  480. }
  481. std::string BuiltInsValidator::GetStorageClassDesc(
  482. const Instruction& inst) const {
  483. std::ostringstream ss;
  484. ss << GetIdDesc(inst) << " uses storage class ";
  485. ss << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_STORAGE_CLASS,
  486. GetStorageClass(inst));
  487. ss << ".";
  488. return ss.str();
  489. }
  490. spv_result_t BuiltInsValidator::ValidateBool(
  491. const Decoration& decoration, const Instruction& inst,
  492. const std::function<spv_result_t(const std::string& message)>& diag) {
  493. uint32_t underlying_type = 0;
  494. if (spv_result_t error =
  495. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  496. return error;
  497. }
  498. if (!_.IsBoolScalarType(underlying_type)) {
  499. return diag(GetDefinitionDesc(decoration, inst) + " is not a bool scalar.");
  500. }
  501. return SPV_SUCCESS;
  502. }
  503. spv_result_t BuiltInsValidator::ValidateI32(
  504. const Decoration& decoration, const Instruction& inst,
  505. const std::function<spv_result_t(const std::string& message)>& diag) {
  506. uint32_t underlying_type = 0;
  507. if (spv_result_t error =
  508. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  509. return error;
  510. }
  511. return ValidateI32Helper(decoration, inst, diag, underlying_type);
  512. }
  513. spv_result_t BuiltInsValidator::ValidateOptionalArrayedI32(
  514. const Decoration& decoration, const Instruction& inst,
  515. const std::function<spv_result_t(const std::string& message)>& diag) {
  516. uint32_t underlying_type = 0;
  517. if (spv_result_t error =
  518. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  519. return error;
  520. }
  521. // Strip the array, if present.
  522. if (_.GetIdOpcode(underlying_type) == SpvOpTypeArray) {
  523. underlying_type = _.FindDef(underlying_type)->word(2u);
  524. }
  525. return ValidateI32Helper(decoration, inst, diag, underlying_type);
  526. }
  527. spv_result_t BuiltInsValidator::ValidateI32Helper(
  528. const Decoration& decoration, const Instruction& inst,
  529. const std::function<spv_result_t(const std::string& message)>& diag,
  530. uint32_t underlying_type) {
  531. if (!_.IsIntScalarType(underlying_type)) {
  532. return diag(GetDefinitionDesc(decoration, inst) + " is not an int scalar.");
  533. }
  534. const uint32_t bit_width = _.GetBitWidth(underlying_type);
  535. if (bit_width != 32) {
  536. std::ostringstream ss;
  537. ss << GetDefinitionDesc(decoration, inst) << " has bit width " << bit_width
  538. << ".";
  539. return diag(ss.str());
  540. }
  541. return SPV_SUCCESS;
  542. }
  543. spv_result_t BuiltInsValidator::ValidateOptionalArrayedF32(
  544. const Decoration& decoration, const Instruction& inst,
  545. const std::function<spv_result_t(const std::string& message)>& diag) {
  546. uint32_t underlying_type = 0;
  547. if (spv_result_t error =
  548. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  549. return error;
  550. }
  551. // Strip the array, if present.
  552. if (_.GetIdOpcode(underlying_type) == SpvOpTypeArray) {
  553. underlying_type = _.FindDef(underlying_type)->word(2u);
  554. }
  555. return ValidateF32Helper(decoration, inst, diag, underlying_type);
  556. }
  557. spv_result_t BuiltInsValidator::ValidateF32(
  558. const Decoration& decoration, const Instruction& inst,
  559. const std::function<spv_result_t(const std::string& message)>& diag) {
  560. uint32_t underlying_type = 0;
  561. if (spv_result_t error =
  562. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  563. return error;
  564. }
  565. return ValidateF32Helper(decoration, inst, diag, underlying_type);
  566. }
  567. spv_result_t BuiltInsValidator::ValidateF32Helper(
  568. const Decoration& decoration, const Instruction& inst,
  569. const std::function<spv_result_t(const std::string& message)>& diag,
  570. uint32_t underlying_type) {
  571. if (!_.IsFloatScalarType(underlying_type)) {
  572. return diag(GetDefinitionDesc(decoration, inst) +
  573. " is not a float scalar.");
  574. }
  575. const uint32_t bit_width = _.GetBitWidth(underlying_type);
  576. if (bit_width != 32) {
  577. std::ostringstream ss;
  578. ss << GetDefinitionDesc(decoration, inst) << " has bit width " << bit_width
  579. << ".";
  580. return diag(ss.str());
  581. }
  582. return SPV_SUCCESS;
  583. }
  584. spv_result_t BuiltInsValidator::ValidateI32Vec(
  585. const Decoration& decoration, const Instruction& inst,
  586. uint32_t num_components,
  587. const std::function<spv_result_t(const std::string& message)>& diag) {
  588. uint32_t underlying_type = 0;
  589. if (spv_result_t error =
  590. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  591. return error;
  592. }
  593. if (!_.IsIntVectorType(underlying_type)) {
  594. return diag(GetDefinitionDesc(decoration, inst) + " is not an int vector.");
  595. }
  596. const uint32_t actual_num_components = _.GetDimension(underlying_type);
  597. if (_.GetDimension(underlying_type) != num_components) {
  598. std::ostringstream ss;
  599. ss << GetDefinitionDesc(decoration, inst) << " has "
  600. << actual_num_components << " components.";
  601. return diag(ss.str());
  602. }
  603. const uint32_t bit_width = _.GetBitWidth(underlying_type);
  604. if (bit_width != 32) {
  605. std::ostringstream ss;
  606. ss << GetDefinitionDesc(decoration, inst)
  607. << " has components with bit width " << bit_width << ".";
  608. return diag(ss.str());
  609. }
  610. return SPV_SUCCESS;
  611. }
  612. spv_result_t BuiltInsValidator::ValidateOptionalArrayedF32Vec(
  613. const Decoration& decoration, const Instruction& inst,
  614. uint32_t num_components,
  615. const std::function<spv_result_t(const std::string& message)>& diag) {
  616. uint32_t underlying_type = 0;
  617. if (spv_result_t error =
  618. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  619. return error;
  620. }
  621. // Strip the array, if present.
  622. if (_.GetIdOpcode(underlying_type) == SpvOpTypeArray) {
  623. underlying_type = _.FindDef(underlying_type)->word(2u);
  624. }
  625. return ValidateF32VecHelper(decoration, inst, num_components, diag,
  626. underlying_type);
  627. }
  628. spv_result_t BuiltInsValidator::ValidateF32Vec(
  629. const Decoration& decoration, const Instruction& inst,
  630. uint32_t num_components,
  631. const std::function<spv_result_t(const std::string& message)>& diag) {
  632. uint32_t underlying_type = 0;
  633. if (spv_result_t error =
  634. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  635. return error;
  636. }
  637. return ValidateF32VecHelper(decoration, inst, num_components, diag,
  638. underlying_type);
  639. }
  640. spv_result_t BuiltInsValidator::ValidateF32VecHelper(
  641. const Decoration& decoration, const Instruction& inst,
  642. uint32_t num_components,
  643. const std::function<spv_result_t(const std::string& message)>& diag,
  644. uint32_t underlying_type) {
  645. if (!_.IsFloatVectorType(underlying_type)) {
  646. return diag(GetDefinitionDesc(decoration, inst) +
  647. " is not a float vector.");
  648. }
  649. const uint32_t actual_num_components = _.GetDimension(underlying_type);
  650. if (_.GetDimension(underlying_type) != num_components) {
  651. std::ostringstream ss;
  652. ss << GetDefinitionDesc(decoration, inst) << " has "
  653. << actual_num_components << " components.";
  654. return diag(ss.str());
  655. }
  656. const uint32_t bit_width = _.GetBitWidth(underlying_type);
  657. if (bit_width != 32) {
  658. std::ostringstream ss;
  659. ss << GetDefinitionDesc(decoration, inst)
  660. << " has components with bit width " << bit_width << ".";
  661. return diag(ss.str());
  662. }
  663. return SPV_SUCCESS;
  664. }
  665. spv_result_t BuiltInsValidator::ValidateI32Arr(
  666. const Decoration& decoration, const Instruction& inst,
  667. const std::function<spv_result_t(const std::string& message)>& diag) {
  668. uint32_t underlying_type = 0;
  669. if (spv_result_t error =
  670. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  671. return error;
  672. }
  673. const Instruction* const type_inst = _.FindDef(underlying_type);
  674. if (type_inst->opcode() != SpvOpTypeArray) {
  675. return diag(GetDefinitionDesc(decoration, inst) + " is not an array.");
  676. }
  677. const uint32_t component_type = type_inst->word(2);
  678. if (!_.IsIntScalarType(component_type)) {
  679. return diag(GetDefinitionDesc(decoration, inst) +
  680. " components are not int scalar.");
  681. }
  682. const uint32_t bit_width = _.GetBitWidth(component_type);
  683. if (bit_width != 32) {
  684. std::ostringstream ss;
  685. ss << GetDefinitionDesc(decoration, inst)
  686. << " has components with bit width " << bit_width << ".";
  687. return diag(ss.str());
  688. }
  689. return SPV_SUCCESS;
  690. }
  691. spv_result_t BuiltInsValidator::ValidateF32Arr(
  692. const Decoration& decoration, const Instruction& inst,
  693. uint32_t num_components,
  694. const std::function<spv_result_t(const std::string& message)>& diag) {
  695. uint32_t underlying_type = 0;
  696. if (spv_result_t error =
  697. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  698. return error;
  699. }
  700. return ValidateF32ArrHelper(decoration, inst, num_components, diag,
  701. underlying_type);
  702. }
  703. spv_result_t BuiltInsValidator::ValidateOptionalArrayedF32Arr(
  704. const Decoration& decoration, const Instruction& inst,
  705. uint32_t num_components,
  706. const std::function<spv_result_t(const std::string& message)>& diag) {
  707. uint32_t underlying_type = 0;
  708. if (spv_result_t error =
  709. GetUnderlyingType(_, decoration, inst, &underlying_type)) {
  710. return error;
  711. }
  712. // Strip an extra layer of arraying if present.
  713. if (_.GetIdOpcode(underlying_type) == SpvOpTypeArray) {
  714. uint32_t subtype = _.FindDef(underlying_type)->word(2u);
  715. if (_.GetIdOpcode(subtype) == SpvOpTypeArray) {
  716. underlying_type = subtype;
  717. }
  718. }
  719. return ValidateF32ArrHelper(decoration, inst, num_components, diag,
  720. underlying_type);
  721. }
  722. spv_result_t BuiltInsValidator::ValidateF32ArrHelper(
  723. const Decoration& decoration, const Instruction& inst,
  724. uint32_t num_components,
  725. const std::function<spv_result_t(const std::string& message)>& diag,
  726. uint32_t underlying_type) {
  727. const Instruction* const type_inst = _.FindDef(underlying_type);
  728. if (type_inst->opcode() != SpvOpTypeArray) {
  729. return diag(GetDefinitionDesc(decoration, inst) + " is not an array.");
  730. }
  731. const uint32_t component_type = type_inst->word(2);
  732. if (!_.IsFloatScalarType(component_type)) {
  733. return diag(GetDefinitionDesc(decoration, inst) +
  734. " components are not float scalar.");
  735. }
  736. const uint32_t bit_width = _.GetBitWidth(component_type);
  737. if (bit_width != 32) {
  738. std::ostringstream ss;
  739. ss << GetDefinitionDesc(decoration, inst)
  740. << " has components with bit width " << bit_width << ".";
  741. return diag(ss.str());
  742. }
  743. if (num_components != 0) {
  744. uint64_t actual_num_components = 0;
  745. if (!_.GetConstantValUint64(type_inst->word(3), &actual_num_components)) {
  746. assert(0 && "Array type definition is corrupt");
  747. }
  748. if (actual_num_components != num_components) {
  749. std::ostringstream ss;
  750. ss << GetDefinitionDesc(decoration, inst) << " has "
  751. << actual_num_components << " components.";
  752. return diag(ss.str());
  753. }
  754. }
  755. return SPV_SUCCESS;
  756. }
  757. spv_result_t BuiltInsValidator::ValidateNotCalledWithExecutionModel(
  758. const char* comment, SpvExecutionModel execution_model,
  759. const Decoration& decoration, const Instruction& built_in_inst,
  760. const Instruction& referenced_inst,
  761. const Instruction& referenced_from_inst) {
  762. if (function_id_) {
  763. if (execution_models_.count(execution_model)) {
  764. const char* execution_model_str = _.grammar().lookupOperandName(
  765. SPV_OPERAND_TYPE_EXECUTION_MODEL, execution_model);
  766. const char* built_in_str = _.grammar().lookupOperandName(
  767. SPV_OPERAND_TYPE_BUILT_IN, decoration.params()[0]);
  768. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  769. << comment << " " << GetIdDesc(referenced_inst) << " depends on "
  770. << GetIdDesc(built_in_inst) << " which is decorated with BuiltIn "
  771. << built_in_str << "."
  772. << " Id <" << referenced_inst.id() << "> is later referenced by "
  773. << GetIdDesc(referenced_from_inst) << " in function <"
  774. << function_id_ << "> which is called with execution model "
  775. << execution_model_str << ".";
  776. }
  777. } else {
  778. // Propagate this rule to all dependant ids in the global scope.
  779. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  780. std::bind(&BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
  781. comment, execution_model, decoration, built_in_inst,
  782. referenced_from_inst, std::placeholders::_1));
  783. }
  784. return SPV_SUCCESS;
  785. }
  786. spv_result_t BuiltInsValidator::ValidateClipOrCullDistanceAtDefinition(
  787. const Decoration& decoration, const Instruction& inst) {
  788. // Seed at reference checks with this built-in.
  789. return ValidateClipOrCullDistanceAtReference(decoration, inst, inst, inst);
  790. }
  791. spv_result_t BuiltInsValidator::ValidateClipOrCullDistanceAtReference(
  792. const Decoration& decoration, const Instruction& built_in_inst,
  793. const Instruction& referenced_inst,
  794. const Instruction& referenced_from_inst) {
  795. if (spvIsVulkanEnv(_.context()->target_env)) {
  796. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  797. if (storage_class != SpvStorageClassMax &&
  798. storage_class != SpvStorageClassInput &&
  799. storage_class != SpvStorageClassOutput) {
  800. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  801. << "Vulkan spec allows BuiltIn "
  802. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  803. decoration.params()[0])
  804. << " to be only used for variables with Input or Output storage "
  805. "class. "
  806. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  807. referenced_from_inst)
  808. << " " << GetStorageClassDesc(referenced_from_inst);
  809. }
  810. if (storage_class == SpvStorageClassInput) {
  811. assert(function_id_ == 0);
  812. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  813. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
  814. "Vulkan spec doesn't allow BuiltIn ClipDistance/CullDistance to be "
  815. "used for variables with Input storage class if execution model is "
  816. "Vertex.",
  817. SpvExecutionModelVertex, decoration, built_in_inst,
  818. referenced_from_inst, std::placeholders::_1));
  819. }
  820. if (storage_class == SpvStorageClassOutput) {
  821. assert(function_id_ == 0);
  822. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  823. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
  824. "Vulkan spec doesn't allow BuiltIn ClipDistance/CullDistance to be "
  825. "used for variables with Output storage class if execution model is "
  826. "Fragment.",
  827. SpvExecutionModelFragment, decoration, built_in_inst,
  828. referenced_from_inst, std::placeholders::_1));
  829. }
  830. for (const SpvExecutionModel execution_model : execution_models_) {
  831. switch (execution_model) {
  832. case SpvExecutionModelFragment:
  833. case SpvExecutionModelVertex: {
  834. if (spv_result_t error = ValidateF32Arr(
  835. decoration, built_in_inst, /* Any number of components */ 0,
  836. [this, &decoration, &referenced_from_inst](
  837. const std::string& message) -> spv_result_t {
  838. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  839. << "According to the Vulkan spec BuiltIn "
  840. << _.grammar().lookupOperandName(
  841. SPV_OPERAND_TYPE_BUILT_IN,
  842. decoration.params()[0])
  843. << " variable needs to be a 32-bit float array. "
  844. << message;
  845. })) {
  846. return error;
  847. }
  848. break;
  849. }
  850. case SpvExecutionModelTessellationControl:
  851. case SpvExecutionModelTessellationEvaluation:
  852. case SpvExecutionModelGeometry:
  853. case SpvExecutionModelMeshNV: {
  854. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  855. // The outer level of array is applied on the variable.
  856. if (spv_result_t error = ValidateF32Arr(
  857. decoration, built_in_inst, /* Any number of components */ 0,
  858. [this, &decoration, &referenced_from_inst](
  859. const std::string& message) -> spv_result_t {
  860. return _.diag(SPV_ERROR_INVALID_DATA,
  861. &referenced_from_inst)
  862. << "According to the Vulkan spec BuiltIn "
  863. << _.grammar().lookupOperandName(
  864. SPV_OPERAND_TYPE_BUILT_IN,
  865. decoration.params()[0])
  866. << " variable needs to be a 32-bit float array. "
  867. << message;
  868. })) {
  869. return error;
  870. }
  871. } else {
  872. if (spv_result_t error = ValidateOptionalArrayedF32Arr(
  873. decoration, built_in_inst, /* Any number of components */ 0,
  874. [this, &decoration, &referenced_from_inst](
  875. const std::string& message) -> spv_result_t {
  876. return _.diag(SPV_ERROR_INVALID_DATA,
  877. &referenced_from_inst)
  878. << "According to the Vulkan spec BuiltIn "
  879. << _.grammar().lookupOperandName(
  880. SPV_OPERAND_TYPE_BUILT_IN,
  881. decoration.params()[0])
  882. << " variable needs to be a 32-bit float array. "
  883. << message;
  884. })) {
  885. return error;
  886. }
  887. }
  888. break;
  889. }
  890. default: {
  891. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  892. << "Vulkan spec allows BuiltIn "
  893. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  894. decoration.params()[0])
  895. << " to be used only with Fragment, Vertex, "
  896. "TessellationControl, TessellationEvaluation or Geometry "
  897. "execution models. "
  898. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  899. referenced_from_inst, execution_model);
  900. }
  901. }
  902. }
  903. }
  904. if (function_id_ == 0) {
  905. // Propagate this rule to all dependant ids in the global scope.
  906. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  907. std::bind(&BuiltInsValidator::ValidateClipOrCullDistanceAtReference,
  908. this, decoration, built_in_inst, referenced_from_inst,
  909. std::placeholders::_1));
  910. }
  911. return SPV_SUCCESS;
  912. }
  913. spv_result_t BuiltInsValidator::ValidateFragCoordAtDefinition(
  914. const Decoration& decoration, const Instruction& inst) {
  915. if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
  916. if (spv_result_t error = ValidateF32Vec(
  917. decoration, inst, 4,
  918. [this, &inst](const std::string& message) -> spv_result_t {
  919. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  920. << "According to the "
  921. << spvLogStringForEnv(_.context()->target_env)
  922. << " spec BuiltIn FragCoord "
  923. "variable needs to be a 4-component 32-bit float "
  924. "vector. "
  925. << message;
  926. })) {
  927. return error;
  928. }
  929. }
  930. // Seed at reference checks with this built-in.
  931. return ValidateFragCoordAtReference(decoration, inst, inst, inst);
  932. }
  933. spv_result_t BuiltInsValidator::ValidateFragCoordAtReference(
  934. const Decoration& decoration, const Instruction& built_in_inst,
  935. const Instruction& referenced_inst,
  936. const Instruction& referenced_from_inst) {
  937. if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
  938. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  939. if (storage_class != SpvStorageClassMax &&
  940. storage_class != SpvStorageClassInput) {
  941. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  942. << spvLogStringForEnv(_.context()->target_env)
  943. << " spec allows BuiltIn FragCoord to be only used for "
  944. "variables with Input storage class. "
  945. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  946. referenced_from_inst)
  947. << " " << GetStorageClassDesc(referenced_from_inst);
  948. }
  949. for (const SpvExecutionModel execution_model : execution_models_) {
  950. if (execution_model != SpvExecutionModelFragment) {
  951. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  952. << spvLogStringForEnv(_.context()->target_env)
  953. << " spec allows BuiltIn FragCoord to be used only with "
  954. "Fragment execution model. "
  955. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  956. referenced_from_inst, execution_model);
  957. }
  958. }
  959. }
  960. if (function_id_ == 0) {
  961. // Propagate this rule to all dependant ids in the global scope.
  962. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  963. &BuiltInsValidator::ValidateFragCoordAtReference, this, decoration,
  964. built_in_inst, referenced_from_inst, std::placeholders::_1));
  965. }
  966. return SPV_SUCCESS;
  967. }
  968. spv_result_t BuiltInsValidator::ValidateFragDepthAtDefinition(
  969. const Decoration& decoration, const Instruction& inst) {
  970. if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
  971. if (spv_result_t error = ValidateF32(
  972. decoration, inst,
  973. [this, &inst](const std::string& message) -> spv_result_t {
  974. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  975. << "According to the "
  976. << spvLogStringForEnv(_.context()->target_env)
  977. << " spec BuiltIn FragDepth "
  978. "variable needs to be a 32-bit float scalar. "
  979. << message;
  980. })) {
  981. return error;
  982. }
  983. }
  984. // Seed at reference checks with this built-in.
  985. return ValidateFragDepthAtReference(decoration, inst, inst, inst);
  986. }
  987. spv_result_t BuiltInsValidator::ValidateFragDepthAtReference(
  988. const Decoration& decoration, const Instruction& built_in_inst,
  989. const Instruction& referenced_inst,
  990. const Instruction& referenced_from_inst) {
  991. if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
  992. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  993. if (storage_class != SpvStorageClassMax &&
  994. storage_class != SpvStorageClassOutput) {
  995. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  996. << spvLogStringForEnv(_.context()->target_env)
  997. << " spec allows BuiltIn FragDepth to be only used for "
  998. "variables with Output storage class. "
  999. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1000. referenced_from_inst)
  1001. << " " << GetStorageClassDesc(referenced_from_inst);
  1002. }
  1003. for (const SpvExecutionModel execution_model : execution_models_) {
  1004. if (execution_model != SpvExecutionModelFragment) {
  1005. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1006. << spvLogStringForEnv(_.context()->target_env)
  1007. << " spec allows BuiltIn FragDepth to be used only with "
  1008. "Fragment execution model. "
  1009. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1010. referenced_from_inst, execution_model);
  1011. }
  1012. }
  1013. for (const uint32_t entry_point : *entry_points_) {
  1014. // Every entry point from which this function is called needs to have
  1015. // Execution Mode DepthReplacing.
  1016. const auto* modes = _.GetExecutionModes(entry_point);
  1017. if (!modes || !modes->count(SpvExecutionModeDepthReplacing)) {
  1018. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1019. << spvLogStringForEnv(_.context()->target_env)
  1020. << " spec requires DepthReplacing execution mode to be "
  1021. "declared when using BuiltIn FragDepth. "
  1022. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1023. referenced_from_inst);
  1024. }
  1025. }
  1026. }
  1027. if (function_id_ == 0) {
  1028. // Propagate this rule to all dependant ids in the global scope.
  1029. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1030. &BuiltInsValidator::ValidateFragDepthAtReference, this, decoration,
  1031. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1032. }
  1033. return SPV_SUCCESS;
  1034. }
  1035. spv_result_t BuiltInsValidator::ValidateFrontFacingAtDefinition(
  1036. const Decoration& decoration, const Instruction& inst) {
  1037. if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
  1038. if (spv_result_t error = ValidateBool(
  1039. decoration, inst,
  1040. [this, &inst](const std::string& message) -> spv_result_t {
  1041. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1042. << "According to the "
  1043. << spvLogStringForEnv(_.context()->target_env)
  1044. << " spec BuiltIn FrontFacing "
  1045. "variable needs to be a bool scalar. "
  1046. << message;
  1047. })) {
  1048. return error;
  1049. }
  1050. }
  1051. // Seed at reference checks with this built-in.
  1052. return ValidateFrontFacingAtReference(decoration, inst, inst, inst);
  1053. }
  1054. spv_result_t BuiltInsValidator::ValidateFrontFacingAtReference(
  1055. const Decoration& decoration, const Instruction& built_in_inst,
  1056. const Instruction& referenced_inst,
  1057. const Instruction& referenced_from_inst) {
  1058. if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
  1059. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1060. if (storage_class != SpvStorageClassMax &&
  1061. storage_class != SpvStorageClassInput) {
  1062. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1063. << spvLogStringForEnv(_.context()->target_env)
  1064. << " spec allows BuiltIn FrontFacing to be only used for "
  1065. "variables with Input storage class. "
  1066. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1067. referenced_from_inst)
  1068. << " " << GetStorageClassDesc(referenced_from_inst);
  1069. }
  1070. for (const SpvExecutionModel execution_model : execution_models_) {
  1071. if (execution_model != SpvExecutionModelFragment) {
  1072. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1073. << spvLogStringForEnv(_.context()->target_env)
  1074. << " spec allows BuiltIn FrontFacing to be used only with "
  1075. "Fragment execution model. "
  1076. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1077. referenced_from_inst, execution_model);
  1078. }
  1079. }
  1080. }
  1081. if (function_id_ == 0) {
  1082. // Propagate this rule to all dependant ids in the global scope.
  1083. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1084. &BuiltInsValidator::ValidateFrontFacingAtReference, this, decoration,
  1085. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1086. }
  1087. return SPV_SUCCESS;
  1088. }
  1089. spv_result_t BuiltInsValidator::ValidateHelperInvocationAtDefinition(
  1090. const Decoration& decoration, const Instruction& inst) {
  1091. if (spvIsVulkanEnv(_.context()->target_env)) {
  1092. if (spv_result_t error = ValidateBool(
  1093. decoration, inst,
  1094. [this, &inst](const std::string& message) -> spv_result_t {
  1095. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1096. << "According to the Vulkan spec BuiltIn HelperInvocation "
  1097. "variable needs to be a bool scalar. "
  1098. << message;
  1099. })) {
  1100. return error;
  1101. }
  1102. }
  1103. // Seed at reference checks with this built-in.
  1104. return ValidateHelperInvocationAtReference(decoration, inst, inst, inst);
  1105. }
  1106. spv_result_t BuiltInsValidator::ValidateHelperInvocationAtReference(
  1107. const Decoration& decoration, const Instruction& built_in_inst,
  1108. const Instruction& referenced_inst,
  1109. const Instruction& referenced_from_inst) {
  1110. if (spvIsVulkanEnv(_.context()->target_env)) {
  1111. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1112. if (storage_class != SpvStorageClassMax &&
  1113. storage_class != SpvStorageClassInput) {
  1114. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1115. << "Vulkan spec allows BuiltIn HelperInvocation to be only used "
  1116. "for variables with Input storage class. "
  1117. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1118. referenced_from_inst)
  1119. << " " << GetStorageClassDesc(referenced_from_inst);
  1120. }
  1121. for (const SpvExecutionModel execution_model : execution_models_) {
  1122. if (execution_model != SpvExecutionModelFragment) {
  1123. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1124. << "Vulkan spec allows BuiltIn HelperInvocation to be used only "
  1125. "with Fragment execution model. "
  1126. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1127. referenced_from_inst, execution_model);
  1128. }
  1129. }
  1130. }
  1131. if (function_id_ == 0) {
  1132. // Propagate this rule to all dependant ids in the global scope.
  1133. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  1134. std::bind(&BuiltInsValidator::ValidateHelperInvocationAtReference, this,
  1135. decoration, built_in_inst, referenced_from_inst,
  1136. std::placeholders::_1));
  1137. }
  1138. return SPV_SUCCESS;
  1139. }
  1140. spv_result_t BuiltInsValidator::ValidateInvocationIdAtDefinition(
  1141. const Decoration& decoration, const Instruction& inst) {
  1142. if (spvIsVulkanEnv(_.context()->target_env)) {
  1143. if (spv_result_t error = ValidateI32(
  1144. decoration, inst,
  1145. [this, &inst](const std::string& message) -> spv_result_t {
  1146. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1147. << "According to the Vulkan spec BuiltIn InvocationId "
  1148. "variable needs to be a 32-bit int scalar. "
  1149. << message;
  1150. })) {
  1151. return error;
  1152. }
  1153. }
  1154. // Seed at reference checks with this built-in.
  1155. return ValidateInvocationIdAtReference(decoration, inst, inst, inst);
  1156. }
  1157. spv_result_t BuiltInsValidator::ValidateInvocationIdAtReference(
  1158. const Decoration& decoration, const Instruction& built_in_inst,
  1159. const Instruction& referenced_inst,
  1160. const Instruction& referenced_from_inst) {
  1161. if (spvIsVulkanEnv(_.context()->target_env)) {
  1162. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1163. if (storage_class != SpvStorageClassMax &&
  1164. storage_class != SpvStorageClassInput) {
  1165. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1166. << "Vulkan spec allows BuiltIn InvocationId to be only used for "
  1167. "variables with Input storage class. "
  1168. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1169. referenced_from_inst)
  1170. << " " << GetStorageClassDesc(referenced_from_inst);
  1171. }
  1172. for (const SpvExecutionModel execution_model : execution_models_) {
  1173. if (execution_model != SpvExecutionModelTessellationControl &&
  1174. execution_model != SpvExecutionModelGeometry) {
  1175. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1176. << "Vulkan spec allows BuiltIn InvocationId to be used only "
  1177. "with TessellationControl or Geometry execution models. "
  1178. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1179. referenced_from_inst, execution_model);
  1180. }
  1181. }
  1182. }
  1183. if (function_id_ == 0) {
  1184. // Propagate this rule to all dependant ids in the global scope.
  1185. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1186. &BuiltInsValidator::ValidateInvocationIdAtReference, this, decoration,
  1187. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1188. }
  1189. return SPV_SUCCESS;
  1190. }
  1191. spv_result_t BuiltInsValidator::ValidateInstanceIndexAtDefinition(
  1192. const Decoration& decoration, const Instruction& inst) {
  1193. if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
  1194. if (spv_result_t error = ValidateI32(
  1195. decoration, inst,
  1196. [this, &inst](const std::string& message) -> spv_result_t {
  1197. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1198. << "According to the "
  1199. << spvLogStringForEnv(_.context()->target_env)
  1200. << " spec BuiltIn InstanceIndex "
  1201. "variable needs to be a 32-bit int scalar. "
  1202. << message;
  1203. })) {
  1204. return error;
  1205. }
  1206. }
  1207. // Seed at reference checks with this built-in.
  1208. return ValidateInstanceIndexAtReference(decoration, inst, inst, inst);
  1209. }
  1210. spv_result_t BuiltInsValidator::ValidateInstanceIndexAtReference(
  1211. const Decoration& decoration, const Instruction& built_in_inst,
  1212. const Instruction& referenced_inst,
  1213. const Instruction& referenced_from_inst) {
  1214. if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
  1215. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1216. if (storage_class != SpvStorageClassMax &&
  1217. storage_class != SpvStorageClassInput) {
  1218. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1219. << spvLogStringForEnv(_.context()->target_env)
  1220. << " spec allows BuiltIn InstanceIndex to be only used for "
  1221. "variables with Input storage class. "
  1222. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1223. referenced_from_inst)
  1224. << " " << GetStorageClassDesc(referenced_from_inst);
  1225. }
  1226. for (const SpvExecutionModel execution_model : execution_models_) {
  1227. if (execution_model != SpvExecutionModelVertex) {
  1228. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1229. << spvLogStringForEnv(_.context()->target_env)
  1230. << " spec allows BuiltIn InstanceIndex to be used only "
  1231. "with Vertex execution model. "
  1232. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1233. referenced_from_inst, execution_model);
  1234. }
  1235. }
  1236. }
  1237. if (function_id_ == 0) {
  1238. // Propagate this rule to all dependant ids in the global scope.
  1239. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1240. &BuiltInsValidator::ValidateInstanceIndexAtReference, this, decoration,
  1241. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1242. }
  1243. return SPV_SUCCESS;
  1244. }
  1245. spv_result_t BuiltInsValidator::ValidatePatchVerticesAtDefinition(
  1246. const Decoration& decoration, const Instruction& inst) {
  1247. if (spvIsVulkanEnv(_.context()->target_env)) {
  1248. if (spv_result_t error = ValidateI32(
  1249. decoration, inst,
  1250. [this, &inst](const std::string& message) -> spv_result_t {
  1251. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1252. << "According to the Vulkan spec BuiltIn PatchVertices "
  1253. "variable needs to be a 32-bit int scalar. "
  1254. << message;
  1255. })) {
  1256. return error;
  1257. }
  1258. }
  1259. // Seed at reference checks with this built-in.
  1260. return ValidatePatchVerticesAtReference(decoration, inst, inst, inst);
  1261. }
  1262. spv_result_t BuiltInsValidator::ValidatePatchVerticesAtReference(
  1263. const Decoration& decoration, const Instruction& built_in_inst,
  1264. const Instruction& referenced_inst,
  1265. const Instruction& referenced_from_inst) {
  1266. if (spvIsVulkanEnv(_.context()->target_env)) {
  1267. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1268. if (storage_class != SpvStorageClassMax &&
  1269. storage_class != SpvStorageClassInput) {
  1270. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1271. << "Vulkan spec allows BuiltIn PatchVertices to be only used for "
  1272. "variables with Input storage class. "
  1273. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1274. referenced_from_inst)
  1275. << " " << GetStorageClassDesc(referenced_from_inst);
  1276. }
  1277. for (const SpvExecutionModel execution_model : execution_models_) {
  1278. if (execution_model != SpvExecutionModelTessellationControl &&
  1279. execution_model != SpvExecutionModelTessellationEvaluation) {
  1280. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1281. << "Vulkan spec allows BuiltIn PatchVertices to be used only "
  1282. "with TessellationControl or TessellationEvaluation "
  1283. "execution models. "
  1284. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1285. referenced_from_inst, execution_model);
  1286. }
  1287. }
  1288. }
  1289. if (function_id_ == 0) {
  1290. // Propagate this rule to all dependant ids in the global scope.
  1291. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1292. &BuiltInsValidator::ValidatePatchVerticesAtReference, this, decoration,
  1293. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1294. }
  1295. return SPV_SUCCESS;
  1296. }
  1297. spv_result_t BuiltInsValidator::ValidatePointCoordAtDefinition(
  1298. const Decoration& decoration, const Instruction& inst) {
  1299. if (spvIsVulkanEnv(_.context()->target_env)) {
  1300. if (spv_result_t error = ValidateF32Vec(
  1301. decoration, inst, 2,
  1302. [this, &inst](const std::string& message) -> spv_result_t {
  1303. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1304. << "According to the Vulkan spec BuiltIn PointCoord "
  1305. "variable needs to be a 2-component 32-bit float "
  1306. "vector. "
  1307. << message;
  1308. })) {
  1309. return error;
  1310. }
  1311. }
  1312. // Seed at reference checks with this built-in.
  1313. return ValidatePointCoordAtReference(decoration, inst, inst, inst);
  1314. }
  1315. spv_result_t BuiltInsValidator::ValidatePointCoordAtReference(
  1316. const Decoration& decoration, const Instruction& built_in_inst,
  1317. const Instruction& referenced_inst,
  1318. const Instruction& referenced_from_inst) {
  1319. if (spvIsVulkanEnv(_.context()->target_env)) {
  1320. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1321. if (storage_class != SpvStorageClassMax &&
  1322. storage_class != SpvStorageClassInput) {
  1323. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1324. << "Vulkan spec allows BuiltIn PointCoord to be only used for "
  1325. "variables with Input storage class. "
  1326. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1327. referenced_from_inst)
  1328. << " " << GetStorageClassDesc(referenced_from_inst);
  1329. }
  1330. for (const SpvExecutionModel execution_model : execution_models_) {
  1331. if (execution_model != SpvExecutionModelFragment) {
  1332. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1333. << "Vulkan spec allows BuiltIn PointCoord to be used only with "
  1334. "Fragment execution model. "
  1335. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1336. referenced_from_inst, execution_model);
  1337. }
  1338. }
  1339. }
  1340. if (function_id_ == 0) {
  1341. // Propagate this rule to all dependant ids in the global scope.
  1342. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1343. &BuiltInsValidator::ValidatePointCoordAtReference, this, decoration,
  1344. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1345. }
  1346. return SPV_SUCCESS;
  1347. }
  1348. spv_result_t BuiltInsValidator::ValidatePointSizeAtDefinition(
  1349. const Decoration& decoration, const Instruction& inst) {
  1350. // Seed at reference checks with this built-in.
  1351. return ValidatePointSizeAtReference(decoration, inst, inst, inst);
  1352. }
  1353. spv_result_t BuiltInsValidator::ValidatePointSizeAtReference(
  1354. const Decoration& decoration, const Instruction& built_in_inst,
  1355. const Instruction& referenced_inst,
  1356. const Instruction& referenced_from_inst) {
  1357. if (spvIsVulkanEnv(_.context()->target_env)) {
  1358. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1359. if (storage_class != SpvStorageClassMax &&
  1360. storage_class != SpvStorageClassInput &&
  1361. storage_class != SpvStorageClassOutput) {
  1362. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1363. << "Vulkan spec allows BuiltIn PointSize to be only used for "
  1364. "variables with Input or Output storage class. "
  1365. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1366. referenced_from_inst)
  1367. << " " << GetStorageClassDesc(referenced_from_inst);
  1368. }
  1369. if (storage_class == SpvStorageClassInput) {
  1370. assert(function_id_ == 0);
  1371. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1372. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
  1373. "Vulkan spec doesn't allow BuiltIn PointSize to be used for "
  1374. "variables with Input storage class if execution model is Vertex.",
  1375. SpvExecutionModelVertex, decoration, built_in_inst,
  1376. referenced_from_inst, std::placeholders::_1));
  1377. }
  1378. for (const SpvExecutionModel execution_model : execution_models_) {
  1379. switch (execution_model) {
  1380. case SpvExecutionModelVertex: {
  1381. if (spv_result_t error = ValidateF32(
  1382. decoration, built_in_inst,
  1383. [this, &referenced_from_inst](
  1384. const std::string& message) -> spv_result_t {
  1385. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1386. << "According to the Vulkan spec BuiltIn PointSize "
  1387. "variable needs to be a 32-bit float scalar. "
  1388. << message;
  1389. })) {
  1390. return error;
  1391. }
  1392. break;
  1393. }
  1394. case SpvExecutionModelTessellationControl:
  1395. case SpvExecutionModelTessellationEvaluation:
  1396. case SpvExecutionModelGeometry:
  1397. case SpvExecutionModelMeshNV: {
  1398. // PointSize can be a per-vertex variable for tessellation control,
  1399. // tessellation evaluation and geometry shader stages. In such cases
  1400. // variables will have an array of 32-bit floats.
  1401. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  1402. // The array is on the variable, so this must be a 32-bit float.
  1403. if (spv_result_t error = ValidateF32(
  1404. decoration, built_in_inst,
  1405. [this, &referenced_from_inst](
  1406. const std::string& message) -> spv_result_t {
  1407. return _.diag(SPV_ERROR_INVALID_DATA,
  1408. &referenced_from_inst)
  1409. << "According to the Vulkan spec BuiltIn "
  1410. "PointSize variable needs to be a 32-bit "
  1411. "float scalar. "
  1412. << message;
  1413. })) {
  1414. return error;
  1415. }
  1416. } else {
  1417. if (spv_result_t error = ValidateOptionalArrayedF32(
  1418. decoration, built_in_inst,
  1419. [this, &referenced_from_inst](
  1420. const std::string& message) -> spv_result_t {
  1421. return _.diag(SPV_ERROR_INVALID_DATA,
  1422. &referenced_from_inst)
  1423. << "According to the Vulkan spec BuiltIn "
  1424. "PointSize variable needs to be a 32-bit "
  1425. "float scalar. "
  1426. << message;
  1427. })) {
  1428. return error;
  1429. }
  1430. }
  1431. break;
  1432. }
  1433. default: {
  1434. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1435. << "Vulkan spec allows BuiltIn PointSize to be used only with "
  1436. "Vertex, TessellationControl, TessellationEvaluation or "
  1437. "Geometry execution models. "
  1438. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1439. referenced_from_inst, execution_model);
  1440. }
  1441. }
  1442. }
  1443. }
  1444. if (function_id_ == 0) {
  1445. // Propagate this rule to all dependant ids in the global scope.
  1446. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1447. &BuiltInsValidator::ValidatePointSizeAtReference, this, decoration,
  1448. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1449. }
  1450. return SPV_SUCCESS;
  1451. }
  1452. spv_result_t BuiltInsValidator::ValidatePositionAtDefinition(
  1453. const Decoration& decoration, const Instruction& inst) {
  1454. // Seed at reference checks with this built-in.
  1455. return ValidatePositionAtReference(decoration, inst, inst, inst);
  1456. }
  1457. spv_result_t BuiltInsValidator::ValidatePositionAtReference(
  1458. const Decoration& decoration, const Instruction& built_in_inst,
  1459. const Instruction& referenced_inst,
  1460. const Instruction& referenced_from_inst) {
  1461. if (spvIsVulkanEnv(_.context()->target_env)) {
  1462. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1463. if (storage_class != SpvStorageClassMax &&
  1464. storage_class != SpvStorageClassInput &&
  1465. storage_class != SpvStorageClassOutput) {
  1466. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1467. << "Vulkan spec allows BuiltIn Position to be only used for "
  1468. "variables with Input or Output storage class. "
  1469. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1470. referenced_from_inst)
  1471. << " " << GetStorageClassDesc(referenced_from_inst);
  1472. }
  1473. if (storage_class == SpvStorageClassInput) {
  1474. assert(function_id_ == 0);
  1475. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1476. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
  1477. "Vulkan spec doesn't allow BuiltIn Position to be used for variables "
  1478. "with Input storage class if execution model is Vertex.",
  1479. SpvExecutionModelVertex, decoration, built_in_inst,
  1480. referenced_from_inst, std::placeholders::_1));
  1481. }
  1482. for (const SpvExecutionModel execution_model : execution_models_) {
  1483. switch (execution_model) {
  1484. case SpvExecutionModelVertex: {
  1485. if (spv_result_t error = ValidateF32Vec(
  1486. decoration, built_in_inst, 4,
  1487. [this, &referenced_from_inst](
  1488. const std::string& message) -> spv_result_t {
  1489. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1490. << "According to the Vulkan spec BuiltIn Position "
  1491. "variable needs to be a 4-component 32-bit float "
  1492. "vector. "
  1493. << message;
  1494. })) {
  1495. return error;
  1496. }
  1497. break;
  1498. }
  1499. case SpvExecutionModelGeometry:
  1500. case SpvExecutionModelTessellationControl:
  1501. case SpvExecutionModelTessellationEvaluation:
  1502. case SpvExecutionModelMeshNV: {
  1503. // Position can be a per-vertex variable for tessellation control,
  1504. // tessellation evaluation, geometry and mesh shader stages. In such
  1505. // cases variables will have an array of 4-component 32-bit float
  1506. // vectors.
  1507. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  1508. // The array is on the variable, so this must be a 4-component
  1509. // 32-bit float vector.
  1510. if (spv_result_t error = ValidateF32Vec(
  1511. decoration, built_in_inst, 4,
  1512. [this, &referenced_from_inst](
  1513. const std::string& message) -> spv_result_t {
  1514. return _.diag(SPV_ERROR_INVALID_DATA,
  1515. &referenced_from_inst)
  1516. << "According to the Vulkan spec BuiltIn Position "
  1517. "variable needs to be a 4-component 32-bit "
  1518. "float vector. "
  1519. << message;
  1520. })) {
  1521. return error;
  1522. }
  1523. } else {
  1524. if (spv_result_t error = ValidateOptionalArrayedF32Vec(
  1525. decoration, built_in_inst, 4,
  1526. [this, &referenced_from_inst](
  1527. const std::string& message) -> spv_result_t {
  1528. return _.diag(SPV_ERROR_INVALID_DATA,
  1529. &referenced_from_inst)
  1530. << "According to the Vulkan spec BuiltIn Position "
  1531. "variable needs to be a 4-component 32-bit "
  1532. "float vector. "
  1533. << message;
  1534. })) {
  1535. return error;
  1536. }
  1537. }
  1538. break;
  1539. }
  1540. default: {
  1541. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1542. << "Vulkan spec allows BuiltIn Position to be used only "
  1543. "with Vertex, TessellationControl, TessellationEvaluation"
  1544. " or Geometry execution models. "
  1545. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1546. referenced_from_inst, execution_model);
  1547. }
  1548. }
  1549. }
  1550. }
  1551. if (spvIsWebGPUEnv(_.context()->target_env)) {
  1552. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1553. if (storage_class != SpvStorageClassMax &&
  1554. storage_class != SpvStorageClassOutput) {
  1555. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1556. << "WebGPU spec allows BuiltIn Position to be only used for "
  1557. "variables with Output storage class. "
  1558. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1559. referenced_from_inst)
  1560. << " " << GetStorageClassDesc(referenced_from_inst);
  1561. }
  1562. for (const SpvExecutionModel execution_model : execution_models_) {
  1563. switch (execution_model) {
  1564. case SpvExecutionModelVertex: {
  1565. if (spv_result_t error = ValidateF32Vec(
  1566. decoration, built_in_inst, 4,
  1567. [this, &referenced_from_inst](
  1568. const std::string& message) -> spv_result_t {
  1569. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1570. << "According to the WebGPU spec BuiltIn Position "
  1571. "variable needs to be a 4-component 32-bit float "
  1572. "vector. "
  1573. << message;
  1574. })) {
  1575. return error;
  1576. }
  1577. break;
  1578. }
  1579. default: {
  1580. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1581. << "WebGPU spec allows BuiltIn Position to be used only "
  1582. "with the Vertex execution model. "
  1583. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1584. referenced_from_inst, execution_model);
  1585. }
  1586. }
  1587. }
  1588. }
  1589. if (function_id_ == 0) {
  1590. // Propagate this rule to all dependant ids in the global scope.
  1591. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1592. &BuiltInsValidator::ValidatePositionAtReference, this, decoration,
  1593. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1594. }
  1595. return SPV_SUCCESS;
  1596. }
  1597. spv_result_t BuiltInsValidator::ValidatePrimitiveIdAtDefinition(
  1598. const Decoration& decoration, const Instruction& inst) {
  1599. if (spvIsVulkanEnv(_.context()->target_env)) {
  1600. // PrimitiveId can be a per-primitive variable for mesh shader stage.
  1601. // In such cases variable will have an array of 32-bit integers.
  1602. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  1603. // This must be a 32-bit int scalar.
  1604. if (spv_result_t error = ValidateI32(
  1605. decoration, inst,
  1606. [this, &inst](const std::string& message) -> spv_result_t {
  1607. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1608. << "According to the Vulkan spec BuiltIn PrimitiveId "
  1609. "variable needs to be a 32-bit int scalar. "
  1610. << message;
  1611. })) {
  1612. return error;
  1613. }
  1614. } else {
  1615. if (spv_result_t error = ValidateOptionalArrayedI32(
  1616. decoration, inst,
  1617. [this, &inst](const std::string& message) -> spv_result_t {
  1618. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1619. << "According to the Vulkan spec BuiltIn PrimitiveId "
  1620. "variable needs to be a 32-bit int scalar. "
  1621. << message;
  1622. })) {
  1623. return error;
  1624. }
  1625. }
  1626. }
  1627. // Seed at reference checks with this built-in.
  1628. return ValidatePrimitiveIdAtReference(decoration, inst, inst, inst);
  1629. }
  1630. spv_result_t BuiltInsValidator::ValidatePrimitiveIdAtReference(
  1631. const Decoration& decoration, const Instruction& built_in_inst,
  1632. const Instruction& referenced_inst,
  1633. const Instruction& referenced_from_inst) {
  1634. if (spvIsVulkanEnv(_.context()->target_env)) {
  1635. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1636. if (storage_class != SpvStorageClassMax &&
  1637. storage_class != SpvStorageClassInput &&
  1638. storage_class != SpvStorageClassOutput) {
  1639. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1640. << "Vulkan spec allows BuiltIn PrimitiveId to be only used for "
  1641. "variables with Input or Output storage class. "
  1642. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1643. referenced_from_inst)
  1644. << " " << GetStorageClassDesc(referenced_from_inst);
  1645. }
  1646. if (storage_class == SpvStorageClassOutput) {
  1647. assert(function_id_ == 0);
  1648. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1649. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
  1650. "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
  1651. "variables with Output storage class if execution model is "
  1652. "TessellationControl.",
  1653. SpvExecutionModelTessellationControl, decoration, built_in_inst,
  1654. referenced_from_inst, std::placeholders::_1));
  1655. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1656. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
  1657. "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
  1658. "variables with Output storage class if execution model is "
  1659. "TessellationEvaluation.",
  1660. SpvExecutionModelTessellationEvaluation, decoration, built_in_inst,
  1661. referenced_from_inst, std::placeholders::_1));
  1662. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1663. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
  1664. "Vulkan spec doesn't allow BuiltIn PrimitiveId to be used for "
  1665. "variables with Output storage class if execution model is "
  1666. "Fragment.",
  1667. SpvExecutionModelFragment, decoration, built_in_inst,
  1668. referenced_from_inst, std::placeholders::_1));
  1669. }
  1670. for (const SpvExecutionModel execution_model : execution_models_) {
  1671. switch (execution_model) {
  1672. case SpvExecutionModelFragment:
  1673. case SpvExecutionModelTessellationControl:
  1674. case SpvExecutionModelTessellationEvaluation:
  1675. case SpvExecutionModelGeometry:
  1676. case SpvExecutionModelMeshNV:
  1677. case SpvExecutionModelRayGenerationNV:
  1678. case SpvExecutionModelIntersectionNV:
  1679. case SpvExecutionModelAnyHitNV:
  1680. case SpvExecutionModelClosestHitNV:
  1681. case SpvExecutionModelMissNV:
  1682. case SpvExecutionModelCallableNV: {
  1683. // Ok.
  1684. break;
  1685. }
  1686. default: {
  1687. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1688. << "Vulkan spec allows BuiltIn PrimitiveId to be used only "
  1689. "with Fragment, TessellationControl, "
  1690. "TessellationEvaluation or Geometry execution models. "
  1691. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1692. referenced_from_inst, execution_model);
  1693. }
  1694. }
  1695. }
  1696. }
  1697. if (function_id_ == 0) {
  1698. // Propagate this rule to all dependant ids in the global scope.
  1699. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1700. &BuiltInsValidator::ValidatePrimitiveIdAtReference, this, decoration,
  1701. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1702. }
  1703. return SPV_SUCCESS;
  1704. }
  1705. spv_result_t BuiltInsValidator::ValidateSampleIdAtDefinition(
  1706. const Decoration& decoration, const Instruction& inst) {
  1707. if (spvIsVulkanEnv(_.context()->target_env)) {
  1708. if (spv_result_t error = ValidateI32(
  1709. decoration, inst,
  1710. [this, &inst](const std::string& message) -> spv_result_t {
  1711. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1712. << "According to the Vulkan spec BuiltIn SampleId "
  1713. "variable needs to be a 32-bit int scalar. "
  1714. << message;
  1715. })) {
  1716. return error;
  1717. }
  1718. }
  1719. // Seed at reference checks with this built-in.
  1720. return ValidateSampleIdAtReference(decoration, inst, inst, inst);
  1721. }
  1722. spv_result_t BuiltInsValidator::ValidateSampleIdAtReference(
  1723. const Decoration& decoration, const Instruction& built_in_inst,
  1724. const Instruction& referenced_inst,
  1725. const Instruction& referenced_from_inst) {
  1726. if (spvIsVulkanEnv(_.context()->target_env)) {
  1727. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1728. if (storage_class != SpvStorageClassMax &&
  1729. storage_class != SpvStorageClassInput) {
  1730. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1731. << "Vulkan spec allows BuiltIn SampleId to be only used for "
  1732. "variables with Input storage class. "
  1733. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1734. referenced_from_inst)
  1735. << " " << GetStorageClassDesc(referenced_from_inst);
  1736. }
  1737. for (const SpvExecutionModel execution_model : execution_models_) {
  1738. if (execution_model != SpvExecutionModelFragment) {
  1739. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1740. << "Vulkan spec allows BuiltIn SampleId to be used only with "
  1741. "Fragment execution model. "
  1742. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1743. referenced_from_inst, execution_model);
  1744. }
  1745. }
  1746. }
  1747. if (function_id_ == 0) {
  1748. // Propagate this rule to all dependant ids in the global scope.
  1749. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1750. &BuiltInsValidator::ValidateSampleIdAtReference, this, decoration,
  1751. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1752. }
  1753. return SPV_SUCCESS;
  1754. }
  1755. spv_result_t BuiltInsValidator::ValidateSampleMaskAtDefinition(
  1756. const Decoration& decoration, const Instruction& inst) {
  1757. if (spvIsVulkanEnv(_.context()->target_env)) {
  1758. if (spv_result_t error = ValidateI32Arr(
  1759. decoration, inst,
  1760. [this, &inst](const std::string& message) -> spv_result_t {
  1761. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1762. << "According to the Vulkan spec BuiltIn SampleMask "
  1763. "variable needs to be a 32-bit int array. "
  1764. << message;
  1765. })) {
  1766. return error;
  1767. }
  1768. }
  1769. // Seed at reference checks with this built-in.
  1770. return ValidateSampleMaskAtReference(decoration, inst, inst, inst);
  1771. }
  1772. spv_result_t BuiltInsValidator::ValidateSampleMaskAtReference(
  1773. const Decoration& decoration, const Instruction& built_in_inst,
  1774. const Instruction& referenced_inst,
  1775. const Instruction& referenced_from_inst) {
  1776. if (spvIsVulkanEnv(_.context()->target_env)) {
  1777. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1778. if (storage_class != SpvStorageClassMax &&
  1779. storage_class != SpvStorageClassInput &&
  1780. storage_class != SpvStorageClassOutput) {
  1781. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1782. << "Vulkan spec allows BuiltIn SampleMask to be only used for "
  1783. "variables with Input or Output storage class. "
  1784. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1785. referenced_from_inst)
  1786. << " " << GetStorageClassDesc(referenced_from_inst);
  1787. }
  1788. for (const SpvExecutionModel execution_model : execution_models_) {
  1789. if (execution_model != SpvExecutionModelFragment) {
  1790. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1791. << "Vulkan spec allows BuiltIn SampleMask to be used only "
  1792. "with "
  1793. "Fragment execution model. "
  1794. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1795. referenced_from_inst, execution_model);
  1796. }
  1797. }
  1798. }
  1799. if (function_id_ == 0) {
  1800. // Propagate this rule to all dependant ids in the global scope.
  1801. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1802. &BuiltInsValidator::ValidateSampleMaskAtReference, this, decoration,
  1803. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1804. }
  1805. return SPV_SUCCESS;
  1806. }
  1807. spv_result_t BuiltInsValidator::ValidateSamplePositionAtDefinition(
  1808. const Decoration& decoration, const Instruction& inst) {
  1809. if (spvIsVulkanEnv(_.context()->target_env)) {
  1810. if (spv_result_t error = ValidateF32Vec(
  1811. decoration, inst, 2,
  1812. [this, &inst](const std::string& message) -> spv_result_t {
  1813. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1814. << "According to the Vulkan spec BuiltIn SamplePosition "
  1815. "variable needs to be a 2-component 32-bit float "
  1816. "vector. "
  1817. << message;
  1818. })) {
  1819. return error;
  1820. }
  1821. }
  1822. // Seed at reference checks with this built-in.
  1823. return ValidateSamplePositionAtReference(decoration, inst, inst, inst);
  1824. }
  1825. spv_result_t BuiltInsValidator::ValidateSamplePositionAtReference(
  1826. const Decoration& decoration, const Instruction& built_in_inst,
  1827. const Instruction& referenced_inst,
  1828. const Instruction& referenced_from_inst) {
  1829. if (spvIsVulkanEnv(_.context()->target_env)) {
  1830. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1831. if (storage_class != SpvStorageClassMax &&
  1832. storage_class != SpvStorageClassInput) {
  1833. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1834. << "Vulkan spec allows BuiltIn SamplePosition to be only used "
  1835. "for "
  1836. "variables with Input storage class. "
  1837. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1838. referenced_from_inst)
  1839. << " " << GetStorageClassDesc(referenced_from_inst);
  1840. }
  1841. for (const SpvExecutionModel execution_model : execution_models_) {
  1842. if (execution_model != SpvExecutionModelFragment) {
  1843. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1844. << "Vulkan spec allows BuiltIn SamplePosition to be used only "
  1845. "with "
  1846. "Fragment execution model. "
  1847. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1848. referenced_from_inst, execution_model);
  1849. }
  1850. }
  1851. }
  1852. if (function_id_ == 0) {
  1853. // Propagate this rule to all dependant ids in the global scope.
  1854. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1855. &BuiltInsValidator::ValidateSamplePositionAtReference, this, decoration,
  1856. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1857. }
  1858. return SPV_SUCCESS;
  1859. }
  1860. spv_result_t BuiltInsValidator::ValidateTessCoordAtDefinition(
  1861. const Decoration& decoration, const Instruction& inst) {
  1862. if (spvIsVulkanEnv(_.context()->target_env)) {
  1863. if (spv_result_t error = ValidateF32Vec(
  1864. decoration, inst, 3,
  1865. [this, &inst](const std::string& message) -> spv_result_t {
  1866. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1867. << "According to the Vulkan spec BuiltIn TessCoord "
  1868. "variable needs to be a 3-component 32-bit float "
  1869. "vector. "
  1870. << message;
  1871. })) {
  1872. return error;
  1873. }
  1874. }
  1875. // Seed at reference checks with this built-in.
  1876. return ValidateTessCoordAtReference(decoration, inst, inst, inst);
  1877. }
  1878. spv_result_t BuiltInsValidator::ValidateTessCoordAtReference(
  1879. const Decoration& decoration, const Instruction& built_in_inst,
  1880. const Instruction& referenced_inst,
  1881. const Instruction& referenced_from_inst) {
  1882. if (spvIsVulkanEnv(_.context()->target_env)) {
  1883. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1884. if (storage_class != SpvStorageClassMax &&
  1885. storage_class != SpvStorageClassInput) {
  1886. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1887. << "Vulkan spec allows BuiltIn TessCoord to be only used for "
  1888. "variables with Input storage class. "
  1889. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1890. referenced_from_inst)
  1891. << " " << GetStorageClassDesc(referenced_from_inst);
  1892. }
  1893. for (const SpvExecutionModel execution_model : execution_models_) {
  1894. if (execution_model != SpvExecutionModelTessellationEvaluation) {
  1895. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1896. << "Vulkan spec allows BuiltIn TessCoord to be used only with "
  1897. "TessellationEvaluation execution model. "
  1898. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1899. referenced_from_inst, execution_model);
  1900. }
  1901. }
  1902. }
  1903. if (function_id_ == 0) {
  1904. // Propagate this rule to all dependant ids in the global scope.
  1905. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1906. &BuiltInsValidator::ValidateTessCoordAtReference, this, decoration,
  1907. built_in_inst, referenced_from_inst, std::placeholders::_1));
  1908. }
  1909. return SPV_SUCCESS;
  1910. }
  1911. spv_result_t BuiltInsValidator::ValidateTessLevelOuterAtDefinition(
  1912. const Decoration& decoration, const Instruction& inst) {
  1913. if (spvIsVulkanEnv(_.context()->target_env)) {
  1914. if (spv_result_t error = ValidateF32Arr(
  1915. decoration, inst, 4,
  1916. [this, &inst](const std::string& message) -> spv_result_t {
  1917. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1918. << "According to the Vulkan spec BuiltIn TessLevelOuter "
  1919. "variable needs to be a 4-component 32-bit float "
  1920. "array. "
  1921. << message;
  1922. })) {
  1923. return error;
  1924. }
  1925. }
  1926. // Seed at reference checks with this built-in.
  1927. return ValidateTessLevelAtReference(decoration, inst, inst, inst);
  1928. }
  1929. spv_result_t BuiltInsValidator::ValidateTessLevelInnerAtDefinition(
  1930. const Decoration& decoration, const Instruction& inst) {
  1931. if (spvIsVulkanEnv(_.context()->target_env)) {
  1932. if (spv_result_t error = ValidateF32Arr(
  1933. decoration, inst, 2,
  1934. [this, &inst](const std::string& message) -> spv_result_t {
  1935. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  1936. << "According to the Vulkan spec BuiltIn TessLevelOuter "
  1937. "variable needs to be a 2-component 32-bit float "
  1938. "array. "
  1939. << message;
  1940. })) {
  1941. return error;
  1942. }
  1943. }
  1944. // Seed at reference checks with this built-in.
  1945. return ValidateTessLevelAtReference(decoration, inst, inst, inst);
  1946. }
  1947. spv_result_t BuiltInsValidator::ValidateTessLevelAtReference(
  1948. const Decoration& decoration, const Instruction& built_in_inst,
  1949. const Instruction& referenced_inst,
  1950. const Instruction& referenced_from_inst) {
  1951. if (spvIsVulkanEnv(_.context()->target_env)) {
  1952. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  1953. if (storage_class != SpvStorageClassMax &&
  1954. storage_class != SpvStorageClassInput &&
  1955. storage_class != SpvStorageClassOutput) {
  1956. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1957. << "Vulkan spec allows BuiltIn "
  1958. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  1959. decoration.params()[0])
  1960. << " to be only used for variables with Input or Output storage "
  1961. "class. "
  1962. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  1963. referenced_from_inst)
  1964. << " " << GetStorageClassDesc(referenced_from_inst);
  1965. }
  1966. if (storage_class == SpvStorageClassInput) {
  1967. assert(function_id_ == 0);
  1968. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1969. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
  1970. "Vulkan spec doesn't allow TessLevelOuter/TessLevelInner to be "
  1971. "used "
  1972. "for variables with Input storage class if execution model is "
  1973. "TessellationControl.",
  1974. SpvExecutionModelTessellationControl, decoration, built_in_inst,
  1975. referenced_from_inst, std::placeholders::_1));
  1976. }
  1977. if (storage_class == SpvStorageClassOutput) {
  1978. assert(function_id_ == 0);
  1979. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  1980. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
  1981. "Vulkan spec doesn't allow TessLevelOuter/TessLevelInner to be "
  1982. "used "
  1983. "for variables with Output storage class if execution model is "
  1984. "TessellationEvaluation.",
  1985. SpvExecutionModelTessellationEvaluation, decoration, built_in_inst,
  1986. referenced_from_inst, std::placeholders::_1));
  1987. }
  1988. for (const SpvExecutionModel execution_model : execution_models_) {
  1989. switch (execution_model) {
  1990. case SpvExecutionModelTessellationControl:
  1991. case SpvExecutionModelTessellationEvaluation: {
  1992. // Ok.
  1993. break;
  1994. }
  1995. default: {
  1996. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  1997. << "Vulkan spec allows BuiltIn "
  1998. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  1999. decoration.params()[0])
  2000. << " to be used only with TessellationControl or "
  2001. "TessellationEvaluation execution models. "
  2002. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2003. referenced_from_inst, execution_model);
  2004. }
  2005. }
  2006. }
  2007. }
  2008. if (function_id_ == 0) {
  2009. // Propagate this rule to all dependant ids in the global scope.
  2010. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2011. &BuiltInsValidator::ValidateTessLevelAtReference, this, decoration,
  2012. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2013. }
  2014. return SPV_SUCCESS;
  2015. }
  2016. spv_result_t BuiltInsValidator::ValidateVertexIndexAtDefinition(
  2017. const Decoration& decoration, const Instruction& inst) {
  2018. if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
  2019. if (spv_result_t error = ValidateI32(
  2020. decoration, inst,
  2021. [this, &inst](const std::string& message) -> spv_result_t {
  2022. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2023. << "According to the "
  2024. << spvLogStringForEnv(_.context()->target_env)
  2025. << " spec BuiltIn VertexIndex variable needs to be a "
  2026. "32-bit int scalar. "
  2027. << message;
  2028. })) {
  2029. return error;
  2030. }
  2031. }
  2032. // Seed at reference checks with this built-in.
  2033. return ValidateVertexIndexAtReference(decoration, inst, inst, inst);
  2034. }
  2035. spv_result_t BuiltInsValidator::ValidateVertexIdOrInstanceIdAtDefinition(
  2036. const Decoration& decoration, const Instruction& inst) {
  2037. const SpvBuiltIn label = SpvBuiltIn(decoration.params()[0]);
  2038. bool allow_instance_id = _.HasCapability(SpvCapabilityRayTracingNV) &&
  2039. label == SpvBuiltInInstanceId;
  2040. if (spvIsVulkanEnv(_.context()->target_env) && !allow_instance_id) {
  2041. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2042. << "Vulkan spec doesn't allow BuiltIn VertexId/InstanceId "
  2043. "to be used.";
  2044. }
  2045. if (label == SpvBuiltInInstanceId) {
  2046. return ValidateInstanceIdAtReference(decoration, inst, inst, inst);
  2047. }
  2048. return SPV_SUCCESS;
  2049. }
  2050. spv_result_t BuiltInsValidator::ValidateInstanceIdAtReference(
  2051. const Decoration& decoration, const Instruction& built_in_inst,
  2052. const Instruction& referenced_inst,
  2053. const Instruction& referenced_from_inst) {
  2054. if (spvIsVulkanEnv(_.context()->target_env)) {
  2055. for (const SpvExecutionModel execution_model : execution_models_) {
  2056. switch (execution_model) {
  2057. case SpvExecutionModelIntersectionNV:
  2058. case SpvExecutionModelClosestHitNV:
  2059. case SpvExecutionModelAnyHitNV:
  2060. // Do nothing, valid stages
  2061. break;
  2062. default:
  2063. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2064. << "Vulkan spec allows BuiltIn InstanceId to be used "
  2065. "only with IntersectionNV, ClosestHitNV and AnyHitNV "
  2066. "execution models. "
  2067. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2068. referenced_from_inst);
  2069. break;
  2070. }
  2071. }
  2072. }
  2073. if (function_id_ == 0) {
  2074. // Propagate this rule to all dependant ids in the global scope.
  2075. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2076. &BuiltInsValidator::ValidateInstanceIdAtReference, this, decoration,
  2077. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2078. }
  2079. return SPV_SUCCESS;
  2080. }
  2081. spv_result_t BuiltInsValidator::ValidateLocalInvocationIndexAtDefinition(
  2082. const Decoration& decoration, const Instruction& inst) {
  2083. if (spvIsWebGPUEnv(_.context()->target_env)) {
  2084. if (spv_result_t error = ValidateI32(
  2085. decoration, inst,
  2086. [this, &inst](const std::string& message) -> spv_result_t {
  2087. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2088. << "According to the WebGPU spec BuiltIn "
  2089. "LocalInvocationIndex variable needs to be a 32-bit "
  2090. "int."
  2091. << message;
  2092. })) {
  2093. return error;
  2094. }
  2095. }
  2096. // Seed at reference checks with this built-in.
  2097. return ValidateLocalInvocationIndexAtReference(decoration, inst, inst, inst);
  2098. }
  2099. spv_result_t BuiltInsValidator::ValidateLocalInvocationIndexAtReference(
  2100. const Decoration& decoration, const Instruction& built_in_inst,
  2101. const Instruction& referenced_inst,
  2102. const Instruction& referenced_from_inst) {
  2103. if (spvIsWebGPUEnv(_.context()->target_env)) {
  2104. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2105. if (storage_class != SpvStorageClassMax &&
  2106. storage_class != SpvStorageClassInput) {
  2107. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2108. << "WebGPU spec allows BuiltIn LocalInvocationIndex to be only "
  2109. "used for variables with Input storage class. "
  2110. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2111. referenced_from_inst)
  2112. << " " << GetStorageClassDesc(referenced_from_inst);
  2113. }
  2114. for (const SpvExecutionModel execution_model : execution_models_) {
  2115. if (execution_model != SpvExecutionModelGLCompute) {
  2116. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2117. << "WebGPU spec allows BuiltIn VertexIndex to be used only "
  2118. "with GLCompute execution model. "
  2119. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2120. referenced_from_inst, execution_model);
  2121. }
  2122. }
  2123. }
  2124. if (function_id_ == 0) {
  2125. // Propagate this rule to all dependant ids in the global scope.
  2126. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  2127. std::bind(&BuiltInsValidator::ValidateLocalInvocationIndexAtReference,
  2128. this, decoration, built_in_inst, referenced_from_inst,
  2129. std::placeholders::_1));
  2130. }
  2131. return SPV_SUCCESS;
  2132. }
  2133. spv_result_t BuiltInsValidator::ValidateVertexIndexAtReference(
  2134. const Decoration& decoration, const Instruction& built_in_inst,
  2135. const Instruction& referenced_inst,
  2136. const Instruction& referenced_from_inst) {
  2137. if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
  2138. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2139. if (storage_class != SpvStorageClassMax &&
  2140. storage_class != SpvStorageClassInput) {
  2141. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2142. << spvLogStringForEnv(_.context()->target_env)
  2143. << " spec allows BuiltIn VertexIndex to be only used for "
  2144. "variables with Input storage class. "
  2145. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2146. referenced_from_inst)
  2147. << " " << GetStorageClassDesc(referenced_from_inst);
  2148. }
  2149. for (const SpvExecutionModel execution_model : execution_models_) {
  2150. if (execution_model != SpvExecutionModelVertex) {
  2151. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2152. << spvLogStringForEnv(_.context()->target_env)
  2153. << " spec allows BuiltIn VertexIndex to be used only with "
  2154. "Vertex execution model. "
  2155. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2156. referenced_from_inst, execution_model);
  2157. }
  2158. }
  2159. }
  2160. if (function_id_ == 0) {
  2161. // Propagate this rule to all dependant ids in the global scope.
  2162. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2163. &BuiltInsValidator::ValidateVertexIndexAtReference, this, decoration,
  2164. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2165. }
  2166. return SPV_SUCCESS;
  2167. }
  2168. spv_result_t BuiltInsValidator::ValidateLayerOrViewportIndexAtDefinition(
  2169. const Decoration& decoration, const Instruction& inst) {
  2170. if (spvIsVulkanEnv(_.context()->target_env)) {
  2171. // This can be a per-primitive variable for mesh shader stage.
  2172. // In such cases variable will have an array of 32-bit integers.
  2173. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  2174. // This must be a 32-bit int scalar.
  2175. if (spv_result_t error = ValidateI32(
  2176. decoration, inst,
  2177. [this, &decoration,
  2178. &inst](const std::string& message) -> spv_result_t {
  2179. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2180. << "According to the Vulkan spec BuiltIn "
  2181. << _.grammar().lookupOperandName(
  2182. SPV_OPERAND_TYPE_BUILT_IN, decoration.params()[0])
  2183. << "variable needs to be a 32-bit int scalar. "
  2184. << message;
  2185. })) {
  2186. return error;
  2187. }
  2188. } else {
  2189. if (spv_result_t error = ValidateOptionalArrayedI32(
  2190. decoration, inst,
  2191. [this, &decoration,
  2192. &inst](const std::string& message) -> spv_result_t {
  2193. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2194. << "According to the Vulkan spec BuiltIn "
  2195. << _.grammar().lookupOperandName(
  2196. SPV_OPERAND_TYPE_BUILT_IN, decoration.params()[0])
  2197. << "variable needs to be a 32-bit int scalar. "
  2198. << message;
  2199. })) {
  2200. return error;
  2201. }
  2202. }
  2203. }
  2204. // Seed at reference checks with this built-in.
  2205. return ValidateLayerOrViewportIndexAtReference(decoration, inst, inst, inst);
  2206. }
  2207. spv_result_t BuiltInsValidator::ValidateLayerOrViewportIndexAtReference(
  2208. const Decoration& decoration, const Instruction& built_in_inst,
  2209. const Instruction& referenced_inst,
  2210. const Instruction& referenced_from_inst) {
  2211. if (spvIsVulkanEnv(_.context()->target_env)) {
  2212. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2213. if (storage_class != SpvStorageClassMax &&
  2214. storage_class != SpvStorageClassInput &&
  2215. storage_class != SpvStorageClassOutput) {
  2216. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2217. << "Vulkan spec allows BuiltIn "
  2218. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2219. decoration.params()[0])
  2220. << " to be only used for variables with Input or Output storage "
  2221. "class. "
  2222. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2223. referenced_from_inst)
  2224. << " " << GetStorageClassDesc(referenced_from_inst);
  2225. }
  2226. if (storage_class == SpvStorageClassInput) {
  2227. assert(function_id_ == 0);
  2228. for (const auto em :
  2229. {SpvExecutionModelVertex, SpvExecutionModelTessellationEvaluation,
  2230. SpvExecutionModelGeometry}) {
  2231. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  2232. std::bind(&BuiltInsValidator::ValidateNotCalledWithExecutionModel,
  2233. this,
  2234. "Vulkan spec doesn't allow BuiltIn Layer and "
  2235. "ViewportIndex to be "
  2236. "used for variables with Input storage class if "
  2237. "execution model is Vertex, TessellationEvaluation, or "
  2238. "Geometry.",
  2239. em, decoration, built_in_inst, referenced_from_inst,
  2240. std::placeholders::_1));
  2241. }
  2242. }
  2243. if (storage_class == SpvStorageClassOutput) {
  2244. assert(function_id_ == 0);
  2245. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2246. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
  2247. "Vulkan spec doesn't allow BuiltIn Layer and "
  2248. "ViewportIndex to be "
  2249. "used for variables with Output storage class if "
  2250. "execution model is "
  2251. "Fragment.",
  2252. SpvExecutionModelFragment, decoration, built_in_inst,
  2253. referenced_from_inst, std::placeholders::_1));
  2254. }
  2255. for (const SpvExecutionModel execution_model : execution_models_) {
  2256. switch (execution_model) {
  2257. case SpvExecutionModelGeometry:
  2258. case SpvExecutionModelFragment:
  2259. case SpvExecutionModelMeshNV:
  2260. // Ok.
  2261. break;
  2262. case SpvExecutionModelVertex:
  2263. case SpvExecutionModelTessellationEvaluation: {
  2264. if (!_.HasCapability(SpvCapabilityShaderViewportIndexLayerEXT)) {
  2265. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2266. << "Using BuiltIn "
  2267. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2268. decoration.params()[0])
  2269. << " in Vertex or Tessellation execution model requires "
  2270. "the ShaderViewportIndexLayerEXT capability.";
  2271. }
  2272. break;
  2273. }
  2274. default: {
  2275. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2276. << "Vulkan spec allows BuiltIn "
  2277. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2278. decoration.params()[0])
  2279. << " to be used only with Vertex, TessellationEvaluation, "
  2280. "Geometry, or Fragment execution models. "
  2281. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2282. referenced_from_inst, execution_model);
  2283. }
  2284. }
  2285. }
  2286. }
  2287. if (function_id_ == 0) {
  2288. // Propagate this rule to all dependant ids in the global scope.
  2289. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  2290. std::bind(&BuiltInsValidator::ValidateLayerOrViewportIndexAtReference,
  2291. this, decoration, built_in_inst, referenced_from_inst,
  2292. std::placeholders::_1));
  2293. }
  2294. return SPV_SUCCESS;
  2295. }
  2296. spv_result_t BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtDefinition(
  2297. const Decoration& decoration, const Instruction& inst) {
  2298. if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
  2299. if (spv_result_t error = ValidateI32Vec(
  2300. decoration, inst, 3,
  2301. [this, &decoration,
  2302. &inst](const std::string& message) -> spv_result_t {
  2303. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2304. << "According to the "
  2305. << spvLogStringForEnv(_.context()->target_env)
  2306. << " spec BuiltIn "
  2307. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2308. decoration.params()[0])
  2309. << " variable needs to be a 3-component 32-bit int "
  2310. "vector. "
  2311. << message;
  2312. })) {
  2313. return error;
  2314. }
  2315. }
  2316. // Seed at reference checks with this built-in.
  2317. return ValidateComputeShaderI32Vec3InputAtReference(decoration, inst, inst,
  2318. inst);
  2319. }
  2320. spv_result_t BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtReference(
  2321. const Decoration& decoration, const Instruction& built_in_inst,
  2322. const Instruction& referenced_inst,
  2323. const Instruction& referenced_from_inst) {
  2324. if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
  2325. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2326. if (storage_class != SpvStorageClassMax &&
  2327. storage_class != SpvStorageClassInput) {
  2328. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2329. << spvLogStringForEnv(_.context()->target_env)
  2330. << " spec allows BuiltIn "
  2331. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2332. decoration.params()[0])
  2333. << " to be only used for variables with Input storage class. "
  2334. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2335. referenced_from_inst)
  2336. << " " << GetStorageClassDesc(referenced_from_inst);
  2337. }
  2338. for (const SpvExecutionModel execution_model : execution_models_) {
  2339. bool has_vulkan_model = execution_model == SpvExecutionModelGLCompute ||
  2340. execution_model == SpvExecutionModelTaskNV ||
  2341. execution_model == SpvExecutionModelMeshNV;
  2342. bool has_webgpu_model = execution_model == SpvExecutionModelGLCompute;
  2343. if ((spvIsVulkanEnv(_.context()->target_env) && !has_vulkan_model) ||
  2344. (spvIsWebGPUEnv(_.context()->target_env) && !has_webgpu_model)) {
  2345. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2346. << spvLogStringForEnv(_.context()->target_env)
  2347. << " spec allows BuiltIn "
  2348. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2349. decoration.params()[0])
  2350. << " to be used only with GLCompute execution model. "
  2351. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2352. referenced_from_inst, execution_model);
  2353. }
  2354. }
  2355. }
  2356. if (function_id_ == 0) {
  2357. // Propagate this rule to all dependant ids in the global scope.
  2358. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2359. &BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtReference, this,
  2360. decoration, built_in_inst, referenced_from_inst,
  2361. std::placeholders::_1));
  2362. }
  2363. return SPV_SUCCESS;
  2364. }
  2365. spv_result_t BuiltInsValidator::ValidateComputeI32InputAtDefinition(
  2366. const Decoration& decoration, const Instruction& inst) {
  2367. if (spvIsVulkanEnv(_.context()->target_env)) {
  2368. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  2369. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2370. << "BuiltIn "
  2371. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2372. decoration.params()[0])
  2373. << " cannot be used as a member decoration ";
  2374. }
  2375. if (spv_result_t error = ValidateI32(
  2376. decoration, inst,
  2377. [this, &decoration,
  2378. &inst](const std::string& message) -> spv_result_t {
  2379. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2380. << "According to the "
  2381. << spvLogStringForEnv(_.context()->target_env)
  2382. << " spec BuiltIn "
  2383. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2384. decoration.params()[0])
  2385. << " variable needs to be a 32-bit int "
  2386. "vector. "
  2387. << message;
  2388. })) {
  2389. return error;
  2390. }
  2391. }
  2392. // Seed at reference checks with this built-in.
  2393. return ValidateComputeI32InputAtReference(decoration, inst, inst, inst);
  2394. }
  2395. spv_result_t BuiltInsValidator::ValidateComputeI32InputAtReference(
  2396. const Decoration& decoration, const Instruction& built_in_inst,
  2397. const Instruction& referenced_inst,
  2398. const Instruction& referenced_from_inst) {
  2399. if (spvIsVulkanEnv(_.context()->target_env)) {
  2400. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2401. if (storage_class != SpvStorageClassMax &&
  2402. storage_class != SpvStorageClassInput) {
  2403. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2404. << spvLogStringForEnv(_.context()->target_env)
  2405. << " spec allows BuiltIn "
  2406. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2407. decoration.params()[0])
  2408. << " to be only used for variables with Input storage class. "
  2409. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2410. referenced_from_inst)
  2411. << " " << GetStorageClassDesc(referenced_from_inst);
  2412. }
  2413. for (const SpvExecutionModel execution_model : execution_models_) {
  2414. bool has_vulkan_model = execution_model == SpvExecutionModelGLCompute ||
  2415. execution_model == SpvExecutionModelTaskNV ||
  2416. execution_model == SpvExecutionModelMeshNV;
  2417. if (spvIsVulkanEnv(_.context()->target_env) && !has_vulkan_model) {
  2418. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2419. << spvLogStringForEnv(_.context()->target_env)
  2420. << " spec allows BuiltIn "
  2421. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2422. decoration.params()[0])
  2423. << " to be used only with GLCompute execution model. "
  2424. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2425. referenced_from_inst, execution_model);
  2426. }
  2427. }
  2428. }
  2429. if (function_id_ == 0) {
  2430. // Propagate this rule to all dependant ids in the global scope.
  2431. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  2432. std::bind(&BuiltInsValidator::ValidateComputeI32InputAtReference, this,
  2433. decoration, built_in_inst, referenced_from_inst,
  2434. std::placeholders::_1));
  2435. }
  2436. return SPV_SUCCESS;
  2437. }
  2438. spv_result_t BuiltInsValidator::ValidateI32InputAtDefinition(
  2439. const Decoration& decoration, const Instruction& inst) {
  2440. if (spvIsVulkanEnv(_.context()->target_env)) {
  2441. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  2442. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2443. << "BuiltIn "
  2444. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2445. decoration.params()[0])
  2446. << " cannot be used as a member decoration ";
  2447. }
  2448. if (spv_result_t error = ValidateI32(
  2449. decoration, inst,
  2450. [this, &decoration,
  2451. &inst](const std::string& message) -> spv_result_t {
  2452. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2453. << "According to the "
  2454. << spvLogStringForEnv(_.context()->target_env)
  2455. << " spec BuiltIn "
  2456. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2457. decoration.params()[0])
  2458. << " variable needs to be a 32-bit int. " << message;
  2459. })) {
  2460. return error;
  2461. }
  2462. const SpvStorageClass storage_class = GetStorageClass(inst);
  2463. if (storage_class != SpvStorageClassMax &&
  2464. storage_class != SpvStorageClassInput) {
  2465. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2466. << spvLogStringForEnv(_.context()->target_env)
  2467. << " spec allows BuiltIn "
  2468. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2469. decoration.params()[0])
  2470. << " to be only used for variables with Input storage class. "
  2471. << GetReferenceDesc(decoration, inst, inst, inst) << " "
  2472. << GetStorageClassDesc(inst);
  2473. }
  2474. }
  2475. return SPV_SUCCESS;
  2476. }
  2477. spv_result_t BuiltInsValidator::ValidateI32Vec4InputAtDefinition(
  2478. const Decoration& decoration, const Instruction& inst) {
  2479. if (spvIsVulkanEnv(_.context()->target_env)) {
  2480. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  2481. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2482. << "BuiltIn "
  2483. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2484. decoration.params()[0])
  2485. << " cannot be used as a member decoration ";
  2486. }
  2487. if (spv_result_t error = ValidateI32Vec(
  2488. decoration, inst, 4,
  2489. [this, &decoration,
  2490. &inst](const std::string& message) -> spv_result_t {
  2491. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2492. << "According to the "
  2493. << spvLogStringForEnv(_.context()->target_env)
  2494. << " spec BuiltIn "
  2495. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2496. decoration.params()[0])
  2497. << " variable needs to be a 4-component 32-bit int "
  2498. "vector. "
  2499. << message;
  2500. })) {
  2501. return error;
  2502. }
  2503. const SpvStorageClass storage_class = GetStorageClass(inst);
  2504. if (storage_class != SpvStorageClassMax &&
  2505. storage_class != SpvStorageClassInput) {
  2506. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2507. << spvLogStringForEnv(_.context()->target_env)
  2508. << " spec allows BuiltIn "
  2509. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2510. decoration.params()[0])
  2511. << " to be only used for variables with Input storage class. "
  2512. << GetReferenceDesc(decoration, inst, inst, inst) << " "
  2513. << GetStorageClassDesc(inst);
  2514. }
  2515. }
  2516. return SPV_SUCCESS;
  2517. }
  2518. spv_result_t BuiltInsValidator::ValidateWorkgroupSizeAtDefinition(
  2519. const Decoration& decoration, const Instruction& inst) {
  2520. if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
  2521. if (spvIsVulkanEnv(_.context()->target_env) &&
  2522. !spvOpcodeIsConstant(inst.opcode())) {
  2523. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2524. << "Vulkan spec requires BuiltIn WorkgroupSize to be a "
  2525. "constant. "
  2526. << GetIdDesc(inst) << " is not a constant.";
  2527. }
  2528. if (spv_result_t error = ValidateI32Vec(
  2529. decoration, inst, 3,
  2530. [this, &inst](const std::string& message) -> spv_result_t {
  2531. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2532. << "According to the "
  2533. << spvLogStringForEnv(_.context()->target_env)
  2534. << " spec BuiltIn WorkgroupSize variable needs to be a "
  2535. "3-component 32-bit int vector. "
  2536. << message;
  2537. })) {
  2538. return error;
  2539. }
  2540. }
  2541. // Seed at reference checks with this built-in.
  2542. return ValidateWorkgroupSizeAtReference(decoration, inst, inst, inst);
  2543. }
  2544. spv_result_t BuiltInsValidator::ValidateWorkgroupSizeAtReference(
  2545. const Decoration& decoration, const Instruction& built_in_inst,
  2546. const Instruction& referenced_inst,
  2547. const Instruction& referenced_from_inst) {
  2548. if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
  2549. for (const SpvExecutionModel execution_model : execution_models_) {
  2550. if (execution_model != SpvExecutionModelGLCompute) {
  2551. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2552. << spvLogStringForEnv(_.context()->target_env)
  2553. << " spec allows BuiltIn "
  2554. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2555. decoration.params()[0])
  2556. << " to be used only with GLCompute execution model. "
  2557. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2558. referenced_from_inst, execution_model);
  2559. }
  2560. }
  2561. }
  2562. if (function_id_ == 0) {
  2563. // Propagate this rule to all dependant ids in the global scope.
  2564. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2565. &BuiltInsValidator::ValidateWorkgroupSizeAtReference, this, decoration,
  2566. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2567. }
  2568. return SPV_SUCCESS;
  2569. }
  2570. spv_result_t BuiltInsValidator::ValidateSMBuiltinsAtDefinition(
  2571. const Decoration& decoration, const Instruction& inst) {
  2572. if (spvIsVulkanEnv(_.context()->target_env)) {
  2573. if (spv_result_t error = ValidateI32(
  2574. decoration, inst,
  2575. [this, &inst,
  2576. &decoration](const std::string& message) -> spv_result_t {
  2577. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2578. << "According to the "
  2579. << spvLogStringForEnv(_.context()->target_env)
  2580. << " spec BuiltIn "
  2581. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2582. decoration.params()[0])
  2583. << " variable needs to be a 32-bit int scalar. "
  2584. << message;
  2585. })) {
  2586. return error;
  2587. }
  2588. }
  2589. // Seed at reference checks with this built-in.
  2590. return ValidateSMBuiltinsAtReference(decoration, inst, inst, inst);
  2591. }
  2592. spv_result_t BuiltInsValidator::ValidateSMBuiltinsAtReference(
  2593. const Decoration& decoration, const Instruction& built_in_inst,
  2594. const Instruction& referenced_inst,
  2595. const Instruction& referenced_from_inst) {
  2596. if (spvIsVulkanEnv(_.context()->target_env)) {
  2597. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2598. if (storage_class != SpvStorageClassMax &&
  2599. storage_class != SpvStorageClassInput) {
  2600. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2601. << spvLogStringForEnv(_.context()->target_env)
  2602. << " spec allows BuiltIn "
  2603. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2604. decoration.params()[0])
  2605. << " to be only used for "
  2606. "variables with Input storage class. "
  2607. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2608. referenced_from_inst)
  2609. << " " << GetStorageClassDesc(referenced_from_inst);
  2610. }
  2611. }
  2612. if (function_id_ == 0) {
  2613. // Propagate this rule to all dependant ids in the global scope.
  2614. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2615. &BuiltInsValidator::ValidateSMBuiltinsAtReference, this, decoration,
  2616. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2617. }
  2618. return SPV_SUCCESS;
  2619. }
  2620. spv_result_t BuiltInsValidator::ValidateSingleBuiltInAtDefinition(
  2621. const Decoration& decoration, const Instruction& inst) {
  2622. const SpvBuiltIn label = SpvBuiltIn(decoration.params()[0]);
  2623. // Builtins can only be applied to variables, structures or constants.
  2624. auto target_opcode = inst.opcode();
  2625. if (target_opcode != SpvOpTypeStruct && target_opcode != SpvOpVariable &&
  2626. !spvOpcodeIsConstant(target_opcode)) {
  2627. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2628. << "BuiltIns can only target variables, structs or constants";
  2629. }
  2630. if (!spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
  2631. // Early return. All currently implemented rules are based on Vulkan or
  2632. // WebGPU spec.
  2633. //
  2634. // TODO: If you are adding validation rules for environments other than
  2635. // Vulkan or WebGPU (or general rules which are not environment
  2636. // independent), then you need to modify or remove this condition. Consider
  2637. // also adding early returns into BuiltIn-specific rules, so that the system
  2638. // doesn't spawn new rules which don't do anything.
  2639. return SPV_SUCCESS;
  2640. }
  2641. if (spvIsWebGPUEnv(_.context()->target_env) &&
  2642. !IsBuiltInValidForWebGPU(label)) {
  2643. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2644. << "WebGPU does not allow BuiltIn "
  2645. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2646. decoration.params()[0]);
  2647. }
  2648. // If you are adding a new BuiltIn enum, please register it here.
  2649. // If the newly added enum has validation rules associated with it
  2650. // consider leaving a TODO and/or creating an issue.
  2651. switch (label) {
  2652. case SpvBuiltInClipDistance:
  2653. case SpvBuiltInCullDistance: {
  2654. return ValidateClipOrCullDistanceAtDefinition(decoration, inst);
  2655. }
  2656. case SpvBuiltInFragCoord: {
  2657. return ValidateFragCoordAtDefinition(decoration, inst);
  2658. }
  2659. case SpvBuiltInFragDepth: {
  2660. return ValidateFragDepthAtDefinition(decoration, inst);
  2661. }
  2662. case SpvBuiltInFrontFacing: {
  2663. return ValidateFrontFacingAtDefinition(decoration, inst);
  2664. }
  2665. case SpvBuiltInGlobalInvocationId:
  2666. case SpvBuiltInLocalInvocationId:
  2667. case SpvBuiltInNumWorkgroups:
  2668. case SpvBuiltInWorkgroupId: {
  2669. return ValidateComputeShaderI32Vec3InputAtDefinition(decoration, inst);
  2670. }
  2671. case SpvBuiltInHelperInvocation: {
  2672. return ValidateHelperInvocationAtDefinition(decoration, inst);
  2673. }
  2674. case SpvBuiltInInvocationId: {
  2675. return ValidateInvocationIdAtDefinition(decoration, inst);
  2676. }
  2677. case SpvBuiltInInstanceIndex: {
  2678. return ValidateInstanceIndexAtDefinition(decoration, inst);
  2679. }
  2680. case SpvBuiltInLayer:
  2681. case SpvBuiltInViewportIndex: {
  2682. return ValidateLayerOrViewportIndexAtDefinition(decoration, inst);
  2683. }
  2684. case SpvBuiltInPatchVertices: {
  2685. return ValidatePatchVerticesAtDefinition(decoration, inst);
  2686. }
  2687. case SpvBuiltInPointCoord: {
  2688. return ValidatePointCoordAtDefinition(decoration, inst);
  2689. }
  2690. case SpvBuiltInPointSize: {
  2691. return ValidatePointSizeAtDefinition(decoration, inst);
  2692. }
  2693. case SpvBuiltInPosition: {
  2694. return ValidatePositionAtDefinition(decoration, inst);
  2695. }
  2696. case SpvBuiltInPrimitiveId: {
  2697. return ValidatePrimitiveIdAtDefinition(decoration, inst);
  2698. }
  2699. case SpvBuiltInSampleId: {
  2700. return ValidateSampleIdAtDefinition(decoration, inst);
  2701. }
  2702. case SpvBuiltInSampleMask: {
  2703. return ValidateSampleMaskAtDefinition(decoration, inst);
  2704. }
  2705. case SpvBuiltInSamplePosition: {
  2706. return ValidateSamplePositionAtDefinition(decoration, inst);
  2707. }
  2708. case SpvBuiltInSubgroupId:
  2709. case SpvBuiltInNumSubgroups: {
  2710. return ValidateComputeI32InputAtDefinition(decoration, inst);
  2711. }
  2712. case SpvBuiltInSubgroupLocalInvocationId:
  2713. case SpvBuiltInSubgroupSize: {
  2714. return ValidateI32InputAtDefinition(decoration, inst);
  2715. }
  2716. case SpvBuiltInSubgroupEqMask:
  2717. case SpvBuiltInSubgroupGeMask:
  2718. case SpvBuiltInSubgroupGtMask:
  2719. case SpvBuiltInSubgroupLeMask:
  2720. case SpvBuiltInSubgroupLtMask: {
  2721. return ValidateI32Vec4InputAtDefinition(decoration, inst);
  2722. }
  2723. case SpvBuiltInTessCoord: {
  2724. return ValidateTessCoordAtDefinition(decoration, inst);
  2725. }
  2726. case SpvBuiltInTessLevelOuter: {
  2727. return ValidateTessLevelOuterAtDefinition(decoration, inst);
  2728. }
  2729. case SpvBuiltInTessLevelInner: {
  2730. return ValidateTessLevelInnerAtDefinition(decoration, inst);
  2731. }
  2732. case SpvBuiltInVertexIndex: {
  2733. return ValidateVertexIndexAtDefinition(decoration, inst);
  2734. }
  2735. case SpvBuiltInWorkgroupSize: {
  2736. return ValidateWorkgroupSizeAtDefinition(decoration, inst);
  2737. }
  2738. case SpvBuiltInVertexId:
  2739. case SpvBuiltInInstanceId: {
  2740. return ValidateVertexIdOrInstanceIdAtDefinition(decoration, inst);
  2741. }
  2742. case SpvBuiltInLocalInvocationIndex: {
  2743. return ValidateLocalInvocationIndexAtDefinition(decoration, inst);
  2744. }
  2745. case SpvBuiltInWarpsPerSMNV:
  2746. case SpvBuiltInSMCountNV:
  2747. case SpvBuiltInWarpIDNV:
  2748. case SpvBuiltInSMIDNV: {
  2749. return ValidateSMBuiltinsAtDefinition(decoration, inst);
  2750. }
  2751. case SpvBuiltInWorkDim:
  2752. case SpvBuiltInGlobalSize:
  2753. case SpvBuiltInEnqueuedWorkgroupSize:
  2754. case SpvBuiltInGlobalOffset:
  2755. case SpvBuiltInGlobalLinearId:
  2756. case SpvBuiltInSubgroupMaxSize:
  2757. case SpvBuiltInNumEnqueuedSubgroups:
  2758. case SpvBuiltInBaseVertex:
  2759. case SpvBuiltInBaseInstance:
  2760. case SpvBuiltInDrawIndex:
  2761. case SpvBuiltInDeviceIndex:
  2762. case SpvBuiltInViewIndex:
  2763. case SpvBuiltInBaryCoordNoPerspAMD:
  2764. case SpvBuiltInBaryCoordNoPerspCentroidAMD:
  2765. case SpvBuiltInBaryCoordNoPerspSampleAMD:
  2766. case SpvBuiltInBaryCoordSmoothAMD:
  2767. case SpvBuiltInBaryCoordSmoothCentroidAMD:
  2768. case SpvBuiltInBaryCoordSmoothSampleAMD:
  2769. case SpvBuiltInBaryCoordPullModelAMD:
  2770. case SpvBuiltInFragStencilRefEXT:
  2771. case SpvBuiltInViewportMaskNV:
  2772. case SpvBuiltInSecondaryPositionNV:
  2773. case SpvBuiltInSecondaryViewportMaskNV:
  2774. case SpvBuiltInPositionPerViewNV:
  2775. case SpvBuiltInViewportMaskPerViewNV:
  2776. case SpvBuiltInFullyCoveredEXT:
  2777. case SpvBuiltInMax:
  2778. case SpvBuiltInTaskCountNV:
  2779. case SpvBuiltInPrimitiveCountNV:
  2780. case SpvBuiltInPrimitiveIndicesNV:
  2781. case SpvBuiltInClipDistancePerViewNV:
  2782. case SpvBuiltInCullDistancePerViewNV:
  2783. case SpvBuiltInLayerPerViewNV:
  2784. case SpvBuiltInMeshViewCountNV:
  2785. case SpvBuiltInMeshViewIndicesNV:
  2786. case SpvBuiltInBaryCoordNV:
  2787. case SpvBuiltInBaryCoordNoPerspNV:
  2788. case SpvBuiltInFragmentSizeNV: // alias SpvBuiltInFragSizeEXT
  2789. case SpvBuiltInInvocationsPerPixelNV: // alias
  2790. // SpvBuiltInFragInvocationCountEXT
  2791. case SpvBuiltInLaunchIdNV:
  2792. case SpvBuiltInLaunchSizeNV:
  2793. case SpvBuiltInWorldRayOriginNV:
  2794. case SpvBuiltInWorldRayDirectionNV:
  2795. case SpvBuiltInObjectRayOriginNV:
  2796. case SpvBuiltInObjectRayDirectionNV:
  2797. case SpvBuiltInRayTminNV:
  2798. case SpvBuiltInRayTmaxNV:
  2799. case SpvBuiltInInstanceCustomIndexNV:
  2800. case SpvBuiltInObjectToWorldNV:
  2801. case SpvBuiltInWorldToObjectNV:
  2802. case SpvBuiltInHitTNV:
  2803. case SpvBuiltInHitKindNV:
  2804. case SpvBuiltInIncomingRayFlagsNV: {
  2805. // No validation rules (for the moment).
  2806. break;
  2807. }
  2808. }
  2809. return SPV_SUCCESS;
  2810. }
  2811. spv_result_t BuiltInsValidator::ValidateBuiltInsAtDefinition() {
  2812. for (const auto& kv : _.id_decorations()) {
  2813. const uint32_t id = kv.first;
  2814. const auto& decorations = kv.second;
  2815. if (decorations.empty()) {
  2816. continue;
  2817. }
  2818. const Instruction* inst = _.FindDef(id);
  2819. assert(inst);
  2820. for (const auto& decoration : kv.second) {
  2821. if (decoration.dec_type() != SpvDecorationBuiltIn) {
  2822. continue;
  2823. }
  2824. if (spv_result_t error =
  2825. ValidateSingleBuiltInAtDefinition(decoration, *inst)) {
  2826. return error;
  2827. }
  2828. }
  2829. }
  2830. return SPV_SUCCESS;
  2831. }
  2832. spv_result_t BuiltInsValidator::Run() {
  2833. // First pass: validate all built-ins at definition and seed
  2834. // id_to_at_reference_checks_ with built-ins.
  2835. if (auto error = ValidateBuiltInsAtDefinition()) {
  2836. return error;
  2837. }
  2838. if (id_to_at_reference_checks_.empty()) {
  2839. // No validation tasks were seeded. Nothing else to do.
  2840. return SPV_SUCCESS;
  2841. }
  2842. // Second pass: validate every id reference in the module using
  2843. // rules in id_to_at_reference_checks_.
  2844. for (const Instruction& inst : _.ordered_instructions()) {
  2845. Update(inst);
  2846. std::set<uint32_t> already_checked;
  2847. for (const auto& operand : inst.operands()) {
  2848. if (!spvIsIdType(operand.type)) {
  2849. // Not id.
  2850. continue;
  2851. }
  2852. const uint32_t id = inst.word(operand.offset);
  2853. if (id == inst.id()) {
  2854. // No need to check result id.
  2855. continue;
  2856. }
  2857. if (!already_checked.insert(id).second) {
  2858. // The instruction has already referenced this id.
  2859. continue;
  2860. }
  2861. // Instruction references the id. Run all checks associated with the id
  2862. // on the instruction. id_to_at_reference_checks_ can be modified in the
  2863. // process, iterators are safe because it's a tree-based map.
  2864. const auto it = id_to_at_reference_checks_.find(id);
  2865. if (it != id_to_at_reference_checks_.end()) {
  2866. for (const auto& check : it->second) {
  2867. if (spv_result_t error = check(inst)) {
  2868. return error;
  2869. }
  2870. }
  2871. }
  2872. }
  2873. }
  2874. return SPV_SUCCESS;
  2875. }
  2876. } // namespace
  2877. // Validates correctness of built-in variables.
  2878. spv_result_t ValidateBuiltIns(ValidationState_t& _) {
  2879. BuiltInsValidator validator(_);
  2880. return validator.Run();
  2881. }
  2882. } // namespace val
  2883. } // namespace spvtools