ir_context.cpp 38 KB

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