ir_context.cpp 38 KB

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