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