Coverage Report

Created: 2026-04-28 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/shaderc/third_party/spirv-tools/source/opt/module.cpp
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
#include "source/opt/module.h"
16
17
#include <algorithm>
18
#include <cstring>
19
#include <ostream>
20
21
#include "source/operand.h"
22
#include "source/opt/ir_context.h"
23
#include "source/opt/reflect.h"
24
25
namespace spvtools {
26
namespace opt {
27
28
12.2k
uint32_t Module::TakeNextIdBound() {
29
12.2k
  if (context()) {
30
12.2k
    if (id_bound() >= context()->max_id_bound()) {
31
0
      return 0;
32
0
    }
33
12.2k
  } else if (id_bound() >= kDefaultMaxIdBound) {
34
0
    return 0;
35
0
  }
36
37
12.2k
  return header_.bound++;
38
12.2k
}
39
40
0
std::vector<Instruction*> Module::GetTypes() {
41
0
  std::vector<Instruction*> type_insts;
42
0
  for (auto& inst : types_values_) {
43
0
    if (IsTypeInst(inst.opcode())) type_insts.push_back(&inst);
44
0
  }
45
0
  return type_insts;
46
0
}
47
48
248
std::vector<const Instruction*> Module::GetTypes() const {
49
248
  std::vector<const Instruction*> type_insts;
50
11.4k
  for (auto& inst : types_values_) {
51
11.4k
    if (IsTypeInst(inst.opcode())) type_insts.push_back(&inst);
52
11.4k
  }
53
248
  return type_insts;
54
248
}
55
56
190
std::vector<Instruction*> Module::GetConstants() {
57
190
  std::vector<Instruction*> const_insts;
58
8.02k
  for (auto& inst : types_values_) {
59
8.02k
    if (IsConstantInst(inst.opcode())) const_insts.push_back(&inst);
60
8.02k
  }
61
190
  return const_insts;
62
190
}
63
64
248
std::vector<const Instruction*> Module::GetConstants() const {
65
248
  std::vector<const Instruction*> const_insts;
66
11.4k
  for (auto& inst : types_values_) {
67
11.4k
    if (IsConstantInst(inst.opcode())) const_insts.push_back(&inst);
68
11.4k
  }
69
248
  return const_insts;
70
248
}
71
72
0
uint32_t Module::GetGlobalValue(spv::Op opcode) const {
73
0
  for (auto& inst : types_values_) {
74
0
    if (inst.opcode() == opcode) return inst.result_id();
75
0
  }
76
0
  return 0;
77
0
}
78
79
void Module::AddGlobalValue(spv::Op opcode, uint32_t result_id,
80
0
                            uint32_t type_id) {
81
0
  std::unique_ptr<Instruction> newGlobal(
82
0
      new Instruction(context(), opcode, type_id, result_id, {}));
83
0
  AddGlobalValue(std::move(newGlobal));
84
0
}
85
86
void Module::ForEachInst(const std::function<void(Instruction*)>& f,
87
976
                         bool run_on_debug_line_insts) {
88
11.7k
#define DELEGATE(list) list.ForEachInst(f, run_on_debug_line_insts)
89
976
  DELEGATE(capabilities_);
90
976
  DELEGATE(extensions_);
91
976
  DELEGATE(ext_inst_imports_);
92
976
  if (memory_model_) memory_model_->ForEachInst(f, run_on_debug_line_insts);
93
976
  if (sampled_image_address_mode_)
94
0
    sampled_image_address_mode_->ForEachInst(f, run_on_debug_line_insts);
95
976
  DELEGATE(entry_points_);
96
976
  DELEGATE(graph_entry_points_);
97
976
  DELEGATE(execution_modes_);
98
976
  DELEGATE(debugs1_);
99
976
  DELEGATE(debugs2_);
100
976
  DELEGATE(debugs3_);
101
976
  DELEGATE(ext_inst_debuginfo_);
102
976
  DELEGATE(annotations_);
103
976
  DELEGATE(types_values_);
104
1.70k
  for (auto& i : functions_) {
105
1.70k
    i->ForEachInst(f, run_on_debug_line_insts,
106
1.70k
                   /* run_on_non_semantic_insts = */ true);
107
1.70k
  }
108
976
  for (auto& g : graphs_) {
109
0
    g->ForEachInst(f, run_on_debug_line_insts,
110
0
                   /* run_on_non_semantic_insts = */ true);
111
0
  }
112
976
#undef DELEGATE
113
976
}
114
115
void Module::ForEachInst(const std::function<void(const Instruction*)>& f,
116
256
                         bool run_on_debug_line_insts) const {
117
15.7k
#define DELEGATE(i) i.ForEachInst(f, run_on_debug_line_insts)
118
824
  for (auto& i : capabilities_) DELEGATE(i);
119
256
  for (auto& i : extensions_) DELEGATE(i);
120
264
  for (auto& i : ext_inst_imports_) DELEGATE(i);
121
256
  if (memory_model_)
122
256
    static_cast<const Instruction*>(memory_model_.get())
123
256
        ->ForEachInst(f, run_on_debug_line_insts);
124
256
  if (sampled_image_address_mode_)
125
0
    static_cast<const Instruction*>(sampled_image_address_mode_.get())
126
0
        ->ForEachInst(f, run_on_debug_line_insts);
127
256
  for (auto& i : entry_points_) DELEGATE(i);
128
256
  for (auto& i : execution_modes_) DELEGATE(i);
129
256
  for (auto& i : debugs1_) DELEGATE(i);
130
256
  for (auto& i : debugs2_) DELEGATE(i);
131
256
  for (auto& i : debugs3_) DELEGATE(i);
132
3.22k
  for (auto& i : annotations_) DELEGATE(i);
133
7.58k
  for (auto& i : types_values_) DELEGATE(i);
134
256
  for (auto& i : ext_inst_debuginfo_) DELEGATE(i);
135
256
  for (auto& i : functions_) {
136
256
    static_cast<const Function*>(i.get())->ForEachInst(
137
256
        f, run_on_debug_line_insts,
138
256
        /* run_on_non_semantic_insts = */ true);
139
256
  }
140
256
  for (auto& i : graph_entry_points_) DELEGATE(i);
141
256
  for (auto& i : graphs_) {
142
0
    static_cast<const Graph*>(i.get())->ForEachInst(
143
0
        f, run_on_debug_line_insts,
144
0
        /* run_on_non_semantic_insts = */ true);
145
0
  }
146
256
  if (run_on_debug_line_insts) {
147
256
    for (auto& i : trailing_dbg_line_info_) DELEGATE(i);
148
256
  }
149
256
#undef DELEGATE
150
256
}
151
152
128
void Module::ToBinary(std::vector<uint32_t>* binary, bool skip_nop) const {
153
128
  binary->push_back(header_.magic_number);
154
128
  binary->push_back(header_.version);
155
  // TODO(antiagainst): should we change the generator number?
156
128
  binary->push_back(header_.generator);
157
128
  binary->push_back(header_.bound);
158
128
  binary->push_back(header_.schema);
159
160
128
  size_t bound_idx = binary->size() - 2;
161
128
  DebugScope last_scope(kNoDebugScope, kNoInlinedAt);
162
128
  const Instruction* last_line_inst = nullptr;
163
128
  bool between_merge_and_branch = false;
164
128
  bool between_label_and_phi_var = false;
165
128
  auto write_inst = [binary, skip_nop, &last_scope, &last_line_inst,
166
128
                     &between_merge_and_branch, &between_label_and_phi_var,
167
24.5k
                     this](const Instruction* i) {
168
    // Skip emitting line instructions between merge and branch instructions.
169
24.5k
    auto opcode = i->opcode();
170
24.5k
    if (between_merge_and_branch && i->IsLineInst()) {
171
0
      return;
172
0
    }
173
24.5k
    if (last_line_inst != nullptr) {
174
      // If the current instruction is OpLine or DebugLine and it is the same
175
      // as the last line instruction that is still effective (can be applied
176
      // to the next instruction), we skip writing the current instruction.
177
0
      if (i->IsLine()) {
178
0
        uint32_t operand_index = 0;
179
0
        if (last_line_inst->WhileEachInOperand(
180
0
                [&operand_index, i](const uint32_t* word) {
181
0
                  assert(i->NumInOperandWords() > operand_index);
182
0
                  return *word == i->GetSingleWordInOperand(operand_index++);
183
0
                })) {
184
0
          return;
185
0
        }
186
0
      } else if (!i->IsNoLine() && i->dbg_line_insts().empty()) {
187
        // If the current instruction does not have the line information,
188
        // the last line information is not effective any more. Emit OpNoLine
189
        // or DebugNoLine to specify it.
190
0
        uint32_t shader_set_id =
191
0
            context()->get_feature_mgr()->GetExtInstImportId_ShaderDebugInfo();
192
0
        if (shader_set_id != 0) {
193
0
          binary->push_back((5 << 16) |
194
0
                            static_cast<uint16_t>(spv::Op::OpExtInst));
195
0
          binary->push_back(context()->get_type_mgr()->GetVoidTypeId());
196
0
          binary->push_back(context()->TakeNextId());
197
0
          binary->push_back(shader_set_id);
198
0
          binary->push_back(NonSemanticShaderDebugInfoDebugNoLine);
199
0
        } else {
200
0
          binary->push_back((1 << 16) |
201
0
                            static_cast<uint16_t>(spv::Op::OpNoLine));
202
0
        }
203
0
        last_line_inst = nullptr;
204
0
      }
205
0
    }
206
207
24.5k
    if (opcode == spv::Op::OpLabel) {
208
2.27k
      between_label_and_phi_var = true;
209
22.3k
    } else if (opcode != spv::Op::OpVariable && opcode != spv::Op::OpPhi &&
210
21.1k
               !spvtools::opt::IsOpLineInst(opcode)) {
211
21.1k
      between_label_and_phi_var = false;
212
21.1k
    }
213
214
24.5k
    if (!(skip_nop && i->IsNop())) {
215
24.5k
      const auto& scope = i->GetDebugScope();
216
24.5k
      if (scope != last_scope && !between_merge_and_branch) {
217
        // Can only emit nonsemantic instructions after all phi instructions
218
        // in a block so don't emit scope instructions before phi instructions
219
        // for NonSemantic.Shader.DebugInfo.
220
0
        if (!between_label_and_phi_var ||
221
0
            context()
222
0
                ->get_feature_mgr()
223
0
                ->GetExtInstImportId_OpenCL100DebugInfo()) {
224
          // Emit DebugScope |scope| to |binary|.
225
0
          auto dbg_inst = ext_inst_debuginfo_.begin();
226
0
          scope.ToBinary(dbg_inst->type_id(), context()->TakeNextId(),
227
0
                         dbg_inst->GetSingleWordOperand(2), binary);
228
0
        }
229
0
        last_scope = scope;
230
0
      }
231
232
24.5k
      i->ToBinaryWithoutAttachedDebugInsts(binary);
233
24.5k
    }
234
    // Update the last line instruction.
235
24.5k
    between_merge_and_branch = false;
236
24.5k
    if (spvOpcodeIsBlockTerminator(opcode) || i->IsNoLine()) {
237
2.27k
      last_line_inst = nullptr;
238
22.3k
    } else if (opcode == spv::Op::OpLoopMerge ||
239
22.1k
               opcode == spv::Op::OpSelectionMerge) {
240
866
      between_merge_and_branch = true;
241
866
      last_line_inst = nullptr;
242
21.4k
    } else if (i->IsLine()) {
243
0
      last_line_inst = i;
244
0
    }
245
24.5k
  };
246
128
  ForEachInst(write_inst, true);
247
248
  // We create new instructions for DebugScope and DebugNoLine. The bound must
249
  // be updated.
250
128
  binary->data()[bound_idx] = header_.bound;
251
128
}
252
253
128
uint32_t Module::ComputeIdBound() const {
254
128
  uint32_t highest = 0;
255
256
128
  ForEachInst(
257
24.5k
      [&highest](const Instruction* inst) {
258
76.7k
        for (const auto& operand : *inst) {
259
76.7k
          if (spvIsIdType(operand.type)) {
260
64.6k
            highest = std::max(highest, operand.words[0]);
261
64.6k
          }
262
76.7k
        }
263
24.5k
      },
264
128
      true /* scan debug line insts as well */);
265
266
128
  return highest + 1;
267
128
}
268
269
0
bool Module::HasExplicitCapability(uint32_t cap) {
270
0
  for (auto& ci : capabilities_) {
271
0
    uint32_t tcap = ci.GetSingleWordOperand(0);
272
0
    if (tcap == cap) {
273
0
      return true;
274
0
    }
275
0
  }
276
0
  return false;
277
0
}
278
279
256
uint32_t Module::GetExtInstImportId(const char* extstr) {
280
256
  for (auto& ei : ext_inst_imports_)
281
260
    if (!ei.GetInOperand(0).AsString().compare(extstr)) return ei.result_id();
282
128
  return 0;
283
256
}
284
285
0
std::ostream& operator<<(std::ostream& str, const Module& module) {
286
0
  module.ForEachInst([&str](const Instruction* inst) {
287
0
    str << *inst;
288
0
    if (inst->opcode() != spv::Op::OpFunctionEnd) {
289
0
      str << std::endl;
290
0
    }
291
0
  });
292
0
  return str;
293
0
}
294
295
}  // namespace opt
296
}  // namespace spvtools