spirv_parser.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  1. /*
  2. * Copyright 2018-2021 Arm Limited
  3. * SPDX-License-Identifier: Apache-2.0 OR MIT
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /*
  18. * At your option, you may choose to accept this material under either:
  19. * 1. The Apache License, Version 2.0, found at <http://www.apache.org/licenses/LICENSE-2.0>, or
  20. * 2. The MIT License, found at <http://opensource.org/licenses/MIT>.
  21. */
  22. #include "spirv_parser.hpp"
  23. #include <assert.h>
  24. using namespace std;
  25. using namespace spv;
  26. namespace SPIRV_CROSS_NAMESPACE
  27. {
  28. Parser::Parser(vector<uint32_t> spirv)
  29. {
  30. ir.spirv = std::move(spirv);
  31. }
  32. Parser::Parser(const uint32_t *spirv_data, size_t word_count)
  33. {
  34. ir.spirv = vector<uint32_t>(spirv_data, spirv_data + word_count);
  35. }
  36. static bool decoration_is_string(Decoration decoration)
  37. {
  38. switch (decoration)
  39. {
  40. case DecorationHlslSemanticGOOGLE:
  41. return true;
  42. default:
  43. return false;
  44. }
  45. }
  46. static inline uint32_t swap_endian(uint32_t v)
  47. {
  48. return ((v >> 24) & 0x000000ffu) | ((v >> 8) & 0x0000ff00u) | ((v << 8) & 0x00ff0000u) | ((v << 24) & 0xff000000u);
  49. }
  50. static bool is_valid_spirv_version(uint32_t version)
  51. {
  52. switch (version)
  53. {
  54. // Allow v99 since it tends to just work.
  55. case 99:
  56. case 0x10000: // SPIR-V 1.0
  57. case 0x10100: // SPIR-V 1.1
  58. case 0x10200: // SPIR-V 1.2
  59. case 0x10300: // SPIR-V 1.3
  60. case 0x10400: // SPIR-V 1.4
  61. case 0x10500: // SPIR-V 1.5
  62. case 0x10600: // SPIR-V 1.6
  63. return true;
  64. default:
  65. return false;
  66. }
  67. }
  68. void Parser::parse()
  69. {
  70. auto &spirv = ir.spirv;
  71. auto len = spirv.size();
  72. if (len < 5)
  73. SPIRV_CROSS_THROW("SPIRV file too small.");
  74. auto s = spirv.data();
  75. // Endian-swap if we need to.
  76. if (s[0] == swap_endian(MagicNumber))
  77. transform(begin(spirv), end(spirv), begin(spirv), [](uint32_t c) { return swap_endian(c); });
  78. if (s[0] != MagicNumber || !is_valid_spirv_version(s[1]))
  79. SPIRV_CROSS_THROW("Invalid SPIRV format.");
  80. uint32_t bound = s[3];
  81. const uint32_t MaximumNumberOfIDs = 0x3fffff;
  82. if (bound > MaximumNumberOfIDs)
  83. SPIRV_CROSS_THROW("ID bound exceeds limit of 0x3fffff.\n");
  84. ir.set_id_bounds(bound);
  85. uint32_t offset = 5;
  86. SmallVector<Instruction> instructions;
  87. while (offset < len)
  88. {
  89. Instruction instr = {};
  90. instr.op = spirv[offset] & 0xffff;
  91. instr.count = (spirv[offset] >> 16) & 0xffff;
  92. if (instr.count == 0)
  93. SPIRV_CROSS_THROW("SPIR-V instructions cannot consume 0 words. Invalid SPIR-V file.");
  94. instr.offset = offset + 1;
  95. instr.length = instr.count - 1;
  96. offset += instr.count;
  97. if (offset > spirv.size())
  98. SPIRV_CROSS_THROW("SPIR-V instruction goes out of bounds.");
  99. instructions.push_back(instr);
  100. }
  101. for (auto &i : instructions)
  102. parse(i);
  103. for (auto &fixup : forward_pointer_fixups)
  104. {
  105. auto &target = get<SPIRType>(fixup.first);
  106. auto &source = get<SPIRType>(fixup.second);
  107. target.member_types = source.member_types;
  108. target.basetype = source.basetype;
  109. target.self = source.self;
  110. }
  111. forward_pointer_fixups.clear();
  112. if (current_function)
  113. SPIRV_CROSS_THROW("Function was not terminated.");
  114. if (current_block)
  115. SPIRV_CROSS_THROW("Block was not terminated.");
  116. if (ir.default_entry_point == 0)
  117. SPIRV_CROSS_THROW("There is no entry point in the SPIR-V module.");
  118. }
  119. const uint32_t *Parser::stream(const Instruction &instr) const
  120. {
  121. // If we're not going to use any arguments, just return nullptr.
  122. // We want to avoid case where we return an out of range pointer
  123. // that trips debug assertions on some platforms.
  124. if (!instr.length)
  125. return nullptr;
  126. if (instr.offset + instr.length > ir.spirv.size())
  127. SPIRV_CROSS_THROW("Compiler::stream() out of range.");
  128. return &ir.spirv[instr.offset];
  129. }
  130. static string extract_string(const vector<uint32_t> &spirv, uint32_t offset)
  131. {
  132. string ret;
  133. for (uint32_t i = offset; i < spirv.size(); i++)
  134. {
  135. uint32_t w = spirv[i];
  136. for (uint32_t j = 0; j < 4; j++, w >>= 8)
  137. {
  138. char c = w & 0xff;
  139. if (c == '\0')
  140. return ret;
  141. ret += c;
  142. }
  143. }
  144. SPIRV_CROSS_THROW("String was not terminated before EOF");
  145. }
  146. void Parser::parse(const Instruction &instruction)
  147. {
  148. auto *ops = stream(instruction);
  149. auto op = static_cast<Op>(instruction.op);
  150. uint32_t length = instruction.length;
  151. // HACK for glslang that might emit OpEmitMeshTasksEXT followed by return / branch.
  152. // Instead of failing hard, just ignore it.
  153. if (ignore_trailing_block_opcodes)
  154. {
  155. ignore_trailing_block_opcodes = false;
  156. if (op == OpReturn || op == OpBranch || op == OpUnreachable)
  157. return;
  158. }
  159. switch (op)
  160. {
  161. case OpSourceContinued:
  162. case OpSourceExtension:
  163. case OpNop:
  164. case OpModuleProcessed:
  165. break;
  166. case OpString:
  167. {
  168. set<SPIRString>(ops[0], extract_string(ir.spirv, instruction.offset + 1));
  169. break;
  170. }
  171. case OpMemoryModel:
  172. ir.addressing_model = static_cast<AddressingModel>(ops[0]);
  173. ir.memory_model = static_cast<MemoryModel>(ops[1]);
  174. break;
  175. case OpSource:
  176. {
  177. auto lang = static_cast<SourceLanguage>(ops[0]);
  178. switch (lang)
  179. {
  180. case SourceLanguageESSL:
  181. ir.source.es = true;
  182. ir.source.version = ops[1];
  183. ir.source.known = true;
  184. ir.source.hlsl = false;
  185. break;
  186. case SourceLanguageGLSL:
  187. ir.source.es = false;
  188. ir.source.version = ops[1];
  189. ir.source.known = true;
  190. ir.source.hlsl = false;
  191. break;
  192. case SourceLanguageHLSL:
  193. // For purposes of cross-compiling, this is GLSL 450.
  194. ir.source.es = false;
  195. ir.source.version = 450;
  196. ir.source.known = true;
  197. ir.source.hlsl = true;
  198. break;
  199. default:
  200. ir.source.known = false;
  201. break;
  202. }
  203. break;
  204. }
  205. case OpUndef:
  206. {
  207. uint32_t result_type = ops[0];
  208. uint32_t id = ops[1];
  209. set<SPIRUndef>(id, result_type);
  210. if (current_block)
  211. current_block->ops.push_back(instruction);
  212. break;
  213. }
  214. case OpCapability:
  215. {
  216. uint32_t cap = ops[0];
  217. if (cap == CapabilityKernel)
  218. SPIRV_CROSS_THROW("Kernel capability not supported.");
  219. ir.declared_capabilities.push_back(static_cast<Capability>(ops[0]));
  220. break;
  221. }
  222. case OpExtension:
  223. {
  224. auto ext = extract_string(ir.spirv, instruction.offset);
  225. ir.declared_extensions.push_back(std::move(ext));
  226. break;
  227. }
  228. case OpExtInstImport:
  229. {
  230. uint32_t id = ops[0];
  231. SPIRExtension::Extension spirv_ext = SPIRExtension::Unsupported;
  232. auto ext = extract_string(ir.spirv, instruction.offset + 1);
  233. if (ext == "GLSL.std.450")
  234. spirv_ext = SPIRExtension::GLSL;
  235. else if (ext == "DebugInfo")
  236. spirv_ext = SPIRExtension::SPV_debug_info;
  237. else if (ext == "SPV_AMD_shader_ballot")
  238. spirv_ext = SPIRExtension::SPV_AMD_shader_ballot;
  239. else if (ext == "SPV_AMD_shader_explicit_vertex_parameter")
  240. spirv_ext = SPIRExtension::SPV_AMD_shader_explicit_vertex_parameter;
  241. else if (ext == "SPV_AMD_shader_trinary_minmax")
  242. spirv_ext = SPIRExtension::SPV_AMD_shader_trinary_minmax;
  243. else if (ext == "SPV_AMD_gcn_shader")
  244. spirv_ext = SPIRExtension::SPV_AMD_gcn_shader;
  245. else if (ext == "NonSemantic.DebugPrintf")
  246. spirv_ext = SPIRExtension::NonSemanticDebugPrintf;
  247. else if (ext == "NonSemantic.Shader.DebugInfo.100")
  248. spirv_ext = SPIRExtension::NonSemanticShaderDebugInfo;
  249. else if (ext.find("NonSemantic.") == 0)
  250. spirv_ext = SPIRExtension::NonSemanticGeneric;
  251. set<SPIRExtension>(id, spirv_ext);
  252. // Other SPIR-V extensions which have ExtInstrs are currently not supported.
  253. break;
  254. }
  255. case OpExtInst:
  256. case OpExtInstWithForwardRefsKHR:
  257. {
  258. // The SPIR-V debug information extended instructions might come at global scope.
  259. if (current_block)
  260. {
  261. current_block->ops.push_back(instruction);
  262. if (length >= 2)
  263. {
  264. const auto *type = maybe_get<SPIRType>(ops[0]);
  265. if (type)
  266. ir.load_type_width.insert({ ops[1], type->width });
  267. }
  268. }
  269. break;
  270. }
  271. case OpEntryPoint:
  272. {
  273. auto itr =
  274. ir.entry_points.insert(make_pair(ops[1], SPIREntryPoint(ops[1], static_cast<ExecutionModel>(ops[0]),
  275. extract_string(ir.spirv, instruction.offset + 2))));
  276. auto &e = itr.first->second;
  277. // Strings need nul-terminator and consume the whole word.
  278. uint32_t strlen_words = uint32_t((e.name.size() + 1 + 3) >> 2);
  279. for (uint32_t i = strlen_words + 2; i < instruction.length; i++)
  280. e.interface_variables.push_back(ops[i]);
  281. // Set the name of the entry point in case OpName is not provided later.
  282. ir.set_name(ops[1], e.name);
  283. // If we don't have an entry, make the first one our "default".
  284. if (!ir.default_entry_point)
  285. ir.default_entry_point = ops[1];
  286. break;
  287. }
  288. case OpExecutionMode:
  289. {
  290. auto &execution = ir.entry_points[ops[0]];
  291. auto mode = static_cast<ExecutionMode>(ops[1]);
  292. execution.flags.set(mode);
  293. switch (mode)
  294. {
  295. case ExecutionModeInvocations:
  296. execution.invocations = ops[2];
  297. break;
  298. case ExecutionModeLocalSize:
  299. execution.workgroup_size.x = ops[2];
  300. execution.workgroup_size.y = ops[3];
  301. execution.workgroup_size.z = ops[4];
  302. break;
  303. case ExecutionModeOutputVertices:
  304. execution.output_vertices = ops[2];
  305. break;
  306. case ExecutionModeOutputPrimitivesEXT:
  307. execution.output_primitives = ops[2];
  308. break;
  309. default:
  310. break;
  311. }
  312. break;
  313. }
  314. case OpExecutionModeId:
  315. {
  316. auto &execution = ir.entry_points[ops[0]];
  317. auto mode = static_cast<ExecutionMode>(ops[1]);
  318. execution.flags.set(mode);
  319. switch (mode)
  320. {
  321. case ExecutionModeLocalSizeId:
  322. execution.workgroup_size.id_x = ops[2];
  323. execution.workgroup_size.id_y = ops[3];
  324. execution.workgroup_size.id_z = ops[4];
  325. break;
  326. case ExecutionModeFPFastMathDefault:
  327. execution.fp_fast_math_defaults[ops[2]] = ops[3];
  328. break;
  329. default:
  330. break;
  331. }
  332. break;
  333. }
  334. case OpName:
  335. {
  336. uint32_t id = ops[0];
  337. ir.set_name(id, extract_string(ir.spirv, instruction.offset + 1));
  338. break;
  339. }
  340. case OpMemberName:
  341. {
  342. uint32_t id = ops[0];
  343. uint32_t member = ops[1];
  344. ir.set_member_name(id, member, extract_string(ir.spirv, instruction.offset + 2));
  345. break;
  346. }
  347. case OpDecorationGroup:
  348. {
  349. // Noop, this simply means an ID should be a collector of decorations.
  350. // The meta array is already a flat array of decorations which will contain the relevant decorations.
  351. break;
  352. }
  353. case OpGroupDecorate:
  354. {
  355. uint32_t group_id = ops[0];
  356. auto &decorations = ir.meta[group_id].decoration;
  357. auto &flags = decorations.decoration_flags;
  358. // Copies decorations from one ID to another. Only copy decorations which are set in the group,
  359. // i.e., we cannot just copy the meta structure directly.
  360. for (uint32_t i = 1; i < length; i++)
  361. {
  362. uint32_t target = ops[i];
  363. flags.for_each_bit([&](uint32_t bit) {
  364. auto decoration = static_cast<Decoration>(bit);
  365. if (decoration_is_string(decoration))
  366. {
  367. ir.set_decoration_string(target, decoration, ir.get_decoration_string(group_id, decoration));
  368. }
  369. else
  370. {
  371. ir.meta[target].decoration_word_offset[decoration] =
  372. ir.meta[group_id].decoration_word_offset[decoration];
  373. ir.set_decoration(target, decoration, ir.get_decoration(group_id, decoration));
  374. }
  375. });
  376. }
  377. break;
  378. }
  379. case OpGroupMemberDecorate:
  380. {
  381. uint32_t group_id = ops[0];
  382. auto &flags = ir.meta[group_id].decoration.decoration_flags;
  383. // Copies decorations from one ID to another. Only copy decorations which are set in the group,
  384. // i.e., we cannot just copy the meta structure directly.
  385. for (uint32_t i = 1; i + 1 < length; i += 2)
  386. {
  387. uint32_t target = ops[i + 0];
  388. uint32_t index = ops[i + 1];
  389. flags.for_each_bit([&](uint32_t bit) {
  390. auto decoration = static_cast<Decoration>(bit);
  391. if (decoration_is_string(decoration))
  392. ir.set_member_decoration_string(target, index, decoration,
  393. ir.get_decoration_string(group_id, decoration));
  394. else
  395. ir.set_member_decoration(target, index, decoration, ir.get_decoration(group_id, decoration));
  396. });
  397. }
  398. break;
  399. }
  400. case OpDecorate:
  401. case OpDecorateId:
  402. {
  403. // OpDecorateId technically supports an array of arguments, but our only supported decorations are single uint,
  404. // so merge decorate and decorate-id here.
  405. uint32_t id = ops[0];
  406. auto decoration = static_cast<Decoration>(ops[1]);
  407. if (length >= 3)
  408. {
  409. ir.meta[id].decoration_word_offset[decoration] = uint32_t(&ops[2] - ir.spirv.data());
  410. ir.set_decoration(id, decoration, ops[2]);
  411. }
  412. else
  413. ir.set_decoration(id, decoration);
  414. break;
  415. }
  416. case OpDecorateStringGOOGLE:
  417. {
  418. uint32_t id = ops[0];
  419. auto decoration = static_cast<Decoration>(ops[1]);
  420. ir.set_decoration_string(id, decoration, extract_string(ir.spirv, instruction.offset + 2));
  421. break;
  422. }
  423. case OpMemberDecorate:
  424. {
  425. uint32_t id = ops[0];
  426. uint32_t member = ops[1];
  427. auto decoration = static_cast<Decoration>(ops[2]);
  428. if (length >= 4)
  429. ir.set_member_decoration(id, member, decoration, ops[3]);
  430. else
  431. ir.set_member_decoration(id, member, decoration);
  432. break;
  433. }
  434. case OpMemberDecorateStringGOOGLE:
  435. {
  436. uint32_t id = ops[0];
  437. uint32_t member = ops[1];
  438. auto decoration = static_cast<Decoration>(ops[2]);
  439. ir.set_member_decoration_string(id, member, decoration, extract_string(ir.spirv, instruction.offset + 3));
  440. break;
  441. }
  442. // Build up basic types.
  443. case OpTypeVoid:
  444. {
  445. uint32_t id = ops[0];
  446. auto &type = set<SPIRType>(id, op);
  447. type.basetype = SPIRType::Void;
  448. break;
  449. }
  450. case OpTypeBool:
  451. {
  452. uint32_t id = ops[0];
  453. auto &type = set<SPIRType>(id, op);
  454. type.basetype = SPIRType::Boolean;
  455. type.width = 1;
  456. break;
  457. }
  458. case OpTypeFloat:
  459. {
  460. uint32_t id = ops[0];
  461. uint32_t width = ops[1];
  462. auto &type = set<SPIRType>(id, op);
  463. if (width != 16 && width != 8 && length > 2)
  464. SPIRV_CROSS_THROW("Unrecognized FP encoding mode for OpTypeFloat.");
  465. if (width == 64)
  466. type.basetype = SPIRType::Double;
  467. else if (width == 32)
  468. type.basetype = SPIRType::Float;
  469. else if (width == 16)
  470. {
  471. if (length > 2)
  472. {
  473. if (ops[2] == spv::FPEncodingBFloat16KHR)
  474. type.basetype = SPIRType::BFloat16;
  475. else
  476. SPIRV_CROSS_THROW("Unrecognized encoding for OpTypeFloat 16.");
  477. }
  478. else
  479. type.basetype = SPIRType::Half;
  480. }
  481. else if (width == 8)
  482. {
  483. if (length < 2)
  484. SPIRV_CROSS_THROW("Missing encoding for OpTypeFloat 8.");
  485. else if (ops[2] == spv::FPEncodingFloat8E4M3EXT)
  486. type.basetype = SPIRType::FloatE4M3;
  487. else if (ops[2] == spv::FPEncodingFloat8E5M2EXT)
  488. type.basetype = SPIRType::FloatE5M2;
  489. else
  490. SPIRV_CROSS_THROW("Invalid encoding for OpTypeFloat 8.");
  491. }
  492. else
  493. SPIRV_CROSS_THROW("Unrecognized bit-width of floating point type.");
  494. type.width = width;
  495. break;
  496. }
  497. case OpTypeInt:
  498. {
  499. uint32_t id = ops[0];
  500. uint32_t width = ops[1];
  501. bool signedness = ops[2] != 0;
  502. auto &type = set<SPIRType>(id, op);
  503. type.basetype = signedness ? to_signed_basetype(width) : to_unsigned_basetype(width);
  504. type.width = width;
  505. break;
  506. }
  507. // Build composite types by "inheriting".
  508. // NOTE: The self member is also copied! For pointers and array modifiers this is a good thing
  509. // since we can refer to decorations on pointee classes which is needed for UBO/SSBO, I/O blocks in geometry/tess etc.
  510. case OpTypeVector:
  511. {
  512. uint32_t id = ops[0];
  513. uint32_t vecsize = ops[2];
  514. auto &base = get<SPIRType>(ops[1]);
  515. auto &vecbase = set<SPIRType>(id, base);
  516. vecbase.op = op;
  517. vecbase.vecsize = vecsize;
  518. vecbase.self = id;
  519. vecbase.parent_type = ops[1];
  520. break;
  521. }
  522. case OpTypeMatrix:
  523. {
  524. uint32_t id = ops[0];
  525. uint32_t colcount = ops[2];
  526. auto &base = get<SPIRType>(ops[1]);
  527. auto &matrixbase = set<SPIRType>(id, base);
  528. matrixbase.op = op;
  529. matrixbase.columns = colcount;
  530. matrixbase.self = id;
  531. matrixbase.parent_type = ops[1];
  532. break;
  533. }
  534. case OpTypeCooperativeMatrixKHR:
  535. {
  536. uint32_t id = ops[0];
  537. auto &base = get<SPIRType>(ops[1]);
  538. auto &matrixbase = set<SPIRType>(id, base);
  539. matrixbase.op = op;
  540. matrixbase.cooperative.scope_id = ops[2];
  541. matrixbase.cooperative.rows_id = ops[3];
  542. matrixbase.cooperative.columns_id = ops[4];
  543. matrixbase.cooperative.use_id = ops[5];
  544. matrixbase.self = id;
  545. matrixbase.parent_type = ops[1];
  546. break;
  547. }
  548. case OpTypeArray:
  549. {
  550. uint32_t id = ops[0];
  551. uint32_t tid = ops[1];
  552. auto &base = get<SPIRType>(tid);
  553. auto &arraybase = set<SPIRType>(id, base);
  554. arraybase.op = op;
  555. arraybase.parent_type = tid;
  556. uint32_t cid = ops[2];
  557. ir.mark_used_as_array_length(cid);
  558. auto *c = maybe_get<SPIRConstant>(cid);
  559. bool literal = c && !c->specialization;
  560. // We're copying type information into Array types, so we'll need a fixup for any physical pointer
  561. // references.
  562. if (base.forward_pointer)
  563. forward_pointer_fixups.push_back({ id, tid });
  564. arraybase.array_size_literal.push_back(literal);
  565. arraybase.array.push_back(literal ? c->scalar() : cid);
  566. // .self resolves down to non-array/non-pointer type.
  567. arraybase.self = base.self;
  568. break;
  569. }
  570. case OpTypeRuntimeArray:
  571. {
  572. uint32_t id = ops[0];
  573. auto &base = get<SPIRType>(ops[1]);
  574. auto &arraybase = set<SPIRType>(id, base);
  575. // We're copying type information into Array types, so we'll need a fixup for any physical pointer
  576. // references.
  577. if (base.forward_pointer)
  578. forward_pointer_fixups.push_back({ id, ops[1] });
  579. arraybase.op = op;
  580. arraybase.array.push_back(0);
  581. arraybase.array_size_literal.push_back(true);
  582. arraybase.parent_type = ops[1];
  583. // .self resolves down to non-array/non-pointer type.
  584. arraybase.self = base.self;
  585. break;
  586. }
  587. case OpTypeImage:
  588. {
  589. uint32_t id = ops[0];
  590. auto &type = set<SPIRType>(id, op);
  591. type.basetype = SPIRType::Image;
  592. type.image.type = ops[1];
  593. type.image.dim = static_cast<Dim>(ops[2]);
  594. type.image.depth = ops[3] == 1;
  595. type.image.arrayed = ops[4] != 0;
  596. type.image.ms = ops[5] != 0;
  597. type.image.sampled = ops[6];
  598. type.image.format = static_cast<ImageFormat>(ops[7]);
  599. type.image.access = (length >= 9) ? static_cast<AccessQualifier>(ops[8]) : AccessQualifierMax;
  600. break;
  601. }
  602. case OpTypeSampledImage:
  603. {
  604. uint32_t id = ops[0];
  605. uint32_t imagetype = ops[1];
  606. auto &type = set<SPIRType>(id, op);
  607. type = get<SPIRType>(imagetype);
  608. type.basetype = SPIRType::SampledImage;
  609. type.self = id;
  610. break;
  611. }
  612. case OpTypeSampler:
  613. {
  614. uint32_t id = ops[0];
  615. auto &type = set<SPIRType>(id, op);
  616. type.basetype = SPIRType::Sampler;
  617. break;
  618. }
  619. case OpTypePointer:
  620. {
  621. uint32_t id = ops[0];
  622. // Very rarely, we might receive a FunctionPrototype here.
  623. // We won't be able to compile it, but we shouldn't crash when parsing.
  624. // We should be able to reflect.
  625. auto *base = maybe_get<SPIRType>(ops[2]);
  626. auto &ptrbase = set<SPIRType>(id, op);
  627. if (base)
  628. {
  629. ptrbase = *base;
  630. ptrbase.op = op;
  631. }
  632. ptrbase.pointer = true;
  633. ptrbase.pointer_depth++;
  634. ptrbase.storage = static_cast<StorageClass>(ops[1]);
  635. if (ptrbase.storage == StorageClassAtomicCounter)
  636. ptrbase.basetype = SPIRType::AtomicCounter;
  637. if (base && base->forward_pointer)
  638. forward_pointer_fixups.push_back({ id, ops[2] });
  639. ptrbase.parent_type = ops[2];
  640. // Do NOT set ptrbase.self!
  641. break;
  642. }
  643. case OpTypeForwardPointer:
  644. {
  645. uint32_t id = ops[0];
  646. auto &ptrbase = set<SPIRType>(id, op);
  647. ptrbase.pointer = true;
  648. ptrbase.pointer_depth++;
  649. ptrbase.storage = static_cast<StorageClass>(ops[1]);
  650. ptrbase.forward_pointer = true;
  651. if (ptrbase.storage == StorageClassAtomicCounter)
  652. ptrbase.basetype = SPIRType::AtomicCounter;
  653. break;
  654. }
  655. case OpTypeStruct:
  656. {
  657. uint32_t id = ops[0];
  658. auto &type = set<SPIRType>(id, op);
  659. type.basetype = SPIRType::Struct;
  660. for (uint32_t i = 1; i < length; i++)
  661. type.member_types.push_back(ops[i]);
  662. // Check if we have seen this struct type before, with just different
  663. // decorations.
  664. //
  665. // Add workaround for issue #17 as well by looking at OpName for the struct
  666. // types, which we shouldn't normally do.
  667. // We should not normally have to consider type aliases like this to begin with
  668. // however ... glslang issues #304, #307 cover this.
  669. // For stripped names, never consider struct type aliasing.
  670. // We risk declaring the same struct multiple times, but type-punning is not allowed
  671. // so this is safe.
  672. bool consider_aliasing = !ir.get_name(type.self).empty();
  673. if (consider_aliasing)
  674. {
  675. for (auto &other : global_struct_cache)
  676. {
  677. if (ir.get_name(type.self) == ir.get_name(other) &&
  678. types_are_logically_equivalent(type, get<SPIRType>(other)))
  679. {
  680. type.type_alias = other;
  681. break;
  682. }
  683. }
  684. if (type.type_alias == TypeID(0))
  685. global_struct_cache.push_back(id);
  686. }
  687. break;
  688. }
  689. case OpTypeFunction:
  690. {
  691. uint32_t id = ops[0];
  692. uint32_t ret = ops[1];
  693. auto &func = set<SPIRFunctionPrototype>(id, ret);
  694. for (uint32_t i = 2; i < length; i++)
  695. func.parameter_types.push_back(ops[i]);
  696. break;
  697. }
  698. case OpTypeAccelerationStructureKHR:
  699. {
  700. uint32_t id = ops[0];
  701. auto &type = set<SPIRType>(id, op);
  702. type.basetype = SPIRType::AccelerationStructure;
  703. break;
  704. }
  705. case OpTypeRayQueryKHR:
  706. {
  707. uint32_t id = ops[0];
  708. auto &type = set<SPIRType>(id, op);
  709. type.basetype = SPIRType::RayQuery;
  710. break;
  711. }
  712. // Variable declaration
  713. // All variables are essentially pointers with a storage qualifier.
  714. case OpVariable:
  715. {
  716. uint32_t type = ops[0];
  717. uint32_t id = ops[1];
  718. auto storage = static_cast<StorageClass>(ops[2]);
  719. uint32_t initializer = length == 4 ? ops[3] : 0;
  720. if (storage == StorageClassFunction)
  721. {
  722. if (!current_function)
  723. SPIRV_CROSS_THROW("No function currently in scope");
  724. current_function->add_local_variable(id);
  725. }
  726. set<SPIRVariable>(id, type, storage, initializer);
  727. break;
  728. }
  729. // OpPhi
  730. // OpPhi is a fairly magical opcode.
  731. // It selects temporary variables based on which parent block we *came from*.
  732. // In high-level languages we can "de-SSA" by creating a function local, and flush out temporaries to this function-local
  733. // variable to emulate SSA Phi.
  734. case OpPhi:
  735. {
  736. if (!current_function)
  737. SPIRV_CROSS_THROW("No function currently in scope");
  738. if (!current_block)
  739. SPIRV_CROSS_THROW("No block currently in scope");
  740. uint32_t result_type = ops[0];
  741. uint32_t id = ops[1];
  742. // Instead of a temporary, create a new function-wide temporary with this ID instead.
  743. auto &var = set<SPIRVariable>(id, result_type, spv::StorageClassFunction);
  744. var.phi_variable = true;
  745. current_function->add_local_variable(id);
  746. for (uint32_t i = 2; i + 2 <= length; i += 2)
  747. current_block->phi_variables.push_back({ ops[i], ops[i + 1], id });
  748. break;
  749. }
  750. // Constants
  751. case OpSpecConstant:
  752. case OpConstant:
  753. case OpConstantCompositeReplicateEXT:
  754. case OpSpecConstantCompositeReplicateEXT:
  755. {
  756. uint32_t id = ops[1];
  757. auto &type = get<SPIRType>(ops[0]);
  758. if (op == OpConstantCompositeReplicateEXT || op == OpSpecConstantCompositeReplicateEXT)
  759. {
  760. auto subconstant = uint32_t(ops[2]);
  761. set<SPIRConstant>(id, ops[0], &subconstant, 1, op == OpSpecConstantCompositeReplicateEXT, true);
  762. }
  763. else
  764. {
  765. if (type.width > 32)
  766. set<SPIRConstant>(id, ops[0], ops[2] | (uint64_t(ops[3]) << 32), op == OpSpecConstant);
  767. else
  768. set<SPIRConstant>(id, ops[0], ops[2], op == OpSpecConstant);
  769. }
  770. break;
  771. }
  772. case OpSpecConstantFalse:
  773. case OpConstantFalse:
  774. {
  775. uint32_t id = ops[1];
  776. set<SPIRConstant>(id, ops[0], uint32_t(0), op == OpSpecConstantFalse);
  777. break;
  778. }
  779. case OpSpecConstantTrue:
  780. case OpConstantTrue:
  781. {
  782. uint32_t id = ops[1];
  783. set<SPIRConstant>(id, ops[0], uint32_t(1), op == OpSpecConstantTrue);
  784. break;
  785. }
  786. case OpConstantNull:
  787. {
  788. uint32_t id = ops[1];
  789. uint32_t type = ops[0];
  790. ir.make_constant_null(id, type, true);
  791. break;
  792. }
  793. case OpSpecConstantComposite:
  794. case OpConstantComposite:
  795. {
  796. uint32_t id = ops[1];
  797. uint32_t type = ops[0];
  798. auto &ctype = get<SPIRType>(type);
  799. // We can have constants which are structs and arrays.
  800. // In this case, our SPIRConstant will be a list of other SPIRConstant ids which we
  801. // can refer to.
  802. if (ctype.basetype == SPIRType::Struct || !ctype.array.empty())
  803. {
  804. set<SPIRConstant>(id, type, ops + 2, length - 2, op == OpSpecConstantComposite);
  805. }
  806. else
  807. {
  808. uint32_t elements = length - 2;
  809. if (elements > 4)
  810. SPIRV_CROSS_THROW("OpConstantComposite only supports 1, 2, 3 and 4 elements.");
  811. SPIRConstant remapped_constant_ops[4];
  812. const SPIRConstant *c[4];
  813. for (uint32_t i = 0; i < elements; i++)
  814. {
  815. // Specialization constants operations can also be part of this.
  816. // We do not know their value, so any attempt to query SPIRConstant later
  817. // will fail. We can only propagate the ID of the expression and use to_expression on it.
  818. auto *constant_op = maybe_get<SPIRConstantOp>(ops[2 + i]);
  819. auto *undef_op = maybe_get<SPIRUndef>(ops[2 + i]);
  820. if (constant_op)
  821. {
  822. if (op == OpConstantComposite)
  823. SPIRV_CROSS_THROW("Specialization constant operation used in OpConstantComposite.");
  824. remapped_constant_ops[i].make_null(get<SPIRType>(constant_op->basetype));
  825. remapped_constant_ops[i].self = constant_op->self;
  826. remapped_constant_ops[i].constant_type = constant_op->basetype;
  827. remapped_constant_ops[i].specialization = true;
  828. c[i] = &remapped_constant_ops[i];
  829. }
  830. else if (undef_op)
  831. {
  832. // Undefined, just pick 0.
  833. remapped_constant_ops[i].make_null(get<SPIRType>(undef_op->basetype));
  834. remapped_constant_ops[i].constant_type = undef_op->basetype;
  835. c[i] = &remapped_constant_ops[i];
  836. }
  837. else
  838. c[i] = &get<SPIRConstant>(ops[2 + i]);
  839. }
  840. set<SPIRConstant>(id, type, c, elements, op == OpSpecConstantComposite);
  841. }
  842. break;
  843. }
  844. // Functions
  845. case OpFunction:
  846. {
  847. uint32_t res = ops[0];
  848. uint32_t id = ops[1];
  849. // Control
  850. uint32_t type = ops[3];
  851. if (current_function)
  852. SPIRV_CROSS_THROW("Must end a function before starting a new one!");
  853. current_function = &set<SPIRFunction>(id, res, type);
  854. break;
  855. }
  856. case OpFunctionParameter:
  857. {
  858. uint32_t type = ops[0];
  859. uint32_t id = ops[1];
  860. if (!current_function)
  861. SPIRV_CROSS_THROW("Must be in a function!");
  862. current_function->add_parameter(type, id);
  863. set<SPIRVariable>(id, type, StorageClassFunction);
  864. break;
  865. }
  866. case OpFunctionEnd:
  867. {
  868. if (current_block)
  869. {
  870. // Very specific error message, but seems to come up quite often.
  871. SPIRV_CROSS_THROW(
  872. "Cannot end a function before ending the current block.\n"
  873. "Likely cause: If this SPIR-V was created from glslang HLSL, make sure the entry point is valid.");
  874. }
  875. current_function = nullptr;
  876. break;
  877. }
  878. // Blocks
  879. case OpLabel:
  880. {
  881. // OpLabel always starts a block.
  882. if (!current_function)
  883. SPIRV_CROSS_THROW("Blocks cannot exist outside functions!");
  884. uint32_t id = ops[0];
  885. current_function->blocks.push_back(id);
  886. if (!current_function->entry_block)
  887. current_function->entry_block = id;
  888. if (current_block)
  889. SPIRV_CROSS_THROW("Cannot start a block before ending the current block.");
  890. current_block = &set<SPIRBlock>(id);
  891. break;
  892. }
  893. // Branch instructions end blocks.
  894. case OpBranch:
  895. {
  896. if (!current_block)
  897. SPIRV_CROSS_THROW("Trying to end a non-existing block.");
  898. uint32_t target = ops[0];
  899. current_block->terminator = SPIRBlock::Direct;
  900. current_block->next_block = target;
  901. current_block = nullptr;
  902. break;
  903. }
  904. case OpBranchConditional:
  905. {
  906. if (!current_block)
  907. SPIRV_CROSS_THROW("Trying to end a non-existing block.");
  908. current_block->condition = ops[0];
  909. current_block->true_block = ops[1];
  910. current_block->false_block = ops[2];
  911. current_block->terminator = SPIRBlock::Select;
  912. if (current_block->true_block == current_block->false_block)
  913. {
  914. // Bogus conditional, translate to a direct branch.
  915. // Avoids some ugly edge cases later when analyzing CFGs.
  916. // There are some super jank cases where the merge block is different from the true/false,
  917. // and later branches can "break" out of the selection construct this way.
  918. // This is complete nonsense, but CTS hits this case.
  919. // In this scenario, we should see the selection construct as more of a Switch with one default case.
  920. // The problem here is that this breaks any attempt to break out of outer switch statements,
  921. // but it's theoretically solvable if this ever comes up using the ladder breaking system ...
  922. if (current_block->true_block != current_block->next_block &&
  923. current_block->merge == SPIRBlock::MergeSelection)
  924. {
  925. uint32_t ids = ir.increase_bound_by(2);
  926. auto &type = set<SPIRType>(ids, OpTypeInt);
  927. type.basetype = SPIRType::Int;
  928. type.width = 32;
  929. auto &c = set<SPIRConstant>(ids + 1, ids);
  930. current_block->condition = c.self;
  931. current_block->default_block = current_block->true_block;
  932. current_block->terminator = SPIRBlock::MultiSelect;
  933. ir.block_meta[current_block->next_block] &= ~ParsedIR::BLOCK_META_SELECTION_MERGE_BIT;
  934. ir.block_meta[current_block->next_block] |= ParsedIR::BLOCK_META_MULTISELECT_MERGE_BIT;
  935. }
  936. else
  937. {
  938. // Collapse loops if we have to.
  939. bool collapsed_loop = current_block->true_block == current_block->merge_block &&
  940. current_block->merge == SPIRBlock::MergeLoop;
  941. if (collapsed_loop)
  942. {
  943. ir.block_meta[current_block->merge_block] &= ~ParsedIR::BLOCK_META_LOOP_MERGE_BIT;
  944. ir.block_meta[current_block->continue_block] &= ~ParsedIR::BLOCK_META_CONTINUE_BIT;
  945. }
  946. current_block->next_block = current_block->true_block;
  947. current_block->condition = 0;
  948. current_block->true_block = 0;
  949. current_block->false_block = 0;
  950. current_block->merge_block = 0;
  951. current_block->merge = SPIRBlock::MergeNone;
  952. current_block->terminator = SPIRBlock::Direct;
  953. }
  954. }
  955. current_block = nullptr;
  956. break;
  957. }
  958. case OpSwitch:
  959. {
  960. if (!current_block)
  961. SPIRV_CROSS_THROW("Trying to end a non-existing block.");
  962. current_block->terminator = SPIRBlock::MultiSelect;
  963. current_block->condition = ops[0];
  964. current_block->default_block = ops[1];
  965. uint32_t remaining_ops = length - 2;
  966. if ((remaining_ops % 2) == 0)
  967. {
  968. for (uint32_t i = 2; i + 2 <= length; i += 2)
  969. current_block->cases_32bit.push_back({ ops[i], ops[i + 1] });
  970. }
  971. if ((remaining_ops % 3) == 0)
  972. {
  973. for (uint32_t i = 2; i + 3 <= length; i += 3)
  974. {
  975. uint64_t value = (static_cast<uint64_t>(ops[i + 1]) << 32) | ops[i];
  976. current_block->cases_64bit.push_back({ value, ops[i + 2] });
  977. }
  978. }
  979. // If we jump to next block, make it break instead since we're inside a switch case block at that point.
  980. ir.block_meta[current_block->next_block] |= ParsedIR::BLOCK_META_MULTISELECT_MERGE_BIT;
  981. current_block = nullptr;
  982. break;
  983. }
  984. case OpKill:
  985. case OpTerminateInvocation:
  986. {
  987. if (!current_block)
  988. SPIRV_CROSS_THROW("Trying to end a non-existing block.");
  989. current_block->terminator = SPIRBlock::Kill;
  990. current_block = nullptr;
  991. break;
  992. }
  993. case OpTerminateRayKHR:
  994. // NV variant is not a terminator.
  995. if (!current_block)
  996. SPIRV_CROSS_THROW("Trying to end a non-existing block.");
  997. current_block->terminator = SPIRBlock::TerminateRay;
  998. current_block = nullptr;
  999. break;
  1000. case OpIgnoreIntersectionKHR:
  1001. // NV variant is not a terminator.
  1002. if (!current_block)
  1003. SPIRV_CROSS_THROW("Trying to end a non-existing block.");
  1004. current_block->terminator = SPIRBlock::IgnoreIntersection;
  1005. current_block = nullptr;
  1006. break;
  1007. case OpEmitMeshTasksEXT:
  1008. if (!current_block)
  1009. SPIRV_CROSS_THROW("Trying to end a non-existing block.");
  1010. current_block->terminator = SPIRBlock::EmitMeshTasks;
  1011. for (uint32_t i = 0; i < 3; i++)
  1012. current_block->mesh.groups[i] = ops[i];
  1013. current_block->mesh.payload = length >= 4 ? ops[3] : 0;
  1014. current_block = nullptr;
  1015. // Currently glslang is bugged and does not treat EmitMeshTasksEXT as a terminator.
  1016. ignore_trailing_block_opcodes = true;
  1017. break;
  1018. case OpReturn:
  1019. {
  1020. if (!current_block)
  1021. SPIRV_CROSS_THROW("Trying to end a non-existing block.");
  1022. current_block->terminator = SPIRBlock::Return;
  1023. current_block = nullptr;
  1024. break;
  1025. }
  1026. case OpReturnValue:
  1027. {
  1028. if (!current_block)
  1029. SPIRV_CROSS_THROW("Trying to end a non-existing block.");
  1030. current_block->terminator = SPIRBlock::Return;
  1031. current_block->return_value = ops[0];
  1032. current_block = nullptr;
  1033. break;
  1034. }
  1035. case OpUnreachable:
  1036. {
  1037. if (!current_block)
  1038. SPIRV_CROSS_THROW("Trying to end a non-existing block.");
  1039. current_block->terminator = SPIRBlock::Unreachable;
  1040. current_block = nullptr;
  1041. break;
  1042. }
  1043. case OpSelectionMerge:
  1044. {
  1045. if (!current_block)
  1046. SPIRV_CROSS_THROW("Trying to modify a non-existing block.");
  1047. current_block->next_block = ops[0];
  1048. current_block->merge = SPIRBlock::MergeSelection;
  1049. ir.block_meta[current_block->next_block] |= ParsedIR::BLOCK_META_SELECTION_MERGE_BIT;
  1050. if (length >= 2)
  1051. {
  1052. if (ops[1] & SelectionControlFlattenMask)
  1053. current_block->hint = SPIRBlock::HintFlatten;
  1054. else if (ops[1] & SelectionControlDontFlattenMask)
  1055. current_block->hint = SPIRBlock::HintDontFlatten;
  1056. }
  1057. break;
  1058. }
  1059. case OpLoopMerge:
  1060. {
  1061. if (!current_block)
  1062. SPIRV_CROSS_THROW("Trying to modify a non-existing block.");
  1063. current_block->merge_block = ops[0];
  1064. current_block->continue_block = ops[1];
  1065. current_block->merge = SPIRBlock::MergeLoop;
  1066. ir.block_meta[current_block->self] |= ParsedIR::BLOCK_META_LOOP_HEADER_BIT;
  1067. ir.block_meta[current_block->merge_block] |= ParsedIR::BLOCK_META_LOOP_MERGE_BIT;
  1068. ir.continue_block_to_loop_header[current_block->continue_block] = BlockID(current_block->self);
  1069. // Don't add loop headers to continue blocks,
  1070. // which would make it impossible branch into the loop header since
  1071. // they are treated as continues.
  1072. if (current_block->continue_block != BlockID(current_block->self))
  1073. ir.block_meta[current_block->continue_block] |= ParsedIR::BLOCK_META_CONTINUE_BIT;
  1074. if (length >= 3)
  1075. {
  1076. if (ops[2] & LoopControlUnrollMask)
  1077. current_block->hint = SPIRBlock::HintUnroll;
  1078. else if (ops[2] & LoopControlDontUnrollMask)
  1079. current_block->hint = SPIRBlock::HintDontUnroll;
  1080. }
  1081. break;
  1082. }
  1083. case OpSpecConstantOp:
  1084. {
  1085. if (length < 3)
  1086. SPIRV_CROSS_THROW("OpSpecConstantOp not enough arguments.");
  1087. uint32_t result_type = ops[0];
  1088. uint32_t id = ops[1];
  1089. auto spec_op = static_cast<Op>(ops[2]);
  1090. set<SPIRConstantOp>(id, result_type, spec_op, ops + 3, length - 3);
  1091. break;
  1092. }
  1093. case OpLine:
  1094. {
  1095. // OpLine might come at global scope, but we don't care about those since they will not be declared in any
  1096. // meaningful correct order.
  1097. // Ignore all OpLine directives which live outside a function.
  1098. if (current_block)
  1099. current_block->ops.push_back(instruction);
  1100. // Line directives may arrive before first OpLabel.
  1101. // Treat this as the line of the function declaration,
  1102. // so warnings for arguments can propagate properly.
  1103. if (current_function)
  1104. {
  1105. // Store the first one we find and emit it before creating the function prototype.
  1106. if (current_function->entry_line.file_id == 0)
  1107. {
  1108. current_function->entry_line.file_id = ops[0];
  1109. current_function->entry_line.line_literal = ops[1];
  1110. }
  1111. }
  1112. break;
  1113. }
  1114. case OpNoLine:
  1115. {
  1116. // OpNoLine might come at global scope.
  1117. if (current_block)
  1118. current_block->ops.push_back(instruction);
  1119. break;
  1120. }
  1121. // Actual opcodes.
  1122. default:
  1123. {
  1124. if (length >= 2)
  1125. {
  1126. const auto *type = maybe_get<SPIRType>(ops[0]);
  1127. if (type)
  1128. ir.load_type_width.insert({ ops[1], type->width });
  1129. }
  1130. if (!current_block)
  1131. SPIRV_CROSS_THROW("Currently no block to insert opcode.");
  1132. current_block->ops.push_back(instruction);
  1133. break;
  1134. }
  1135. }
  1136. }
  1137. bool Parser::types_are_logically_equivalent(const SPIRType &a, const SPIRType &b) const
  1138. {
  1139. if (a.basetype != b.basetype)
  1140. return false;
  1141. if (a.width != b.width)
  1142. return false;
  1143. if (a.vecsize != b.vecsize)
  1144. return false;
  1145. if (a.columns != b.columns)
  1146. return false;
  1147. if (a.array.size() != b.array.size())
  1148. return false;
  1149. size_t array_count = a.array.size();
  1150. if (array_count && memcmp(a.array.data(), b.array.data(), array_count * sizeof(uint32_t)) != 0)
  1151. return false;
  1152. if (a.basetype == SPIRType::Image || a.basetype == SPIRType::SampledImage)
  1153. {
  1154. if (memcmp(&a.image, &b.image, sizeof(SPIRType::Image)) != 0)
  1155. return false;
  1156. }
  1157. if (a.member_types.size() != b.member_types.size())
  1158. return false;
  1159. size_t member_types = a.member_types.size();
  1160. for (size_t i = 0; i < member_types; i++)
  1161. {
  1162. if (!types_are_logically_equivalent(get<SPIRType>(a.member_types[i]), get<SPIRType>(b.member_types[i])))
  1163. return false;
  1164. }
  1165. return true;
  1166. }
  1167. bool Parser::variable_storage_is_aliased(const SPIRVariable &v) const
  1168. {
  1169. auto &type = get<SPIRType>(v.basetype);
  1170. auto *type_meta = ir.find_meta(type.self);
  1171. bool ssbo = v.storage == StorageClassStorageBuffer ||
  1172. (type_meta && type_meta->decoration.decoration_flags.get(DecorationBufferBlock));
  1173. bool image = type.basetype == SPIRType::Image;
  1174. bool counter = type.basetype == SPIRType::AtomicCounter;
  1175. bool is_restrict;
  1176. if (ssbo)
  1177. is_restrict = ir.get_buffer_block_flags(v).get(DecorationRestrict);
  1178. else
  1179. is_restrict = ir.has_decoration(v.self, DecorationRestrict);
  1180. return !is_restrict && (ssbo || image || counter);
  1181. }
  1182. } // namespace SPIRV_CROSS_NAMESPACE