compact_ids_pass.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/compact_ids_pass.h"
  15. #include <cassert>
  16. #include <unordered_map>
  17. #include "source/opt/ir_context.h"
  18. namespace spvtools {
  19. namespace opt {
  20. namespace {
  21. // Returns the remapped id of |id| from |result_id_mapping|. If the remapped
  22. // id does not exist, adds a new one to |result_id_mapping| and returns it.
  23. uint32_t GetRemappedId(
  24. std::unordered_map<uint32_t, uint32_t>* result_id_mapping, uint32_t id) {
  25. auto it = result_id_mapping->find(id);
  26. if (it == result_id_mapping->end()) {
  27. const uint32_t new_id =
  28. static_cast<uint32_t>(result_id_mapping->size()) + 1;
  29. const auto insertion_result = result_id_mapping->emplace(id, new_id);
  30. it = insertion_result.first;
  31. assert(insertion_result.second);
  32. }
  33. return it->second;
  34. }
  35. } // namespace
  36. Pass::Status CompactIdsPass::Process() {
  37. bool modified = false;
  38. std::unordered_map<uint32_t, uint32_t> result_id_mapping;
  39. // Disable automatic DebugInfo analysis for the life of the CompactIds pass.
  40. // The DebugInfo manager requires the SPIR-V to be valid to run, but this is
  41. // not true at all times in CompactIds as it remaps all ids.
  42. context()->InvalidateAnalyses(IRContext::kAnalysisDebugInfo);
  43. context()->module()->ForEachInst(
  44. [&result_id_mapping, &modified](Instruction* inst) {
  45. auto operand = inst->begin();
  46. while (operand != inst->end()) {
  47. const auto type = operand->type;
  48. if (spvIsIdType(type)) {
  49. assert(operand->words.size() == 1);
  50. uint32_t& id = operand->words[0];
  51. uint32_t new_id = GetRemappedId(&result_id_mapping, id);
  52. if (id != new_id) {
  53. modified = true;
  54. id = new_id;
  55. // Update data cached in the instruction object.
  56. if (type == SPV_OPERAND_TYPE_RESULT_ID) {
  57. inst->SetResultId(id);
  58. } else if (type == SPV_OPERAND_TYPE_TYPE_ID) {
  59. inst->SetResultType(id);
  60. }
  61. }
  62. }
  63. ++operand;
  64. }
  65. uint32_t scope_id = inst->GetDebugScope().GetLexicalScope();
  66. if (scope_id != kNoDebugScope) {
  67. uint32_t new_id = GetRemappedId(&result_id_mapping, scope_id);
  68. if (scope_id != new_id) {
  69. inst->UpdateLexicalScope(new_id);
  70. modified = true;
  71. }
  72. }
  73. uint32_t inlinedat_id = inst->GetDebugInlinedAt();
  74. if (inlinedat_id != kNoInlinedAt) {
  75. uint32_t new_id = GetRemappedId(&result_id_mapping, inlinedat_id);
  76. if (inlinedat_id != new_id) {
  77. inst->UpdateDebugInlinedAt(new_id);
  78. modified = true;
  79. }
  80. }
  81. },
  82. true);
  83. if (context()->module()->id_bound() != result_id_mapping.size() + 1) {
  84. modified = true;
  85. context()->module()->SetIdBound(
  86. static_cast<uint32_t>(result_id_mapping.size() + 1));
  87. // There are ids in the feature manager that could now be invalid
  88. context()->ResetFeatureManager();
  89. }
  90. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  91. }
  92. } // namespace opt
  93. } // namespace spvtools