• 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 #ifndef LIBSPIRV_OPT_IR_LOADER_H_
16 #define LIBSPIRV_OPT_IR_LOADER_H_
17 
18 #include <memory>
19 
20 #include "basic_block.h"
21 #include "instruction.h"
22 #include "module.h"
23 #include "spirv-tools/libspirv.hpp"
24 
25 namespace spvtools {
26 namespace ir {
27 
28 // Loader class for constructing SPIR-V in-memory IR representation. Methods in
29 // this class are designed to work with the interface for spvBinaryParse() in
30 // libspirv.h so that we can leverage the syntax checks implemented behind it.
31 //
32 // The user is expected to call SetModuleHeader() to fill in the module's
33 // header, and then AddInstruction() for each decoded instruction, and finally
34 // EndModule() to finalize the module. The instructions processed in sequence
35 // by AddInstruction() should comprise a valid SPIR-V module.
36 class IrLoader {
37  public:
38   // Instantiates a builder to construct the given |module| gradually.
39   // All internal messages will be communicated to the outside via the given
40   // message |consumer|. This instance only keeps a reference to the |consumer|,
41   // so the |consumer| should outlive this instance.
42   IrLoader(const MessageConsumer& consumer, Module* module);
43 
44   // Sets the source name of the module.
SetSource(const std::string & src)45   void SetSource(const std::string& src) { source_ = src; }
46 
47   // Sets the fields in the module's header to the given parameters.
SetModuleHeader(uint32_t magic,uint32_t version,uint32_t generator,uint32_t bound,uint32_t reserved)48   void SetModuleHeader(uint32_t magic, uint32_t version, uint32_t generator,
49                        uint32_t bound, uint32_t reserved) {
50     module_->SetHeader({magic, version, generator, bound, reserved});
51   }
52   // Adds an instruction to the module. Returns true if no error occurs. This
53   // method will properly capture and store the data provided in |inst| so that
54   // |inst| is no longer needed after returning.
55   bool AddInstruction(const spv_parsed_instruction_t* inst);
56   // Finalizes the module construction. This must be called after the module
57   // header has been set and all instructions have been added.  This is
58   // forgiving in the case of a missing terminator instruction on a basic block,
59   // or a missing OpFunctionEnd.  Resolves internal bookkeeping.
60   void EndModule();
61 
62  private:
63   // Consumer for communicating messages to outside.
64   const MessageConsumer& consumer_;
65   // The module to be built.
66   Module* module_;
67   // The source name of the module.
68   std::string source_;
69   // The last used instruction index.
70   uint32_t inst_index_;
71   // The current Function under construction.
72   std::unique_ptr<Function> function_;
73   // The current BasicBlock under construction.
74   std::unique_ptr<BasicBlock> block_;
75   // Line related debug instructions accumulated thus far.
76   std::vector<Instruction> dbg_line_info_;
77 };
78 
79 }  // namespace ir
80 }  // namespace spvtools
81 
82 #endif  // LIBSPIRV_OPT_IR_LOADER_H_
83