ir_context.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. // Copyright (c) 2017 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/opt/ir_context.h"
  15. #include <cstring>
  16. #include "OpenCLDebugInfo100.h"
  17. #include "source/latest_version_glsl_std_450_header.h"
  18. #include "source/opt/log.h"
  19. #include "source/opt/mem_pass.h"
  20. #include "source/opt/reflect.h"
  21. namespace {
  22. static const int kSpvDecorateTargetIdInIdx = 0;
  23. static const int kSpvDecorateDecorationInIdx = 1;
  24. static const int kSpvDecorateBuiltinInIdx = 2;
  25. static const int kEntryPointInterfaceInIdx = 3;
  26. static const int kEntryPointFunctionIdInIdx = 1;
  27. // Constants for OpenCL.DebugInfo.100 / NonSemantic.Vulkan.DebugInfo.100
  28. // extension instructions.
  29. static const uint32_t kDebugFunctionOperandFunctionIndex = 13;
  30. static const uint32_t kDebugGlobalVariableOperandVariableIndex = 11;
  31. } // anonymous namespace
  32. namespace spvtools {
  33. namespace opt {
  34. void IRContext::BuildInvalidAnalyses(IRContext::Analysis set) {
  35. if (set & kAnalysisDefUse) {
  36. BuildDefUseManager();
  37. }
  38. if (set & kAnalysisInstrToBlockMapping) {
  39. BuildInstrToBlockMapping();
  40. }
  41. if (set & kAnalysisDecorations) {
  42. BuildDecorationManager();
  43. }
  44. if (set & kAnalysisCFG) {
  45. BuildCFG();
  46. }
  47. if (set & kAnalysisDominatorAnalysis) {
  48. ResetDominatorAnalysis();
  49. }
  50. if (set & kAnalysisLoopAnalysis) {
  51. ResetLoopAnalysis();
  52. }
  53. if (set & kAnalysisBuiltinVarId) {
  54. ResetBuiltinAnalysis();
  55. }
  56. if (set & kAnalysisNameMap) {
  57. BuildIdToNameMap();
  58. }
  59. if (set & kAnalysisScalarEvolution) {
  60. BuildScalarEvolutionAnalysis();
  61. }
  62. if (set & kAnalysisRegisterPressure) {
  63. BuildRegPressureAnalysis();
  64. }
  65. if (set & kAnalysisValueNumberTable) {
  66. BuildValueNumberTable();
  67. }
  68. if (set & kAnalysisStructuredCFG) {
  69. BuildStructuredCFGAnalysis();
  70. }
  71. if (set & kAnalysisIdToFuncMapping) {
  72. BuildIdToFuncMapping();
  73. }
  74. if (set & kAnalysisConstants) {
  75. BuildConstantManager();
  76. }
  77. if (set & kAnalysisTypes) {
  78. BuildTypeManager();
  79. }
  80. if (set & kAnalysisDebugInfo) {
  81. BuildDebugInfoManager();
  82. }
  83. }
  84. void IRContext::InvalidateAnalysesExceptFor(
  85. IRContext::Analysis preserved_analyses) {
  86. uint32_t analyses_to_invalidate = valid_analyses_ & (~preserved_analyses);
  87. InvalidateAnalyses(static_cast<IRContext::Analysis>(analyses_to_invalidate));
  88. }
  89. void IRContext::InvalidateAnalyses(IRContext::Analysis analyses_to_invalidate) {
  90. // The ConstantManager and DebugInfoManager contain Type pointers. If the
  91. // TypeManager goes away, the ConstantManager and DebugInfoManager have to
  92. // go away.
  93. if (analyses_to_invalidate & kAnalysisTypes) {
  94. analyses_to_invalidate |= kAnalysisConstants;
  95. analyses_to_invalidate |= kAnalysisDebugInfo;
  96. }
  97. // The dominator analysis hold the psuedo entry and exit nodes from the CFG.
  98. // Also if the CFG change the dominators many changed as well, so the
  99. // dominator analysis should be invalidated as well.
  100. if (analyses_to_invalidate & kAnalysisCFG) {
  101. analyses_to_invalidate |= kAnalysisDominatorAnalysis;
  102. }
  103. if (analyses_to_invalidate & kAnalysisDefUse) {
  104. def_use_mgr_.reset(nullptr);
  105. }
  106. if (analyses_to_invalidate & kAnalysisInstrToBlockMapping) {
  107. instr_to_block_.clear();
  108. }
  109. if (analyses_to_invalidate & kAnalysisDecorations) {
  110. decoration_mgr_.reset(nullptr);
  111. }
  112. if (analyses_to_invalidate & kAnalysisCombinators) {
  113. combinator_ops_.clear();
  114. }
  115. if (analyses_to_invalidate & kAnalysisBuiltinVarId) {
  116. builtin_var_id_map_.clear();
  117. }
  118. if (analyses_to_invalidate & kAnalysisCFG) {
  119. cfg_.reset(nullptr);
  120. }
  121. if (analyses_to_invalidate & kAnalysisDominatorAnalysis) {
  122. dominator_trees_.clear();
  123. post_dominator_trees_.clear();
  124. }
  125. if (analyses_to_invalidate & kAnalysisNameMap) {
  126. id_to_name_.reset(nullptr);
  127. }
  128. if (analyses_to_invalidate & kAnalysisValueNumberTable) {
  129. vn_table_.reset(nullptr);
  130. }
  131. if (analyses_to_invalidate & kAnalysisStructuredCFG) {
  132. struct_cfg_analysis_.reset(nullptr);
  133. }
  134. if (analyses_to_invalidate & kAnalysisIdToFuncMapping) {
  135. id_to_func_.clear();
  136. }
  137. if (analyses_to_invalidate & kAnalysisConstants) {
  138. constant_mgr_.reset(nullptr);
  139. }
  140. if (analyses_to_invalidate & kAnalysisTypes) {
  141. type_mgr_.reset(nullptr);
  142. }
  143. if (analyses_to_invalidate & kAnalysisDebugInfo) {
  144. debug_info_mgr_.reset(nullptr);
  145. }
  146. valid_analyses_ = Analysis(valid_analyses_ & ~analyses_to_invalidate);
  147. }
  148. Instruction* IRContext::KillInst(Instruction* inst) {
  149. if (!inst) {
  150. return nullptr;
  151. }
  152. KillNamesAndDecorates(inst);
  153. KillOperandFromDebugInstructions(inst);
  154. if (AreAnalysesValid(kAnalysisDefUse)) {
  155. get_def_use_mgr()->ClearInst(inst);
  156. }
  157. if (AreAnalysesValid(kAnalysisInstrToBlockMapping)) {
  158. instr_to_block_.erase(inst);
  159. }
  160. if (AreAnalysesValid(kAnalysisDecorations)) {
  161. if (inst->IsDecoration()) {
  162. decoration_mgr_->RemoveDecoration(inst);
  163. }
  164. }
  165. if (AreAnalysesValid(kAnalysisDebugInfo)) {
  166. get_debug_info_mgr()->ClearDebugScopeAndInlinedAtUses(inst);
  167. get_debug_info_mgr()->ClearDebugInfo(inst);
  168. }
  169. if (type_mgr_ && IsTypeInst(inst->opcode())) {
  170. type_mgr_->RemoveId(inst->result_id());
  171. }
  172. if (constant_mgr_ && IsConstantInst(inst->opcode())) {
  173. constant_mgr_->RemoveId(inst->result_id());
  174. }
  175. if (inst->opcode() == SpvOpCapability || inst->opcode() == SpvOpExtension) {
  176. // We reset the feature manager, instead of updating it, because it is just
  177. // as much work. We would have to remove all capabilities implied by this
  178. // capability that are not also implied by the remaining OpCapability
  179. // instructions. We could update extensions, but we will see if it is
  180. // needed.
  181. ResetFeatureManager();
  182. }
  183. RemoveFromIdToName(inst);
  184. Instruction* next_instruction = nullptr;
  185. if (inst->IsInAList()) {
  186. next_instruction = inst->NextNode();
  187. inst->RemoveFromList();
  188. delete inst;
  189. } else {
  190. // Needed for instructions that are not part of a list like OpLabels,
  191. // OpFunction, OpFunctionEnd, etc..
  192. inst->ToNop();
  193. }
  194. return next_instruction;
  195. }
  196. void IRContext::CollectNonSemanticTree(
  197. Instruction* inst, std::unordered_set<Instruction*>* to_kill) {
  198. if (!inst->HasResultId()) return;
  199. std::vector<Instruction*> work_list;
  200. std::unordered_set<Instruction*> seen;
  201. work_list.push_back(inst);
  202. while (!work_list.empty()) {
  203. auto* i = work_list.back();
  204. work_list.pop_back();
  205. get_def_use_mgr()->ForEachUser(
  206. i, [&work_list, to_kill, &seen](Instruction* user) {
  207. if (user->IsNonSemanticInstruction() && seen.insert(user).second) {
  208. work_list.push_back(user);
  209. to_kill->insert(user);
  210. }
  211. });
  212. }
  213. }
  214. bool IRContext::KillDef(uint32_t id) {
  215. Instruction* def = get_def_use_mgr()->GetDef(id);
  216. if (def != nullptr) {
  217. KillInst(def);
  218. return true;
  219. }
  220. return false;
  221. }
  222. bool IRContext::ReplaceAllUsesWith(uint32_t before, uint32_t after) {
  223. return ReplaceAllUsesWithPredicate(before, after,
  224. [](Instruction*) { return true; });
  225. }
  226. bool IRContext::ReplaceAllUsesWithPredicate(
  227. uint32_t before, uint32_t after,
  228. const std::function<bool(Instruction*)>& predicate) {
  229. if (before == after) return false;
  230. if (AreAnalysesValid(kAnalysisDebugInfo)) {
  231. get_debug_info_mgr()->ReplaceAllUsesInDebugScopeWithPredicate(before, after,
  232. predicate);
  233. }
  234. // Ensure that |after| has been registered as def.
  235. assert(get_def_use_mgr()->GetDef(after) &&
  236. "'after' is not a registered def.");
  237. std::vector<std::pair<Instruction*, uint32_t>> uses_to_update;
  238. get_def_use_mgr()->ForEachUse(
  239. before, [&predicate, &uses_to_update](Instruction* user, uint32_t index) {
  240. if (predicate(user)) {
  241. uses_to_update.emplace_back(user, index);
  242. }
  243. });
  244. Instruction* prev = nullptr;
  245. for (auto p : uses_to_update) {
  246. Instruction* user = p.first;
  247. uint32_t index = p.second;
  248. if (prev == nullptr || prev != user) {
  249. ForgetUses(user);
  250. prev = user;
  251. }
  252. const uint32_t type_result_id_count =
  253. (user->result_id() != 0) + (user->type_id() != 0);
  254. if (index < type_result_id_count) {
  255. // Update the type_id. Note that result id is immutable so it should
  256. // never be updated.
  257. if (user->type_id() != 0 && index == 0) {
  258. user->SetResultType(after);
  259. } else if (user->type_id() == 0) {
  260. SPIRV_ASSERT(consumer_, false,
  261. "Result type id considered as use while the instruction "
  262. "doesn't have a result type id.");
  263. (void)consumer_; // Makes the compiler happy for release build.
  264. } else {
  265. SPIRV_ASSERT(consumer_, false,
  266. "Trying setting the immutable result id.");
  267. }
  268. } else {
  269. // Update an in-operand.
  270. uint32_t in_operand_pos = index - type_result_id_count;
  271. // Make the modification in the instruction.
  272. user->SetInOperand(in_operand_pos, {after});
  273. }
  274. AnalyzeUses(user);
  275. }
  276. return true;
  277. }
  278. bool IRContext::IsConsistent() {
  279. #ifndef SPIRV_CHECK_CONTEXT
  280. return true;
  281. #else
  282. if (AreAnalysesValid(kAnalysisDefUse)) {
  283. analysis::DefUseManager new_def_use(module());
  284. if (*get_def_use_mgr() != new_def_use) {
  285. return false;
  286. }
  287. }
  288. if (AreAnalysesValid(kAnalysisIdToFuncMapping)) {
  289. for (auto& fn : *module_) {
  290. if (id_to_func_[fn.result_id()] != &fn) {
  291. return false;
  292. }
  293. }
  294. }
  295. if (AreAnalysesValid(kAnalysisInstrToBlockMapping)) {
  296. for (auto& func : *module()) {
  297. for (auto& block : func) {
  298. if (!block.WhileEachInst([this, &block](Instruction* inst) {
  299. if (get_instr_block(inst) != &block) {
  300. return false;
  301. }
  302. return true;
  303. }))
  304. return false;
  305. }
  306. }
  307. }
  308. if (!CheckCFG()) {
  309. return false;
  310. }
  311. if (AreAnalysesValid(kAnalysisDecorations)) {
  312. analysis::DecorationManager* dec_mgr = get_decoration_mgr();
  313. analysis::DecorationManager current(module());
  314. if (*dec_mgr != current) {
  315. return false;
  316. }
  317. }
  318. if (feature_mgr_ != nullptr) {
  319. FeatureManager current(grammar_);
  320. current.Analyze(module());
  321. if (current != *feature_mgr_) {
  322. return false;
  323. }
  324. }
  325. return true;
  326. #endif
  327. }
  328. void IRContext::ForgetUses(Instruction* inst) {
  329. if (AreAnalysesValid(kAnalysisDefUse)) {
  330. get_def_use_mgr()->EraseUseRecordsOfOperandIds(inst);
  331. }
  332. if (AreAnalysesValid(kAnalysisDecorations)) {
  333. if (inst->IsDecoration()) {
  334. get_decoration_mgr()->RemoveDecoration(inst);
  335. }
  336. }
  337. if (AreAnalysesValid(kAnalysisDebugInfo)) {
  338. get_debug_info_mgr()->ClearDebugInfo(inst);
  339. }
  340. RemoveFromIdToName(inst);
  341. }
  342. void IRContext::AnalyzeUses(Instruction* inst) {
  343. if (AreAnalysesValid(kAnalysisDefUse)) {
  344. get_def_use_mgr()->AnalyzeInstUse(inst);
  345. }
  346. if (AreAnalysesValid(kAnalysisDecorations)) {
  347. if (inst->IsDecoration()) {
  348. get_decoration_mgr()->AddDecoration(inst);
  349. }
  350. }
  351. if (AreAnalysesValid(kAnalysisDebugInfo)) {
  352. get_debug_info_mgr()->AnalyzeDebugInst(inst);
  353. }
  354. if (id_to_name_ &&
  355. (inst->opcode() == SpvOpName || inst->opcode() == SpvOpMemberName)) {
  356. id_to_name_->insert({inst->GetSingleWordInOperand(0), inst});
  357. }
  358. }
  359. void IRContext::KillNamesAndDecorates(uint32_t id) {
  360. analysis::DecorationManager* dec_mgr = get_decoration_mgr();
  361. dec_mgr->RemoveDecorationsFrom(id);
  362. std::vector<Instruction*> name_to_kill;
  363. for (auto name : GetNames(id)) {
  364. name_to_kill.push_back(name.second);
  365. }
  366. for (Instruction* name_inst : name_to_kill) {
  367. KillInst(name_inst);
  368. }
  369. }
  370. void IRContext::KillNamesAndDecorates(Instruction* inst) {
  371. const uint32_t rId = inst->result_id();
  372. if (rId == 0) return;
  373. KillNamesAndDecorates(rId);
  374. }
  375. void IRContext::KillOperandFromDebugInstructions(Instruction* inst) {
  376. const auto opcode = inst->opcode();
  377. const uint32_t id = inst->result_id();
  378. // Kill id of OpFunction from DebugFunction.
  379. if (opcode == SpvOpFunction) {
  380. for (auto it = module()->ext_inst_debuginfo_begin();
  381. it != module()->ext_inst_debuginfo_end(); ++it) {
  382. if (it->GetOpenCL100DebugOpcode() != OpenCLDebugInfo100DebugFunction)
  383. continue;
  384. auto& operand = it->GetOperand(kDebugFunctionOperandFunctionIndex);
  385. if (operand.words[0] == id) {
  386. operand.words[0] =
  387. get_debug_info_mgr()->GetDebugInfoNone()->result_id();
  388. get_def_use_mgr()->AnalyzeInstUse(&*it);
  389. }
  390. }
  391. }
  392. // Kill id of OpVariable for global variable from DebugGlobalVariable.
  393. if (opcode == SpvOpVariable || IsConstantInst(opcode)) {
  394. for (auto it = module()->ext_inst_debuginfo_begin();
  395. it != module()->ext_inst_debuginfo_end(); ++it) {
  396. if (it->GetCommonDebugOpcode() != CommonDebugInfoDebugGlobalVariable)
  397. continue;
  398. auto& operand = it->GetOperand(kDebugGlobalVariableOperandVariableIndex);
  399. if (operand.words[0] == id) {
  400. operand.words[0] =
  401. get_debug_info_mgr()->GetDebugInfoNone()->result_id();
  402. get_def_use_mgr()->AnalyzeInstUse(&*it);
  403. }
  404. }
  405. }
  406. }
  407. void IRContext::AddCombinatorsForCapability(uint32_t capability) {
  408. if (capability == SpvCapabilityShader) {
  409. combinator_ops_[0].insert({SpvOpNop,
  410. SpvOpUndef,
  411. SpvOpConstant,
  412. SpvOpConstantTrue,
  413. SpvOpConstantFalse,
  414. SpvOpConstantComposite,
  415. SpvOpConstantSampler,
  416. SpvOpConstantNull,
  417. SpvOpTypeVoid,
  418. SpvOpTypeBool,
  419. SpvOpTypeInt,
  420. SpvOpTypeFloat,
  421. SpvOpTypeVector,
  422. SpvOpTypeMatrix,
  423. SpvOpTypeImage,
  424. SpvOpTypeSampler,
  425. SpvOpTypeSampledImage,
  426. SpvOpTypeAccelerationStructureNV,
  427. SpvOpTypeAccelerationStructureKHR,
  428. SpvOpTypeRayQueryKHR,
  429. SpvOpTypeArray,
  430. SpvOpTypeRuntimeArray,
  431. SpvOpTypeStruct,
  432. SpvOpTypeOpaque,
  433. SpvOpTypePointer,
  434. SpvOpTypeFunction,
  435. SpvOpTypeEvent,
  436. SpvOpTypeDeviceEvent,
  437. SpvOpTypeReserveId,
  438. SpvOpTypeQueue,
  439. SpvOpTypePipe,
  440. SpvOpTypeForwardPointer,
  441. SpvOpVariable,
  442. SpvOpImageTexelPointer,
  443. SpvOpLoad,
  444. SpvOpAccessChain,
  445. SpvOpInBoundsAccessChain,
  446. SpvOpArrayLength,
  447. SpvOpVectorExtractDynamic,
  448. SpvOpVectorInsertDynamic,
  449. SpvOpVectorShuffle,
  450. SpvOpCompositeConstruct,
  451. SpvOpCompositeExtract,
  452. SpvOpCompositeInsert,
  453. SpvOpCopyObject,
  454. SpvOpTranspose,
  455. SpvOpSampledImage,
  456. SpvOpImageSampleImplicitLod,
  457. SpvOpImageSampleExplicitLod,
  458. SpvOpImageSampleDrefImplicitLod,
  459. SpvOpImageSampleDrefExplicitLod,
  460. SpvOpImageSampleProjImplicitLod,
  461. SpvOpImageSampleProjExplicitLod,
  462. SpvOpImageSampleProjDrefImplicitLod,
  463. SpvOpImageSampleProjDrefExplicitLod,
  464. SpvOpImageFetch,
  465. SpvOpImageGather,
  466. SpvOpImageDrefGather,
  467. SpvOpImageRead,
  468. SpvOpImage,
  469. SpvOpImageQueryFormat,
  470. SpvOpImageQueryOrder,
  471. SpvOpImageQuerySizeLod,
  472. SpvOpImageQuerySize,
  473. SpvOpImageQueryLevels,
  474. SpvOpImageQuerySamples,
  475. SpvOpConvertFToU,
  476. SpvOpConvertFToS,
  477. SpvOpConvertSToF,
  478. SpvOpConvertUToF,
  479. SpvOpUConvert,
  480. SpvOpSConvert,
  481. SpvOpFConvert,
  482. SpvOpQuantizeToF16,
  483. SpvOpBitcast,
  484. SpvOpSNegate,
  485. SpvOpFNegate,
  486. SpvOpIAdd,
  487. SpvOpFAdd,
  488. SpvOpISub,
  489. SpvOpFSub,
  490. SpvOpIMul,
  491. SpvOpFMul,
  492. SpvOpUDiv,
  493. SpvOpSDiv,
  494. SpvOpFDiv,
  495. SpvOpUMod,
  496. SpvOpSRem,
  497. SpvOpSMod,
  498. SpvOpFRem,
  499. SpvOpFMod,
  500. SpvOpVectorTimesScalar,
  501. SpvOpMatrixTimesScalar,
  502. SpvOpVectorTimesMatrix,
  503. SpvOpMatrixTimesVector,
  504. SpvOpMatrixTimesMatrix,
  505. SpvOpOuterProduct,
  506. SpvOpDot,
  507. SpvOpIAddCarry,
  508. SpvOpISubBorrow,
  509. SpvOpUMulExtended,
  510. SpvOpSMulExtended,
  511. SpvOpAny,
  512. SpvOpAll,
  513. SpvOpIsNan,
  514. SpvOpIsInf,
  515. SpvOpLogicalEqual,
  516. SpvOpLogicalNotEqual,
  517. SpvOpLogicalOr,
  518. SpvOpLogicalAnd,
  519. SpvOpLogicalNot,
  520. SpvOpSelect,
  521. SpvOpIEqual,
  522. SpvOpINotEqual,
  523. SpvOpUGreaterThan,
  524. SpvOpSGreaterThan,
  525. SpvOpUGreaterThanEqual,
  526. SpvOpSGreaterThanEqual,
  527. SpvOpULessThan,
  528. SpvOpSLessThan,
  529. SpvOpULessThanEqual,
  530. SpvOpSLessThanEqual,
  531. SpvOpFOrdEqual,
  532. SpvOpFUnordEqual,
  533. SpvOpFOrdNotEqual,
  534. SpvOpFUnordNotEqual,
  535. SpvOpFOrdLessThan,
  536. SpvOpFUnordLessThan,
  537. SpvOpFOrdGreaterThan,
  538. SpvOpFUnordGreaterThan,
  539. SpvOpFOrdLessThanEqual,
  540. SpvOpFUnordLessThanEqual,
  541. SpvOpFOrdGreaterThanEqual,
  542. SpvOpFUnordGreaterThanEqual,
  543. SpvOpShiftRightLogical,
  544. SpvOpShiftRightArithmetic,
  545. SpvOpShiftLeftLogical,
  546. SpvOpBitwiseOr,
  547. SpvOpBitwiseXor,
  548. SpvOpBitwiseAnd,
  549. SpvOpNot,
  550. SpvOpBitFieldInsert,
  551. SpvOpBitFieldSExtract,
  552. SpvOpBitFieldUExtract,
  553. SpvOpBitReverse,
  554. SpvOpBitCount,
  555. SpvOpPhi,
  556. SpvOpImageSparseSampleImplicitLod,
  557. SpvOpImageSparseSampleExplicitLod,
  558. SpvOpImageSparseSampleDrefImplicitLod,
  559. SpvOpImageSparseSampleDrefExplicitLod,
  560. SpvOpImageSparseSampleProjImplicitLod,
  561. SpvOpImageSparseSampleProjExplicitLod,
  562. SpvOpImageSparseSampleProjDrefImplicitLod,
  563. SpvOpImageSparseSampleProjDrefExplicitLod,
  564. SpvOpImageSparseFetch,
  565. SpvOpImageSparseGather,
  566. SpvOpImageSparseDrefGather,
  567. SpvOpImageSparseTexelsResident,
  568. SpvOpImageSparseRead,
  569. SpvOpSizeOf});
  570. }
  571. }
  572. void IRContext::AddCombinatorsForExtension(Instruction* extension) {
  573. assert(extension->opcode() == SpvOpExtInstImport &&
  574. "Expecting an import of an extension's instruction set.");
  575. const char* extension_name =
  576. reinterpret_cast<const char*>(&extension->GetInOperand(0).words[0]);
  577. if (!strcmp(extension_name, "GLSL.std.450")) {
  578. combinator_ops_[extension->result_id()] = {GLSLstd450Round,
  579. GLSLstd450RoundEven,
  580. GLSLstd450Trunc,
  581. GLSLstd450FAbs,
  582. GLSLstd450SAbs,
  583. GLSLstd450FSign,
  584. GLSLstd450SSign,
  585. GLSLstd450Floor,
  586. GLSLstd450Ceil,
  587. GLSLstd450Fract,
  588. GLSLstd450Radians,
  589. GLSLstd450Degrees,
  590. GLSLstd450Sin,
  591. GLSLstd450Cos,
  592. GLSLstd450Tan,
  593. GLSLstd450Asin,
  594. GLSLstd450Acos,
  595. GLSLstd450Atan,
  596. GLSLstd450Sinh,
  597. GLSLstd450Cosh,
  598. GLSLstd450Tanh,
  599. GLSLstd450Asinh,
  600. GLSLstd450Acosh,
  601. GLSLstd450Atanh,
  602. GLSLstd450Atan2,
  603. GLSLstd450Pow,
  604. GLSLstd450Exp,
  605. GLSLstd450Log,
  606. GLSLstd450Exp2,
  607. GLSLstd450Log2,
  608. GLSLstd450Sqrt,
  609. GLSLstd450InverseSqrt,
  610. GLSLstd450Determinant,
  611. GLSLstd450MatrixInverse,
  612. GLSLstd450ModfStruct,
  613. GLSLstd450FMin,
  614. GLSLstd450UMin,
  615. GLSLstd450SMin,
  616. GLSLstd450FMax,
  617. GLSLstd450UMax,
  618. GLSLstd450SMax,
  619. GLSLstd450FClamp,
  620. GLSLstd450UClamp,
  621. GLSLstd450SClamp,
  622. GLSLstd450FMix,
  623. GLSLstd450IMix,
  624. GLSLstd450Step,
  625. GLSLstd450SmoothStep,
  626. GLSLstd450Fma,
  627. GLSLstd450FrexpStruct,
  628. GLSLstd450Ldexp,
  629. GLSLstd450PackSnorm4x8,
  630. GLSLstd450PackUnorm4x8,
  631. GLSLstd450PackSnorm2x16,
  632. GLSLstd450PackUnorm2x16,
  633. GLSLstd450PackHalf2x16,
  634. GLSLstd450PackDouble2x32,
  635. GLSLstd450UnpackSnorm2x16,
  636. GLSLstd450UnpackUnorm2x16,
  637. GLSLstd450UnpackHalf2x16,
  638. GLSLstd450UnpackSnorm4x8,
  639. GLSLstd450UnpackUnorm4x8,
  640. GLSLstd450UnpackDouble2x32,
  641. GLSLstd450Length,
  642. GLSLstd450Distance,
  643. GLSLstd450Cross,
  644. GLSLstd450Normalize,
  645. GLSLstd450FaceForward,
  646. GLSLstd450Reflect,
  647. GLSLstd450Refract,
  648. GLSLstd450FindILsb,
  649. GLSLstd450FindSMsb,
  650. GLSLstd450FindUMsb,
  651. GLSLstd450InterpolateAtCentroid,
  652. GLSLstd450InterpolateAtSample,
  653. GLSLstd450InterpolateAtOffset,
  654. GLSLstd450NMin,
  655. GLSLstd450NMax,
  656. GLSLstd450NClamp};
  657. } else {
  658. // Map the result id to the empty set.
  659. combinator_ops_[extension->result_id()];
  660. }
  661. }
  662. void IRContext::InitializeCombinators() {
  663. get_feature_mgr()->GetCapabilities()->ForEach(
  664. [this](SpvCapability cap) { AddCombinatorsForCapability(cap); });
  665. for (auto& extension : module()->ext_inst_imports()) {
  666. AddCombinatorsForExtension(&extension);
  667. }
  668. valid_analyses_ |= kAnalysisCombinators;
  669. }
  670. void IRContext::RemoveFromIdToName(const Instruction* inst) {
  671. if (id_to_name_ &&
  672. (inst->opcode() == SpvOpName || inst->opcode() == SpvOpMemberName)) {
  673. auto range = id_to_name_->equal_range(inst->GetSingleWordInOperand(0));
  674. for (auto it = range.first; it != range.second; ++it) {
  675. if (it->second == inst) {
  676. id_to_name_->erase(it);
  677. break;
  678. }
  679. }
  680. }
  681. }
  682. LoopDescriptor* IRContext::GetLoopDescriptor(const Function* f) {
  683. if (!AreAnalysesValid(kAnalysisLoopAnalysis)) {
  684. ResetLoopAnalysis();
  685. }
  686. std::unordered_map<const Function*, LoopDescriptor>::iterator it =
  687. loop_descriptors_.find(f);
  688. if (it == loop_descriptors_.end()) {
  689. return &loop_descriptors_
  690. .emplace(std::make_pair(f, LoopDescriptor(this, f)))
  691. .first->second;
  692. }
  693. return &it->second;
  694. }
  695. uint32_t IRContext::FindBuiltinInputVar(uint32_t builtin) {
  696. for (auto& a : module_->annotations()) {
  697. if (a.opcode() != SpvOpDecorate) continue;
  698. if (a.GetSingleWordInOperand(kSpvDecorateDecorationInIdx) !=
  699. SpvDecorationBuiltIn)
  700. continue;
  701. if (a.GetSingleWordInOperand(kSpvDecorateBuiltinInIdx) != builtin) continue;
  702. uint32_t target_id = a.GetSingleWordInOperand(kSpvDecorateTargetIdInIdx);
  703. Instruction* b_var = get_def_use_mgr()->GetDef(target_id);
  704. if (b_var->opcode() != SpvOpVariable) continue;
  705. if (b_var->GetSingleWordInOperand(0) != SpvStorageClassInput) continue;
  706. return target_id;
  707. }
  708. return 0;
  709. }
  710. void IRContext::AddVarToEntryPoints(uint32_t var_id) {
  711. uint32_t ocnt = 0;
  712. for (auto& e : module()->entry_points()) {
  713. bool found = false;
  714. e.ForEachInOperand([&ocnt, &found, &var_id](const uint32_t* idp) {
  715. if (ocnt >= kEntryPointInterfaceInIdx) {
  716. if (*idp == var_id) found = true;
  717. }
  718. ++ocnt;
  719. });
  720. if (!found) {
  721. e.AddOperand({SPV_OPERAND_TYPE_ID, {var_id}});
  722. get_def_use_mgr()->AnalyzeInstDefUse(&e);
  723. }
  724. }
  725. }
  726. uint32_t IRContext::GetBuiltinInputVarId(uint32_t builtin) {
  727. if (!AreAnalysesValid(kAnalysisBuiltinVarId)) ResetBuiltinAnalysis();
  728. // If cached, return it.
  729. std::unordered_map<uint32_t, uint32_t>::iterator it =
  730. builtin_var_id_map_.find(builtin);
  731. if (it != builtin_var_id_map_.end()) return it->second;
  732. // Look for one in shader
  733. uint32_t var_id = FindBuiltinInputVar(builtin);
  734. if (var_id == 0) {
  735. // If not found, create it
  736. // TODO(greg-lunarg): Add support for all builtins
  737. analysis::TypeManager* type_mgr = get_type_mgr();
  738. analysis::Type* reg_type;
  739. switch (builtin) {
  740. case SpvBuiltInFragCoord: {
  741. analysis::Float float_ty(32);
  742. analysis::Type* reg_float_ty = type_mgr->GetRegisteredType(&float_ty);
  743. analysis::Vector v4float_ty(reg_float_ty, 4);
  744. reg_type = type_mgr->GetRegisteredType(&v4float_ty);
  745. break;
  746. }
  747. case SpvBuiltInVertexIndex:
  748. case SpvBuiltInInstanceIndex:
  749. case SpvBuiltInPrimitiveId:
  750. case SpvBuiltInInvocationId:
  751. case SpvBuiltInSubgroupLocalInvocationId: {
  752. analysis::Integer uint_ty(32, false);
  753. reg_type = type_mgr->GetRegisteredType(&uint_ty);
  754. break;
  755. }
  756. case SpvBuiltInGlobalInvocationId:
  757. case SpvBuiltInLaunchIdNV: {
  758. analysis::Integer uint_ty(32, false);
  759. analysis::Type* reg_uint_ty = type_mgr->GetRegisteredType(&uint_ty);
  760. analysis::Vector v3uint_ty(reg_uint_ty, 3);
  761. reg_type = type_mgr->GetRegisteredType(&v3uint_ty);
  762. break;
  763. }
  764. case SpvBuiltInTessCoord: {
  765. analysis::Float float_ty(32);
  766. analysis::Type* reg_float_ty = type_mgr->GetRegisteredType(&float_ty);
  767. analysis::Vector v3float_ty(reg_float_ty, 3);
  768. reg_type = type_mgr->GetRegisteredType(&v3float_ty);
  769. break;
  770. }
  771. case SpvBuiltInSubgroupLtMask: {
  772. analysis::Integer uint_ty(32, false);
  773. analysis::Type* reg_uint_ty = type_mgr->GetRegisteredType(&uint_ty);
  774. analysis::Vector v4uint_ty(reg_uint_ty, 4);
  775. reg_type = type_mgr->GetRegisteredType(&v4uint_ty);
  776. break;
  777. }
  778. default: {
  779. assert(false && "unhandled builtin");
  780. return 0;
  781. }
  782. }
  783. uint32_t type_id = type_mgr->GetTypeInstruction(reg_type);
  784. uint32_t varTyPtrId =
  785. type_mgr->FindPointerToType(type_id, SpvStorageClassInput);
  786. // TODO(1841): Handle id overflow.
  787. var_id = TakeNextId();
  788. std::unique_ptr<Instruction> newVarOp(
  789. new Instruction(this, SpvOpVariable, varTyPtrId, var_id,
  790. {{spv_operand_type_t::SPV_OPERAND_TYPE_LITERAL_INTEGER,
  791. {SpvStorageClassInput}}}));
  792. get_def_use_mgr()->AnalyzeInstDefUse(&*newVarOp);
  793. module()->AddGlobalValue(std::move(newVarOp));
  794. get_decoration_mgr()->AddDecorationVal(var_id, SpvDecorationBuiltIn,
  795. builtin);
  796. AddVarToEntryPoints(var_id);
  797. }
  798. builtin_var_id_map_[builtin] = var_id;
  799. return var_id;
  800. }
  801. void IRContext::AddCalls(const Function* func, std::queue<uint32_t>* todo) {
  802. for (auto bi = func->begin(); bi != func->end(); ++bi)
  803. for (auto ii = bi->begin(); ii != bi->end(); ++ii)
  804. if (ii->opcode() == SpvOpFunctionCall)
  805. todo->push(ii->GetSingleWordInOperand(0));
  806. }
  807. bool IRContext::ProcessEntryPointCallTree(ProcessFunction& pfn) {
  808. // Collect all of the entry points as the roots.
  809. std::queue<uint32_t> roots;
  810. for (auto& e : module()->entry_points()) {
  811. roots.push(e.GetSingleWordInOperand(kEntryPointFunctionIdInIdx));
  812. }
  813. return ProcessCallTreeFromRoots(pfn, &roots);
  814. }
  815. bool IRContext::ProcessReachableCallTree(ProcessFunction& pfn) {
  816. std::queue<uint32_t> roots;
  817. // Add all entry points since they can be reached from outside the module.
  818. for (auto& e : module()->entry_points())
  819. roots.push(e.GetSingleWordInOperand(kEntryPointFunctionIdInIdx));
  820. // Add all exported functions since they can be reached from outside the
  821. // module.
  822. for (auto& a : annotations()) {
  823. // TODO: Handle group decorations as well. Currently not generate by any
  824. // front-end, but could be coming.
  825. if (a.opcode() == SpvOp::SpvOpDecorate) {
  826. if (a.GetSingleWordOperand(1) ==
  827. SpvDecoration::SpvDecorationLinkageAttributes) {
  828. uint32_t lastOperand = a.NumOperands() - 1;
  829. if (a.GetSingleWordOperand(lastOperand) ==
  830. SpvLinkageType::SpvLinkageTypeExport) {
  831. uint32_t id = a.GetSingleWordOperand(0);
  832. if (GetFunction(id)) {
  833. roots.push(id);
  834. }
  835. }
  836. }
  837. }
  838. }
  839. return ProcessCallTreeFromRoots(pfn, &roots);
  840. }
  841. bool IRContext::ProcessCallTreeFromRoots(ProcessFunction& pfn,
  842. std::queue<uint32_t>* roots) {
  843. // Process call tree
  844. bool modified = false;
  845. std::unordered_set<uint32_t> done;
  846. while (!roots->empty()) {
  847. const uint32_t fi = roots->front();
  848. roots->pop();
  849. if (done.insert(fi).second) {
  850. Function* fn = GetFunction(fi);
  851. assert(fn && "Trying to process a function that does not exist.");
  852. modified = pfn(fn) || modified;
  853. AddCalls(fn, roots);
  854. }
  855. }
  856. return modified;
  857. }
  858. void IRContext::EmitErrorMessage(std::string message, Instruction* inst) {
  859. if (!consumer()) {
  860. return;
  861. }
  862. Instruction* line_inst = inst;
  863. while (line_inst != nullptr) { // Stop at the beginning of the basic block.
  864. if (!line_inst->dbg_line_insts().empty()) {
  865. line_inst = &line_inst->dbg_line_insts().back();
  866. if (line_inst->opcode() == SpvOpNoLine) {
  867. line_inst = nullptr;
  868. }
  869. break;
  870. }
  871. line_inst = line_inst->PreviousNode();
  872. }
  873. uint32_t line_number = 0;
  874. uint32_t col_number = 0;
  875. char* source = nullptr;
  876. if (line_inst != nullptr) {
  877. Instruction* file_name =
  878. get_def_use_mgr()->GetDef(line_inst->GetSingleWordInOperand(0));
  879. source = reinterpret_cast<char*>(&file_name->GetInOperand(0).words[0]);
  880. // Get the line number and column number.
  881. line_number = line_inst->GetSingleWordInOperand(1);
  882. col_number = line_inst->GetSingleWordInOperand(2);
  883. }
  884. message +=
  885. "\n " + inst->PrettyPrint(SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
  886. consumer()(SPV_MSG_ERROR, source, {line_number, col_number, 0},
  887. message.c_str());
  888. }
  889. // Gets the dominator analysis for function |f|.
  890. DominatorAnalysis* IRContext::GetDominatorAnalysis(const Function* f) {
  891. if (!AreAnalysesValid(kAnalysisDominatorAnalysis)) {
  892. ResetDominatorAnalysis();
  893. }
  894. if (dominator_trees_.find(f) == dominator_trees_.end()) {
  895. dominator_trees_[f].InitializeTree(*cfg(), f);
  896. }
  897. return &dominator_trees_[f];
  898. }
  899. // Gets the postdominator analysis for function |f|.
  900. PostDominatorAnalysis* IRContext::GetPostDominatorAnalysis(const Function* f) {
  901. if (!AreAnalysesValid(kAnalysisDominatorAnalysis)) {
  902. ResetDominatorAnalysis();
  903. }
  904. if (post_dominator_trees_.find(f) == post_dominator_trees_.end()) {
  905. post_dominator_trees_[f].InitializeTree(*cfg(), f);
  906. }
  907. return &post_dominator_trees_[f];
  908. }
  909. bool IRContext::CheckCFG() {
  910. std::unordered_map<uint32_t, std::vector<uint32_t>> real_preds;
  911. if (!AreAnalysesValid(kAnalysisCFG)) {
  912. return true;
  913. }
  914. for (Function& function : *module()) {
  915. for (const auto& bb : function) {
  916. bb.ForEachSuccessorLabel([&bb, &real_preds](const uint32_t lab_id) {
  917. real_preds[lab_id].push_back(bb.id());
  918. });
  919. }
  920. for (auto& bb : function) {
  921. std::vector<uint32_t> preds = cfg()->preds(bb.id());
  922. std::vector<uint32_t> real = real_preds[bb.id()];
  923. std::sort(preds.begin(), preds.end());
  924. std::sort(real.begin(), real.end());
  925. bool same = true;
  926. if (preds.size() != real.size()) {
  927. same = false;
  928. }
  929. for (size_t i = 0; i < real.size() && same; i++) {
  930. if (preds[i] != real[i]) {
  931. same = false;
  932. }
  933. }
  934. if (!same) {
  935. std::cerr << "Predecessors for " << bb.id() << " are different:\n";
  936. std::cerr << "Real:";
  937. for (uint32_t i : real) {
  938. std::cerr << ' ' << i;
  939. }
  940. std::cerr << std::endl;
  941. std::cerr << "Recorded:";
  942. for (uint32_t i : preds) {
  943. std::cerr << ' ' << i;
  944. }
  945. std::cerr << std::endl;
  946. }
  947. if (!same) return false;
  948. }
  949. }
  950. return true;
  951. }
  952. bool IRContext::IsReachable(const opt::BasicBlock& bb) {
  953. auto enclosing_function = bb.GetParent();
  954. return GetDominatorAnalysis(enclosing_function)
  955. ->Dominates(enclosing_function->entry().get(), &bb);
  956. }
  957. } // namespace opt
  958. } // namespace spvtools