2
0

ir_loader.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. // Sets whether extra OpLine instructions should be injected to better
  58. // track line information.
  59. void SetExtraLineTracking(bool flag) { extra_line_tracking_ = flag; }
  60. private:
  61. // Consumer for communicating messages to outside.
  62. const MessageConsumer& consumer_;
  63. // The module to be built.
  64. Module* module_;
  65. // The source name of the module.
  66. std::string source_;
  67. // The last used instruction index.
  68. uint32_t inst_index_;
  69. // The current Function under construction.
  70. std::unique_ptr<Function> function_;
  71. // The current BasicBlock under construction.
  72. std::unique_ptr<BasicBlock> block_;
  73. // Line related debug instructions accumulated thus far.
  74. std::vector<Instruction> dbg_line_info_;
  75. // If doing extra line tracking, this is the line instruction that should be
  76. // applied to the next instruction. Otherwise it always contains null.
  77. std::unique_ptr<Instruction> last_line_inst_;
  78. // The last DebugScope information that IrLoader::AddInstruction() handled.
  79. DebugScope last_dbg_scope_;
  80. // When true, do extra line information tracking: Additional OpLine
  81. // instructions will be injected to help track line info more robustly during
  82. // transformations.
  83. bool extra_line_tracking_ = true;
  84. };
  85. } // namespace opt
  86. } // namespace spvtools
  87. #endif // SOURCE_OPT_IR_LOADER_H_