ir_loader.h 3.3 KB

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