• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2016 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/module.h"
16 
17 #include <algorithm>
18 #include <cstring>
19 #include <ostream>
20 
21 #include "source/operand.h"
22 #include "source/opt/ir_context.h"
23 #include "source/opt/reflect.h"
24 
25 namespace spvtools {
26 namespace opt {
27 
TakeNextIdBound()28 uint32_t Module::TakeNextIdBound() {
29   if (context()) {
30     if (id_bound() >= context()->max_id_bound()) {
31       return 0;
32     }
33   } else if (id_bound() >= kDefaultMaxIdBound) {
34     return 0;
35   }
36 
37   return header_.bound++;
38 }
39 
GetTypes()40 std::vector<Instruction*> Module::GetTypes() {
41   std::vector<Instruction*> type_insts;
42   for (auto& inst : types_values_) {
43     if (IsTypeInst(inst.opcode())) type_insts.push_back(&inst);
44   }
45   return type_insts;
46 }
47 
GetTypes() const48 std::vector<const Instruction*> Module::GetTypes() const {
49   std::vector<const Instruction*> type_insts;
50   for (auto& inst : types_values_) {
51     if (IsTypeInst(inst.opcode())) type_insts.push_back(&inst);
52   }
53   return type_insts;
54 }
55 
GetConstants()56 std::vector<Instruction*> Module::GetConstants() {
57   std::vector<Instruction*> const_insts;
58   for (auto& inst : types_values_) {
59     if (IsConstantInst(inst.opcode())) const_insts.push_back(&inst);
60   }
61   return const_insts;
62 }
63 
GetConstants() const64 std::vector<const Instruction*> Module::GetConstants() const {
65   std::vector<const Instruction*> const_insts;
66   for (auto& inst : types_values_) {
67     if (IsConstantInst(inst.opcode())) const_insts.push_back(&inst);
68   }
69   return const_insts;
70 }
71 
GetGlobalValue(spv::Op opcode) const72 uint32_t Module::GetGlobalValue(spv::Op opcode) const {
73   for (auto& inst : types_values_) {
74     if (inst.opcode() == opcode) return inst.result_id();
75   }
76   return 0;
77 }
78 
AddGlobalValue(spv::Op opcode,uint32_t result_id,uint32_t type_id)79 void Module::AddGlobalValue(spv::Op opcode, uint32_t result_id,
80                             uint32_t type_id) {
81   std::unique_ptr<Instruction> newGlobal(
82       new Instruction(context(), opcode, type_id, result_id, {}));
83   AddGlobalValue(std::move(newGlobal));
84 }
85 
ForEachInst(const std::function<void (Instruction *)> & f,bool run_on_debug_line_insts)86 void Module::ForEachInst(const std::function<void(Instruction*)>& f,
87                          bool run_on_debug_line_insts) {
88 #define DELEGATE(list) list.ForEachInst(f, run_on_debug_line_insts)
89   DELEGATE(capabilities_);
90   DELEGATE(extensions_);
91   DELEGATE(ext_inst_imports_);
92   if (memory_model_) memory_model_->ForEachInst(f, run_on_debug_line_insts);
93   if (sampled_image_address_mode_)
94     sampled_image_address_mode_->ForEachInst(f, run_on_debug_line_insts);
95   DELEGATE(entry_points_);
96   DELEGATE(execution_modes_);
97   DELEGATE(debugs1_);
98   DELEGATE(debugs2_);
99   DELEGATE(debugs3_);
100   DELEGATE(ext_inst_debuginfo_);
101   DELEGATE(annotations_);
102   DELEGATE(types_values_);
103   for (auto& i : functions_) {
104     i->ForEachInst(f, run_on_debug_line_insts,
105                    /* run_on_non_semantic_insts = */ true);
106   }
107 #undef DELEGATE
108 }
109 
ForEachInst(const std::function<void (const Instruction *)> & f,bool run_on_debug_line_insts) const110 void Module::ForEachInst(const std::function<void(const Instruction*)>& f,
111                          bool run_on_debug_line_insts) const {
112 #define DELEGATE(i) i.ForEachInst(f, run_on_debug_line_insts)
113   for (auto& i : capabilities_) DELEGATE(i);
114   for (auto& i : extensions_) DELEGATE(i);
115   for (auto& i : ext_inst_imports_) DELEGATE(i);
116   if (memory_model_)
117     static_cast<const Instruction*>(memory_model_.get())
118         ->ForEachInst(f, run_on_debug_line_insts);
119   if (sampled_image_address_mode_)
120     static_cast<const Instruction*>(sampled_image_address_mode_.get())
121         ->ForEachInst(f, run_on_debug_line_insts);
122   for (auto& i : entry_points_) DELEGATE(i);
123   for (auto& i : execution_modes_) DELEGATE(i);
124   for (auto& i : debugs1_) DELEGATE(i);
125   for (auto& i : debugs2_) DELEGATE(i);
126   for (auto& i : debugs3_) DELEGATE(i);
127   for (auto& i : annotations_) DELEGATE(i);
128   for (auto& i : types_values_) DELEGATE(i);
129   for (auto& i : ext_inst_debuginfo_) DELEGATE(i);
130   for (auto& i : functions_) {
131     static_cast<const Function*>(i.get())->ForEachInst(
132         f, run_on_debug_line_insts,
133         /* run_on_non_semantic_insts = */ true);
134   }
135   if (run_on_debug_line_insts) {
136     for (auto& i : trailing_dbg_line_info_) DELEGATE(i);
137   }
138 #undef DELEGATE
139 }
140 
ToBinary(std::vector<uint32_t> * binary,bool skip_nop) const141 void Module::ToBinary(std::vector<uint32_t>* binary, bool skip_nop) const {
142   binary->push_back(header_.magic_number);
143   binary->push_back(header_.version);
144   // TODO(antiagainst): should we change the generator number?
145   binary->push_back(header_.generator);
146   binary->push_back(header_.bound);
147   binary->push_back(header_.schema);
148 
149   size_t bound_idx = binary->size() - 2;
150   DebugScope last_scope(kNoDebugScope, kNoInlinedAt);
151   const Instruction* last_line_inst = nullptr;
152   bool between_merge_and_branch = false;
153   bool between_label_and_phi_var = false;
154   auto write_inst = [binary, skip_nop, &last_scope, &last_line_inst,
155                      &between_merge_and_branch, &between_label_and_phi_var,
156                      this](const Instruction* i) {
157     // Skip emitting line instructions between merge and branch instructions.
158     auto opcode = i->opcode();
159     if (between_merge_and_branch && i->IsLineInst()) {
160       return;
161     }
162     if (last_line_inst != nullptr) {
163       // If the current instruction is OpLine or DebugLine and it is the same
164       // as the last line instruction that is still effective (can be applied
165       // to the next instruction), we skip writing the current instruction.
166       if (i->IsLine()) {
167         uint32_t operand_index = 0;
168         if (last_line_inst->WhileEachInOperand(
169                 [&operand_index, i](const uint32_t* word) {
170                   assert(i->NumInOperandWords() > operand_index);
171                   return *word == i->GetSingleWordInOperand(operand_index++);
172                 })) {
173           return;
174         }
175       } else if (!i->IsNoLine() && i->dbg_line_insts().empty()) {
176         // If the current instruction does not have the line information,
177         // the last line information is not effective any more. Emit OpNoLine
178         // or DebugNoLine to specify it.
179         uint32_t shader_set_id = context()
180                                      ->get_feature_mgr()
181                                      ->GetExtInstImportId_Shader100DebugInfo();
182         if (shader_set_id != 0) {
183           binary->push_back((5 << 16) |
184                             static_cast<uint16_t>(spv::Op::OpExtInst));
185           binary->push_back(context()->get_type_mgr()->GetVoidTypeId());
186           binary->push_back(context()->TakeNextId());
187           binary->push_back(shader_set_id);
188           binary->push_back(NonSemanticShaderDebugInfo100DebugNoLine);
189         } else {
190           binary->push_back((1 << 16) |
191                             static_cast<uint16_t>(spv::Op::OpNoLine));
192         }
193         last_line_inst = nullptr;
194       }
195     }
196 
197     if (opcode == spv::Op::OpLabel) {
198       between_label_and_phi_var = true;
199     } else if (opcode != spv::Op::OpVariable && opcode != spv::Op::OpPhi &&
200                !spvtools::opt::IsOpLineInst(opcode)) {
201       between_label_and_phi_var = false;
202     }
203 
204     if (!(skip_nop && i->IsNop())) {
205       const auto& scope = i->GetDebugScope();
206       if (scope != last_scope && !between_merge_and_branch) {
207         // Can only emit nonsemantic instructions after all phi instructions
208         // in a block so don't emit scope instructions before phi instructions
209         // for NonSemantic.Shader.DebugInfo.100.
210         if (!between_label_and_phi_var ||
211             context()
212                 ->get_feature_mgr()
213                 ->GetExtInstImportId_OpenCL100DebugInfo()) {
214           // Emit DebugScope |scope| to |binary|.
215           auto dbg_inst = ext_inst_debuginfo_.begin();
216           scope.ToBinary(dbg_inst->type_id(), context()->TakeNextId(),
217                          dbg_inst->GetSingleWordOperand(2), binary);
218         }
219         last_scope = scope;
220       }
221 
222       i->ToBinaryWithoutAttachedDebugInsts(binary);
223     }
224     // Update the last line instruction.
225     between_merge_and_branch = false;
226     if (spvOpcodeIsBlockTerminator(opcode) || i->IsNoLine()) {
227       last_line_inst = nullptr;
228     } else if (opcode == spv::Op::OpLoopMerge ||
229                opcode == spv::Op::OpSelectionMerge) {
230       between_merge_and_branch = true;
231       last_line_inst = nullptr;
232     } else if (i->IsLine()) {
233       last_line_inst = i;
234     }
235   };
236   ForEachInst(write_inst, true);
237 
238   // We create new instructions for DebugScope and DebugNoLine. The bound must
239   // be updated.
240   binary->data()[bound_idx] = header_.bound;
241 }
242 
ComputeIdBound() const243 uint32_t Module::ComputeIdBound() const {
244   uint32_t highest = 0;
245 
246   ForEachInst(
247       [&highest](const Instruction* inst) {
248         for (const auto& operand : *inst) {
249           if (spvIsIdType(operand.type)) {
250             highest = std::max(highest, operand.words[0]);
251           }
252         }
253       },
254       true /* scan debug line insts as well */);
255 
256   return highest + 1;
257 }
258 
HasExplicitCapability(uint32_t cap)259 bool Module::HasExplicitCapability(uint32_t cap) {
260   for (auto& ci : capabilities_) {
261     uint32_t tcap = ci.GetSingleWordOperand(0);
262     if (tcap == cap) {
263       return true;
264     }
265   }
266   return false;
267 }
268 
GetExtInstImportId(const char * extstr)269 uint32_t Module::GetExtInstImportId(const char* extstr) {
270   for (auto& ei : ext_inst_imports_)
271     if (!ei.GetInOperand(0).AsString().compare(extstr)) return ei.result_id();
272   return 0;
273 }
274 
operator <<(std::ostream & str,const Module & module)275 std::ostream& operator<<(std::ostream& str, const Module& module) {
276   module.ForEachInst([&str](const Instruction* inst) {
277     str << *inst;
278     if (inst->opcode() != spv::Op::OpFunctionEnd) {
279       str << std::endl;
280     }
281   });
282   return str;
283 }
284 
285 }  // namespace opt
286 }  // namespace spvtools
287