Debian Codegraph @

third_party/spirv-tools/source/opt/module.h

raw
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 #ifndef SOURCE_OPT_MODULE_H_
16 #define SOURCE_OPT_MODULE_H_
17
18 #include <functional>
19 #include <memory>
20 #include <unordered_map>
21 #include <utility>
22 #include <vector>
23
24 #include "source/opt/function.h"
25 #include "source/opt/instruction.h"
26 #include "source/opt/iterator.h"
27
28 namespace spvtools {
29 namespace opt {
30
31 class IRContext;
32
33 // A struct for containing the module header information.
34 struct ModuleHeader {
35 uint32_t magic_number;
36 uint32_t version;
37 uint32_t generator;
38 uint32_t bound;
39 uint32_t reserved;
40 };
41
42 // A SPIR-V module. It contains all the information for a SPIR-V module and
43 // serves as the backbone of optimization transformations.
44 class Module {
45 public:
46 using iterator = UptrVectorIterator<Function>;
47 using const_iterator = UptrVectorIterator<Function, true>;
48 using inst_iterator = InstructionList::iterator;
49 using const_inst_iterator = InstructionList::const_iterator;
50
51 // Creates an empty module with zero'd header.
52 Module() : header_({}), contains_debug_scope_(false) {}
53
54 // Sets the header to the given |header|.
55 void SetHeader(const ModuleHeader& header) { header_ = header; }
56
57 // Sets the Id bound. The Id bound cannot be set to 0.
58 void SetIdBound(uint32_t bound) {
59 assert(bound != 0);
60 header_.bound = bound;
61 }
62
63 // Returns the Id bound.
64 uint32_t IdBound() { return header_.bound; }
65
66 // Returns the current Id bound and increases it to the next available value.
67 // If the id bound has already reached its maximum value, then 0 is returned.
68 // The maximum value for the id bound is obtained from the context. If there
69 // is none, then the minimum that limit can be according to the spir-v
70 // specification.
71 // TODO(1841): Update the uses to check for a 0 return value.
72 uint32_t TakeNextIdBound();
73
74 // Appends a capability instruction to this module.
75 inline void AddCapability(std::unique_ptr<Instruction> c);
76
77 // Appends an extension instruction to this module.
78 inline void AddExtension(std::unique_ptr<Instruction> e);
79
80 // Appends an extended instruction set instruction to this module.
81 inline void AddExtInstImport(std::unique_ptr<Instruction> e);
82
83 // Set the memory model for this module.
84 inline void SetMemoryModel(std::unique_ptr<Instruction> m);
85
86 // Appends an entry point instruction to this module.
87 inline void AddEntryPoint(std::unique_ptr<Instruction> e);
88
89 // Appends an execution mode instruction to this module.
90 inline void AddExecutionMode(std::unique_ptr<Instruction> e);
91
92 // Appends a debug 1 instruction (excluding OpLine & OpNoLine) to this module.
93 // "debug 1" instructions are the ones in layout section 7.a), see section
94 // 2.4 Logical Layout of a Module from the SPIR-V specification.
95 inline void AddDebug1Inst(std::unique_ptr<Instruction> d);
96
97 // Appends a debug 2 instruction (excluding OpLine & OpNoLine) to this module.
98 // "debug 2" instructions are the ones in layout section 7.b), see section
99 // 2.4 Logical Layout of a Module from the SPIR-V specification.
100 inline void AddDebug2Inst(std::unique_ptr<Instruction> d);
101
102 // Appends a debug 3 instruction (OpModuleProcessed) to this module.
103 // This is due to decision by the SPIR Working Group, pending publication.
104 inline void AddDebug3Inst(std::unique_ptr<Instruction> d);
105
106 // Appends a debug info extension (OpenCL.DebugInfo.100 or DebugInfo)
107 // instruction to this module.
108 inline void AddExtInstDebugInfo(std::unique_ptr<Instruction> d);
109
110 // Appends an annotation instruction to this module.
111 inline void AddAnnotationInst(std::unique_ptr<Instruction> a);
112
113 // Appends a type-declaration instruction to this module.
114 inline void AddType(std::unique_ptr<Instruction> t);
115
116 // Appends a constant, global variable, or OpUndef instruction to this module.
117 inline void AddGlobalValue(std::unique_ptr<Instruction> v);
118
119 // Appends a function to this module.
120 inline void AddFunction(std::unique_ptr<Function> f);
121
122 // Sets |contains_debug_scope_| as true.
123 inline void SetContainsDebugScope();
124 inline bool ContainsDebugScope() { return contains_debug_scope_; }
125
126 // Returns a vector of pointers to type-declaration instructions in this
127 // module.
128 std::vector<Instruction*> GetTypes();
129 std::vector<const Instruction*> GetTypes() const;
130 // Returns a vector of pointers to constant-creation instructions in this
131 // module.
132 std::vector<Instruction*> GetConstants();
133 std::vector<const Instruction*> GetConstants() const;
134
135 // Return result id of global value with |opcode|, 0 if not present.
136 uint32_t GetGlobalValue(SpvOp opcode) const;
137
138 // Add global value with |opcode|, |result_id| and |type_id|
139 void AddGlobalValue(SpvOp opcode, uint32_t result_id, uint32_t type_id);
140
141 inline uint32_t id_bound() const { return header_.bound; }
142
143 inline uint32_t version() const { return header_.version; }
144
145 inline void set_version(uint32_t v) { header_.version = v; }
146
147 // Iterators for capabilities instructions contained in this module.
148 inline inst_iterator capability_begin();
149 inline inst_iterator capability_end();
150 inline IteratorRange<inst_iterator> capabilities();
151 inline IteratorRange<const_inst_iterator> capabilities() const;
152
153 // Iterators for ext_inst_imports instructions contained in this module.
154 inline inst_iterator ext_inst_import_begin();
155 inline inst_iterator ext_inst_import_end();
156 inline IteratorRange<inst_iterator> ext_inst_imports();
157 inline IteratorRange<const_inst_iterator> ext_inst_imports() const;
158
159 // Return the memory model instruction contained inthis module.
160 inline Instruction* GetMemoryModel() { return memory_model_.get(); }
161 inline const Instruction* GetMemoryModel() const {
162 return memory_model_.get();
163 }
164
165 // There are several kinds of debug instructions, according to where they can
166 // appear in the logical layout of a module:
167 // - Section 7a: OpString, OpSourceExtension, OpSource, OpSourceContinued
168 // - Section 7b: OpName, OpMemberName
169 // - Section 7c: OpModuleProcessed
170 // - Mostly anywhere: OpLine and OpNoLine
171 //
172
173 // Iterators for debug 1 instructions (excluding OpLine & OpNoLine) contained
174 // in this module. These are for layout section 7a.
175 inline inst_iterator debug1_begin();
176 inline inst_iterator debug1_end();
177 inline IteratorRange<inst_iterator> debugs1();
178 inline IteratorRange<const_inst_iterator> debugs1() const;
179
180 // Iterators for debug 2 instructions (excluding OpLine & OpNoLine) contained
181 // in this module. These are for layout section 7b.
182 inline inst_iterator debug2_begin();
183 inline inst_iterator debug2_end();
184 inline IteratorRange<inst_iterator> debugs2();
185 inline IteratorRange<const_inst_iterator> debugs2() const;
186
187 // Iterators for debug 3 instructions (excluding OpLine & OpNoLine) contained
188 // in this module. These are for layout section 7c.
189 inline inst_iterator debug3_begin();
190 inline inst_iterator debug3_end();
191 inline IteratorRange<inst_iterator> debugs3();
192 inline IteratorRange<const_inst_iterator> debugs3() const;
193
194 // Iterators for debug info instructions (excluding OpLine & OpNoLine)
195 // contained in this module. These are OpExtInst for OpenCL.DebugInfo.100
196 // or DebugInfo extension placed between section 9 and 10.
197 inline inst_iterator ext_inst_debuginfo_begin();
198 inline inst_iterator ext_inst_debuginfo_end();
199 inline IteratorRange<inst_iterator> ext_inst_debuginfo();
200 inline IteratorRange<const_inst_iterator> ext_inst_debuginfo() const;
201
202 // Iterators for entry point instructions contained in this module
203 inline IteratorRange<inst_iterator> entry_points();
204 inline IteratorRange<const_inst_iterator> entry_points() const;
205
206 // Iterators for execution_modes instructions contained in this module.
207 inline inst_iterator execution_mode_begin();
208 inline inst_iterator execution_mode_end();
209 inline IteratorRange<inst_iterator> execution_modes();
210 inline IteratorRange<const_inst_iterator> execution_modes() const;
211
212 // Iterators for annotation instructions contained in this module.
213 inline inst_iterator annotation_begin();
214 inline inst_iterator annotation_end();
215 IteratorRange<inst_iterator> annotations();
216 IteratorRange<const_inst_iterator> annotations() const;
217
218 // Iterators for extension instructions contained in this module.
219 inline inst_iterator extension_begin();
220 inline inst_iterator extension_end();
221 IteratorRange<inst_iterator> extensions();
222 IteratorRange<const_inst_iterator> extensions() const;
223
224 // Iterators for types, constants and global variables instructions.
225 inline inst_iterator types_values_begin();
226 inline inst_iterator types_values_end();
227 inline IteratorRange<inst_iterator> types_values();
228 inline IteratorRange<const_inst_iterator> types_values() const;
229
230 // Iterators for functions contained in this module.
231 iterator begin() { return iterator(&functions_, functions_.begin()); }
232 iterator end() { return iterator(&functions_, functions_.end()); }
233 const_iterator begin() const { return cbegin(); }
234 const_iterator end() const { return cend(); }
235 inline const_iterator cbegin() const;
236 inline const_iterator cend() const;
237
238 // Invokes function |f| on all instructions in this module, and optionally on
239 // the debug line instructions that precede them.
240 void ForEachInst(const std::function<void(Instruction*)>& f,
241 bool run_on_debug_line_insts = false);
242 void ForEachInst(const std::function<void(const Instruction*)>& f,
243 bool run_on_debug_line_insts = false) const;
244
245 // Pushes the binary segments for this instruction into the back of *|binary|.
246 // If |skip_nop| is true and this is a OpNop, do nothing.
247 void ToBinary(std::vector<uint32_t>* binary, bool skip_nop) const;
248
249 // Pushes the binary segments for this instruction into the back of *|binary|
250 // including all OpLine and OpNoLine even if we can skip emitting some line
251 // instructions. If |skip_nop| is true and this is a OpNop, do nothing.
252 void ToBinaryWithAllOpLines(std::vector<uint32_t>* binary,
253 bool skip_nop) const;
254
255 // Returns 1 more than the maximum Id value mentioned in the module.
256 uint32_t ComputeIdBound() const;
257
258 // Returns true if module has capability |cap|
259 bool HasExplicitCapability(uint32_t cap);
260
261 // Returns id for OpExtInst instruction for extension |extstr|.
262 // Returns 0 if not found.
263 uint32_t GetExtInstImportId(const char* extstr);
264
265 // Sets the associated context for this module
266 void SetContext(IRContext* c) { context_ = c; }
267
268 // Gets the associated context for this module
269 IRContext* context() const { return context_; }
270
271 // Sets the trailing debug line info to |dbg_line_info|.
272 void SetTrailingDbgLineInfo(std::vector<Instruction>&& dbg_line_info) {
273 trailing_dbg_line_info_ = std::move(dbg_line_info);
274 }
275
276 std::vector<Instruction>& trailing_dbg_line_info() {
277 return trailing_dbg_line_info_;
278 }
279
280 const std::vector<Instruction>& trailing_dbg_line_info() const {
281 return trailing_dbg_line_info_;
282 }
283
284 private:
285 ModuleHeader header_; // Module header
286
287 // The following fields respect the "Logical Layout of a Module" in
288 // Section 2.4 of the SPIR-V specification.
289 IRContext* context_;
290 InstructionList capabilities_;
291 InstructionList extensions_;
292 InstructionList ext_inst_imports_;
293 // A module only has one memory model instruction.
294 std::unique_ptr<Instruction> memory_model_;
295 InstructionList entry_points_;
296 InstructionList execution_modes_;
297 InstructionList debugs1_;
298 InstructionList debugs2_;
299 InstructionList debugs3_;
300 InstructionList ext_inst_debuginfo_;
301 InstructionList annotations_;
302 // Type declarations, constants, and global variable declarations.
303 InstructionList types_values_;
304 std::vector<std::unique_ptr<Function>> functions_;
305
306 // If the module ends with Op*Line instruction, they will not be attached to
307 // any instruction. We record them here, so they will not be lost.
308 std::vector<Instruction> trailing_dbg_line_info_;
309
310 // This module contains DebugScope or DebugNoScope.
311 bool contains_debug_scope_;
312 };
313
314 // Pretty-prints |module| to |str|. Returns |str|.
315 std::ostream& operator<<(std::ostream& str, const Module& module);
316
317 inline void Module::AddCapability(std::unique_ptr<Instruction> c) {
318 capabilities_.push_back(std::move(c));
319 }
320
321 inline void Module::AddExtension(std::unique_ptr<Instruction> e) {
322 extensions_.push_back(std::move(e));
323 }
324
325 inline void Module::AddExtInstImport(std::unique_ptr<Instruction> e) {
326 ext_inst_imports_.push_back(std::move(e));
327 }
328
329 inline void Module::SetMemoryModel(std::unique_ptr<Instruction> m) {
330 memory_model_ = std::move(m);
331 }
332
333 inline void Module::AddEntryPoint(std::unique_ptr<Instruction> e) {
334 entry_points_.push_back(std::move(e));
335 }
336
337 inline void Module::AddExecutionMode(std::unique_ptr<Instruction> e) {
338 execution_modes_.push_back(std::move(e));
339 }
340
341 inline void Module::AddDebug1Inst(std::unique_ptr<Instruction> d) {
342 debugs1_.push_back(std::move(d));
343 }
344
345 inline void Module::AddDebug2Inst(std::unique_ptr<Instruction> d) {
346 debugs2_.push_back(std::move(d));
347 }
348
349 inline void Module::AddDebug3Inst(std::unique_ptr<Instruction> d) {
350 debugs3_.push_back(std::move(d));
351 }
352
353 inline void Module::AddExtInstDebugInfo(std::unique_ptr<Instruction> d) {
354 ext_inst_debuginfo_.push_back(std::move(d));
355 }
356
357 inline void Module::AddAnnotationInst(std::unique_ptr<Instruction> a) {
358 annotations_.push_back(std::move(a));
359 }
360
361 inline void Module::AddType(std::unique_ptr<Instruction> t) {
362 types_values_.push_back(std::move(t));
363 }
364
365 inline void Module::AddGlobalValue(std::unique_ptr<Instruction> v) {
366 types_values_.push_back(std::move(v));
367 }
368
369 inline void Module::AddFunction(std::unique_ptr<Function> f) {
370 functions_.emplace_back(std::move(f));
371 }
372
373 inline void Module::SetContainsDebugScope() { contains_debug_scope_ = true; }
374
375 inline Module::inst_iterator Module::capability_begin() {
376 return capabilities_.begin();
377 }
378 inline Module::inst_iterator Module::capability_end() {
379 return capabilities_.end();
380 }
381
382 inline IteratorRange<Module::inst_iterator> Module::capabilities() {
383 return make_range(capabilities_.begin(), capabilities_.end());
384 }
385
386 inline IteratorRange<Module::const_inst_iterator> Module::capabilities() const {
387 return make_range(capabilities_.begin(), capabilities_.end());
388 }
389
390 inline Module::inst_iterator Module::ext_inst_import_begin() {
391 return ext_inst_imports_.begin();
392 }
393 inline Module::inst_iterator Module::ext_inst_import_end() {
394 return ext_inst_imports_.end();
395 }
396
397 inline IteratorRange<Module::inst_iterator> Module::ext_inst_imports() {
398 return make_range(ext_inst_imports_.begin(), ext_inst_imports_.end());
399 }
400
401 inline IteratorRange<Module::const_inst_iterator> Module::ext_inst_imports()
402 const {
403 return make_range(ext_inst_imports_.begin(), ext_inst_imports_.end());
404 }
405
406 inline Module::inst_iterator Module::debug1_begin() { return debugs1_.begin(); }
407 inline Module::inst_iterator Module::debug1_end() { return debugs1_.end(); }
408
409 inline IteratorRange<Module::inst_iterator> Module::debugs1() {
410 return make_range(debugs1_.begin(), debugs1_.end());
411 }
412
413 inline IteratorRange<Module::const_inst_iterator> Module::debugs1() const {
414 return make_range(debugs1_.begin(), debugs1_.end());
415 }
416
417 inline Module::inst_iterator Module::debug2_begin() { return debugs2_.begin(); }
418 inline Module::inst_iterator Module::debug2_end() { return debugs2_.end(); }
419
420 inline IteratorRange<Module::inst_iterator> Module::debugs2() {
421 return make_range(debugs2_.begin(), debugs2_.end());
422 }
423
424 inline IteratorRange<Module::const_inst_iterator> Module::debugs2() const {
425 return make_range(debugs2_.begin(), debugs2_.end());
426 }
427
428 inline Module::inst_iterator Module::debug3_begin() { return debugs3_.begin(); }
429 inline Module::inst_iterator Module::debug3_end() { return debugs3_.end(); }
430
431 inline IteratorRange<Module::inst_iterator> Module::debugs3() {
432 return make_range(debugs3_.begin(), debugs3_.end());
433 }
434
435 inline IteratorRange<Module::const_inst_iterator> Module::debugs3() const {
436 return make_range(debugs3_.begin(), debugs3_.end());
437 }
438
439 inline Module::inst_iterator Module::ext_inst_debuginfo_begin() {
440 return ext_inst_debuginfo_.begin();
441 }
442 inline Module::inst_iterator Module::ext_inst_debuginfo_end() {
443 return ext_inst_debuginfo_.end();
444 }
445
446 inline IteratorRange<Module::inst_iterator> Module::ext_inst_debuginfo() {
447 return make_range(ext_inst_debuginfo_.begin(), ext_inst_debuginfo_.end());
448 }
449
450 inline IteratorRange<Module::const_inst_iterator> Module::ext_inst_debuginfo()
451 const {
452 return make_range(ext_inst_debuginfo_.begin(), ext_inst_debuginfo_.end());
453 }
454
455 inline IteratorRange<Module::inst_iterator> Module::entry_points() {
456 return make_range(entry_points_.begin(), entry_points_.end());
457 }
458
459 inline IteratorRange<Module::const_inst_iterator> Module::entry_points() const {
460 return make_range(entry_points_.begin(), entry_points_.end());
461 }
462
463 inline Module::inst_iterator Module::execution_mode_begin() {
464 return execution_modes_.begin();
465 }
466 inline Module::inst_iterator Module::execution_mode_end() {
467 return execution_modes_.end();
468 }
469
470 inline IteratorRange<Module::inst_iterator> Module::execution_modes() {
471 return make_range(execution_modes_.begin(), execution_modes_.end());
472 }
473
474 inline IteratorRange<Module::const_inst_iterator> Module::execution_modes()
475 const {
476 return make_range(execution_modes_.begin(), execution_modes_.end());
477 }
478
479 inline Module::inst_iterator Module::annotation_begin() {
480 return annotations_.begin();
481 }
482 inline Module::inst_iterator Module::annotation_end() {
483 return annotations_.end();
484 }
485
486 inline IteratorRange<Module::inst_iterator> Module::annotations() {
487 return make_range(annotations_.begin(), annotations_.end());
488 }
489
490 inline IteratorRange<Module::const_inst_iterator> Module::annotations() const {
491 return make_range(annotations_.begin(), annotations_.end());
492 }
493
494 inline Module::inst_iterator Module::extension_begin() {
495 return extensions_.begin();
496 }
497 inline Module::inst_iterator Module::extension_end() {
498 return extensions_.end();
499 }
500
501 inline IteratorRange<Module::inst_iterator> Module::extensions() {
502 return make_range(extensions_.begin(), extensions_.end());
503 }
504
505 inline IteratorRange<Module::const_inst_iterator> Module::extensions() const {
506 return make_range(extensions_.begin(), extensions_.end());
507 }
508
509 inline Module::inst_iterator Module::types_values_begin() {
510 return types_values_.begin();
511 }
512
513 inline Module::inst_iterator Module::types_values_end() {
514 return types_values_.end();
515 }
516
517 inline IteratorRange<Module::inst_iterator> Module::types_values() {
518 return make_range(types_values_.begin(), types_values_.end());
519 }
520
521 inline IteratorRange<Module::const_inst_iterator> Module::types_values() const {
522 return make_range(types_values_.begin(), types_values_.end());
523 }
524
525 inline Module::const_iterator Module::cbegin() const {
526 return const_iterator(&functions_, functions_.cbegin());
527 }
528
529 inline Module::const_iterator Module::cend() const {
530 return const_iterator(&functions_, functions_.cend());
531 }
532
533 } // namespace opt
534 } // namespace spvtools
535
536 #endif // SOURCE_OPT_MODULE_H_