ir_context.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. // Copyright (c) 2017 Google Inc.
  2. // Modifications Copyright (C) 2024 Advanced Micro Devices, Inc. All rights
  3. // reserved.
  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. #include "source/opt/ir_context.h"
  17. #include <cstring>
  18. #include "OpenCLDebugInfo100.h"
  19. #include "source/latest_version_glsl_std_450_header.h"
  20. #include "source/opt/log.h"
  21. #include "source/opt/reflect.h"
  22. namespace spvtools {
  23. namespace opt {
  24. namespace {
  25. constexpr int kSpvDecorateTargetIdInIdx = 0;
  26. constexpr int kSpvDecorateDecorationInIdx = 1;
  27. constexpr int kSpvDecorateBuiltinInIdx = 2;
  28. constexpr int kEntryPointInterfaceInIdx = 3;
  29. constexpr int kEntryPointFunctionIdInIdx = 1;
  30. constexpr int kEntryPointExecutionModelInIdx = 0;
  31. // Constants for OpenCL.DebugInfo.100 / NonSemantic.Shader.DebugInfo.100
  32. // extension instructions.
  33. constexpr uint32_t kDebugFunctionOperandFunctionIndex = 13;
  34. constexpr uint32_t kDebugGlobalVariableOperandVariableIndex = 11;
  35. } // namespace
  36. void IRContext::BuildInvalidAnalyses(IRContext::Analysis set) {
  37. set = Analysis(set & ~valid_analyses_);
  38. if (set & kAnalysisDefUse) {
  39. BuildDefUseManager();
  40. }
  41. if (set & kAnalysisInstrToBlockMapping) {
  42. BuildInstrToBlockMapping();
  43. }
  44. if (set & kAnalysisDecorations) {
  45. BuildDecorationManager();
  46. }
  47. if (set & kAnalysisCFG) {
  48. BuildCFG();
  49. }
  50. if (set & kAnalysisDominatorAnalysis) {
  51. ResetDominatorAnalysis();
  52. }
  53. if (set & kAnalysisLoopAnalysis) {
  54. ResetLoopAnalysis();
  55. }
  56. if (set & kAnalysisBuiltinVarId) {
  57. ResetBuiltinAnalysis();
  58. }
  59. if (set & kAnalysisNameMap) {
  60. BuildIdToNameMap();
  61. }
  62. if (set & kAnalysisScalarEvolution) {
  63. BuildScalarEvolutionAnalysis();
  64. }
  65. if (set & kAnalysisRegisterPressure) {
  66. BuildRegPressureAnalysis();
  67. }
  68. if (set & kAnalysisValueNumberTable) {
  69. BuildValueNumberTable();
  70. }
  71. if (set & kAnalysisStructuredCFG) {
  72. BuildStructuredCFGAnalysis();
  73. }
  74. if (set & kAnalysisIdToFuncMapping) {
  75. BuildIdToFuncMapping();
  76. }
  77. if (set & kAnalysisConstants) {
  78. BuildConstantManager();
  79. }
  80. if (set & kAnalysisTypes) {
  81. BuildTypeManager();
  82. }
  83. if (set & kAnalysisDebugInfo) {
  84. BuildDebugInfoManager();
  85. }
  86. if (set & kAnalysisLiveness) {
  87. BuildLivenessManager();
  88. }
  89. if (set & kAnalysisIdToGraphMapping) {
  90. BuildIdToGraphMapping();
  91. }
  92. }
  93. void IRContext::InvalidateAnalysesExceptFor(
  94. IRContext::Analysis preserved_analyses) {
  95. uint32_t analyses_to_invalidate = valid_analyses_ & (~preserved_analyses);
  96. InvalidateAnalyses(static_cast<IRContext::Analysis>(analyses_to_invalidate));
  97. }
  98. void IRContext::InvalidateAnalyses(IRContext::Analysis analyses_to_invalidate) {
  99. // The ConstantManager and DebugInfoManager contain Type pointers. If the
  100. // TypeManager goes away, the ConstantManager and DebugInfoManager have to
  101. // go away.
  102. if (analyses_to_invalidate & kAnalysisTypes) {
  103. analyses_to_invalidate |= kAnalysisConstants;
  104. analyses_to_invalidate |= kAnalysisDebugInfo;
  105. }
  106. // The dominator analysis hold the pseudo entry and exit nodes from the CFG.
  107. // Also if the CFG change the dominators many changed as well, so the
  108. // dominator analysis should be invalidated as well.
  109. if (analyses_to_invalidate & kAnalysisCFG) {
  110. analyses_to_invalidate |= kAnalysisDominatorAnalysis;
  111. }
  112. if (analyses_to_invalidate & kAnalysisDefUse) {
  113. def_use_mgr_.reset(nullptr);
  114. }
  115. if (analyses_to_invalidate & kAnalysisInstrToBlockMapping) {
  116. instr_to_block_.clear();
  117. }
  118. if (analyses_to_invalidate & kAnalysisDecorations) {
  119. decoration_mgr_.reset(nullptr);
  120. }
  121. if (analyses_to_invalidate & kAnalysisCombinators) {
  122. combinator_ops_.clear();
  123. }
  124. if (analyses_to_invalidate & kAnalysisBuiltinVarId) {
  125. builtin_var_id_map_.clear();
  126. }
  127. if (analyses_to_invalidate & kAnalysisCFG) {
  128. cfg_.reset(nullptr);
  129. }
  130. if (analyses_to_invalidate & kAnalysisDominatorAnalysis) {
  131. dominator_trees_.clear();
  132. post_dominator_trees_.clear();
  133. }
  134. if (analyses_to_invalidate & kAnalysisNameMap) {
  135. id_to_name_.reset(nullptr);
  136. }
  137. if (analyses_to_invalidate & kAnalysisValueNumberTable) {
  138. vn_table_.reset(nullptr);
  139. }
  140. if (analyses_to_invalidate & kAnalysisStructuredCFG) {
  141. struct_cfg_analysis_.reset(nullptr);
  142. }
  143. if (analyses_to_invalidate & kAnalysisIdToFuncMapping) {
  144. id_to_func_.clear();
  145. }
  146. if (analyses_to_invalidate & kAnalysisConstants) {
  147. constant_mgr_.reset(nullptr);
  148. }
  149. if (analyses_to_invalidate & kAnalysisLiveness) {
  150. liveness_mgr_.reset(nullptr);
  151. }
  152. if (analyses_to_invalidate & kAnalysisTypes) {
  153. type_mgr_.reset(nullptr);
  154. }
  155. if (analyses_to_invalidate & kAnalysisDebugInfo) {
  156. debug_info_mgr_.reset(nullptr);
  157. }
  158. if (analyses_to_invalidate & kAnalysisIdToGraphMapping) {
  159. id_to_graph_.clear();
  160. }
  161. valid_analyses_ = Analysis(valid_analyses_ & ~analyses_to_invalidate);
  162. }
  163. Instruction* IRContext::KillInst(Instruction* inst) {
  164. if (!inst) {
  165. return nullptr;
  166. }
  167. KillNamesAndDecorates(inst);
  168. KillOperandFromDebugInstructions(inst);
  169. if (AreAnalysesValid(kAnalysisDefUse)) {
  170. analysis::DefUseManager* def_use_mgr = get_def_use_mgr();
  171. def_use_mgr->ClearInst(inst);
  172. for (auto& l_inst : inst->dbg_line_insts()) def_use_mgr->ClearInst(&l_inst);
  173. }
  174. if (AreAnalysesValid(kAnalysisInstrToBlockMapping)) {
  175. instr_to_block_.erase(inst);
  176. }
  177. if (AreAnalysesValid(kAnalysisDecorations)) {
  178. if (inst->IsDecoration()) {
  179. decoration_mgr_->RemoveDecoration(inst);
  180. }
  181. }
  182. if (AreAnalysesValid(kAnalysisDebugInfo)) {
  183. get_debug_info_mgr()->ClearDebugScopeAndInlinedAtUses(inst);
  184. get_debug_info_mgr()->ClearDebugInfo(inst);
  185. }
  186. if (type_mgr_ && IsTypeInst(inst->opcode())) {
  187. type_mgr_->RemoveId(inst->result_id());
  188. }
  189. if (constant_mgr_ && IsConstantInst(inst->opcode())) {
  190. constant_mgr_->RemoveId(inst->result_id());
  191. }
  192. if (inst->opcode() == spv::Op::OpCapability ||
  193. inst->opcode() == spv::Op::OpConditionalCapabilityINTEL ||
  194. inst->opcode() == spv::Op::OpExtension ||
  195. inst->opcode() == spv::Op::OpConditionalExtensionINTEL) {
  196. // We reset the feature manager, instead of updating it, because it is just
  197. // as much work. We would have to remove all capabilities implied by this
  198. // capability that are not also implied by the remaining OpCapability
  199. // instructions. We could update extensions, but we will see if it is
  200. // needed.
  201. ResetFeatureManager();
  202. }
  203. RemoveFromIdToName(inst);
  204. Instruction* next_instruction = nullptr;
  205. if (inst->IsInAList()) {
  206. next_instruction = inst->NextNode();
  207. inst->RemoveFromList();
  208. delete inst;
  209. } else {
  210. // Needed for instructions that are not part of a list like OpLabels,
  211. // OpFunction, OpFunctionEnd, etc..
  212. inst->ToNop();
  213. }
  214. return next_instruction;
  215. }
  216. bool IRContext::KillInstructionIf(Module::inst_iterator begin,
  217. Module::inst_iterator end,
  218. std::function<bool(Instruction*)> condition) {
  219. bool removed = false;
  220. for (auto it = begin; it != end;) {
  221. if (!condition(&*it)) {
  222. ++it;
  223. continue;
  224. }
  225. removed = true;
  226. // `it` is an iterator on an intrusive list. Next is invalidated on the
  227. // current node when an instruction is killed. The iterator must be moved
  228. // forward before deleting the node.
  229. auto instruction = &*it;
  230. ++it;
  231. KillInst(instruction);
  232. }
  233. return removed;
  234. }
  235. void IRContext::CollectNonSemanticTree(
  236. Instruction* inst, std::unordered_set<Instruction*>* to_kill) {
  237. if (!inst->HasResultId()) return;
  238. // Debug[No]Line result id is not used, so we are done
  239. if (inst->IsDebugLineInst()) return;
  240. std::vector<Instruction*> work_list;
  241. std::unordered_set<Instruction*> seen;
  242. work_list.push_back(inst);
  243. while (!work_list.empty()) {
  244. auto* i = work_list.back();
  245. work_list.pop_back();
  246. get_def_use_mgr()->ForEachUser(
  247. i, [&work_list, to_kill, &seen](Instruction* user) {
  248. if (user->IsNonSemanticInstruction() && seen.insert(user).second) {
  249. work_list.push_back(user);
  250. to_kill->insert(user);
  251. }
  252. });
  253. }
  254. }
  255. bool IRContext::KillDef(uint32_t id) {
  256. Instruction* def = get_def_use_mgr()->GetDef(id);
  257. if (def != nullptr) {
  258. KillInst(def);
  259. return true;
  260. }
  261. return false;
  262. }
  263. bool IRContext::RemoveCapability(spv::Capability capability) {
  264. const bool removed = KillInstructionIf(
  265. module()->capability_begin(), module()->capability_end(),
  266. [capability](Instruction* inst) {
  267. return static_cast<spv::Capability>(inst->GetSingleWordOperand(0)) ==
  268. capability;
  269. });
  270. if (removed && feature_mgr_ != nullptr) {
  271. feature_mgr_->RemoveCapability(capability);
  272. }
  273. return removed;
  274. }
  275. bool IRContext::RemoveExtension(Extension extension) {
  276. const std::string_view extensionName = ExtensionToString(extension);
  277. const bool removed = KillInstructionIf(
  278. module()->extension_begin(), module()->extension_end(),
  279. [&extensionName](Instruction* inst) {
  280. return inst->GetOperand(0).AsString() == extensionName;
  281. });
  282. if (removed && feature_mgr_ != nullptr) {
  283. feature_mgr_->RemoveExtension(extension);
  284. }
  285. return removed;
  286. }
  287. bool IRContext::ReplaceAllUsesWith(uint32_t before, uint32_t after) {
  288. return ReplaceAllUsesWithPredicate(before, after,
  289. [](Instruction*) { return true; });
  290. }
  291. bool IRContext::ReplaceAllUsesWithPredicate(
  292. uint32_t before, uint32_t after,
  293. const std::function<bool(Instruction*)>& predicate) {
  294. if (before == after) return false;
  295. if (AreAnalysesValid(kAnalysisDebugInfo)) {
  296. get_debug_info_mgr()->ReplaceAllUsesInDebugScopeWithPredicate(before, after,
  297. predicate);
  298. }
  299. // Ensure that |after| has been registered as def.
  300. assert(get_def_use_mgr()->GetDef(after) &&
  301. "'after' is not a registered def.");
  302. std::vector<std::pair<Instruction*, uint32_t>> uses_to_update;
  303. get_def_use_mgr()->ForEachUse(
  304. before, [&predicate, &uses_to_update](Instruction* user, uint32_t index) {
  305. if (predicate(user)) {
  306. uses_to_update.emplace_back(user, index);
  307. }
  308. });
  309. Instruction* prev = nullptr;
  310. for (auto p : uses_to_update) {
  311. Instruction* user = p.first;
  312. uint32_t index = p.second;
  313. if (prev == nullptr || prev != user) {
  314. ForgetUses(user);
  315. prev = user;
  316. }
  317. const uint32_t type_result_id_count =
  318. (user->result_id() != 0) + (user->type_id() != 0);
  319. if (index < type_result_id_count) {
  320. // Update the type_id. Note that result id is immutable so it should
  321. // never be updated.
  322. if (user->type_id() != 0 && index == 0) {
  323. user->SetResultType(after);
  324. } else if (user->type_id() == 0) {
  325. SPIRV_ASSERT(consumer_, false,
  326. "Result type id considered as use while the instruction "
  327. "doesn't have a result type id.");
  328. (void)consumer_; // Makes the compiler happy for release build.
  329. } else {
  330. SPIRV_ASSERT(consumer_, false,
  331. "Trying setting the immutable result id.");
  332. }
  333. } else {
  334. // Update an in-operand.
  335. uint32_t in_operand_pos = index - type_result_id_count;
  336. // Make the modification in the instruction.
  337. user->SetInOperand(in_operand_pos, {after});
  338. }
  339. AnalyzeUses(user);
  340. }
  341. return true;
  342. }
  343. bool IRContext::IsConsistent() {
  344. #ifndef SPIRV_CHECK_CONTEXT
  345. return true;
  346. #else
  347. if (AreAnalysesValid(kAnalysisDefUse)) {
  348. analysis::DefUseManager new_def_use(module());
  349. if (!CompareAndPrintDifferences(*get_def_use_mgr(), new_def_use)) {
  350. return false;
  351. }
  352. }
  353. return true;
  354. if (AreAnalysesValid(kAnalysisIdToFuncMapping)) {
  355. for (auto& fn : *module_) {
  356. if (id_to_func_[fn.result_id()] != &fn) {
  357. return false;
  358. }
  359. }
  360. }
  361. if (AreAnalysesValid(kAnalysisIdToGraphMapping)) {
  362. for (auto& g : module_->graphs()) {
  363. if (id_to_graph_[g->DefInst().result_id()] != g.get()) {
  364. return false;
  365. }
  366. }
  367. }
  368. if (AreAnalysesValid(kAnalysisInstrToBlockMapping)) {
  369. for (auto& func : *module()) {
  370. for (auto& block : func) {
  371. if (!block.WhileEachInst([this, &block](Instruction* inst) {
  372. if (get_instr_block(inst) != &block) {
  373. return false;
  374. }
  375. return true;
  376. })) {
  377. return false;
  378. }
  379. }
  380. }
  381. }
  382. if (!CheckCFG()) {
  383. return false;
  384. }
  385. if (AreAnalysesValid(kAnalysisDecorations)) {
  386. analysis::DecorationManager* dec_mgr = get_decoration_mgr();
  387. analysis::DecorationManager current(module());
  388. if (*dec_mgr != current) {
  389. return false;
  390. }
  391. }
  392. if (feature_mgr_ != nullptr) {
  393. FeatureManager current(grammar_);
  394. current.Analyze(module());
  395. if (current != *feature_mgr_) {
  396. return false;
  397. }
  398. }
  399. return true;
  400. #endif
  401. }
  402. void IRContext::ForgetUses(Instruction* inst) {
  403. if (AreAnalysesValid(kAnalysisDefUse)) {
  404. get_def_use_mgr()->EraseUseRecordsOfOperandIds(inst);
  405. }
  406. if (AreAnalysesValid(kAnalysisDecorations)) {
  407. if (inst->IsDecoration()) {
  408. get_decoration_mgr()->RemoveDecoration(inst);
  409. }
  410. }
  411. if (AreAnalysesValid(kAnalysisDebugInfo)) {
  412. get_debug_info_mgr()->ClearDebugInfo(inst);
  413. }
  414. RemoveFromIdToName(inst);
  415. }
  416. void IRContext::AnalyzeUses(Instruction* inst) {
  417. if (AreAnalysesValid(kAnalysisDefUse)) {
  418. get_def_use_mgr()->AnalyzeInstUse(inst);
  419. }
  420. if (AreAnalysesValid(kAnalysisDecorations)) {
  421. if (inst->IsDecoration()) {
  422. get_decoration_mgr()->AddDecoration(inst);
  423. }
  424. }
  425. if (AreAnalysesValid(kAnalysisDebugInfo)) {
  426. get_debug_info_mgr()->AnalyzeDebugInst(inst);
  427. }
  428. if (id_to_name_ && (inst->opcode() == spv::Op::OpName ||
  429. inst->opcode() == spv::Op::OpMemberName)) {
  430. id_to_name_->insert({inst->GetSingleWordInOperand(0), inst});
  431. }
  432. }
  433. void IRContext::KillNamesAndDecorates(uint32_t id) {
  434. analysis::DecorationManager* dec_mgr = get_decoration_mgr();
  435. dec_mgr->RemoveDecorationsFrom(id);
  436. std::vector<Instruction*> name_to_kill;
  437. for (auto name : GetNames(id)) {
  438. name_to_kill.push_back(name.second);
  439. }
  440. for (Instruction* name_inst : name_to_kill) {
  441. KillInst(name_inst);
  442. }
  443. }
  444. void IRContext::KillNamesAndDecorates(Instruction* inst) {
  445. const uint32_t rId = inst->result_id();
  446. if (rId == 0) return;
  447. KillNamesAndDecorates(rId);
  448. }
  449. void IRContext::KillOperandFromDebugInstructions(Instruction* inst) {
  450. const auto opcode = inst->opcode();
  451. const uint32_t id = inst->result_id();
  452. // Kill id of OpFunction from DebugFunction.
  453. if (opcode == spv::Op::OpFunction) {
  454. for (auto it = module()->ext_inst_debuginfo_begin();
  455. it != module()->ext_inst_debuginfo_end(); ++it) {
  456. if (it->GetOpenCL100DebugOpcode() != OpenCLDebugInfo100DebugFunction)
  457. continue;
  458. auto& operand = it->GetOperand(kDebugFunctionOperandFunctionIndex);
  459. if (operand.words[0] == id) {
  460. operand.words[0] =
  461. get_debug_info_mgr()->GetDebugInfoNone()->result_id();
  462. get_def_use_mgr()->AnalyzeInstUse(&*it);
  463. }
  464. }
  465. }
  466. // Kill id of OpVariable for global variable from DebugGlobalVariable.
  467. if (opcode == spv::Op::OpVariable || IsConstantInst(opcode)) {
  468. for (auto it = module()->ext_inst_debuginfo_begin();
  469. it != module()->ext_inst_debuginfo_end(); ++it) {
  470. if (it->GetCommonDebugOpcode() != CommonDebugInfoDebugGlobalVariable)
  471. continue;
  472. auto& operand = it->GetOperand(kDebugGlobalVariableOperandVariableIndex);
  473. if (operand.words[0] == id) {
  474. operand.words[0] =
  475. get_debug_info_mgr()->GetDebugInfoNone()->result_id();
  476. get_def_use_mgr()->AnalyzeInstUse(&*it);
  477. }
  478. }
  479. }
  480. }
  481. void IRContext::AddCombinatorsForCapability(uint32_t capability) {
  482. spv::Capability cap = spv::Capability(capability);
  483. if (cap == spv::Capability::Shader) {
  484. combinator_ops_[0].insert(
  485. {(uint32_t)spv::Op::OpNop,
  486. (uint32_t)spv::Op::OpUndef,
  487. (uint32_t)spv::Op::OpConstant,
  488. (uint32_t)spv::Op::OpConstantTrue,
  489. (uint32_t)spv::Op::OpConstantFalse,
  490. (uint32_t)spv::Op::OpConstantComposite,
  491. (uint32_t)spv::Op::OpConstantSampler,
  492. (uint32_t)spv::Op::OpConstantNull,
  493. (uint32_t)spv::Op::OpTypeVoid,
  494. (uint32_t)spv::Op::OpTypeBool,
  495. (uint32_t)spv::Op::OpTypeInt,
  496. (uint32_t)spv::Op::OpTypeFloat,
  497. (uint32_t)spv::Op::OpTypeVector,
  498. (uint32_t)spv::Op::OpTypeMatrix,
  499. (uint32_t)spv::Op::OpTypeImage,
  500. (uint32_t)spv::Op::OpTypeSampler,
  501. (uint32_t)spv::Op::OpTypeSampledImage,
  502. (uint32_t)spv::Op::OpTypeAccelerationStructureNV,
  503. (uint32_t)spv::Op::OpTypeAccelerationStructureKHR,
  504. (uint32_t)spv::Op::OpTypeRayQueryKHR,
  505. (uint32_t)spv::Op::OpTypeHitObjectNV,
  506. (uint32_t)spv::Op::OpTypeHitObjectEXT,
  507. (uint32_t)spv::Op::OpTypeArray,
  508. (uint32_t)spv::Op::OpTypeRuntimeArray,
  509. (uint32_t)spv::Op::OpTypeNodePayloadArrayAMDX,
  510. (uint32_t)spv::Op::OpTypeStruct,
  511. (uint32_t)spv::Op::OpTypeOpaque,
  512. (uint32_t)spv::Op::OpTypePointer,
  513. (uint32_t)spv::Op::OpTypeFunction,
  514. (uint32_t)spv::Op::OpTypeEvent,
  515. (uint32_t)spv::Op::OpTypeDeviceEvent,
  516. (uint32_t)spv::Op::OpTypeReserveId,
  517. (uint32_t)spv::Op::OpTypeQueue,
  518. (uint32_t)spv::Op::OpTypePipe,
  519. (uint32_t)spv::Op::OpTypeForwardPointer,
  520. (uint32_t)spv::Op::OpVariable,
  521. (uint32_t)spv::Op::OpImageTexelPointer,
  522. (uint32_t)spv::Op::OpLoad,
  523. (uint32_t)spv::Op::OpAccessChain,
  524. (uint32_t)spv::Op::OpInBoundsAccessChain,
  525. (uint32_t)spv::Op::OpArrayLength,
  526. (uint32_t)spv::Op::OpVectorExtractDynamic,
  527. (uint32_t)spv::Op::OpVectorInsertDynamic,
  528. (uint32_t)spv::Op::OpVectorShuffle,
  529. (uint32_t)spv::Op::OpCompositeConstruct,
  530. (uint32_t)spv::Op::OpCompositeExtract,
  531. (uint32_t)spv::Op::OpCompositeInsert,
  532. (uint32_t)spv::Op::OpCopyLogical,
  533. (uint32_t)spv::Op::OpCopyObject,
  534. (uint32_t)spv::Op::OpTranspose,
  535. (uint32_t)spv::Op::OpSampledImage,
  536. (uint32_t)spv::Op::OpImageSampleImplicitLod,
  537. (uint32_t)spv::Op::OpImageSampleExplicitLod,
  538. (uint32_t)spv::Op::OpImageSampleDrefImplicitLod,
  539. (uint32_t)spv::Op::OpImageSampleDrefExplicitLod,
  540. (uint32_t)spv::Op::OpImageSampleProjImplicitLod,
  541. (uint32_t)spv::Op::OpImageSampleProjExplicitLod,
  542. (uint32_t)spv::Op::OpImageSampleProjDrefImplicitLod,
  543. (uint32_t)spv::Op::OpImageSampleProjDrefExplicitLod,
  544. (uint32_t)spv::Op::OpImageFetch,
  545. (uint32_t)spv::Op::OpImageGather,
  546. (uint32_t)spv::Op::OpImageDrefGather,
  547. (uint32_t)spv::Op::OpImageRead,
  548. (uint32_t)spv::Op::OpImage,
  549. (uint32_t)spv::Op::OpImageQueryFormat,
  550. (uint32_t)spv::Op::OpImageQueryOrder,
  551. (uint32_t)spv::Op::OpImageQuerySizeLod,
  552. (uint32_t)spv::Op::OpImageQuerySize,
  553. (uint32_t)spv::Op::OpImageQueryLevels,
  554. (uint32_t)spv::Op::OpImageQuerySamples,
  555. (uint32_t)spv::Op::OpConvertFToU,
  556. (uint32_t)spv::Op::OpConvertFToS,
  557. (uint32_t)spv::Op::OpConvertSToF,
  558. (uint32_t)spv::Op::OpConvertUToF,
  559. (uint32_t)spv::Op::OpUConvert,
  560. (uint32_t)spv::Op::OpSConvert,
  561. (uint32_t)spv::Op::OpFConvert,
  562. (uint32_t)spv::Op::OpQuantizeToF16,
  563. (uint32_t)spv::Op::OpBitcast,
  564. (uint32_t)spv::Op::OpSNegate,
  565. (uint32_t)spv::Op::OpFNegate,
  566. (uint32_t)spv::Op::OpIAdd,
  567. (uint32_t)spv::Op::OpFAdd,
  568. (uint32_t)spv::Op::OpISub,
  569. (uint32_t)spv::Op::OpFSub,
  570. (uint32_t)spv::Op::OpIMul,
  571. (uint32_t)spv::Op::OpFMul,
  572. (uint32_t)spv::Op::OpUDiv,
  573. (uint32_t)spv::Op::OpSDiv,
  574. (uint32_t)spv::Op::OpFDiv,
  575. (uint32_t)spv::Op::OpUMod,
  576. (uint32_t)spv::Op::OpSRem,
  577. (uint32_t)spv::Op::OpSMod,
  578. (uint32_t)spv::Op::OpFRem,
  579. (uint32_t)spv::Op::OpFMod,
  580. (uint32_t)spv::Op::OpVectorTimesScalar,
  581. (uint32_t)spv::Op::OpMatrixTimesScalar,
  582. (uint32_t)spv::Op::OpVectorTimesMatrix,
  583. (uint32_t)spv::Op::OpMatrixTimesVector,
  584. (uint32_t)spv::Op::OpMatrixTimesMatrix,
  585. (uint32_t)spv::Op::OpOuterProduct,
  586. (uint32_t)spv::Op::OpDot,
  587. (uint32_t)spv::Op::OpIAddCarry,
  588. (uint32_t)spv::Op::OpISubBorrow,
  589. (uint32_t)spv::Op::OpUMulExtended,
  590. (uint32_t)spv::Op::OpSMulExtended,
  591. (uint32_t)spv::Op::OpAny,
  592. (uint32_t)spv::Op::OpAll,
  593. (uint32_t)spv::Op::OpIsNan,
  594. (uint32_t)spv::Op::OpIsInf,
  595. (uint32_t)spv::Op::OpLogicalEqual,
  596. (uint32_t)spv::Op::OpLogicalNotEqual,
  597. (uint32_t)spv::Op::OpLogicalOr,
  598. (uint32_t)spv::Op::OpLogicalAnd,
  599. (uint32_t)spv::Op::OpLogicalNot,
  600. (uint32_t)spv::Op::OpSelect,
  601. (uint32_t)spv::Op::OpIEqual,
  602. (uint32_t)spv::Op::OpINotEqual,
  603. (uint32_t)spv::Op::OpUGreaterThan,
  604. (uint32_t)spv::Op::OpSGreaterThan,
  605. (uint32_t)spv::Op::OpUGreaterThanEqual,
  606. (uint32_t)spv::Op::OpSGreaterThanEqual,
  607. (uint32_t)spv::Op::OpULessThan,
  608. (uint32_t)spv::Op::OpSLessThan,
  609. (uint32_t)spv::Op::OpULessThanEqual,
  610. (uint32_t)spv::Op::OpSLessThanEqual,
  611. (uint32_t)spv::Op::OpFOrdEqual,
  612. (uint32_t)spv::Op::OpFUnordEqual,
  613. (uint32_t)spv::Op::OpFOrdNotEqual,
  614. (uint32_t)spv::Op::OpFUnordNotEqual,
  615. (uint32_t)spv::Op::OpFOrdLessThan,
  616. (uint32_t)spv::Op::OpFUnordLessThan,
  617. (uint32_t)spv::Op::OpFOrdGreaterThan,
  618. (uint32_t)spv::Op::OpFUnordGreaterThan,
  619. (uint32_t)spv::Op::OpFOrdLessThanEqual,
  620. (uint32_t)spv::Op::OpFUnordLessThanEqual,
  621. (uint32_t)spv::Op::OpFOrdGreaterThanEqual,
  622. (uint32_t)spv::Op::OpFUnordGreaterThanEqual,
  623. (uint32_t)spv::Op::OpShiftRightLogical,
  624. (uint32_t)spv::Op::OpShiftRightArithmetic,
  625. (uint32_t)spv::Op::OpShiftLeftLogical,
  626. (uint32_t)spv::Op::OpBitwiseOr,
  627. (uint32_t)spv::Op::OpBitwiseXor,
  628. (uint32_t)spv::Op::OpBitwiseAnd,
  629. (uint32_t)spv::Op::OpNot,
  630. (uint32_t)spv::Op::OpBitFieldInsert,
  631. (uint32_t)spv::Op::OpBitFieldSExtract,
  632. (uint32_t)spv::Op::OpBitFieldUExtract,
  633. (uint32_t)spv::Op::OpBitReverse,
  634. (uint32_t)spv::Op::OpBitCount,
  635. (uint32_t)spv::Op::OpPhi,
  636. (uint32_t)spv::Op::OpImageSparseSampleImplicitLod,
  637. (uint32_t)spv::Op::OpImageSparseSampleExplicitLod,
  638. (uint32_t)spv::Op::OpImageSparseSampleDrefImplicitLod,
  639. (uint32_t)spv::Op::OpImageSparseSampleDrefExplicitLod,
  640. (uint32_t)spv::Op::OpImageSparseSampleProjImplicitLod,
  641. (uint32_t)spv::Op::OpImageSparseSampleProjExplicitLod,
  642. (uint32_t)spv::Op::OpImageSparseSampleProjDrefImplicitLod,
  643. (uint32_t)spv::Op::OpImageSparseSampleProjDrefExplicitLod,
  644. (uint32_t)spv::Op::OpImageSparseFetch,
  645. (uint32_t)spv::Op::OpImageSparseGather,
  646. (uint32_t)spv::Op::OpImageSparseDrefGather,
  647. (uint32_t)spv::Op::OpImageSparseTexelsResident,
  648. (uint32_t)spv::Op::OpImageSparseRead,
  649. (uint32_t)spv::Op::OpSizeOf});
  650. }
  651. }
  652. void IRContext::AddCombinatorsForExtension(Instruction* extension) {
  653. assert(extension->opcode() == spv::Op::OpExtInstImport &&
  654. "Expecting an import of an extension's instruction set.");
  655. const std::string extension_name = extension->GetInOperand(0).AsString();
  656. if (extension_name == "GLSL.std.450") {
  657. combinator_ops_[extension->result_id()] = {
  658. (uint32_t)GLSLstd450Round,
  659. (uint32_t)GLSLstd450RoundEven,
  660. (uint32_t)GLSLstd450Trunc,
  661. (uint32_t)GLSLstd450FAbs,
  662. (uint32_t)GLSLstd450SAbs,
  663. (uint32_t)GLSLstd450FSign,
  664. (uint32_t)GLSLstd450SSign,
  665. (uint32_t)GLSLstd450Floor,
  666. (uint32_t)GLSLstd450Ceil,
  667. (uint32_t)GLSLstd450Fract,
  668. (uint32_t)GLSLstd450Radians,
  669. (uint32_t)GLSLstd450Degrees,
  670. (uint32_t)GLSLstd450Sin,
  671. (uint32_t)GLSLstd450Cos,
  672. (uint32_t)GLSLstd450Tan,
  673. (uint32_t)GLSLstd450Asin,
  674. (uint32_t)GLSLstd450Acos,
  675. (uint32_t)GLSLstd450Atan,
  676. (uint32_t)GLSLstd450Sinh,
  677. (uint32_t)GLSLstd450Cosh,
  678. (uint32_t)GLSLstd450Tanh,
  679. (uint32_t)GLSLstd450Asinh,
  680. (uint32_t)GLSLstd450Acosh,
  681. (uint32_t)GLSLstd450Atanh,
  682. (uint32_t)GLSLstd450Atan2,
  683. (uint32_t)GLSLstd450Pow,
  684. (uint32_t)GLSLstd450Exp,
  685. (uint32_t)GLSLstd450Log,
  686. (uint32_t)GLSLstd450Exp2,
  687. (uint32_t)GLSLstd450Log2,
  688. (uint32_t)GLSLstd450Sqrt,
  689. (uint32_t)GLSLstd450InverseSqrt,
  690. (uint32_t)GLSLstd450Determinant,
  691. (uint32_t)GLSLstd450MatrixInverse,
  692. (uint32_t)GLSLstd450ModfStruct,
  693. (uint32_t)GLSLstd450FMin,
  694. (uint32_t)GLSLstd450UMin,
  695. (uint32_t)GLSLstd450SMin,
  696. (uint32_t)GLSLstd450FMax,
  697. (uint32_t)GLSLstd450UMax,
  698. (uint32_t)GLSLstd450SMax,
  699. (uint32_t)GLSLstd450FClamp,
  700. (uint32_t)GLSLstd450UClamp,
  701. (uint32_t)GLSLstd450SClamp,
  702. (uint32_t)GLSLstd450FMix,
  703. (uint32_t)GLSLstd450IMix,
  704. (uint32_t)GLSLstd450Step,
  705. (uint32_t)GLSLstd450SmoothStep,
  706. (uint32_t)GLSLstd450Fma,
  707. (uint32_t)GLSLstd450FrexpStruct,
  708. (uint32_t)GLSLstd450Ldexp,
  709. (uint32_t)GLSLstd450PackSnorm4x8,
  710. (uint32_t)GLSLstd450PackUnorm4x8,
  711. (uint32_t)GLSLstd450PackSnorm2x16,
  712. (uint32_t)GLSLstd450PackUnorm2x16,
  713. (uint32_t)GLSLstd450PackHalf2x16,
  714. (uint32_t)GLSLstd450PackDouble2x32,
  715. (uint32_t)GLSLstd450UnpackSnorm2x16,
  716. (uint32_t)GLSLstd450UnpackUnorm2x16,
  717. (uint32_t)GLSLstd450UnpackHalf2x16,
  718. (uint32_t)GLSLstd450UnpackSnorm4x8,
  719. (uint32_t)GLSLstd450UnpackUnorm4x8,
  720. (uint32_t)GLSLstd450UnpackDouble2x32,
  721. (uint32_t)GLSLstd450Length,
  722. (uint32_t)GLSLstd450Distance,
  723. (uint32_t)GLSLstd450Cross,
  724. (uint32_t)GLSLstd450Normalize,
  725. (uint32_t)GLSLstd450FaceForward,
  726. (uint32_t)GLSLstd450Reflect,
  727. (uint32_t)GLSLstd450Refract,
  728. (uint32_t)GLSLstd450FindILsb,
  729. (uint32_t)GLSLstd450FindSMsb,
  730. (uint32_t)GLSLstd450FindUMsb,
  731. (uint32_t)GLSLstd450InterpolateAtCentroid,
  732. (uint32_t)GLSLstd450InterpolateAtSample,
  733. (uint32_t)GLSLstd450InterpolateAtOffset,
  734. (uint32_t)GLSLstd450NMin,
  735. (uint32_t)GLSLstd450NMax,
  736. (uint32_t)GLSLstd450NClamp};
  737. } else {
  738. // Map the result id to the empty set.
  739. combinator_ops_[extension->result_id()];
  740. }
  741. }
  742. void IRContext::InitializeCombinators() {
  743. for (auto capability : get_feature_mgr()->GetCapabilities()) {
  744. AddCombinatorsForCapability(uint32_t(capability));
  745. }
  746. for (auto& extension : module()->ext_inst_imports()) {
  747. AddCombinatorsForExtension(&extension);
  748. }
  749. valid_analyses_ |= kAnalysisCombinators;
  750. }
  751. void IRContext::RemoveFromIdToName(const Instruction* inst) {
  752. if (id_to_name_ && (inst->opcode() == spv::Op::OpName ||
  753. inst->opcode() == spv::Op::OpMemberName)) {
  754. auto range = id_to_name_->equal_range(inst->GetSingleWordInOperand(0));
  755. for (auto it = range.first; it != range.second; ++it) {
  756. if (it->second == inst) {
  757. id_to_name_->erase(it);
  758. break;
  759. }
  760. }
  761. }
  762. }
  763. LoopDescriptor* IRContext::GetLoopDescriptor(const Function* f) {
  764. if (!AreAnalysesValid(kAnalysisLoopAnalysis)) {
  765. ResetLoopAnalysis();
  766. }
  767. std::unordered_map<const Function*, LoopDescriptor>::iterator it =
  768. loop_descriptors_.find(f);
  769. if (it == loop_descriptors_.end()) {
  770. return &loop_descriptors_
  771. .emplace(std::make_pair(f, LoopDescriptor(this, f)))
  772. .first->second;
  773. }
  774. return &it->second;
  775. }
  776. uint32_t IRContext::FindBuiltinInputVar(uint32_t builtin) {
  777. for (auto& a : module_->annotations()) {
  778. if (spv::Op(a.opcode()) != spv::Op::OpDecorate) continue;
  779. if (spv::Decoration(a.GetSingleWordInOperand(
  780. kSpvDecorateDecorationInIdx)) != spv::Decoration::BuiltIn)
  781. continue;
  782. if (a.GetSingleWordInOperand(kSpvDecorateBuiltinInIdx) != builtin) continue;
  783. uint32_t target_id = a.GetSingleWordInOperand(kSpvDecorateTargetIdInIdx);
  784. Instruction* b_var = get_def_use_mgr()->GetDef(target_id);
  785. if (b_var->opcode() != spv::Op::OpVariable) continue;
  786. if (spv::StorageClass(b_var->GetSingleWordInOperand(0)) !=
  787. spv::StorageClass::Input)
  788. continue;
  789. return target_id;
  790. }
  791. return 0;
  792. }
  793. void IRContext::AddVarToEntryPoints(uint32_t var_id) {
  794. uint32_t ocnt = 0;
  795. for (auto& e : module()->entry_points()) {
  796. bool found = false;
  797. e.ForEachInOperand([&ocnt, &found, &var_id](const uint32_t* idp) {
  798. if (ocnt >= kEntryPointInterfaceInIdx) {
  799. if (*idp == var_id) found = true;
  800. }
  801. ++ocnt;
  802. });
  803. if (!found) {
  804. e.AddOperand({SPV_OPERAND_TYPE_ID, {var_id}});
  805. get_def_use_mgr()->AnalyzeInstDefUse(&e);
  806. }
  807. }
  808. }
  809. uint32_t IRContext::GetBuiltinInputVarId(uint32_t builtin) {
  810. if (!AreAnalysesValid(kAnalysisBuiltinVarId)) ResetBuiltinAnalysis();
  811. // If cached, return it.
  812. std::unordered_map<uint32_t, uint32_t>::iterator it =
  813. builtin_var_id_map_.find(builtin);
  814. if (it != builtin_var_id_map_.end()) return it->second;
  815. // Look for one in shader
  816. uint32_t var_id = FindBuiltinInputVar(builtin);
  817. if (var_id == 0) {
  818. // If not found, create it
  819. // TODO(greg-lunarg): Add support for all builtins
  820. analysis::TypeManager* type_mgr = get_type_mgr();
  821. analysis::Type* reg_type;
  822. switch (spv::BuiltIn(builtin)) {
  823. case spv::BuiltIn::FragCoord: {
  824. analysis::Float float_ty(32);
  825. analysis::Type* reg_float_ty = type_mgr->GetRegisteredType(&float_ty);
  826. analysis::Vector v4float_ty(reg_float_ty, 4);
  827. reg_type = type_mgr->GetRegisteredType(&v4float_ty);
  828. break;
  829. }
  830. case spv::BuiltIn::VertexIndex:
  831. case spv::BuiltIn::InstanceIndex:
  832. case spv::BuiltIn::PrimitiveId:
  833. case spv::BuiltIn::InvocationId:
  834. case spv::BuiltIn::SubgroupLocalInvocationId: {
  835. analysis::Integer uint_ty(32, false);
  836. reg_type = type_mgr->GetRegisteredType(&uint_ty);
  837. break;
  838. }
  839. case spv::BuiltIn::GlobalInvocationId:
  840. case spv::BuiltIn::LaunchIdNV: {
  841. analysis::Integer uint_ty(32, false);
  842. analysis::Type* reg_uint_ty = type_mgr->GetRegisteredType(&uint_ty);
  843. analysis::Vector v3uint_ty(reg_uint_ty, 3);
  844. reg_type = type_mgr->GetRegisteredType(&v3uint_ty);
  845. break;
  846. }
  847. case spv::BuiltIn::TessCoord: {
  848. analysis::Float float_ty(32);
  849. analysis::Type* reg_float_ty = type_mgr->GetRegisteredType(&float_ty);
  850. analysis::Vector v3float_ty(reg_float_ty, 3);
  851. reg_type = type_mgr->GetRegisteredType(&v3float_ty);
  852. break;
  853. }
  854. case spv::BuiltIn::SubgroupLtMask: {
  855. analysis::Integer uint_ty(32, false);
  856. analysis::Type* reg_uint_ty = type_mgr->GetRegisteredType(&uint_ty);
  857. analysis::Vector v4uint_ty(reg_uint_ty, 4);
  858. reg_type = type_mgr->GetRegisteredType(&v4uint_ty);
  859. break;
  860. }
  861. default: {
  862. assert(false && "unhandled builtin");
  863. return 0;
  864. }
  865. }
  866. if (reg_type == nullptr) return 0; // Error
  867. uint32_t type_id = type_mgr->GetTypeInstruction(reg_type);
  868. uint32_t varTyPtrId =
  869. type_mgr->FindPointerToType(type_id, spv::StorageClass::Input);
  870. var_id = TakeNextId();
  871. if (var_id == 0) return 0; // Error
  872. std::unique_ptr<Instruction> newVarOp(
  873. new Instruction(this, spv::Op::OpVariable, varTyPtrId, var_id,
  874. {{spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER,
  875. {uint32_t(spv::StorageClass::Input)}}}));
  876. get_def_use_mgr()->AnalyzeInstDefUse(&*newVarOp);
  877. module()->AddGlobalValue(std::move(newVarOp));
  878. get_decoration_mgr()->AddDecorationVal(
  879. var_id, uint32_t(spv::Decoration::BuiltIn), builtin);
  880. AddVarToEntryPoints(var_id);
  881. }
  882. builtin_var_id_map_[builtin] = var_id;
  883. return var_id;
  884. }
  885. void IRContext::AddCalls(const Function* func, std::queue<uint32_t>* todo) {
  886. for (auto bi = func->begin(); bi != func->end(); ++bi)
  887. for (auto ii = bi->begin(); ii != bi->end(); ++ii) {
  888. if (ii->opcode() == spv::Op::OpFunctionCall)
  889. todo->push(ii->GetSingleWordInOperand(0));
  890. if (ii->opcode() == spv::Op::OpCooperativeMatrixPerElementOpNV)
  891. todo->push(ii->GetSingleWordInOperand(1));
  892. if (ii->opcode() == spv::Op::OpCooperativeMatrixReduceNV)
  893. todo->push(ii->GetSingleWordInOperand(2));
  894. if (ii->opcode() == spv::Op::OpCooperativeMatrixLoadTensorNV) {
  895. const auto memory_operands_index = 3;
  896. auto mask = ii->GetSingleWordInOperand(memory_operands_index);
  897. uint32_t count = 1;
  898. if (mask & uint32_t(spv::MemoryAccessMask::Aligned)) ++count;
  899. if (mask & uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR))
  900. ++count;
  901. if (mask & uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR))
  902. ++count;
  903. const auto tensor_operands_index = memory_operands_index + count;
  904. mask = ii->GetSingleWordInOperand(tensor_operands_index);
  905. count = 1;
  906. if (mask & uint32_t(spv::TensorAddressingOperandsMask::TensorView))
  907. ++count;
  908. if (mask & uint32_t(spv::TensorAddressingOperandsMask::DecodeFunc)) {
  909. todo->push(ii->GetSingleWordInOperand(tensor_operands_index + count));
  910. }
  911. }
  912. }
  913. }
  914. bool IRContext::ProcessEntryPointCallTree(ProcessFunction& pfn) {
  915. // Collect all of the entry points as the roots.
  916. std::queue<uint32_t> roots;
  917. for (auto& e : module()->entry_points()) {
  918. roots.push(e.GetSingleWordInOperand(kEntryPointFunctionIdInIdx));
  919. }
  920. return ProcessCallTreeFromRoots(pfn, &roots);
  921. }
  922. bool IRContext::ProcessReachableCallTree(ProcessFunction& pfn) {
  923. std::queue<uint32_t> roots;
  924. // Add all entry points since they can be reached from outside the module.
  925. for (auto& e : module()->entry_points())
  926. roots.push(e.GetSingleWordInOperand(kEntryPointFunctionIdInIdx));
  927. // Add all exported functions since they can be reached from outside the
  928. // module.
  929. for (auto& a : annotations()) {
  930. // TODO: Handle group decorations as well. Currently not generate by any
  931. // front-end, but could be coming.
  932. if (a.opcode() == spv::Op::OpDecorate) {
  933. if (spv::Decoration(a.GetSingleWordOperand(1)) ==
  934. spv::Decoration::LinkageAttributes) {
  935. uint32_t lastOperand = a.NumOperands() - 1;
  936. if (spv::LinkageType(a.GetSingleWordOperand(lastOperand)) ==
  937. spv::LinkageType::Export) {
  938. uint32_t id = a.GetSingleWordOperand(0);
  939. if (GetFunction(id)) {
  940. roots.push(id);
  941. }
  942. }
  943. }
  944. }
  945. }
  946. return ProcessCallTreeFromRoots(pfn, &roots);
  947. }
  948. bool IRContext::ProcessCallTreeFromRoots(ProcessFunction& pfn,
  949. std::queue<uint32_t>* roots) {
  950. // Process call tree
  951. bool modified = false;
  952. std::unordered_set<uint32_t> done;
  953. while (!roots->empty()) {
  954. const uint32_t fi = roots->front();
  955. roots->pop();
  956. if (done.insert(fi).second) {
  957. Function* fn = GetFunction(fi);
  958. assert(fn && "Trying to process a function that does not exist.");
  959. modified = pfn(fn) || modified;
  960. AddCalls(fn, roots);
  961. }
  962. }
  963. return modified;
  964. }
  965. void IRContext::CollectCallTreeFromRoots(unsigned entryId,
  966. std::unordered_set<uint32_t>* funcs) {
  967. std::queue<uint32_t> roots;
  968. roots.push(entryId);
  969. while (!roots.empty()) {
  970. const uint32_t fi = roots.front();
  971. roots.pop();
  972. funcs->insert(fi);
  973. Function* fn = GetFunction(fi);
  974. AddCalls(fn, &roots);
  975. }
  976. }
  977. void IRContext::EmitErrorMessage(std::string message, Instruction* inst) {
  978. if (!consumer()) {
  979. return;
  980. }
  981. Instruction* line_inst = inst;
  982. while (line_inst != nullptr) { // Stop at the beginning of the basic block.
  983. if (!line_inst->dbg_line_insts().empty()) {
  984. line_inst = &line_inst->dbg_line_insts().back();
  985. if (line_inst->IsNoLine()) {
  986. line_inst = nullptr;
  987. }
  988. break;
  989. }
  990. line_inst = line_inst->PreviousNode();
  991. }
  992. uint32_t line_number = 0;
  993. uint32_t col_number = 0;
  994. std::string source;
  995. if (line_inst != nullptr) {
  996. Instruction* file_name =
  997. get_def_use_mgr()->GetDef(line_inst->GetSingleWordInOperand(0));
  998. source = file_name->GetInOperand(0).AsString();
  999. // Get the line number and column number.
  1000. line_number = line_inst->GetSingleWordInOperand(1);
  1001. col_number = line_inst->GetSingleWordInOperand(2);
  1002. }
  1003. message +=
  1004. "\n " + inst->PrettyPrint(SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
  1005. consumer()(SPV_MSG_ERROR, source.c_str(), {line_number, col_number, 0},
  1006. message.c_str());
  1007. }
  1008. // Gets the dominator analysis for function |f|.
  1009. DominatorAnalysis* IRContext::GetDominatorAnalysis(const Function* f) {
  1010. if (!AreAnalysesValid(kAnalysisDominatorAnalysis)) {
  1011. ResetDominatorAnalysis();
  1012. }
  1013. if (dominator_trees_.find(f) == dominator_trees_.end()) {
  1014. dominator_trees_[f].InitializeTree(*cfg(), f);
  1015. }
  1016. return &dominator_trees_[f];
  1017. }
  1018. // Gets the postdominator analysis for function |f|.
  1019. PostDominatorAnalysis* IRContext::GetPostDominatorAnalysis(const Function* f) {
  1020. if (!AreAnalysesValid(kAnalysisDominatorAnalysis)) {
  1021. ResetDominatorAnalysis();
  1022. }
  1023. if (post_dominator_trees_.find(f) == post_dominator_trees_.end()) {
  1024. post_dominator_trees_[f].InitializeTree(*cfg(), f);
  1025. }
  1026. return &post_dominator_trees_[f];
  1027. }
  1028. bool IRContext::CheckCFG() {
  1029. std::unordered_map<uint32_t, std::vector<uint32_t>> real_preds;
  1030. if (!AreAnalysesValid(kAnalysisCFG)) {
  1031. return true;
  1032. }
  1033. for (Function& function : *module()) {
  1034. for (const auto& bb : function) {
  1035. bb.ForEachSuccessorLabel([&bb, &real_preds](const uint32_t lab_id) {
  1036. real_preds[lab_id].push_back(bb.id());
  1037. });
  1038. }
  1039. for (auto& bb : function) {
  1040. std::vector<uint32_t> preds = cfg()->preds(bb.id());
  1041. std::vector<uint32_t> real = real_preds[bb.id()];
  1042. std::sort(preds.begin(), preds.end());
  1043. std::sort(real.begin(), real.end());
  1044. bool same = true;
  1045. if (preds.size() != real.size()) {
  1046. same = false;
  1047. }
  1048. for (size_t i = 0; i < real.size() && same; i++) {
  1049. if (preds[i] != real[i]) {
  1050. same = false;
  1051. }
  1052. }
  1053. if (!same) {
  1054. std::cerr << "Predecessors for " << bb.id() << " are different:\n";
  1055. std::cerr << "Real:";
  1056. for (uint32_t i : real) {
  1057. std::cerr << ' ' << i;
  1058. }
  1059. std::cerr << std::endl;
  1060. std::cerr << "Recorded:";
  1061. for (uint32_t i : preds) {
  1062. std::cerr << ' ' << i;
  1063. }
  1064. std::cerr << std::endl;
  1065. }
  1066. if (!same) return false;
  1067. }
  1068. }
  1069. return true;
  1070. }
  1071. bool IRContext::IsReachable(const opt::BasicBlock& bb) {
  1072. auto enclosing_function = bb.GetParent();
  1073. return GetDominatorAnalysis(enclosing_function)
  1074. ->Dominates(enclosing_function->entry().get(), &bb);
  1075. }
  1076. spv::ExecutionModel IRContext::GetStage() {
  1077. const auto& entry_points = module()->entry_points();
  1078. if (entry_points.empty()) {
  1079. return spv::ExecutionModel::Max;
  1080. }
  1081. uint32_t stage = entry_points.begin()->GetSingleWordInOperand(
  1082. kEntryPointExecutionModelInIdx);
  1083. auto it = std::find_if(
  1084. entry_points.begin(), entry_points.end(), [stage](const Instruction& x) {
  1085. return x.GetSingleWordInOperand(kEntryPointExecutionModelInIdx) !=
  1086. stage;
  1087. });
  1088. if (it != entry_points.end()) {
  1089. EmitErrorMessage("Mixed stage shader module not supported", &(*it));
  1090. }
  1091. return static_cast<spv::ExecutionModel>(stage);
  1092. }
  1093. } // namespace opt
  1094. } // namespace spvtools