/src/spirv-tools/source/opt/compact_ids_pass.cpp
Line | Count | Source |
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. |
28 | | uint32_t GetRemappedId( |
29 | 0 | std::unordered_map<uint32_t, uint32_t>* result_id_mapping, uint32_t id) { |
30 | 0 | auto it = result_id_mapping->find(id); |
31 | 0 | if (it == result_id_mapping->end()) { |
32 | 0 | const uint32_t new_id = |
33 | 0 | static_cast<uint32_t>(result_id_mapping->size()) + 1; |
34 | 0 | const auto insertion_result = result_id_mapping->emplace(id, new_id); |
35 | 0 | it = insertion_result.first; |
36 | 0 | assert(insertion_result.second); |
37 | 0 | } |
38 | 0 | return it->second; |
39 | 0 | } |
40 | | |
41 | | } // namespace |
42 | | |
43 | 0 | Pass::Status CompactIdsPass::Process() { |
44 | 0 | bool modified = false; |
45 | 0 | std::unordered_map<uint32_t, uint32_t> result_id_mapping; |
46 | | |
47 | | // Disable automatic DebugInfo analysis for the life of the CompactIds pass. |
48 | | // The DebugInfo manager requires the SPIR-V to be valid to run, but this is |
49 | | // not true at all times in CompactIds as it remaps all ids. |
50 | 0 | context()->InvalidateAnalyses(IRContext::kAnalysisDebugInfo); |
51 | |
|
52 | 0 | context()->module()->ForEachInst( |
53 | 0 | [&result_id_mapping, &modified](Instruction* inst) { |
54 | 0 | auto operand = inst->begin(); |
55 | 0 | while (operand != inst->end()) { |
56 | 0 | const auto type = operand->type; |
57 | 0 | if (spvIsIdType(type)) { |
58 | 0 | assert(operand->words.size() == 1); |
59 | 0 | uint32_t& id = operand->words[0]; |
60 | 0 | uint32_t new_id = GetRemappedId(&result_id_mapping, id); |
61 | 0 | if (id != new_id) { |
62 | 0 | modified = true; |
63 | 0 | id = new_id; |
64 | | // Update data cached in the instruction object. |
65 | 0 | if (type == SPV_OPERAND_TYPE_RESULT_ID) { |
66 | 0 | inst->SetResultId(id); |
67 | 0 | } else if (type == SPV_OPERAND_TYPE_TYPE_ID) { |
68 | 0 | inst->SetResultType(id); |
69 | 0 | } |
70 | 0 | } |
71 | 0 | } |
72 | 0 | ++operand; |
73 | 0 | } |
74 | | |
75 | 0 | uint32_t scope_id = inst->GetDebugScope().GetLexicalScope(); |
76 | 0 | if (scope_id != kNoDebugScope) { |
77 | 0 | uint32_t new_id = GetRemappedId(&result_id_mapping, scope_id); |
78 | 0 | if (scope_id != new_id) { |
79 | 0 | inst->UpdateLexicalScope(new_id); |
80 | 0 | modified = true; |
81 | 0 | } |
82 | 0 | } |
83 | 0 | uint32_t inlinedat_id = inst->GetDebugInlinedAt(); |
84 | 0 | if (inlinedat_id != kNoInlinedAt) { |
85 | 0 | uint32_t new_id = GetRemappedId(&result_id_mapping, inlinedat_id); |
86 | 0 | if (inlinedat_id != new_id) { |
87 | 0 | inst->UpdateDebugInlinedAt(new_id); |
88 | 0 | modified = true; |
89 | 0 | } |
90 | 0 | } |
91 | 0 | }, |
92 | 0 | true); |
93 | |
|
94 | 0 | if (context()->module()->id_bound() != result_id_mapping.size() + 1) { |
95 | 0 | modified = true; |
96 | 0 | context()->module()->SetIdBound( |
97 | 0 | static_cast<uint32_t>(result_id_mapping.size() + 1)); |
98 | | // There are ids in the feature manager that could now be invalid |
99 | 0 | context()->ResetFeatureManager(); |
100 | 0 | } |
101 | |
|
102 | 0 | return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange; |
103 | 0 | } |
104 | | |
105 | | } // namespace opt |
106 | | } // namespace spvtools |