ir_context.cpp 37 KB

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