• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   // 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   context()->InvalidateAnalyses(IRContext::kAnalysisDebugInfo);
51 
52   context()->module()->ForEachInst(
53       [&result_id_mapping, &modified](Instruction* inst) {
54         auto operand = inst->begin();
55         while (operand != inst->end()) {
56           const auto type = operand->type;
57           if (spvIsIdType(type)) {
58             assert(operand->words.size() == 1);
59             uint32_t& id = operand->words[0];
60             uint32_t new_id = GetRemappedId(&result_id_mapping, id);
61             if (id != new_id) {
62               modified = true;
63               id = new_id;
64               // Update data cached in the instruction object.
65               if (type == SPV_OPERAND_TYPE_RESULT_ID) {
66                 inst->SetResultId(id);
67               } else if (type == SPV_OPERAND_TYPE_TYPE_ID) {
68                 inst->SetResultType(id);
69               }
70             }
71           }
72           ++operand;
73         }
74 
75         uint32_t scope_id = inst->GetDebugScope().GetLexicalScope();
76         if (scope_id != kNoDebugScope) {
77           uint32_t new_id = GetRemappedId(&result_id_mapping, scope_id);
78           if (scope_id != new_id) {
79             inst->UpdateLexicalScope(new_id);
80             modified = true;
81           }
82         }
83         uint32_t inlinedat_id = inst->GetDebugInlinedAt();
84         if (inlinedat_id != kNoInlinedAt) {
85           uint32_t new_id = GetRemappedId(&result_id_mapping, inlinedat_id);
86           if (inlinedat_id != new_id) {
87             inst->UpdateDebugInlinedAt(new_id);
88             modified = true;
89           }
90         }
91       },
92       true);
93 
94   if (context()->module()->id_bound() != result_id_mapping.size() + 1) {
95     modified = true;
96     context()->module()->SetIdBound(
97         static_cast<uint32_t>(result_id_mapping.size() + 1));
98     // There are ids in the feature manager that could now be invalid
99     context()->ResetFeatureManager();
100   }
101 
102   return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
103 }
104 
105 }  // namespace opt
106 }  // namespace spvtools
107