2
0

validate_builtins.cpp 133 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188
  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 =
  2039. (_.HasCapability(SpvCapabilityRayTracingNV) ||
  2040. _.HasCapability(SpvCapabilityRayTracingProvisionalKHR)) &&
  2041. label == SpvBuiltInInstanceId;
  2042. if (spvIsVulkanEnv(_.context()->target_env) && !allow_instance_id) {
  2043. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2044. << "Vulkan spec doesn't allow BuiltIn VertexId/InstanceId "
  2045. "to be used.";
  2046. }
  2047. if (label == SpvBuiltInInstanceId) {
  2048. return ValidateInstanceIdAtReference(decoration, inst, inst, inst);
  2049. }
  2050. return SPV_SUCCESS;
  2051. }
  2052. spv_result_t BuiltInsValidator::ValidateInstanceIdAtReference(
  2053. const Decoration& decoration, const Instruction& built_in_inst,
  2054. const Instruction& referenced_inst,
  2055. const Instruction& referenced_from_inst) {
  2056. if (spvIsVulkanEnv(_.context()->target_env)) {
  2057. for (const SpvExecutionModel execution_model : execution_models_) {
  2058. switch (execution_model) {
  2059. case SpvExecutionModelIntersectionNV:
  2060. case SpvExecutionModelClosestHitNV:
  2061. case SpvExecutionModelAnyHitNV:
  2062. // Do nothing, valid stages
  2063. break;
  2064. default:
  2065. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2066. << "Vulkan spec allows BuiltIn InstanceId to be used "
  2067. "only with IntersectionNV, ClosestHitNV and AnyHitNV "
  2068. "execution models. "
  2069. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2070. referenced_from_inst);
  2071. break;
  2072. }
  2073. }
  2074. }
  2075. if (function_id_ == 0) {
  2076. // Propagate this rule to all dependant ids in the global scope.
  2077. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2078. &BuiltInsValidator::ValidateInstanceIdAtReference, this, decoration,
  2079. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2080. }
  2081. return SPV_SUCCESS;
  2082. }
  2083. spv_result_t BuiltInsValidator::ValidateLocalInvocationIndexAtDefinition(
  2084. const Decoration& decoration, const Instruction& inst) {
  2085. if (spvIsWebGPUEnv(_.context()->target_env)) {
  2086. if (spv_result_t error = ValidateI32(
  2087. decoration, inst,
  2088. [this, &inst](const std::string& message) -> spv_result_t {
  2089. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2090. << "According to the WebGPU spec BuiltIn "
  2091. "LocalInvocationIndex variable needs to be a 32-bit "
  2092. "int."
  2093. << message;
  2094. })) {
  2095. return error;
  2096. }
  2097. }
  2098. // Seed at reference checks with this built-in.
  2099. return ValidateLocalInvocationIndexAtReference(decoration, inst, inst, inst);
  2100. }
  2101. spv_result_t BuiltInsValidator::ValidateLocalInvocationIndexAtReference(
  2102. const Decoration& decoration, const Instruction& built_in_inst,
  2103. const Instruction& referenced_inst,
  2104. const Instruction& referenced_from_inst) {
  2105. if (spvIsWebGPUEnv(_.context()->target_env)) {
  2106. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2107. if (storage_class != SpvStorageClassMax &&
  2108. storage_class != SpvStorageClassInput) {
  2109. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2110. << "WebGPU spec allows BuiltIn LocalInvocationIndex to be only "
  2111. "used for variables with Input storage class. "
  2112. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2113. referenced_from_inst)
  2114. << " " << GetStorageClassDesc(referenced_from_inst);
  2115. }
  2116. for (const SpvExecutionModel execution_model : execution_models_) {
  2117. if (execution_model != SpvExecutionModelGLCompute) {
  2118. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2119. << "WebGPU spec allows BuiltIn VertexIndex to be used only "
  2120. "with GLCompute execution model. "
  2121. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2122. referenced_from_inst, execution_model);
  2123. }
  2124. }
  2125. }
  2126. if (function_id_ == 0) {
  2127. // Propagate this rule to all dependant ids in the global scope.
  2128. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  2129. std::bind(&BuiltInsValidator::ValidateLocalInvocationIndexAtReference,
  2130. this, decoration, built_in_inst, referenced_from_inst,
  2131. std::placeholders::_1));
  2132. }
  2133. return SPV_SUCCESS;
  2134. }
  2135. spv_result_t BuiltInsValidator::ValidateVertexIndexAtReference(
  2136. const Decoration& decoration, const Instruction& built_in_inst,
  2137. const Instruction& referenced_inst,
  2138. const Instruction& referenced_from_inst) {
  2139. if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
  2140. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2141. if (storage_class != SpvStorageClassMax &&
  2142. storage_class != SpvStorageClassInput) {
  2143. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2144. << spvLogStringForEnv(_.context()->target_env)
  2145. << " spec allows BuiltIn VertexIndex to be only used for "
  2146. "variables with Input storage class. "
  2147. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2148. referenced_from_inst)
  2149. << " " << GetStorageClassDesc(referenced_from_inst);
  2150. }
  2151. for (const SpvExecutionModel execution_model : execution_models_) {
  2152. if (execution_model != SpvExecutionModelVertex) {
  2153. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2154. << spvLogStringForEnv(_.context()->target_env)
  2155. << " spec allows BuiltIn VertexIndex to be used only with "
  2156. "Vertex execution model. "
  2157. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2158. referenced_from_inst, execution_model);
  2159. }
  2160. }
  2161. }
  2162. if (function_id_ == 0) {
  2163. // Propagate this rule to all dependant ids in the global scope.
  2164. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2165. &BuiltInsValidator::ValidateVertexIndexAtReference, this, decoration,
  2166. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2167. }
  2168. return SPV_SUCCESS;
  2169. }
  2170. spv_result_t BuiltInsValidator::ValidateLayerOrViewportIndexAtDefinition(
  2171. const Decoration& decoration, const Instruction& inst) {
  2172. if (spvIsVulkanEnv(_.context()->target_env)) {
  2173. // This can be a per-primitive variable for mesh shader stage.
  2174. // In such cases variable will have an array of 32-bit integers.
  2175. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  2176. // This must be a 32-bit int scalar.
  2177. if (spv_result_t error = ValidateI32(
  2178. decoration, inst,
  2179. [this, &decoration,
  2180. &inst](const std::string& message) -> spv_result_t {
  2181. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2182. << "According to the Vulkan spec BuiltIn "
  2183. << _.grammar().lookupOperandName(
  2184. SPV_OPERAND_TYPE_BUILT_IN, decoration.params()[0])
  2185. << "variable needs to be a 32-bit int scalar. "
  2186. << message;
  2187. })) {
  2188. return error;
  2189. }
  2190. } else {
  2191. if (spv_result_t error = ValidateOptionalArrayedI32(
  2192. decoration, inst,
  2193. [this, &decoration,
  2194. &inst](const std::string& message) -> spv_result_t {
  2195. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2196. << "According to the Vulkan spec BuiltIn "
  2197. << _.grammar().lookupOperandName(
  2198. SPV_OPERAND_TYPE_BUILT_IN, decoration.params()[0])
  2199. << "variable needs to be a 32-bit int scalar. "
  2200. << message;
  2201. })) {
  2202. return error;
  2203. }
  2204. }
  2205. }
  2206. // Seed at reference checks with this built-in.
  2207. return ValidateLayerOrViewportIndexAtReference(decoration, inst, inst, inst);
  2208. }
  2209. spv_result_t BuiltInsValidator::ValidateLayerOrViewportIndexAtReference(
  2210. const Decoration& decoration, const Instruction& built_in_inst,
  2211. const Instruction& referenced_inst,
  2212. const Instruction& referenced_from_inst) {
  2213. if (spvIsVulkanEnv(_.context()->target_env)) {
  2214. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2215. if (storage_class != SpvStorageClassMax &&
  2216. storage_class != SpvStorageClassInput &&
  2217. storage_class != SpvStorageClassOutput) {
  2218. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2219. << "Vulkan spec allows BuiltIn "
  2220. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2221. decoration.params()[0])
  2222. << " to be only used for variables with Input or Output storage "
  2223. "class. "
  2224. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2225. referenced_from_inst)
  2226. << " " << GetStorageClassDesc(referenced_from_inst);
  2227. }
  2228. if (storage_class == SpvStorageClassInput) {
  2229. assert(function_id_ == 0);
  2230. for (const auto em :
  2231. {SpvExecutionModelVertex, SpvExecutionModelTessellationEvaluation,
  2232. SpvExecutionModelGeometry}) {
  2233. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  2234. std::bind(&BuiltInsValidator::ValidateNotCalledWithExecutionModel,
  2235. this,
  2236. "Vulkan spec doesn't allow BuiltIn Layer and "
  2237. "ViewportIndex to be "
  2238. "used for variables with Input storage class if "
  2239. "execution model is Vertex, TessellationEvaluation, or "
  2240. "Geometry.",
  2241. em, decoration, built_in_inst, referenced_from_inst,
  2242. std::placeholders::_1));
  2243. }
  2244. }
  2245. if (storage_class == SpvStorageClassOutput) {
  2246. assert(function_id_ == 0);
  2247. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2248. &BuiltInsValidator::ValidateNotCalledWithExecutionModel, this,
  2249. "Vulkan spec doesn't allow BuiltIn Layer and "
  2250. "ViewportIndex to be "
  2251. "used for variables with Output storage class if "
  2252. "execution model is "
  2253. "Fragment.",
  2254. SpvExecutionModelFragment, decoration, built_in_inst,
  2255. referenced_from_inst, std::placeholders::_1));
  2256. }
  2257. for (const SpvExecutionModel execution_model : execution_models_) {
  2258. switch (execution_model) {
  2259. case SpvExecutionModelGeometry:
  2260. case SpvExecutionModelFragment:
  2261. case SpvExecutionModelMeshNV:
  2262. // Ok.
  2263. break;
  2264. case SpvExecutionModelVertex:
  2265. case SpvExecutionModelTessellationEvaluation: {
  2266. if (!_.HasCapability(SpvCapabilityShaderViewportIndexLayerEXT)) {
  2267. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2268. << "Using BuiltIn "
  2269. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2270. decoration.params()[0])
  2271. << " in Vertex or Tessellation execution model requires "
  2272. "the ShaderViewportIndexLayerEXT capability.";
  2273. }
  2274. break;
  2275. }
  2276. default: {
  2277. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2278. << "Vulkan spec allows BuiltIn "
  2279. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2280. decoration.params()[0])
  2281. << " to be used only with Vertex, TessellationEvaluation, "
  2282. "Geometry, or Fragment execution models. "
  2283. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2284. referenced_from_inst, execution_model);
  2285. }
  2286. }
  2287. }
  2288. }
  2289. if (function_id_ == 0) {
  2290. // Propagate this rule to all dependant ids in the global scope.
  2291. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  2292. std::bind(&BuiltInsValidator::ValidateLayerOrViewportIndexAtReference,
  2293. this, decoration, built_in_inst, referenced_from_inst,
  2294. std::placeholders::_1));
  2295. }
  2296. return SPV_SUCCESS;
  2297. }
  2298. spv_result_t BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtDefinition(
  2299. const Decoration& decoration, const Instruction& inst) {
  2300. if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
  2301. if (spv_result_t error = ValidateI32Vec(
  2302. decoration, inst, 3,
  2303. [this, &decoration,
  2304. &inst](const std::string& message) -> spv_result_t {
  2305. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2306. << "According to the "
  2307. << spvLogStringForEnv(_.context()->target_env)
  2308. << " spec BuiltIn "
  2309. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2310. decoration.params()[0])
  2311. << " variable needs to be a 3-component 32-bit int "
  2312. "vector. "
  2313. << message;
  2314. })) {
  2315. return error;
  2316. }
  2317. }
  2318. // Seed at reference checks with this built-in.
  2319. return ValidateComputeShaderI32Vec3InputAtReference(decoration, inst, inst,
  2320. inst);
  2321. }
  2322. spv_result_t BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtReference(
  2323. const Decoration& decoration, const Instruction& built_in_inst,
  2324. const Instruction& referenced_inst,
  2325. const Instruction& referenced_from_inst) {
  2326. if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
  2327. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2328. if (storage_class != SpvStorageClassMax &&
  2329. storage_class != SpvStorageClassInput) {
  2330. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2331. << spvLogStringForEnv(_.context()->target_env)
  2332. << " spec allows BuiltIn "
  2333. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2334. decoration.params()[0])
  2335. << " to be only used for variables with Input storage class. "
  2336. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2337. referenced_from_inst)
  2338. << " " << GetStorageClassDesc(referenced_from_inst);
  2339. }
  2340. for (const SpvExecutionModel execution_model : execution_models_) {
  2341. bool has_vulkan_model = execution_model == SpvExecutionModelGLCompute ||
  2342. execution_model == SpvExecutionModelTaskNV ||
  2343. execution_model == SpvExecutionModelMeshNV;
  2344. bool has_webgpu_model = execution_model == SpvExecutionModelGLCompute;
  2345. if ((spvIsVulkanEnv(_.context()->target_env) && !has_vulkan_model) ||
  2346. (spvIsWebGPUEnv(_.context()->target_env) && !has_webgpu_model)) {
  2347. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2348. << spvLogStringForEnv(_.context()->target_env)
  2349. << " spec allows BuiltIn "
  2350. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2351. decoration.params()[0])
  2352. << " to be used only with GLCompute execution model. "
  2353. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2354. referenced_from_inst, execution_model);
  2355. }
  2356. }
  2357. }
  2358. if (function_id_ == 0) {
  2359. // Propagate this rule to all dependant ids in the global scope.
  2360. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2361. &BuiltInsValidator::ValidateComputeShaderI32Vec3InputAtReference, this,
  2362. decoration, built_in_inst, referenced_from_inst,
  2363. std::placeholders::_1));
  2364. }
  2365. return SPV_SUCCESS;
  2366. }
  2367. spv_result_t BuiltInsValidator::ValidateComputeI32InputAtDefinition(
  2368. const Decoration& decoration, const Instruction& inst) {
  2369. if (spvIsVulkanEnv(_.context()->target_env)) {
  2370. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  2371. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2372. << "BuiltIn "
  2373. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2374. decoration.params()[0])
  2375. << " cannot be used as a member decoration ";
  2376. }
  2377. if (spv_result_t error = ValidateI32(
  2378. decoration, inst,
  2379. [this, &decoration,
  2380. &inst](const std::string& message) -> spv_result_t {
  2381. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2382. << "According to the "
  2383. << spvLogStringForEnv(_.context()->target_env)
  2384. << " spec BuiltIn "
  2385. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2386. decoration.params()[0])
  2387. << " variable needs to be a 32-bit int "
  2388. "vector. "
  2389. << message;
  2390. })) {
  2391. return error;
  2392. }
  2393. }
  2394. // Seed at reference checks with this built-in.
  2395. return ValidateComputeI32InputAtReference(decoration, inst, inst, inst);
  2396. }
  2397. spv_result_t BuiltInsValidator::ValidateComputeI32InputAtReference(
  2398. const Decoration& decoration, const Instruction& built_in_inst,
  2399. const Instruction& referenced_inst,
  2400. const Instruction& referenced_from_inst) {
  2401. if (spvIsVulkanEnv(_.context()->target_env)) {
  2402. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2403. if (storage_class != SpvStorageClassMax &&
  2404. storage_class != SpvStorageClassInput) {
  2405. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2406. << spvLogStringForEnv(_.context()->target_env)
  2407. << " spec allows BuiltIn "
  2408. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2409. decoration.params()[0])
  2410. << " to be only used for variables with Input storage class. "
  2411. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2412. referenced_from_inst)
  2413. << " " << GetStorageClassDesc(referenced_from_inst);
  2414. }
  2415. for (const SpvExecutionModel execution_model : execution_models_) {
  2416. bool has_vulkan_model = execution_model == SpvExecutionModelGLCompute ||
  2417. execution_model == SpvExecutionModelTaskNV ||
  2418. execution_model == SpvExecutionModelMeshNV;
  2419. if (spvIsVulkanEnv(_.context()->target_env) && !has_vulkan_model) {
  2420. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2421. << spvLogStringForEnv(_.context()->target_env)
  2422. << " spec allows BuiltIn "
  2423. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2424. decoration.params()[0])
  2425. << " to be used only with GLCompute execution model. "
  2426. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2427. referenced_from_inst, execution_model);
  2428. }
  2429. }
  2430. }
  2431. if (function_id_ == 0) {
  2432. // Propagate this rule to all dependant ids in the global scope.
  2433. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(
  2434. std::bind(&BuiltInsValidator::ValidateComputeI32InputAtReference, this,
  2435. decoration, built_in_inst, referenced_from_inst,
  2436. std::placeholders::_1));
  2437. }
  2438. return SPV_SUCCESS;
  2439. }
  2440. spv_result_t BuiltInsValidator::ValidateI32InputAtDefinition(
  2441. const Decoration& decoration, const Instruction& inst) {
  2442. if (spvIsVulkanEnv(_.context()->target_env)) {
  2443. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  2444. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2445. << "BuiltIn "
  2446. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2447. decoration.params()[0])
  2448. << " cannot be used as a member decoration ";
  2449. }
  2450. if (spv_result_t error = ValidateI32(
  2451. decoration, inst,
  2452. [this, &decoration,
  2453. &inst](const std::string& message) -> spv_result_t {
  2454. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2455. << "According to the "
  2456. << spvLogStringForEnv(_.context()->target_env)
  2457. << " spec BuiltIn "
  2458. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2459. decoration.params()[0])
  2460. << " variable needs to be a 32-bit int. " << message;
  2461. })) {
  2462. return error;
  2463. }
  2464. const SpvStorageClass storage_class = GetStorageClass(inst);
  2465. if (storage_class != SpvStorageClassMax &&
  2466. storage_class != SpvStorageClassInput) {
  2467. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2468. << spvLogStringForEnv(_.context()->target_env)
  2469. << " spec allows BuiltIn "
  2470. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2471. decoration.params()[0])
  2472. << " to be only used for variables with Input storage class. "
  2473. << GetReferenceDesc(decoration, inst, inst, inst) << " "
  2474. << GetStorageClassDesc(inst);
  2475. }
  2476. }
  2477. return SPV_SUCCESS;
  2478. }
  2479. spv_result_t BuiltInsValidator::ValidateI32Vec4InputAtDefinition(
  2480. const Decoration& decoration, const Instruction& inst) {
  2481. if (spvIsVulkanEnv(_.context()->target_env)) {
  2482. if (decoration.struct_member_index() != Decoration::kInvalidMember) {
  2483. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2484. << "BuiltIn "
  2485. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2486. decoration.params()[0])
  2487. << " cannot be used as a member decoration ";
  2488. }
  2489. if (spv_result_t error = ValidateI32Vec(
  2490. decoration, inst, 4,
  2491. [this, &decoration,
  2492. &inst](const std::string& message) -> spv_result_t {
  2493. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2494. << "According to the "
  2495. << spvLogStringForEnv(_.context()->target_env)
  2496. << " spec BuiltIn "
  2497. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2498. decoration.params()[0])
  2499. << " variable needs to be a 4-component 32-bit int "
  2500. "vector. "
  2501. << message;
  2502. })) {
  2503. return error;
  2504. }
  2505. const SpvStorageClass storage_class = GetStorageClass(inst);
  2506. if (storage_class != SpvStorageClassMax &&
  2507. storage_class != SpvStorageClassInput) {
  2508. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2509. << spvLogStringForEnv(_.context()->target_env)
  2510. << " spec allows BuiltIn "
  2511. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2512. decoration.params()[0])
  2513. << " to be only used for variables with Input storage class. "
  2514. << GetReferenceDesc(decoration, inst, inst, inst) << " "
  2515. << GetStorageClassDesc(inst);
  2516. }
  2517. }
  2518. return SPV_SUCCESS;
  2519. }
  2520. spv_result_t BuiltInsValidator::ValidateWorkgroupSizeAtDefinition(
  2521. const Decoration& decoration, const Instruction& inst) {
  2522. if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
  2523. if (spvIsVulkanEnv(_.context()->target_env) &&
  2524. !spvOpcodeIsConstant(inst.opcode())) {
  2525. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2526. << "Vulkan spec requires BuiltIn WorkgroupSize to be a "
  2527. "constant. "
  2528. << GetIdDesc(inst) << " is not a constant.";
  2529. }
  2530. if (spv_result_t error = ValidateI32Vec(
  2531. decoration, inst, 3,
  2532. [this, &inst](const std::string& message) -> spv_result_t {
  2533. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2534. << "According to the "
  2535. << spvLogStringForEnv(_.context()->target_env)
  2536. << " spec BuiltIn WorkgroupSize variable needs to be a "
  2537. "3-component 32-bit int vector. "
  2538. << message;
  2539. })) {
  2540. return error;
  2541. }
  2542. }
  2543. // Seed at reference checks with this built-in.
  2544. return ValidateWorkgroupSizeAtReference(decoration, inst, inst, inst);
  2545. }
  2546. spv_result_t BuiltInsValidator::ValidateWorkgroupSizeAtReference(
  2547. const Decoration& decoration, const Instruction& built_in_inst,
  2548. const Instruction& referenced_inst,
  2549. const Instruction& referenced_from_inst) {
  2550. if (spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
  2551. for (const SpvExecutionModel execution_model : execution_models_) {
  2552. if (execution_model != SpvExecutionModelGLCompute) {
  2553. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2554. << spvLogStringForEnv(_.context()->target_env)
  2555. << " spec allows BuiltIn "
  2556. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2557. decoration.params()[0])
  2558. << " to be used only with GLCompute execution model. "
  2559. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2560. referenced_from_inst, execution_model);
  2561. }
  2562. }
  2563. }
  2564. if (function_id_ == 0) {
  2565. // Propagate this rule to all dependant ids in the global scope.
  2566. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2567. &BuiltInsValidator::ValidateWorkgroupSizeAtReference, this, decoration,
  2568. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2569. }
  2570. return SPV_SUCCESS;
  2571. }
  2572. spv_result_t BuiltInsValidator::ValidateSMBuiltinsAtDefinition(
  2573. const Decoration& decoration, const Instruction& inst) {
  2574. if (spvIsVulkanEnv(_.context()->target_env)) {
  2575. if (spv_result_t error = ValidateI32(
  2576. decoration, inst,
  2577. [this, &inst,
  2578. &decoration](const std::string& message) -> spv_result_t {
  2579. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2580. << "According to the "
  2581. << spvLogStringForEnv(_.context()->target_env)
  2582. << " spec BuiltIn "
  2583. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2584. decoration.params()[0])
  2585. << " variable needs to be a 32-bit int scalar. "
  2586. << message;
  2587. })) {
  2588. return error;
  2589. }
  2590. }
  2591. // Seed at reference checks with this built-in.
  2592. return ValidateSMBuiltinsAtReference(decoration, inst, inst, inst);
  2593. }
  2594. spv_result_t BuiltInsValidator::ValidateSMBuiltinsAtReference(
  2595. const Decoration& decoration, const Instruction& built_in_inst,
  2596. const Instruction& referenced_inst,
  2597. const Instruction& referenced_from_inst) {
  2598. if (spvIsVulkanEnv(_.context()->target_env)) {
  2599. const SpvStorageClass storage_class = GetStorageClass(referenced_from_inst);
  2600. if (storage_class != SpvStorageClassMax &&
  2601. storage_class != SpvStorageClassInput) {
  2602. return _.diag(SPV_ERROR_INVALID_DATA, &referenced_from_inst)
  2603. << spvLogStringForEnv(_.context()->target_env)
  2604. << " spec allows BuiltIn "
  2605. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2606. decoration.params()[0])
  2607. << " to be only used for "
  2608. "variables with Input storage class. "
  2609. << GetReferenceDesc(decoration, built_in_inst, referenced_inst,
  2610. referenced_from_inst)
  2611. << " " << GetStorageClassDesc(referenced_from_inst);
  2612. }
  2613. }
  2614. if (function_id_ == 0) {
  2615. // Propagate this rule to all dependant ids in the global scope.
  2616. id_to_at_reference_checks_[referenced_from_inst.id()].push_back(std::bind(
  2617. &BuiltInsValidator::ValidateSMBuiltinsAtReference, this, decoration,
  2618. built_in_inst, referenced_from_inst, std::placeholders::_1));
  2619. }
  2620. return SPV_SUCCESS;
  2621. }
  2622. spv_result_t BuiltInsValidator::ValidateSingleBuiltInAtDefinition(
  2623. const Decoration& decoration, const Instruction& inst) {
  2624. const SpvBuiltIn label = SpvBuiltIn(decoration.params()[0]);
  2625. // Builtins can only be applied to variables, structures or constants.
  2626. auto target_opcode = inst.opcode();
  2627. if (target_opcode != SpvOpTypeStruct && target_opcode != SpvOpVariable &&
  2628. !spvOpcodeIsConstant(target_opcode)) {
  2629. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2630. << "BuiltIns can only target variables, structs or constants";
  2631. }
  2632. if (!spvIsVulkanOrWebGPUEnv(_.context()->target_env)) {
  2633. // Early return. All currently implemented rules are based on Vulkan or
  2634. // WebGPU spec.
  2635. //
  2636. // TODO: If you are adding validation rules for environments other than
  2637. // Vulkan or WebGPU (or general rules which are not environment
  2638. // independent), then you need to modify or remove this condition. Consider
  2639. // also adding early returns into BuiltIn-specific rules, so that the system
  2640. // doesn't spawn new rules which don't do anything.
  2641. return SPV_SUCCESS;
  2642. }
  2643. if (spvIsWebGPUEnv(_.context()->target_env) &&
  2644. !IsBuiltInValidForWebGPU(label)) {
  2645. return _.diag(SPV_ERROR_INVALID_DATA, &inst)
  2646. << "WebGPU does not allow BuiltIn "
  2647. << _.grammar().lookupOperandName(SPV_OPERAND_TYPE_BUILT_IN,
  2648. decoration.params()[0]);
  2649. }
  2650. // If you are adding a new BuiltIn enum, please register it here.
  2651. // If the newly added enum has validation rules associated with it
  2652. // consider leaving a TODO and/or creating an issue.
  2653. switch (label) {
  2654. case SpvBuiltInClipDistance:
  2655. case SpvBuiltInCullDistance: {
  2656. return ValidateClipOrCullDistanceAtDefinition(decoration, inst);
  2657. }
  2658. case SpvBuiltInFragCoord: {
  2659. return ValidateFragCoordAtDefinition(decoration, inst);
  2660. }
  2661. case SpvBuiltInFragDepth: {
  2662. return ValidateFragDepthAtDefinition(decoration, inst);
  2663. }
  2664. case SpvBuiltInFrontFacing: {
  2665. return ValidateFrontFacingAtDefinition(decoration, inst);
  2666. }
  2667. case SpvBuiltInGlobalInvocationId:
  2668. case SpvBuiltInLocalInvocationId:
  2669. case SpvBuiltInNumWorkgroups:
  2670. case SpvBuiltInWorkgroupId: {
  2671. return ValidateComputeShaderI32Vec3InputAtDefinition(decoration, inst);
  2672. }
  2673. case SpvBuiltInHelperInvocation: {
  2674. return ValidateHelperInvocationAtDefinition(decoration, inst);
  2675. }
  2676. case SpvBuiltInInvocationId: {
  2677. return ValidateInvocationIdAtDefinition(decoration, inst);
  2678. }
  2679. case SpvBuiltInInstanceIndex: {
  2680. return ValidateInstanceIndexAtDefinition(decoration, inst);
  2681. }
  2682. case SpvBuiltInLayer:
  2683. case SpvBuiltInViewportIndex: {
  2684. return ValidateLayerOrViewportIndexAtDefinition(decoration, inst);
  2685. }
  2686. case SpvBuiltInPatchVertices: {
  2687. return ValidatePatchVerticesAtDefinition(decoration, inst);
  2688. }
  2689. case SpvBuiltInPointCoord: {
  2690. return ValidatePointCoordAtDefinition(decoration, inst);
  2691. }
  2692. case SpvBuiltInPointSize: {
  2693. return ValidatePointSizeAtDefinition(decoration, inst);
  2694. }
  2695. case SpvBuiltInPosition: {
  2696. return ValidatePositionAtDefinition(decoration, inst);
  2697. }
  2698. case SpvBuiltInPrimitiveId: {
  2699. return ValidatePrimitiveIdAtDefinition(decoration, inst);
  2700. }
  2701. case SpvBuiltInSampleId: {
  2702. return ValidateSampleIdAtDefinition(decoration, inst);
  2703. }
  2704. case SpvBuiltInSampleMask: {
  2705. return ValidateSampleMaskAtDefinition(decoration, inst);
  2706. }
  2707. case SpvBuiltInSamplePosition: {
  2708. return ValidateSamplePositionAtDefinition(decoration, inst);
  2709. }
  2710. case SpvBuiltInSubgroupId:
  2711. case SpvBuiltInNumSubgroups: {
  2712. return ValidateComputeI32InputAtDefinition(decoration, inst);
  2713. }
  2714. case SpvBuiltInSubgroupLocalInvocationId:
  2715. case SpvBuiltInSubgroupSize: {
  2716. return ValidateI32InputAtDefinition(decoration, inst);
  2717. }
  2718. case SpvBuiltInSubgroupEqMask:
  2719. case SpvBuiltInSubgroupGeMask:
  2720. case SpvBuiltInSubgroupGtMask:
  2721. case SpvBuiltInSubgroupLeMask:
  2722. case SpvBuiltInSubgroupLtMask: {
  2723. return ValidateI32Vec4InputAtDefinition(decoration, inst);
  2724. }
  2725. case SpvBuiltInTessCoord: {
  2726. return ValidateTessCoordAtDefinition(decoration, inst);
  2727. }
  2728. case SpvBuiltInTessLevelOuter: {
  2729. return ValidateTessLevelOuterAtDefinition(decoration, inst);
  2730. }
  2731. case SpvBuiltInTessLevelInner: {
  2732. return ValidateTessLevelInnerAtDefinition(decoration, inst);
  2733. }
  2734. case SpvBuiltInVertexIndex: {
  2735. return ValidateVertexIndexAtDefinition(decoration, inst);
  2736. }
  2737. case SpvBuiltInWorkgroupSize: {
  2738. return ValidateWorkgroupSizeAtDefinition(decoration, inst);
  2739. }
  2740. case SpvBuiltInVertexId:
  2741. case SpvBuiltInInstanceId: {
  2742. return ValidateVertexIdOrInstanceIdAtDefinition(decoration, inst);
  2743. }
  2744. case SpvBuiltInLocalInvocationIndex: {
  2745. return ValidateLocalInvocationIndexAtDefinition(decoration, inst);
  2746. }
  2747. case SpvBuiltInWarpsPerSMNV:
  2748. case SpvBuiltInSMCountNV:
  2749. case SpvBuiltInWarpIDNV:
  2750. case SpvBuiltInSMIDNV: {
  2751. return ValidateSMBuiltinsAtDefinition(decoration, inst);
  2752. }
  2753. case SpvBuiltInWorkDim:
  2754. case SpvBuiltInGlobalSize:
  2755. case SpvBuiltInEnqueuedWorkgroupSize:
  2756. case SpvBuiltInGlobalOffset:
  2757. case SpvBuiltInGlobalLinearId:
  2758. case SpvBuiltInSubgroupMaxSize:
  2759. case SpvBuiltInNumEnqueuedSubgroups:
  2760. case SpvBuiltInBaseVertex:
  2761. case SpvBuiltInBaseInstance:
  2762. case SpvBuiltInDrawIndex:
  2763. case SpvBuiltInDeviceIndex:
  2764. case SpvBuiltInViewIndex:
  2765. case SpvBuiltInBaryCoordNoPerspAMD:
  2766. case SpvBuiltInBaryCoordNoPerspCentroidAMD:
  2767. case SpvBuiltInBaryCoordNoPerspSampleAMD:
  2768. case SpvBuiltInBaryCoordSmoothAMD:
  2769. case SpvBuiltInBaryCoordSmoothCentroidAMD:
  2770. case SpvBuiltInBaryCoordSmoothSampleAMD:
  2771. case SpvBuiltInBaryCoordPullModelAMD:
  2772. case SpvBuiltInFragStencilRefEXT:
  2773. case SpvBuiltInViewportMaskNV:
  2774. case SpvBuiltInSecondaryPositionNV:
  2775. case SpvBuiltInSecondaryViewportMaskNV:
  2776. case SpvBuiltInPositionPerViewNV:
  2777. case SpvBuiltInViewportMaskPerViewNV:
  2778. case SpvBuiltInFullyCoveredEXT:
  2779. case SpvBuiltInMax:
  2780. case SpvBuiltInTaskCountNV:
  2781. case SpvBuiltInPrimitiveCountNV:
  2782. case SpvBuiltInPrimitiveIndicesNV:
  2783. case SpvBuiltInClipDistancePerViewNV:
  2784. case SpvBuiltInCullDistancePerViewNV:
  2785. case SpvBuiltInLayerPerViewNV:
  2786. case SpvBuiltInMeshViewCountNV:
  2787. case SpvBuiltInMeshViewIndicesNV:
  2788. case SpvBuiltInBaryCoordNV:
  2789. case SpvBuiltInBaryCoordNoPerspNV:
  2790. case SpvBuiltInFragmentSizeNV: // alias SpvBuiltInFragSizeEXT
  2791. case SpvBuiltInInvocationsPerPixelNV: // alias
  2792. // SpvBuiltInFragInvocationCountEXT
  2793. case SpvBuiltInLaunchIdNV:
  2794. case SpvBuiltInLaunchSizeNV:
  2795. case SpvBuiltInWorldRayOriginNV:
  2796. case SpvBuiltInWorldRayDirectionNV:
  2797. case SpvBuiltInObjectRayOriginNV:
  2798. case SpvBuiltInObjectRayDirectionNV:
  2799. case SpvBuiltInRayTminNV:
  2800. case SpvBuiltInRayTmaxNV:
  2801. case SpvBuiltInInstanceCustomIndexNV:
  2802. case SpvBuiltInObjectToWorldNV:
  2803. case SpvBuiltInWorldToObjectNV:
  2804. case SpvBuiltInHitTNV:
  2805. case SpvBuiltInHitKindNV:
  2806. case SpvBuiltInIncomingRayFlagsNV:
  2807. case SpvBuiltInRayGeometryIndexKHR: {
  2808. // No validation rules (for the moment).
  2809. break;
  2810. }
  2811. }
  2812. return SPV_SUCCESS;
  2813. }
  2814. spv_result_t BuiltInsValidator::ValidateBuiltInsAtDefinition() {
  2815. for (const auto& kv : _.id_decorations()) {
  2816. const uint32_t id = kv.first;
  2817. const auto& decorations = kv.second;
  2818. if (decorations.empty()) {
  2819. continue;
  2820. }
  2821. const Instruction* inst = _.FindDef(id);
  2822. assert(inst);
  2823. for (const auto& decoration : kv.second) {
  2824. if (decoration.dec_type() != SpvDecorationBuiltIn) {
  2825. continue;
  2826. }
  2827. if (spv_result_t error =
  2828. ValidateSingleBuiltInAtDefinition(decoration, *inst)) {
  2829. return error;
  2830. }
  2831. }
  2832. }
  2833. return SPV_SUCCESS;
  2834. }
  2835. spv_result_t BuiltInsValidator::Run() {
  2836. // First pass: validate all built-ins at definition and seed
  2837. // id_to_at_reference_checks_ with built-ins.
  2838. if (auto error = ValidateBuiltInsAtDefinition()) {
  2839. return error;
  2840. }
  2841. if (id_to_at_reference_checks_.empty()) {
  2842. // No validation tasks were seeded. Nothing else to do.
  2843. return SPV_SUCCESS;
  2844. }
  2845. // Second pass: validate every id reference in the module using
  2846. // rules in id_to_at_reference_checks_.
  2847. for (const Instruction& inst : _.ordered_instructions()) {
  2848. Update(inst);
  2849. std::set<uint32_t> already_checked;
  2850. for (const auto& operand : inst.operands()) {
  2851. if (!spvIsIdType(operand.type)) {
  2852. // Not id.
  2853. continue;
  2854. }
  2855. const uint32_t id = inst.word(operand.offset);
  2856. if (id == inst.id()) {
  2857. // No need to check result id.
  2858. continue;
  2859. }
  2860. if (!already_checked.insert(id).second) {
  2861. // The instruction has already referenced this id.
  2862. continue;
  2863. }
  2864. // Instruction references the id. Run all checks associated with the id
  2865. // on the instruction. id_to_at_reference_checks_ can be modified in the
  2866. // process, iterators are safe because it's a tree-based map.
  2867. const auto it = id_to_at_reference_checks_.find(id);
  2868. if (it != id_to_at_reference_checks_.end()) {
  2869. for (const auto& check : it->second) {
  2870. if (spv_result_t error = check(inst)) {
  2871. return error;
  2872. }
  2873. }
  2874. }
  2875. }
  2876. }
  2877. return SPV_SUCCESS;
  2878. }
  2879. } // namespace
  2880. // Validates correctness of built-in variables.
  2881. spv_result_t ValidateBuiltIns(ValidationState_t& _) {
  2882. BuiltInsValidator validator(_);
  2883. return validator.Run();
  2884. }
  2885. } // namespace val
  2886. } // namespace spvtools