ir_context.cpp 34 KB

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