compact_ids_pass.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. context()->module()->ForEachInst(
  40. [&result_id_mapping, &modified](Instruction* inst) {
  41. auto operand = inst->begin();
  42. while (operand != inst->end()) {
  43. const auto type = operand->type;
  44. if (spvIsIdType(type)) {
  45. assert(operand->words.size() == 1);
  46. uint32_t& id = operand->words[0];
  47. uint32_t new_id = GetRemappedId(&result_id_mapping, id);
  48. if (id != new_id) {
  49. modified = true;
  50. id = new_id;
  51. // Update data cached in the instruction object.
  52. if (type == SPV_OPERAND_TYPE_RESULT_ID) {
  53. inst->SetResultId(id);
  54. } else if (type == SPV_OPERAND_TYPE_TYPE_ID) {
  55. inst->SetResultType(id);
  56. }
  57. }
  58. }
  59. ++operand;
  60. }
  61. uint32_t scope_id = inst->GetDebugScope().GetLexicalScope();
  62. if (scope_id != kNoDebugScope) {
  63. uint32_t new_id = GetRemappedId(&result_id_mapping, scope_id);
  64. if (scope_id != new_id) {
  65. inst->UpdateLexicalScope(new_id);
  66. modified = true;
  67. }
  68. }
  69. uint32_t inlinedat_id = inst->GetDebugInlinedAt();
  70. if (inlinedat_id != kNoInlinedAt) {
  71. uint32_t new_id = GetRemappedId(&result_id_mapping, inlinedat_id);
  72. if (inlinedat_id != new_id) {
  73. inst->UpdateDebugInlinedAt(new_id);
  74. modified = true;
  75. }
  76. }
  77. },
  78. true);
  79. if (modified) {
  80. context()->module()->SetIdBound(
  81. static_cast<uint32_t>(result_id_mapping.size() + 1));
  82. // There are ids in the feature manager that could now be invalid
  83. context()->ResetFeatureManager();
  84. }
  85. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  86. }
  87. } // namespace opt
  88. } // namespace spvtools