Coverage Report

Created: 2025-11-16 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/spirv-tools/source/opt/ir_loader.h
Line
Count
Source
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_IR_LOADER_H_
16
#define SOURCE_OPT_IR_LOADER_H_
17
18
#include <memory>
19
#include <string>
20
#include <vector>
21
22
#include "source/opt/basic_block.h"
23
#include "source/opt/graph.h"
24
#include "source/opt/instruction.h"
25
#include "source/opt/module.h"
26
#include "spirv-tools/libspirv.hpp"
27
28
namespace spvtools {
29
namespace opt {
30
31
// Loader class for constructing SPIR-V in-memory IR representation. Methods in
32
// this class are designed to work with the interface for spvBinaryParse() in
33
// libspirv.h so that we can leverage the syntax checks implemented behind it.
34
//
35
// The user is expected to call SetModuleHeader() to fill in the module's
36
// header, and then AddInstruction() for each decoded instruction, and finally
37
// EndModule() to finalize the module. The instructions processed in sequence
38
// by AddInstruction() should comprise a valid SPIR-V module.
39
class IrLoader {
40
 public:
41
  // Instantiates a builder to construct the given |module| gradually.
42
  // All internal messages will be communicated to the outside via the given
43
  // message |consumer|. This instance only keeps a reference to the |consumer|,
44
  // so the |consumer| should outlive this instance.
45
  IrLoader(const MessageConsumer& consumer, Module* m);
46
47
  // Sets the source name of the module.
48
0
  void SetSource(const std::string& src) { source_ = src; }
49
50
21.4M
  Module* module() const { return module_; }
51
52
  // Sets the fields in the module's header to the given parameters.
53
  void SetModuleHeader(uint32_t magic, uint32_t version, uint32_t generator,
54
45.5k
                       uint32_t bound, uint32_t reserved) {
55
45.5k
    module_->SetHeader({magic, version, generator, bound, reserved});
56
45.5k
  }
57
  // Adds an instruction to the module. Returns true if no error occurs. This
58
  // method will properly capture and store the data provided in |inst| so that
59
  // |inst| is no longer needed after returning.
60
  bool AddInstruction(const spv_parsed_instruction_t* inst);
61
  // Finalizes the module construction. This must be called after the module
62
  // header has been set and all instructions have been added.  This is
63
  // forgiving in the case of a missing terminator instruction on a basic block,
64
  // or a missing OpFunctionEnd.  Resolves internal bookkeeping.
65
  void EndModule();
66
67
  // Sets whether extra OpLine instructions should be injected to better
68
  // track line information.
69
45.7k
  void SetExtraLineTracking(bool flag) { extra_line_tracking_ = flag; }
70
71
 private:
72
  // Consumer for communicating messages to outside.
73
  const MessageConsumer& consumer_;
74
  // The module to be built.
75
  Module* module_;
76
  // The source name of the module.
77
  std::string source_;
78
  // The last used instruction index.
79
  uint32_t inst_index_;
80
  // The current Function under construction.
81
  std::unique_ptr<Function> function_;
82
  // The current BasicBlock under construction.
83
  std::unique_ptr<BasicBlock> block_;
84
  // The current Graph under construction.
85
  std::unique_ptr<Graph> graph_;
86
  // Line related debug instructions accumulated thus far.
87
  std::vector<Instruction> dbg_line_info_;
88
  // If doing extra line tracking, this is the line instruction that should be
89
  // applied to the next instruction.  Otherwise it always contains null.
90
  std::unique_ptr<Instruction> last_line_inst_;
91
92
  // The last DebugScope information that IrLoader::AddInstruction() handled.
93
  DebugScope last_dbg_scope_;
94
95
  // When true, do extra line information tracking: Additional OpLine
96
  // instructions will be injected to help track line info more robustly during
97
  // transformations.
98
  bool extra_line_tracking_ = true;
99
};
100
101
}  // namespace opt
102
}  // namespace spvtools
103
104
#endif  // SOURCE_OPT_IR_LOADER_H_